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