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 "content/browser/payments/payment_app_database.h" | 5 #include "content/browser/payments/payment_app_database.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/optional.h" | 10 #include "base/optional.h" |
11 #include "content/browser/payments/payment_app.pb.h" | 11 #include "content/browser/payments/payment_app.pb.h" |
12 #include "content/browser/payments/payment_app_context_impl.h" | 12 #include "content/browser/payments/payment_app_context_impl.h" |
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 namespace { | 18 namespace { |
19 | 19 |
| 20 using ::payments::mojom::PaymentHandlerStatus; |
| 21 using ::payments::mojom::PaymentInstrument; |
| 22 using ::payments::mojom::PaymentInstrumentPtr; |
| 23 |
20 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; | 24 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; |
21 | 25 |
22 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( | 26 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( |
23 const std::string& input) { | 27 const std::string& input) { |
24 PaymentAppManifestProto manifest_proto; | 28 PaymentAppManifestProto manifest_proto; |
25 if (!manifest_proto.ParseFromString(input)) | 29 if (!manifest_proto.ParseFromString(input)) |
26 return nullptr; | 30 return nullptr; |
27 | 31 |
28 payments::mojom::PaymentAppManifestPtr manifest = | 32 payments::mojom::PaymentAppManifestPtr manifest = |
29 payments::mojom::PaymentAppManifest::New(); | 33 payments::mojom::PaymentAppManifest::New(); |
30 manifest->name = manifest_proto.name(); | 34 manifest->name = manifest_proto.name(); |
31 if (manifest_proto.has_icon()) | 35 if (manifest_proto.has_icon()) |
32 manifest->icon = manifest_proto.icon(); | 36 manifest->icon = manifest_proto.icon(); |
33 for (const auto& option_proto : manifest_proto.options()) { | 37 for (const auto& option_proto : manifest_proto.options()) { |
34 payments::mojom::PaymentAppOptionPtr option = | 38 payments::mojom::PaymentAppOptionPtr option = |
35 payments::mojom::PaymentAppOption::New(); | 39 payments::mojom::PaymentAppOption::New(); |
36 option->name = option_proto.name(); | 40 option->name = option_proto.name(); |
37 if (option_proto.has_icon()) | 41 if (option_proto.has_icon()) |
38 option->icon = option_proto.icon(); | 42 option->icon = option_proto.icon(); |
39 option->id = option_proto.id(); | 43 option->id = option_proto.id(); |
40 for (const auto& method : option_proto.enabled_methods()) | 44 for (const auto& method : option_proto.enabled_methods()) |
41 option->enabled_methods.push_back(method); | 45 option->enabled_methods.push_back(method); |
42 manifest->options.push_back(std::move(option)); | 46 manifest->options.push_back(std::move(option)); |
43 } | 47 } |
44 | 48 |
45 return manifest; | 49 return manifest; |
46 } | 50 } |
47 | 51 |
| 52 PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) { |
| 53 PaymentInstrumentProto instrument_proto; |
| 54 if (!instrument_proto.ParseFromString(input)) |
| 55 return nullptr; |
| 56 |
| 57 PaymentInstrumentPtr instrument = PaymentInstrument::New(); |
| 58 instrument->name = instrument_proto.name(); |
| 59 for (const auto& method : instrument_proto.enabled_methods()) |
| 60 instrument->enabled_methods.push_back(method); |
| 61 instrument->stringified_capabilities = |
| 62 instrument_proto.stringified_capabilities(); |
| 63 |
| 64 return instrument; |
| 65 } |
| 66 |
48 } // namespace | 67 } // namespace |
49 | 68 |
50 PaymentAppDatabase::PaymentAppDatabase( | 69 PaymentAppDatabase::PaymentAppDatabase( |
51 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 70 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
52 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | 71 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { |
53 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
54 } | 73 } |
55 | 74 |
56 PaymentAppDatabase::~PaymentAppDatabase() { | 75 PaymentAppDatabase::~PaymentAppDatabase() { |
57 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 76 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
(...skipping 23 matching lines...) Expand all Loading... |
81 void PaymentAppDatabase::ReadAllManifests( | 100 void PaymentAppDatabase::ReadAllManifests( |
82 const ReadAllManifestsCallback& callback) { | 101 const ReadAllManifestsCallback& callback) { |
83 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
84 | 103 |
85 service_worker_context_->GetUserDataForAllRegistrations( | 104 service_worker_context_->GetUserDataForAllRegistrations( |
86 kPaymentAppManifestDataKey, | 105 kPaymentAppManifestDataKey, |
87 base::Bind(&PaymentAppDatabase::DidReadAllManifests, | 106 base::Bind(&PaymentAppDatabase::DidReadAllManifests, |
88 weak_ptr_factory_.GetWeakPtr(), callback)); | 107 weak_ptr_factory_.GetWeakPtr(), callback)); |
89 } | 108 } |
90 | 109 |
| 110 void PaymentAppDatabase::ReadPaymentInstrument( |
| 111 const GURL& scope, |
| 112 const std::string& instrumentKey, |
| 113 ReadPaymentInstrumentCallback callback) { |
| 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 115 |
| 116 service_worker_context_->FindReadyRegistrationForPattern( |
| 117 scope, |
| 118 base::Bind( |
| 119 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, |
| 120 weak_ptr_factory_.GetWeakPtr(), instrumentKey, |
| 121 base::Passed(std::move(callback)))); |
| 122 } |
| 123 |
| 124 void PaymentAppDatabase::WritePaymentInstrument( |
| 125 const GURL& scope, |
| 126 const std::string& instrumentKey, |
| 127 PaymentInstrumentPtr instrument, |
| 128 WritePaymentInstrumentCallback callback) { |
| 129 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 130 |
| 131 service_worker_context_->FindReadyRegistrationForPattern( |
| 132 scope, |
| 133 base::Bind( |
| 134 &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument, |
| 135 weak_ptr_factory_.GetWeakPtr(), instrumentKey, |
| 136 base::Passed(std::move(instrument)), |
| 137 base::Passed(std::move(callback)))); |
| 138 } |
| 139 |
91 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( | 140 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( |
92 payments::mojom::PaymentAppManifestPtr manifest, | 141 payments::mojom::PaymentAppManifestPtr manifest, |
93 const WriteManifestCallback& callback, | 142 const WriteManifestCallback& callback, |
94 ServiceWorkerStatusCode status, | 143 ServiceWorkerStatusCode status, |
95 scoped_refptr<ServiceWorkerRegistration> registration) { | 144 scoped_refptr<ServiceWorkerRegistration> registration) { |
96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 145 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
97 if (status != SERVICE_WORKER_OK) { | 146 if (status != SERVICE_WORKER_OK) { |
98 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); | 147 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); |
99 return; | 148 return; |
100 } | 149 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (!manifest) | 242 if (!manifest) |
194 continue; | 243 continue; |
195 | 244 |
196 manifests.push_back( | 245 manifests.push_back( |
197 ManifestWithID(item_of_raw_data.first, std::move(manifest))); | 246 ManifestWithID(item_of_raw_data.first, std::move(manifest))); |
198 } | 247 } |
199 | 248 |
200 callback.Run(std::move(manifests)); | 249 callback.Run(std::move(manifests)); |
201 } | 250 } |
202 | 251 |
| 252 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument( |
| 253 const std::string& instrumentKey, |
| 254 ReadPaymentInstrumentCallback callback, |
| 255 ServiceWorkerStatusCode status, |
| 256 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 257 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 258 if (status != SERVICE_WORKER_OK) { |
| 259 std::move(callback).Run(PaymentInstrument::New(), |
| 260 PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 261 return; |
| 262 } |
| 263 |
| 264 service_worker_context_->GetRegistrationUserData( |
| 265 registration->id(), {instrumentKey}, |
| 266 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument, |
| 267 weak_ptr_factory_.GetWeakPtr(), |
| 268 base::Passed(std::move(callback)))); |
| 269 } |
| 270 |
| 271 void PaymentAppDatabase::DidReadPaymentInstrument( |
| 272 ReadPaymentInstrumentCallback callback, |
| 273 const std::vector<std::string>& data, |
| 274 ServiceWorkerStatusCode status) { |
| 275 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 276 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
| 277 std::move(callback).Run(PaymentInstrument::New(), |
| 278 PaymentHandlerStatus::NOT_FOUND); |
| 279 return; |
| 280 } |
| 281 |
| 282 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); |
| 283 if (!instrument) { |
| 284 std::move(callback).Run(PaymentInstrument::New(), |
| 285 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
| 286 return; |
| 287 } |
| 288 |
| 289 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); |
| 290 } |
| 291 |
| 292 void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument( |
| 293 const std::string& instrumentKey, |
| 294 PaymentInstrumentPtr instrument, |
| 295 WritePaymentInstrumentCallback callback, |
| 296 ServiceWorkerStatusCode status, |
| 297 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 298 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 299 if (status != SERVICE_WORKER_OK) { |
| 300 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 301 return; |
| 302 } |
| 303 |
| 304 PaymentInstrumentProto instrument_proto; |
| 305 instrument_proto.set_name(instrument->name); |
| 306 for (const auto& method : instrument->enabled_methods) { |
| 307 instrument_proto.add_enabled_methods(method); |
| 308 } |
| 309 instrument_proto.set_stringified_capabilities( |
| 310 instrument->stringified_capabilities); |
| 311 |
| 312 std::string serialized; |
| 313 bool success = instrument_proto.SerializeToString(&serialized); |
| 314 DCHECK(success); |
| 315 |
| 316 service_worker_context_->StoreRegistrationUserData( |
| 317 registration->id(), registration->pattern().GetOrigin(), |
| 318 {{instrumentKey, serialized}}, |
| 319 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, |
| 320 weak_ptr_factory_.GetWeakPtr(), |
| 321 base::Passed(std::move(callback)))); |
| 322 } |
| 323 |
| 324 void PaymentAppDatabase::DidWritePaymentInstrument( |
| 325 WritePaymentInstrumentCallback callback, |
| 326 ServiceWorkerStatusCode status) { |
| 327 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 328 return std::move(callback).Run( |
| 329 status == SERVICE_WORKER_OK |
| 330 ? PaymentHandlerStatus::SUCCESS |
| 331 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
| 332 } |
| 333 |
203 } // namespace content | 334 } // namespace content |
OLD | NEW |