Chromium Code Reviews| 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), m_backgroundSyncService(nullptr) { |
|
jbroman
2016/11/21 19:25:51
nit: mojo::InterfacePtr (like BackgroundSyncServic
adithyas
2016/11/21 20:27:54
Fixed!
| |
| 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 |
| 57 // static | |
| 58 void SyncManager::registerCallback(ScriptPromiseResolver* resolver, | |
|
iclelland
2016/11/21 20:17:15
nit: Move this below getBackgroundSyncServicePtr t
adithyas
2016/11/21 20:27:54
Moved this and getRegistrationsCallback below getB
| |
| 59 mojom::blink::BackgroundSyncError error, | |
| 60 mojom::blink::SyncRegistrationPtr options) { | |
| 61 // TODO(iclelland): Determine the correct error message to return in each case | |
| 62 switch (error) { | |
| 63 case mojom::blink::BackgroundSyncError::NONE: | |
| 64 if (!options) { | |
| 65 resolver->resolve(v8::Null(resolver->getScriptState()->isolate())); | |
| 66 return; | |
| 67 } | |
| 68 resolver->resolve(); | |
| 69 break; | |
| 70 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 71 NOTREACHED(); | |
| 72 break; | |
| 73 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 74 resolver->reject( | |
| 75 DOMException::create(UnknownError, "Background Sync is disabled.")); | |
| 76 break; | |
| 77 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 78 resolver->reject( | |
| 79 DOMException::create(InvalidAccessError, | |
| 80 "Attempted to register a sync event without a " | |
| 81 "window or registration tag too long.")); | |
| 82 break; | |
| 83 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 84 resolver->reject( | |
| 85 DOMException::create(PermissionDeniedError, "Permission denied.")); | |
| 86 break; | |
| 87 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 88 resolver->reject( | |
| 89 DOMException::create(UnknownError, "No service worker is active.")); | |
| 90 break; | |
| 91 } | |
| 92 } | |
| 93 | |
| 61 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { | 94 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { |
| 62 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 95 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 63 ScriptPromise promise = resolver->promise(); | 96 ScriptPromise promise = resolver->promise(); |
| 64 | 97 |
| 65 backgroundSyncProvider()->getRegistrations(m_registration->webRegistration(), | 98 getBackgroundSyncServicePtr()->GetRegistrations( |
| 66 resolver); | 99 m_registration->webRegistration()->registrationId(), |
| 100 convertToBaseCallback(WTF::bind(&SyncManager::getRegistrationsCallback, | |
| 101 wrapPersistent(resolver)))); | |
| 67 | 102 |
| 68 return promise; | 103 return promise; |
| 69 } | 104 } |
| 70 | 105 |
| 106 // static | |
| 107 void SyncManager::getRegistrationsCallback( | |
|
iclelland
2016/11/21 20:17:15
Ditto
| |
| 108 ScriptPromiseResolver* resolver, | |
| 109 mojom::blink::BackgroundSyncError error, | |
| 110 mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { | |
| 111 // TODO(iclelland): Determine the correct error message to return in each case | |
| 112 switch (error) { | |
| 113 case mojom::blink::BackgroundSyncError::NONE: { | |
| 114 Vector<String> tags; | |
| 115 for (const auto& r : registrations.storage()) { | |
| 116 tags.append(r->tag); | |
| 117 } | |
| 118 resolver->resolve(tags); | |
| 119 break; | |
| 120 } | |
| 121 case mojom::blink::BackgroundSyncError::NOT_FOUND: | |
| 122 case mojom::blink::BackgroundSyncError::NOT_ALLOWED: | |
| 123 case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: | |
| 124 // These errors should never be returned from | |
| 125 // BackgroundSyncManager::GetRegistrations | |
| 126 NOTREACHED(); | |
| 127 break; | |
| 128 case mojom::blink::BackgroundSyncError::STORAGE: | |
| 129 resolver->reject( | |
| 130 DOMException::create(UnknownError, "Background Sync is disabled.")); | |
| 131 break; | |
| 132 case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: | |
| 133 resolver->reject( | |
| 134 DOMException::create(UnknownError, "No service worker is active.")); | |
| 135 break; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 const mojom::blink::BackgroundSyncServicePtr& | |
| 140 SyncManager::getBackgroundSyncServicePtr() { | |
| 141 if (!m_backgroundSyncService.get()) { | |
| 142 mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request = | |
|
jbroman
2016/11/21 19:25:51
super-nit: we usually just call mojo::GetProxy dir
adithyas
2016/11/21 20:27:54
Done.
| |
| 143 mojo::GetProxy(&m_backgroundSyncService); | |
| 144 Platform::current()->interfaceProvider()->getInterface(std::move(request)); | |
| 145 } | |
| 146 return m_backgroundSyncService; | |
| 147 } | |
| 148 | |
| 71 DEFINE_TRACE(SyncManager) { | 149 DEFINE_TRACE(SyncManager) { |
| 72 visitor->trace(m_registration); | 150 visitor->trace(m_registration); |
| 73 } | 151 } |
| 74 | 152 |
| 75 } // namespace blink | 153 } // namespace blink |
| OLD | NEW |