Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1530)

Unified Diff: Source/modules/background_sync/PeriodicSyncManager.cpp

Issue 1096503002: [Background Sync] Converting Blink code to the MVP API (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Unregister method requires the sync registration tag Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/modules/background_sync/PeriodicSyncManager.cpp
diff --git a/Source/modules/background_sync/PeriodicSyncManager.cpp b/Source/modules/background_sync/PeriodicSyncManager.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..aa531abbc0fddd8e592895e28a7d31bb3c54ed2e
--- /dev/null
+++ b/Source/modules/background_sync/PeriodicSyncManager.cpp
@@ -0,0 +1,134 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "modules/background_sync/PeriodicSyncManager.h"
+
+#include "bindings/core/v8/CallbackPromiseAdapter.h"
+#include "bindings/core/v8/ScriptPromise.h"
+#include "bindings/core/v8/ScriptPromiseResolver.h"
+#include "bindings/core/v8/ScriptState.h"
+#include "core/dom/DOMException.h"
+#include "core/dom/Document.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/dom/ExecutionContext.h"
+#include "modules/background_sync/PeriodicSyncRegistrationOptions.h"
+#include "modules/background_sync/SyncCallbacks.h"
+#include "modules/serviceworkers/ServiceWorkerRegistration.h"
+#include "public/platform/Platform.h"
+#include "public/platform/modules/background_sync/WebSyncProvider.h"
+#include "public/platform/modules/background_sync/WebSyncRegistration.h"
+#include "wtf/RefPtr.h"
+
+
+namespace blink {
+namespace {
+
+/* This is the minimum period which will be allowed by the Background
+ * Sync manager process. It is recorded here in order to be able to
+ * respond to syncManager.minAllowablePeriod.
+ * The time is expressed in milliseconds,
+ */
+unsigned long kMinPossiblePeriod = 12 * 60 * 60 * 1000;
+
+WebSyncProvider* backgroundSyncProvider()
+{
+ WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncProvider();
+ ASSERT(webSyncProvider);
+ return webSyncProvider;
+}
+
+}
+
+PeriodicSyncManager::PeriodicSyncManager(ServiceWorkerRegistration* registration)
+ : m_registration(registration)
+{
+ ASSERT(registration);
+}
+
+unsigned long PeriodicSyncManager::minPossiblePeriod()
+{
+ return kMinPossiblePeriod;
+}
+
+ScriptPromise PeriodicSyncManager::registerFunction(blink::ScriptState* scriptState, const PeriodicSyncRegistrationOptions& options)
+{
+ if (!m_registration->active())
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Registration failed - no active Service Worker"));
+
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+
+ WebSyncRegistration::NetworkState networkState;
+ String networkStateString = options.networkState();
+ if (networkStateString == "any") {
+ networkState = WebSyncRegistration::NetworkState::NetworkStateAny;
+ } else if (networkStateString == "avoid-cellular") {
+ networkState = WebSyncRegistration::NetworkState::NetworkStateAvoidCellular;
+ } else {
+ networkState = WebSyncRegistration::NetworkState::NetworkStateOnline;
+ }
+ WebSyncRegistration::PowerState powerState;
+ String powerStateString = options.powerState();
+ if (powerStateString == "avoid-draining") {
+ powerState = WebSyncRegistration::PowerState::PowerStateAvoidDraining;
+ } else {
+ powerState = WebSyncRegistration::PowerState::PowerStateAuto;
+ }
+ WebSyncRegistration* webSyncRegistration = new WebSyncRegistration(
+ WebSyncRegistration::UNREGISTERED_SYNC_ID,
+ WebSyncRegistration::PeriodicityPeriodic,
+ options.tag(),
+ options.minPeriod(),
+ networkState,
+ powerState
+ );
+ backgroundSyncProvider()->registerBackgroundSync(webSyncRegistration, m_registration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registration));
+
+ return promise;
+}
+
+ScriptPromise PeriodicSyncManager::getRegistration(blink::ScriptState* scriptState, const String& syncRegistrationTag)
+{
+ if (!m_registration->active())
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Operation failed - no active Service Worker"));
+
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+
+ backgroundSyncProvider()->getRegistration(blink::WebSyncRegistration::PeriodicityPeriodic, syncRegistrationTag, m_registration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registration));
+
+ return promise;
+}
+
+ScriptPromise PeriodicSyncManager::getRegistrations(blink::ScriptState* scriptState)
+{
+ if (!m_registration->active())
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Operation failed - no active Service Worker"));
+
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+
+ backgroundSyncProvider()->getRegistrations(blink::WebSyncRegistration::PeriodicityPeriodic, m_registration->webRegistration(), new SyncGetRegistrationsCallbacks(resolver, m_registration));
+
+ return promise;
+}
+
+ScriptPromise PeriodicSyncManager::permissionState(blink::ScriptState* scriptState)
+{
+ if (!m_registration->active())
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Operation failed - no active Service Worker"));
+
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+ resolver->resolve(String::fromUTF8("granted"));
+ return promise;
+}
+
+DEFINE_TRACE(PeriodicSyncManager)
+{
+ visitor->trace(m_registration);
+}
+
+} // namespace blink
« no previous file with comments | « Source/modules/background_sync/PeriodicSyncManager.h ('k') | Source/modules/background_sync/PeriodicSyncManager.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698