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/base64.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/optional.h" | 13 #include "base/optional.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "content/browser/payments/payment_app.pb.h" | 15 #include "content/browser/payments/payment_app.pb.h" |
16 #include "content/browser/payments/payment_app_context_impl.h" | 16 #include "content/browser/payments/payment_app_context_impl.h" |
17 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
18 #include "content/browser/service_worker/service_worker_registration.h" | 18 #include "content/browser/service_worker/service_worker_registration.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/stored_payment_instrument.h" | |
21 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
22 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
23 #include "url/gurl.h" | 22 #include "url/gurl.h" |
24 #include "url/origin.h" | 23 #include "url/origin.h" |
25 | 24 |
26 namespace content { | 25 namespace content { |
27 namespace { | 26 namespace { |
28 | 27 |
29 using ::payments::mojom::PaymentHandlerStatus; | 28 using ::payments::mojom::PaymentHandlerStatus; |
30 using ::payments::mojom::PaymentInstrument; | 29 using ::payments::mojom::PaymentInstrument; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 // original icon format that was downloaded. | 97 // original icon format that was downloaded. |
99 gfx::Image icon_image = gfx::Image::CreateFrom1xPNGBytes( | 98 gfx::Image icon_image = gfx::Image::CreateFrom1xPNGBytes( |
100 reinterpret_cast<const unsigned char*>(icon_raw_data.data()), | 99 reinterpret_cast<const unsigned char*>(icon_raw_data.data()), |
101 icon_raw_data.size()); | 100 icon_raw_data.size()); |
102 app->icon = base::MakeUnique<SkBitmap>(icon_image.AsBitmap()); | 101 app->icon = base::MakeUnique<SkBitmap>(icon_image.AsBitmap()); |
103 } | 102 } |
104 | 103 |
105 return app; | 104 return app; |
106 } | 105 } |
107 | 106 |
108 std::unique_ptr<StoredPaymentInstrument> ToStoredPaymentInstrument( | |
109 const std::string& input) { | |
110 StoredPaymentInstrumentProto instrument_proto; | |
111 if (!instrument_proto.ParseFromString(input)) | |
112 return std::unique_ptr<StoredPaymentInstrument>(); | |
113 | |
114 std::unique_ptr<StoredPaymentInstrument> instrument = | |
115 base::MakeUnique<StoredPaymentInstrument>(); | |
116 instrument->instrument_key = instrument_proto.instrument_key(); | |
117 instrument->origin = GURL(instrument_proto.origin()); | |
118 instrument->name = instrument_proto.name(); | |
119 | |
120 if (!instrument_proto.decoded_instrument_icon().empty()) { | |
121 std::string icon_raw_data; | |
122 base::Base64Decode(instrument_proto.decoded_instrument_icon(), | |
123 &icon_raw_data); | |
124 // Note that the icon has been decoded to PNG raw data regardless of the | |
125 // original icon format that was downloaded. | |
126 gfx::Image icon_image = gfx::Image::CreateFrom1xPNGBytes( | |
127 reinterpret_cast<const unsigned char*>(icon_raw_data.data()), | |
128 icon_raw_data.size()); | |
129 instrument->icon = base::MakeUnique<SkBitmap>(icon_image.AsBitmap()); | |
130 } | |
131 for (const auto& method : instrument_proto.enabled_methods()) | |
132 instrument->enabled_methods.push_back(method); | |
133 | |
134 return instrument; | |
135 } | |
136 | |
137 } // namespace | 107 } // namespace |
138 | 108 |
139 PaymentAppDatabase::PaymentAppDatabase( | 109 PaymentAppDatabase::PaymentAppDatabase( |
140 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 110 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
141 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | 111 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { |
142 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
143 } | 113 } |
144 | 114 |
145 PaymentAppDatabase::~PaymentAppDatabase() { | 115 PaymentAppDatabase::~PaymentAppDatabase() { |
146 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 ReadAllPaymentAppsCallback callback, | 354 ReadAllPaymentAppsCallback callback, |
385 const std::vector<std::pair<int64_t, std::string>>& raw_data, | 355 const std::vector<std::pair<int64_t, std::string>>& raw_data, |
386 ServiceWorkerStatusCode status) { | 356 ServiceWorkerStatusCode status) { |
387 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 357 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
388 if (status != SERVICE_WORKER_OK) { | 358 if (status != SERVICE_WORKER_OK) { |
389 std::move(callback).Run(std::move(apps)); | 359 std::move(callback).Run(std::move(apps)); |
390 return; | 360 return; |
391 } | 361 } |
392 | 362 |
393 for (const auto& item_of_raw_data : raw_data) { | 363 for (const auto& item_of_raw_data : raw_data) { |
394 std::unique_ptr<StoredPaymentInstrument> instrument = | 364 StoredPaymentInstrumentProto instrument_proto; |
395 ToStoredPaymentInstrument(item_of_raw_data.second); | 365 if (!instrument_proto.ParseFromString(item_of_raw_data.second)) |
396 if (!instrument || !base::ContainsKey(apps, instrument->origin)) | |
397 continue; | 366 continue; |
398 apps[instrument->origin]->instruments.push_back(std::move(instrument)); | 367 |
| 368 GURL origin = GURL(instrument_proto.origin()); |
| 369 if (!base::ContainsKey(apps, origin)) |
| 370 continue; |
| 371 |
| 372 for (const auto& method : instrument_proto.enabled_methods()) { |
| 373 apps[origin]->enabled_methods.push_back(method); |
| 374 } |
399 } | 375 } |
400 | 376 |
401 std::move(callback).Run(std::move(apps)); | 377 std::move(callback).Run(std::move(apps)); |
402 } | 378 } |
403 | 379 |
404 void PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument( | 380 void PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument( |
405 const std::string& instrument_key, | 381 const std::string& instrument_key, |
406 DeletePaymentInstrumentCallback callback, | 382 DeletePaymentInstrumentCallback callback, |
407 ServiceWorkerStatusCode status, | 383 ServiceWorkerStatusCode status, |
408 scoped_refptr<ServiceWorkerRegistration> registration) { | 384 scoped_refptr<ServiceWorkerRegistration> registration) { |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 void PaymentAppDatabase::DidClearPaymentInstruments( | 645 void PaymentAppDatabase::DidClearPaymentInstruments( |
670 ClearPaymentInstrumentsCallback callback, | 646 ClearPaymentInstrumentsCallback callback, |
671 ServiceWorkerStatusCode status) { | 647 ServiceWorkerStatusCode status) { |
672 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 648 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
673 return std::move(callback).Run(status == SERVICE_WORKER_OK | 649 return std::move(callback).Run(status == SERVICE_WORKER_OK |
674 ? PaymentHandlerStatus::SUCCESS | 650 ? PaymentHandlerStatus::SUCCESS |
675 : PaymentHandlerStatus::NOT_FOUND); | 651 : PaymentHandlerStatus::NOT_FOUND); |
676 } | 652 } |
677 | 653 |
678 } // namespace content | 654 } // namespace content |
OLD | NEW |