Add test for the ItemChooserDialog class.
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider.cc
blob28ebdf33293d621e8a0f05625a434ab4771342c2
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::DuplicateRegistrationHandle(
147 int handle_id,
148 const BackgroundSyncService::DuplicateRegistrationHandleCallback&
149 callback) {
150 GetBackgroundSyncServicePtr()->DuplicateRegistrationHandle(handle_id,
151 callback);
154 void BackgroundSyncProvider::RegisterCallback(
155 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
156 BackgroundSyncError error,
157 const SyncRegistrationPtr& options) {
158 // TODO(iclelland): Determine the correct error message to return in each case
159 scoped_ptr<blink::WebSyncRegistration> result;
160 switch (error) {
161 case BACKGROUND_SYNC_ERROR_NONE:
162 if (!options.is_null())
163 result =
164 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
165 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
166 break;
167 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
168 NOTREACHED();
169 break;
170 case BACKGROUND_SYNC_ERROR_STORAGE:
171 callbacks->onError(
172 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
173 "Background Sync is disabled."));
174 break;
175 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
176 callbacks->onError(blink::WebSyncError(
177 blink::WebSyncError::ErrorTypeNoPermission,
178 "Cannot register a sync event without a window client."));
179 break;
180 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
181 callbacks->onError(
182 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
183 "No service worker is active."));
184 break;
188 void BackgroundSyncProvider::UnregisterCallback(
189 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks,
190 BackgroundSyncError error) {
191 // TODO(iclelland): Determine the correct error message to return in each case
192 switch (error) {
193 case BACKGROUND_SYNC_ERROR_NONE:
194 callbacks->onSuccess(true);
195 break;
196 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
197 callbacks->onSuccess(false);
198 break;
199 case BACKGROUND_SYNC_ERROR_STORAGE:
200 callbacks->onError(
201 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
202 "Background Sync is disabled."));
203 break;
204 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
205 // This error should never be returned from
206 // BackgroundSyncManager::Unregister
207 NOTREACHED();
208 break;
209 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
210 callbacks->onError(
211 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
212 "No service worker is active."));
213 break;
217 void BackgroundSyncProvider::GetRegistrationCallback(
218 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
219 BackgroundSyncError error,
220 const SyncRegistrationPtr& options) {
221 // TODO(iclelland): Determine the correct error message to return in each case
222 scoped_ptr<blink::WebSyncRegistration> result;
223 switch (error) {
224 case BACKGROUND_SYNC_ERROR_NONE:
225 if (!options.is_null())
226 result =
227 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
228 callbacks->onSuccess(blink::adoptWebPtr(result.release()));
229 break;
230 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
231 callbacks->onSuccess(nullptr);
232 break;
233 case BACKGROUND_SYNC_ERROR_STORAGE:
234 callbacks->onError(
235 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
236 "Background Sync is disabled."));
237 break;
238 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
239 // This error should never be returned from
240 // BackgroundSyncManager::GetRegistration
241 NOTREACHED();
242 break;
243 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
244 callbacks->onError(
245 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
246 "No service worker is active."));
247 break;
251 void BackgroundSyncProvider::GetRegistrationsCallback(
252 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
253 BackgroundSyncError error,
254 const mojo::Array<SyncRegistrationPtr>& registrations) {
255 // TODO(iclelland): Determine the correct error message to return in each case
256 switch (error) {
257 case BACKGROUND_SYNC_ERROR_NONE: {
258 blink::WebVector<blink::WebSyncRegistration*> results(
259 registrations.size());
260 for (size_t i = 0; i < registrations.size(); ++i) {
261 results[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(
262 registrations[i])
263 .release();
265 callbacks->onSuccess(results);
266 break;
268 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
269 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
270 // These errors should never be returned from
271 // BackgroundSyncManager::GetRegistrations
272 NOTREACHED();
273 break;
274 case BACKGROUND_SYNC_ERROR_STORAGE:
275 callbacks->onError(
276 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
277 "Background Sync is disabled."));
278 break;
279 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
280 callbacks->onError(
281 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
282 "No service worker is active."));
283 break;
287 void BackgroundSyncProvider::GetPermissionStatusCallback(
288 scoped_ptr<blink::WebSyncGetPermissionStatusCallbacks> callbacks,
289 BackgroundSyncError error,
290 PermissionStatus status) {
291 // TODO(iclelland): Determine the correct error message to return in each case
292 switch (error) {
293 case BACKGROUND_SYNC_ERROR_NONE:
294 switch (status) {
295 case PERMISSION_STATUS_GRANTED:
296 callbacks->onSuccess(blink::WebSyncPermissionStatusGranted);
297 break;
298 case PERMISSION_STATUS_DENIED:
299 callbacks->onSuccess(blink::WebSyncPermissionStatusDenied);
300 break;
301 case PERMISSION_STATUS_ASK:
302 callbacks->onSuccess(blink::WebSyncPermissionStatusPrompt);
303 break;
305 break;
306 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
307 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
308 // These errors should never be returned from
309 // BackgroundSyncManager::GetPermissionStatus
310 NOTREACHED();
311 break;
312 case BACKGROUND_SYNC_ERROR_STORAGE:
313 callbacks->onError(
314 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
315 "Background Sync is disabled."));
316 break;
317 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
318 callbacks->onError(
319 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
320 "No service worker is active."));
321 break;
325 BackgroundSyncServicePtr&
326 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
327 if (!background_sync_service_.get()) {
328 service_registry_->ConnectToRemoteService(
329 mojo::GetProxy(&background_sync_service_));
331 return background_sync_service_;
334 } // namespace content