Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "platform/WebTaskRunner.h" | |
|
jbroman
2016/11/07 19:48:10
super-nit: This include seems unused now that the
adithyas
2016/11/08 16:47:47
Done.
| |
| 8 #include "public/platform/InterfaceProvider.h" | |
| 9 #include "public/platform/Platform.h" | |
| 10 #include "public/platform/modules/background_sync/WebSyncError.h" | |
| 11 #include "wtf/Functional.h" | |
| 12 #include <memory> | |
| 13 #include <stddef.h> | |
| 14 #include <utility> | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 void BackgroundSyncProvider::registerBackgroundSync( | |
| 19 mojom::blink::SyncRegistrationPtr options, | |
| 20 WebServiceWorkerRegistration* serviceWorkerRegistration, | |
| 21 std::unique_ptr<SyncRegistrationCallbacks> callbacks) { | |
| 22 DCHECK(options); | |
| 23 DCHECK(serviceWorkerRegistration); | |
| 24 DCHECK(callbacks); | |
| 25 int64_t serviceWorkerRegistrationId = | |
| 26 serviceWorkerRegistration->registrationId(); | |
| 27 | |
| 28 GetBackgroundSyncServicePtr()->Register( | |
| 29 std::move(options), serviceWorkerRegistrationId, | |
| 30 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback, | |
| 31 WTF::passed(std::move(callbacks))))); | |
|
haraken
2016/11/08 07:11:36
Why do we need WTF::passed? It looks strange that
dcheng
2016/11/08 07:29:27
WTF::passed() only binds to rvalues, so this needs
| |
| 32 } | |
| 33 | |
| 34 void BackgroundSyncProvider::getRegistrations( | |
| 35 WebServiceWorkerRegistration* serviceWorkerRegistration, | |
| 36 std::unique_ptr<SyncGetRegistrationsCallbacks> callbacks) { | |
| 37 DCHECK(serviceWorkerRegistration); | |
| 38 DCHECK(callbacks); | |
| 39 int64_t serviceWorkerRegistrationId = | |
| 40 serviceWorkerRegistration->registrationId(); | |
| 41 | |
| 42 GetBackgroundSyncServicePtr()->GetRegistrations( | |
| 43 serviceWorkerRegistrationId, | |
| 44 convertToBaseCallback( | |
| 45 WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback, | |
| 46 WTF::passed(std::move(callbacks))))); | |
|
haraken
2016/11/08 07:11:36
Ditto.
| |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 void BackgroundSyncProvider::registerCallback( | |
| 51 std::unique_ptr<SyncRegistrationCallbacks> callbacks, | |
| 52 mojom::blink::BackgroundSyncError error, | |
| 53 mojom::blink::SyncRegistrationPtr options) { | |
| 54 // TODO(iclelland): Determine the correct error message to return in each case | |
| 55 switch (error) { | |
| 56 case mojom::blink::BackgroundSyncError::NONE: | |
| 57 if (!options.is_null()) | |
| 58 callbacks->onSuccess(std::move(options)); | |
| 59 break; | |
| 60 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 61 NOTREACHED(); | |
| 62 break; | |
| 63 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 64 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | |
| 65 "Background Sync is disabled.")); | |
| 66 break; | |
| 67 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 68 callbacks->onError( | |
| 69 blink::WebSyncError(WebSyncError::ErrorTypeNoPermission, | |
| 70 "Attempted to register a sync event without a " | |
| 71 "window or registration tag too long.")); | |
| 72 break; | |
| 73 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 74 callbacks->onError(blink::WebSyncError( | |
| 75 WebSyncError::ErrorTypePermissionDenied, "Permission denied.")); | |
| 76 break; | |
| 77 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 78 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | |
| 79 "No service worker is active.")); | |
| 80 break; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 void BackgroundSyncProvider::getRegistrationsCallback( | |
| 86 std::unique_ptr<SyncGetRegistrationsCallbacks> syncGetRegistrationCallbacks, | |
| 87 mojom::blink::BackgroundSyncError error, | |
| 88 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { | |
| 89 // TODO(iclelland): Determine the correct error message to return in each case | |
| 90 switch (error) { | |
| 91 case mojom::blink::BackgroundSyncError::NONE: { | |
| 92 WebVector<mojom::blink::SyncRegistration*> results(registrations.size()); | |
| 93 for (size_t i = 0; i < registrations.size(); ++i) { | |
| 94 results[i] = registrations[i].get(); | |
| 95 } | |
| 96 syncGetRegistrationCallbacks->onSuccess(results); | |
|
haraken
2016/11/08 07:11:36
Why can't we directly pass in registrations?
adithyas
2016/11/08 16:47:47
We can! I'll be fixing this in a follow-up CL (htt
| |
| 97 break; | |
| 98 } | |
| 99 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 100 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 101 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 102 // These errors should never be returned from | |
| 103 // BackgroundSyncManager::GetRegistrations | |
| 104 NOTREACHED(); | |
| 105 break; | |
| 106 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 107 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | |
| 108 WebSyncError::ErrorTypeUnknown, "Background Sync is disabled.")); | |
| 109 break; | |
| 110 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 111 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | |
| 112 WebSyncError::ErrorTypeUnknown, "No service worker is active.")); | |
| 113 break; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 mojom::blink::BackgroundSyncServicePtr& | |
| 118 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { | |
| 119 if (!m_backgroundSyncService.get()) { | |
| 120 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = | |
| 121 mojo::GetProxy(&m_backgroundSyncService); | |
| 122 Platform::current()->interfaceProvider()->getInterface(std::move(request)); | |
| 123 } | |
| 124 return m_backgroundSyncService; | |
| 125 } | |
| 126 | |
| 127 } // namespace blink | |
| OLD | NEW |