Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/background_sync/BackgroundSyncProvider.h" | 5 #include "modules/background_sync/BackgroundSyncProvider.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
| 8 #include "core/dom/DOMException.h" | |
| 9 #include "core/dom/ExceptionCode.h" | |
| 10 #include "platform/heap/Persistent.h" | |
| 7 #include "public/platform/InterfaceProvider.h" | 11 #include "public/platform/InterfaceProvider.h" |
| 8 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
| 9 #include "public/platform/modules/background_sync/WebSyncError.h" | |
| 10 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" | 13 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" |
| 11 #include "wtf/Functional.h" | 14 #include "wtf/Functional.h" |
| 12 | 15 |
| 13 namespace blink { | 16 namespace blink { |
| 14 | 17 |
| 15 void BackgroundSyncProvider::registerBackgroundSync( | 18 void BackgroundSyncProvider::registerBackgroundSync( |
| 16 mojom::blink::SyncRegistrationPtr options, | 19 mojom::blink::SyncRegistrationPtr options, |
| 17 WebServiceWorkerRegistration* serviceWorkerRegistration, | 20 WebServiceWorkerRegistration* serviceWorkerRegistration, |
| 18 std::unique_ptr<SyncRegistrationCallbacks> callbacks) { | 21 ScriptPromiseResolver* resolver) { |
| 19 DCHECK(options); | 22 DCHECK(options); |
| 20 DCHECK(serviceWorkerRegistration); | 23 DCHECK(serviceWorkerRegistration); |
| 21 DCHECK(callbacks); | 24 DCHECK(resolver); |
| 22 int64_t serviceWorkerRegistrationId = | 25 int64_t serviceWorkerRegistrationId = |
| 23 serviceWorkerRegistration->registrationId(); | 26 serviceWorkerRegistration->registrationId(); |
| 24 | 27 |
| 25 GetBackgroundSyncServicePtr()->Register( | 28 GetBackgroundSyncServicePtr()->Register( |
| 26 std::move(options), serviceWorkerRegistrationId, | 29 std::move(options), serviceWorkerRegistrationId, |
| 27 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback, | 30 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback, |
| 28 WTF::passed(std::move(callbacks))))); | 31 wrapPersistent(resolver)))); |
| 29 } | 32 } |
| 30 | 33 |
| 31 void BackgroundSyncProvider::getRegistrations( | 34 void BackgroundSyncProvider::getRegistrations( |
| 32 WebServiceWorkerRegistration* serviceWorkerRegistration, | 35 WebServiceWorkerRegistration* serviceWorkerRegistration, |
| 33 std::unique_ptr<SyncGetRegistrationsCallbacks> callbacks) { | 36 ScriptPromiseResolver* resolver) { |
| 34 DCHECK(serviceWorkerRegistration); | 37 DCHECK(serviceWorkerRegistration); |
| 35 DCHECK(callbacks); | 38 DCHECK(resolver); |
| 36 int64_t serviceWorkerRegistrationId = | 39 int64_t serviceWorkerRegistrationId = |
| 37 serviceWorkerRegistration->registrationId(); | 40 serviceWorkerRegistration->registrationId(); |
| 38 | 41 |
| 39 GetBackgroundSyncServicePtr()->GetRegistrations( | 42 GetBackgroundSyncServicePtr()->GetRegistrations( |
| 40 serviceWorkerRegistrationId, | 43 serviceWorkerRegistrationId, |
| 41 convertToBaseCallback( | 44 convertToBaseCallback( |
| 42 WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback, | 45 WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback, |
| 43 WTF::passed(std::move(callbacks))))); | 46 wrapPersistent(resolver)))); |
| 44 } | 47 } |
| 45 | 48 |
| 46 // static | 49 // static |
| 47 void BackgroundSyncProvider::registerCallback( | 50 void BackgroundSyncProvider::registerCallback( |
| 48 std::unique_ptr<SyncRegistrationCallbacks> callbacks, | 51 ScriptPromiseResolver* resolver, |
| 49 mojom::blink::BackgroundSyncError error, | 52 mojom::blink::BackgroundSyncError error, |
| 50 mojom::blink::SyncRegistrationPtr options) { | 53 mojom::blink::SyncRegistrationPtr options) { |
| 51 // TODO(iclelland): Determine the correct error message to return in each case | 54 // TODO(iclelland): Determine the correct error message to return in each case |
| 52 switch (error) { | 55 switch (error) { |
| 53 case mojom::blink::BackgroundSyncError::NONE: | 56 case mojom::blink::BackgroundSyncError::NONE: |
| 54 if (!options.is_null()) | 57 if (!options) { |
| 55 callbacks->onSuccess(std::move(options)); | 58 resolver->resolve(v8::Null(resolver->getScriptState()->isolate())); |
| 59 return; | |
| 60 } | |
| 61 resolver->resolve(); | |
| 56 break; | 62 break; |
| 57 case mojom::blink::BackgroundSyncError::NOT_FOUND: | 63 case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| 58 NOTREACHED(); | 64 NOTREACHED(); |
| 59 break; | 65 break; |
| 60 case mojom::blink::BackgroundSyncError::STORAGE: | 66 case mojom::blink::BackgroundSyncError::STORAGE: |
| 61 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | 67 resolver->reject( |
| 62 "Background Sync is disabled.")); | 68 DOMException::create(UnknownError, "Background Sync is disabled.")); |
| 63 break; | 69 break; |
| 64 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | 70 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| 65 callbacks->onError( | 71 resolver->reject( |
| 66 blink::WebSyncError(WebSyncError::ErrorTypeNoPermission, | 72 DOMException::create(InvalidAccessError, |
| 67 "Attempted to register a sync event without a " | 73 "Attempted to register a sync event without a " |
| 68 "window or registration tag too long.")); | 74 "window or registration tag too long.")); |
| 69 break; | 75 break; |
| 70 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | 76 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| 71 callbacks->onError(blink::WebSyncError( | 77 resolver->reject( |
| 72 WebSyncError::ErrorTypePermissionDenied, "Permission denied.")); | 78 DOMException::create(PermissionDeniedError, "Permission denied.")); |
| 73 break; | 79 break; |
| 74 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | 80 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| 75 callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown, | 81 resolver->reject( |
| 76 "No service worker is active.")); | 82 DOMException::create(UnknownError, "No service worker is active.")); |
| 77 break; | 83 break; |
| 78 } | 84 } |
| 79 } | 85 } |
| 80 | 86 |
| 81 // static | 87 // static |
| 82 void BackgroundSyncProvider::getRegistrationsCallback( | 88 void BackgroundSyncProvider::getRegistrationsCallback( |
| 83 std::unique_ptr<SyncGetRegistrationsCallbacks> syncGetRegistrationCallbacks, | 89 ScriptPromiseResolver* resolver, |
| 84 mojom::blink::BackgroundSyncError error, | 90 mojom::blink::BackgroundSyncError error, |
| 85 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { | 91 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { |
| 86 // TODO(iclelland): Determine the correct error message to return in each case | 92 // TODO(iclelland): Determine the correct error message to return in each case |
| 87 switch (error) { | 93 switch (error) { |
| 88 case mojom::blink::BackgroundSyncError::NONE: { | 94 case mojom::blink::BackgroundSyncError::NONE: { |
| 89 WebVector<mojom::blink::SyncRegistration*> results(registrations.size()); | 95 Vector<String> tags; |
| 90 for (size_t i = 0; i < registrations.size(); ++i) { | 96 for (const mojom::blink::SyncRegistrationPtr& r : |
|
jbroman
2016/11/18 20:14:17
super-nit: that this line has to wrap is kinda unf
adithyas
2016/11/21 15:25:16
Done.
| |
| 91 results[i] = registrations[i].get(); | 97 registrations.storage()) { |
| 98 tags.append(r->tag); | |
| 92 } | 99 } |
| 93 syncGetRegistrationCallbacks->onSuccess(results); | 100 resolver->resolve(tags); |
| 94 break; | 101 break; |
| 95 } | 102 } |
| 96 case mojom::blink::BackgroundSyncError::NOT_FOUND: | 103 case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| 97 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | 104 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| 98 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | 105 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| 99 // These errors should never be returned from | 106 // These errors should never be returned from |
| 100 // BackgroundSyncManager::GetRegistrations | 107 // BackgroundSyncManager::GetRegistrations |
| 101 NOTREACHED(); | 108 NOTREACHED(); |
| 102 break; | 109 break; |
| 103 case mojom::blink::BackgroundSyncError::STORAGE: | 110 case mojom::blink::BackgroundSyncError::STORAGE: |
| 104 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | 111 resolver->reject( |
| 105 WebSyncError::ErrorTypeUnknown, "Background Sync is disabled.")); | 112 DOMException::create(UnknownError, "Background Sync is disabled.")); |
| 106 break; | 113 break; |
| 107 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | 114 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| 108 syncGetRegistrationCallbacks->onError(blink::WebSyncError( | 115 resolver->reject( |
| 109 WebSyncError::ErrorTypeUnknown, "No service worker is active.")); | 116 DOMException::create(UnknownError, "No service worker is active.")); |
| 110 break; | 117 break; |
| 111 } | 118 } |
| 112 } | 119 } |
| 113 | 120 |
| 114 mojom::blink::BackgroundSyncServicePtr& | 121 mojom::blink::BackgroundSyncServicePtr& |
| 115 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { | 122 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { |
| 116 if (!m_backgroundSyncService.get()) { | 123 if (!m_backgroundSyncService.get()) { |
| 117 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = | 124 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = |
| 118 mojo::GetProxy(&m_backgroundSyncService); | 125 mojo::GetProxy(&m_backgroundSyncService); |
| 119 Platform::current()->interfaceProvider()->getInterface(std::move(request)); | 126 Platform::current()->interfaceProvider()->getInterface(std::move(request)); |
| 120 } | 127 } |
| 121 return m_backgroundSyncService; | 128 return m_backgroundSyncService; |
| 122 } | 129 } |
| 123 | 130 |
| 124 } // namespace blink | 131 } // namespace blink |
| OLD | NEW |