Chromium Code Reviews| 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" | 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 9 #include "content/browser/service_worker/service_worker_metrics.h" | 9 #include "content/browser/service_worker/service_worker_metrics.h" |
| 10 #include "content/browser/service_worker/service_worker_version.h" | 10 #include "content/browser/service_worker/service_worker_version.h" |
| 11 #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" | 12 #include "content/common/service_worker/service_worker_status_code.h" |
| 13 #include "content/common/service_worker/service_worker_utils.h" | |
| 13 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 20 class ResponseCallback : public mojom::PaymentAppResponseCallback { | |
| 21 public: | |
| 22 static mojom::PaymentAppResponseCallbackPtr Create( | |
| 23 int event_id, | |
| 24 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 25 const PaymentAppProvider::InvokePaymentAppCallback callback) { | |
| 26 ResponseCallback* response_callback = new ResponseCallback( | |
| 27 event_id, std::move(service_worker_version), callback); | |
| 28 return response_callback->binding_.CreateInterfacePtrAndBind(); | |
| 29 } | |
| 30 ~ResponseCallback() override {} | |
| 31 | |
| 32 void OnPaymentAppResponse(payments::mojom::PaymentAppResponsePtr response, | |
| 33 base::Time dispatch_event_time) override { | |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 35 service_worker_version_->FinishRequest(event_id_, false, | |
| 36 dispatch_event_time); | |
| 37 BrowserThread::PostTask( | |
| 38 BrowserThread::UI, FROM_HERE, | |
| 39 base::Bind(callback_, base::Passed(std::move(response)))); | |
| 40 delete this; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 ResponseCallback(int event_id, | |
| 45 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 46 const PaymentAppProvider::InvokePaymentAppCallback callback) | |
| 47 : event_id_(event_id), | |
| 48 service_worker_version_(service_worker_version), | |
| 49 callback_(callback), | |
| 50 binding_(this) {} | |
| 51 | |
| 52 int event_id_; | |
| 53 scoped_refptr<ServiceWorkerVersion> service_worker_version_; | |
| 54 const PaymentAppProvider::InvokePaymentAppCallback callback_; | |
| 55 mojo::Binding<mojom::PaymentAppResponseCallback> binding_; | |
| 56 }; | |
| 57 | |
| 19 void DidGetAllManifestsOnIO( | 58 void DidGetAllManifestsOnIO( |
| 20 const PaymentAppProvider::GetAllManifestsCallback& callback, | 59 const PaymentAppProvider::GetAllManifestsCallback& callback, |
| 21 PaymentAppProvider::Manifests manifests) { | 60 PaymentAppProvider::Manifests manifests) { |
| 22 BrowserThread::PostTask( | 61 BrowserThread::PostTask( |
| 23 BrowserThread::UI, FROM_HERE, | 62 BrowserThread::UI, FROM_HERE, |
| 24 base::Bind(callback, base::Passed(std::move(manifests)))); | 63 base::Bind(callback, base::Passed(std::move(manifests)))); |
| 25 } | 64 } |
| 26 | 65 |
| 27 void GetAllManifestsOnIO( | 66 void GetAllManifestsOnIO( |
| 28 scoped_refptr<PaymentAppContextImpl> payment_app_context, | 67 scoped_refptr<PaymentAppContextImpl> payment_app_context, |
| 29 const PaymentAppProvider::GetAllManifestsCallback& callback) { | 68 const PaymentAppProvider::GetAllManifestsCallback& callback) { |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 69 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 31 | 70 |
| 32 payment_app_context->payment_app_database()->ReadAllManifests( | 71 payment_app_context->payment_app_database()->ReadAllManifests( |
| 33 base::Bind(&DidGetAllManifestsOnIO, callback)); | 72 base::Bind(&DidGetAllManifestsOnIO, callback)); |
| 34 } | 73 } |
| 35 | 74 |
| 36 void DidDispatchPaymentRequestEvent( | 75 void DidDispatchPaymentRequestEvent( |
| 37 scoped_refptr<ServiceWorkerVersion> active_version, | 76 scoped_refptr<ServiceWorkerVersion> active_version, |
| 38 int request_id, | 77 int event_finish_id, |
| 39 ServiceWorkerStatusCode service_worker_status, | 78 ServiceWorkerStatusCode service_worker_status, |
| 40 base::Time dispatch_event_time) { | 79 base::Time dispatch_event_time) { |
| 41 active_version->FinishRequest(request_id, | 80 active_version->FinishRequest(event_finish_id, |
| 42 service_worker_status == SERVICE_WORKER_OK, | 81 service_worker_status == SERVICE_WORKER_OK, |
| 43 dispatch_event_time); | 82 dispatch_event_time); |
| 44 } | 83 } |
| 45 | 84 |
| 46 void DispatchPaymentRequestEventError( | 85 void DispatchPaymentRequestEventError( |
| 47 ServiceWorkerStatusCode service_worker_status) { | 86 ServiceWorkerStatusCode service_worker_status) { |
| 48 NOTIMPLEMENTED(); | 87 NOTIMPLEMENTED(); |
| 49 } | 88 } |
| 50 | 89 |
| 51 void DispatchPaymentRequestEvent( | 90 void DispatchPaymentRequestEvent( |
| 52 payments::mojom::PaymentAppRequestPtr app_request, | 91 payments::mojom::PaymentAppRequestPtr app_request, |
| 92 const PaymentAppProvider::InvokePaymentAppCallback& callback, | |
| 53 scoped_refptr<ServiceWorkerVersion> active_version) { | 93 scoped_refptr<ServiceWorkerVersion> active_version) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 94 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 55 DCHECK(active_version); | 95 DCHECK(active_version); |
| 56 | 96 |
| 57 int request_id = active_version->StartRequest( | 97 int event_id = active_version->StartRequest( |
|
shimazu
2017/03/08 01:39:39
|event_id| vs |event_finish_id| seems a bit ambigu
zino
2017/03/19 04:47:23
Done.
| |
| 58 ServiceWorkerMetrics::EventType::PAYMENT_REQUEST, | 98 ServiceWorkerMetrics::EventType::PAYMENT_REQUEST, |
| 59 base::Bind(&DispatchPaymentRequestEventError)); | 99 base::Bind(&DispatchPaymentRequestEventError)); |
| 100 int event_finish_id = active_version->StartRequest( | |
| 101 ServiceWorkerMetrics::EventType::PAYMENT_REQUEST, | |
| 102 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
| 60 | 103 |
| 104 mojom::PaymentAppResponseCallbackPtr response_callback_ptr = | |
| 105 ResponseCallback::Create(event_id, active_version, callback); | |
| 106 DCHECK(response_callback_ptr); | |
| 61 active_version->event_dispatcher()->DispatchPaymentRequestEvent( | 107 active_version->event_dispatcher()->DispatchPaymentRequestEvent( |
| 62 std::move(app_request), | 108 event_id, std::move(app_request), std::move(response_callback_ptr), |
| 63 base::Bind(&DidDispatchPaymentRequestEvent, active_version, request_id)); | 109 base::Bind(&DidDispatchPaymentRequestEvent, active_version, |
|
shimazu
2017/03/08 01:39:39
Could you use active_version->CreateSimpleEventCal
zino
2017/03/19 04:47:23
Done.
| |
| 110 event_finish_id)); | |
| 64 } | 111 } |
| 65 | 112 |
| 66 void DidFindRegistrationOnIO( | 113 void DidFindRegistrationOnIO( |
| 67 payments::mojom::PaymentAppRequestPtr app_request, | 114 payments::mojom::PaymentAppRequestPtr app_request, |
| 115 const PaymentAppProvider::InvokePaymentAppCallback& callback, | |
| 68 ServiceWorkerStatusCode service_worker_status, | 116 ServiceWorkerStatusCode service_worker_status, |
| 69 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { | 117 scoped_refptr<ServiceWorkerRegistration> service_worker_registration) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 71 | 119 |
| 72 if (service_worker_status != SERVICE_WORKER_OK) | 120 if (service_worker_status != SERVICE_WORKER_OK) |
| 73 return; | 121 return; |
| 74 | 122 |
| 75 ServiceWorkerVersion* active_version = | 123 ServiceWorkerVersion* active_version = |
| 76 service_worker_registration->active_version(); | 124 service_worker_registration->active_version(); |
| 77 DCHECK(active_version); | 125 DCHECK(active_version); |
| 78 active_version->RunAfterStartWorker( | 126 active_version->RunAfterStartWorker( |
| 79 ServiceWorkerMetrics::EventType::PAYMENT_REQUEST, | 127 ServiceWorkerMetrics::EventType::PAYMENT_REQUEST, |
| 80 base::Bind(&DispatchPaymentRequestEvent, | 128 base::Bind(&DispatchPaymentRequestEvent, |
| 81 base::Passed(std::move(app_request)), | 129 base::Passed(std::move(app_request)), callback, |
| 82 make_scoped_refptr(active_version)), | 130 make_scoped_refptr(active_version)), |
| 83 base::Bind(&DispatchPaymentRequestEventError)); | 131 base::Bind(&DispatchPaymentRequestEventError)); |
| 84 } | 132 } |
| 85 | 133 |
| 86 void FindRegistrationOnIO( | 134 void FindRegistrationOnIO( |
| 87 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, | 135 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
| 88 int64_t registration_id, | 136 int64_t registration_id, |
| 89 payments::mojom::PaymentAppRequestPtr app_request) { | 137 payments::mojom::PaymentAppRequestPtr app_request, |
| 138 const PaymentAppProvider::InvokePaymentAppCallback& callback) { | |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 91 | 140 |
| 92 service_worker_context->FindReadyRegistrationForIdOnly( | 141 service_worker_context->FindReadyRegistrationForIdOnly( |
| 93 registration_id, base::Bind(&DidFindRegistrationOnIO, | 142 registration_id, |
| 94 base::Passed(std::move(app_request)))); | 143 base::Bind(&DidFindRegistrationOnIO, base::Passed(std::move(app_request)), |
| 144 callback)); | |
| 95 } | 145 } |
| 96 | 146 |
| 97 } // namespace | 147 } // namespace |
| 98 | 148 |
| 99 // static | 149 // static |
| 100 PaymentAppProvider* PaymentAppProvider::GetInstance() { | 150 PaymentAppProvider* PaymentAppProvider::GetInstance() { |
| 101 return PaymentAppProviderImpl::GetInstance(); | 151 return PaymentAppProviderImpl::GetInstance(); |
| 102 } | 152 } |
| 103 | 153 |
| 104 // static | 154 // static |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 118 partition->GetPaymentAppContext(); | 168 partition->GetPaymentAppContext(); |
| 119 | 169 |
| 120 BrowserThread::PostTask( | 170 BrowserThread::PostTask( |
| 121 BrowserThread::IO, FROM_HERE, | 171 BrowserThread::IO, FROM_HERE, |
| 122 base::Bind(&GetAllManifestsOnIO, payment_app_context, callback)); | 172 base::Bind(&GetAllManifestsOnIO, payment_app_context, callback)); |
| 123 } | 173 } |
| 124 | 174 |
| 125 void PaymentAppProviderImpl::InvokePaymentApp( | 175 void PaymentAppProviderImpl::InvokePaymentApp( |
| 126 BrowserContext* browser_context, | 176 BrowserContext* browser_context, |
| 127 int64_t registration_id, | 177 int64_t registration_id, |
| 128 payments::mojom::PaymentAppRequestPtr app_request) { | 178 payments::mojom::PaymentAppRequestPtr app_request, |
| 179 const InvokePaymentAppCallback& callback) { | |
| 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 180 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 130 | 181 |
| 131 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( | 182 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 132 BrowserContext::GetDefaultStoragePartition(browser_context)); | 183 BrowserContext::GetDefaultStoragePartition(browser_context)); |
| 133 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | 184 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
| 134 partition->GetServiceWorkerContext(); | 185 partition->GetServiceWorkerContext(); |
| 135 | 186 |
| 136 BrowserThread::PostTask( | 187 BrowserThread::PostTask( |
| 137 BrowserThread::IO, FROM_HERE, | 188 BrowserThread::IO, FROM_HERE, |
| 138 base::Bind(&FindRegistrationOnIO, std::move(service_worker_context), | 189 base::Bind(&FindRegistrationOnIO, std::move(service_worker_context), |
| 139 registration_id, base::Passed(std::move(app_request)))); | 190 registration_id, base::Passed(std::move(app_request)), |
| 191 callback)); | |
| 140 } | 192 } |
| 141 | 193 |
| 142 PaymentAppProviderImpl::PaymentAppProviderImpl() {} | 194 PaymentAppProviderImpl::PaymentAppProviderImpl() {} |
| 143 | 195 |
| 144 PaymentAppProviderImpl::~PaymentAppProviderImpl() {} | 196 PaymentAppProviderImpl::~PaymentAppProviderImpl() {} |
| 145 | 197 |
| 146 } // namespace content | 198 } // namespace content |
| OLD | NEW |