Rename CoalescedPermissionMessage to PermissionMessage
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider.cc
blob1e4fb48b806d6ee53376f6000d427bf501137725
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 #include "content/child/background_sync/background_sync_provider.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "content/child/background_sync/background_sync_type_converters.h"
10 #include "content/child/service_worker/web_service_worker_registration_impl.h"
11 #include "content/child/worker_task_runner.h"
12 #include "content/public/common/background_sync.mojom.h"
13 #include "content/public/common/permission_status.mojom.h"
14 #include "content/public/common/service_registry.h"
15 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncError.h"
16 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegistration.h"
18 namespace content {
19 namespace {
21 // Returns the id of the given |service_worker_registration|, which
22 // is only available on the implementation of the interface.
23 int64 GetServiceWorkerRegistrationId(
24 blink::WebServiceWorkerRegistration* service_worker_registration) {
25 return static_cast<WebServiceWorkerRegistrationImpl*>(
26 service_worker_registration)->registration_id();
29 } // namespace
31 BackgroundSyncProvider::BackgroundSyncProvider(
32 ServiceRegistry* service_registry)
33 : service_registry_(service_registry) {
34 DCHECK(service_registry);
37 BackgroundSyncProvider::~BackgroundSyncProvider() {
40 void BackgroundSyncProvider::registerBackgroundSync(
41 const blink::WebSyncRegistration* options,
42 blink::WebServiceWorkerRegistration* service_worker_registration,
43 blink::WebSyncRegistrationCallbacks* callbacks) {
44 DCHECK(options);
45 DCHECK(service_worker_registration);
46 DCHECK(callbacks);
47 int64 service_worker_registration_id =
48 GetServiceWorkerRegistrationId(service_worker_registration);
49 scoped_ptr<const blink::WebSyncRegistration> optionsPtr(options);
50 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks);
52 // base::Unretained is safe here, as the mojo channel will be deleted (and
53 // will wipe its callbacks) before 'this' is deleted.
54 GetBackgroundSyncServicePtr()->Register(
55 mojo::ConvertTo<SyncRegistrationPtr>(*(optionsPtr.get())),
56 service_worker_registration_id,
57 base::Bind(&BackgroundSyncProvider::RegisterCallback,
58 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
61 void BackgroundSyncProvider::unregisterBackgroundSync(
62 blink::WebSyncRegistration::Periodicity periodicity,
63 int64_t id,
64 const blink::WebString& tag,
65 blink::WebServiceWorkerRegistration* service_worker_registration,
66 blink::WebSyncUnregistrationCallbacks* callbacks) {
67 DCHECK(service_worker_registration);
68 DCHECK(callbacks);
69 int64 service_worker_registration_id =
70 GetServiceWorkerRegistrationId(service_worker_registration);
71 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacksPtr(callbacks);
73 // base::Unretained is safe here, as the mojo channel will be deleted (and
74 // will wipe its callbacks) before 'this' is deleted.
75 GetBackgroundSyncServicePtr()->Unregister(
76 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), id, tag.utf8(),
77 service_worker_registration_id,
78 base::Bind(&BackgroundSyncProvider::UnregisterCallback,
79 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
82 void BackgroundSyncProvider::getRegistration(
83 blink::WebSyncRegistration::Periodicity periodicity,
84 const blink::WebString& tag,
85 blink::WebServiceWorkerRegistration* service_worker_registration,
86 blink::WebSyncRegistrationCallbacks* callbacks) {
87 DCHECK(service_worker_registration);
88 DCHECK(callbacks);
89 int64 service_worker_registration_id =
90 GetServiceWorkerRegistrationId(service_worker_registration);
91 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks);
93 // base::Unretained is safe here, as the mojo channel will be deleted (and
94 // will wipe its callbacks) before 'this' is deleted.
95 GetBackgroundSyncServicePtr()->GetRegistration(
96 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), tag.utf8(),
97 service_worker_registration_id,
98 base::Bind(&BackgroundSyncProvider::GetRegistrationCallback,
99 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
102 void BackgroundSyncProvider::getRegistrations(
103 blink::WebSyncRegistration::Periodicity periodicity,
104 blink::WebServiceWorkerRegistration* service_worker_registration,
105 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
106 DCHECK(service_worker_registration);
107 DCHECK(callbacks);
108 int64 service_worker_registration_id =
109 GetServiceWorkerRegistrationId(service_worker_registration);
110 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacksPtr(callbacks);
112 // base::Unretained is safe here, as the mojo channel will be deleted (and
113 // will wipe its callbacks) before 'this' is deleted.
114 GetBackgroundSyncServicePtr()->GetRegistrations(
115 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
116 service_worker_registration_id,
117 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback,
118 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
121 void BackgroundSyncProvider::getPermissionStatus(
122 blink::WebSyncRegistration::Periodicity periodicity,
123 blink::WebServiceWorkerRegistration* service_worker_registration,
124 blink::WebSyncGetPermissionStatusCallbacks* callbacks) {
125 DCHECK(service_worker_registration);
126 DCHECK(callbacks);
127 int64 service_worker_registration_id =
128 GetServiceWorkerRegistrationId(service_worker_registration);
129 scoped_ptr<blink::WebSyncGetPermissionStatusCallbacks> callbacksPtr(
130 callbacks);
132 // base::Unretained is safe here, as the mojo channel will be deleted (and
133 // will wipe its callbacks) before 'this' is deleted.
134 GetBackgroundSyncServicePtr()->GetPermissionStatus(
135 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
136 service_worker_registration_id,
137 base::Bind(&BackgroundSyncProvider::GetPermissionStatusCallback,
138 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
141 void BackgroundSyncProvider::RegisterCallback(
142 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
143 BackgroundSyncError error,
144 const SyncRegistrationPtr& options) {
145 // TODO(iclelland): Determine the correct error message to return in each case
146 scoped_ptr<blink::WebSyncRegistration> result;
147 switch (error) {
148 case BACKGROUND_SYNC_ERROR_NONE:
149 if (!options.is_null())
150 result =
151 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
152 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
153 break;
154 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
155 NOTREACHED();
156 break;
157 case BACKGROUND_SYNC_ERROR_STORAGE:
158 callbacks->onError(
159 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
160 "Background Sync is disabled."));
161 break;
162 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
163 callbacks->onError(blink::WebSyncError(
164 blink::WebSyncError::ErrorTypeNoPermission,
165 "Cannot register a sync event without a window client."));
166 break;
167 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
168 callbacks->onError(
169 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
170 "No service worker is active."));
171 break;
175 void BackgroundSyncProvider::UnregisterCallback(
176 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks,
177 BackgroundSyncError error) {
178 // TODO(iclelland): Determine the correct error message to return in each case
179 switch (error) {
180 case BACKGROUND_SYNC_ERROR_NONE:
181 callbacks->onSuccess(true);
182 break;
183 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
184 callbacks->onSuccess(false);
185 break;
186 case BACKGROUND_SYNC_ERROR_STORAGE:
187 callbacks->onError(
188 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
189 "Background Sync is disabled."));
190 break;
191 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
192 // This error should never be returned from
193 // BackgroundSyncManager::Unregister
194 NOTREACHED();
195 break;
196 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
197 callbacks->onError(
198 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
199 "No service worker is active."));
200 break;
204 void BackgroundSyncProvider::GetRegistrationCallback(
205 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
206 BackgroundSyncError error,
207 const SyncRegistrationPtr& options) {
208 // TODO(iclelland): Determine the correct error message to return in each case
209 scoped_ptr<blink::WebSyncRegistration> result;
210 switch (error) {
211 case BACKGROUND_SYNC_ERROR_NONE:
212 if (!options.is_null())
213 result =
214 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
215 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
216 break;
217 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
218 callbacks->onSuccess(nullptr);
219 break;
220 case BACKGROUND_SYNC_ERROR_STORAGE:
221 callbacks->onError(
222 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
223 "Background Sync is disabled."));
224 break;
225 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
226 // This error should never be returned from
227 // BackgroundSyncManager::GetRegistration
228 NOTREACHED();
229 break;
230 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
231 callbacks->onError(
232 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
233 "No service worker is active."));
234 break;
238 void BackgroundSyncProvider::GetRegistrationsCallback(
239 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
240 BackgroundSyncError error,
241 const mojo::Array<SyncRegistrationPtr>& registrations) {
242 // TODO(iclelland): Determine the correct error message to return in each case
243 switch (error) {
244 case BACKGROUND_SYNC_ERROR_NONE: {
245 blink::WebVector<blink::WebSyncRegistration*> results(
246 registrations.size());
247 for (size_t i = 0; i < registrations.size(); ++i) {
248 results[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(
249 registrations[i])
250 .release();
252 callbacks->onSuccess(results);
253 break;
255 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
256 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
257 // These errors should never be returned from
258 // BackgroundSyncManager::GetRegistrations
259 NOTREACHED();
260 break;
261 case BACKGROUND_SYNC_ERROR_STORAGE:
262 callbacks->onError(
263 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
264 "Background Sync is disabled."));
265 break;
266 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
267 callbacks->onError(
268 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
269 "No service worker is active."));
270 break;
274 void BackgroundSyncProvider::GetPermissionStatusCallback(
275 scoped_ptr<blink::WebSyncGetPermissionStatusCallbacks> callbacks,
276 BackgroundSyncError error,
277 PermissionStatus status) {
278 // TODO(iclelland): Determine the correct error message to return in each case
279 switch (error) {
280 case BACKGROUND_SYNC_ERROR_NONE:
281 switch (status) {
282 case PERMISSION_STATUS_GRANTED:
283 callbacks->onSuccess(blink::WebSyncPermissionStatusGranted);
284 break;
285 case PERMISSION_STATUS_DENIED:
286 callbacks->onSuccess(blink::WebSyncPermissionStatusDenied);
287 break;
288 case PERMISSION_STATUS_ASK:
289 callbacks->onSuccess(blink::WebSyncPermissionStatusPrompt);
290 break;
292 break;
293 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
294 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
295 // These errors should never be returned from
296 // BackgroundSyncManager::GetPermissionStatus
297 NOTREACHED();
298 break;
299 case BACKGROUND_SYNC_ERROR_STORAGE:
300 callbacks->onError(
301 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
302 "Background Sync is disabled."));
303 break;
304 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
305 callbacks->onError(
306 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
307 "No service worker is active."));
308 break;
312 BackgroundSyncServicePtr&
313 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
314 if (!background_sync_service_.get()) {
315 service_registry_->ConnectToRemoteService(
316 mojo::GetProxy(&background_sync_service_));
318 return background_sync_service_;
321 } // namespace content