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

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: 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 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 "modules/background_fetch/BackgroundFetchOptions.h"
8 #include "modules/background_fetch/BackgroundFetchRegistration.h"
9 #include "modules/background_fetch/IconDefinition.h"
10 #include "public/platform/InterfaceProvider.h"
11 #include "public/platform/Platform.h"
12 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
13
14 namespace blink {
15
16 namespace {
17
18 // Creates a new BackgroundFetchRegistration instance given a Service Worker
19 // Registration and a Mojo BackgroundFetchRegistrationPtr instance.
20 BackgroundFetchRegistration* CreateBackgroundFetchRegistration(
21 ServiceWorkerRegistration* serviceWorkerRegistration,
22 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) {
23 HeapVector<IconDefinition> icons;
24
25 for (const auto& iconPtr : registrationPtr->icons) {
26 IconDefinition icon;
27 icon.setSrc(iconPtr->src);
28 icon.setSizes(iconPtr->sizes);
29 icon.setType(iconPtr->type);
haraken 2017/03/14 09:13:13 Can we probably use a mojo's type-mapping to avoid
Peter Beverloo 2017/03/14 17:28:15 Since IconDefinition is a dictionary and not garba
30
31 icons.push_back(icon);
32 }
33
34 return new BackgroundFetchRegistration(
35 serviceWorkerRegistration, registrationPtr->tag, std::move(icons),
36 registrationPtr->total_download_size, registrationPtr->title);
37 }
38
39 } // namespace
40
41 // static
42 BackgroundFetchBridge* BackgroundFetchBridge::from(
43 ServiceWorkerRegistration* serviceWorkerRegistration) {
44 DCHECK(serviceWorkerRegistration);
45
46 BackgroundFetchBridge* bridge = static_cast<BackgroundFetchBridge*>(
47 Supplement<ServiceWorkerRegistration>::from(serviceWorkerRegistration,
48 supplementName()));
49
50 if (!bridge) {
51 bridge = new BackgroundFetchBridge(*serviceWorkerRegistration);
52 Supplement<ServiceWorkerRegistration>::provideTo(*serviceWorkerRegistration,
53 supplementName(), bridge);
54 }
55
56 return bridge;
57 }
58
59 // static
60 const char* BackgroundFetchBridge::supplementName() {
61 return "BackgroundFetchBridge";
62 }
63
64 BackgroundFetchBridge::BackgroundFetchBridge(
65 ServiceWorkerRegistration& registration)
66 : Supplement<ServiceWorkerRegistration>(registration) {}
67
68 BackgroundFetchBridge::~BackgroundFetchBridge() = default;
69
70 void BackgroundFetchBridge::abort(const String& tag) {
71 GetService()->Abort(supplementable()->webRegistration()->registrationId(),
72 tag);
73 }
74
75 void BackgroundFetchBridge::updateUI(
76 const String& tag,
77 const String& title,
78 std::unique_ptr<UpdateUICallback> callback) {
79 GetService()->UpdateUI(supplementable()->webRegistration()->registrationId(),
80 tag, title,
81 convertToBaseCallback(std::move(callback)));
82 }
83
84 void BackgroundFetchBridge::getRegistration(
85 const String& tag,
86 std::unique_ptr<GetRegistrationCallback> callback) {
87 GetService()->GetRegistration(
88 supplementable()->webRegistration()->registrationId(), tag,
89 convertToBaseCallback(
90 WTF::bind(&BackgroundFetchBridge::didGetRegistration,
91 wrapPersistent(this), WTF::passed(std::move(callback)))));
92 }
93
94 void BackgroundFetchBridge::didGetRegistration(
95 std::unique_ptr<GetRegistrationCallback> callback,
96 mojom::blink::BackgroundFetchError error,
97 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) {
98 switch (error) {
99 case mojom::blink::BackgroundFetchError::NONE:
100 DCHECK(registrationPtr);
101
102 // Invoke the |callback| after creating the resulting Background Fetch
103 // Registration object representing the fetched registration.
104 (*callback)(error, CreateBackgroundFetchRegistration(
105 supplementable(), std::move(registrationPtr)));
106 return;
107 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG:
108 DCHECK(!registrationPtr);
109
110 // Invoke the |callback| without a Background Fetch registration.
111 (*callback)(error, nullptr /* registration */);
112 return;
113 }
114
115 NOTREACHED();
116 }
117
118 void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) {
119 GetService()->GetTags(supplementable()->webRegistration()->registrationId(),
120 convertToBaseCallback(std::move(callback)));
121 }
122
123 const mojom::blink::BackgroundFetchServicePtr&
124 BackgroundFetchBridge::GetService() {
haraken 2017/03/14 09:13:14 Nit: getService.
Peter Beverloo 2017/03/14 15:17:21 Done.
125 if (!m_backgroundFetchService.get()) {
126 Platform::current()->interfaceProvider()->getInterface(
127 mojo::MakeRequest(&m_backgroundFetchService));
128 }
129 return m_backgroundFetchService;
130 }
131
132 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698