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" |
| 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "core/dom/DOMException.h" |
7 #include "modules/payments/PaymentAppManifest.h" | 10 #include "modules/payments/PaymentAppManifest.h" |
| 11 #include "modules/payments/PaymentAppOption.h" |
8 #include "modules/serviceworkers/ServiceWorkerRegistration.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->label = input.hasLabel() ? input.label() : WTF::emptyString(); |
| 29 output->icon = input.hasIcon() ? input.icon() : WTF::String(); |
| 30 output->id = input.hasId() ? input.id() : WTF::emptyString(); |
| 31 output->enabled_methods = WTF::Vector<WTF::String>(input.enabledMethods()); |
| 32 return output; |
| 33 } |
| 34 }; |
| 35 |
| 36 template <> |
| 37 struct TypeConverter<PaymentAppManifestPtr, blink::PaymentAppManifest> { |
| 38 static PaymentAppManifestPtr Convert(const blink::PaymentAppManifest& input) { |
| 39 PaymentAppManifestPtr output = PaymentAppManifest::New(); |
| 40 output->label = input.hasLabel() ? input.label() : WTF::emptyString(); |
| 41 output->icon = input.hasIcon() ? input.icon() : WTF::String(); |
| 42 if (input.hasOptions()) { |
| 43 for (size_t i = 0; i < input.options().size(); ++i) { |
| 44 output->options.append(PaymentAppOption::From(input.options()[i])); |
| 45 } |
| 46 } |
| 47 return output; |
| 48 } |
| 49 }; |
| 50 |
| 51 } // namespace mojo |
9 | 52 |
10 namespace blink { | 53 namespace blink { |
11 | 54 |
12 PaymentAppManager* PaymentAppManager::create( | 55 PaymentAppManager* PaymentAppManager::create( |
| 56 ScriptState* scriptState, |
13 ServiceWorkerRegistration* registration) { | 57 ServiceWorkerRegistration* registration) { |
14 return new PaymentAppManager(registration); | 58 return new PaymentAppManager(scriptState, registration); |
15 } | 59 } |
16 | 60 |
17 ScriptPromise PaymentAppManager::getManifest() { | 61 ScriptPromise PaymentAppManager::getManifest(ScriptState* scriptState) { |
18 NOTIMPLEMENTED(); | 62 NOTIMPLEMENTED(); |
19 return ScriptPromise(); | 63 return ScriptPromise(); |
20 } | 64 } |
21 | 65 |
22 ScriptPromise PaymentAppManager::setManifest( | 66 ScriptPromise PaymentAppManager::setManifest( |
| 67 ScriptState* scriptState, |
23 const PaymentAppManifest& manifest) { | 68 const PaymentAppManifest& manifest) { |
24 NOTIMPLEMENTED(); | 69 if (!m_manager) { |
25 return ScriptPromise(); | 70 return ScriptPromise::rejectWithDOMException( |
| 71 scriptState, DOMException::create(InvalidStateError, |
| 72 "Payment app manager unavailable.")); |
| 73 } |
| 74 |
| 75 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 76 ScriptPromise promise = resolver->promise(); |
| 77 |
| 78 m_manager->SetManifest( |
| 79 m_registration->scope(), |
| 80 payments::mojom::blink::PaymentAppManifest::From(manifest), |
| 81 convertToBaseCallback(WTF::bind(&PaymentAppManager::onSetManifest, |
| 82 wrapPersistent(this), |
| 83 wrapPersistent(resolver)))); |
| 84 |
| 85 return promise; |
| 86 } |
| 87 |
| 88 void PaymentAppManager::onSetManifest( |
| 89 ScriptPromiseResolver* resolver, |
| 90 payments::mojom::blink::PaymentAppManifestError error) { |
| 91 DCHECK(resolver); |
| 92 switch (error) { |
| 93 case payments::mojom::blink::PaymentAppManifestError::NOT_IMPLEMENTED: |
| 94 resolver->reject( |
| 95 DOMException::create(NotSupportedError, "Not implemented yet.")); |
| 96 break; |
| 97 default: |
| 98 NOTREACHED(); |
| 99 } |
26 } | 100 } |
27 | 101 |
28 DEFINE_TRACE(PaymentAppManager) { | 102 DEFINE_TRACE(PaymentAppManager) { |
29 visitor->trace(m_registration); | 103 visitor->trace(m_registration); |
| 104 ContextLifecycleObserver::trace(visitor); |
30 } | 105 } |
31 | 106 |
32 PaymentAppManager::PaymentAppManager(ServiceWorkerRegistration* registration) | 107 PaymentAppManager::PaymentAppManager(ScriptState* scriptState, |
33 : m_registration(registration) { | 108 ServiceWorkerRegistration* registration) |
| 109 : ContextLifecycleObserver(scriptState->getExecutionContext()), |
| 110 m_registration(registration) { |
34 DCHECK(registration); | 111 DCHECK(registration); |
| 112 Platform::current()->interfaceProvider()->getInterface( |
| 113 mojo::GetProxy(&m_manager)); |
| 114 |
| 115 m_manager.set_connection_error_handler(convertToBaseCallback(WTF::bind( |
| 116 &PaymentAppManager::onServiceConnectionError, wrapWeakPersistent(this)))); |
| 117 } |
| 118 |
| 119 void PaymentAppManager::onServiceConnectionError() { |
| 120 if (!Platform::current()) { |
| 121 return; |
| 122 } |
| 123 |
| 124 m_manager.reset(); |
| 125 } |
| 126 |
| 127 void PaymentAppManager::contextDestroyed() { |
| 128 m_manager.reset(); |
35 } | 129 } |
36 | 130 |
37 } // namespace blink | 131 } // namespace blink |
OLD | NEW |