| 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/base64.h" |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/optional.h" | 13 #include "base/optional.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "content/browser/payments/payment_app.pb.h" | 15 #include "content/browser/payments/payment_app.pb.h" |
| 15 #include "content/browser/payments/payment_app_context_impl.h" | 16 #include "content/browser/payments/payment_app_context_impl.h" |
| 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 17 #include "content/browser/service_worker/service_worker_registration.h" | 18 #include "content/browser/service_worker/service_worker_registration.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "ui/gfx/image/image.h" |
| 19 | 22 |
| 20 namespace content { | 23 namespace content { |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 using ::payments::mojom::PaymentHandlerStatus; | 26 using ::payments::mojom::PaymentHandlerStatus; |
| 24 using ::payments::mojom::PaymentInstrument; | 27 using ::payments::mojom::PaymentInstrument; |
| 25 using ::payments::mojom::PaymentInstrumentPtr; | 28 using ::payments::mojom::PaymentInstrumentPtr; |
| 26 | 29 |
| 27 const char kPaymentInstrumentPrefix[] = "PaymentInstrument:"; | 30 const char kPaymentInstrumentPrefix[] = "PaymentInstrument:"; |
| 28 const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:"; | 31 const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:"; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 51 return key_info; | 54 return key_info; |
| 52 } | 55 } |
| 53 | 56 |
| 54 PaymentInstrumentPtr ToPaymentInstrumentForMojo(const std::string& input) { | 57 PaymentInstrumentPtr ToPaymentInstrumentForMojo(const std::string& input) { |
| 55 StoredPaymentInstrumentProto instrument_proto; | 58 StoredPaymentInstrumentProto instrument_proto; |
| 56 if (!instrument_proto.ParseFromString(input)) | 59 if (!instrument_proto.ParseFromString(input)) |
| 57 return nullptr; | 60 return nullptr; |
| 58 | 61 |
| 59 PaymentInstrumentPtr instrument = PaymentInstrument::New(); | 62 PaymentInstrumentPtr instrument = PaymentInstrument::New(); |
| 60 instrument->name = instrument_proto.name(); | 63 instrument->name = instrument_proto.name(); |
| 64 for (const auto& icon : instrument_proto.icons()) { |
| 65 payments::mojom::ImageObjectPtr image_object = |
| 66 payments::mojom::ImageObject::New(); |
| 67 image_object->src = icon.src(); |
| 68 image_object->sizes = icon.sizes(); |
| 69 image_object->type = icon.type(); |
| 70 instrument->icons.emplace_back(std::move(image_object)); |
| 71 } |
| 61 for (const auto& method : instrument_proto.enabled_methods()) | 72 for (const auto& method : instrument_proto.enabled_methods()) |
| 62 instrument->enabled_methods.push_back(method); | 73 instrument->enabled_methods.push_back(method); |
| 63 instrument->stringified_capabilities = | 74 instrument->stringified_capabilities = |
| 64 instrument_proto.stringified_capabilities(); | 75 instrument_proto.stringified_capabilities(); |
| 65 | 76 |
| 66 return instrument; | 77 return instrument; |
| 67 } | 78 } |
| 68 | 79 |
| 69 std::unique_ptr<StoredPaymentInstrument> ToStoredPaymentInstrument( | 80 std::unique_ptr<StoredPaymentInstrument> ToStoredPaymentInstrument( |
| 70 const std::string& input) { | 81 const std::string& input) { |
| 71 StoredPaymentInstrumentProto instrument_proto; | 82 StoredPaymentInstrumentProto instrument_proto; |
| 72 if (!instrument_proto.ParseFromString(input)) | 83 if (!instrument_proto.ParseFromString(input)) |
| 73 return std::unique_ptr<StoredPaymentInstrument>(); | 84 return std::unique_ptr<StoredPaymentInstrument>(); |
| 74 | 85 |
| 75 std::unique_ptr<StoredPaymentInstrument> instrument = | 86 std::unique_ptr<StoredPaymentInstrument> instrument = |
| 76 base::MakeUnique<StoredPaymentInstrument>(); | 87 base::MakeUnique<StoredPaymentInstrument>(); |
| 77 instrument->registration_id = instrument_proto.registration_id(); | 88 instrument->registration_id = instrument_proto.registration_id(); |
| 78 instrument->instrument_key = instrument_proto.instrument_key(); | 89 instrument->instrument_key = instrument_proto.instrument_key(); |
| 79 instrument->origin = GURL(instrument_proto.origin()); | 90 instrument->origin = GURL(instrument_proto.origin()); |
| 80 instrument->name = instrument_proto.name(); | 91 instrument->name = instrument_proto.name(); |
| 92 |
| 93 if (!instrument_proto.decoded_instrument_icon().empty()) { |
| 94 std::string icon_raw_data; |
| 95 base::Base64Decode(instrument_proto.decoded_instrument_icon(), |
| 96 &icon_raw_data); |
| 97 // Note that the icon has been decoded to PNG raw data regardless of the |
| 98 // original icon format that was downloaded. |
| 99 gfx::Image icon_image = gfx::Image::CreateFrom1xPNGBytes( |
| 100 reinterpret_cast<const unsigned char*>(icon_raw_data.data()), |
| 101 icon_raw_data.size()); |
| 102 instrument->icon = base::MakeUnique<SkBitmap>(icon_image.AsBitmap()); |
| 103 } |
| 81 for (const auto& method : instrument_proto.enabled_methods()) | 104 for (const auto& method : instrument_proto.enabled_methods()) |
| 82 instrument->enabled_methods.push_back(method); | 105 instrument->enabled_methods.push_back(method); |
| 83 | 106 |
| 84 return instrument; | 107 return instrument; |
| 85 } | 108 } |
| 86 | 109 |
| 87 } // namespace | 110 } // namespace |
| 88 | 111 |
| 89 PaymentAppDatabase::PaymentAppDatabase( | 112 PaymentAppDatabase::PaymentAppDatabase( |
| 90 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 113 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 PaymentInstrumentPtr instrument, | 396 PaymentInstrumentPtr instrument, |
| 374 WritePaymentInstrumentCallback callback, | 397 WritePaymentInstrumentCallback callback, |
| 375 ServiceWorkerStatusCode status, | 398 ServiceWorkerStatusCode status, |
| 376 scoped_refptr<ServiceWorkerRegistration> registration) { | 399 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 377 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 400 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 378 if (status != SERVICE_WORKER_OK) { | 401 if (status != SERVICE_WORKER_OK) { |
| 379 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 402 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 380 return; | 403 return; |
| 381 } | 404 } |
| 382 | 405 |
| 406 instrument_icon_fetcher_ = base::MakeUnique<PaymentInstrumentIconFetcher>(); |
| 407 instrument_icon_fetcher_->start( |
| 408 registration->pattern(), instrument->icons, service_worker_context_, |
| 409 base::Bind(&PaymentAppDatabase::DidFetchedPaymentInstrumentIcon, |
| 410 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
| 411 base::Passed(std::move(instrument)), |
| 412 base::Passed(std::move(callback)), |
| 413 base::Passed(std::move(registration)))); |
| 414 } |
| 415 |
| 416 void PaymentAppDatabase::DidFetchedPaymentInstrumentIcon( |
| 417 const std::string& instrument_key, |
| 418 payments::mojom::PaymentInstrumentPtr instrument, |
| 419 WritePaymentInstrumentCallback callback, |
| 420 scoped_refptr<ServiceWorkerRegistration> registration, |
| 421 const std::string& icon) { |
| 422 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 423 |
| 424 instrument_icon_fetcher_.reset(nullptr); |
| 425 if (icon.empty()) { |
| 426 std::move(callback).Run(PaymentHandlerStatus::FETCH_INSTRUMENT_ICON_FAILED); |
| 427 return; |
| 428 } |
| 429 |
| 383 StoredPaymentInstrumentProto instrument_proto; | 430 StoredPaymentInstrumentProto instrument_proto; |
| 431 instrument_proto.set_decoded_instrument_icon(icon); |
| 384 instrument_proto.set_registration_id(registration->id()); | 432 instrument_proto.set_registration_id(registration->id()); |
| 385 instrument_proto.set_instrument_key(instrument_key); | 433 instrument_proto.set_instrument_key(instrument_key); |
| 386 instrument_proto.set_origin(registration->pattern().GetOrigin().spec()); | 434 instrument_proto.set_origin(registration->pattern().GetOrigin().spec()); |
| 387 instrument_proto.set_name(instrument->name); | 435 instrument_proto.set_name(instrument->name); |
| 388 for (const auto& method : instrument->enabled_methods) { | 436 for (const auto& method : instrument->enabled_methods) { |
| 389 instrument_proto.add_enabled_methods(method); | 437 instrument_proto.add_enabled_methods(method); |
| 390 } | 438 } |
| 439 for (const auto& image_object : instrument->icons) { |
| 440 StoredPaymentInstrumentImageObject* image_object_proto = |
| 441 instrument_proto.add_icons(); |
| 442 image_object_proto->set_src(image_object->src); |
| 443 image_object_proto->set_sizes(image_object->sizes); |
| 444 image_object_proto->set_type(image_object->type); |
| 445 } |
| 391 instrument_proto.set_stringified_capabilities( | 446 instrument_proto.set_stringified_capabilities( |
| 392 instrument->stringified_capabilities); | 447 instrument->stringified_capabilities); |
| 393 | 448 |
| 394 std::string serialized_instrument; | 449 std::string serialized_instrument; |
| 395 bool success = instrument_proto.SerializeToString(&serialized_instrument); | 450 bool success = instrument_proto.SerializeToString(&serialized_instrument); |
| 396 DCHECK(success); | 451 DCHECK(success); |
| 397 | 452 |
| 398 StoredPaymentInstrumentKeyInfoProto key_info_proto; | 453 StoredPaymentInstrumentKeyInfoProto key_info_proto; |
| 399 key_info_proto.set_key(instrument_key); | 454 key_info_proto.set_key(instrument_key); |
| 400 key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue()); | 455 key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue()); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 void PaymentAppDatabase::DidClearPaymentInstruments( | 525 void PaymentAppDatabase::DidClearPaymentInstruments( |
| 471 ClearPaymentInstrumentsCallback callback, | 526 ClearPaymentInstrumentsCallback callback, |
| 472 ServiceWorkerStatusCode status) { | 527 ServiceWorkerStatusCode status) { |
| 473 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 474 return std::move(callback).Run(status == SERVICE_WORKER_OK | 529 return std::move(callback).Run(status == SERVICE_WORKER_OK |
| 475 ? PaymentHandlerStatus::SUCCESS | 530 ? PaymentHandlerStatus::SUCCESS |
| 476 : PaymentHandlerStatus::NOT_FOUND); | 531 : PaymentHandlerStatus::NOT_FOUND); |
| 477 } | 532 } |
| 478 | 533 |
| 479 } // namespace content | 534 } // namespace content |
| OLD | NEW |