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

Unified Diff: third_party/WebKit/Source/modules/background_sync/BackgroundSyncProvider.cpp

Issue 2473483012: Move content/child/background_sync to Blink. (Closed)
Patch Set: Fix nits Created 4 years, 1 month 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: third_party/WebKit/Source/modules/background_sync/BackgroundSyncProvider.cpp
diff --git a/third_party/WebKit/Source/modules/background_sync/BackgroundSyncProvider.cpp b/third_party/WebKit/Source/modules/background_sync/BackgroundSyncProvider.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..63229a7daf1e0c2fbd853d67b191ab69eff83068
--- /dev/null
+++ b/third_party/WebKit/Source/modules/background_sync/BackgroundSyncProvider.cpp
@@ -0,0 +1,124 @@
+// Copyright 2016 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 "modules/background_sync/BackgroundSyncProvider.h"
+
+#include "public/platform/InterfaceProvider.h"
+#include "public/platform/Platform.h"
+#include "public/platform/modules/background_sync/WebSyncError.h"
+#include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
+#include "wtf/Functional.h"
+
+namespace blink {
+
+void BackgroundSyncProvider::registerBackgroundSync(
+ mojom::blink::SyncRegistrationPtr options,
+ WebServiceWorkerRegistration* serviceWorkerRegistration,
+ std::unique_ptr<SyncRegistrationCallbacks> callbacks) {
+ DCHECK(options);
+ DCHECK(serviceWorkerRegistration);
+ DCHECK(callbacks);
+ int64_t serviceWorkerRegistrationId =
+ serviceWorkerRegistration->registrationId();
+
+ GetBackgroundSyncServicePtr()->Register(
+ std::move(options), serviceWorkerRegistrationId,
+ convertToBaseCallback(WTF::bind(&BackgroundSyncProvider::registerCallback,
+ WTF::passed(std::move(callbacks)))));
+}
+
+void BackgroundSyncProvider::getRegistrations(
+ WebServiceWorkerRegistration* serviceWorkerRegistration,
+ std::unique_ptr<SyncGetRegistrationsCallbacks> callbacks) {
+ DCHECK(serviceWorkerRegistration);
+ DCHECK(callbacks);
+ int64_t serviceWorkerRegistrationId =
+ serviceWorkerRegistration->registrationId();
+
+ GetBackgroundSyncServicePtr()->GetRegistrations(
+ serviceWorkerRegistrationId,
+ convertToBaseCallback(
+ WTF::bind(&BackgroundSyncProvider::getRegistrationsCallback,
+ WTF::passed(std::move(callbacks)))));
+}
+
+// static
+void BackgroundSyncProvider::registerCallback(
+ std::unique_ptr<SyncRegistrationCallbacks> callbacks,
+ mojom::blink::BackgroundSyncError error,
+ mojom::blink::SyncRegistrationPtr options) {
+ // TODO(iclelland): Determine the correct error message to return in each case
+ switch (error) {
+ case mojom::blink::BackgroundSyncError::NONE:
+ if (!options.is_null())
+ callbacks->onSuccess(std::move(options));
+ break;
+ case mojom::blink::BackgroundSyncError::NOT_FOUND:
+ NOTREACHED();
+ break;
+ case mojom::blink::BackgroundSyncError::STORAGE:
+ callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown,
+ "Background Sync is disabled."));
+ break;
+ case mojom::blink::BackgroundSyncError::NOT_ALLOWED:
+ callbacks->onError(
+ blink::WebSyncError(WebSyncError::ErrorTypeNoPermission,
+ "Attempted to register a sync event without a "
+ "window or registration tag too long."));
+ break;
+ case mojom::blink::BackgroundSyncError::PERMISSION_DENIED:
+ callbacks->onError(blink::WebSyncError(
+ WebSyncError::ErrorTypePermissionDenied, "Permission denied."));
+ break;
+ case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER:
+ callbacks->onError(blink::WebSyncError(WebSyncError::ErrorTypeUnknown,
+ "No service worker is active."));
+ break;
+ }
+}
+
+// static
+void BackgroundSyncProvider::getRegistrationsCallback(
+ std::unique_ptr<SyncGetRegistrationsCallbacks> syncGetRegistrationCallbacks,
+ mojom::blink::BackgroundSyncError error,
+ mojo::WTFArray<mojom::blink::SyncRegistrationPtr> registrations) {
+ // TODO(iclelland): Determine the correct error message to return in each case
+ switch (error) {
+ case mojom::blink::BackgroundSyncError::NONE: {
+ WebVector<mojom::blink::SyncRegistration*> results(registrations.size());
+ for (size_t i = 0; i < registrations.size(); ++i) {
+ results[i] = registrations[i].get();
+ }
+ syncGetRegistrationCallbacks->onSuccess(results);
+ break;
+ }
+ case mojom::blink::BackgroundSyncError::NOT_FOUND:
+ case mojom::blink::BackgroundSyncError::NOT_ALLOWED:
+ case mojom::blink::BackgroundSyncError::PERMISSION_DENIED:
+ // These errors should never be returned from
+ // BackgroundSyncManager::GetRegistrations
+ NOTREACHED();
+ break;
+ case mojom::blink::BackgroundSyncError::STORAGE:
+ syncGetRegistrationCallbacks->onError(blink::WebSyncError(
+ WebSyncError::ErrorTypeUnknown, "Background Sync is disabled."));
+ break;
+ case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER:
+ syncGetRegistrationCallbacks->onError(blink::WebSyncError(
+ WebSyncError::ErrorTypeUnknown, "No service worker is active."));
+ break;
+ }
+}
+
+mojom::blink::BackgroundSyncServicePtr&
+BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
+ if (!m_backgroundSyncService.get()) {
+ mojo::InterfaceRequest<mojom::blink::BackgroundSyncService> request =
+ mojo::GetProxy(&m_backgroundSyncService);
+ Platform::current()->interfaceProvider()->getInterface(std::move(request));
+ }
+ return m_backgroundSyncService;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698