Chromium Code Reviews| Index: third_party/WebKit/Source/modules/payments/PaymentAppManager.cpp |
| diff --git a/third_party/WebKit/Source/modules/payments/PaymentAppManager.cpp b/third_party/WebKit/Source/modules/payments/PaymentAppManager.cpp |
| index c6488fa0b66c096a9259bcd065ec8a586ee46476..f48b527c03cef1cf27c7f00a591ba2345c53e9b3 100644 |
| --- a/third_party/WebKit/Source/modules/payments/PaymentAppManager.cpp |
| +++ b/third_party/WebKit/Source/modules/payments/PaymentAppManager.cpp |
| @@ -4,34 +4,122 @@ |
| #include "modules/payments/PaymentAppManager.h" |
| +#include "bindings/core/v8/ScriptPromise.h" |
| +#include "bindings/core/v8/ScriptState.h" |
| +#include "core/dom/DOMException.h" |
| #include "modules/payments/PaymentAppManifest.h" |
| +#include "modules/payments/PaymentAppOption.h" |
| #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| +#include "platform/mojo/MojoHelper.h" |
| +#include "public/platform/InterfaceProvider.h" |
| +#include "public/platform/Platform.h" |
| + |
| +namespace mojo { |
| + |
| +using payments::mojom::blink::PaymentAppManifest; |
| +using payments::mojom::blink::PaymentAppManifestPtr; |
| +using payments::mojom::blink::PaymentAppOption; |
| +using payments::mojom::blink::PaymentAppOptionPtr; |
| + |
| +template <> |
| +struct TypeConverter<PaymentAppOptionPtr, blink::PaymentAppOption> { |
| + static PaymentAppOptionPtr Convert(const blink::PaymentAppOption& input) { |
| + PaymentAppOptionPtr output = PaymentAppOption::New(); |
| + output->label = input.hasLabel() ? input.label() : WTF::emptyString(); |
| + output->icon = input.hasIcon() ? input.icon() : WTF::String(); |
| + output->id = input.hasId() ? input.id() : WTF::emptyString(); |
| + output->enabled_methods = WTF::Vector<WTF::String>(input.enabledMethods()); |
| + return output; |
| + } |
| +}; |
| + |
| +template <> |
| +struct TypeConverter<PaymentAppManifestPtr, blink::PaymentAppManifest> { |
| + static PaymentAppManifestPtr Convert(const blink::PaymentAppManifest& input) { |
| + PaymentAppManifestPtr output = PaymentAppManifest::New(); |
| + output->label = input.hasLabel() ? input.label() : WTF::emptyString(); |
| + output->icon = input.hasIcon() ? input.icon() : WTF::String(); |
| + if (input.hasOptions()) { |
| + for (size_t i = 0; i < input.options().size(); ++i) { |
| + output->options.append(PaymentAppOption::From(input.options()[i])); |
| + } |
| + } |
| + return output; |
| + } |
| +}; |
| + |
| +} // namespace mojo |
| namespace blink { |
| PaymentAppManager* PaymentAppManager::create( |
| + ScriptState* scriptState, |
| ServiceWorkerRegistration* registration) { |
| - return new PaymentAppManager(registration); |
| + return new PaymentAppManager(scriptState, registration); |
| } |
| -ScriptPromise PaymentAppManager::getManifest() { |
| +ScriptPromise PaymentAppManager::getManifest(ScriptState* scriptState) { |
| NOTIMPLEMENTED(); |
| return ScriptPromise(); |
| } |
| ScriptPromise PaymentAppManager::setManifest( |
| + ScriptState* scriptState, |
| const PaymentAppManifest& manifest) { |
| - NOTIMPLEMENTED(); |
| - return ScriptPromise(); |
| + ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + |
| + m_manager->SetManifest( |
| + m_registration->scope(), |
| + payments::mojom::blink::PaymentAppManifest::From(manifest), |
| + convertToBaseCallback(WTF::bind(&PaymentAppManager::onSetManifest, |
| + wrapPersistent(this), |
| + wrapPersistent(resolver)))); |
| + |
| + return promise; |
| +} |
| + |
| +void PaymentAppManager::onSetManifest( |
| + ScriptPromiseResolver* resolver, |
| + payments::mojom::blink::PaymentAppManifestError error) { |
| + DCHECK(resolver); |
| + switch (error) { |
| + case payments::mojom::blink::PaymentAppManifestError::NOT_IMPLEMENTED: |
| + resolver->reject( |
| + DOMException::create(NotSupportedError, "Not implemented yet.")); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| } |
| DEFINE_TRACE(PaymentAppManager) { |
| visitor->trace(m_registration); |
| + ContextLifecycleObserver::trace(visitor); |
| } |
| -PaymentAppManager::PaymentAppManager(ServiceWorkerRegistration* registration) |
| - : m_registration(registration) { |
| +PaymentAppManager::PaymentAppManager(ScriptState* scriptState, |
| + ServiceWorkerRegistration* registration) |
| + : ContextLifecycleObserver(scriptState->getExecutionContext()), |
| + m_registration(registration) { |
| DCHECK(registration); |
| + Platform::current()->interfaceProvider()->getInterface( |
| + mojo::GetProxy(&m_manager)); |
| + |
| + m_manager.set_connection_error_handler(convertToBaseCallback(WTF::bind( |
| + &PaymentAppManager::onServiceConnectionError, wrapWeakPersistent(this)))); |
| +} |
| + |
| +void PaymentAppManager::onServiceConnectionError() { |
| + if (!Platform::current()) { |
| + return; |
| + } |
| + |
| + 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.
|
| +} |
| + |
| +void PaymentAppManager::contextDestroyed() { |
| + m_manager.reset(); |
| } |
| } // namespace blink |