| 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 "config.h" | 5 #include "config.h" |
| 6 #include "modules/background_sync/SyncManager.h" | 6 #include "modules/background_sync/SyncManager.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
| 12 #include "core/dom/DOMException.h" | 12 #include "core/dom/DOMException.h" |
| 13 #include "core/dom/Document.h" | 13 #include "core/dom/Document.h" |
| 14 #include "core/dom/ExceptionCode.h" | 14 #include "core/dom/ExceptionCode.h" |
| 15 #include "core/dom/ExecutionContext.h" | 15 #include "core/dom/ExecutionContext.h" |
| 16 #include "modules/background_sync/SyncCallbacks.h" | 16 #include "modules/background_sync/SyncCallbacks.h" |
| 17 #include "modules/background_sync/SyncRegistrationOptions.h" | 17 #include "modules/background_sync/SyncRegistrationOptions.h" |
| 18 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 18 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 19 #include "public/platform/Platform.h" | 19 #include "public/platform/Platform.h" |
| 20 #include "public/platform/modules/background_sync/WebSyncProvider.h" | 20 #include "public/platform/modules/background_sync/WebSyncProvider.h" |
| 21 #include "public/platform/modules/background_sync/WebSyncRegistration.h" | 21 #include "public/platform/modules/background_sync/WebSyncRegistration.h" |
| 22 #include "wtf/RefPtr.h" | 22 #include "wtf/RefPtr.h" |
| 23 | 23 |
| 24 | 24 |
| 25 namespace blink { | 25 namespace blink { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 /* This is the minimum period which will be allowed by the Background | |
| 29 * Sync manager process. It is recorded here in order to be able to | |
| 30 * respond to syncManager.minAllowablePeriod. | |
| 31 * The time is expressed in milliseconds, | |
| 32 */ | |
| 33 unsigned long kMinAllowablePeriod = 12 * 60 * 60 * 1000; | |
| 34 | |
| 35 WebSyncProvider* backgroundSyncProvider() | 28 WebSyncProvider* backgroundSyncProvider() |
| 36 { | 29 { |
| 37 WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncProvid
er(); | 30 WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncProvid
er(); |
| 38 ASSERT(webSyncProvider); | 31 ASSERT(webSyncProvider); |
| 39 return webSyncProvider; | 32 return webSyncProvider; |
| 40 } | 33 } |
| 41 | 34 |
| 42 } | 35 } |
| 43 | 36 |
| 44 SyncManager::SyncManager(ServiceWorkerRegistration* registration) | 37 SyncManager::SyncManager(ServiceWorkerRegistration* registration) |
| 45 : m_registration(registration) | 38 : m_registration(registration) |
| 46 { | 39 { |
| 47 ASSERT(registration); | 40 ASSERT(registration); |
| 48 } | 41 } |
| 49 | 42 |
| 50 unsigned long SyncManager::minAllowablePeriod() | |
| 51 { | |
| 52 return kMinAllowablePeriod; | |
| 53 } | |
| 54 | |
| 55 ScriptPromise SyncManager::registerFunction(blink::ScriptState* scriptState, con
st SyncRegistrationOptions& options) | 43 ScriptPromise SyncManager::registerFunction(blink::ScriptState* scriptState, con
st SyncRegistrationOptions& options) |
| 56 { | 44 { |
| 57 if (!m_registration->active()) | 45 if (!m_registration->active()) |
| 58 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Registration failed - no active Service Worker")); | 46 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Registration failed - no active Service Worker")); |
| 59 | 47 |
| 60 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 48 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 61 ScriptPromise promise = resolver->promise(); | 49 ScriptPromise promise = resolver->promise(); |
| 62 | 50 |
| 63 WebSyncRegistration::NetworkType networkType; | 51 WebSyncRegistration webSyncRegistration( |
| 64 String minRequiredNetwork = options.minRequiredNetwork(); | 52 WebSyncRegistration::UNREGISTERED_SYNC_ID /* id */, |
| 65 if (minRequiredNetwork == "network-any") { | 53 WebSyncRegistration::PeriodicityOneShot, |
| 66 networkType = WebSyncRegistration::NetworkType::NetworkTypeAny; | 54 options.tag(), |
| 67 } else if (minRequiredNetwork == "network-non-mobile") { | 55 0 /* minPeriod */, |
| 68 networkType = WebSyncRegistration::NetworkType::NetworkTypeNonMobile; | 56 WebSyncRegistration::NetworkStateAny /* networkState */, |
| 69 } else if (minRequiredNetwork == "network-offline") { | 57 WebSyncRegistration::PowerStateAuto /* powerState */ |
| 70 networkType = WebSyncRegistration::NetworkType::NetworkTypeOffline; | 58 ); |
| 71 } else { | |
| 72 networkType = WebSyncRegistration::NetworkType::NetworkTypeOnline; | |
| 73 } | |
| 74 WebSyncRegistration webSyncRegistration(options.id(), options.minDelay(), op
tions.maxDelay(), options.minPeriod(), networkType, options.allowOnBattery(), op
tions.idleRequired()); | |
| 75 backgroundSyncProvider()->registerBackgroundSync(&webSyncRegistration, m_reg
istration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registra
tion)); | 59 backgroundSyncProvider()->registerBackgroundSync(&webSyncRegistration, m_reg
istration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registra
tion)); |
| 76 | 60 |
| 77 return promise; | 61 return promise; |
| 78 } | 62 } |
| 79 | 63 |
| 80 ScriptPromise SyncManager::getRegistration(blink::ScriptState* scriptState, cons
t String& syncRegistrationId) | 64 ScriptPromise SyncManager::getRegistration(blink::ScriptState* scriptState, cons
t String& syncRegistrationId) |
| 81 { | 65 { |
| 82 if (!m_registration->active()) | 66 if (!m_registration->active()) |
| 83 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); | 67 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
| 84 | 68 |
| 85 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 69 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 86 ScriptPromise promise = resolver->promise(); | 70 ScriptPromise promise = resolver->promise(); |
| 87 | 71 |
| 88 backgroundSyncProvider()->getRegistration(syncRegistrationId, m_registration
->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registration)); | 72 backgroundSyncProvider()->getRegistration(blink::WebSyncRegistration::Period
icityOneShot, syncRegistrationId, m_registration->webRegistration(), new SyncReg
istrationCallbacks(resolver, m_registration)); |
| 89 | 73 |
| 90 return promise; | 74 return promise; |
| 91 } | 75 } |
| 92 | 76 |
| 93 ScriptPromise SyncManager::getRegistrations(blink::ScriptState* scriptState) | 77 ScriptPromise SyncManager::getRegistrations(blink::ScriptState* scriptState) |
| 94 { | 78 { |
| 95 if (!m_registration->active()) | 79 if (!m_registration->active()) |
| 96 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); | 80 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
| 97 | 81 |
| 98 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 82 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 99 ScriptPromise promise = resolver->promise(); | 83 ScriptPromise promise = resolver->promise(); |
| 100 | 84 |
| 101 backgroundSyncProvider()->getRegistrations(m_registration->webRegistration()
, new SyncGetRegistrationsCallbacks(resolver, m_registration)); | 85 backgroundSyncProvider()->getRegistrations(blink::WebSyncRegistration::Perio
dicityOneShot, m_registration->webRegistration(), new SyncGetRegistrationsCallba
cks(resolver, m_registration)); |
| 102 | 86 |
| 103 return promise; | 87 return promise; |
| 104 } | 88 } |
| 105 | 89 |
| 90 ScriptPromise SyncManager::permissionState(blink::ScriptState* scriptState) |
| 91 { |
| 92 if (!m_registration->active()) |
| 93 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
| 94 |
| 95 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 96 ScriptPromise promise = resolver->promise(); |
| 97 resolver->resolve(String::fromUTF8("granted")); |
| 98 return promise; |
| 99 } |
| 100 |
| 106 DEFINE_TRACE(SyncManager) | 101 DEFINE_TRACE(SyncManager) |
| 107 { | 102 { |
| 108 visitor->trace(m_registration); | 103 visitor->trace(m_registration); |
| 109 } | 104 } |
| 110 | 105 |
| 111 } // namespace blink | 106 } // namespace blink |
| OLD | NEW |