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

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

Issue 2762573003: Implement BackgroundFetchManager.fetch() and struct traits (Closed)
Patch Set: Add a missing file 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/background_fetch/BackgroundFetchBridge.h" 5 #include "modules/background_fetch/BackgroundFetchBridge.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include "modules/background_fetch/BackgroundFetchOptions.h" 8 #include "modules/background_fetch/BackgroundFetchOptions.h"
9 #include "modules/background_fetch/BackgroundFetchRegistration.h" 9 #include "modules/background_fetch/BackgroundFetchRegistration.h"
10 #include "modules/background_fetch/BackgroundFetchTypeConverters.h"
dcheng 2017/03/21 07:03:20 Let's use typemapping on the Blink side. It'll sav
Peter Beverloo 2017/03/21 14:05:19 These types live in //Source/modules/, whereas the
10 #include "modules/background_fetch/IconDefinition.h" 11 #include "modules/background_fetch/IconDefinition.h"
11 #include "public/platform/InterfaceProvider.h" 12 #include "public/platform/InterfaceProvider.h"
12 #include "public/platform/Platform.h" 13 #include "public/platform/Platform.h"
13 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" 14 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
14 15
15 namespace blink { 16 namespace blink {
16 17
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 18 // static
43 BackgroundFetchBridge* BackgroundFetchBridge::from( 19 BackgroundFetchBridge* BackgroundFetchBridge::from(
44 ServiceWorkerRegistration* serviceWorkerRegistration) { 20 ServiceWorkerRegistration* serviceWorkerRegistration) {
45 DCHECK(serviceWorkerRegistration); 21 DCHECK(serviceWorkerRegistration);
46 22
47 BackgroundFetchBridge* bridge = static_cast<BackgroundFetchBridge*>( 23 BackgroundFetchBridge* bridge = static_cast<BackgroundFetchBridge*>(
48 Supplement<ServiceWorkerRegistration>::from(serviceWorkerRegistration, 24 Supplement<ServiceWorkerRegistration>::from(serviceWorkerRegistration,
49 supplementName())); 25 supplementName()));
50 26
51 if (!bridge) { 27 if (!bridge) {
52 bridge = new BackgroundFetchBridge(*serviceWorkerRegistration); 28 bridge = new BackgroundFetchBridge(*serviceWorkerRegistration);
53 Supplement<ServiceWorkerRegistration>::provideTo(*serviceWorkerRegistration, 29 Supplement<ServiceWorkerRegistration>::provideTo(*serviceWorkerRegistration,
54 supplementName(), bridge); 30 supplementName(), bridge);
55 } 31 }
56 32
57 return bridge; 33 return bridge;
58 } 34 }
59 35
60 // static 36 // static
61 const char* BackgroundFetchBridge::supplementName() { 37 const char* BackgroundFetchBridge::supplementName() {
62 return "BackgroundFetchBridge"; 38 return "BackgroundFetchBridge";
63 } 39 }
64 40
65 BackgroundFetchBridge::BackgroundFetchBridge( 41 BackgroundFetchBridge::BackgroundFetchBridge(
66 ServiceWorkerRegistration& registration) 42 ServiceWorkerRegistration& registration)
67 : Supplement<ServiceWorkerRegistration>(registration) {} 43 : Supplement<ServiceWorkerRegistration>(registration) {}
68 44
69 BackgroundFetchBridge::~BackgroundFetchBridge() = default; 45 BackgroundFetchBridge::~BackgroundFetchBridge() = default;
70 46
47 void BackgroundFetchBridge::fetch(
48 const String& tag,
49 const BackgroundFetchOptions& options,
50 std::unique_ptr<RegistrationCallback> callback) {
51 getService()->Fetch(
52 supplementable()->webRegistration()->registrationId(), tag,
53 mojom::blink::BackgroundFetchOptions::From(options),
54 convertToBaseCallback(
55 WTF::bind(&BackgroundFetchBridge::didGetRegistration,
56 wrapPersistent(this), WTF::passed(std::move(callback)))));
57 }
58
71 void BackgroundFetchBridge::abort(const String& tag) { 59 void BackgroundFetchBridge::abort(const String& tag) {
72 getService()->Abort(supplementable()->webRegistration()->registrationId(), 60 getService()->Abort(supplementable()->webRegistration()->registrationId(),
73 tag); 61 tag);
74 } 62 }
75 63
76 void BackgroundFetchBridge::updateUI( 64 void BackgroundFetchBridge::updateUI(
77 const String& tag, 65 const String& tag,
78 const String& title, 66 const String& title,
79 std::unique_ptr<UpdateUICallback> callback) { 67 std::unique_ptr<UpdateUICallback> callback) {
80 getService()->UpdateUI(supplementable()->webRegistration()->registrationId(), 68 getService()->UpdateUI(supplementable()->webRegistration()->registrationId(),
81 tag, title, 69 tag, title,
82 convertToBaseCallback(std::move(callback))); 70 convertToBaseCallback(std::move(callback)));
83 } 71 }
84 72
85 void BackgroundFetchBridge::getRegistration( 73 void BackgroundFetchBridge::getRegistration(
86 const String& tag, 74 const String& tag,
87 std::unique_ptr<GetRegistrationCallback> callback) { 75 std::unique_ptr<RegistrationCallback> callback) {
88 getService()->GetRegistration( 76 getService()->GetRegistration(
89 supplementable()->webRegistration()->registrationId(), tag, 77 supplementable()->webRegistration()->registrationId(), tag,
90 convertToBaseCallback( 78 convertToBaseCallback(
91 WTF::bind(&BackgroundFetchBridge::didGetRegistration, 79 WTF::bind(&BackgroundFetchBridge::didGetRegistration,
92 wrapPersistent(this), WTF::passed(std::move(callback))))); 80 wrapPersistent(this), WTF::passed(std::move(callback)))));
93 } 81 }
94 82
95 void BackgroundFetchBridge::didGetRegistration( 83 void BackgroundFetchBridge::didGetRegistration(
harkness 2017/03/21 11:30:00 optional: I'm glad that we're reusing the code for
Peter Beverloo 2017/03/21 13:46:27 Can you suggest a name that you think would be cle
96 std::unique_ptr<GetRegistrationCallback> callback, 84 std::unique_ptr<RegistrationCallback> callback,
97 mojom::blink::BackgroundFetchError error, 85 mojom::blink::BackgroundFetchError error,
98 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) { 86 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) {
99 BackgroundFetchRegistration* registration = nullptr; 87 BackgroundFetchRegistration* registration =
88 registrationPtr.To<BackgroundFetchRegistration*>();
100 89
101 if (registrationPtr) { 90 if (registration) {
102 DCHECK_EQ(error, mojom::blink::BackgroundFetchError::NONE); 91 DCHECK_EQ(error, mojom::blink::BackgroundFetchError::NONE);
103 registration = CreateBackgroundFetchRegistration( 92 registration->setServiceWorkerRegistration(supplementable());
104 supplementable(), std::move(registrationPtr));
105 } 93 }
106 94
107 (*callback)(error, registration); 95 (*callback)(error, registration);
108 } 96 }
109 97
110 void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) { 98 void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) {
111 getService()->GetTags(supplementable()->webRegistration()->registrationId(), 99 getService()->GetTags(supplementable()->webRegistration()->registrationId(),
112 convertToBaseCallback(std::move(callback))); 100 convertToBaseCallback(std::move(callback)));
113 } 101 }
114 102
115 mojom::blink::BackgroundFetchServicePtr& BackgroundFetchBridge::getService() { 103 mojom::blink::BackgroundFetchServicePtr& BackgroundFetchBridge::getService() {
116 if (!m_backgroundFetchService) { 104 if (!m_backgroundFetchService) {
117 Platform::current()->interfaceProvider()->getInterface( 105 Platform::current()->interfaceProvider()->getInterface(
118 mojo::MakeRequest(&m_backgroundFetchService)); 106 mojo::MakeRequest(&m_backgroundFetchService));
119 } 107 }
120 return m_backgroundFetchService; 108 return m_backgroundFetchService;
121 } 109 }
122 110
123 } // namespace blink 111 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698