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