Chromium Code Reviews| Index: content/browser/payments/payment_app_database.cc |
| diff --git a/content/browser/payments/payment_app_database.cc b/content/browser/payments/payment_app_database.cc |
| index 1809cc398d9fa3abf405902f41b5b7e637e78e55..2155e6ef66e2782f21e488008b98693bca283d63 100644 |
| --- a/content/browser/payments/payment_app_database.cc |
| +++ b/content/browser/payments/payment_app_database.cc |
| @@ -45,6 +45,21 @@ payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( |
| return manifest; |
| } |
| +PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) { |
| + PaymentInstrumentProto instrument_proto; |
| + if (!instrument_proto.ParseFromString(input)) |
| + return nullptr; |
| + |
| + PaymentInstrumentPtr instrument = PaymentInstrument::New(); |
| + instrument->name = instrument_proto.name(); |
| + for (const auto& method : instrument_proto.enabled_methods()) |
| + instrument->enabled_methods.push_back(method); |
|
please use gerrit instead
2017/04/12 19:11:31
Does this work?
instrument->enabled_methods = ins
zino
2017/04/15 05:18:09
We can not, the instrument_proto.enabled_methods()
|
| + instrument->stringified_capabilities = |
| + instrument_proto.stringified_capabilities(); |
| + |
| + return instrument; |
| +} |
| + |
| } // namespace |
| PaymentAppDatabase::PaymentAppDatabase( |
| @@ -88,6 +103,36 @@ void PaymentAppDatabase::ReadAllManifests( |
| weak_ptr_factory_.GetWeakPtr(), callback)); |
| } |
| +void PaymentAppDatabase::ReadPaymentInstrument( |
| + const GURL& scope, |
| + const std::string& instrumentKey, |
| + ReadPaymentInstrumentCallback callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + service_worker_context_->FindReadyRegistrationForPattern( |
| + scope, |
| + base::Bind( |
| + &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, |
| + weak_ptr_factory_.GetWeakPtr(), instrumentKey, |
| + base::Passed(std::move(callback)))); |
|
please use gerrit instead
2017/04/12 19:11:31
Is base::Passed(std::move()) wrapper necessary? Yo
zino
2017/04/15 05:18:09
We can not..
The FindReadyRegistrationForPattern s
|
| +} |
| + |
| +void PaymentAppDatabase::WritePaymentInstrument( |
| + const GURL& scope, |
| + const std::string& instrumentKey, |
| + PaymentInstrumentPtr instrument, |
| + WritePaymentInstrumentCallback callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + service_worker_context_->FindReadyRegistrationForPattern( |
| + scope, |
| + base::Bind( |
| + &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument, |
| + weak_ptr_factory_.GetWeakPtr(), instrumentKey, |
| + base::Passed(std::move(instrument)), |
| + base::Passed(std::move(callback)))); |
| +} |
| + |
| void PaymentAppDatabase::DidFindRegistrationToWriteManifest( |
| payments::mojom::PaymentAppManifestPtr manifest, |
| const WriteManifestCallback& callback, |
| @@ -200,4 +245,86 @@ void PaymentAppDatabase::DidReadAllManifests( |
| callback.Run(std::move(manifests)); |
| } |
| +void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument( |
| + const std::string& instrumentKey, |
| + ReadPaymentInstrumentCallback callback, |
| + ServiceWorkerStatusCode status, |
| + scoped_refptr<ServiceWorkerRegistration> registration) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (status != SERVICE_WORKER_OK) { |
| + std::move(callback).Run(PaymentInstrument::New(), |
| + PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| + return; |
| + } |
| + |
| + service_worker_context_->GetRegistrationUserData( |
| + registration->id(), {instrumentKey}, |
| + base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + base::Passed(std::move(callback)))); |
| +} |
| + |
| +void PaymentAppDatabase::DidReadPaymentInstrument( |
| + ReadPaymentInstrumentCallback callback, |
| + const std::vector<std::string>& data, |
| + ServiceWorkerStatusCode status) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (status != SERVICE_WORKER_OK || data.size() != 1) { |
| + std::move(callback).Run(PaymentInstrument::New(), |
| + PaymentHandlerStatus::NOT_FOUND); |
| + return; |
| + } |
| + |
| + PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); |
| + if (!instrument) { |
| + std::move(callback).Run(PaymentInstrument::New(), |
| + PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
| + return; |
| + } |
| + |
| + std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); |
| +} |
| + |
| +void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument( |
| + const std::string& instrumentKey, |
| + PaymentInstrumentPtr instrument, |
| + WritePaymentInstrumentCallback callback, |
| + ServiceWorkerStatusCode status, |
| + scoped_refptr<ServiceWorkerRegistration> registration) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (status != SERVICE_WORKER_OK) { |
| + std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
| + return; |
| + } |
| + |
| + PaymentInstrumentProto instrument_proto; |
| + instrument_proto.set_name(instrument->name); |
| + for (const auto& method : instrument->enabled_methods) { |
| + instrument_proto.add_enabled_methods(method); |
| + } |
| + instrument_proto.set_stringified_capabilities( |
| + instrument->stringified_capabilities); |
| + |
| + std::string serialized; |
| + bool success = instrument_proto.SerializeToString(&serialized); |
| + DCHECK(success); |
| + |
| + service_worker_context_->StoreRegistrationUserData( |
| + registration->id(), registration->pattern().GetOrigin(), |
| + {{instrumentKey, serialized}}, |
| + base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + base::Passed(std::move(callback)))); |
| +} |
| + |
| +void PaymentAppDatabase::DidWritePaymentInstrument( |
| + WritePaymentInstrumentCallback callback, |
| + ServiceWorkerStatusCode status) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + return std::move(callback).Run( |
| + status == SERVICE_WORKER_OK |
| + ? PaymentHandlerStatus::SUCCESS |
| + : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
| +} |
| + |
| } // namespace content |