Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_manager.h
bloba2cad0d4d3afae1383de7eec01c886ccf5b202b0
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 ServiceWorkerContextWrapper;
26 // BackgroundSyncManager manages and stores the set of background sync
27 // registrations across all registered service workers for a profile.
28 // Registrations are stored along with their associated Service Worker
29 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered,
30 // the sync registrations are removed. This class expects to be run on the IO
31 // thread. The asynchronous methods are executed sequentially.
33 // TODO(jkarlin): Check permissions when registering, scheduling, and firing
34 // background sync. In the meantime, --enable-service-worker-sync is required to
35 // fire a sync event.
36 // TODO(jkarlin): Unregister syncs when permission is revoked.
37 // TODO(jkarlin): Create a background sync scheduler to actually run the
38 // registered events.
39 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the
40 // Background" is true and a sync is registered.
41 class CONTENT_EXPORT BackgroundSyncManager
42 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) {
43 public:
44 enum ErrorType {
45 ERROR_TYPE_OK = 0,
46 ERROR_TYPE_STORAGE,
47 ERROR_TYPE_NOT_FOUND
50 // TODO(jkarlin): Remove this and use the struct from IPC messages once it
51 // lands.
52 struct CONTENT_EXPORT BackgroundSyncRegistration {
53 using RegistrationId = int64;
54 static const RegistrationId kInvalidRegistrationId;
55 BackgroundSyncRegistration() {}
57 bool Equals(const BackgroundSyncRegistration& other) {
58 return this->name == other.name && this->fire_once == other.fire_once &&
59 this->min_period == other.min_period &&
60 network_state == other.network_state &&
61 power_state == other.power_state;
64 RegistrationId id = kInvalidRegistrationId;
65 std::string name;
66 bool fire_once = true;
67 int64 min_period = 0;
68 SyncNetworkState network_state = NETWORK_STATE_ONLINE;
69 SyncPowerState power_state = POWER_STATE_AVOID_DRAINING;
72 struct CONTENT_EXPORT BackgroundSyncRegistrations {
73 using NameToRegistrationMap =
74 std::map<std::string, BackgroundSyncRegistration>;
75 static const BackgroundSyncRegistration::RegistrationId kInitialId;
77 BackgroundSyncRegistrations();
78 explicit BackgroundSyncRegistrations(
79 BackgroundSyncRegistration::RegistrationId next_id);
80 ~BackgroundSyncRegistrations();
82 NameToRegistrationMap name_to_registration_map;
83 BackgroundSyncRegistration::RegistrationId next_id;
86 using StatusCallback = base::Callback<void(ErrorType)>;
87 using StatusAndRegistrationCallback =
88 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>;
90 static scoped_ptr<BackgroundSyncManager> Create(
91 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
92 ~BackgroundSyncManager() override;
94 // Stores the given background sync registration and adds it to the scheduling
95 // queue. Overwrites any existing registration with the same name but
96 // different parameters (other than the id). Calls |callback| with ErrorTypeOK
97 // and the accepted registration on success. The accepted registration will
98 // have a unique id. It may also have altered parameters if the user or UA
99 // chose different parameters than those supplied.
100 void Register(const GURL& origin,
101 int64 sw_registration_id,
102 const BackgroundSyncRegistration& sync_registration,
103 const StatusAndRegistrationCallback& callback);
105 // Removes the background sync registration with |sync_registration_name| if
106 // the |sync_registration_id| matches. |sync_registration_id| will not match
107 // if, for instance, a new registration with the same name has replaced it.
108 // Calls |callback| with ErrorTypeNotFound if no match is found. Calls
109 // |callback| with ErrorTypeOK on success.
110 void Unregister(
111 const GURL& origin,
112 int64 sw_registration_id,
113 const std::string& sync_registration_name,
114 BackgroundSyncRegistration::RegistrationId sync_registration_id,
115 const StatusCallback& callback);
117 // Finds the background sync registration associated with
118 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't
119 // exist. Calls |callback| with ErrorTypeOK on success.
120 void GetRegistration(const GURL& origin,
121 int64 sw_registration_id,
122 const std::string sync_registration_name,
123 const StatusAndRegistrationCallback& callback);
125 // ServiceWorkerContextObserver overrides.
126 void OnRegistrationDeleted(int64 registration_id,
127 const GURL& pattern) override;
128 void OnStorageWiped() override;
130 protected:
131 explicit BackgroundSyncManager(
132 const scoped_refptr<ServiceWorkerContextWrapper>& context);
134 // Init must be called before any public member function. Only call it once.
135 void Init();
137 // The following methods are virtual for testing.
138 virtual void StoreDataInBackend(
139 int64 sw_registration_id,
140 const GURL& origin,
141 const std::string& key,
142 const std::string& data,
143 const ServiceWorkerStorage::StatusCallback& callback);
144 virtual void GetDataFromBackend(
145 const std::string& key,
146 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
147 callback);
149 private:
150 using PermissionStatusCallback = base::Callback<void(bool)>;
151 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>;
153 // Disable the manager. Already queued operations will abort once they start
154 // to run (in their impl methods). Future operations will not queue. Any
155 // registrations are cleared from memory and the backend (if it's still
156 // functioning). The manager will reenable itself once it receives the
157 // OnStorageWiped message or on browser restart.
158 void DisableAndClearManager(const base::Closure& callback);
159 void DisableAndClearDidGetRegistrations(
160 const base::Closure& callback,
161 const std::vector<std::pair<int64, std::string>>& user_data,
162 ServiceWorkerStatusCode status);
163 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure,
164 ServiceWorkerStatusCode status);
166 // Returns the existing registration in |existing_registration| if it is not
167 // null.
168 bool LookupRegistration(int64 sw_registration_id,
169 const std::string& sync_registration_name,
170 BackgroundSyncRegistration* existing_registration);
172 // Store all registrations for a given |sw_registration_id|.
173 void StoreRegistrations(const GURL& origin,
174 int64 sw_registration_id,
175 const ServiceWorkerStorage::StatusCallback& callback);
177 // If the registration is in the map, removes it and returns the removed
178 // registration in |old_registration|. |old_registration| may be null.
179 void RemoveRegistrationFromMap(int64 sw_registration_id,
180 const std::string& sync_registration_name,
181 BackgroundSyncRegistration* old_registration);
183 void AddRegistrationToMap(
184 int64 sw_registration_id,
185 const BackgroundSyncRegistration& sync_registration);
187 void InitImpl(const base::Closure& callback);
188 void InitDidGetDataFromBackend(
189 const base::Closure& callback,
190 const std::vector<std::pair<int64, std::string>>& user_data,
191 ServiceWorkerStatusCode status);
193 // Register callbacks
194 void RegisterImpl(const GURL& origin,
195 int64 sw_registration_id,
196 const BackgroundSyncRegistration& sync_registration,
197 const StatusAndRegistrationCallback& callback);
198 void RegisterDidStore(int64 sw_registration_id,
199 const BackgroundSyncRegistration& sync_registration,
200 const StatusAndRegistrationCallback& callback,
201 ServiceWorkerStatusCode status);
203 // Unregister callbacks
204 void UnregisterImpl(
205 const GURL& origin,
206 int64 sw_registration_id,
207 const std::string& sync_registration_name,
208 BackgroundSyncRegistration::RegistrationId sync_registration_id,
209 const StatusCallback& callback);
210 void UnregisterDidStore(
211 int64 sw_registration_id,
212 const BackgroundSyncRegistration& old_sync_registration,
213 const StatusCallback& callback,
214 ServiceWorkerStatusCode status);
216 // GetRegistration callbacks
217 void GetRegistrationImpl(const GURL& origin,
218 int64 sw_registration_id,
219 const std::string sync_registration_name,
220 const StatusAndRegistrationCallback& callback);
222 // OnRegistrationDeleted callbacks
223 void OnRegistrationDeletedImpl(int64 registration_id,
224 const base::Closure& callback);
226 // OnStorageWiped callbacks
227 void OnStorageWipedImpl(const base::Closure& callback);
229 // Operation Scheduling callbacks
230 void PendingStatusAndRegistrationCallback(
231 const StatusAndRegistrationCallback& callback,
232 ErrorType error,
233 const BackgroundSyncRegistration& sync_registration);
234 void PendingStatusCallback(const StatusCallback& callback, ErrorType error);
235 void PendingClosure(const base::Closure& closure);
237 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion(
238 const StatusAndRegistrationCallback& callback);
239 BackgroundSyncManager::StatusCallback MakeStatusCompletion(
240 const StatusCallback& callback);
241 base::Closure MakeEmptyCompletion();
243 SWIdToRegistrationsMap sw_to_registrations_map_;
244 CacheStorageScheduler op_scheduler_;
245 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
246 bool disabled_;
248 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_;
250 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager);
253 } // namespace content
255 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_