Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider.cc
blobc9f5ab75b991eec49e4b8ae50f5b4a99cb308729
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 bool requested_from_service_worker,
44 blink::WebSyncRegistrationCallbacks* callbacks) {
45 DCHECK(options);
46 DCHECK(service_worker_registration);
47 DCHECK(callbacks);
48 int64 service_worker_registration_id =
49 GetServiceWorkerRegistrationId(service_worker_registration);
50 scoped_ptr<const blink::WebSyncRegistration> optionsPtr(options);
51 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks);
53 // base::Unretained is safe here, as the mojo channel will be deleted (and
54 // will wipe its callbacks) before 'this' is deleted.
55 GetBackgroundSyncServicePtr()->Register(
56 mojo::ConvertTo<SyncRegistrationPtr>(*(optionsPtr.get())),
57 service_worker_registration_id, requested_from_service_worker,
58 base::Bind(&BackgroundSyncProvider::RegisterCallback,
59 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
62 void BackgroundSyncProvider::unregisterBackgroundSync(
63 blink::WebSyncRegistration::Periodicity periodicity,
64 int64_t handle_id,
65 const blink::WebString& tag,
66 blink::WebServiceWorkerRegistration* service_worker_registration,
67 blink::WebSyncUnregistrationCallbacks* callbacks) {
68 DCHECK(service_worker_registration);
69 DCHECK(callbacks);
70 int64 service_worker_registration_id =
71 GetServiceWorkerRegistrationId(service_worker_registration);
72 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacksPtr(callbacks);
74 // base::Unretained is safe here, as the mojo channel will be deleted (and
75 // will wipe its callbacks) before 'this' is deleted.
76 GetBackgroundSyncServicePtr()->Unregister(
77 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), handle_id,
78 service_worker_registration_id,
79 base::Bind(&BackgroundSyncProvider::UnregisterCallback,
80 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
83 void BackgroundSyncProvider::getRegistration(
84 blink::WebSyncRegistration::Periodicity periodicity,
85 const blink::WebString& tag,
86 blink::WebServiceWorkerRegistration* service_worker_registration,
87 blink::WebSyncRegistrationCallbacks* callbacks) {
88 DCHECK(service_worker_registration);
89 DCHECK(callbacks);
90 int64 service_worker_registration_id =
91 GetServiceWorkerRegistrationId(service_worker_registration);
92 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks);
94 // base::Unretained is safe here, as the mojo channel will be deleted (and
95 // will wipe its callbacks) before 'this' is deleted.
96 GetBackgroundSyncServicePtr()->GetRegistration(
97 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), tag.utf8(),
98 service_worker_registration_id,
99 base::Bind(&BackgroundSyncProvider::GetRegistrationCallback,
100 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
103 void BackgroundSyncProvider::getRegistrations(
104 blink::WebSyncRegistration::Periodicity periodicity,
105 blink::WebServiceWorkerRegistration* service_worker_registration,
106 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
107 DCHECK(service_worker_registration);
108 DCHECK(callbacks);
109 int64 service_worker_registration_id =
110 GetServiceWorkerRegistrationId(service_worker_registration);
111 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacksPtr(callbacks);
113 // base::Unretained is safe here, as the mojo channel will be deleted (and
114 // will wipe its callbacks) before 'this' is deleted.
115 GetBackgroundSyncServicePtr()->GetRegistrations(
116 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
117 service_worker_registration_id,
118 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback,
119 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
122 void BackgroundSyncProvider::getPermissionStatus(
123 blink::WebSyncRegistration::Periodicity periodicity,
124 blink::WebServiceWorkerRegistration* service_worker_registration,
125 blink::WebSyncGetPermissionStatusCallbacks* callbacks) {
126 DCHECK(service_worker_registration);
127 DCHECK(callbacks);
128 int64 service_worker_registration_id =
129 GetServiceWorkerRegistrationId(service_worker_registration);
130 scoped_ptr<blink::WebSyncGetPermissionStatusCallbacks> callbacksPtr(
131 callbacks);
133 // base::Unretained is safe here, as the mojo channel will be deleted (and
134 // will wipe its callbacks) before 'this' is deleted.
135 GetBackgroundSyncServicePtr()->GetPermissionStatus(
136 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
137 service_worker_registration_id,
138 base::Bind(&BackgroundSyncProvider::GetPermissionStatusCallback,
139 base::Unretained(this), base::Passed(callbacksPtr.Pass())));
142 void BackgroundSyncProvider::releaseRegistration(int64_t handle_id) {
143 GetBackgroundSyncServicePtr()->ReleaseRegistration(handle_id);
146 void BackgroundSyncProvider::notifyWhenDone(
147 int64_t handle_id,
148 blink::WebSyncNotifyWhenDoneCallbacks* callbacks) {
149 DCHECK(callbacks);
151 scoped_ptr<blink::WebSyncNotifyWhenDoneCallbacks> callbacks_ptr(callbacks);
153 // base::Unretained is safe here, as the mojo channel will be deleted (and
154 // will wipe its callbacks) before 'this' is deleted.
155 GetBackgroundSyncServicePtr()->NotifyWhenDone(
156 handle_id,
157 base::Bind(&BackgroundSyncProvider::NotifyWhenDoneCallback,
158 base::Unretained(this), base::Passed(callbacks_ptr.Pass())));
161 void BackgroundSyncProvider::DuplicateRegistrationHandle(
162 int handle_id,
163 const BackgroundSyncService::DuplicateRegistrationHandleCallback&
164 callback) {
165 GetBackgroundSyncServicePtr()->DuplicateRegistrationHandle(handle_id,
166 callback);
169 void BackgroundSyncProvider::RegisterCallback(
170 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
171 BackgroundSyncError error,
172 const SyncRegistrationPtr& options) {
173 // TODO(iclelland): Determine the correct error message to return in each case
174 scoped_ptr<blink::WebSyncRegistration> result;
175 switch (error) {
176 case BACKGROUND_SYNC_ERROR_NONE:
177 if (!options.is_null())
178 result =
179 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
180 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
181 break;
182 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
183 NOTREACHED();
184 break;
185 case BACKGROUND_SYNC_ERROR_STORAGE:
186 callbacks->onError(
187 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
188 "Background Sync is disabled."));
189 break;
190 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
191 callbacks->onError(
192 blink::WebSyncError(blink::WebSyncError::ErrorTypeNoPermission,
193 "Attempted to register a sync event without a "
194 "window or registration tag too long."));
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::UnregisterCallback(
205 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks,
206 BackgroundSyncError error) {
207 // TODO(iclelland): Determine the correct error message to return in each case
208 switch (error) {
209 case BACKGROUND_SYNC_ERROR_NONE:
210 callbacks->onSuccess(true);
211 break;
212 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
213 callbacks->onSuccess(false);
214 break;
215 case BACKGROUND_SYNC_ERROR_STORAGE:
216 callbacks->onError(
217 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
218 "Background Sync is disabled."));
219 break;
220 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
221 // This error should never be returned from
222 // BackgroundSyncManager::Unregister
223 NOTREACHED();
224 break;
225 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
226 callbacks->onError(
227 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
228 "No service worker is active."));
229 break;
233 void BackgroundSyncProvider::GetRegistrationCallback(
234 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
235 BackgroundSyncError error,
236 const SyncRegistrationPtr& options) {
237 // TODO(iclelland): Determine the correct error message to return in each case
238 scoped_ptr<blink::WebSyncRegistration> result;
239 switch (error) {
240 case BACKGROUND_SYNC_ERROR_NONE:
241 if (!options.is_null())
242 result =
243 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
244 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
245 break;
246 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
247 callbacks->onSuccess(nullptr);
248 break;
249 case BACKGROUND_SYNC_ERROR_STORAGE:
250 callbacks->onError(
251 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
252 "Background Sync is disabled."));
253 break;
254 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
255 // This error should never be returned from
256 // BackgroundSyncManager::GetRegistration
257 NOTREACHED();
258 break;
259 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
260 callbacks->onError(
261 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
262 "No service worker is active."));
263 break;
267 void BackgroundSyncProvider::GetRegistrationsCallback(
268 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
269 BackgroundSyncError error,
270 const mojo::Array<SyncRegistrationPtr>& registrations) {
271 // TODO(iclelland): Determine the correct error message to return in each case
272 switch (error) {
273 case BACKGROUND_SYNC_ERROR_NONE: {
274 blink::WebVector<blink::WebSyncRegistration*> results(
275 registrations.size());
276 for (size_t i = 0; i < registrations.size(); ++i) {
277 results[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(
278 registrations[i])
279 .release();
281 callbacks->onSuccess(results);
282 break;
284 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
285 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
286 // These errors should never be returned from
287 // BackgroundSyncManager::GetRegistrations
288 NOTREACHED();
289 break;
290 case BACKGROUND_SYNC_ERROR_STORAGE:
291 callbacks->onError(
292 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
293 "Background Sync is disabled."));
294 break;
295 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
296 callbacks->onError(
297 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
298 "No service worker is active."));
299 break;
303 void BackgroundSyncProvider::GetPermissionStatusCallback(
304 scoped_ptr<blink::WebSyncGetPermissionStatusCallbacks> callbacks,
305 BackgroundSyncError error,
306 PermissionStatus status) {
307 // TODO(iclelland): Determine the correct error message to return in each case
308 switch (error) {
309 case BACKGROUND_SYNC_ERROR_NONE:
310 switch (status) {
311 case PERMISSION_STATUS_GRANTED:
312 callbacks->onSuccess(blink::WebSyncPermissionStatusGranted);
313 break;
314 case PERMISSION_STATUS_DENIED:
315 callbacks->onSuccess(blink::WebSyncPermissionStatusDenied);
316 break;
317 case PERMISSION_STATUS_ASK:
318 callbacks->onSuccess(blink::WebSyncPermissionStatusPrompt);
319 break;
321 break;
322 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
323 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
324 // These errors should never be returned from
325 // BackgroundSyncManager::GetPermissionStatus
326 NOTREACHED();
327 break;
328 case BACKGROUND_SYNC_ERROR_STORAGE:
329 callbacks->onError(
330 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
331 "Background Sync is disabled."));
332 break;
333 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
334 callbacks->onError(
335 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
336 "No service worker is active."));
337 break;
341 void BackgroundSyncProvider::NotifyWhenDoneCallback(
342 scoped_ptr<blink::WebSyncNotifyWhenDoneCallbacks> callbacks,
343 BackgroundSyncError error,
344 BackgroundSyncState state) {
345 switch (error) {
346 case BACKGROUND_SYNC_ERROR_NONE:
347 switch (state) {
348 case BACKGROUND_SYNC_STATE_PENDING:
349 case BACKGROUND_SYNC_STATE_FIRING:
350 case BACKGROUND_SYNC_STATE_UNREGISTERED_WHILE_FIRING:
351 NOTREACHED();
352 break;
353 case BACKGROUND_SYNC_STATE_SUCCESS:
354 callbacks->onSuccess(true);
355 break;
356 case BACKGROUND_SYNC_STATE_FAILED:
357 case BACKGROUND_SYNC_STATE_UNREGISTERED:
358 callbacks->onSuccess(false);
359 break;
361 break;
362 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
363 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
364 NOTREACHED();
365 break;
366 case BACKGROUND_SYNC_ERROR_STORAGE:
367 callbacks->onError(
368 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
369 "Background Sync is disabled."));
370 break;
371 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
372 callbacks->onError(
373 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
374 "No service worker is active."));
375 break;
379 BackgroundSyncServicePtr&
380 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
381 if (!background_sync_service_.get()) {
382 service_registry_->ConnectToRemoteService(
383 mojo::GetProxy(&background_sync_service_));
385 return background_sync_service_;
388 } // namespace content