Chromium Code Reviews| Index: third_party/WebKit/Source/modules/background_sync/SyncManager.cpp |
| diff --git a/third_party/WebKit/Source/modules/background_sync/SyncManager.cpp b/third_party/WebKit/Source/modules/background_sync/SyncManager.cpp |
| index 5215bc3f595f2216503fba38dbc36e5024f63cdf..56ce69c8ba355a786a53d5ce5bd4e6296670c77b 100644 |
| --- a/third_party/WebKit/Source/modules/background_sync/SyncManager.cpp |
| +++ b/third_party/WebKit/Source/modules/background_sync/SyncManager.cpp |
| @@ -11,24 +11,17 @@ |
| #include "core/dom/DOMException.h" |
| #include "core/dom/ExceptionCode.h" |
| #include "core/dom/ExecutionContext.h" |
| -#include "modules/background_sync/BackgroundSyncProvider.h" |
| #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| +#include "platform/heap/Persistent.h" |
| +#include "public/platform/InterfaceProvider.h" |
| #include "public/platform/Platform.h" |
| +#include "wtf/Functional.h" |
| #include "wtf/PtrUtil.h" |
| -#include "wtf/ThreadSpecific.h" |
| namespace blink { |
| -// static |
| -BackgroundSyncProvider* SyncManager::backgroundSyncProvider() { |
| - DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<BackgroundSyncProvider>, |
| - syncProvider, |
| - new ThreadSpecific<BackgroundSyncProvider>); |
| - return syncProvider; |
| -} |
| - |
| SyncManager::SyncManager(ServiceWorkerRegistration* registration) |
| - : m_registration(registration) { |
| + : 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!
|
| DCHECK(registration); |
| } |
| @@ -52,22 +45,107 @@ ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, |
| syncRegistration->network_state = |
| blink::mojom::BackgroundSyncNetworkState::ONLINE; |
| - backgroundSyncProvider()->registerBackgroundSync( |
| - std::move(syncRegistration), m_registration->webRegistration(), resolver); |
| + getBackgroundSyncServicePtr()->Register( |
| + std::move(syncRegistration), |
| + m_registration->webRegistration()->registrationId(), |
| + convertToBaseCallback( |
| + WTF::bind(SyncManager::registerCallback, wrapPersistent(resolver)))); |
| return promise; |
| } |
| +// static |
| +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
|
| + mojom::blink::BackgroundSyncError error, |
| + mojom::blink::SyncRegistrationPtr options) { |
| + // TODO(iclelland): Determine the correct error message to return in each case |
| + switch (error) { |
| + case mojom::blink::BackgroundSyncError::NONE: |
| + if (!options) { |
| + resolver->resolve(v8::Null(resolver->getScriptState()->isolate())); |
| + return; |
| + } |
| + resolver->resolve(); |
| + break; |
| + case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| + NOTREACHED(); |
| + break; |
| + case mojom::blink::BackgroundSyncError::STORAGE: |
| + resolver->reject( |
| + DOMException::create(UnknownError, "Background Sync is disabled.")); |
| + break; |
| + case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| + resolver->reject( |
| + DOMException::create(InvalidAccessError, |
| + "Attempted to register a sync event without a " |
| + "window or registration tag too long.")); |
| + break; |
| + case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| + resolver->reject( |
| + DOMException::create(PermissionDeniedError, "Permission denied.")); |
| + break; |
| + case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| + resolver->reject( |
| + DOMException::create(UnknownError, "No service worker is active.")); |
| + break; |
| + } |
| +} |
| + |
| ScriptPromise SyncManager::getTags(ScriptState* scriptState) { |
| ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| ScriptPromise promise = resolver->promise(); |
| - backgroundSyncProvider()->getRegistrations(m_registration->webRegistration(), |
| - resolver); |
| + getBackgroundSyncServicePtr()->GetRegistrations( |
| + m_registration->webRegistration()->registrationId(), |
| + convertToBaseCallback(WTF::bind(&SyncManager::getRegistrationsCallback, |
| + wrapPersistent(resolver)))); |
| return promise; |
| } |
| +// static |
| +void SyncManager::getRegistrationsCallback( |
|
iclelland
2016/11/21 20:17:15
Ditto
|
| + ScriptPromiseResolver* resolver, |
| + mojom::blink::BackgroundSyncError error, |
| + mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) { |
| + // TODO(iclelland): Determine the correct error message to return in each case |
| + switch (error) { |
| + case mojom::blink::BackgroundSyncError::NONE: { |
| + Vector<String> tags; |
| + for (const auto& r : registrations.storage()) { |
| + tags.append(r->tag); |
| + } |
| + resolver->resolve(tags); |
| + break; |
| + } |
| + case mojom::blink::BackgroundSyncError::NOT_FOUND: |
| + case mojom::blink::BackgroundSyncError::NOT_ALLOWED: |
| + case mojom::blink::BackgroundSyncError::PERMISSION_DENIED: |
| + // These errors should never be returned from |
| + // BackgroundSyncManager::GetRegistrations |
| + NOTREACHED(); |
| + break; |
| + case mojom::blink::BackgroundSyncError::STORAGE: |
| + resolver->reject( |
| + DOMException::create(UnknownError, "Background Sync is disabled.")); |
| + break; |
| + case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER: |
| + resolver->reject( |
| + DOMException::create(UnknownError, "No service worker is active.")); |
| + break; |
| + } |
| +} |
| + |
| +const mojom::blink::BackgroundSyncServicePtr& |
| +SyncManager::getBackgroundSyncServicePtr() { |
| + if (!m_backgroundSyncService.get()) { |
| + 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.
|
| + mojo::GetProxy(&m_backgroundSyncService); |
| + Platform::current()->interfaceProvider()->getInterface(std::move(request)); |
| + } |
| + return m_backgroundSyncService; |
| +} |
| + |
| DEFINE_TRACE(SyncManager) { |
| visitor->trace(m_registration); |
| } |