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> | 7 #include <map> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/optional.h" | 12 #include "base/optional.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "content/browser/payments/payment_app.pb.h" | 14 #include "content/browser/payments/payment_app.pb.h" |
14 #include "content/browser/payments/payment_app_context_impl.h" | 15 #include "content/browser/payments/payment_app_context_impl.h" |
15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
16 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 namespace { | 21 namespace { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 option->icon = option_proto.icon(); | 56 option->icon = option_proto.icon(); |
56 option->id = option_proto.id(); | 57 option->id = option_proto.id(); |
57 for (const auto& method : option_proto.enabled_methods()) | 58 for (const auto& method : option_proto.enabled_methods()) |
58 option->enabled_methods.push_back(method); | 59 option->enabled_methods.push_back(method); |
59 manifest->options.push_back(std::move(option)); | 60 manifest->options.push_back(std::move(option)); |
60 } | 61 } |
61 | 62 |
62 return manifest; | 63 return manifest; |
63 } | 64 } |
64 | 65 |
65 std::map<uint64_t, std::string> DeserializePaymentInstrumentKeyInfo( | 66 std::map<uint64_t, std::string> ToStoredPaymentInstrumentKeyInfos( |
66 const std::vector<std::string>& inputs) { | 67 const std::vector<std::string>& inputs) { |
67 std::map<uint64_t, std::string> key_info; | 68 std::map<uint64_t, std::string> key_info; |
68 for (const auto& input : inputs) { | 69 for (const auto& input : inputs) { |
69 PaymentInstrumentKeyInfoProto key_info_proto; | 70 StoredPaymentInstrumentKeyInfoProto key_info_proto; |
70 if (!key_info_proto.ParseFromString(input)) | 71 if (!key_info_proto.ParseFromString(input)) |
71 return std::map<uint64_t, std::string>(); | 72 return std::map<uint64_t, std::string>(); |
72 | 73 |
73 key_info.insert(std::pair<uint64_t, std::string>( | 74 key_info.insert(std::pair<uint64_t, std::string>( |
74 key_info_proto.insertion_order(), key_info_proto.key())); | 75 key_info_proto.insertion_order(), key_info_proto.key())); |
75 } | 76 } |
76 | 77 |
77 return key_info; | 78 return key_info; |
78 } | 79 } |
79 | 80 |
80 PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) { | 81 PaymentInstrumentPtr ToPaymentInstrumentForMojo(const std::string& input) { |
81 PaymentInstrumentProto instrument_proto; | 82 StoredPaymentInstrumentProto instrument_proto; |
82 if (!instrument_proto.ParseFromString(input)) | 83 if (!instrument_proto.ParseFromString(input)) |
83 return nullptr; | 84 return nullptr; |
84 | 85 |
85 PaymentInstrumentPtr instrument = PaymentInstrument::New(); | 86 PaymentInstrumentPtr instrument = PaymentInstrument::New(); |
86 instrument->name = instrument_proto.name(); | 87 instrument->name = instrument_proto.name(); |
87 for (const auto& method : instrument_proto.enabled_methods()) | 88 for (const auto& method : instrument_proto.enabled_methods()) |
88 instrument->enabled_methods.push_back(method); | 89 instrument->enabled_methods.push_back(method); |
89 instrument->stringified_capabilities = | 90 instrument->stringified_capabilities = |
90 instrument_proto.stringified_capabilities(); | 91 instrument_proto.stringified_capabilities(); |
91 | 92 |
92 return instrument; | 93 return instrument; |
93 } | 94 } |
94 | 95 |
| 96 std::unique_ptr<StoredPaymentInstrument> ToStoredPaymentInstrument( |
| 97 const std::string& input) { |
| 98 StoredPaymentInstrumentProto instrument_proto; |
| 99 if (!instrument_proto.ParseFromString(input)) |
| 100 return std::unique_ptr<StoredPaymentInstrument>(); |
| 101 |
| 102 std::unique_ptr<StoredPaymentInstrument> instrument = |
| 103 base::MakeUnique<StoredPaymentInstrument>(); |
| 104 instrument->registration_id = instrument_proto.registration_id(); |
| 105 instrument->instrument_key = instrument_proto.instrument_key(); |
| 106 instrument->origin = GURL(instrument_proto.origin()); |
| 107 instrument->name = instrument_proto.name(); |
| 108 for (const auto& method : instrument_proto.enabled_methods()) |
| 109 instrument->enabled_methods.push_back(method); |
| 110 |
| 111 return instrument; |
| 112 } |
| 113 |
95 } // namespace | 114 } // namespace |
96 | 115 |
97 PaymentAppDatabase::PaymentAppDatabase( | 116 PaymentAppDatabase::PaymentAppDatabase( |
98 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 117 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
99 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | 118 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { |
100 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
101 } | 120 } |
102 | 121 |
103 PaymentAppDatabase::~PaymentAppDatabase() { | 122 PaymentAppDatabase::~PaymentAppDatabase() { |
104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
(...skipping 23 matching lines...) Expand all Loading... |
128 | 147 |
129 void PaymentAppDatabase::ReadAllManifests(ReadAllManifestsCallback callback) { | 148 void PaymentAppDatabase::ReadAllManifests(ReadAllManifestsCallback callback) { |
130 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 149 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
131 | 150 |
132 service_worker_context_->GetUserDataForAllRegistrations( | 151 service_worker_context_->GetUserDataForAllRegistrations( |
133 kPaymentAppManifestDataKey, | 152 kPaymentAppManifestDataKey, |
134 base::Bind(&PaymentAppDatabase::DidReadAllManifests, | 153 base::Bind(&PaymentAppDatabase::DidReadAllManifests, |
135 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | 154 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); |
136 } | 155 } |
137 | 156 |
| 157 void PaymentAppDatabase::ReadAllPaymentApps( |
| 158 ReadAllPaymentAppsCallback callback) { |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 160 |
| 161 service_worker_context_->GetUserDataForAllRegistrationsByKeyPrefix( |
| 162 kPaymentInstrumentPrefix, |
| 163 base::Bind(&PaymentAppDatabase::DidReadAllPaymentApps, |
| 164 weak_ptr_factory_.GetWeakPtr(), |
| 165 base::Passed(std::move(callback)))); |
| 166 } |
| 167 |
138 void PaymentAppDatabase::DeletePaymentInstrument( | 168 void PaymentAppDatabase::DeletePaymentInstrument( |
139 const GURL& scope, | 169 const GURL& scope, |
140 const std::string& instrument_key, | 170 const std::string& instrument_key, |
141 DeletePaymentInstrumentCallback callback) { | 171 DeletePaymentInstrumentCallback callback) { |
142 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
143 | 173 |
144 service_worker_context_->FindReadyRegistrationForPattern( | 174 service_worker_context_->FindReadyRegistrationForPattern( |
145 scope, | 175 scope, |
146 base::Bind( | 176 base::Bind( |
147 &PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument, | 177 &PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument, |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 if (!manifest) | 353 if (!manifest) |
324 continue; | 354 continue; |
325 | 355 |
326 manifests.push_back( | 356 manifests.push_back( |
327 ManifestWithID(item_of_raw_data.first, std::move(manifest))); | 357 ManifestWithID(item_of_raw_data.first, std::move(manifest))); |
328 } | 358 } |
329 | 359 |
330 std::move(callback).Run(std::move(manifests)); | 360 std::move(callback).Run(std::move(manifests)); |
331 } | 361 } |
332 | 362 |
| 363 void PaymentAppDatabase::DidReadAllPaymentApps( |
| 364 ReadAllPaymentAppsCallback callback, |
| 365 const std::vector<std::pair<int64_t, std::string>>& raw_data, |
| 366 ServiceWorkerStatusCode status) { |
| 367 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 368 if (status != SERVICE_WORKER_OK) { |
| 369 std::move(callback).Run(PaymentApps()); |
| 370 return; |
| 371 } |
| 372 |
| 373 PaymentApps apps; |
| 374 for (const auto& item_of_raw_data : raw_data) { |
| 375 std::unique_ptr<StoredPaymentInstrument> instrument = |
| 376 ToStoredPaymentInstrument(item_of_raw_data.second); |
| 377 if (!instrument) |
| 378 continue; |
| 379 if (!base::ContainsKey(apps, instrument->origin)) |
| 380 apps.insert(std::make_pair(instrument->origin, Instruments())); |
| 381 apps[instrument->origin].push_back(std::move(instrument)); |
| 382 } |
| 383 |
| 384 std::move(callback).Run(std::move(apps)); |
| 385 } |
| 386 |
333 void PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument( | 387 void PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument( |
334 const std::string& instrument_key, | 388 const std::string& instrument_key, |
335 DeletePaymentInstrumentCallback callback, | 389 DeletePaymentInstrumentCallback callback, |
336 ServiceWorkerStatusCode status, | 390 ServiceWorkerStatusCode status, |
337 scoped_refptr<ServiceWorkerRegistration> registration) { | 391 scoped_refptr<ServiceWorkerRegistration> registration) { |
338 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 392 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
339 if (status != SERVICE_WORKER_OK) { | 393 if (status != SERVICE_WORKER_OK) { |
340 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 394 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
341 return; | 395 return; |
342 } | 396 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 ReadPaymentInstrumentCallback callback, | 455 ReadPaymentInstrumentCallback callback, |
402 const std::vector<std::string>& data, | 456 const std::vector<std::string>& data, |
403 ServiceWorkerStatusCode status) { | 457 ServiceWorkerStatusCode status) { |
404 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 458 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
405 if (status != SERVICE_WORKER_OK || data.size() != 1) { | 459 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
406 std::move(callback).Run(PaymentInstrument::New(), | 460 std::move(callback).Run(PaymentInstrument::New(), |
407 PaymentHandlerStatus::NOT_FOUND); | 461 PaymentHandlerStatus::NOT_FOUND); |
408 return; | 462 return; |
409 } | 463 } |
410 | 464 |
411 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); | 465 PaymentInstrumentPtr instrument = ToPaymentInstrumentForMojo(data[0]); |
412 if (!instrument) { | 466 if (!instrument) { |
413 std::move(callback).Run(PaymentInstrument::New(), | 467 std::move(callback).Run(PaymentInstrument::New(), |
414 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 468 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
415 return; | 469 return; |
416 } | 470 } |
417 | 471 |
418 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); | 472 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); |
419 } | 473 } |
420 | 474 |
421 void PaymentAppDatabase::DidFindRegistrationToGetKeys( | 475 void PaymentAppDatabase::DidFindRegistrationToGetKeys( |
(...skipping 19 matching lines...) Expand all Loading... |
441 const std::vector<std::string>& data, | 495 const std::vector<std::string>& data, |
442 ServiceWorkerStatusCode status) { | 496 ServiceWorkerStatusCode status) { |
443 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 497 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
444 if (status != SERVICE_WORKER_OK) { | 498 if (status != SERVICE_WORKER_OK) { |
445 std::move(callback).Run(std::vector<std::string>(), | 499 std::move(callback).Run(std::vector<std::string>(), |
446 PaymentHandlerStatus::NOT_FOUND); | 500 PaymentHandlerStatus::NOT_FOUND); |
447 return; | 501 return; |
448 } | 502 } |
449 | 503 |
450 std::vector<std::string> keys; | 504 std::vector<std::string> keys; |
451 for (const auto& key_info : DeserializePaymentInstrumentKeyInfo(data)) { | 505 for (const auto& key_info : ToStoredPaymentInstrumentKeyInfos(data)) { |
452 keys.push_back(key_info.second); | 506 keys.push_back(key_info.second); |
453 } | 507 } |
454 | 508 |
455 std::move(callback).Run(keys, PaymentHandlerStatus::SUCCESS); | 509 std::move(callback).Run(keys, PaymentHandlerStatus::SUCCESS); |
456 } | 510 } |
457 | 511 |
458 void PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument( | 512 void PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument( |
459 const std::string& instrument_key, | 513 const std::string& instrument_key, |
460 HasPaymentInstrumentCallback callback, | 514 HasPaymentInstrumentCallback callback, |
461 ServiceWorkerStatusCode status, | 515 ServiceWorkerStatusCode status, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 PaymentInstrumentPtr instrument, | 547 PaymentInstrumentPtr instrument, |
494 WritePaymentInstrumentCallback callback, | 548 WritePaymentInstrumentCallback callback, |
495 ServiceWorkerStatusCode status, | 549 ServiceWorkerStatusCode status, |
496 scoped_refptr<ServiceWorkerRegistration> registration) { | 550 scoped_refptr<ServiceWorkerRegistration> registration) { |
497 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 551 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
498 if (status != SERVICE_WORKER_OK) { | 552 if (status != SERVICE_WORKER_OK) { |
499 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 553 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
500 return; | 554 return; |
501 } | 555 } |
502 | 556 |
503 PaymentInstrumentProto instrument_proto; | 557 StoredPaymentInstrumentProto instrument_proto; |
| 558 instrument_proto.set_registration_id(registration->id()); |
| 559 instrument_proto.set_instrument_key(instrument_key); |
| 560 instrument_proto.set_origin(registration->pattern().GetOrigin().spec()); |
504 instrument_proto.set_name(instrument->name); | 561 instrument_proto.set_name(instrument->name); |
505 for (const auto& method : instrument->enabled_methods) { | 562 for (const auto& method : instrument->enabled_methods) { |
506 instrument_proto.add_enabled_methods(method); | 563 instrument_proto.add_enabled_methods(method); |
507 } | 564 } |
508 instrument_proto.set_stringified_capabilities( | 565 instrument_proto.set_stringified_capabilities( |
509 instrument->stringified_capabilities); | 566 instrument->stringified_capabilities); |
510 | 567 |
511 std::string serialized_instrument; | 568 std::string serialized_instrument; |
512 bool success = instrument_proto.SerializeToString(&serialized_instrument); | 569 bool success = instrument_proto.SerializeToString(&serialized_instrument); |
513 DCHECK(success); | 570 DCHECK(success); |
514 | 571 |
515 PaymentInstrumentKeyInfoProto key_info_proto; | 572 StoredPaymentInstrumentKeyInfoProto key_info_proto; |
516 key_info_proto.set_key(instrument_key); | 573 key_info_proto.set_key(instrument_key); |
517 key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue()); | 574 key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue()); |
518 | 575 |
519 std::string serialized_key_info; | 576 std::string serialized_key_info; |
520 success = key_info_proto.SerializeToString(&serialized_key_info); | 577 success = key_info_proto.SerializeToString(&serialized_key_info); |
521 DCHECK(success); | 578 DCHECK(success); |
522 | 579 |
523 service_worker_context_->StoreRegistrationUserData( | 580 service_worker_context_->StoreRegistrationUserData( |
524 registration->id(), registration->pattern().GetOrigin(), | 581 registration->id(), registration->pattern().GetOrigin(), |
525 {{CreatePaymentInstrumentKey(instrument_key), serialized_instrument}, | 582 {{CreatePaymentInstrumentKey(instrument_key), serialized_instrument}, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 void PaymentAppDatabase::DidClearPaymentInstruments( | 644 void PaymentAppDatabase::DidClearPaymentInstruments( |
588 ClearPaymentInstrumentsCallback callback, | 645 ClearPaymentInstrumentsCallback callback, |
589 ServiceWorkerStatusCode status) { | 646 ServiceWorkerStatusCode status) { |
590 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 647 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
591 return std::move(callback).Run(status == SERVICE_WORKER_OK | 648 return std::move(callback).Run(status == SERVICE_WORKER_OK |
592 ? PaymentHandlerStatus::SUCCESS | 649 ? PaymentHandlerStatus::SUCCESS |
593 : PaymentHandlerStatus::NOT_FOUND); | 650 : PaymentHandlerStatus::NOT_FOUND); |
594 } | 651 } |
595 | 652 |
596 } // namespace content | 653 } // namespace content |
OLD | NEW |