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

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

Issue 2847533003: PaymentHandler: Add a key prefix to avoid key conflicts with others. (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
11 #include "base/strings/stringprintf.h"
11 #include "content/browser/payments/payment_app.pb.h" 12 #include "content/browser/payments/payment_app.pb.h"
12 #include "content/browser/payments/payment_app_context_impl.h" 13 #include "content/browser/payments/payment_app_context_impl.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" 14 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/browser/service_worker/service_worker_registration.h" 15 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 17
17 namespace content { 18 namespace content {
18 namespace { 19 namespace {
19 20
20 using ::payments::mojom::PaymentHandlerStatus; 21 using ::payments::mojom::PaymentHandlerStatus;
21 using ::payments::mojom::PaymentInstrument; 22 using ::payments::mojom::PaymentInstrument;
22 using ::payments::mojom::PaymentInstrumentPtr; 23 using ::payments::mojom::PaymentInstrumentPtr;
23 24
24 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; 25 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData";
26 const char kPaymentInstrumentKeyPrefix[] = "PaymentInstrument:";
27
28 std::string CreatePaymentInstrumentKey(const std::string& instrument_key) {
29 return base::StringPrintf("%s%s", kPaymentInstrumentKeyPrefix,
please use gerrit instead 2017/04/27 15:37:40 Stringprintf seems like an overkill. Why not this?
30 instrument_key.c_str());
31 }
25 32
26 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( 33 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest(
27 const std::string& input) { 34 const std::string& input) {
28 PaymentAppManifestProto manifest_proto; 35 PaymentAppManifestProto manifest_proto;
29 if (!manifest_proto.ParseFromString(input)) 36 if (!manifest_proto.ParseFromString(input))
30 return nullptr; 37 return nullptr;
31 38
32 payments::mojom::PaymentAppManifestPtr manifest = 39 payments::mojom::PaymentAppManifestPtr manifest =
33 payments::mojom::PaymentAppManifest::New(); 40 payments::mojom::PaymentAppManifest::New();
34 manifest->name = manifest_proto.name(); 41 manifest->name = manifest_proto.name();
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 DeletePaymentInstrumentCallback callback, 288 DeletePaymentInstrumentCallback callback,
282 ServiceWorkerStatusCode status, 289 ServiceWorkerStatusCode status,
283 scoped_refptr<ServiceWorkerRegistration> registration) { 290 scoped_refptr<ServiceWorkerRegistration> registration) {
284 DCHECK_CURRENTLY_ON(BrowserThread::IO); 291 DCHECK_CURRENTLY_ON(BrowserThread::IO);
285 if (status != SERVICE_WORKER_OK) { 292 if (status != SERVICE_WORKER_OK) {
286 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); 293 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER);
287 return; 294 return;
288 } 295 }
289 296
290 service_worker_context_->GetRegistrationUserData( 297 service_worker_context_->GetRegistrationUserData(
291 registration->id(), {instrument_key}, 298 registration->id(), {CreatePaymentInstrumentKey(instrument_key)},
292 base::Bind(&PaymentAppDatabase::DidFindPaymentInstrument, 299 base::Bind(&PaymentAppDatabase::DidFindPaymentInstrument,
293 weak_ptr_factory_.GetWeakPtr(), registration->id(), 300 weak_ptr_factory_.GetWeakPtr(), registration->id(),
294 instrument_key, base::Passed(std::move(callback)))); 301 instrument_key, base::Passed(std::move(callback))));
295 } 302 }
296 303
297 void PaymentAppDatabase::DidFindPaymentInstrument( 304 void PaymentAppDatabase::DidFindPaymentInstrument(
298 int64_t registration_id, 305 int64_t registration_id,
299 const std::string& instrument_key, 306 const std::string& instrument_key,
300 DeletePaymentInstrumentCallback callback, 307 DeletePaymentInstrumentCallback callback,
301 const std::vector<std::string>& data, 308 const std::vector<std::string>& data,
302 ServiceWorkerStatusCode status) { 309 ServiceWorkerStatusCode status) {
303 DCHECK_CURRENTLY_ON(BrowserThread::IO); 310 DCHECK_CURRENTLY_ON(BrowserThread::IO);
304 if (status != SERVICE_WORKER_OK || data.size() != 1) { 311 if (status != SERVICE_WORKER_OK || data.size() != 1) {
305 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); 312 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND);
306 return; 313 return;
307 } 314 }
308 315
309 service_worker_context_->ClearRegistrationUserData( 316 service_worker_context_->ClearRegistrationUserData(
310 registration_id, {instrument_key}, 317 registration_id, {CreatePaymentInstrumentKey(instrument_key)},
311 base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument, 318 base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument,
312 weak_ptr_factory_.GetWeakPtr(), 319 weak_ptr_factory_.GetWeakPtr(),
313 base::Passed(std::move(callback)))); 320 base::Passed(std::move(callback))));
314 } 321 }
315 322
316 void PaymentAppDatabase::DidDeletePaymentInstrument( 323 void PaymentAppDatabase::DidDeletePaymentInstrument(
317 DeletePaymentInstrumentCallback callback, 324 DeletePaymentInstrumentCallback callback,
318 ServiceWorkerStatusCode status) { 325 ServiceWorkerStatusCode status) {
319 DCHECK_CURRENTLY_ON(BrowserThread::IO); 326 DCHECK_CURRENTLY_ON(BrowserThread::IO);
320 return std::move(callback).Run(status == SERVICE_WORKER_OK 327 return std::move(callback).Run(status == SERVICE_WORKER_OK
321 ? PaymentHandlerStatus::SUCCESS 328 ? PaymentHandlerStatus::SUCCESS
322 : PaymentHandlerStatus::NOT_FOUND); 329 : PaymentHandlerStatus::NOT_FOUND);
323 } 330 }
324 331
325 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument( 332 void PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument(
326 const std::string& instrument_key, 333 const std::string& instrument_key,
327 ReadPaymentInstrumentCallback callback, 334 ReadPaymentInstrumentCallback callback,
328 ServiceWorkerStatusCode status, 335 ServiceWorkerStatusCode status,
329 scoped_refptr<ServiceWorkerRegistration> registration) { 336 scoped_refptr<ServiceWorkerRegistration> registration) {
330 DCHECK_CURRENTLY_ON(BrowserThread::IO); 337 DCHECK_CURRENTLY_ON(BrowserThread::IO);
331 if (status != SERVICE_WORKER_OK) { 338 if (status != SERVICE_WORKER_OK) {
332 std::move(callback).Run(PaymentInstrument::New(), 339 std::move(callback).Run(PaymentInstrument::New(),
333 PaymentHandlerStatus::NO_ACTIVE_WORKER); 340 PaymentHandlerStatus::NO_ACTIVE_WORKER);
334 return; 341 return;
335 } 342 }
336 343
337 service_worker_context_->GetRegistrationUserData( 344 service_worker_context_->GetRegistrationUserData(
338 registration->id(), {instrument_key}, 345 registration->id(), {CreatePaymentInstrumentKey(instrument_key)},
339 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument, 346 base::Bind(&PaymentAppDatabase::DidReadPaymentInstrument,
340 weak_ptr_factory_.GetWeakPtr(), 347 weak_ptr_factory_.GetWeakPtr(),
341 base::Passed(std::move(callback)))); 348 base::Passed(std::move(callback))));
342 } 349 }
343 350
344 void PaymentAppDatabase::DidReadPaymentInstrument( 351 void PaymentAppDatabase::DidReadPaymentInstrument(
345 ReadPaymentInstrumentCallback callback, 352 ReadPaymentInstrumentCallback callback,
346 const std::vector<std::string>& data, 353 const std::vector<std::string>& data,
347 ServiceWorkerStatusCode status) { 354 ServiceWorkerStatusCode status) {
348 DCHECK_CURRENTLY_ON(BrowserThread::IO); 355 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 18 matching lines...) Expand all
367 HasPaymentInstrumentCallback callback, 374 HasPaymentInstrumentCallback callback,
368 ServiceWorkerStatusCode status, 375 ServiceWorkerStatusCode status,
369 scoped_refptr<ServiceWorkerRegistration> registration) { 376 scoped_refptr<ServiceWorkerRegistration> registration) {
370 DCHECK_CURRENTLY_ON(BrowserThread::IO); 377 DCHECK_CURRENTLY_ON(BrowserThread::IO);
371 if (status != SERVICE_WORKER_OK) { 378 if (status != SERVICE_WORKER_OK) {
372 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); 379 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER);
373 return; 380 return;
374 } 381 }
375 382
376 service_worker_context_->GetRegistrationUserData( 383 service_worker_context_->GetRegistrationUserData(
377 registration->id(), {instrument_key}, 384 registration->id(), {CreatePaymentInstrumentKey(instrument_key)},
378 base::Bind(&PaymentAppDatabase::DidHasPaymentInstrument, 385 base::Bind(&PaymentAppDatabase::DidHasPaymentInstrument,
379 weak_ptr_factory_.GetWeakPtr(), registration->id(), 386 weak_ptr_factory_.GetWeakPtr(), registration->id(),
380 instrument_key, base::Passed(std::move(callback)))); 387 instrument_key, base::Passed(std::move(callback))));
381 } 388 }
382 389
383 void PaymentAppDatabase::DidHasPaymentInstrument( 390 void PaymentAppDatabase::DidHasPaymentInstrument(
384 int64_t registration_id, 391 int64_t registration_id,
385 const std::string& instrument_key, 392 const std::string& instrument_key,
386 DeletePaymentInstrumentCallback callback, 393 DeletePaymentInstrumentCallback callback,
387 const std::vector<std::string>& data, 394 const std::vector<std::string>& data,
(...skipping 26 matching lines...) Expand all
414 } 421 }
415 instrument_proto.set_stringified_capabilities( 422 instrument_proto.set_stringified_capabilities(
416 instrument->stringified_capabilities); 423 instrument->stringified_capabilities);
417 424
418 std::string serialized; 425 std::string serialized;
419 bool success = instrument_proto.SerializeToString(&serialized); 426 bool success = instrument_proto.SerializeToString(&serialized);
420 DCHECK(success); 427 DCHECK(success);
421 428
422 service_worker_context_->StoreRegistrationUserData( 429 service_worker_context_->StoreRegistrationUserData(
423 registration->id(), registration->pattern().GetOrigin(), 430 registration->id(), registration->pattern().GetOrigin(),
424 {{instrument_key, serialized}}, 431 {{CreatePaymentInstrumentKey(instrument_key), serialized}},
425 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, 432 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument,
426 weak_ptr_factory_.GetWeakPtr(), 433 weak_ptr_factory_.GetWeakPtr(),
427 base::Passed(std::move(callback)))); 434 base::Passed(std::move(callback))));
428 } 435 }
429 436
430 void PaymentAppDatabase::DidWritePaymentInstrument( 437 void PaymentAppDatabase::DidWritePaymentInstrument(
431 WritePaymentInstrumentCallback callback, 438 WritePaymentInstrumentCallback callback,
432 ServiceWorkerStatusCode status) { 439 ServiceWorkerStatusCode status) {
433 DCHECK_CURRENTLY_ON(BrowserThread::IO); 440 DCHECK_CURRENTLY_ON(BrowserThread::IO);
434 return std::move(callback).Run( 441 return std::move(callback).Run(
435 status == SERVICE_WORKER_OK 442 status == SERVICE_WORKER_OK
436 ? PaymentHandlerStatus::SUCCESS 443 ? PaymentHandlerStatus::SUCCESS
437 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); 444 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED);
438 } 445 }
439 446
440 } // namespace content 447 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698