| 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/PaymentManager.h" | 5 #include "modules/payments/PaymentManager.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
| 9 #include "modules/payments/PaymentAppManifest.h" | |
| 10 #include "modules/payments/PaymentAppOption.h" | |
| 11 #include "modules/payments/PaymentInstruments.h" | 9 #include "modules/payments/PaymentInstruments.h" |
| 12 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 10 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 13 #include "platform/bindings/ScriptState.h" | 11 #include "platform/bindings/ScriptState.h" |
| 14 #include "platform/mojo/MojoHelper.h" | 12 #include "platform/mojo/MojoHelper.h" |
| 15 #include "public/platform/InterfaceProvider.h" | 13 #include "public/platform/InterfaceProvider.h" |
| 16 #include "public/platform/Platform.h" | 14 #include "public/platform/Platform.h" |
| 17 | 15 |
| 18 namespace mojo { | |
| 19 | |
| 20 using payments::mojom::blink::PaymentAppManifest; | |
| 21 using payments::mojom::blink::PaymentAppManifestPtr; | |
| 22 using payments::mojom::blink::PaymentAppOption; | |
| 23 using payments::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->name = input.hasName() ? input.name() : WTF::g_empty_string; | |
| 30 output->icon = input.hasIcon() ? input.icon() : WTF::String(); | |
| 31 output->id = input.hasId() ? input.id() : WTF::g_empty_string; | |
| 32 if (input.hasEnabledMethods()) { | |
| 33 output->enabled_methods = | |
| 34 WTF::Vector<WTF::String>(input.enabledMethods()); | |
| 35 } | |
| 36 return output; | |
| 37 } | |
| 38 }; | |
| 39 | |
| 40 template <> | |
| 41 struct TypeConverter<PaymentAppManifestPtr, blink::PaymentAppManifest> { | |
| 42 static PaymentAppManifestPtr Convert(const blink::PaymentAppManifest& input) { | |
| 43 PaymentAppManifestPtr output = PaymentAppManifest::New(); | |
| 44 output->name = input.hasName() ? input.name() : WTF::g_empty_string; | |
| 45 output->icon = input.hasIcon() ? input.icon() : WTF::String(); | |
| 46 if (input.hasOptions()) { | |
| 47 for (size_t i = 0; i < input.options().size(); ++i) { | |
| 48 output->options.push_back(PaymentAppOption::From(input.options()[i])); | |
| 49 } | |
| 50 } | |
| 51 return output; | |
| 52 } | |
| 53 }; | |
| 54 | |
| 55 template <> | |
| 56 struct TypeConverter<blink::PaymentAppManifest, PaymentAppManifestPtr> { | |
| 57 static blink::PaymentAppManifest Convert(const PaymentAppManifestPtr& input) { | |
| 58 blink::PaymentAppManifest output; | |
| 59 output.setName(input->name); | |
| 60 output.setIcon(input->icon); | |
| 61 blink::HeapVector<blink::PaymentAppOption> options; | |
| 62 for (const auto& option : input->options) { | |
| 63 options.push_back(mojo::ConvertTo<blink::PaymentAppOption>(option)); | |
| 64 } | |
| 65 output.setOptions(options); | |
| 66 return output; | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 template <> | |
| 71 struct TypeConverter<blink::PaymentAppOption, PaymentAppOptionPtr> { | |
| 72 static blink::PaymentAppOption Convert(const PaymentAppOptionPtr& input) { | |
| 73 blink::PaymentAppOption output; | |
| 74 output.setName(input->name); | |
| 75 output.setIcon(input->icon); | |
| 76 output.setId(input->id); | |
| 77 Vector<WTF::String> enabledMethods; | |
| 78 for (const auto& method : input->enabled_methods) { | |
| 79 enabledMethods.push_back(method); | |
| 80 } | |
| 81 output.setEnabledMethods(enabledMethods); | |
| 82 return output; | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 } // namespace mojo | |
| 87 | |
| 88 namespace blink { | 16 namespace blink { |
| 89 | 17 |
| 90 PaymentManager* PaymentManager::Create( | 18 PaymentManager* PaymentManager::Create( |
| 91 ServiceWorkerRegistration* registration) { | 19 ServiceWorkerRegistration* registration) { |
| 92 return new PaymentManager(registration); | 20 return new PaymentManager(registration); |
| 93 } | 21 } |
| 94 | 22 |
| 95 ScriptPromise PaymentManager::setManifest(ScriptState* script_state, | |
| 96 const PaymentAppManifest& manifest) { | |
| 97 if (!manager_) { | |
| 98 return ScriptPromise::RejectWithDOMException( | |
| 99 script_state, DOMException::Create(kInvalidStateError, | |
| 100 "Payment app manager unavailable.")); | |
| 101 } | |
| 102 | |
| 103 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | |
| 104 ScriptPromise promise = resolver->Promise(); | |
| 105 | |
| 106 manager_->SetManifest( | |
| 107 payments::mojom::blink::PaymentAppManifest::From(manifest), | |
| 108 ConvertToBaseCallback(WTF::Bind(&PaymentManager::OnSetManifest, | |
| 109 WrapPersistent(this), | |
| 110 WrapPersistent(resolver)))); | |
| 111 | |
| 112 return promise; | |
| 113 } | |
| 114 | |
| 115 ScriptPromise PaymentManager::getManifest(ScriptState* script_state) { | |
| 116 if (!manager_) { | |
| 117 return ScriptPromise::RejectWithDOMException( | |
| 118 script_state, DOMException::Create(kInvalidStateError, | |
| 119 "Payment app manager unavailable.")); | |
| 120 } | |
| 121 | |
| 122 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | |
| 123 ScriptPromise promise = resolver->Promise(); | |
| 124 | |
| 125 manager_->GetManifest(ConvertToBaseCallback( | |
| 126 WTF::Bind(&PaymentManager::OnGetManifest, WrapPersistent(this), | |
| 127 WrapPersistent(resolver)))); | |
| 128 | |
| 129 return promise; | |
| 130 } | |
| 131 | |
| 132 PaymentInstruments* PaymentManager::instruments() { | 23 PaymentInstruments* PaymentManager::instruments() { |
| 133 if (!instruments_) | 24 if (!instruments_) |
| 134 instruments_ = new PaymentInstruments(manager_); | 25 instruments_ = new PaymentInstruments(manager_); |
| 135 return instruments_; | 26 return instruments_; |
| 136 } | 27 } |
| 137 | 28 |
| 138 DEFINE_TRACE(PaymentManager) { | 29 DEFINE_TRACE(PaymentManager) { |
| 139 visitor->Trace(registration_); | 30 visitor->Trace(registration_); |
| 140 visitor->Trace(instruments_); | 31 visitor->Trace(instruments_); |
| 141 } | 32 } |
| 142 | 33 |
| 143 PaymentManager::PaymentManager(ServiceWorkerRegistration* registration) | 34 PaymentManager::PaymentManager(ServiceWorkerRegistration* registration) |
| 144 : registration_(registration), instruments_(nullptr) { | 35 : registration_(registration), instruments_(nullptr) { |
| 145 DCHECK(registration); | 36 DCHECK(registration); |
| 146 Platform::Current()->GetInterfaceProvider()->GetInterface( | 37 Platform::Current()->GetInterfaceProvider()->GetInterface( |
| 147 mojo::MakeRequest(&manager_)); | 38 mojo::MakeRequest(&manager_)); |
| 148 | 39 |
| 149 manager_.set_connection_error_handler(ConvertToBaseCallback(WTF::Bind( | 40 manager_.set_connection_error_handler(ConvertToBaseCallback(WTF::Bind( |
| 150 &PaymentManager::OnServiceConnectionError, WrapWeakPersistent(this)))); | 41 &PaymentManager::OnServiceConnectionError, WrapWeakPersistent(this)))); |
| 151 | 42 |
| 152 manager_->Init(registration_->scope()); | 43 manager_->Init(registration_->scope()); |
| 153 } | 44 } |
| 154 | 45 |
| 155 void PaymentManager::OnSetManifest( | |
| 156 ScriptPromiseResolver* resolver, | |
| 157 payments::mojom::blink::PaymentAppManifestError error) { | |
| 158 DCHECK(resolver); | |
| 159 switch (error) { | |
| 160 case payments::mojom::blink::PaymentAppManifestError::NONE: | |
| 161 resolver->Resolve(); | |
| 162 break; | |
| 163 case payments::mojom::blink::PaymentAppManifestError::NOT_IMPLEMENTED: | |
| 164 resolver->Reject( | |
| 165 DOMException::Create(kNotSupportedError, "Not implemented yet.")); | |
| 166 break; | |
| 167 case payments::mojom::blink::PaymentAppManifestError::NO_ACTIVE_WORKER: | |
| 168 resolver->Reject(DOMException::Create(kInvalidStateError, | |
| 169 "No active service worker.")); | |
| 170 break; | |
| 171 case payments::mojom::blink::PaymentAppManifestError:: | |
| 172 MANIFEST_STORAGE_OPERATION_FAILED: | |
| 173 resolver->Reject(DOMException::Create( | |
| 174 kInvalidStateError, "Storing manifest data is failed.")); | |
| 175 break; | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 void PaymentManager::OnGetManifest( | |
| 180 ScriptPromiseResolver* resolver, | |
| 181 payments::mojom::blink::PaymentAppManifestPtr manifest, | |
| 182 payments::mojom::blink::PaymentAppManifestError error) { | |
| 183 DCHECK(resolver); | |
| 184 switch (error) { | |
| 185 case payments::mojom::blink::PaymentAppManifestError::NONE: | |
| 186 resolver->Resolve( | |
| 187 mojo::ConvertTo<PaymentAppManifest>(std::move(manifest))); | |
| 188 break; | |
| 189 case payments::mojom::blink::PaymentAppManifestError::NOT_IMPLEMENTED: | |
| 190 resolver->Reject( | |
| 191 DOMException::Create(kNotSupportedError, "Not implemented yet.")); | |
| 192 break; | |
| 193 case payments::mojom::blink::PaymentAppManifestError::NO_ACTIVE_WORKER: | |
| 194 case payments::mojom::blink::PaymentAppManifestError:: | |
| 195 MANIFEST_STORAGE_OPERATION_FAILED: | |
| 196 resolver->Reject(DOMException::Create( | |
| 197 kAbortError, | |
| 198 "No payment app manifest associated with the service worker.")); | |
| 199 break; | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 void PaymentManager::OnServiceConnectionError() { | 46 void PaymentManager::OnServiceConnectionError() { |
| 204 manager_.reset(); | 47 manager_.reset(); |
| 205 } | 48 } |
| 206 | 49 |
| 207 } // namespace blink | 50 } // namespace blink |
| OLD | NEW |