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

Side by Side Diff: third_party/WebKit/Source/modules/background_fetch/BackgroundFetchBridge.cpp

Issue 2745243002: Implement a Mojo service for Background Fetch (Closed)
Patch Set: rebase + comments v2 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "modules/background_fetch/BackgroundFetchBridge.h"
6
7 #include <utility>
8 #include "modules/background_fetch/BackgroundFetchOptions.h"
9 #include "modules/background_fetch/BackgroundFetchRegistration.h"
10 #include "modules/background_fetch/IconDefinition.h"
11 #include "public/platform/InterfaceProvider.h"
12 #include "public/platform/Platform.h"
13 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
14
15 namespace blink {
16
17 namespace {
18
19 // Creates a new BackgroundFetchRegistration instance given a Service Worker
20 // Registration and a Mojo BackgroundFetchRegistrationPtr instance.
21 BackgroundFetchRegistration* CreateBackgroundFetchRegistration(
22 ServiceWorkerRegistration* serviceWorkerRegistration,
23 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) {
24 HeapVector<IconDefinition> icons;
25
26 for (const auto& iconPtr : registrationPtr->icons) {
27 IconDefinition icon;
28 icon.setSrc(iconPtr->src);
29 icon.setSizes(iconPtr->sizes);
30 icon.setType(iconPtr->type);
31
32 icons.push_back(icon);
33 }
34
35 return new BackgroundFetchRegistration(
36 serviceWorkerRegistration, registrationPtr->tag, std::move(icons),
37 registrationPtr->total_download_size, registrationPtr->title);
38 }
39
40 } // namespace
41
42 // static
43 BackgroundFetchBridge* BackgroundFetchBridge::from(
44 ServiceWorkerRegistration* serviceWorkerRegistration) {
45 DCHECK(serviceWorkerRegistration);
46
47 BackgroundFetchBridge* bridge = static_cast<BackgroundFetchBridge*>(
48 Supplement<ServiceWorkerRegistration>::from(serviceWorkerRegistration,
49 supplementName()));
50
51 if (!bridge) {
52 bridge = new BackgroundFetchBridge(*serviceWorkerRegistration);
53 Supplement<ServiceWorkerRegistration>::provideTo(*serviceWorkerRegistration,
54 supplementName(), bridge);
55 }
56
57 return bridge;
58 }
59
60 // static
61 const char* BackgroundFetchBridge::supplementName() {
62 return "BackgroundFetchBridge";
63 }
64
65 BackgroundFetchBridge::BackgroundFetchBridge(
66 ServiceWorkerRegistration& registration)
67 : Supplement<ServiceWorkerRegistration>(registration) {}
68
69 BackgroundFetchBridge::~BackgroundFetchBridge() = default;
70
71 void BackgroundFetchBridge::abort(const String& tag) {
72 getService()->Abort(supplementable()->webRegistration()->registrationId(),
73 tag);
74 }
75
76 void BackgroundFetchBridge::updateUI(
77 const String& tag,
78 const String& title,
79 std::unique_ptr<UpdateUICallback> callback) {
80 getService()->UpdateUI(supplementable()->webRegistration()->registrationId(),
81 tag, title,
82 convertToBaseCallback(std::move(callback)));
83 }
84
85 void BackgroundFetchBridge::getRegistration(
86 const String& tag,
87 std::unique_ptr<GetRegistrationCallback> callback) {
88 getService()->GetRegistration(
89 supplementable()->webRegistration()->registrationId(), tag,
90 convertToBaseCallback(
91 WTF::bind(&BackgroundFetchBridge::didGetRegistration,
92 wrapPersistent(this), WTF::passed(std::move(callback)))));
93 }
94
95 void BackgroundFetchBridge::didGetRegistration(
96 std::unique_ptr<GetRegistrationCallback> callback,
97 mojom::blink::BackgroundFetchError error,
98 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) {
99 BackgroundFetchRegistration* registration = nullptr;
100
101 if (registrationPtr) {
102 DCHECK_EQ(error, mojom::blink::BackgroundFetchError::NONE);
103 registration = CreateBackgroundFetchRegistration(
104 supplementable(), std::move(registrationPtr));
105 }
106
107 (*callback)(error, registration);
108 }
109
110 void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) {
111 getService()->GetTags(supplementable()->webRegistration()->registrationId(),
112 convertToBaseCallback(std::move(callback)));
113 }
114
115 mojom::blink::BackgroundFetchServicePtr& BackgroundFetchBridge::getService() {
116 if (!m_backgroundFetchService) {
117 Platform::current()->interfaceProvider()->getInterface(
118 mojo::MakeRequest(&m_backgroundFetchService));
119 }
120 return m_backgroundFetchService;
121 }
122
123 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698