OLD | NEW |
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" |
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 std::unique_ptr<AbortCallback> callback) { | 60 std::unique_ptr<AbortCallback> callback) { |
73 getService()->Abort(supplementable()->webRegistration()->registrationId(), | 61 getService()->Abort(supplementable()->webRegistration()->registrationId(), |
74 tag, convertToBaseCallback(std::move(callback))); | 62 tag, convertToBaseCallback(std::move(callback))); |
75 } | 63 } |
76 | 64 |
77 void BackgroundFetchBridge::updateUI( | 65 void BackgroundFetchBridge::updateUI( |
78 const String& tag, | 66 const String& tag, |
79 const String& title, | 67 const String& title, |
80 std::unique_ptr<UpdateUICallback> callback) { | 68 std::unique_ptr<UpdateUICallback> callback) { |
81 getService()->UpdateUI(supplementable()->webRegistration()->registrationId(), | 69 getService()->UpdateUI(supplementable()->webRegistration()->registrationId(), |
82 tag, title, | 70 tag, title, |
83 convertToBaseCallback(std::move(callback))); | 71 convertToBaseCallback(std::move(callback))); |
84 } | 72 } |
85 | 73 |
86 void BackgroundFetchBridge::getRegistration( | 74 void BackgroundFetchBridge::getRegistration( |
87 const String& tag, | 75 const String& tag, |
88 std::unique_ptr<GetRegistrationCallback> callback) { | 76 std::unique_ptr<RegistrationCallback> callback) { |
89 getService()->GetRegistration( | 77 getService()->GetRegistration( |
90 supplementable()->webRegistration()->registrationId(), tag, | 78 supplementable()->webRegistration()->registrationId(), tag, |
91 convertToBaseCallback( | 79 convertToBaseCallback( |
92 WTF::bind(&BackgroundFetchBridge::didGetRegistration, | 80 WTF::bind(&BackgroundFetchBridge::didGetRegistration, |
93 wrapPersistent(this), WTF::passed(std::move(callback))))); | 81 wrapPersistent(this), WTF::passed(std::move(callback))))); |
94 } | 82 } |
95 | 83 |
96 void BackgroundFetchBridge::didGetRegistration( | 84 void BackgroundFetchBridge::didGetRegistration( |
97 std::unique_ptr<GetRegistrationCallback> callback, | 85 std::unique_ptr<RegistrationCallback> callback, |
98 mojom::blink::BackgroundFetchError error, | 86 mojom::blink::BackgroundFetchError error, |
99 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) { | 87 mojom::blink::BackgroundFetchRegistrationPtr registrationPtr) { |
100 BackgroundFetchRegistration* registration = nullptr; | 88 BackgroundFetchRegistration* registration = |
| 89 registrationPtr.To<BackgroundFetchRegistration*>(); |
101 | 90 |
102 if (registrationPtr) { | 91 if (registration) { |
103 DCHECK_EQ(error, mojom::blink::BackgroundFetchError::NONE); | 92 DCHECK_EQ(error, mojom::blink::BackgroundFetchError::NONE); |
104 registration = CreateBackgroundFetchRegistration( | 93 registration->setServiceWorkerRegistration(supplementable()); |
105 supplementable(), std::move(registrationPtr)); | |
106 } | 94 } |
107 | 95 |
108 (*callback)(error, registration); | 96 (*callback)(error, registration); |
109 } | 97 } |
110 | 98 |
111 void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) { | 99 void BackgroundFetchBridge::getTags(std::unique_ptr<GetTagsCallback> callback) { |
112 getService()->GetTags(supplementable()->webRegistration()->registrationId(), | 100 getService()->GetTags(supplementable()->webRegistration()->registrationId(), |
113 convertToBaseCallback(std::move(callback))); | 101 convertToBaseCallback(std::move(callback))); |
114 } | 102 } |
115 | 103 |
116 mojom::blink::BackgroundFetchServicePtr& BackgroundFetchBridge::getService() { | 104 mojom::blink::BackgroundFetchServicePtr& BackgroundFetchBridge::getService() { |
117 if (!m_backgroundFetchService) { | 105 if (!m_backgroundFetchService) { |
118 Platform::current()->interfaceProvider()->getInterface( | 106 Platform::current()->interfaceProvider()->getInterface( |
119 mojo::MakeRequest(&m_backgroundFetchService)); | 107 mojo::MakeRequest(&m_backgroundFetchService)); |
120 } | 108 } |
121 return m_backgroundFetchService; | 109 return m_backgroundFetchService; |
122 } | 110 } |
123 | 111 |
124 } // namespace blink | 112 } // namespace blink |
OLD | NEW |