Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: third_party/WebKit/Source/modules/background_sync/BackgroundSyncProvider.cpp

Issue 2473483012: Move content/child/background_sync to Blink. (Closed)
Patch Set: Fix nits Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "modules/background_sync/BackgroundSyncProvider.h"
6
7 #include "public/platform/InterfaceProvider.h"
8 #include "public/platform/Platform.h"
9 #include "public/platform/modules/background_sync/WebSyncError.h"
10 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
11 #include "wtf/Functional.h"
12
13 namespace blink {
14
15 void BackgroundSyncProvider::registerBackgroundSync(
16 mojom::blink::SyncRegistrationPtr options,
17 WebServiceWorkerRegistration* serviceWorkerRegistration,
18 std::unique_ptr<SyncRegistrationCallbacks> callbacks) {
19 DCHECK(options);
20 DCHECK(serviceWorkerRegistration);
21 DCHECK(callbacks);
22 int64_t serviceWorkerRegistrationId =
23 serviceWorkerRegistration->registrationId();
24
25 GetBackgroundSyncServicePtr()->Register(
26 std::move(options), serviceWorkerRegistrationId,
27 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback,
28 WTF::passed(std::move(callbacks)))));
29 }
30
31 void BackgroundSyncProvider::getRegistrations(
32 WebServiceWorkerRegistration* serviceWorkerRegistration,
33 std::unique_ptr<SyncGetRegistrationsCallbacks> callbacks) {
34 DCHECK(serviceWorkerRegistration);
35 DCHECK(callbacks);
36 int64_t serviceWorkerRegistrationId =
37 serviceWorkerRegistration->registrationId();
38
39 GetBackgroundSyncServicePtr()->GetRegistrations(
40 serviceWorkerRegistrationId,
41 convertToBaseCallback(
42 WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback,
43 WTF::passed(std::move(callbacks)))));
44 }
45
46 // static
47 void BackgroundSyncProvider::registerCallback(
48 std::unique_ptr<SyncRegistrationCallbacks> callbacks,
49 mojom::blink::BackgroundSyncError error,
50 mojom::blink::SyncRegistrationPtr options) {
51 // TODO(iclelland): Determine the correct error message to return in each case
52 switch (error) {
53 case mojom::blink::BackgroundSyncError::NONE:
54 if (!options.is_null())
55 callbacks->onSuccess(std::move(options));
56 break;
57 case mojom::blink::BackgroundSyncError::NOT_FOUND:
58 NOTREACHED();
59 break;
60 case mojom::blink::BackgroundSyncError::STORAGE:
61 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown,
62 "Background Sync is disabled."));
63 break;
64 case mojom::blink::BackgroundSyncError::NOT_ALLOWED:
65 callbacks->onError(
66 blink::WebSyncError(WebSyncError::ErrorTypeNoPermission,
67 "Attempted to register a sync event without a "
68 "window or registration tag too long."));
69 break;
70 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED:
71 callbacks->onError(blink::WebSyncError(
72 WebSyncError::ErrorTypePermissionDenied, "Permission denied."));
73 break;
74 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER:
75 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown,
76 "No service worker is active."));
77 break;
78 }
79 }
80
81 // static
82 void BackgroundSyncProvider::getRegistrationsCallback(
83 std::unique_ptr<SyncGetRegistrationsCallbacks> syncGetRegistrationCallbacks,
84 mojom::blink::BackgroundSyncError error,
85 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) {
86 // TODO(iclelland): Determine the correct error message to return in each case
87 switch (error) {
88 case mojom::blink::BackgroundSyncError::NONE: {
89 WebVector<mojom::blink::SyncRegistration*> results(registrations.size());
90 for (size_t i = 0; i < registrations.size(); ++i) {
91 results[i] = registrations[i].get();
92 }
93 syncGetRegistrationCallbacks->onSuccess(results);
94 break;
95 }
96 case mojom::blink::BackgroundSyncError::NOT_FOUND:
97 case mojom::blink::BackgroundSyncError::NOT_ALLOWED:
98 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED:
99 // These errors should never be returned from
100 // BackgroundSyncManager::GetRegistrations
101 NOTREACHED();
102 break;
103 case mojom::blink::BackgroundSyncError::STORAGE:
104 syncGetRegistrationCallbacks->onError(blink::WebSyncError(
105 WebSyncError::ErrorTypeUnknown, "Background Sync is disabled."));
106 break;
107 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER:
108 syncGetRegistrationCallbacks->onError(blink::WebSyncError(
109 WebSyncError::ErrorTypeUnknown, "No service worker is active."));
110 break;
111 }
112 }
113
114 mojom::blink::BackgroundSyncServicePtr&
115 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
116 if (!m_backgroundSyncService.get()) {
117 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request =
118 mojo::GetProxy(&m_backgroundSyncService);
119 Platform::current()->interfaceProvider()->getInterface(std::move(request));
120 }
121 return m_backgroundSyncService;
122 }
123
124 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698