Chromium Code Reviews| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 DeserializePaymentInstrumentForMojo( |
|
nasko
2017/05/11 21:45:39
It is a bit strange to have the same type name def
zino
2017/05/12 20:59:21
Done.
| |
| 81 const std::string& input) { | |
| 81 PaymentInstrumentProto instrument_proto; | 82 PaymentInstrumentProto 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 = payments::mojom::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<content::PaymentInstrument> DeserializePaymentInstrument( | |
| 97 const std::string& input) { | |
| 98 PaymentInstrumentProto instrument_proto; | |
| 99 if (!instrument_proto.ParseFromString(input)) | |
| 100 return std::unique_ptr<content::PaymentInstrument>(); | |
| 101 | |
| 102 std::unique_ptr<content::PaymentInstrument> instrument = | |
| 103 base::MakeUnique<content::PaymentInstrument>(); | |
| 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<PaymentInstrument> instrument = | |
| 376 DeserializePaymentInstrument(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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 : PaymentHandlerStatus::NOT_FOUND); | 432 : PaymentHandlerStatus::NOT_FOUND); |
| 379 } | 433 } |
| 380 | 434 |
| 381 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument( | 435 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument( |
| 382 const std::string& instrument_key, | 436 const std::string& instrument_key, |
| 383 ReadPaymentInstrumentCallback callback, | 437 ReadPaymentInstrumentCallback callback, |
| 384 ServiceWorkerStatusCode status, | 438 ServiceWorkerStatusCode status, |
| 385 scoped_refptr<ServiceWorkerRegistration> registration) { | 439 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 386 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 440 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 387 if (status != SERVICE_WORKER_OK) { | 441 if (status != SERVICE_WORKER_OK) { |
| 388 std::move(callback).Run(PaymentInstrument::New(), | 442 std::move(callback).Run(payments::mojom::PaymentInstrument::New(), |
| 389 PaymentHandlerStatus::NO_ACTIVE_WORKER); | 443 PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 390 return; | 444 return; |
| 391 } | 445 } |
| 392 | 446 |
| 393 service_worker_context_->GetRegistrationUserData( | 447 service_worker_context_->GetRegistrationUserData( |
| 394 registration->id(), {CreatePaymentInstrumentKey(instrument_key)}, | 448 registration->id(), {CreatePaymentInstrumentKey(instrument_key)}, |
| 395 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument, | 449 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument, |
| 396 weak_ptr_factory_.GetWeakPtr(), | 450 weak_ptr_factory_.GetWeakPtr(), |
| 397 base::Passed(std::move(callback)))); | 451 base::Passed(std::move(callback)))); |
| 398 } | 452 } |
| 399 | 453 |
| 400 void PaymentAppDatabase::DidReadPaymentInstrument( | 454 void PaymentAppDatabase::DidReadPaymentInstrument( |
| 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(payments::mojom::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 = |
| 466 DeserializePaymentInstrumentForMojo(data[0]); | |
| 412 if (!instrument) { | 467 if (!instrument) { |
| 413 std::move(callback).Run(PaymentInstrument::New(), | 468 std::move(callback).Run(payments::mojom::PaymentInstrument::New(), |
| 414 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 469 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
| 415 return; | 470 return; |
| 416 } | 471 } |
| 417 | 472 |
| 418 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); | 473 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); |
| 419 } | 474 } |
| 420 | 475 |
| 421 void PaymentAppDatabase::DidFindRegistrationToGetKeys( | 476 void PaymentAppDatabase::DidFindRegistrationToGetKeys( |
| 422 KeysOfPaymentInstrumentsCallback callback, | 477 KeysOfPaymentInstrumentsCallback callback, |
| 423 ServiceWorkerStatusCode status, | 478 ServiceWorkerStatusCode status, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 WritePaymentInstrumentCallback callback, | 549 WritePaymentInstrumentCallback callback, |
| 495 ServiceWorkerStatusCode status, | 550 ServiceWorkerStatusCode status, |
| 496 scoped_refptr<ServiceWorkerRegistration> registration) { | 551 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 497 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 552 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 498 if (status != SERVICE_WORKER_OK) { | 553 if (status != SERVICE_WORKER_OK) { |
| 499 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 554 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 500 return; | 555 return; |
| 501 } | 556 } |
| 502 | 557 |
| 503 PaymentInstrumentProto instrument_proto; | 558 PaymentInstrumentProto instrument_proto; |
| 559 instrument_proto.set_registration_id(registration->id()); | |
| 560 instrument_proto.set_instrument_key(instrument_key); | |
| 561 instrument_proto.set_origin(registration->pattern().GetOrigin().spec()); | |
| 504 instrument_proto.set_name(instrument->name); | 562 instrument_proto.set_name(instrument->name); |
| 505 for (const auto& method : instrument->enabled_methods) { | 563 for (const auto& method : instrument->enabled_methods) { |
| 506 instrument_proto.add_enabled_methods(method); | 564 instrument_proto.add_enabled_methods(method); |
| 507 } | 565 } |
| 508 instrument_proto.set_stringified_capabilities( | 566 instrument_proto.set_stringified_capabilities( |
| 509 instrument->stringified_capabilities); | 567 instrument->stringified_capabilities); |
| 510 | 568 |
| 511 std::string serialized_instrument; | 569 std::string serialized_instrument; |
| 512 bool success = instrument_proto.SerializeToString(&serialized_instrument); | 570 bool success = instrument_proto.SerializeToString(&serialized_instrument); |
| 513 DCHECK(success); | 571 DCHECK(success); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 void PaymentAppDatabase::DidClearPaymentInstruments( | 645 void PaymentAppDatabase::DidClearPaymentInstruments( |
| 588 ClearPaymentInstrumentsCallback callback, | 646 ClearPaymentInstrumentsCallback callback, |
| 589 ServiceWorkerStatusCode status) { | 647 ServiceWorkerStatusCode status) { |
| 590 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 648 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 591 return std::move(callback).Run(status == SERVICE_WORKER_OK | 649 return std::move(callback).Run(status == SERVICE_WORKER_OK |
| 592 ? PaymentHandlerStatus::SUCCESS | 650 ? PaymentHandlerStatus::SUCCESS |
| 593 : PaymentHandlerStatus::NOT_FOUND); | 651 : PaymentHandlerStatus::NOT_FOUND); |
| 594 } | 652 } |
| 595 | 653 |
| 596 } // namespace content | 654 } // namespace content |
| OLD | NEW |