[BackgroundSync] Make BackgroundSyncManager::LookupRegistration return a pointer...
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_manager.h
blobd9925a6858c87dfe22a281e41040954dedd05c6e
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_MANAGER_H_
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
8 #include <list>
9 #include <map>
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/browser/background_sync/background_sync.pb.h"
15 #include "content/browser/cache_storage/cache_storage_scheduler.h"
16 #include "content/browser/service_worker/service_worker_context_observer.h"
17 #include "content/browser/service_worker/service_worker_storage.h"
18 #include "content/common/content_export.h"
19 #include "content/common/service_worker/service_worker_status_code.h"
20 #include "url/gurl.h"
22 namespace content {
24 class BackgroundSyncNetworkObserver;
25 class ServiceWorkerContextWrapper;
27 // BackgroundSyncManager manages and stores the set of background sync
28 // registrations across all registered service workers for a profile.
29 // Registrations are stored along with their associated Service Worker
30 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered,
31 // the sync registrations are removed. This class expects to be run on the IO
32 // thread. The asynchronous methods are executed sequentially.
34 // TODO(jkarlin): Check permissions when registering, scheduling, and firing
35 // background sync. In the meantime, --enable-service-worker-sync is required to
36 // fire a sync event.
37 // TODO(jkarlin): Unregister syncs when permission is revoked.
38 // TODO(jkarlin): Create a background sync scheduler to actually run the
39 // registered events.
40 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the
41 // Background" is true and a sync is registered.
42 class CONTENT_EXPORT BackgroundSyncManager
43 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) {
44 public:
45 enum ErrorType {
46 ERROR_TYPE_OK = 0,
47 ERROR_TYPE_STORAGE,
48 ERROR_TYPE_NOT_FOUND
51 // TODO(jkarlin): Remove this and use the struct from IPC messages once it
52 // lands.
53 struct CONTENT_EXPORT BackgroundSyncRegistration {
54 using RegistrationId = int64;
55 static const RegistrationId kInvalidRegistrationId;
56 static const RegistrationId kInitialId;
57 BackgroundSyncRegistration() {}
59 bool Equals(const BackgroundSyncRegistration& other) const {
60 return this->tag == other.tag && this->periodicity == other.periodicity &&
61 this->min_period == other.min_period &&
62 network_state == other.network_state &&
63 power_state == other.power_state;
66 RegistrationId id = kInvalidRegistrationId;
67 std::string tag;
68 SyncPeriodicity periodicity = SYNC_ONE_SHOT;
69 int64 min_period = 0;
70 SyncNetworkState network_state = NETWORK_STATE_ONLINE;
71 SyncPowerState power_state = POWER_STATE_AVOID_DRAINING;
74 using StatusCallback = base::Callback<void(ErrorType)>;
75 using StatusAndRegistrationCallback =
76 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>;
78 static scoped_ptr<BackgroundSyncManager> Create(
79 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
80 ~BackgroundSyncManager() override;
82 // Stores the given background sync registration and adds it to the scheduling
83 // queue. It will overwrite an existing registration with the same tag and
84 // periodicity unless they're identical (save for the id). Calls |callback|
85 // with ErrorTypeOK and the accepted registration on success. The accepted
86 // registration will have a unique id. It may also have altered parameters if
87 // the user or UA chose different parameters than those supplied.
88 void Register(const GURL& origin,
89 int64 sw_registration_id,
90 const BackgroundSyncRegistration& sync_registration,
91 const StatusAndRegistrationCallback& callback);
93 // Removes the background sync with tag |sync_registration_tag|, periodicity
94 // |periodicity|, and id |sync_registration_id|. Calls |callback| with
95 // ErrorTypeNotFound if no match is found. Calls |callback| with ErrorTypeOK
96 // on success.
97 void Unregister(
98 const GURL& origin,
99 int64 sw_registration_id,
100 const std::string& sync_registration_tag,
101 SyncPeriodicity periodicity,
102 BackgroundSyncRegistration::RegistrationId sync_registration_id,
103 const StatusCallback& callback);
105 // Finds the background sync registration associated with
106 // |sw_registration_id| with periodicity |periodicity|. Calls
107 // |callback| with ErrorTypeNotFound if it doesn't exist. Calls |callback|
108 // with ErrorTypeOK on success.
109 void GetRegistration(const GURL& origin,
110 int64 sw_registration_id,
111 const std::string sync_registration_tag,
112 SyncPeriodicity periodicity,
113 const StatusAndRegistrationCallback& callback);
115 // ServiceWorkerContextObserver overrides.
116 void OnRegistrationDeleted(int64 registration_id,
117 const GURL& pattern) override;
118 void OnStorageWiped() override;
120 protected:
121 explicit BackgroundSyncManager(
122 const scoped_refptr<ServiceWorkerContextWrapper>& context);
124 // Init must be called before any public member function. Only call it once.
125 void Init();
127 // The following methods are virtual for testing.
128 virtual void StoreDataInBackend(
129 int64 sw_registration_id,
130 const GURL& origin,
131 const std::string& backend_key,
132 const std::string& data,
133 const ServiceWorkerStorage::StatusCallback& callback);
134 virtual void GetDataFromBackend(
135 const std::string& backend_key,
136 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
137 callback);
139 private:
140 class RegistrationKey {
141 public:
142 explicit RegistrationKey(const BackgroundSyncRegistration& registration);
143 RegistrationKey(const std::string& tag, SyncPeriodicity periodicity);
144 RegistrationKey(const RegistrationKey& other) = default;
145 RegistrationKey& operator=(const RegistrationKey& other) = default;
147 bool operator<(const RegistrationKey& rhs) const {
148 return value_ < rhs.value_;
151 private:
152 std::string value_;
155 struct BackgroundSyncRegistrations {
156 using RegistrationMap =
157 std::map<RegistrationKey, BackgroundSyncRegistration>;
159 BackgroundSyncRegistrations();
160 explicit BackgroundSyncRegistrations(
161 BackgroundSyncRegistration::RegistrationId next_id);
162 ~BackgroundSyncRegistrations();
164 RegistrationMap registration_map;
165 BackgroundSyncRegistration::RegistrationId next_id;
168 using PermissionStatusCallback = base::Callback<void(bool)>;
169 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>;
171 // Disable the manager. Already queued operations will abort once they start
172 // to run (in their impl methods). Future operations will not queue. Any
173 // registrations are cleared from memory and the backend (if it's still
174 // functioning). The manager will reenable itself once it receives the
175 // OnStorageWiped message or on browser restart.
176 void DisableAndClearManager(const base::Closure& callback);
177 void DisableAndClearDidGetRegistrations(
178 const base::Closure& callback,
179 const std::vector<std::pair<int64, std::string>>& user_data,
180 ServiceWorkerStatusCode status);
181 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure,
182 ServiceWorkerStatusCode status);
184 // Returns the existing registration in |existing_registration| if it is not
185 // null.
186 BackgroundSyncRegistration* LookupRegistration(
187 int64 sw_registration_id,
188 const RegistrationKey& registration_key);
190 // Store all registrations for a given |sw_registration_id|.
191 void StoreRegistrations(const GURL& origin,
192 int64 sw_registration_id,
193 const ServiceWorkerStorage::StatusCallback& callback);
195 // Removes the registration if it is in the map.
196 void RemoveRegistrationFromMap(int64 sw_registration_id,
197 const RegistrationKey& registration_key);
199 void AddRegistrationToMap(
200 int64 sw_registration_id,
201 const BackgroundSyncRegistration& sync_registration);
203 void InitImpl(const base::Closure& callback);
204 void InitDidGetDataFromBackend(
205 const base::Closure& callback,
206 const std::vector<std::pair<int64, std::string>>& user_data,
207 ServiceWorkerStatusCode status);
209 // Register callbacks
210 void RegisterImpl(const GURL& origin,
211 int64 sw_registration_id,
212 const BackgroundSyncRegistration& sync_registration,
213 const StatusAndRegistrationCallback& callback);
214 void RegisterDidStore(int64 sw_registration_id,
215 const BackgroundSyncRegistration& sync_registration,
216 const StatusAndRegistrationCallback& callback,
217 ServiceWorkerStatusCode status);
219 // Unregister callbacks
220 void UnregisterImpl(
221 const GURL& origin,
222 int64 sw_registration_id,
223 const RegistrationKey& registration_key,
224 BackgroundSyncRegistration::RegistrationId sync_registration_id,
225 const StatusCallback& callback);
226 void UnregisterDidStore(
227 int64 sw_registration_id,
228 const StatusCallback& callback,
229 ServiceWorkerStatusCode status);
231 // GetRegistration callbacks
232 void GetRegistrationImpl(const GURL& origin,
233 int64 sw_registration_id,
234 const RegistrationKey& registration_key,
235 const StatusAndRegistrationCallback& callback);
237 // OnRegistrationDeleted callbacks
238 void OnRegistrationDeletedImpl(int64 registration_id,
239 const base::Closure& callback);
241 // OnStorageWiped callbacks
242 void OnStorageWipedImpl(const base::Closure& callback);
244 void OnNetworkChanged();
246 // Operation Scheduling callbacks
247 void PendingStatusAndRegistrationCallback(
248 const StatusAndRegistrationCallback& callback,
249 ErrorType error,
250 const BackgroundSyncRegistration& sync_registration);
251 void PendingStatusCallback(const StatusCallback& callback, ErrorType error);
252 void PendingClosure(const base::Closure& closure);
254 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion(
255 const StatusAndRegistrationCallback& callback);
256 BackgroundSyncManager::StatusCallback MakeStatusCompletion(
257 const StatusCallback& callback);
258 base::Closure MakeEmptyCompletion();
260 SWIdToRegistrationsMap sw_to_registrations_map_;
261 CacheStorageScheduler op_scheduler_;
262 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
263 bool disabled_;
265 scoped_ptr<BackgroundSyncNetworkObserver> network_observer_;
267 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_;
269 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager);
272 } // namespace content
274 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_