OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/background_sync/PeriodicSyncManager.h" |
| 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/ScriptState.h" |
| 12 #include "core/dom/DOMException.h" |
| 13 #include "core/dom/Document.h" |
| 14 #include "core/dom/ExceptionCode.h" |
| 15 #include "core/dom/ExecutionContext.h" |
| 16 #include "modules/background_sync/PeriodicSyncRegistrationOptions.h" |
| 17 #include "modules/background_sync/SyncCallbacks.h" |
| 18 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 19 #include "public/platform/Platform.h" |
| 20 #include "public/platform/modules/background_sync/WebSyncProvider.h" |
| 21 #include "public/platform/modules/background_sync/WebSyncRegistration.h" |
| 22 #include "wtf/RefPtr.h" |
| 23 |
| 24 |
| 25 namespace blink { |
| 26 namespace { |
| 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 kMinPossiblePeriod = 12 * 60 * 60 * 1000; |
| 34 |
| 35 WebSyncProvider* backgroundSyncProvider() |
| 36 { |
| 37 WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncProvid
er(); |
| 38 ASSERT(webSyncProvider); |
| 39 return webSyncProvider; |
| 40 } |
| 41 |
| 42 } |
| 43 |
| 44 PeriodicSyncManager::PeriodicSyncManager(ServiceWorkerRegistration* registration
) |
| 45 : m_registration(registration) |
| 46 { |
| 47 ASSERT(registration); |
| 48 } |
| 49 |
| 50 unsigned long PeriodicSyncManager::minPossiblePeriod() |
| 51 { |
| 52 return kMinPossiblePeriod; |
| 53 } |
| 54 |
| 55 ScriptPromise PeriodicSyncManager::registerFunction(blink::ScriptState* scriptSt
ate, const PeriodicSyncRegistrationOptions& options) |
| 56 { |
| 57 if (!m_registration->active()) |
| 58 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Registration failed - no active Service Worker")); |
| 59 |
| 60 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 61 ScriptPromise promise = resolver->promise(); |
| 62 |
| 63 WebSyncRegistration::NetworkState networkState; |
| 64 String networkStateString = options.networkState(); |
| 65 if (networkStateString == "any") { |
| 66 networkState = WebSyncRegistration::NetworkState::NetworkStateAny; |
| 67 } else if (networkStateString == "avoid-cellular") { |
| 68 networkState = WebSyncRegistration::NetworkState::NetworkStateAvoidCellu
lar; |
| 69 } else { |
| 70 networkState = WebSyncRegistration::NetworkState::NetworkStateOnline; |
| 71 } |
| 72 WebSyncRegistration::PowerState powerState; |
| 73 String powerStateString = options.powerState(); |
| 74 if (powerStateString == "avoid-draining") { |
| 75 powerState = WebSyncRegistration::PowerState::PowerStateAvoidDraining; |
| 76 } else { |
| 77 powerState = WebSyncRegistration::PowerState::PowerStateAuto; |
| 78 } |
| 79 WebSyncRegistration* webSyncRegistration = new WebSyncRegistration( |
| 80 WebSyncRegistration::UNREGISTERED_SYNC_ID, |
| 81 WebSyncRegistration::PeriodicityPeriodic, |
| 82 options.tag(), |
| 83 options.minPeriod(), |
| 84 networkState, |
| 85 powerState |
| 86 ); |
| 87 backgroundSyncProvider()->registerBackgroundSync(webSyncRegistration, m_regi
stration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registrat
ion)); |
| 88 |
| 89 return promise; |
| 90 } |
| 91 |
| 92 ScriptPromise PeriodicSyncManager::getRegistration(blink::ScriptState* scriptSta
te, const String& syncRegistrationTag) |
| 93 { |
| 94 if (!m_registration->active()) |
| 95 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
| 96 |
| 97 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 98 ScriptPromise promise = resolver->promise(); |
| 99 |
| 100 backgroundSyncProvider()->getRegistration(blink::WebSyncRegistration::Period
icityPeriodic, syncRegistrationTag, m_registration->webRegistration(), new SyncR
egistrationCallbacks(resolver, m_registration)); |
| 101 |
| 102 return promise; |
| 103 } |
| 104 |
| 105 ScriptPromise PeriodicSyncManager::getRegistrations(blink::ScriptState* scriptSt
ate) |
| 106 { |
| 107 if (!m_registration->active()) |
| 108 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
| 109 |
| 110 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 111 ScriptPromise promise = resolver->promise(); |
| 112 |
| 113 backgroundSyncProvider()->getRegistrations(blink::WebSyncRegistration::Perio
dicityPeriodic, m_registration->webRegistration(), new SyncGetRegistrationsCallb
acks(resolver, m_registration)); |
| 114 |
| 115 return promise; |
| 116 } |
| 117 |
| 118 ScriptPromise PeriodicSyncManager::permissionState(blink::ScriptState* scriptSta
te) |
| 119 { |
| 120 if (!m_registration->active()) |
| 121 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
| 122 |
| 123 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 124 ScriptPromise promise = resolver->promise(); |
| 125 resolver->resolve(String::fromUTF8("granted")); |
| 126 return promise; |
| 127 } |
| 128 |
| 129 DEFINE_TRACE(PeriodicSyncManager) |
| 130 { |
| 131 visitor->trace(m_registration); |
| 132 } |
| 133 |
| 134 } // namespace blink |
OLD | NEW |