| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/optional.h" | 10 #include "base/optional.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 void PaymentAppDatabase::ReadAllManifests( | 100 void PaymentAppDatabase::ReadAllManifests( |
| 101 const ReadAllManifestsCallback& callback) { | 101 const ReadAllManifestsCallback& callback) { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 103 | 103 |
| 104 service_worker_context_->GetUserDataForAllRegistrations( | 104 service_worker_context_->GetUserDataForAllRegistrations( |
| 105 kPaymentAppManifestDataKey, | 105 kPaymentAppManifestDataKey, |
| 106 base::Bind(&PaymentAppDatabase::DidReadAllManifests, | 106 base::Bind(&PaymentAppDatabase::DidReadAllManifests, |
| 107 weak_ptr_factory_.GetWeakPtr(), callback)); | 107 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void PaymentAppDatabase::DeletePaymentInstrument( |
| 111 const GURL& scope, |
| 112 const std::string& instrument_key, |
| 113 DeletePaymentInstrumentCallback callback) { |
| 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 115 |
| 116 service_worker_context_->FindReadyRegistrationForPattern( |
| 117 scope, |
| 118 base::Bind( |
| 119 &PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument, |
| 120 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
| 121 base::Passed(std::move(callback)))); |
| 122 } |
| 123 |
| 110 void PaymentAppDatabase::ReadPaymentInstrument( | 124 void PaymentAppDatabase::ReadPaymentInstrument( |
| 111 const GURL& scope, | 125 const GURL& scope, |
| 112 const std::string& instrumentKey, | 126 const std::string& instrument_key, |
| 113 ReadPaymentInstrumentCallback callback) { | 127 ReadPaymentInstrumentCallback callback) { |
| 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 128 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 115 | 129 |
| 116 service_worker_context_->FindReadyRegistrationForPattern( | 130 service_worker_context_->FindReadyRegistrationForPattern( |
| 117 scope, | 131 scope, |
| 118 base::Bind( | 132 base::Bind( |
| 119 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, | 133 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, |
| 120 weak_ptr_factory_.GetWeakPtr(), instrumentKey, | 134 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
| 121 base::Passed(std::move(callback)))); | 135 base::Passed(std::move(callback)))); |
| 122 } | 136 } |
| 123 | 137 |
| 124 void PaymentAppDatabase::WritePaymentInstrument( | 138 void PaymentAppDatabase::WritePaymentInstrument( |
| 125 const GURL& scope, | 139 const GURL& scope, |
| 126 const std::string& instrumentKey, | 140 const std::string& instrument_key, |
| 127 PaymentInstrumentPtr instrument, | 141 PaymentInstrumentPtr instrument, |
| 128 WritePaymentInstrumentCallback callback) { | 142 WritePaymentInstrumentCallback callback) { |
| 129 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 143 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 130 | 144 |
| 131 service_worker_context_->FindReadyRegistrationForPattern( | 145 service_worker_context_->FindReadyRegistrationForPattern( |
| 132 scope, | 146 scope, |
| 133 base::Bind( | 147 base::Bind( |
| 134 &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument, | 148 &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument, |
| 135 weak_ptr_factory_.GetWeakPtr(), instrumentKey, | 149 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
| 136 base::Passed(std::move(instrument)), | 150 base::Passed(std::move(instrument)), |
| 137 base::Passed(std::move(callback)))); | 151 base::Passed(std::move(callback)))); |
| 138 } | 152 } |
| 139 | 153 |
| 140 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( | 154 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( |
| 141 payments::mojom::PaymentAppManifestPtr manifest, | 155 payments::mojom::PaymentAppManifestPtr manifest, |
| 142 const WriteManifestCallback& callback, | 156 const WriteManifestCallback& callback, |
| 143 ServiceWorkerStatusCode status, | 157 ServiceWorkerStatusCode status, |
| 144 scoped_refptr<ServiceWorkerRegistration> registration) { | 158 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 145 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 if (!manifest) | 256 if (!manifest) |
| 243 continue; | 257 continue; |
| 244 | 258 |
| 245 manifests.push_back( | 259 manifests.push_back( |
| 246 ManifestWithID(item_of_raw_data.first, std::move(manifest))); | 260 ManifestWithID(item_of_raw_data.first, std::move(manifest))); |
| 247 } | 261 } |
| 248 | 262 |
| 249 callback.Run(std::move(manifests)); | 263 callback.Run(std::move(manifests)); |
| 250 } | 264 } |
| 251 | 265 |
| 266 void PaymentAppDatabase::DidFindRegistrationToDeletePaymentInstrument( |
| 267 const std::string& instrument_key, |
| 268 DeletePaymentInstrumentCallback callback, |
| 269 ServiceWorkerStatusCode status, |
| 270 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 271 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 272 if (status != SERVICE_WORKER_OK) { |
| 273 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 274 return; |
| 275 } |
| 276 |
| 277 service_worker_context_->GetRegistrationUserData( |
| 278 registration->id(), {instrument_key}, |
| 279 base::Bind(&PaymentAppDatabase::DidFindPaymentInstrument, |
| 280 weak_ptr_factory_.GetWeakPtr(), registration->id(), |
| 281 instrument_key, base::Passed(std::move(callback)))); |
| 282 } |
| 283 |
| 284 void PaymentAppDatabase::DidFindPaymentInstrument( |
| 285 int64_t registration_id, |
| 286 const std::string& instrument_key, |
| 287 DeletePaymentInstrumentCallback callback, |
| 288 const std::vector<std::string>& data, |
| 289 ServiceWorkerStatusCode status) { |
| 290 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 291 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
| 292 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); |
| 293 return; |
| 294 } |
| 295 |
| 296 service_worker_context_->ClearRegistrationUserData( |
| 297 registration_id, {instrument_key}, |
| 298 base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument, |
| 299 weak_ptr_factory_.GetWeakPtr(), |
| 300 base::Passed(std::move(callback)))); |
| 301 } |
| 302 |
| 303 void PaymentAppDatabase::DidDeletePaymentInstrument( |
| 304 DeletePaymentInstrumentCallback callback, |
| 305 ServiceWorkerStatusCode status) { |
| 306 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 307 return std::move(callback).Run(status == SERVICE_WORKER_OK |
| 308 ? PaymentHandlerStatus::SUCCESS |
| 309 : PaymentHandlerStatus::NOT_FOUND); |
| 310 } |
| 311 |
| 252 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument( | 312 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument( |
| 253 const std::string& instrumentKey, | 313 const std::string& instrument_key, |
| 254 ReadPaymentInstrumentCallback callback, | 314 ReadPaymentInstrumentCallback callback, |
| 255 ServiceWorkerStatusCode status, | 315 ServiceWorkerStatusCode status, |
| 256 scoped_refptr<ServiceWorkerRegistration> registration) { | 316 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 257 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 317 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 258 if (status != SERVICE_WORKER_OK) { | 318 if (status != SERVICE_WORKER_OK) { |
| 259 std::move(callback).Run(PaymentInstrument::New(), | 319 std::move(callback).Run(PaymentInstrument::New(), |
| 260 PaymentHandlerStatus::NO_ACTIVE_WORKER); | 320 PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 261 return; | 321 return; |
| 262 } | 322 } |
| 263 | 323 |
| 264 service_worker_context_->GetRegistrationUserData( | 324 service_worker_context_->GetRegistrationUserData( |
| 265 registration->id(), {instrumentKey}, | 325 registration->id(), {instrument_key}, |
| 266 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument, | 326 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument, |
| 267 weak_ptr_factory_.GetWeakPtr(), | 327 weak_ptr_factory_.GetWeakPtr(), |
| 268 base::Passed(std::move(callback)))); | 328 base::Passed(std::move(callback)))); |
| 269 } | 329 } |
| 270 | 330 |
| 271 void PaymentAppDatabase::DidReadPaymentInstrument( | 331 void PaymentAppDatabase::DidReadPaymentInstrument( |
| 272 ReadPaymentInstrumentCallback callback, | 332 ReadPaymentInstrumentCallback callback, |
| 273 const std::vector<std::string>& data, | 333 const std::vector<std::string>& data, |
| 274 ServiceWorkerStatusCode status) { | 334 ServiceWorkerStatusCode status) { |
| 275 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 335 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 276 if (status != SERVICE_WORKER_OK || data.size() != 1) { | 336 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
| 277 std::move(callback).Run(PaymentInstrument::New(), | 337 std::move(callback).Run(PaymentInstrument::New(), |
| 278 PaymentHandlerStatus::NOT_FOUND); | 338 PaymentHandlerStatus::NOT_FOUND); |
| 279 return; | 339 return; |
| 280 } | 340 } |
| 281 | 341 |
| 282 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); | 342 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); |
| 283 if (!instrument) { | 343 if (!instrument) { |
| 284 std::move(callback).Run(PaymentInstrument::New(), | 344 std::move(callback).Run(PaymentInstrument::New(), |
| 285 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 345 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
| 286 return; | 346 return; |
| 287 } | 347 } |
| 288 | 348 |
| 289 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); | 349 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); |
| 290 } | 350 } |
| 291 | 351 |
| 292 void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument( | 352 void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument( |
| 293 const std::string& instrumentKey, | 353 const std::string& instrument_key, |
| 294 PaymentInstrumentPtr instrument, | 354 PaymentInstrumentPtr instrument, |
| 295 WritePaymentInstrumentCallback callback, | 355 WritePaymentInstrumentCallback callback, |
| 296 ServiceWorkerStatusCode status, | 356 ServiceWorkerStatusCode status, |
| 297 scoped_refptr<ServiceWorkerRegistration> registration) { | 357 scoped_refptr<ServiceWorkerRegistration> registration) { |
| 298 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 358 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 299 if (status != SERVICE_WORKER_OK) { | 359 if (status != SERVICE_WORKER_OK) { |
| 300 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 360 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| 301 return; | 361 return; |
| 302 } | 362 } |
| 303 | 363 |
| 304 PaymentInstrumentProto instrument_proto; | 364 PaymentInstrumentProto instrument_proto; |
| 305 instrument_proto.set_name(instrument->name); | 365 instrument_proto.set_name(instrument->name); |
| 306 for (const auto& method : instrument->enabled_methods) { | 366 for (const auto& method : instrument->enabled_methods) { |
| 307 instrument_proto.add_enabled_methods(method); | 367 instrument_proto.add_enabled_methods(method); |
| 308 } | 368 } |
| 309 instrument_proto.set_stringified_capabilities( | 369 instrument_proto.set_stringified_capabilities( |
| 310 instrument->stringified_capabilities); | 370 instrument->stringified_capabilities); |
| 311 | 371 |
| 312 std::string serialized; | 372 std::string serialized; |
| 313 bool success = instrument_proto.SerializeToString(&serialized); | 373 bool success = instrument_proto.SerializeToString(&serialized); |
| 314 DCHECK(success); | 374 DCHECK(success); |
| 315 | 375 |
| 316 service_worker_context_->StoreRegistrationUserData( | 376 service_worker_context_->StoreRegistrationUserData( |
| 317 registration->id(), registration->pattern().GetOrigin(), | 377 registration->id(), registration->pattern().GetOrigin(), |
| 318 {{instrumentKey, serialized}}, | 378 {{instrument_key, serialized}}, |
| 319 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, | 379 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, |
| 320 weak_ptr_factory_.GetWeakPtr(), | 380 weak_ptr_factory_.GetWeakPtr(), |
| 321 base::Passed(std::move(callback)))); | 381 base::Passed(std::move(callback)))); |
| 322 } | 382 } |
| 323 | 383 |
| 324 void PaymentAppDatabase::DidWritePaymentInstrument( | 384 void PaymentAppDatabase::DidWritePaymentInstrument( |
| 325 WritePaymentInstrumentCallback callback, | 385 WritePaymentInstrumentCallback callback, |
| 326 ServiceWorkerStatusCode status) { | 386 ServiceWorkerStatusCode status) { |
| 327 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 387 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 328 return std::move(callback).Run( | 388 return std::move(callback).Run( |
| 329 status == SERVICE_WORKER_OK | 389 status == SERVICE_WORKER_OK |
| 330 ? PaymentHandlerStatus::SUCCESS | 390 ? PaymentHandlerStatus::SUCCESS |
| 331 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 391 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
| 332 } | 392 } |
| 333 | 393 |
| 334 } // namespace content | 394 } // namespace content |
| OLD | NEW |