Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: content/browser/payments/payment_app_database.cc

Issue 2806133002: PaymentHandler: Implement set/get methods in PaymentInstruments (content) (Closed)
Patch Set: content Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 27 matching lines...) Expand all
38 option->icon = option_proto.icon(); 38 option->icon = option_proto.icon();
39 option->id = option_proto.id(); 39 option->id = option_proto.id();
40 for (const auto& method : option_proto.enabled_methods()) 40 for (const auto& method : option_proto.enabled_methods())
41 option->enabled_methods.push_back(method); 41 option->enabled_methods.push_back(method);
42 manifest->options.push_back(std::move(option)); 42 manifest->options.push_back(std::move(option));
43 } 43 }
44 44
45 return manifest; 45 return manifest;
46 } 46 }
47 47
48 PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) {
49 PaymentInstrumentProto instrument_proto;
50 if (!instrument_proto.ParseFromString(input))
51 return nullptr;
52
53 PaymentInstrumentPtr instrument = PaymentInstrument::New();
54 instrument->name = instrument_proto.name();
55 for (const auto& method : instrument_proto.enabled_methods())
56 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()
57 instrument->stringified_capabilities =
58 instrument_proto.stringified_capabilities();
59
60 return instrument;
61 }
62
48 } // namespace 63 } // namespace
49 64
50 PaymentAppDatabase::PaymentAppDatabase( 65 PaymentAppDatabase::PaymentAppDatabase(
51 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) 66 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
52 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { 67 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) {
53 DCHECK_CURRENTLY_ON(BrowserThread::IO); 68 DCHECK_CURRENTLY_ON(BrowserThread::IO);
54 } 69 }
55 70
56 PaymentAppDatabase::~PaymentAppDatabase() { 71 PaymentAppDatabase::~PaymentAppDatabase() {
57 DCHECK_CURRENTLY_ON(BrowserThread::IO); 72 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 23 matching lines...) Expand all
81 void PaymentAppDatabase::ReadAllManifests( 96 void PaymentAppDatabase::ReadAllManifests(
82 const ReadAllManifestsCallback& callback) { 97 const ReadAllManifestsCallback& callback) {
83 DCHECK_CURRENTLY_ON(BrowserThread::IO); 98 DCHECK_CURRENTLY_ON(BrowserThread::IO);
84 99
85 service_worker_context_->GetUserDataForAllRegistrations( 100 service_worker_context_->GetUserDataForAllRegistrations(
86 kPaymentAppManifestDataKey, 101 kPaymentAppManifestDataKey,
87 base::Bind(&PaymentAppDatabase::DidReadAllManifests, 102 base::Bind(&PaymentAppDatabase::DidReadAllManifests,
88 weak_ptr_factory_.GetWeakPtr(), callback)); 103 weak_ptr_factory_.GetWeakPtr(), callback));
89 } 104 }
90 105
106 void PaymentAppDatabase::ReadPaymentInstrument(
107 const GURL& scope,
108 const std::string& instrumentKey,
109 ReadPaymentInstrumentCallback callback) {
110 DCHECK_CURRENTLY_ON(BrowserThread::IO);
111
112 service_worker_context_->FindReadyRegistrationForPattern(
113 scope,
114 base::Bind(
115 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument,
116 weak_ptr_factory_.GetWeakPtr(), instrumentKey,
117 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
118 }
119
120 void PaymentAppDatabase::WritePaymentInstrument(
121 const GURL& scope,
122 const std::string& instrumentKey,
123 PaymentInstrumentPtr instrument,
124 WritePaymentInstrumentCallback callback) {
125 DCHECK_CURRENTLY_ON(BrowserThread::IO);
126
127 service_worker_context_->FindReadyRegistrationForPattern(
128 scope,
129 base::Bind(
130 &PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument,
131 weak_ptr_factory_.GetWeakPtr(), instrumentKey,
132 base::Passed(std::move(instrument)),
133 base::Passed(std::move(callback))));
134 }
135
91 void PaymentAppDatabase::DidFindRegistrationToWriteManifest( 136 void PaymentAppDatabase::DidFindRegistrationToWriteManifest(
92 payments::mojom::PaymentAppManifestPtr manifest, 137 payments::mojom::PaymentAppManifestPtr manifest,
93 const WriteManifestCallback& callback, 138 const WriteManifestCallback& callback,
94 ServiceWorkerStatusCode status, 139 ServiceWorkerStatusCode status,
95 scoped_refptr<ServiceWorkerRegistration> registration) { 140 scoped_refptr<ServiceWorkerRegistration> registration) {
96 DCHECK_CURRENTLY_ON(BrowserThread::IO); 141 DCHECK_CURRENTLY_ON(BrowserThread::IO);
97 if (status != SERVICE_WORKER_OK) { 142 if (status != SERVICE_WORKER_OK) {
98 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER); 143 callback.Run(payments::mojom::PaymentAppManifestError::NO_ACTIVE_WORKER);
99 return; 144 return;
100 } 145 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 if (!manifest) 238 if (!manifest)
194 continue; 239 continue;
195 240
196 manifests.push_back( 241 manifests.push_back(
197 ManifestWithID(item_of_raw_data.first, std::move(manifest))); 242 ManifestWithID(item_of_raw_data.first, std::move(manifest)));
198 } 243 }
199 244
200 callback.Run(std::move(manifests)); 245 callback.Run(std::move(manifests));
201 } 246 }
202 247
248 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument(
249 const std::string& instrumentKey,
250 ReadPaymentInstrumentCallback callback,
251 ServiceWorkerStatusCode status,
252 scoped_refptr<ServiceWorkerRegistration> registration) {
253 DCHECK_CURRENTLY_ON(BrowserThread::IO);
254 if (status != SERVICE_WORKER_OK) {
255 std::move(callback).Run(PaymentInstrument::New(),
256 PaymentHandlerStatus::NO_ACTIVE_WORKER);
257 return;
258 }
259
260 service_worker_context_->GetRegistrationUserData(
261 registration->id(), {instrumentKey},
262 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument,
263 weak_ptr_factory_.GetWeakPtr(),
264 base::Passed(std::move(callback))));
265 }
266
267 void PaymentAppDatabase::DidReadPaymentInstrument(
268 ReadPaymentInstrumentCallback callback,
269 const std::vector<std::string>& data,
270 ServiceWorkerStatusCode status) {
271 DCHECK_CURRENTLY_ON(BrowserThread::IO);
272 if (status != SERVICE_WORKER_OK || data.size() != 1) {
273 std::move(callback).Run(PaymentInstrument::New(),
274 PaymentHandlerStatus::NOT_FOUND);
275 return;
276 }
277
278 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]);
279 if (!instrument) {
280 std::move(callback).Run(PaymentInstrument::New(),
281 PaymentHandlerStatus::STORAGE_OPERATION_FAILED);
282 return;
283 }
284
285 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS);
286 }
287
288 void PaymentAppDatabase::DidFindRegistrationToWritePaymentInstrument(
289 const std::string& instrumentKey,
290 PaymentInstrumentPtr instrument,
291 WritePaymentInstrumentCallback callback,
292 ServiceWorkerStatusCode status,
293 scoped_refptr<ServiceWorkerRegistration> registration) {
294 DCHECK_CURRENTLY_ON(BrowserThread::IO);
295 if (status != SERVICE_WORKER_OK) {
296 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER);
297 return;
298 }
299
300 PaymentInstrumentProto instrument_proto;
301 instrument_proto.set_name(instrument->name);
302 for (const auto& method : instrument->enabled_methods) {
303 instrument_proto.add_enabled_methods(method);
304 }
305 instrument_proto.set_stringified_capabilities(
306 instrument->stringified_capabilities);
307
308 std::string serialized;
309 bool success = instrument_proto.SerializeToString(&serialized);
310 DCHECK(success);
311
312 service_worker_context_->StoreRegistrationUserData(
313 registration->id(), registration->pattern().GetOrigin(),
314 {{instrumentKey, serialized}},
315 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument,
316 weak_ptr_factory_.GetWeakPtr(),
317 base::Passed(std::move(callback))));
318 }
319
320 void PaymentAppDatabase::DidWritePaymentInstrument(
321 WritePaymentInstrumentCallback callback,
322 ServiceWorkerStatusCode status) {
323 DCHECK_CURRENTLY_ON(BrowserThread::IO);
324 return std::move(callback).Run(
325 status == SERVICE_WORKER_OK
326 ? PaymentHandlerStatus::SUCCESS
327 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED);
328 }
329
203 } // namespace content 330 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698