| 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/memory/ptr_util.h" |
| 12 #include "base/optional.h" | 12 #include "base/optional.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "content/browser/payments/payment_app.pb.h" | 14 #include "content/browser/payments/payment_app.pb.h" |
| 15 #include "content/browser/payments/payment_app_context_impl.h" | 15 #include "content/browser/payments/payment_app_context_impl.h" |
| 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 16 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 17 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 using ::payments::mojom::PaymentHandlerStatus; | 23 using ::payments::mojom::PaymentHandlerStatus; |
| 24 using ::payments::mojom::PaymentInstrument; | 24 using ::payments::mojom::PaymentInstrument; |
| 25 using ::payments::mojom::PaymentInstrumentPtr; | 25 using ::payments::mojom::PaymentInstrumentPtr; |
| 26 | 26 |
| 27 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; | |
| 28 const char kPaymentInstrumentPrefix[] = "PaymentInstrument:"; | 27 const char kPaymentInstrumentPrefix[] = "PaymentInstrument:"; |
| 29 const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:"; | 28 const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:"; |
| 30 | 29 |
| 31 std::string CreatePaymentInstrumentKey(const std::string& instrument_key) { | 30 std::string CreatePaymentInstrumentKey(const std::string& instrument_key) { |
| 32 return kPaymentInstrumentPrefix + instrument_key; | 31 return kPaymentInstrumentPrefix + instrument_key; |
| 33 } | 32 } |
| 34 | 33 |
| 35 std::string CreatePaymentInstrumentKeyInfoKey( | 34 std::string CreatePaymentInstrumentKeyInfoKey( |
| 36 const std::string& instrument_key) { | 35 const std::string& instrument_key) { |
| 37 return kPaymentInstrumentKeyInfoPrefix + instrument_key; | 36 return kPaymentInstrumentKeyInfoPrefix + instrument_key; |
| 38 } | 37 } |
| 39 | 38 |
| 40 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( | |
| 41 const std::string& input) { | |
| 42 PaymentAppManifestProto manifest_proto; | |
| 43 if (!manifest_proto.ParseFromString(input)) | |
| 44 return nullptr; | |
| 45 | |
| 46 payments::mojom::PaymentAppManifestPtr manifest = | |
| 47 payments::mojom::PaymentAppManifest::New(); | |
| 48 manifest->name = manifest_proto.name(); | |
| 49 if (manifest_proto.has_icon()) | |
| 50 manifest->icon = manifest_proto.icon(); | |
| 51 for (const auto& option_proto : manifest_proto.options()) { | |
| 52 payments::mojom::PaymentAppOptionPtr option = | |
| 53 payments::mojom::PaymentAppOption::New(); | |
| 54 option->name = option_proto.name(); | |
| 55 if (option_proto.has_icon()) | |
| 56 option->icon = option_proto.icon(); | |
| 57 option->id = option_proto.id(); | |
| 58 for (const auto& method : option_proto.enabled_methods()) | |
| 59 option->enabled_methods.push_back(method); | |
| 60 manifest->options.push_back(std::move(option)); | |
| 61 } | |
| 62 | |
| 63 return manifest; | |
| 64 } | |
| 65 | |
| 66 std::map<uint64_t, std::string> ToStoredPaymentInstrumentKeyInfos( | 39 std::map<uint64_t, std::string> ToStoredPaymentInstrumentKeyInfos( |
| 67 const std::vector<std::string>& inputs) { | 40 const std::vector<std::string>& inputs) { |
| 68 std::map<uint64_t, std::string> key_info; | 41 std::map<uint64_t, std::string> key_info; |
| 69 for (const auto& input : inputs) { | 42 for (const auto& input : inputs) { |
| 70 StoredPaymentInstrumentKeyInfoProto key_info_proto; | 43 StoredPaymentInstrumentKeyInfoProto key_info_proto; |
| 71 if (!key_info_proto.ParseFromString(input)) | 44 if (!key_info_proto.ParseFromString(input)) |
| 72 return std::map<uint64_t, std::string>(); | 45 return std::map<uint64_t, std::string>(); |
| 73 | 46 |
| 74 key_info.insert(std::pair<uint64_t, std::string>( | 47 key_info.insert(std::pair<uint64_t, std::string>( |
| 75 key_info_proto.insertion_order(), key_info_proto.key())); | 48 key_info_proto.insertion_order(), key_info_proto.key())); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 PaymentAppDatabase::PaymentAppDatabase( | 89 PaymentAppDatabase::PaymentAppDatabase( |
| 117 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 90 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) |
| 118 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | 91 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 120 } | 93 } |
| 121 | 94 |
| 122 PaymentAppDatabase::~PaymentAppDatabase() { | 95 PaymentAppDatabase::~PaymentAppDatabase() { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 124 } | 97 } |
| 125 | 98 |
| 126 void PaymentAppDatabase::WriteManifest( | |
| 127 const GURL& scope, | |
| 128 payments::mojom::PaymentAppManifestPtr manifest, | |
| 129 WriteManifestCallback callback) { | |
| 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 131 | |
| 132 service_worker_context_->FindReadyRegistrationForPattern( | |
| 133 scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToWriteManifest, | |
| 134 weak_ptr_factory_.GetWeakPtr(), base::Passed(&manifest), | |
| 135 base::Passed(&callback))); | |
| 136 } | |
| 137 | |
| 138 void PaymentAppDatabase::ReadManifest(const GURL& scope, | |
| 139 ReadManifestCallback callback) { | |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 141 | |
| 142 service_worker_context_->FindReadyRegistrationForPattern( | |
| 143 scope, | |
| 144 base::Bind(&PaymentAppDatabase::DidFindRegistrationToReadManifest, | |
| 145 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | |
| 146 } | |
| 147 | |
| 148 void PaymentAppDatabase::ReadAllManifests(ReadAllManifestsCallback callback) { | |
| 149 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 150 | |
| 151 service_worker_context_->GetUserDataForAllRegistrations( | |
| 152 kPaymentAppManifestDataKey, | |
| 153 base::Bind(&PaymentAppDatabase::DidReadAllManifests, | |
| 154 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | |
| 155 } | |
| 156 | |
| 157 void PaymentAppDatabase::ReadAllPaymentApps( | 99 void PaymentAppDatabase::ReadAllPaymentApps( |
| 158 ReadAllPaymentAppsCallback callback) { | 100 ReadAllPaymentAppsCallback callback) { |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 160 | 102 |
| 161 service_worker_context_->GetUserDataForAllRegistrationsByKeyPrefix( | 103 service_worker_context_->GetUserDataForAllRegistrationsByKeyPrefix( |
| 162 kPaymentInstrumentPrefix, | 104 kPaymentInstrumentPrefix, |
| 163 base::Bind(&PaymentAppDatabase::DidReadAllPaymentApps, | 105 base::Bind(&PaymentAppDatabase::DidReadAllPaymentApps, |
| 164 weak_ptr_factory_.GetWeakPtr(), | 106 weak_ptr_factory_.GetWeakPtr(), |
| 165 base::Passed(std::move(callback)))); | 107 base::Passed(std::move(callback)))); |
| 166 } | 108 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 240 | 182 |
| 241 service_worker_context_->FindReadyRegistrationForPattern( | 183 service_worker_context_->FindReadyRegistrationForPattern( |
| 242 scope, | 184 scope, |
| 243 base::Bind( | 185 base::Bind( |
| 244 &PaymentAppDatabase::DidFindRegistrationToClearPaymentInstruments, | 186 &PaymentAppDatabase::DidFindRegistrationToClearPaymentInstruments, |
| 245 weak_ptr_factory_.GetWeakPtr(), scope, | 187 weak_ptr_factory_.GetWeakPtr(), scope, |
| 246 base::Passed(std::move(callback)))); | 188 base::Passed(std::move(callback)))); |
| 247 } | 189 } |
| 248 | 190 |
| 249 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( | |
| 250 payments::mojom::PaymentAppManifestPtr manifest, | |
| 251 WriteManifestCallback callback, | |
| 252 ServiceWorkerStatusCode status, | |
| 253 scoped_refptr<ServiceWorkerRegistration> registration) { | |
| 254 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 255 if (status != SERVICE_WORKER_OK) { | |
| 256 std::move(callback).Run( | |
| 257 payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); | |
| 258 return; | |
| 259 } | |
| 260 | |
| 261 PaymentAppManifestProto manifest_proto; | |
| 262 manifest_proto.set_name(manifest->name); | |
| 263 if (manifest->icon) | |
| 264 manifest_proto.set_icon(manifest->icon.value()); | |
| 265 | |
| 266 for (const auto& option : manifest->options) { | |
| 267 PaymentAppOptionProto* option_proto = manifest_proto.add_options(); | |
| 268 option_proto->set_name(option->name); | |
| 269 if (option->icon) | |
| 270 option_proto->set_icon(option->icon.value()); | |
| 271 option_proto->set_id(option->id); | |
| 272 for (const auto& method : option->enabled_methods) { | |
| 273 option_proto->add_enabled_methods(method); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 std::string serialized; | |
| 278 bool success = manifest_proto.SerializeToString(&serialized); | |
| 279 DCHECK(success); | |
| 280 | |
| 281 service_worker_context_->StoreRegistrationUserData( | |
| 282 registration->id(), registration->pattern().GetOrigin(), | |
| 283 {{kPaymentAppManifestDataKey, serialized}}, | |
| 284 base::Bind(&PaymentAppDatabase::DidWriteManifest, | |
| 285 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | |
| 286 } | |
| 287 | |
| 288 void PaymentAppDatabase::DidWriteManifest(WriteManifestCallback callback, | |
| 289 ServiceWorkerStatusCode status) { | |
| 290 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 291 std::move(callback).Run(status == SERVICE_WORKER_OK | |
| 292 ? payments::mojom::PaymentAppManifestError::NONE | |
| 293 : payments::mojom::PaymentAppManifestError:: | |
| 294 MANIFEST_STORAGE_OPERATION_FAILED); | |
| 295 } | |
| 296 | |
| 297 void PaymentAppDatabase::DidFindRegistrationToReadManifest( | |
| 298 ReadManifestCallback callback, | |
| 299 ServiceWorkerStatusCode status, | |
| 300 scoped_refptr<ServiceWorkerRegistration> registration) { | |
| 301 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 302 if (status != SERVICE_WORKER_OK) { | |
| 303 std::move(callback).Run( | |
| 304 payments::mojom::PaymentAppManifest::New(), | |
| 305 payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); | |
| 306 return; | |
| 307 } | |
| 308 | |
| 309 service_worker_context_->GetRegistrationUserData( | |
| 310 registration->id(), {kPaymentAppManifestDataKey}, | |
| 311 base::Bind(&PaymentAppDatabase::DidReadManifest, | |
| 312 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | |
| 313 } | |
| 314 | |
| 315 void PaymentAppDatabase::DidReadManifest(ReadManifestCallback callback, | |
| 316 const std::vector<std::string>& data, | |
| 317 ServiceWorkerStatusCode status) { | |
| 318 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 319 if (status != SERVICE_WORKER_OK || data.size() != 1) { | |
| 320 std::move(callback).Run(payments::mojom::PaymentAppManifest::New(), | |
| 321 payments::mojom::PaymentAppManifestError:: | |
| 322 MANIFEST_STORAGE_OPERATION_FAILED); | |
| 323 return; | |
| 324 } | |
| 325 | |
| 326 payments::mojom::PaymentAppManifestPtr manifest = | |
| 327 DeserializePaymentAppManifest(data[0]); | |
| 328 if (!manifest) { | |
| 329 std::move(callback).Run(payments::mojom::PaymentAppManifest::New(), | |
| 330 payments::mojom::PaymentAppManifestError:: | |
| 331 MANIFEST_STORAGE_OPERATION_FAILED); | |
| 332 return; | |
| 333 } | |
| 334 | |
| 335 std::move(callback).Run(std::move(manifest), | |
| 336 payments::mojom::PaymentAppManifestError::NONE); | |
| 337 } | |
| 338 | |
| 339 void PaymentAppDatabase::DidReadAllManifests( | |
| 340 ReadAllManifestsCallback callback, | |
| 341 const std::vector<std::pair<int64_t, std::string>>& raw_data, | |
| 342 ServiceWorkerStatusCode status) { | |
| 343 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 344 if (status != SERVICE_WORKER_OK) { | |
| 345 std::move(callback).Run(Manifests()); | |
| 346 return; | |
| 347 } | |
| 348 | |
| 349 Manifests manifests; | |
| 350 for (const auto& item_of_raw_data : raw_data) { | |
| 351 payments::mojom::PaymentAppManifestPtr manifest = | |
| 352 DeserializePaymentAppManifest(item_of_raw_data.second); | |
| 353 if (!manifest) | |
| 354 continue; | |
| 355 | |
| 356 manifests.push_back( | |
| 357 ManifestWithID(item_of_raw_data.first, std::move(manifest))); | |
| 358 } | |
| 359 | |
| 360 std::move(callback).Run(std::move(manifests)); | |
| 361 } | |
| 362 | |
| 363 void PaymentAppDatabase::DidReadAllPaymentApps( | 191 void PaymentAppDatabase::DidReadAllPaymentApps( |
| 364 ReadAllPaymentAppsCallback callback, | 192 ReadAllPaymentAppsCallback callback, |
| 365 const std::vector<std::pair<int64_t, std::string>>& raw_data, | 193 const std::vector<std::pair<int64_t, std::string>>& raw_data, |
| 366 ServiceWorkerStatusCode status) { | 194 ServiceWorkerStatusCode status) { |
| 367 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 195 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 368 if (status != SERVICE_WORKER_OK) { | 196 if (status != SERVICE_WORKER_OK) { |
| 369 std::move(callback).Run(PaymentApps()); | 197 std::move(callback).Run(PaymentApps()); |
| 370 return; | 198 return; |
| 371 } | 199 } |
| 372 | 200 |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 void PaymentAppDatabase::DidClearPaymentInstruments( | 472 void PaymentAppDatabase::DidClearPaymentInstruments( |
| 645 ClearPaymentInstrumentsCallback callback, | 473 ClearPaymentInstrumentsCallback callback, |
| 646 ServiceWorkerStatusCode status) { | 474 ServiceWorkerStatusCode status) { |
| 647 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 475 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 648 return std::move(callback).Run(status == SERVICE_WORKER_OK | 476 return std::move(callback).Run(status == SERVICE_WORKER_OK |
| 649 ? PaymentHandlerStatus::SUCCESS | 477 ? PaymentHandlerStatus::SUCCESS |
| 650 : PaymentHandlerStatus::NOT_FOUND); | 478 : PaymentHandlerStatus::NOT_FOUND); |
| 651 } | 479 } |
| 652 | 480 |
| 653 } // namespace content | 481 } // namespace content |
| OLD | NEW |