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

Unified Diff: third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp

Issue 2745243002: Implement a Mojo service for Background Fetch (Closed)
Patch Set: Implement a Mojo service for Background Fetch Created 3 years, 9 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: third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp
diff --git a/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp b/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..56f40f82a09c7a1b715dcf635c9a8eee17c03af4
--- /dev/null
+++ b/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp
@@ -0,0 +1,125 @@
+// Copyright 2017 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_fetch/BackgroundFetchBridge.h"
+
+#include "modules/background_fetch/BackgroundFetchOptions.h"
+#include "modules/background_fetch/BackgroundFetchRegistration.h"
+#include "modules/background_fetch/IconDefinition.h"
+#include "public/platform/InterfaceProvider.h"
+#include "public/platform/Platform.h"
+#include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
+
+namespace blink {
+
+namespace {
+
+// Creates a new BackgroundFetchRegistration instance given a Service Worker
+// Registration and a Mojo BackgroundFetchRegistrationPtr instance.
+BackgroundFetchRegistration* CreateBackgroundFetchRegistration(
+ ServiceWorkerRegistration* serviceWorkerRegistration,
+ mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) {
+ HeapVector<IconDefinition> icons;
+
+ for (const auto& iconPtr : registrationPtr->icons) {
dcheng 2017/03/16 07:14:08 Is this something that would make sense to typemap
Peter Beverloo 2017/03/16 15:32:55 There's two questions here - (1) Mojo structs t
+ IconDefinition icon;
+ icon.setSrc(iconPtr->src);
+ icon.setSizes(iconPtr->sizes);
+ icon.setType(iconPtr->type);
+
+ icons.push_back(icon);
+ }
+
+ return new BackgroundFetchRegistration(
+ serviceWorkerRegistration, registrationPtr->tag, std::move(icons),
dcheng 2017/03/16 07:14:08 Nit: #include <utility> for std::move
Peter Beverloo 2017/03/16 15:32:56 Done.
+ registrationPtr->total_download_size, registrationPtr->title);
+}
+
+} // namespace
+
+// static
+BackgroundFetchBridge* BackgroundFetchBridge::from(
+ ServiceWorkerRegistration* serviceWorkerRegistration) {
+ DCHECK(serviceWorkerRegistration);
+
+ BackgroundFetchBridge* bridge = static_cast<BackgroundFetchBridge*>(
+ Supplement<ServiceWorkerRegistration>::from(serviceWorkerRegistration,
+ supplementName()));
+
+ if (!bridge) {
+ bridge = new BackgroundFetchBridge(*serviceWorkerRegistration);
+ Supplement<ServiceWorkerRegistration>::provideTo(*serviceWorkerRegistration,
+ supplementName(), bridge);
+ }
+
+ return bridge;
+}
+
+// static
+const char* BackgroundFetchBridge::supplementName() {
+ return "BackgroundFetchBridge";
+}
+
+BackgroundFetchBridge::BackgroundFetchBridge(
+ ServiceWorkerRegistration& registration)
+ : Supplement<ServiceWorkerRegistration>(registration) {}
+
+BackgroundFetchBridge::~BackgroundFetchBridge() = default;
+
+void BackgroundFetchBridge::abort(const String& tag) {
+ getService()->Abort(supplementable()->webRegistration()->registrationId(),
+ tag);
+}
+
+void BackgroundFetchBridge::updateUI(
+ const String& tag,
+ const String& title,
+ std::unique_ptr<UpdateUICallback> callback) {
+ getService()->UpdateUI(supplementable()->webRegistration()->registrationId(),
+ tag, title,
+ convertToBaseCallback(std::move(callback)));
+}
+
+void BackgroundFetchBridge::getRegistration(
+ const String& tag,
+ std::unique_ptr<GetRegistrationCallback> callback) {
+ getService()->GetRegistration(
+ supplementable()->webRegistration()->registrationId(), tag,
+ convertToBaseCallback(
+ WTF::bind(&BackgroundFetchBridge::didGetRegistration,
+ wrapPersistent(this), WTF::passed(std::move(callback)))));
+}
+
+void BackgroundFetchBridge::didGetRegistration(
+ std::unique_ptr<GetRegistrationCallback> callback,
+ mojom::blink::BackgroundFetchError error,
+ mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) {
+ BackgroundFetchRegistration* registration = nullptr;
+
+ if (registrationPtr) {
+ DCHECK_EQ(error, mojom::blink::BackgroundFetchError::NONE);
+ registration = CreateBackgroundFetchRegistration(
+ supplementable(), std::move(registrationPtr));
+ }
+
+ // Invoke the |callback| with the |error| and the constructed Background
+ // Fetch registration, which can't be a typemap as it's garbage collected.
dcheng 2017/03/16 07:14:08 Can you help me understand why Oilpan heap objects
Peter Beverloo 2017/03/16 15:32:55 This actually is a left-over comment -- the first
+ (*callback)(error, registration);
+}
+
+void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) {
+ getService()->GetTags(supplementable()->webRegistration()->registrationId(),
+ convertToBaseCallback(std::move(callback)));
+}
+
+const mojom::blink::BackgroundFetchServicePtr&
dcheng 2017/03/16 07:14:08 It seems unusual to mark this const: it kind of ma
Peter Beverloo 2017/03/16 15:32:55 Done.
+BackgroundFetchBridge::getService() {
+ if (!m_backgroundFetchService.get()) {
dcheng 2017/03/16 07:14:08 Nit: no .get()
Peter Beverloo 2017/03/16 15:32:55 Done.
+ Platform::current()->interfaceProvider()->getInterface(
+ mojo::MakeRequest(&m_backgroundFetchService));
+ }
+ return m_backgroundFetchService;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698