| 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" |
| 14 #include "modules/background_sync/SyncCallbacks.h" | 15 #include "modules/background_sync/SyncCallbacks.h" |
| 15 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 16 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 16 #include "public/platform/Platform.h" | 17 #include "public/platform/Platform.h" |
| 17 #include "public/platform/modules/background_sync/WebSyncProvider.h" | 18 #include "wtf/PtrUtil.h" |
| 18 #include "public/platform/modules/background_sync/WebSyncRegistration.h" | 19 #include "wtf/ThreadSpecific.h" |
| 19 #include "wtf/RefPtr.h" | |
| 20 | 20 |
| 21 namespace blink { | 21 namespace blink { |
| 22 namespace { | |
| 23 | 22 |
| 24 WebSyncProvider* backgroundSyncProvider() { | 23 // static |
| 25 WebSyncProvider* webSyncProvider = | 24 BackgroundSyncProvider* SyncManager::backgroundSyncProvider() { |
| 26 Platform::current()->backgroundSyncProvider(); | 25 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<BackgroundSyncProvider>, |
| 27 ASSERT(webSyncProvider); | 26 syncProvider, |
| 28 return webSyncProvider; | 27 new ThreadSpecific<BackgroundSyncProvider>); |
| 29 } | 28 return syncProvider; |
| 30 } | 29 } |
| 31 | 30 |
| 32 SyncManager::SyncManager(ServiceWorkerRegistration* registration) | 31 SyncManager::SyncManager(ServiceWorkerRegistration* registration) |
| 33 : m_registration(registration) { | 32 : m_registration(registration) { |
| 34 ASSERT(registration); | 33 DCHECK(registration); |
| 35 } | 34 } |
| 36 | 35 |
| 37 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, | 36 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, |
| 38 ExecutionContext* context, | 37 ExecutionContext* context, |
| 39 const String& tag) { | 38 const String& tag) { |
| 40 // TODO(jkarlin): Wait for the registration to become active instead of | 39 // TODO(jkarlin): Wait for the registration to become active instead of |
| 41 // rejecting. See crbug.com/542437. | 40 // rejecting. See crbug.com/542437. |
| 42 if (!m_registration->active()) | 41 if (!m_registration->active()) |
| 43 return ScriptPromise::rejectWithDOMException( | 42 return ScriptPromise::rejectWithDOMException( |
| 44 scriptState, | 43 scriptState, |
| 45 DOMException::create(AbortError, | 44 DOMException::create(AbortError, |
| 46 "Registration failed - no active Service Worker")); | 45 "Registration failed - no active Service Worker")); |
| 47 | 46 |
| 48 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 47 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 49 ScriptPromise promise = resolver->promise(); | 48 ScriptPromise promise = resolver->promise(); |
| 50 | 49 |
| 51 WebSyncRegistration* webSyncRegistration = new WebSyncRegistration( | 50 mojom::blink::SyncRegistrationPtr syncRegistration = |
| 52 WebSyncRegistration::UNREGISTERED_SYNC_ID /* id */, tag, | 51 mojom::blink::SyncRegistration::New(); |
| 53 WebSyncRegistration::NetworkStateOnline /* networkState */ | 52 syncRegistration->id = SyncManager::kUnregisteredSyncID; |
| 54 ); | 53 syncRegistration->tag = tag; |
| 54 syncRegistration->network_state = |
| 55 blink::mojom::BackgroundSyncNetworkState::ONLINE; |
| 56 |
| 55 backgroundSyncProvider()->registerBackgroundSync( | 57 backgroundSyncProvider()->registerBackgroundSync( |
| 56 webSyncRegistration, m_registration->webRegistration(), | 58 std::move(syncRegistration), m_registration->webRegistration(), |
| 57 new SyncRegistrationCallbacks(resolver, m_registration)); | 59 makeUnique<SyncRegistrationCallbacks>(resolver, m_registration)); |
| 58 | 60 |
| 59 return promise; | 61 return promise; |
| 60 } | 62 } |
| 61 | 63 |
| 62 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { | 64 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { |
| 63 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 65 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 64 ScriptPromise promise = resolver->promise(); | 66 ScriptPromise promise = resolver->promise(); |
| 65 | 67 |
| 66 backgroundSyncProvider()->getRegistrations( | 68 backgroundSyncProvider()->getRegistrations( |
| 67 m_registration->webRegistration(), | 69 m_registration->webRegistration(), |
| 68 new SyncGetRegistrationsCallbacks(resolver, m_registration)); | 70 makeUnique<SyncGetRegistrationsCallbacks>(resolver, m_registration)); |
| 69 | 71 |
| 70 return promise; | 72 return promise; |
| 71 } | 73 } |
| 72 | 74 |
| 73 DEFINE_TRACE(SyncManager) { | 75 DEFINE_TRACE(SyncManager) { |
| 74 visitor->trace(m_registration); | 76 visitor->trace(m_registration); |
| 75 } | 77 } |
| 76 | 78 |
| 77 } // namespace blink | 79 } // namespace blink |
| OLD | NEW |