OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | |
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | |
7 | |
8 #include <list> | |
9 #include <map> | |
10 | |
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" | |
19 | |
20 namespace content { | |
21 | |
22 class ServiceWorkerContextWrapper; | |
23 | |
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. | |
30 | |
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 class CONTENT_EXPORT BackgroundSyncManager { | |
41 public: | |
42 enum ErrorType { | |
43 ERROR_TYPE_OK = 0, | |
44 ERROR_TYPE_EXISTS, | |
45 ERROR_TYPE_STORAGE, | |
46 ERROR_TYPE_NOT_FOUND | |
47 }; | |
48 | |
49 // TODO(jkarlin): Remove this and use the struct from IPC messages once it | |
50 // lands. | |
51 struct BackgroundSyncRegistration { | |
52 BackgroundSyncRegistration() {} | |
53 explicit BackgroundSyncRegistration(const std::string& id) : id(id) {} | |
54 std::string id; | |
55 }; | |
56 | |
57 using StatusCallback = base::Callback<void(ErrorType)>; | |
58 using StatusAndRegistrationCallback = | |
59 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; | |
60 | |
61 static scoped_ptr<BackgroundSyncManager> Create( | |
62 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | |
63 virtual ~BackgroundSyncManager(); | |
64 | |
65 // Stores the given background sync registration and adds it to the scheduling | |
66 // queue. Will request permission from the user if necessary. Calls |callback| | |
67 // with ErrorTypeExists if the registration is already registered. Calls | |
68 // |callback| with ErrorTypeOK and the registration on success. | |
davidben
2015/03/17 18:47:15
There's no way to change an existing registration,
jkarlin
2015/03/20 13:50:49
The plan was to change this to overwrite existing
| |
69 void Register(const GURL& origin, | |
70 int64 sw_registration_id, | |
71 const BackgroundSyncRegistration& sync_registratin, | |
72 const StatusAndRegistrationCallback& callback); | |
73 | |
74 // Removes the given background sync registration from storage and the | |
75 // scheduling queue. Calls |callback| with ErrorTypeNotFound if not | |
76 // registered. Calls |callback| with ErrorTypeOK on success. | |
77 void Unregister(const GURL& origin, | |
78 int64 sw_registration_id, | |
79 const BackgroundSyncRegistration& sync_registration, | |
davidben
2015/03/17 18:47:15
Should this pass in a sync_registration_id rather
jkarlin
2015/03/20 13:50:49
Done.
| |
80 const StatusCallback& callback); | |
81 | |
82 // Finds the background sync registration associated with | |
83 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't | |
84 // exist. Calls |callback| with ErrorTypeOK on success. | |
85 void GetRegistration(const GURL& origin, | |
86 int64 sw_registration_id, | |
87 const std::string sync_registration_id, | |
88 const StatusAndRegistrationCallback& callback); | |
89 | |
90 private: | |
91 using PermissionStatusCallback = base::Callback<void(bool)>; | |
92 using IdToRegistrationMap = | |
93 std::map<int64, std::list<BackgroundSyncRegistration>>; | |
94 | |
95 explicit BackgroundSyncManager( | |
96 const scoped_refptr<ServiceWorkerContextWrapper>& context); | |
97 | |
98 bool HasRegistration(int64 sw_registration_id, | |
99 const BackgroundSyncRegistration& sync_registration); | |
100 | |
101 // Store all registrations for a given |sw_registration_id|. | |
102 void StoreRegistrations(const GURL& origin, | |
103 int64 sw_registration_id, | |
104 const ServiceWorkerStorage::StatusCallback& callback); | |
105 | |
106 // If the registration is in the map, removes it. Otherwise returns false. | |
107 bool RemoveRegistrationFromMap( | |
108 int64 sw_registration_id, | |
109 const BackgroundSyncRegistration& sync_registration); | |
110 | |
111 // If the registration is not in the map, adds it. Otherwise returns false. | |
112 bool AddRegistrationToMap( | |
113 int64 sw_registration_id, | |
114 const BackgroundSyncRegistration& sync_registration); | |
115 | |
116 // Init must be called before any public member function. Only call it once. | |
117 void Init(); | |
118 void InitImpl(); | |
119 void InitDidGetUserData( | |
120 const std::vector<std::pair<int64, std::string>>& user_data, | |
121 ServiceWorkerStatusCode status); | |
122 | |
123 // Register callbacks | |
124 void RegisterImpl(const GURL& origin, | |
125 int64 sw_registration_id, | |
126 const BackgroundSyncRegistration& sync_registration, | |
127 const StatusAndRegistrationCallback& callback); | |
128 void RegisterDidStore(int64 sw_registration_id, | |
129 const BackgroundSyncRegistration& sync_registration, | |
130 const StatusAndRegistrationCallback& callback, | |
131 ServiceWorkerStatusCode status); | |
132 | |
133 // Unregister callbacks | |
134 void UnregisterImpl(const GURL& origin, | |
135 int64 sw_registration_id, | |
136 const BackgroundSyncRegistration& sync_registration, | |
137 const StatusCallback& callback); | |
138 void UnregisterDidStore(int64 sw_registration_id, | |
139 const BackgroundSyncRegistration& sync_registration, | |
140 const StatusCallback& callback, | |
141 ServiceWorkerStatusCode status); | |
142 | |
143 // GetRegistration callbacks | |
144 void GetRegistrationImpl(const GURL& origin, | |
145 int64 sw_registration_id, | |
146 const std::string sync_registration_id, | |
147 const StatusAndRegistrationCallback& callback); | |
148 | |
149 // Operation Scheduling callbacks | |
150 void PendingStatusAndRegistrationCallback( | |
151 const StatusAndRegistrationCallback& callback, | |
152 ErrorType error, | |
153 const BackgroundSyncRegistration& sync_registration); | |
154 void PendingStatusCallback(const StatusCallback& callback, ErrorType error); | |
155 | |
156 IdToRegistrationMap registration_map_; | |
157 ServiceWorkerCacheScheduler op_scheduler_; | |
davidben
2015/03/17 18:47:15
It's a little odd that operations from one sw_regi
jkarlin
2015/03/20 13:50:49
I intend to add such parallelism in the future. Se
| |
158 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
159 | |
160 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | |
163 }; | |
164 | |
165 } // namespace content | |
166 | |
167 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | |
OLD | NEW |