| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/payments/payment_app_provider_impl.h" | 5 #include "content/browser/payments/payment_app_provider_impl.h" |
| 6 | 6 |
| 7 #include "content/browser/payments/payment_app_context_impl.h" | 7 #include "content/browser/payments/payment_app_context_impl.h" |
| 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 9 #include "content/browser/service_worker/service_worker_metrics.h" |
| 10 #include "content/browser/service_worker/service_worker_version.h" |
| 8 #include "content/browser/storage_partition_impl.h" | 11 #include "content/browser/storage_partition_impl.h" |
| 12 #include "content/common/service_worker/service_worker_status_code.h" |
| 9 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 11 | 15 |
| 12 namespace content { | 16 namespace content { |
| 13 namespace { | 17 namespace { |
| 14 | 18 |
| 15 void DidGetAllManifestsOnIO( | 19 void DidGetAllManifestsOnIO( |
| 16 const PaymentAppProvider::GetAllManifestsCallback& callback, | 20 const PaymentAppProvider::GetAllManifestsCallback& callback, |
| 17 PaymentAppProvider::Manifests manifests) { | 21 PaymentAppProvider::Manifests manifests) { |
| 18 BrowserThread::PostTask( | 22 BrowserThread::PostTask( |
| 19 BrowserThread::UI, FROM_HERE, | 23 BrowserThread::UI, FROM_HERE, |
| 20 base::Bind(callback, base::Passed(std::move(manifests)))); | 24 base::Bind(callback, base::Passed(std::move(manifests)))); |
| 21 } | 25 } |
| 22 | 26 |
| 23 void GetAllManifestsOnIO( | 27 void GetAllManifestsOnIO( |
| 24 const scoped_refptr<PaymentAppContextImpl>& payment_app_context, | 28 scoped_refptr<PaymentAppContextImpl> payment_app_context, |
| 25 const PaymentAppProvider::GetAllManifestsCallback& callback) { | 29 const PaymentAppProvider::GetAllManifestsCallback& callback) { |
| 26 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 27 | 31 |
| 28 payment_app_context->payment_app_database()->ReadAllManifests( | 32 payment_app_context->payment_app_database()->ReadAllManifests( |
| 29 base::Bind(&DidGetAllManifestsOnIO, callback)); | 33 base::Bind(&DidGetAllManifestsOnIO, callback)); |
| 30 } | 34 } |
| 31 | 35 |
| 36 void DidDispatchPaymentRequestEvent( |
| 37 scoped_refptr<ServiceWorkerVersion> active_version, |
| 38 int request_id, |
| 39 ServiceWorkerStatusCode service_worker_status, |
| 40 base::Time dispatch_event_time) { |
| 41 active_version->FinishRequest(request_id, |
| 42 service_worker_status == SERVICE_WORKER_OK, |
| 43 dispatch_event_time); |
| 44 } |
| 45 |
| 46 void DispatchPaymentRequestEventError( |
| 47 ServiceWorkerStatusCode service_worker_status) { |
| 48 NOTIMPLEMENTED(); |
| 49 } |
| 50 |
| 51 void DispatchPaymentRequestEvent( |
| 52 payments::mojom::PaymentAppRequestDataPtr data, |
| 53 scoped_refptr<ServiceWorkerVersion> active_version) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 55 DCHECK(active_version); |
| 56 |
| 57 int request_id = active_version->StartRequest( |
| 58 ServiceWorkerMetrics::EventType::PAYMENT_REQUEST, |
| 59 base::Bind(&DispatchPaymentRequestEventError)); |
| 60 |
| 61 active_version->event_dispatcher()->DispatchPaymentRequestEvent( |
| 62 std::move(data), |
| 63 base::Bind(&DidDispatchPaymentRequestEvent, active_version, request_id)); |
| 64 } |
| 65 |
| 66 void DidFindRegistrationOnIO( |
| 67 payments::mojom::PaymentAppRequestDataPtr data, |
| 68 ServiceWorkerStatusCode service_worker_status, |
| 69 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 71 |
| 72 if (service_worker_status != SERVICE_WORKER_OK) |
| 73 return; |
| 74 |
| 75 ServiceWorkerVersion* active_version = |
| 76 service_worker_registration->active_version(); |
| 77 DCHECK(active_version); |
| 78 active_version->RunAfterStartWorker( |
| 79 ServiceWorkerMetrics::EventType::PAYMENT_REQUEST, |
| 80 base::Bind(&DispatchPaymentRequestEvent, base::Passed(std::move(data)), |
| 81 make_scoped_refptr(active_version)), |
| 82 base::Bind(&DispatchPaymentRequestEventError)); |
| 83 } |
| 84 |
| 85 void FindRegistrationOnIO( |
| 86 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
| 87 int64_t registration_id, |
| 88 payments::mojom::PaymentAppRequestDataPtr data) { |
| 89 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 90 |
| 91 service_worker_context->FindReadyRegistrationForIdOnly( |
| 92 registration_id, |
| 93 base::Bind(&DidFindRegistrationOnIO, base::Passed(std::move(data)))); |
| 94 } |
| 95 |
| 32 } // namespace | 96 } // namespace |
| 33 | 97 |
| 34 // static | 98 // static |
| 35 PaymentAppProvider* PaymentAppProvider::GetInstance() { | 99 PaymentAppProvider* PaymentAppProvider::GetInstance() { |
| 36 return PaymentAppProviderImpl::GetInstance(); | 100 return PaymentAppProviderImpl::GetInstance(); |
| 37 } | 101 } |
| 38 | 102 |
| 39 // static | 103 // static |
| 40 PaymentAppProviderImpl* PaymentAppProviderImpl::GetInstance() { | 104 PaymentAppProviderImpl* PaymentAppProviderImpl::GetInstance() { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 105 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 54 | 118 |
| 55 BrowserThread::PostTask( | 119 BrowserThread::PostTask( |
| 56 BrowserThread::IO, FROM_HERE, | 120 BrowserThread::IO, FROM_HERE, |
| 57 base::Bind(&GetAllManifestsOnIO, payment_app_context, callback)); | 121 base::Bind(&GetAllManifestsOnIO, payment_app_context, callback)); |
| 58 } | 122 } |
| 59 | 123 |
| 60 void PaymentAppProviderImpl::InvokePaymentApp( | 124 void PaymentAppProviderImpl::InvokePaymentApp( |
| 61 BrowserContext* browser_context, | 125 BrowserContext* browser_context, |
| 62 int64_t registration_id, | 126 int64_t registration_id, |
| 63 payments::mojom::PaymentAppRequestDataPtr data) { | 127 payments::mojom::PaymentAppRequestDataPtr data) { |
| 64 NOTIMPLEMENTED(); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 129 |
| 130 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 131 BrowserContext::GetDefaultStoragePartition(browser_context)); |
| 132 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
| 133 partition->GetServiceWorkerContext(); |
| 134 |
| 135 BrowserThread::PostTask( |
| 136 BrowserThread::IO, FROM_HERE, |
| 137 base::Bind(&FindRegistrationOnIO, std::move(service_worker_context), |
| 138 registration_id, base::Passed(std::move(data)))); |
| 65 } | 139 } |
| 66 | 140 |
| 67 PaymentAppProviderImpl::PaymentAppProviderImpl() {} | 141 PaymentAppProviderImpl::PaymentAppProviderImpl() {} |
| 68 | 142 |
| 69 PaymentAppProviderImpl::~PaymentAppProviderImpl() {} | 143 PaymentAppProviderImpl::~PaymentAppProviderImpl() {} |
| 70 | 144 |
| 71 } // namespace content | 145 } // namespace content |
| OLD | NEW |