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.h" | |
davidben
2015/03/13 22:05:44
I think you only need callback_forward.h in this f
jkarlin
2015/03/14 01:17:27
Done.
| |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "content/browser/service_worker/service_worker_storage.h" | |
15 #include "content/common/content_export.h" | |
16 #include "content/common/service_worker/service_worker_status_code.h" | |
17 #include "url/gurl.h" | |
18 | |
19 namespace content { | |
20 | |
21 class ServiceWorkerCacheScheduler; | |
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 ErrorTypeOK = 0, | |
44 ErrorTypeExists, | |
45 ErrorTypeStorage, | |
46 ErrorTypeNotFound | |
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. Returns | |
67 // ErrorTypeExists if the registration is already registered. Returns | |
68 // ErrorTypeOK and the registration on success. | |
davidben
2015/03/13 22:05:44
"Returns" -> "Calls |callback| with"
jkarlin
2015/03/14 01:17:27
Done.
| |
69 void Register(const GURL& origin, | |
70 int64 sw_registration_id, | |
71 const BackgroundSyncRegistration& sync_registratin, | |
davidben
2015/03/13 22:05:44
sync_registratin -> sync_registration
I don't rea
jkarlin
2015/03/14 01:17:27
In the future there is a chance that the Register(
davidben
2015/03/17 18:47:14
Ah, okay. Perhaps add a comment to this effect or
jkarlin
2015/03/20 13:50:49
Done.
| |
72 const StatusAndRegistrationCallback& callback); | |
73 | |
74 // Removes the given background sync registration from storage and the | |
75 // scheduling queue. Returns ErrorTypeNotFound if not registered. Returns | |
davidben
2015/03/13 22:05:44
Returns -> Calls |callback with
jkarlin
2015/03/14 01:17:27
Done.
| |
76 // ErrorTypeOK on success. | |
77 void Unregister(const GURL& origin, | |
78 int64 sw_registration_id, | |
79 const BackgroundSyncRegistration& sync_registration, | |
80 const StatusCallback& callback); | |
81 | |
82 // Finds the background sync registration associated with | |
83 // |sw_registration_id|. Returns ErrorTypeNotFound if it doesn't exist. | |
84 // Returns ErrorTypeOK on success. | |
davidben
2015/03/13 22:05:44
Returns -> Calls |callback| with
jkarlin
2015/03/14 01:17:27
Done.
| |
85 void GetRegistration(const GURL& origin, | |
86 int64 sw_registration_id, | |
87 const std::string sync_registration_id, | |
88 const StatusAndRegistrationCallback& callback); | |
davidben
2015/03/13 22:05:43
I'm assuming the BackgroundSyncRegistration will g
jkarlin
2015/03/14 01:17:27
Yep!
| |
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. | |
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 bool initialized_; | |
157 bool initializing_; | |
davidben
2015/03/13 22:05:44
These two bits of state are redundant, right? You
jkarlin
2015/03/14 01:17:27
Since init() is now called by ::Create() (it wasn'
| |
158 | |
159 scoped_ptr<IdToRegistrationMap> registration_map_; | |
davidben
2015/03/13 22:05:44
Why put this in a scoped_ptr? It doesn't look like
jkarlin
2015/03/14 01:17:27
Done.
| |
160 scoped_ptr<ServiceWorkerCacheScheduler> op_scheduler_; | |
davidben
2015/03/13 22:05:44
(Does this need to be a scoped_ptr? I guess other
jkarlin
2015/03/14 01:17:27
Done.
| |
161 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
162 | |
163 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); | |
166 }; | |
167 | |
168 } // namespace content | |
169 | |
170 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ | |
OLD | NEW |