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 <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/optional.h" | 11 #include "base/optional.h" |
12 #include "base/time/time.h" | |
11 #include "content/browser/payments/payment_app.pb.h" | 13 #include "content/browser/payments/payment_app.pb.h" |
12 #include "content/browser/payments/payment_app_context_impl.h" | 14 #include "content/browser/payments/payment_app_context_impl.h" |
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 16 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
16 | 18 |
17 namespace content { | 19 namespace content { |
18 namespace { | 20 namespace { |
19 | 21 |
20 using ::payments::mojom::PaymentHandlerStatus; | 22 using ::payments::mojom::PaymentHandlerStatus; |
21 using ::payments::mojom::PaymentInstrument; | 23 using ::payments::mojom::PaymentInstrument; |
22 using ::payments::mojom::PaymentInstrumentPtr; | 24 using ::payments::mojom::PaymentInstrumentPtr; |
23 | 25 |
24 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; | 26 const char kPaymentAppManifestDataKey[] = "PaymentAppManifestData"; |
25 const char kPaymentInstrumentKeyPrefix[] = "PaymentInstrument:"; | 27 const char kPaymentInstrumentPrefix[] = "PaymentInstrument:"; |
28 const char kPaymentInstrumentKeyInfoPrefix[] = "PaymentInstrumentKeyInfo:"; | |
26 | 29 |
27 std::string CreatePaymentInstrumentKey(const std::string& instrument_key) { | 30 std::string CreatePaymentInstrumentKey(const std::string& instrument_key) { |
28 return kPaymentInstrumentKeyPrefix + instrument_key; | 31 return kPaymentInstrumentPrefix + instrument_key; |
32 } | |
33 | |
34 std::string CreatePaymentInstrumentKeyInfoKey( | |
35 const std::string& instrument_key) { | |
36 return kPaymentInstrumentKeyInfoPrefix + instrument_key; | |
29 } | 37 } |
30 | 38 |
31 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( | 39 payments::mojom::PaymentAppManifestPtr DeserializePaymentAppManifest( |
32 const std::string& input) { | 40 const std::string& input) { |
33 PaymentAppManifestProto manifest_proto; | 41 PaymentAppManifestProto manifest_proto; |
34 if (!manifest_proto.ParseFromString(input)) | 42 if (!manifest_proto.ParseFromString(input)) |
35 return nullptr; | 43 return nullptr; |
36 | 44 |
37 payments::mojom::PaymentAppManifestPtr manifest = | 45 payments::mojom::PaymentAppManifestPtr manifest = |
38 payments::mojom::PaymentAppManifest::New(); | 46 payments::mojom::PaymentAppManifest::New(); |
39 manifest->name = manifest_proto.name(); | 47 manifest->name = manifest_proto.name(); |
40 if (manifest_proto.has_icon()) | 48 if (manifest_proto.has_icon()) |
41 manifest->icon = manifest_proto.icon(); | 49 manifest->icon = manifest_proto.icon(); |
42 for (const auto& option_proto : manifest_proto.options()) { | 50 for (const auto& option_proto : manifest_proto.options()) { |
43 payments::mojom::PaymentAppOptionPtr option = | 51 payments::mojom::PaymentAppOptionPtr option = |
44 payments::mojom::PaymentAppOption::New(); | 52 payments::mojom::PaymentAppOption::New(); |
45 option->name = option_proto.name(); | 53 option->name = option_proto.name(); |
46 if (option_proto.has_icon()) | 54 if (option_proto.has_icon()) |
47 option->icon = option_proto.icon(); | 55 option->icon = option_proto.icon(); |
48 option->id = option_proto.id(); | 56 option->id = option_proto.id(); |
49 for (const auto& method : option_proto.enabled_methods()) | 57 for (const auto& method : option_proto.enabled_methods()) |
50 option->enabled_methods.push_back(method); | 58 option->enabled_methods.push_back(method); |
51 manifest->options.push_back(std::move(option)); | 59 manifest->options.push_back(std::move(option)); |
52 } | 60 } |
53 | 61 |
54 return manifest; | 62 return manifest; |
55 } | 63 } |
56 | 64 |
65 std::map<uint64_t, std::string> DeserializePaymentInstrumentKeyInfo( | |
66 const std::vector<std::string>& inputs) { | |
67 std::map<uint64_t, std::string> key_info; | |
68 for (const auto& input : inputs) { | |
69 PaymentInstrumentKeyInfoProto key_info_proto; | |
70 if (!key_info_proto.ParseFromString(input)) | |
71 return std::map<uint64_t, std::string>(); | |
72 | |
73 key_info.insert(std::pair<uint64_t, std::string>( | |
74 key_info_proto.insertion_order(), key_info_proto.key())); | |
75 } | |
76 | |
77 return key_info; | |
78 } | |
79 | |
57 PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) { | 80 PaymentInstrumentPtr DeserializePaymentInstrument(const std::string& input) { |
58 PaymentInstrumentProto instrument_proto; | 81 PaymentInstrumentProto instrument_proto; |
59 if (!instrument_proto.ParseFromString(input)) | 82 if (!instrument_proto.ParseFromString(input)) |
60 return nullptr; | 83 return nullptr; |
61 | 84 |
62 PaymentInstrumentPtr instrument = PaymentInstrument::New(); | 85 PaymentInstrumentPtr instrument = PaymentInstrument::New(); |
63 instrument->name = instrument_proto.name(); | 86 instrument->name = instrument_proto.name(); |
64 for (const auto& method : instrument_proto.enabled_methods()) | 87 for (const auto& method : instrument_proto.enabled_methods()) |
65 instrument->enabled_methods.push_back(method); | 88 instrument->enabled_methods.push_back(method); |
66 instrument->stringified_capabilities = | 89 instrument->stringified_capabilities = |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 156 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
134 | 157 |
135 service_worker_context_->FindReadyRegistrationForPattern( | 158 service_worker_context_->FindReadyRegistrationForPattern( |
136 scope, | 159 scope, |
137 base::Bind( | 160 base::Bind( |
138 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, | 161 &PaymentAppDatabase::DidFindRegistrationToReadPaymentInstrument, |
139 weak_ptr_factory_.GetWeakPtr(), instrument_key, | 162 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
140 base::Passed(std::move(callback)))); | 163 base::Passed(std::move(callback)))); |
141 } | 164 } |
142 | 165 |
166 void PaymentAppDatabase::KeysOfPaymentInstruments( | |
167 const GURL& scope, | |
168 KeysOfPaymentInstrumentsCallback callback) { | |
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
170 | |
171 service_worker_context_->FindReadyRegistrationForPattern( | |
172 scope, base::Bind(&PaymentAppDatabase::DidFindRegistrationToGetKeys, | |
173 weak_ptr_factory_.GetWeakPtr(), | |
174 base::Passed(std::move(callback)))); | |
175 } | |
176 | |
143 void PaymentAppDatabase::HasPaymentInstrument( | 177 void PaymentAppDatabase::HasPaymentInstrument( |
144 const GURL& scope, | 178 const GURL& scope, |
145 const std::string& instrument_key, | 179 const std::string& instrument_key, |
146 HasPaymentInstrumentCallback callback) { | 180 HasPaymentInstrumentCallback callback) { |
147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
148 | 182 |
149 service_worker_context_->FindReadyRegistrationForPattern( | 183 service_worker_context_->FindReadyRegistrationForPattern( |
150 scope, | 184 scope, |
151 base::Bind(&PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument, | 185 base::Bind(&PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument, |
152 weak_ptr_factory_.GetWeakPtr(), instrument_key, | 186 weak_ptr_factory_.GetWeakPtr(), instrument_key, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 DeletePaymentInstrumentCallback callback, | 339 DeletePaymentInstrumentCallback callback, |
306 const std::vector<std::string>& data, | 340 const std::vector<std::string>& data, |
307 ServiceWorkerStatusCode status) { | 341 ServiceWorkerStatusCode status) { |
308 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 342 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
309 if (status != SERVICE_WORKER_OK || data.size() != 1) { | 343 if (status != SERVICE_WORKER_OK || data.size() != 1) { |
310 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); | 344 std::move(callback).Run(PaymentHandlerStatus::NOT_FOUND); |
311 return; | 345 return; |
312 } | 346 } |
313 | 347 |
314 service_worker_context_->ClearRegistrationUserData( | 348 service_worker_context_->ClearRegistrationUserData( |
315 registration_id, {CreatePaymentInstrumentKey(instrument_key)}, | 349 registration_id, |
350 {CreatePaymentInstrumentKey(instrument_key), | |
351 CreatePaymentInstrumentKeyInfoKey(instrument_key)}, | |
316 base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument, | 352 base::Bind(&PaymentAppDatabase::DidDeletePaymentInstrument, |
317 weak_ptr_factory_.GetWeakPtr(), | 353 weak_ptr_factory_.GetWeakPtr(), |
318 base::Passed(std::move(callback)))); | 354 base::Passed(std::move(callback)))); |
319 } | 355 } |
320 | 356 |
321 void PaymentAppDatabase::DidDeletePaymentInstrument( | 357 void PaymentAppDatabase::DidDeletePaymentInstrument( |
322 DeletePaymentInstrumentCallback callback, | 358 DeletePaymentInstrumentCallback callback, |
323 ServiceWorkerStatusCode status) { | 359 ServiceWorkerStatusCode status) { |
324 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 360 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
325 return std::move(callback).Run(status == SERVICE_WORKER_OK | 361 return std::move(callback).Run(status == SERVICE_WORKER_OK |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); | 396 PaymentInstrumentPtr instrument = DeserializePaymentInstrument(data[0]); |
361 if (!instrument) { | 397 if (!instrument) { |
362 std::move(callback).Run(PaymentInstrument::New(), | 398 std::move(callback).Run(PaymentInstrument::New(), |
363 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 399 PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
364 return; | 400 return; |
365 } | 401 } |
366 | 402 |
367 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); | 403 std::move(callback).Run(std::move(instrument), PaymentHandlerStatus::SUCCESS); |
368 } | 404 } |
369 | 405 |
406 void PaymentAppDatabase::DidFindRegistrationToGetKeys( | |
407 KeysOfPaymentInstrumentsCallback callback, | |
408 ServiceWorkerStatusCode status, | |
409 scoped_refptr<ServiceWorkerRegistration> registration) { | |
410 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
411 if (status != SERVICE_WORKER_OK) { | |
412 std::move(callback).Run(std::vector<std::string>(), | |
413 PaymentHandlerStatus::NO_ACTIVE_WORKER); | |
414 return; | |
415 } | |
416 | |
417 service_worker_context_->GetRegistrationUserDataByKeyPrefix( | |
418 registration->id(), {kPaymentInstrumentKeyInfoPrefix}, | |
419 base::Bind(&PaymentAppDatabase::DidGetKeysOfPaymentInstruments, | |
420 weak_ptr_factory_.GetWeakPtr(), | |
421 base::Passed(std::move(callback)))); | |
422 } | |
423 | |
424 void PaymentAppDatabase::DidGetKeysOfPaymentInstruments( | |
425 KeysOfPaymentInstrumentsCallback callback, | |
426 const std::vector<std::string>& data, | |
427 ServiceWorkerStatusCode status) { | |
428 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
429 if (status != SERVICE_WORKER_OK) { | |
430 std::move(callback).Run(std::vector<std::string>(), | |
431 PaymentHandlerStatus::NOT_FOUND); | |
432 return; | |
433 } | |
434 | |
435 std::vector<std::string> keys; | |
436 for (const auto& key_info : DeserializePaymentInstrumentKeyInfo(data)) { | |
437 keys.push_back(key_info.second); | |
438 } | |
439 | |
440 std::move(callback).Run(keys, PaymentHandlerStatus::SUCCESS); | |
441 } | |
442 | |
370 void PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument( | 443 void PaymentAppDatabase::DidFindRegistrationToHasPaymentInstrument( |
371 const std::string& instrument_key, | 444 const std::string& instrument_key, |
372 HasPaymentInstrumentCallback callback, | 445 HasPaymentInstrumentCallback callback, |
373 ServiceWorkerStatusCode status, | 446 ServiceWorkerStatusCode status, |
374 scoped_refptr<ServiceWorkerRegistration> registration) { | 447 scoped_refptr<ServiceWorkerRegistration> registration) { |
375 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 448 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
376 if (status != SERVICE_WORKER_OK) { | 449 if (status != SERVICE_WORKER_OK) { |
377 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); | 450 std::move(callback).Run(PaymentHandlerStatus::NO_ACTIVE_WORKER); |
378 return; | 451 return; |
379 } | 452 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 } | 486 } |
414 | 487 |
415 PaymentInstrumentProto instrument_proto; | 488 PaymentInstrumentProto instrument_proto; |
416 instrument_proto.set_name(instrument->name); | 489 instrument_proto.set_name(instrument->name); |
417 for (const auto& method : instrument->enabled_methods) { | 490 for (const auto& method : instrument->enabled_methods) { |
418 instrument_proto.add_enabled_methods(method); | 491 instrument_proto.add_enabled_methods(method); |
419 } | 492 } |
420 instrument_proto.set_stringified_capabilities( | 493 instrument_proto.set_stringified_capabilities( |
421 instrument->stringified_capabilities); | 494 instrument->stringified_capabilities); |
422 | 495 |
423 std::string serialized; | 496 std::string serialized_instrument; |
424 bool success = instrument_proto.SerializeToString(&serialized); | 497 bool success = instrument_proto.SerializeToString(&serialized_instrument); |
zino
2017/05/03 14:30:33
In the original CL, this line was in DCHECK as fol
please use gerrit instead
2017/05/03 14:43:30
DCHECK is disabled in release mode (a.k.a., in pro
| |
498 DCHECK(success); | |
499 | |
500 PaymentInstrumentKeyInfoProto key_info_proto; | |
501 key_info_proto.set_key(instrument_key); | |
502 key_info_proto.set_insertion_order(base::Time::Now().ToInternalValue()); | |
brucedawson
2017/05/17 21:50:01
I'm unclear about the purpose of this usage of bas
| |
503 | |
504 std::string serialized_key_info; | |
505 success = key_info_proto.SerializeToString(&serialized_key_info); | |
zino
2017/05/03 14:30:33
ditto
| |
425 DCHECK(success); | 506 DCHECK(success); |
426 | 507 |
427 service_worker_context_->StoreRegistrationUserData( | 508 service_worker_context_->StoreRegistrationUserData( |
428 registration->id(), registration->pattern().GetOrigin(), | 509 registration->id(), registration->pattern().GetOrigin(), |
429 {{CreatePaymentInstrumentKey(instrument_key), serialized}}, | 510 {{CreatePaymentInstrumentKey(instrument_key), serialized_instrument}, |
511 {CreatePaymentInstrumentKeyInfoKey(instrument_key), | |
512 serialized_key_info}}, | |
430 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, | 513 base::Bind(&PaymentAppDatabase::DidWritePaymentInstrument, |
431 weak_ptr_factory_.GetWeakPtr(), | 514 weak_ptr_factory_.GetWeakPtr(), |
432 base::Passed(std::move(callback)))); | 515 base::Passed(std::move(callback)))); |
433 } | 516 } |
434 | 517 |
435 void PaymentAppDatabase::DidWritePaymentInstrument( | 518 void PaymentAppDatabase::DidWritePaymentInstrument( |
436 WritePaymentInstrumentCallback callback, | 519 WritePaymentInstrumentCallback callback, |
437 ServiceWorkerStatusCode status) { | 520 ServiceWorkerStatusCode status) { |
438 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 521 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
439 return std::move(callback).Run( | 522 return std::move(callback).Run( |
440 status == SERVICE_WORKER_OK | 523 status == SERVICE_WORKER_OK |
441 ? PaymentHandlerStatus::SUCCESS | 524 ? PaymentHandlerStatus::SUCCESS |
442 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); | 525 : PaymentHandlerStatus::STORAGE_OPERATION_FAILED); |
443 } | 526 } |
444 | 527 |
445 } // namespace content | 528 } // namespace content |
OLD | NEW |