| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/SyncManager.h" | 5 #include "modules/background_sync/SyncManager.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "modules/background_sync/BackgroundSyncProvider.h" | |
| 15 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 14 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 15 #include "platform/heap/Persistent.h" |
| 16 #include "public/platform/InterfaceProvider.h" |
| 16 #include "public/platform/Platform.h" | 17 #include "public/platform/Platform.h" |
| 18 #include "wtf/Functional.h" |
| 17 #include "wtf/PtrUtil.h" | 19 #include "wtf/PtrUtil.h" |
| 18 #include "wtf/ThreadSpecific.h" | |
| 19 | 20 |
| 20 namespace blink { | 21 namespace blink { |
| 21 | 22 |
| 22 // static | |
| 23 BackgroundSyncProvider* SyncManager::backgroundSyncProvider() { | |
| 24 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<BackgroundSyncProvider>, | |
| 25 syncProvider, | |
| 26 new ThreadSpecific<BackgroundSyncProvider>); | |
| 27 return syncProvider; | |
| 28 } | |
| 29 | |
| 30 SyncManager::SyncManager(ServiceWorkerRegistration* registration) | 23 SyncManager::SyncManager(ServiceWorkerRegistration* registration) |
| 31 : m_registration(registration) { | 24 : m_registration(registration) { |
| 32 DCHECK(registration); | 25 DCHECK(registration); |
| 33 } | 26 } |
| 34 | 27 |
| 35 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, | 28 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, |
| 36 const String& tag) { | 29 const String& tag) { |
| 37 // TODO(jkarlin): Wait for the registration to become active instead of | 30 // TODO(jkarlin): Wait for the registration to become active instead of |
| 38 // rejecting. See crbug.com/542437. | 31 // rejecting. See crbug.com/542437. |
| 39 if (!m_registration->active()) | 32 if (!m_registration->active()) |
| 40 return ScriptPromise::rejectWithDOMException( | 33 return ScriptPromise::rejectWithDOMException( |
| 41 scriptState, | 34 scriptState, |
| 42 DOMException::create(AbortError, | 35 DOMException::create(AbortError, |
| 43 "Registration failed - no active Service Worker")); | 36 "Registration failed - no active Service Worker")); |
| 44 | 37 |
| 45 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 38 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 46 ScriptPromise promise = resolver->promise(); | 39 ScriptPromise promise = resolver->promise(); |
| 47 | 40 |
| 48 mojom::blink::SyncRegistrationPtr syncRegistration = | 41 mojom::blink::SyncRegistrationPtr syncRegistration = |
| 49 mojom::blink::SyncRegistration::New(); | 42 mojom::blink::SyncRegistration::New(); |
| 50 syncRegistration->id = SyncManager::kUnregisteredSyncID; | 43 syncRegistration->id = SyncManager::kUnregisteredSyncID; |
| 51 syncRegistration->tag = tag; | 44 syncRegistration->tag = tag; |
| 52 syncRegistration->network_state = | 45 syncRegistration->network_state = |
| 53 blink::mojom::BackgroundSyncNetworkState::ONLINE; | 46 blink::mojom::BackgroundSyncNetworkState::ONLINE; |
| 54 | 47 |
| 55 backgroundSyncProvider()->registerBackgroundSync( | 48 getBackgroundSyncServicePtr()->Register( |
| 56 std::move(syncRegistration), m_registration->webRegistration(), resolver); | 49 std::move(syncRegistration), |
| 50 m_registration->webRegistration()->registrationId(), |
| 51 convertToBaseCallback( |
| 52 WTF::bind(SyncManager::registerCallback, wrapPersistent(resolver)))); |
| 57 | 53 |
| 58 return promise; | 54 return promise; |
| 59 } | 55 } |
| 60 | 56 |
| 61 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { | 57 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { |
| 62 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 58 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 63 ScriptPromise promise = resolver->promise(); | 59 ScriptPromise promise = resolver->promise(); |
| 64 | 60 |
| 65 backgroundSyncProvider()->getRegistrations(m_registration->webRegistration(), | 61 getBackgroundSyncServicePtr()->GetRegistrations( |
| 66 resolver); | 62 m_registration->webRegistration()->registrationId(), |
| 63 convertToBaseCallback(WTF::bind(&SyncManager::getRegistrationsCallback, |
| 64 wrapPersistent(resolver)))); |
| 67 | 65 |
| 68 return promise; | 66 return promise; |
| 69 } | 67 } |
| 70 | 68 |
| 69 const mojom::blink::BackgroundSyncServicePtr& |
| 70 SyncManager::getBackgroundSyncServicePtr() { |
| 71 if (!m_backgroundSyncService.get()) { |
| 72 Platform::current()->interfaceProvider()->getInterface( |
| 73 mojo::GetProxy(&m_backgroundSyncService)); |
| 74 } |
| 75 return m_backgroundSyncService; |
| 76 } |
| 77 |
| 78 // static |
| 79 void SyncManager::registerCallback(ScriptPromiseResolver* resolver, |
| 80 mojom::blink::BackgroundSyncError error, |
| 81 mojom::blink::SyncRegistrationPtr options) { |
| 82 // TODO(iclelland): Determine the correct error message to return in each case |
| 83 switch (error) { |
| 84 case mojom::blink::BackgroundSyncError::NONE: |
| 85 if (!options) { |
| 86 resolver->resolve(v8::Null(resolver->getScriptState()->isolate())); |
| 87 return; |
| 88 } |
| 89 resolver->resolve(); |
| 90 break; |
| 91 case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| 92 NOTREACHED(); |
| 93 break; |
| 94 case mojom::blink::BackgroundSyncError::STORAGE: |
| 95 resolver->reject( |
| 96 DOMException::create(UnknownError, "Background Sync is disabled.")); |
| 97 break; |
| 98 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| 99 resolver->reject( |
| 100 DOMException::create(InvalidAccessError, |
| 101 "Attempted to register a sync event without a " |
| 102 "window or registration tag too long.")); |
| 103 break; |
| 104 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| 105 resolver->reject( |
| 106 DOMException::create(PermissionDeniedError, "Permission denied.")); |
| 107 break; |
| 108 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| 109 resolver->reject( |
| 110 DOMException::create(UnknownError, "No service worker is active.")); |
| 111 break; |
| 112 } |
| 113 } |
| 114 |
| 115 // static |
| 116 void SyncManager::getRegistrationsCallback( |
| 117 ScriptPromiseResolver* resolver, |
| 118 mojom::blink::BackgroundSyncError error, |
| 119 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { |
| 120 // TODO(iclelland): Determine the correct error message to return in each case |
| 121 switch (error) { |
| 122 case mojom::blink::BackgroundSyncError::NONE: { |
| 123 Vector<String> tags; |
| 124 for (const auto& r : registrations.storage()) { |
| 125 tags.append(r->tag); |
| 126 } |
| 127 resolver->resolve(tags); |
| 128 break; |
| 129 } |
| 130 case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| 131 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| 132 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| 133 // These errors should never be returned from |
| 134 // BackgroundSyncManager::GetRegistrations |
| 135 NOTREACHED(); |
| 136 break; |
| 137 case mojom::blink::BackgroundSyncError::STORAGE: |
| 138 resolver->reject( |
| 139 DOMException::create(UnknownError, "Background Sync is disabled.")); |
| 140 break; |
| 141 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| 142 resolver->reject( |
| 143 DOMException::create(UnknownError, "No service worker is active.")); |
| 144 break; |
| 145 } |
| 146 } |
| 147 |
| 71 DEFINE_TRACE(SyncManager) { | 148 DEFINE_TRACE(SyncManager) { |
| 72 visitor->trace(m_registration); | 149 visitor->trace(m_registration); |
| 73 } | 150 } |
| 74 | 151 |
| 75 } // namespace blink | 152 } // namespace blink |
| OLD | NEW |