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