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" | |
| 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 DCHECK(syncProvider); |
|
iclelland
2016/11/14 18:43:07
I don't think the DCHECK is necessary here
adithyas
2016/11/14 19:51:50
Removed, I had that in for debugging :)
| |
| 29 return syncProvider; | |
| 30 } | 30 } |
| 31 | 31 |
| 32 SyncManager::SyncManager(ServiceWorkerRegistration* registration) | 32 SyncManager::SyncManager(ServiceWorkerRegistration* registration) |
| 33 : m_registration(registration) { | 33 : m_registration(registration) { |
| 34 ASSERT(registration); | 34 DCHECK(registration); |
| 35 } | 35 } |
| 36 | 36 |
| 37 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, | 37 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, |
| 38 ExecutionContext* context, | 38 ExecutionContext* context, |
| 39 const String& tag) { | 39 const String& tag) { |
| 40 // TODO(jkarlin): Wait for the registration to become active instead of | 40 // TODO(jkarlin): Wait for the registration to become active instead of |
| 41 // rejecting. See crbug.com/542437. | 41 // rejecting. See crbug.com/542437. |
| 42 if (!m_registration->active()) | 42 if (!m_registration->active()) |
| 43 return ScriptPromise::rejectWithDOMException( | 43 return ScriptPromise::rejectWithDOMException( |
| 44 scriptState, | 44 scriptState, |
| 45 DOMException::create(AbortError, | 45 DOMException::create(AbortError, |
| 46 "Registration failed - no active Service Worker")); | 46 "Registration failed - no active Service Worker")); |
| 47 | 47 |
| 48 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 48 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 49 ScriptPromise promise = resolver->promise(); | 49 ScriptPromise promise = resolver->promise(); |
| 50 | 50 |
| 51 WebSyncRegistration* webSyncRegistration = new WebSyncRegistration( | 51 mojom::blink::SyncRegistrationPtr syncRegistration = |
| 52 WebSyncRegistration::UNREGISTERED_SYNC_ID /* id */, tag, | 52 mojom::blink::SyncRegistration::New(); |
| 53 WebSyncRegistration::NetworkStateOnline /* networkState */ | 53 syncRegistration->id = SyncManager::kUnregisteredSyncID; |
| 54 ); | 54 syncRegistration->tag = tag; |
| 55 syncRegistration->network_state = | |
| 56 blink::mojom::BackgroundSyncNetworkState::ONLINE; | |
| 57 | |
| 55 backgroundSyncProvider()->registerBackgroundSync( | 58 backgroundSyncProvider()->registerBackgroundSync( |
| 56 webSyncRegistration, m_registration->webRegistration(), | 59 std::move(syncRegistration), m_registration->webRegistration(), |
| 57 new SyncRegistrationCallbacks(resolver, m_registration)); | 60 makeUnique<SyncRegistrationCallbacks>(resolver, m_registration)); |
| 58 | 61 |
| 59 return promise; | 62 return promise; |
| 60 } | 63 } |
| 61 | 64 |
| 62 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { | 65 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { |
| 63 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 66 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 64 ScriptPromise promise = resolver->promise(); | 67 ScriptPromise promise = resolver->promise(); |
| 65 | 68 |
| 66 backgroundSyncProvider()->getRegistrations( | 69 backgroundSyncProvider()->getRegistrations( |
| 67 m_registration->webRegistration(), | 70 m_registration->webRegistration(), |
| 68 new SyncGetRegistrationsCallbacks(resolver, m_registration)); | 71 makeUnique<SyncGetRegistrationsCallbacks>(resolver, m_registration)); |
| 69 | 72 |
| 70 return promise; | 73 return promise; |
| 71 } | 74 } |
| 72 | 75 |
| 73 DEFINE_TRACE(SyncManager) { | 76 DEFINE_TRACE(SyncManager) { |
| 74 visitor->trace(m_registration); | 77 visitor->trace(m_registration); |
| 75 } | 78 } |
| 76 | 79 |
| 77 } // namespace blink | 80 } // namespace blink |
| OLD | NEW |