cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider.cc
blob277e1a7b9962af245023ec58747ef56fcd49d0d7
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 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), id, tag.utf8(),
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::RegisterCallback(
143 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
144 BackgroundSyncError error,
145 const SyncRegistrationPtr& options) {
146 // TODO(iclelland): Determine the correct error message to return in each case
147 scoped_ptr<blink::WebSyncRegistration> result;
148 switch (error) {
149 case BACKGROUND_SYNC_ERROR_NONE:
150 if (!options.is_null())
151 result =
152 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
153 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
154 break;
155 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
156 NOTREACHED();
157 break;
158 case BACKGROUND_SYNC_ERROR_STORAGE:
159 callbacks->onError(
160 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
161 "Background Sync is disabled."));
162 break;
163 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
164 callbacks->onError(blink::WebSyncError(
165 blink::WebSyncError::ErrorTypeNoPermission,
166 "Cannot register a sync event without a window client."));
167 break;
168 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
169 callbacks->onError(
170 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
171 "No service worker is active."));
172 break;
176 void BackgroundSyncProvider::UnregisterCallback(
177 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks,
178 BackgroundSyncError error) {
179 // TODO(iclelland): Determine the correct error message to return in each case
180 switch (error) {
181 case BACKGROUND_SYNC_ERROR_NONE:
182 callbacks->onSuccess(true);
183 break;
184 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
185 callbacks->onSuccess(false);
186 break;
187 case BACKGROUND_SYNC_ERROR_STORAGE:
188 callbacks->onError(
189 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
190 "Background Sync is disabled."));
191 break;
192 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
193 // This error should never be returned from
194 // BackgroundSyncManager::Unregister
195 NOTREACHED();
196 break;
197 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
198 callbacks->onError(
199 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
200 "No service worker is active."));
201 break;
205 void BackgroundSyncProvider::GetRegistrationCallback(
206 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
207 BackgroundSyncError error,
208 const SyncRegistrationPtr& options) {
209 // TODO(iclelland): Determine the correct error message to return in each case
210 scoped_ptr<blink::WebSyncRegistration> result;
211 switch (error) {
212 case BACKGROUND_SYNC_ERROR_NONE:
213 if (!options.is_null())
214 result =
215 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
216 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
217 break;
218 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
219 callbacks->onSuccess(nullptr);
220 break;
221 case BACKGROUND_SYNC_ERROR_STORAGE:
222 callbacks->onError(
223 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
224 "Background Sync is disabled."));
225 break;
226 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
227 // This error should never be returned from
228 // BackgroundSyncManager::GetRegistration
229 NOTREACHED();
230 break;
231 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
232 callbacks->onError(
233 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
234 "No service worker is active."));
235 break;
239 void BackgroundSyncProvider::GetRegistrationsCallback(
240 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
241 BackgroundSyncError error,
242 const mojo::Array<SyncRegistrationPtr>& registrations) {
243 // TODO(iclelland): Determine the correct error message to return in each case
244 switch (error) {
245 case BACKGROUND_SYNC_ERROR_NONE: {
246 blink::WebVector<blink::WebSyncRegistration*> results(
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])
251 .release();
253 callbacks->onSuccess(results);
254 break;
256 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
257 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
258 // These errors should never be returned from
259 // BackgroundSyncManager::GetRegistrations
260 NOTREACHED();
261 break;
262 case BACKGROUND_SYNC_ERROR_STORAGE:
263 callbacks->onError(
264 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
265 "Background Sync is disabled."));
266 break;
267 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
268 callbacks->onError(
269 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
270 "No service worker is active."));
271 break;
275 void BackgroundSyncProvider::GetPermissionStatusCallback(
276 scoped_ptr<blink::WebSyncGetPermissionStatusCallbacks> callbacks,
277 BackgroundSyncError error,
278 PermissionStatus status) {
279 // TODO(iclelland): Determine the correct error message to return in each case
280 switch (error) {
281 case BACKGROUND_SYNC_ERROR_NONE:
282 switch (status) {
283 case PERMISSION_STATUS_GRANTED:
284 callbacks->onSuccess(blink::WebSyncPermissionStatusGranted);
285 break;
286 case PERMISSION_STATUS_DENIED:
287 callbacks->onSuccess(blink::WebSyncPermissionStatusDenied);
288 break;
289 case PERMISSION_STATUS_ASK:
290 callbacks->onSuccess(blink::WebSyncPermissionStatusPrompt);
291 break;
293 break;
294 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
295 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
296 // These errors should never be returned from
297 // BackgroundSyncManager::GetPermissionStatus
298 NOTREACHED();
299 break;
300 case BACKGROUND_SYNC_ERROR_STORAGE:
301 callbacks->onError(
302 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
303 "Background Sync is disabled."));
304 break;
305 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
306 callbacks->onError(
307 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
308 "No service worker is active."));
309 break;
313 BackgroundSyncServicePtr&
314 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
315 if (!background_sync_service_.get()) {
316 service_registry_->ConnectToRemoteService(
317 mojo::GetProxy(&background_sync_service_));
319 return background_sync_service_;
322 } // namespace content