OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/payments/PaymentAppManager.h" | |
6 | |
7 #include "bindings/core/v8/ScriptPromise.h" | |
8 #include "bindings/core/v8/ScriptState.h" | |
9 #include "core/dom/DOMException.h" | |
10 #include "modules/payments/PaymentAppManifest.h" | |
11 #include "modules/payments/PaymentAppOption.h" | |
12 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | |
13 #include "platform/mojo/MojoHelper.h" | |
14 #include "public/platform/InterfaceProvider.h" | |
15 #include "public/platform/Platform.h" | |
16 | |
17 namespace mojo { | |
18 | |
19 using payments::mojom::blink::PaymentAppManifest; | |
20 using payments::mojom::blink::PaymentAppManifestPtr; | |
21 using payments::mojom::blink::PaymentAppOption; | |
22 using payments::mojom::blink::PaymentAppOptionPtr; | |
23 | |
24 template <> | |
25 struct TypeConverter<PaymentAppOptionPtr, blink::PaymentAppOption> { | |
26 static PaymentAppOptionPtr Convert(const blink::PaymentAppOption& input) { | |
27 PaymentAppOptionPtr output = PaymentAppOption::New(); | |
28 output->name = input.hasName() ? input.name() : WTF::emptyString; | |
29 output->icon = input.hasIcon() ? input.icon() : WTF::String(); | |
30 output->id = input.hasId() ? input.id() : WTF::emptyString; | |
31 if (input.hasEnabledMethods()) { | |
32 output->enabled_methods = | |
33 WTF::Vector<WTF::String>(input.enabledMethods()); | |
34 } | |
35 return output; | |
36 } | |
37 }; | |
38 | |
39 template <> | |
40 struct TypeConverter<PaymentAppManifestPtr, blink::PaymentAppManifest> { | |
41 static PaymentAppManifestPtr Convert(const blink::PaymentAppManifest& input) { | |
42 PaymentAppManifestPtr output = PaymentAppManifest::New(); | |
43 output->name = input.hasName() ? input.name() : WTF::emptyString; | |
44 output->icon = input.hasIcon() ? input.icon() : WTF::String(); | |
45 if (input.hasOptions()) { | |
46 for (size_t i = 0; i < input.options().size(); ++i) { | |
47 output->options.push_back(PaymentAppOption::From(input.options()[i])); | |
48 } | |
49 } | |
50 return output; | |
51 } | |
52 }; | |
53 | |
54 template <> | |
55 struct TypeConverter<blink::PaymentAppManifest, PaymentAppManifestPtr> { | |
56 static blink::PaymentAppManifest Convert(const PaymentAppManifestPtr& input) { | |
57 blink::PaymentAppManifest output; | |
58 output.setName(input->name); | |
59 output.setIcon(input->icon); | |
60 blink::HeapVector<blink::PaymentAppOption> options; | |
61 for (const auto& option : input->options) { | |
62 options.push_back(mojo::ConvertTo<blink::PaymentAppOption>(option)); | |
63 } | |
64 output.setOptions(options); | |
65 return output; | |
66 } | |
67 }; | |
68 | |
69 template <> | |
70 struct TypeConverter<blink::PaymentAppOption, PaymentAppOptionPtr> { | |
71 static blink::PaymentAppOption Convert(const PaymentAppOptionPtr& input) { | |
72 blink::PaymentAppOption output; | |
73 output.setName(input->name); | |
74 output.setIcon(input->icon); | |
75 output.setId(input->id); | |
76 Vector<WTF::String> enabledMethods; | |
77 for (const auto& method : input->enabled_methods) { | |
78 enabledMethods.push_back(method); | |
79 } | |
80 output.setEnabledMethods(enabledMethods); | |
81 return output; | |
82 } | |
83 }; | |
84 | |
85 } // namespace mojo | |
86 | |
87 namespace blink { | |
88 | |
89 PaymentAppManager* PaymentAppManager::create( | |
90 ServiceWorkerRegistration* registration) { | |
91 return new PaymentAppManager(registration); | |
92 } | |
93 | |
94 ScriptPromise PaymentAppManager::setManifest( | |
95 ScriptState* scriptState, | |
96 const PaymentAppManifest& manifest) { | |
97 if (!m_manager) { | |
98 return ScriptPromise::rejectWithDOMException( | |
99 scriptState, DOMException::create(InvalidStateError, | |
100 "Payment app manager unavailable.")); | |
101 } | |
102 | |
103 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | |
104 ScriptPromise promise = resolver->promise(); | |
105 | |
106 m_manager->SetManifest( | |
107 payments::mojom::blink::PaymentAppManifest::From(manifest), | |
108 convertToBaseCallback(WTF::bind(&PaymentAppManager::onSetManifest, | |
109 wrapPersistent(this), | |
110 wrapPersistent(resolver)))); | |
111 | |
112 return promise; | |
113 } | |
114 | |
115 ScriptPromise PaymentAppManager::getManifest(ScriptState* scriptState) { | |
116 if (!m_manager) { | |
117 return ScriptPromise::rejectWithDOMException( | |
118 scriptState, DOMException::create(InvalidStateError, | |
119 "Payment app manager unavailable.")); | |
120 } | |
121 | |
122 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | |
123 ScriptPromise promise = resolver->promise(); | |
124 | |
125 m_manager->GetManifest(convertToBaseCallback( | |
126 WTF::bind(&PaymentAppManager::onGetManifest, wrapPersistent(this), | |
127 wrapPersistent(resolver)))); | |
128 | |
129 return promise; | |
130 } | |
131 | |
132 DEFINE_TRACE(PaymentAppManager) { | |
133 visitor->trace(m_registration); | |
134 } | |
135 | |
136 PaymentAppManager::PaymentAppManager(ServiceWorkerRegistration* registration) | |
137 : m_registration(registration) { | |
138 DCHECK(registration); | |
139 Platform::current()->interfaceProvider()->getInterface( | |
140 mojo::MakeRequest(&m_manager)); | |
141 | |
142 m_manager.set_connection_error_handler(convertToBaseCallback(WTF::bind( | |
143 &PaymentAppManager::onServiceConnectionError, wrapWeakPersistent(this)))); | |
144 | |
145 m_manager->Init(m_registration->scope()); | |
146 } | |
147 | |
148 void PaymentAppManager::onSetManifest( | |
149 ScriptPromiseResolver* resolver, | |
150 payments::mojom::blink::PaymentAppManifestError error) { | |
151 DCHECK(resolver); | |
152 switch (error) { | |
153 case payments::mojom::blink::PaymentAppManifestError::NONE: | |
154 resolver->resolve(); | |
155 break; | |
156 case payments::mojom::blink::PaymentAppManifestError::NOT_IMPLEMENTED: | |
157 resolver->reject( | |
158 DOMException::create(NotSupportedError, "Not implemented yet.")); | |
159 break; | |
160 case payments::mojom::blink::PaymentAppManifestError::NO_ACTIVE_WORKER: | |
161 resolver->reject( | |
162 DOMException::create(InvalidStateError, "No active service worker.")); | |
163 break; | |
164 case payments::mojom::blink::PaymentAppManifestError:: | |
165 MANIFEST_STORAGE_OPERATION_FAILED: | |
166 resolver->reject(DOMException::create( | |
167 InvalidStateError, "Storing manifest data is failed.")); | |
168 break; | |
169 } | |
170 } | |
171 | |
172 void PaymentAppManager::onGetManifest( | |
173 ScriptPromiseResolver* resolver, | |
174 payments::mojom::blink::PaymentAppManifestPtr manifest, | |
175 payments::mojom::blink::PaymentAppManifestError error) { | |
176 DCHECK(resolver); | |
177 switch (error) { | |
178 case payments::mojom::blink::PaymentAppManifestError::NONE: | |
179 resolver->resolve( | |
180 mojo::ConvertTo<PaymentAppManifest>(std::move(manifest))); | |
181 break; | |
182 case payments::mojom::blink::PaymentAppManifestError::NOT_IMPLEMENTED: | |
183 resolver->reject( | |
184 DOMException::create(NotSupportedError, "Not implemented yet.")); | |
185 break; | |
186 case payments::mojom::blink::PaymentAppManifestError::NO_ACTIVE_WORKER: | |
187 case payments::mojom::blink::PaymentAppManifestError:: | |
188 MANIFEST_STORAGE_OPERATION_FAILED: | |
189 resolver->reject(DOMException::create( | |
190 AbortError, | |
191 "No payment app manifest associated with the service worker.")); | |
192 break; | |
193 } | |
194 } | |
195 | |
196 void PaymentAppManager::onServiceConnectionError() { | |
197 m_manager.reset(); | |
198 } | |
199 | |
200 } // namespace blink | |
OLD | NEW |