[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider.cc
blobbbe67d505b2797bf8eb19abbd3ae0c74b28d689b
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 blink::WebSyncRegistrationCallbacks* callbacks) {
44 DCHECK(options);
45 DCHECK(service_worker_registration);
46 DCHECK(callbacks);
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,
63 int64_t id,
64 const blink::WebString& tag,
65 blink::WebServiceWorkerRegistration* service_worker_registration,
66 blink::WebSyncUnregistrationCallbacks* callbacks) {
67 DCHECK(service_worker_registration);
68 DCHECK(callbacks);
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);
88 DCHECK(callbacks);
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);
107 DCHECK(callbacks);
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);
126 DCHECK(callbacks);
127 int64 service_worker_registration_id =
128 GetServiceWorkerRegistrationId(service_worker_registration);
129 scoped_ptr<blink::WebSyncGetPermissionStatusCallbacks> callbacksPtr(
130 callbacks);
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;
147 switch (error) {
148 case BACKGROUND_SYNC_ERROR_NONE:
149 if (!options.is_null())
150 result =
151 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
152 callbacks->onSuccess(result.release());
153 break;
154 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
155 NOTREACHED();
156 break;
157 case BACKGROUND_SYNC_ERROR_STORAGE:
158 callbacks->onError(
159 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
160 "Background Sync is disabled."));
161 break;
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."));
166 break;
167 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
168 callbacks->onError(
169 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
170 "No service worker is active."));
171 break;
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
179 switch (error) {
180 case BACKGROUND_SYNC_ERROR_NONE:
181 callbacks->onSuccess(new bool(true));
182 break;
183 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
184 callbacks->onSuccess(new bool(false));
185 break;
186 case BACKGROUND_SYNC_ERROR_STORAGE:
187 callbacks->onError(
188 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
189 "Background Sync is disabled."));
190 break;
191 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
192 // This error should never be returned from
193 // BackgroundSyncManager::Unregister
194 NOTREACHED();
195 break;
196 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
197 callbacks->onError(
198 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
199 "No service worker is active."));
200 break;
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;
210 switch (error) {
211 case BACKGROUND_SYNC_ERROR_NONE:
212 if (!options.is_null())
213 result =
214 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
215 callbacks->onSuccess(result.release());
216 break;
217 case BACKGROUND_SYNC_ERROR_NOT_FOUND:
218 callbacks->onSuccess(nullptr);
219 break;
220 case BACKGROUND_SYNC_ERROR_STORAGE:
221 callbacks->onError(
222 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
223 "Background Sync is disabled."));
224 break;
225 case BACKGROUND_SYNC_ERROR_NOT_ALLOWED:
226 // This error should never be returned from
227 // BackgroundSyncManager::GetRegistration
228 NOTREACHED();
229 break;
230 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
231 callbacks->onError(
232 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
233 "No service worker is active."));
234 break;
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;
244 switch (error) {
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);
253 break;
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
258 NOTREACHED();
259 break;
260 case BACKGROUND_SYNC_ERROR_STORAGE:
261 callbacks->onError(
262 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
263 "Background Sync is disabled."));
264 break;
265 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
266 callbacks->onError(
267 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
268 "No service worker is active."));
269 break;
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
278 switch (error) {
279 case BACKGROUND_SYNC_ERROR_NONE:
280 switch (status) {
281 case PERMISSION_STATUS_GRANTED:
282 callbacks->onSuccess(new blink::WebSyncPermissionStatus(
283 blink::WebSyncPermissionStatusGranted));
284 break;
285 case PERMISSION_STATUS_DENIED:
286 callbacks->onSuccess(new blink::WebSyncPermissionStatus(
287 blink::WebSyncPermissionStatusDenied));
288 break;
289 case PERMISSION_STATUS_ASK:
290 callbacks->onSuccess(new blink::WebSyncPermissionStatus(
291 blink::WebSyncPermissionStatusPrompt));
292 break;
294 break;
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
299 NOTREACHED();
300 break;
301 case BACKGROUND_SYNC_ERROR_STORAGE:
302 callbacks->onError(
303 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
304 "Background Sync is disabled."));
305 break;
306 case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
307 callbacks->onError(
308 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
309 "No service worker is active."));
310 break;
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