Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_registration_handle.h
blobf2915d19dbb89f6bf0048c1226bafca254d81097
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_REGISTRATION_HANDLE_H_
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_REGISTRATION_HANDLE_H_
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/background_sync/background_sync_registration.h"
12 #include "content/browser/background_sync/background_sync_status.h"
13 #include "content/common/background_sync_service.mojom.h"
14 #include "content/common/content_export.h"
16 namespace content {
18 class BackgroundSyncManager;
20 // Handle to BackgroundSyncRegistration that is exposed to clients. Each
21 // BackgroundSyncRegistrationHandle is given a unique handle id (by the
22 // BackgroundSyncManager) which is released at destruction.
23 // BackgroundSyncRegistrationHandle objects must not be used (but may be
24 // destroyed) after the BackgroundSyncManager has been deleted.
25 class CONTENT_EXPORT BackgroundSyncRegistrationHandle {
26 public:
27 using HandleId = int;
28 using StatusCallback = base::Callback<void(BackgroundSyncStatus)>;
29 using StatusAndStateCallback =
30 base::Callback<void(BackgroundSyncStatus, BackgroundSyncState)>;
32 ~BackgroundSyncRegistrationHandle();
34 const BackgroundSyncRegistrationOptions* options() const {
35 DCHECK(background_sync_manager_);
36 return registration_->options();
39 BackgroundSyncState sync_state() const {
40 DCHECK(background_sync_manager_);
41 return registration_->sync_state();
44 // Unregisters the background sync registration. Calls |callback|
45 // with BACKGROUND_SYNC_STATUS_OK if it succeeds.
46 void Unregister(int64_t service_worker_id, const StatusCallback& callback);
48 // Runs |callback| when the registration associated with |handle_id|
49 // completes.The provided status is BACKGROUND_SYNC_STATUS_OK if the operation
50 // succeeded. The provided state is BACKGROUND_SYNC_STATE_SUCCESS on success,
51 // BACKGRUOND_SYNC_STATE_FAILED on final failure, and
52 // BACKGROUND_SYNC_STATE_UNREGISTERED if the registration was unregistered
53 // before it could complete. NotifyWhenDone should only be called for
54 // SYNC_ONE_SHOT registrations.
55 void NotifyWhenDone(const StatusAndStateCallback& callback);
57 // Returns true if the handle is backed by a BackgroundSyncRegistration in the
58 // BackgroundSyncManager.
59 bool IsValid() const;
61 HandleId handle_id() const { return handle_id_; }
63 private:
64 friend class BackgroundSyncManager;
66 BackgroundSyncRegistrationHandle(
67 base::WeakPtr<BackgroundSyncManager> background_sync_manager,
68 HandleId handle_id);
70 BackgroundSyncRegistration* registration() {
71 DCHECK(background_sync_manager_);
72 return registration_;
75 // The BackgroundSyncManager is expected to remain alive for all operations
76 // except for possibly at destruction.
77 base::WeakPtr<BackgroundSyncManager> background_sync_manager_;
79 // Each BackgroundSyncRegistrationHandle is assigned a unique handle id.
80 // The BackgroundSyncManager maps the id to an internal pointer.
81 HandleId handle_id_;
83 // This is owned by background_sync_manager_ and is valid until handle_id_ is
84 // released in the destructor or background_sync_manager_ has been destroyed.
85 BackgroundSyncRegistration* registration_;
87 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncRegistrationHandle);
90 } // namespace
92 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_REGISTRATION_HANDLE_H_