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"
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"
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();
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
) {
45 DCHECK(service_worker_registration
);
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
,
64 const blink::WebString
& tag
,
65 blink::WebServiceWorkerRegistration
* service_worker_registration
,
66 blink::WebSyncUnregistrationCallbacks
* callbacks
) {
67 DCHECK(service_worker_registration
);
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
);
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
);
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
);
127 int64 service_worker_registration_id
=
128 GetServiceWorkerRegistrationId(service_worker_registration
);
129 scoped_ptr
<blink::WebSyncGetPermissionStatusCallbacks
> callbacksPtr(
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
;
148 case BACKGROUND_SYNC_ERROR_NONE
:
149 if (!options
.is_null())
151 mojo::ConvertTo
<scoped_ptr
<blink::WebSyncRegistration
>>(options
);
152 callbacks
->onSuccess(blink::adoptWebPtr(result
.release()));
154 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
157 case BACKGROUND_SYNC_ERROR_STORAGE
:
159 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
160 "Background Sync is disabled."));
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."));
167 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER
:
169 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
170 "No service worker is active."));
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
180 case BACKGROUND_SYNC_ERROR_NONE
:
181 callbacks
->onSuccess(true);
183 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
184 callbacks
->onSuccess(false);
186 case BACKGROUND_SYNC_ERROR_STORAGE
:
188 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
189 "Background Sync is disabled."));
191 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED
:
192 // This error should never be returned from
193 // BackgroundSyncManager::Unregister
196 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER
:
198 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
199 "No service worker is active."));
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
;
211 case BACKGROUND_SYNC_ERROR_NONE
:
212 if (!options
.is_null())
214 mojo::ConvertTo
<scoped_ptr
<blink::WebSyncRegistration
>>(options
);
215 callbacks
->onSuccess(blink::adoptWebPtr(result
.release()));
217 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
218 callbacks
->onSuccess(nullptr);
220 case BACKGROUND_SYNC_ERROR_STORAGE
:
222 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
223 "Background Sync is disabled."));
225 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED
:
226 // This error should never be returned from
227 // BackgroundSyncManager::GetRegistration
230 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER
:
232 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
233 "No service worker is active."));
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
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
>>(
252 callbacks
->onSuccess(results
);
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
261 case BACKGROUND_SYNC_ERROR_STORAGE
:
263 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
264 "Background Sync is disabled."));
266 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER
:
268 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
269 "No service worker is active."));
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
280 case BACKGROUND_SYNC_ERROR_NONE
:
282 case PERMISSION_STATUS_GRANTED
:
283 callbacks
->onSuccess(blink::WebSyncPermissionStatusGranted
);
285 case PERMISSION_STATUS_DENIED
:
286 callbacks
->onSuccess(blink::WebSyncPermissionStatusDenied
);
288 case PERMISSION_STATUS_ASK
:
289 callbacks
->onSuccess(blink::WebSyncPermissionStatusPrompt
);
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
299 case BACKGROUND_SYNC_ERROR_STORAGE
:
301 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
302 "Background Sync is disabled."));
304 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER
:
306 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
307 "No service worker is active."));
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