| 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 kMethodDataData[] = "data"; |
| 19 static const char kSupportedMethods[] = "supportedMethods"; |
| 20 static const char kSupportedNetworks[] = "supportedNetworks"; |
| 21 static const char kSupportedTypes[] = "supportedTypes"; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 PaymentMethodData::PaymentMethodData() {} |
| 26 PaymentMethodData::PaymentMethodData(const PaymentMethodData& other) = default; |
| 27 PaymentMethodData::~PaymentMethodData() = default; |
| 28 |
| 29 bool PaymentMethodData::operator==(const PaymentMethodData& other) const { |
| 30 return this->supported_methods == other.supported_methods && |
| 31 this->data == other.data && |
| 32 this->supported_networks == other.supported_networks && |
| 33 this->supported_types == other.supported_types; |
| 34 } |
| 35 |
| 36 bool PaymentMethodData::operator!=(const PaymentMethodData& other) const { |
| 37 return !(*this == other); |
| 38 } |
| 39 |
| 40 bool PaymentMethodData::FromDictionaryValue( |
| 41 const base::DictionaryValue& value) { |
| 42 this->supported_methods.clear(); |
| 43 this->supported_networks.clear(); |
| 44 this->supported_types.clear(); |
| 45 |
| 46 const base::ListValue* supported_methods_list = nullptr; |
| 47 // At least one supported method is required. |
| 48 if (!value.GetList(kSupportedMethods, &supported_methods_list) || |
| 49 supported_methods_list->GetSize() == 0) { |
| 50 return false; |
| 51 } |
| 52 for (size_t i = 0; i < supported_methods_list->GetSize(); ++i) { |
| 53 base::string16 supported_method; |
| 54 if (!supported_methods_list->GetString(i, &supported_method)) { |
| 55 return false; |
| 56 } |
| 57 this->supported_methods.push_back(supported_method); |
| 58 } |
| 59 |
| 60 // Data is optional, but if a dictionary is present, save a stringified |
| 61 // version and attempt to parse supportedNetworks/supportedTypes. |
| 62 const base::DictionaryValue* data_dict = nullptr; |
| 63 if (value.GetDictionary(kMethodDataData, &data_dict)) { |
| 64 std::string json_data; |
| 65 base::JSONWriter::Write(*data_dict, &json_data); |
| 66 this->data = base::UTF8ToUTF16(json_data); |
| 67 const base::ListValue* supported_networks_list = nullptr; |
| 68 if (data_dict->GetList(kSupportedNetworks, &supported_networks_list)) { |
| 69 for (size_t i = 0; i < supported_networks_list->GetSize(); ++i) { |
| 70 base::string16 supported_network; |
| 71 if (!supported_networks_list->GetString(i, &supported_network)) { |
| 72 return false; |
| 73 } |
| 74 this->supported_networks.push_back(supported_network); |
| 75 } |
| 76 } |
| 77 const base::ListValue* supported_types_list = nullptr; |
| 78 if (data_dict->GetList(kSupportedTypes, &supported_types_list)) { |
| 79 for (size_t i = 0; i < supported_types_list->GetSize(); ++i) { |
| 80 base::string16 supported_type; |
| 81 if (!supported_types_list->GetString(i, &supported_type)) { |
| 82 return false; |
| 83 } |
| 84 this->supported_types.push_back(supported_type); |
| 85 } |
| 86 } |
| 87 } |
| 88 return true; |
| 89 } |
| 90 |
| 91 } // namespace payments |
| OLD | NEW |