Refactors gesture conversion functions to ui/events/blink
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_manager.h
blob674186c8712c25b831a195b32438a63ada1fe660
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/service_worker/service_worker_cache_scheduler.h"
15 #include "content/browser/service_worker/service_worker_storage.h"
16 #include "content/common/content_export.h"
17 #include "content/common/service_worker/service_worker_status_code.h"
18 #include "url/gurl.h"
20 namespace content {
22 class ServiceWorkerContextWrapper;
24 // BackgroundSyncManager manages and stores the set of background sync
25 // registrations across all registered service workers for a profile.
26 // Registrations are stored along with their associated Service Worker
27 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered,
28 // the sync registrations are removed. This class expects to be run on the IO
29 // thread. The asynchronous methods are executed sequentially.
31 // TODO(jkarlin): Check permissions when registering, scheduling, and firing
32 // background sync. In the meantime, --enable-service-worker-sync is required to
33 // fire a sync event.
34 // TODO(jkarlin): Unregister syncs when permission is revoked.
35 // TODO(jkarlin): Create a background sync scheduler to actually run the
36 // registered events.
37 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the
38 // Background" is true and a sync is registered.
39 // TODO(jkarlin): Unregister syncs when storage for an origin is cleared.
40 // TODO(jkarlin): Detect and handle a corrupt or broken backend.
41 class CONTENT_EXPORT BackgroundSyncManager {
42 public:
43 enum ErrorType {
44 ERROR_TYPE_OK = 0,
45 ERROR_TYPE_STORAGE,
46 ERROR_TYPE_NOT_FOUND
49 // TODO(jkarlin): Remove this and use the struct from IPC messages once it
50 // lands.
51 struct CONTENT_EXPORT BackgroundSyncRegistration {
52 using RegistrationId = int64;
53 static const RegistrationId kInvalidRegistrationId;
55 BackgroundSyncRegistration()
56 : BackgroundSyncRegistration(kInvalidRegistrationId, "") {}
57 explicit BackgroundSyncRegistration(const std::string& name)
58 : BackgroundSyncRegistration(kInvalidRegistrationId, name) {}
59 BackgroundSyncRegistration(int64 id, const std::string& name)
60 : id(id), min_period(0), name(name) {}
62 bool Equals(const BackgroundSyncRegistration& other) {
63 return this->name == other.name && this->min_period == other.min_period;
66 RegistrationId id;
67 int64 min_period;
68 std::string name;
71 struct CONTENT_EXPORT BackgroundSyncRegistrations {
72 using NameToRegistrationMap =
73 std::map<std::string, BackgroundSyncRegistration>;
74 static const BackgroundSyncRegistration::RegistrationId kInitialId;
76 BackgroundSyncRegistrations();
77 explicit BackgroundSyncRegistrations(
78 BackgroundSyncRegistration::RegistrationId next_id);
79 ~BackgroundSyncRegistrations();
81 NameToRegistrationMap name_to_registration_map;
82 BackgroundSyncRegistration::RegistrationId next_id;
85 using StatusCallback = base::Callback<void(ErrorType)>;
86 using StatusAndRegistrationCallback =
87 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>;
89 static scoped_ptr<BackgroundSyncManager> Create(
90 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
91 virtual ~BackgroundSyncManager();
93 // Stores the given background sync registration and adds it to the scheduling
94 // queue. Overwrites any existing registration with the same name but
95 // different parameters (other than the id). Calls |callback| with ErrorTypeOK
96 // and the accepted registration on success. The accepted registration will
97 // have a unique id. It may also have altered parameters if the user or UA
98 // chose different parameters than those supplied.
99 void Register(const GURL& origin,
100 int64 sw_registration_id,
101 const BackgroundSyncRegistration& sync_registration,
102 const StatusAndRegistrationCallback& callback);
104 // Removes the background sync registration with |sync_registration_name| if
105 // the |sync_registration_id| matches. |sync_registration_id| will not match
106 // if, for instance, a new registration with the same name has replaced it.
107 // Calls |callback| with ErrorTypeNotFound if no match is found. Calls
108 // |callback| with ErrorTypeOK on success.
109 void Unregister(
110 const GURL& origin,
111 int64 sw_registration_id,
112 const std::string& sync_registration_name,
113 BackgroundSyncRegistration::RegistrationId sync_registration_id,
114 const StatusCallback& callback);
116 // Finds the background sync registration associated with
117 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't
118 // exist. Calls |callback| with ErrorTypeOK on success.
119 void GetRegistration(const GURL& origin,
120 int64 sw_registration_id,
121 const std::string sync_registration_name,
122 const StatusAndRegistrationCallback& callback);
124 protected:
125 explicit BackgroundSyncManager(
126 const scoped_refptr<ServiceWorkerContextWrapper>& context);
128 // Init must be called before any public member function. Only call it once.
129 void Init();
131 // The following methods are virtual for testing.
132 virtual void StoreDataInBackend(
133 int64 sw_registration_id,
134 const GURL& origin,
135 const std::string& key,
136 const std::string& data,
137 const ServiceWorkerStorage::StatusCallback& callback);
138 virtual void GetDataFromBackend(
139 const std::string& key,
140 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
141 callback);
143 private:
144 using PermissionStatusCallback = base::Callback<void(bool)>;
145 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>;
147 // Returns the existing registration in |existing_registration| if it is not
148 // null.
149 bool LookupRegistration(int64 sw_registration_id,
150 const std::string& sync_registration_name,
151 BackgroundSyncRegistration* existing_registration);
153 // Store all registrations for a given |sw_registration_id|.
154 void StoreRegistrations(const GURL& origin,
155 int64 sw_registration_id,
156 const ServiceWorkerStorage::StatusCallback& callback);
158 // If the registration is in the map, removes it and returns the removed
159 // registration in |old_registration|. |old_registration| may be null.
160 void RemoveRegistrationFromMap(int64 sw_registration_id,
161 const std::string& sync_registration_name,
162 BackgroundSyncRegistration* old_registration);
164 void AddRegistrationToMap(
165 int64 sw_registration_id,
166 const BackgroundSyncRegistration& sync_registration);
168 void InitImpl();
169 void InitDidGetDataFromBackend(
170 const std::vector<std::pair<int64, std::string>>& user_data,
171 ServiceWorkerStatusCode status);
173 // Register callbacks
174 void RegisterImpl(const GURL& origin,
175 int64 sw_registration_id,
176 const BackgroundSyncRegistration& sync_registration,
177 const StatusAndRegistrationCallback& callback);
178 void RegisterDidStore(int64 sw_registration_id,
179 const BackgroundSyncRegistration& sync_registration,
180 const BackgroundSyncRegistration& previous_registration,
181 const StatusAndRegistrationCallback& callback,
182 ServiceWorkerStatusCode status);
184 // Unregister callbacks
185 void UnregisterImpl(
186 const GURL& origin,
187 int64 sw_registration_id,
188 const std::string& sync_registration_name,
189 BackgroundSyncRegistration::RegistrationId sync_registration_id,
190 const StatusCallback& callback);
191 void UnregisterDidStore(
192 int64 sw_registration_id,
193 const BackgroundSyncRegistration& old_sync_registration,
194 const StatusCallback& callback,
195 ServiceWorkerStatusCode status);
197 // GetRegistration callbacks
198 void GetRegistrationImpl(const GURL& origin,
199 int64 sw_registration_id,
200 const std::string sync_registration_name,
201 const StatusAndRegistrationCallback& callback);
203 // Operation Scheduling callbacks
204 void PendingStatusAndRegistrationCallback(
205 const StatusAndRegistrationCallback& callback,
206 ErrorType error,
207 const BackgroundSyncRegistration& sync_registration);
208 void PendingStatusCallback(const StatusCallback& callback, ErrorType error);
210 SWIdToRegistrationsMap sw_to_registrations_map_;
211 ServiceWorkerCacheScheduler op_scheduler_;
212 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
214 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_;
216 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager);
219 } // namespace content
221 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_