Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_manager.h
blobca910bfd030ed3e3c83159bf76606733844651ce
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 // Removes the registration if it is in the map.
178 void RemoveRegistrationFromMap(int64 sw_registration_id,
179 const std::string& sync_registration_name);
181 void AddRegistrationToMap(
182 int64 sw_registration_id,
183 const BackgroundSyncRegistration& sync_registration);
185 void InitImpl(const base::Closure& callback);
186 void InitDidGetDataFromBackend(
187 const base::Closure& callback,
188 const std::vector<std::pair<int64, std::string>>& user_data,
189 ServiceWorkerStatusCode status);
191 // Register callbacks
192 void RegisterImpl(const GURL& origin,
193 int64 sw_registration_id,
194 const BackgroundSyncRegistration& sync_registration,
195 const StatusAndRegistrationCallback& callback);
196 void RegisterDidStore(int64 sw_registration_id,
197 const BackgroundSyncRegistration& sync_registration,
198 const StatusAndRegistrationCallback& callback,
199 ServiceWorkerStatusCode status);
201 // Unregister callbacks
202 void UnregisterImpl(
203 const GURL& origin,
204 int64 sw_registration_id,
205 const std::string& sync_registration_name,
206 BackgroundSyncRegistration::RegistrationId sync_registration_id,
207 const StatusCallback& callback);
208 void UnregisterDidStore(
209 int64 sw_registration_id,
210 const StatusCallback& callback,
211 ServiceWorkerStatusCode status);
213 // GetRegistration callbacks
214 void GetRegistrationImpl(const GURL& origin,
215 int64 sw_registration_id,
216 const std::string sync_registration_name,
217 const StatusAndRegistrationCallback& callback);
219 // OnRegistrationDeleted callbacks
220 void OnRegistrationDeletedImpl(int64 registration_id,
221 const base::Closure& callback);
223 // OnStorageWiped callbacks
224 void OnStorageWipedImpl(const base::Closure& callback);
226 // Operation Scheduling callbacks
227 void PendingStatusAndRegistrationCallback(
228 const StatusAndRegistrationCallback& callback,
229 ErrorType error,
230 const BackgroundSyncRegistration& sync_registration);
231 void PendingStatusCallback(const StatusCallback& callback, ErrorType error);
232 void PendingClosure(const base::Closure& closure);
234 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion(
235 const StatusAndRegistrationCallback& callback);
236 BackgroundSyncManager::StatusCallback MakeStatusCompletion(
237 const StatusCallback& callback);
238 base::Closure MakeEmptyCompletion();
240 SWIdToRegistrationsMap sw_to_registrations_map_;
241 CacheStorageScheduler op_scheduler_;
242 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
243 bool disabled_;
245 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_;
247 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager);
250 } // namespace content
252 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_