| 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 instrument->icons.emplace_back( |
| 66 payments::mojom::ImageObject::New(GURL(icon.src()))); |
| 67 } |
| 61 for (const auto& method : instrument_proto.enabled_methods()) | 68 for (const auto& method : instrument_proto.enabled_methods()) |
| 62 instrument->enabled_methods.push_back(method); | 69 instrument->enabled_methods.push_back(method); |
| 63 instrument->stringified_capabilities = | 70 instrument->stringified_capabilities = |
| 64 instrument_proto.stringified_capabilities(); | 71 instrument_proto.stringified_capabilities(); |
| 65 | 72 |
| 66 return instrument; | 73 return instrument; |
| 67 } | 74 } |
| 68 | 75 |
| 69 std::unique_ptr<StoredPaymentInstrument> ToStoredPaymentInstrument( | 76 std::unique_ptr<StoredPaymentInstrument> ToStoredPaymentInstrument( |
| 70 const std::string& input) { | 77 const std::string& input) { |
| 71 StoredPaymentInstrumentProto instrument_proto; | 78 StoredPaymentInstrumentProto instrument_proto; |
| 72 if (!instrument_proto.ParseFromString(input)) | 79 if (!instrument_proto.ParseFromString(input)) |
| 73 return std::unique_ptr<StoredPaymentInstrument>(); | 80 return std::unique_ptr<StoredPaymentInstrument>(); |
| 74 | 81 |
| 75 std::unique_ptr<StoredPaymentInstrument> instrument = | 82 std::unique_ptr<StoredPaymentInstrument> instrument = |
| 76 base::MakeUnique<StoredPaymentInstrument>(); | 83 base::MakeUnique<StoredPaymentInstrument>(); |
| 77 instrument->registration_id = instrument_proto.registration_id(); | 84 instrument->registration_id = instrument_proto.registration_id(); |
| 78 instrument->instrument_key = instrument_proto.instrument_key(); | 85 instrument->instrument_key = instrument_proto.instrument_key(); |
| 79 instrument->origin = GURL(instrument_proto.origin()); | 86 instrument->origin = GURL(instrument_proto.origin()); |
| 80 instrument->name = instrument_proto.name(); | 87 instrument->name = instrument_proto.name(); |
| 88 |
| 89 if (!instrument_proto.decoded_instrument_icon().empty()) { |
| 90 std::string icon_raw_data; |
| 91 base::Base64Decode(instrument_proto.decoded_instrument_icon(), |
| 92 &icon_raw_data); |
| 93 // Note that the icon has been decoded to PNG raw data regardless of the |
| 94 // original icon format that was downloaded. |
| 95 gfx::Image icon_image = gfx::Image::CreateFrom1xPNGBytes( |
| 96 reinterpret_cast<const unsigned char*>(icon_raw_data.data()), |
| 97 icon_raw_data.size()); |
| 98 instrument->icon = base::MakeUnique<SkBitmap>(icon_image.AsBitmap()); |
| 99 } |
| 81 for (const auto& method : instrument_proto.enabled_methods()) | 100 for (const auto& method : instrument_proto.enabled_methods()) |
| 82 instrument->enabled_methods.push_back(method); | 101 instrument->enabled_methods.push_back(method); |
| 83 | 102 |
| 84 return instrument; | 103 return instrument; |
| 85 } | 104 } |
| 86 | 105 |
| 87 } // namespace | 106 } // namespace |
| 88 | 107 |
| 89 PaymentAppDatabase::PaymentAppDatabase( | 108 PaymentAppDatabase::PaymentAppDatabase( |
| 90 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 109 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 base::Passed(std::move(callback)))); | 178 base::Passed(std::move(callback)))); |
| 160 } | 179 } |
| 161 | 180 |
| 162 void PaymentAppDatabase::WritePaymentInstrument( | 181 void PaymentAppDatabase::WritePaymentInstrument( |
| 163 const GURL& scope, | 182 const GURL& scope, |
| 164 const std::string& instrument_key, | 183 const std::string& instrument_key, |
| 165 PaymentInstrumentPtr instrument, | 184 PaymentInstrumentPtr instrument, |
| 166 WritePaymentInstrumentCallback callback) { | 185 WritePaymentInstrumentCallback callback) { |
| 167 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 168 | 187 |
| 188 if (instrument->icons.size() > 0) { |
| 189 instrument_icon_fetcher_ = |
| 190 base::MakeRefCounted<PaymentInstrumentIconFetcher>(); |
| 191 instrument_icon_fetcher_->Start( |
| 192 instrument->icons, service_worker_context_, |
| 193 base::Bind(&PaymentAppDatabase::DidFetchedPaymentInstrumentIcon, |
| 194 weak_ptr_factory_.GetWeakPtr(), scope, instrument_key, |
| 195 base::Passed(std::move(instrument)), |
| 196 base::Passed(std::move(callback)))); |
| 197 } else { |
| 198 service_worker_context_->FindReadyRegistrationForPattern( |
| 199 scope, |
| 200 base::Bind( |
| 201 &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument, |
| 202 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
| 203 base::Passed(std::move(instrument)), std::string(), |
| 204 base::Passed(std::move(callback)))); |
| 205 } |
| 206 } |
| 207 |
| 208 void PaymentAppDatabase::DidFetchedPaymentInstrumentIcon( |
| 209 const GURL& scope, |
| 210 const std::string& instrument_key, |
| 211 payments::mojom::PaymentInstrumentPtr instrument, |
| 212 WritePaymentInstrumentCallback callback, |
| 213 const std::string& icon) { |
| 214 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 215 |
| 216 instrument_icon_fetcher_ = nullptr; |
| 217 if (icon.empty()) { |
| 218 std::move(callback).Run(PaymentHandlerStatus::FETCH_INSTRUMENT_ICON_FAILED); |
| 219 return; |
| 220 } |
| 221 |
| 169 service_worker_context_->FindReadyRegistrationForPattern( | 222 service_worker_context_->FindReadyRegistrationForPattern( |
| 170 scope, | 223 scope, |
| 171 base::Bind( | 224 base::Bind( |
| 172 &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument, | 225 &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument, |
| 173 weak_ptr_factory_.GetWeakPtr(), instrument_key, | 226 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
| 174 base::Passed(std::move(instrument)), | 227 base::Passed(std::move(instrument)), icon, |
| 175 base::Passed(std::move(callback)))); | 228 base::Passed(std::move(callback)))); |
| 176 } | 229 } |
| 177 | 230 |
| 178 void PaymentAppDatabase::ClearPaymentInstruments( | 231 void PaymentAppDatabase::ClearPaymentInstruments( |
| 179 const GURL& scope, | 232 const GURL& scope, |
| 180 ClearPaymentInstrumentsCallback callback) { | 233 ClearPaymentInstrumentsCallback callback) { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 234 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 182 | 235 |
| 183 service_worker_context_->FindReadyRegistrationForPattern( | 236 service_worker_context_->FindReadyRegistrationForPattern( |
| 184 scope, | 237 scope, |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); | 417 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); |
| 365 return; | 418 return; |
| 366 } | 419 } |
| 367 | 420 |
| 368 std::move(callback).Run(PaymentHandlerStatus::SUCCESS); | 421 std::move(callback).Run(PaymentHandlerStatus::SUCCESS); |
| 369 } | 422 } |
| 370 | 423 |
| 371 void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument( | 424 void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument( |
| 372 const std::string& instrument_key, | 425 const std::string& instrument_key, |
| 373 PaymentInstrumentPtr instrument, | 426 PaymentInstrumentPtr instrument, |
| 427 const std::string& decoded_instrument_icon, |
| 374 WritePaymentInstrumentCallback callback, | 428 WritePaymentInstrumentCallback callback, |
| 375 ServiceWorkerStatusCode status, | 429 ServiceWorkerStatusCode status, |
| 376 scoped_refptr<ServiceWorkerRegistration> registration) { | 430 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 377 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 431 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 378 if (status != SERVICE_WORKER_OK) { | 432 if (status != SERVICE_WORKER_OK) { |
| 379 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 433 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 380 return; | 434 return; |
| 381 } | 435 } |
| 382 | 436 |
| 383 StoredPaymentInstrumentProto instrument_proto; | 437 StoredPaymentInstrumentProto instrument_proto; |
| 438 instrument_proto.set_decoded_instrument_icon(decoded_instrument_icon); |
| 384 instrument_proto.set_registration_id(registration->id()); | 439 instrument_proto.set_registration_id(registration->id()); |
| 385 instrument_proto.set_instrument_key(instrument_key); | 440 instrument_proto.set_instrument_key(instrument_key); |
| 386 instrument_proto.set_origin(registration->pattern().GetOrigin().spec()); | 441 instrument_proto.set_origin(registration->pattern().GetOrigin().spec()); |
| 387 instrument_proto.set_name(instrument->name); | 442 instrument_proto.set_name(instrument->name); |
| 388 for (const auto& method : instrument->enabled_methods) { | 443 for (const auto& method : instrument->enabled_methods) { |
| 389 instrument_proto.add_enabled_methods(method); | 444 instrument_proto.add_enabled_methods(method); |
| 390 } | 445 } |
| 446 for (const auto& image_object : instrument->icons) { |
| 447 StoredPaymentInstrumentImageObject* image_object_proto = |
| 448 instrument_proto.add_icons(); |
| 449 image_object_proto->set_src(image_object->src.spec()); |
| 450 } |
| 391 instrument_proto.set_stringified_capabilities( | 451 instrument_proto.set_stringified_capabilities( |
| 392 instrument->stringified_capabilities); | 452 instrument->stringified_capabilities); |
| 393 | 453 |
| 394 std::string serialized_instrument; | 454 std::string serialized_instrument; |
| 395 bool success = instrument_proto.SerializeToString(&serialized_instrument); | 455 bool success = instrument_proto.SerializeToString(&serialized_instrument); |
| 396 DCHECK(success); | 456 DCHECK(success); |
| 397 | 457 |
| 398 StoredPaymentInstrumentKeyInfoProto key_info_proto; | 458 StoredPaymentInstrumentKeyInfoProto key_info_proto; |
| 399 key_info_proto.set_key(instrument_key); | 459 key_info_proto.set_key(instrument_key); |
| 400 key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue()); | 460 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( | 530 void PaymentAppDatabase::DidClearPaymentInstruments( |
| 471 ClearPaymentInstrumentsCallback callback, | 531 ClearPaymentInstrumentsCallback callback, |
| 472 ServiceWorkerStatusCode status) { | 532 ServiceWorkerStatusCode status) { |
| 473 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 533 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 474 return std::move(callback).Run(status == SERVICE_WORKER_OK | 534 return std::move(callback).Run(status == SERVICE_WORKER_OK |
| 475 ? PaymentHandlerStatus::SUCCESS | 535 ? PaymentHandlerStatus::SUCCESS |
| 476 : PaymentHandlerStatus::NOT_FOUND); | 536 : PaymentHandlerStatus::NOT_FOUND); |
| 477 } | 537 } |
| 478 | 538 |
| 479 } // namespace content | 539 } // namespace content |
| OLD | NEW |