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