Chromium Code Reviews| 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 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 25 return ScriptPromise(); | 70 ScriptPromise promise = resolver->promise(); |
| 71 | |
| 72 m_manager->SetManifest( | |
| 73 m_registration->scope(), | |
| 74 payments::mojom::blink::PaymentAppManifest::From(manifest), | |
| 75 convertToBaseCallback(WTF::bind(&PaymentAppManager::onSetManifest, | |
| 76 wrapPersistent(this), | |
| 77 wrapPersistent(resolver)))); | |
| 78 | |
| 79 return promise; | |
| 80 } | |
| 81 | |
| 82 void PaymentAppManager::onSetManifest( | |
| 83 ScriptPromiseResolver* resolver, | |
| 84 payments::mojom::blink::PaymentAppManifestError error) { | |
| 85 DCHECK(resolver); | |
| 86 switch (error) { | |
| 87 case payments::mojom::blink::PaymentAppManifestError::NOT_IMPLEMENTED: | |
| 88 resolver->reject( | |
| 89 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(); | |
|
jkarlin
2016/11/08 13:48:53
If m_manager can be reset, should it be checked fo
zino
2016/11/08 14:34:21
Done.
| |
| 119 } | |
| 120 | |
| 121 void PaymentAppManager::contextDestroyed() { | |
| 122 m_manager.reset(); | |
| 35 } | 123 } |
| 36 | 124 |
| 37 } // namespace blink | 125 } // namespace blink |
| OLD | NEW |