| 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 "core/dom/UserGestureIndicator.h" |
| 9 #include "modules/payments/PaymentInstruments.h" | 10 #include "modules/payments/PaymentInstruments.h" |
| 11 #include "modules/permissions/PermissionUtils.h" |
| 10 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 12 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 11 #include "platform/bindings/ScriptState.h" | 13 #include "platform/bindings/ScriptState.h" |
| 12 #include "platform/mojo/MojoHelper.h" | 14 #include "platform/mojo/MojoHelper.h" |
| 13 #include "public/platform/InterfaceProvider.h" | 15 #include "public/platform/InterfaceProvider.h" |
| 14 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 15 | 17 |
| 16 namespace blink { | 18 namespace blink { |
| 17 | 19 |
| 18 PaymentManager* PaymentManager::Create( | 20 PaymentManager* PaymentManager::Create( |
| 19 ServiceWorkerRegistration* registration) { | 21 ServiceWorkerRegistration* registration) { |
| 20 return new PaymentManager(registration); | 22 return new PaymentManager(registration); |
| 21 } | 23 } |
| 22 | 24 |
| 23 PaymentInstruments* PaymentManager::instruments() { | 25 PaymentInstruments* PaymentManager::instruments() { |
| 24 if (!instruments_) | 26 if (!instruments_) |
| 25 instruments_ = new PaymentInstruments(manager_); | 27 instruments_ = new PaymentInstruments(manager_); |
| 26 return instruments_; | 28 return instruments_; |
| 27 } | 29 } |
| 28 | 30 |
| 31 ScriptPromise PaymentManager::requestPermission(ScriptState* script_state) { |
| 32 ExecutionContext* context = ExecutionContext::From(script_state); |
| 33 |
| 34 if (!permission_service_) { |
| 35 ConnectToPermissionService(context, |
| 36 mojo::MakeRequest(&permission_service_)); |
| 37 permission_service_.set_connection_error_handler(ConvertToBaseCallback( |
| 38 WTF::Bind(&PaymentManager::OnPermissionServiceConnectionError, |
| 39 WrapWeakPersistent(this)))); |
| 40 } |
| 41 |
| 42 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 43 ScriptPromise promise = resolver->Promise(); |
| 44 |
| 45 permission_service_->RequestPermission( |
| 46 CreatePermissionDescriptor(mojom::blink::PermissionName::PAYMENT_HANDLER), |
| 47 context->GetSecurityOrigin(), |
| 48 UserGestureIndicator::ProcessingUserGesture(), |
| 49 ConvertToBaseCallback( |
| 50 WTF::Bind(&PaymentManager::OnPermissionRequestComplete, |
| 51 WrapPersistent(this), WrapPersistent(resolver)))); |
| 52 |
| 53 return promise; |
| 54 } |
| 55 |
| 29 DEFINE_TRACE(PaymentManager) { | 56 DEFINE_TRACE(PaymentManager) { |
| 30 visitor->Trace(registration_); | 57 visitor->Trace(registration_); |
| 31 visitor->Trace(instruments_); | 58 visitor->Trace(instruments_); |
| 32 } | 59 } |
| 33 | 60 |
| 34 PaymentManager::PaymentManager(ServiceWorkerRegistration* registration) | 61 PaymentManager::PaymentManager(ServiceWorkerRegistration* registration) |
| 35 : registration_(registration), instruments_(nullptr) { | 62 : registration_(registration), instruments_(nullptr) { |
| 36 DCHECK(registration); | 63 DCHECK(registration); |
| 37 Platform::Current()->GetInterfaceProvider()->GetInterface( | 64 Platform::Current()->GetInterfaceProvider()->GetInterface( |
| 38 mojo::MakeRequest(&manager_)); | 65 mojo::MakeRequest(&manager_)); |
| 39 | 66 |
| 40 manager_.set_connection_error_handler(ConvertToBaseCallback(WTF::Bind( | 67 manager_.set_connection_error_handler(ConvertToBaseCallback(WTF::Bind( |
| 41 &PaymentManager::OnServiceConnectionError, WrapWeakPersistent(this)))); | 68 &PaymentManager::OnServiceConnectionError, WrapWeakPersistent(this)))); |
| 42 | 69 |
| 43 manager_->Init(registration_->scope()); | 70 manager_->Init(registration_->scope()); |
| 44 } | 71 } |
| 45 | 72 |
| 46 void PaymentManager::OnServiceConnectionError() { | 73 void PaymentManager::OnServiceConnectionError() { |
| 47 manager_.reset(); | 74 manager_.reset(); |
| 48 } | 75 } |
| 49 | 76 |
| 77 void PaymentManager::OnPermissionRequestComplete( |
| 78 ScriptPromiseResolver* resolver, |
| 79 mojom::blink::PermissionStatus status) { |
| 80 switch (status) { |
| 81 case mojom::blink::PermissionStatus::GRANTED: |
| 82 return resolver->Resolve("granted"); |
| 83 case mojom::blink::PermissionStatus::DENIED: |
| 84 return resolver->Resolve("denied"); |
| 85 case mojom::blink::PermissionStatus::ASK: |
| 86 return resolver->Resolve("default"); |
| 87 } |
| 88 } |
| 89 |
| 90 void PaymentManager::OnPermissionServiceConnectionError() { |
| 91 permission_service_.reset(); |
| 92 } |
| 93 |
| 50 } // namespace blink | 94 } // namespace blink |
| OLD | NEW |