Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/payments/core/payment_method_data.h" | |
| 6 | |
| 7 #include "base/json/json_writer.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 | |
| 12 namespace payments { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // These are defined as part of the spec at: | |
| 17 // https://w3c.github.io/browser-payment-api/#paymentmethoddata-dictionary | |
| 18 static const char kBasicCardMethodName[] = "basic-card"; | |
| 19 static const char kMethodDataData[] = "data"; | |
| 20 static const char kSupportedMethods[] = "supportedMethods"; | |
| 21 static const char kSupportedNetworks[] = "supportedNetworks"; | |
| 22 static const char kSupportedTypes[] = "supportedTypes"; | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 PaymentMethodData::PaymentMethodData() {} | |
| 27 PaymentMethodData::PaymentMethodData(const PaymentMethodData& other) = default; | |
| 28 PaymentMethodData::~PaymentMethodData() = default; | |
| 29 | |
| 30 bool PaymentMethodData::operator==(const PaymentMethodData& other) const { | |
| 31 return this->supported_methods == other.supported_methods && | |
| 32 this->data == other.data && | |
| 33 this->supported_networks == other.supported_networks && | |
| 34 this->supported_types == other.supported_types; | |
| 35 } | |
| 36 | |
| 37 bool PaymentMethodData::operator!=(const PaymentMethodData& other) const { | |
| 38 return !(*this == other); | |
| 39 } | |
| 40 | |
| 41 bool PaymentMethodData::FromDictionaryValue( | |
| 42 const base::DictionaryValue& value) { | |
| 43 this->supported_methods.clear(); | |
| 44 this->supported_networks.clear(); | |
| 45 this->supported_types.clear(); | |
| 46 | |
| 47 bool has_basic_card = false; | |
| 48 const base::ListValue* supported_methods_list = nullptr; | |
| 49 // At least one supported method is required. | |
| 50 if (!value.GetList(kSupportedMethods, &supported_methods_list) || | |
| 51 supported_methods_list->GetSize() == 0) { | |
| 52 return false; | |
| 53 } | |
| 54 for (size_t i = 0; i < supported_methods_list->GetSize(); ++i) { | |
| 55 base::string16 supported_method; | |
| 56 if (!supported_methods_list->GetString(i, &supported_method)) { | |
| 57 return false; | |
| 58 } | |
| 59 if (supported_method == base::ASCIIToUTF16(kBasicCardMethodName)) | |
| 60 has_basic_card = true; | |
| 61 this->supported_methods.push_back(supported_method); | |
| 62 } | |
| 63 | |
| 64 // Data is optional, but if a dictionary is present, save a stringified | |
| 65 // version and attempt to parse supportedNetworks/supportedTypes. | |
| 66 const base::DictionaryValue* data_dict = nullptr; | |
| 67 if (value.GetDictionary(kMethodDataData, &data_dict)) { | |
| 68 std::string json_data; | |
| 69 base::JSONWriter::Write(*data_dict, &json_data); | |
| 70 this->data = base::UTF8ToUTF16(json_data); | |
| 71 const base::ListValue* supported_networks_list = nullptr; | |
| 72 if (data_dict->GetList(kSupportedNetworks, &supported_networks_list)) { | |
| 73 for (size_t i = 0; i < supported_networks_list->GetSize(); ++i) { | |
| 74 base::string16 supported_network; | |
| 75 if (!supported_networks_list->GetString(i, &supported_network)) { | |
| 76 break; // TODO(mathp): or return false? or continue? | |
|
Mathieu
2017/04/04 03:21:08
Not sure how this should validate. If item at this
Mathieu
2017/04/04 12:33:02
I went with return false, as the caller should not
Moe
2017/04/04 17:52:50
I agree. Returning false makes more sense.
| |
| 77 } | |
| 78 this->supported_networks.push_back(supported_network); | |
| 79 } | |
| 80 } | |
| 81 const base::ListValue* supported_types_list = nullptr; | |
| 82 if (data_dict->GetList(kSupportedTypes, &supported_types_list)) { | |
| 83 for (size_t i = 0; i < supported_types_list->GetSize(); ++i) { | |
| 84 base::string16 supported_type; | |
| 85 if (!supported_types_list->GetString(i, &supported_type)) { | |
| 86 break; // TODO(mathp): or return false? or continue? | |
| 87 } | |
| 88 this->supported_types.push_back(supported_type); | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 } // namespace payments | |
| OLD | NEW |