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