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 <map> | |
8 #include <utility> | 7 #include <utility> |
9 | 8 |
10 #include "base/bind.h" | 9 #include "base/bind.h" |
11 #include "base/optional.h" | 10 #include "base/optional.h" |
12 #include "base/time/time.h" | |
13 #include "content/browser/payments/payment_app.pb.h" | 11 #include "content/browser/payments/payment_app.pb.h" |
14 #include "content/browser/payments/payment_app_context_impl.h" | 12 #include "content/browser/payments/payment_app_context_impl.h" |
15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
16 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
17 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
18 | 16 |
19 namespace content { | 17 namespace content { |
20 namespace { | 18 namespace { |
21 | 19 |
22 using ::payments::mojom::PaymentHandlerStatus; | 20 using ::payments::mojom::PaymentHandlerStatus; |
23 using ::payments::mojom::PaymentInstrument; | 21 using ::payments::mojom::PaymentInstrument; |
24 using ::payments::mojom::PaymentInstrumentPtr; | 22 using ::payments::mojom::PaymentInstrumentPtr; |
25 | 23 |
26 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; | 24 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; |
27 const char kPaymentInstrumentPrefix[] = "PaymentInstrument:"; | 25 const char kPaymentInstrumentKeyPrefix[] = "PaymentInstrument:"; |
28 const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:"; | |
29 | 26 |
30 std::string CreatePaymentInstrumentKey(const std::string& instrument_key) { | 27 std::string CreatePaymentInstrumentKey(const std::string& instrument_key) { |
31 return kPaymentInstrumentPrefix + instrument_key; | 28 return kPaymentInstrumentKeyPrefix + instrument_key; |
32 } | |
33 | |
34 std::string CreatePaymentInstrumentKeyInfoKey( | |
35 const std::string& instrument_key) { | |
36 return kPaymentInstrumentKeyInfoPrefix + instrument_key; | |
37 } | 29 } |
38 | 30 |
39 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( | 31 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( |
40 const std::string& input) { | 32 const std::string& input) { |
41 PaymentAppManifestProto manifest_proto; | 33 PaymentAppManifestProto manifest_proto; |
42 if (!manifest_proto.ParseFromString(input)) | 34 if (!manifest_proto.ParseFromString(input)) |
43 return nullptr; | 35 return nullptr; |
44 | 36 |
45 payments::mojom::PaymentAppManifestPtr manifest = | 37 payments::mojom::PaymentAppManifestPtr manifest = |
46 payments::mojom::PaymentAppManifest::New(); | 38 payments::mojom::PaymentAppManifest::New(); |
47 manifest->name = manifest_proto.name(); | 39 manifest->name = manifest_proto.name(); |
48 if (manifest_proto.has_icon()) | 40 if (manifest_proto.has_icon()) |
49 manifest->icon = manifest_proto.icon(); | 41 manifest->icon = manifest_proto.icon(); |
50 for (const auto& option_proto : manifest_proto.options()) { | 42 for (const auto& option_proto : manifest_proto.options()) { |
51 payments::mojom::PaymentAppOptionPtr option = | 43 payments::mojom::PaymentAppOptionPtr option = |
52 payments::mojom::PaymentAppOption::New(); | 44 payments::mojom::PaymentAppOption::New(); |
53 option->name = option_proto.name(); | 45 option->name = option_proto.name(); |
54 if (option_proto.has_icon()) | 46 if (option_proto.has_icon()) |
55 option->icon = option_proto.icon(); | 47 option->icon = option_proto.icon(); |
56 option->id = option_proto.id(); | 48 option->id = option_proto.id(); |
57 for (const auto& method : option_proto.enabled_methods()) | 49 for (const auto& method : option_proto.enabled_methods()) |
58 option->enabled_methods.push_back(method); | 50 option->enabled_methods.push_back(method); |
59 manifest->options.push_back(std::move(option)); | 51 manifest->options.push_back(std::move(option)); |
60 } | 52 } |
61 | 53 |
62 return manifest; | 54 return manifest; |
63 } | 55 } |
64 | 56 |
65 std::map<uint64_t, std::string> DeserializePaymentInstrumentKeyInfo( | |
66 const std::vector<std::string>& inputs) { | |
67 std::map<uint64_t, std::string> key_info; | |
68 for (const auto& input : inputs) { | |
69 PaymentInstrumentKeyInfoProto key_info_proto; | |
70 if (!key_info_proto.ParseFromString(input)) | |
71 return std::map<uint64_t, std::string>(); | |
72 | |
73 key_info.insert(std::pair<uint64_t, std::string>( | |
74 key_info_proto.insertion_order(), key_info_proto.key())); | |
75 } | |
76 | |
77 return key_info; | |
78 } | |
79 | |
80 PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) { | 57 PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) { |
81 PaymentInstrumentProto instrument_proto; | 58 PaymentInstrumentProto instrument_proto; |
82 if (!instrument_proto.ParseFromString(input)) | 59 if (!instrument_proto.ParseFromString(input)) |
83 return nullptr; | 60 return nullptr; |
84 | 61 |
85 PaymentInstrumentPtr instrument = PaymentInstrument::New(); | 62 PaymentInstrumentPtr instrument = PaymentInstrument::New(); |
86 instrument->name = instrument_proto.name(); | 63 instrument->name = instrument_proto.name(); |
87 for (const auto& method : instrument_proto.enabled_methods()) | 64 for (const auto& method : instrument_proto.enabled_methods()) |
88 instrument->enabled_methods.push_back(method); | 65 instrument->enabled_methods.push_back(method); |
89 instrument->stringified_capabilities = | 66 instrument->stringified_capabilities = |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 133 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
157 | 134 |
158 service_worker_context_->FindReadyRegistrationForPattern( | 135 service_worker_context_->FindReadyRegistrationForPattern( |
159 scope, | 136 scope, |
160 base::Bind( | 137 base::Bind( |
161 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, | 138 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, |
162 weak_ptr_factory_.GetWeakPtr(), instrument_key, | 139 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
163 base::Passed(std::move(callback)))); | 140 base::Passed(std::move(callback)))); |
164 } | 141 } |
165 | 142 |
166 void PaymentAppDatabase::KeysOfPaymentInstruments( | |
167 const GURL& scope, | |
168 KeysOfPaymentInstrumentsCallback callback) { | |
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
170 | |
171 service_worker_context_->FindReadyRegistrationForPattern( | |
172 scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToGetKeys, | |
173 weak_ptr_factory_.GetWeakPtr(), | |
174 base::Passed(std::move(callback)))); | |
175 } | |
176 | |
177 void PaymentAppDatabase::HasPaymentInstrument( | 143 void PaymentAppDatabase::HasPaymentInstrument( |
178 const GURL& scope, | 144 const GURL& scope, |
179 const std::string& instrument_key, | 145 const std::string& instrument_key, |
180 HasPaymentInstrumentCallback callback) { | 146 HasPaymentInstrumentCallback callback) { |
181 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
182 | 148 |
183 service_worker_context_->FindReadyRegistrationForPattern( | 149 service_worker_context_->FindReadyRegistrationForPattern( |
184 scope, | 150 scope, |
185 base::Bind(&PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument, | 151 base::Bind(&PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument, |
186 weak_ptr_factory_.GetWeakPtr(), instrument_key, | 152 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 DeletePaymentInstrumentCallback callback, | 305 DeletePaymentInstrumentCallback callback, |
340 const std::vector<std::string>& data, | 306 const std::vector<std::string>& data, |
341 ServiceWorkerStatusCode status) { | 307 ServiceWorkerStatusCode status) { |
342 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 308 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
343 if (status != SERVICE_WORKER_OK || data.size() != 1) { | 309 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
344 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); | 310 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); |
345 return; | 311 return; |
346 } | 312 } |
347 | 313 |
348 service_worker_context_->ClearRegistrationUserData( | 314 service_worker_context_->ClearRegistrationUserData( |
349 registration_id, | 315 registration_id, {CreatePaymentInstrumentKey(instrument_key)}, |
350 {CreatePaymentInstrumentKey(instrument_key), | |
351 CreatePaymentInstrumentKeyInfoKey(instrument_key)}, | |
352 base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument, | 316 base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument, |
353 weak_ptr_factory_.GetWeakPtr(), | 317 weak_ptr_factory_.GetWeakPtr(), |
354 base::Passed(std::move(callback)))); | 318 base::Passed(std::move(callback)))); |
355 } | 319 } |
356 | 320 |
357 void PaymentAppDatabase::DidDeletePaymentInstrument( | 321 void PaymentAppDatabase::DidDeletePaymentInstrument( |
358 DeletePaymentInstrumentCallback callback, | 322 DeletePaymentInstrumentCallback callback, |
359 ServiceWorkerStatusCode status) { | 323 ServiceWorkerStatusCode status) { |
360 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 324 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
361 return std::move(callback).Run(status == SERVICE_WORKER_OK | 325 return std::move(callback).Run(status == SERVICE_WORKER_OK |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); | 360 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); |
397 if (!instrument) { | 361 if (!instrument) { |
398 std::move(callback).Run(PaymentInstrument::New(), | 362 std::move(callback).Run(PaymentInstrument::New(), |
399 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 363 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
400 return; | 364 return; |
401 } | 365 } |
402 | 366 |
403 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); | 367 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); |
404 } | 368 } |
405 | 369 |
406 void PaymentAppDatabase::DidFindRegistrationToGetKeys( | |
407 KeysOfPaymentInstrumentsCallback callback, | |
408 ServiceWorkerStatusCode status, | |
409 scoped_refptr<ServiceWorkerRegistration> registration) { | |
410 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
411 if (status != SERVICE_WORKER_OK) { | |
412 std::move(callback).Run(std::vector<std::string>(), | |
413 PaymentHandlerStatus::NO_ACTIVE_WORKER); | |
414 return; | |
415 } | |
416 | |
417 service_worker_context_->GetRegistrationUserDataByKeyPrefix( | |
418 registration->id(), {kPaymentInstrumentKeyInfoPrefix}, | |
419 base::Bind(&PaymentAppDatabase::DidGetKeysOfPaymentInstruments, | |
420 weak_ptr_factory_.GetWeakPtr(), | |
421 base::Passed(std::move(callback)))); | |
422 } | |
423 | |
424 void PaymentAppDatabase::DidGetKeysOfPaymentInstruments( | |
425 KeysOfPaymentInstrumentsCallback callback, | |
426 const std::vector<std::string>& data, | |
427 ServiceWorkerStatusCode status) { | |
428 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
429 if (status != SERVICE_WORKER_OK) { | |
430 std::move(callback).Run(std::vector<std::string>(), | |
431 PaymentHandlerStatus::NOT_FOUND); | |
432 return; | |
433 } | |
434 | |
435 std::vector<std::string> keys; | |
436 for (const auto& key_info : DeserializePaymentInstrumentKeyInfo(data)) { | |
437 keys.push_back(key_info.second); | |
438 } | |
439 | |
440 std::move(callback).Run(keys, PaymentHandlerStatus::SUCCESS); | |
441 } | |
442 | |
443 void PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument( | 370 void PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument( |
444 const std::string& instrument_key, | 371 const std::string& instrument_key, |
445 HasPaymentInstrumentCallback callback, | 372 HasPaymentInstrumentCallback callback, |
446 ServiceWorkerStatusCode status, | 373 ServiceWorkerStatusCode status, |
447 scoped_refptr<ServiceWorkerRegistration> registration) { | 374 scoped_refptr<ServiceWorkerRegistration> registration) { |
448 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 375 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
449 if (status != SERVICE_WORKER_OK) { | 376 if (status != SERVICE_WORKER_OK) { |
450 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 377 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
451 return; | 378 return; |
452 } | 379 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 } | 413 } |
487 | 414 |
488 PaymentInstrumentProto instrument_proto; | 415 PaymentInstrumentProto instrument_proto; |
489 instrument_proto.set_name(instrument->name); | 416 instrument_proto.set_name(instrument->name); |
490 for (const auto& method : instrument->enabled_methods) { | 417 for (const auto& method : instrument->enabled_methods) { |
491 instrument_proto.add_enabled_methods(method); | 418 instrument_proto.add_enabled_methods(method); |
492 } | 419 } |
493 instrument_proto.set_stringified_capabilities( | 420 instrument_proto.set_stringified_capabilities( |
494 instrument->stringified_capabilities); | 421 instrument->stringified_capabilities); |
495 | 422 |
496 std::string serialized_instrument; | 423 std::string serialized; |
497 DCHECK(instrument_proto.SerializeToString(&serialized_instrument)); | 424 bool success = instrument_proto.SerializeToString(&serialized); |
498 | 425 DCHECK(success); |
499 PaymentInstrumentKeyInfoProto key_info_proto; | |
500 key_info_proto.set_key(instrument_key); | |
501 key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue()); | |
502 | |
503 std::string serialized_key_info; | |
504 DCHECK(key_info_proto.SerializeToString(&serialized_key_info)); | |
505 | 426 |
506 service_worker_context_->StoreRegistrationUserData( | 427 service_worker_context_->StoreRegistrationUserData( |
507 registration->id(), registration->pattern().GetOrigin(), | 428 registration->id(), registration->pattern().GetOrigin(), |
508 {{CreatePaymentInstrumentKey(instrument_key), serialized_instrument}, | 429 {{CreatePaymentInstrumentKey(instrument_key), serialized}}, |
509 {CreatePaymentInstrumentKeyInfoKey(instrument_key), | |
510 serialized_key_info}}, | |
511 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, | 430 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, |
512 weak_ptr_factory_.GetWeakPtr(), | 431 weak_ptr_factory_.GetWeakPtr(), |
513 base::Passed(std::move(callback)))); | 432 base::Passed(std::move(callback)))); |
514 } | 433 } |
515 | 434 |
516 void PaymentAppDatabase::DidWritePaymentInstrument( | 435 void PaymentAppDatabase::DidWritePaymentInstrument( |
517 WritePaymentInstrumentCallback callback, | 436 WritePaymentInstrumentCallback callback, |
518 ServiceWorkerStatusCode status) { | 437 ServiceWorkerStatusCode status) { |
519 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 438 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
520 return std::move(callback).Run( | 439 return std::move(callback).Run( |
521 status == SERVICE_WORKER_OK | 440 status == SERVICE_WORKER_OK |
522 ? PaymentHandlerStatus::SUCCESS | 441 ? PaymentHandlerStatus::SUCCESS |
523 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 442 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
524 } | 443 } |
525 | 444 |
526 } // namespace content | 445 } // namespace content |
OLD | NEW |