| 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 "bindings/core/v8/ScriptPromiseResolver.h" | |
| 8 #include "core/dom/DOMException.h" | |
| 9 #include "core/dom/ExceptionCode.h" | |
| 10 #include "platform/heap/Persistent.h" | |
| 11 #include "public/platform/InterfaceProvider.h" | |
| 12 #include "public/platform/Platform.h" | |
| 13 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" | |
| 14 #include "wtf/Functional.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 void BackgroundSyncProvider::registerBackgroundSync( | |
| 19 mojom::blink::SyncRegistrationPtr options, | |
| 20 WebServiceWorkerRegistration* serviceWorkerRegistration, | |
| 21 ScriptPromiseResolver* resolver) { | |
| 22 DCHECK(options); | |
| 23 DCHECK(serviceWorkerRegistration); | |
| 24 DCHECK(resolver); | |
| 25 int64_t serviceWorkerRegistrationId = | |
| 26 serviceWorkerRegistration->registrationId(); | |
| 27 | |
| 28 GetBackgroundSyncServicePtr()->Register( | |
| 29 std::move(options), serviceWorkerRegistrationId, | |
| 30 convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback, | |
| 31 wrapPersistent(resolver)))); | |
| 32 } | |
| 33 | |
| 34 void BackgroundSyncProvider::getRegistrations( | |
| 35 WebServiceWorkerRegistration* serviceWorkerRegistration, | |
| 36 ScriptPromiseResolver* resolver) { | |
| 37 DCHECK(serviceWorkerRegistration); | |
| 38 DCHECK(resolver); | |
| 39 int64_t serviceWorkerRegistrationId = | |
| 40 serviceWorkerRegistration->registrationId(); | |
| 41 | |
| 42 GetBackgroundSyncServicePtr()->GetRegistrations( | |
| 43 serviceWorkerRegistrationId, | |
| 44 convertToBaseCallback( | |
| 45 WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback, | |
| 46 wrapPersistent(resolver)))); | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 void BackgroundSyncProvider::registerCallback( | |
| 51 ScriptPromiseResolver* resolver, | |
| 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) { | |
| 58 resolver->resolve(v8::Null(resolver->getScriptState()->isolate())); | |
| 59 return; | |
| 60 } | |
| 61 resolver->resolve(); | |
| 62 break; | |
| 63 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 64 NOTREACHED(); | |
| 65 break; | |
| 66 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 67 resolver->reject( | |
| 68 DOMException::create(UnknownError, "Background Sync is disabled.")); | |
| 69 break; | |
| 70 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 71 resolver->reject( | |
| 72 DOMException::create(InvalidAccessError, | |
| 73 "Attempted to register a sync event without a " | |
| 74 "window or registration tag too long.")); | |
| 75 break; | |
| 76 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 77 resolver->reject( | |
| 78 DOMException::create(PermissionDeniedError, "Permission denied.")); | |
| 79 break; | |
| 80 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 81 resolver->reject( | |
| 82 DOMException::create(UnknownError, "No service worker is active.")); | |
| 83 break; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 void BackgroundSyncProvider::getRegistrationsCallback( | |
| 89 ScriptPromiseResolver* resolver, | |
| 90 mojom::blink::BackgroundSyncError error, | |
| 91 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { | |
| 92 // TODO(iclelland): Determine the correct error message to return in each case | |
| 93 switch (error) { | |
| 94 case mojom::blink::BackgroundSyncError::NONE: { | |
| 95 Vector<String> tags; | |
| 96 for (const auto& r : registrations.storage()) { | |
| 97 tags.append(r->tag); | |
| 98 } | |
| 99 resolver->resolve(tags); | |
| 100 break; | |
| 101 } | |
| 102 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 103 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 104 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 105 // These errors should never be returned from | |
| 106 // BackgroundSyncManager::GetRegistrations | |
| 107 NOTREACHED(); | |
| 108 break; | |
| 109 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 110 resolver->reject( | |
| 111 DOMException::create(UnknownError, "Background Sync is disabled.")); | |
| 112 break; | |
| 113 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 114 resolver->reject( | |
| 115 DOMException::create(UnknownError, "No service worker is active.")); | |
| 116 break; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 mojom::blink::BackgroundSyncServicePtr& | |
| 121 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { | |
| 122 if (!m_backgroundSyncService.get()) { | |
| 123 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = | |
| 124 mojo::GetProxy(&m_backgroundSyncService); | |
| 125 Platform::current()->interfaceProvider()->getInterface(std::move(request)); | |
| 126 } | |
| 127 return m_backgroundSyncService; | |
| 128 } | |
| 129 | |
| 130 } // namespace blink | |
| OLD | NEW |