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(result
.release());
154 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
157 case BACKGROUND_SYNC_ERROR_STORAGE
:
159 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
160 "Background Sync is disabled."));
162 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED
:
163 callbacks
->onError(new 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 new 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(new bool(true));
183 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
184 callbacks
->onSuccess(new bool(false));
186 case BACKGROUND_SYNC_ERROR_STORAGE
:
188 new 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 new 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(result
.release());
217 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
218 callbacks
->onSuccess(nullptr);
220 case BACKGROUND_SYNC_ERROR_STORAGE
:
222 new 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 new 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
243 blink::WebVector
<blink::WebSyncRegistration
*>* results
;
245 case BACKGROUND_SYNC_ERROR_NONE
:
246 results
= new blink::WebVector
<blink::WebSyncRegistration
*>(
247 registrations
.size());
248 for (size_t i
= 0; i
< registrations
.size(); ++i
) {
249 (*results
)[i
] = mojo::ConvertTo
<scoped_ptr
<blink::WebSyncRegistration
>>(
250 registrations
[i
]).release();
252 callbacks
->onSuccess(results
);
254 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
255 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED
:
256 // These errors should never be returned from
257 // BackgroundSyncManager::GetRegistrations
260 case BACKGROUND_SYNC_ERROR_STORAGE
:
262 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
263 "Background Sync is disabled."));
265 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER
:
267 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
268 "No service worker is active."));
273 void BackgroundSyncProvider::GetPermissionStatusCallback(
274 scoped_ptr
<blink::WebSyncGetPermissionStatusCallbacks
> callbacks
,
275 BackgroundSyncError error
,
276 PermissionStatus status
) {
277 // TODO(iclelland): Determine the correct error message to return in each case
279 case BACKGROUND_SYNC_ERROR_NONE
:
281 case PERMISSION_STATUS_GRANTED
:
282 callbacks
->onSuccess(new blink::WebSyncPermissionStatus(
283 blink::WebSyncPermissionStatusGranted
));
285 case PERMISSION_STATUS_DENIED
:
286 callbacks
->onSuccess(new blink::WebSyncPermissionStatus(
287 blink::WebSyncPermissionStatusDenied
));
289 case PERMISSION_STATUS_ASK
:
290 callbacks
->onSuccess(new blink::WebSyncPermissionStatus(
291 blink::WebSyncPermissionStatusPrompt
));
295 case BACKGROUND_SYNC_ERROR_NOT_FOUND
:
296 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED
:
297 // These errors should never be returned from
298 // BackgroundSyncManager::GetPermissionStatus
301 case BACKGROUND_SYNC_ERROR_STORAGE
:
303 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
304 "Background Sync is disabled."));
306 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER
:
308 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown
,
309 "No service worker is active."));
314 BackgroundSyncServicePtr
&
315 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
316 if (!background_sync_service_
.get()) {
317 service_registry_
->ConnectToRemoteService(
318 mojo::GetProxy(&background_sync_service_
));
320 return background_sync_service_
;
323 } // namespace content