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

Unified Diff: components/payments/core/payment_method_data.cc

Issue 2797633002: [Payments] Move PaymentMethodData to components/payments/core (Closed)
Patch Set: clean 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 side-by-side diff with in-line comments
Download patch
Index: components/payments/core/payment_method_data.cc
diff --git a/components/payments/core/payment_method_data.cc b/components/payments/core/payment_method_data.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6bd753a3e1807dc4c74fcdcde91e986caf902f32
--- /dev/null
+++ b/components/payments/core/payment_method_data.cc
@@ -0,0 +1,91 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/payments/core/payment_method_data.h"
+
+#include "base/json/json_writer.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+
+namespace payments {
+
+namespace {
+
+// These are defined as part of the spec at:
+// https://w3c.github.io/browser-payment-api/#paymentmethoddata-dictionary
+static const char kMethodDataData[] = "data";
+static const char kSupportedMethods[] = "supportedMethods";
+static const char kSupportedNetworks[] = "supportedNetworks";
+static const char kSupportedTypes[] = "supportedTypes";
+
+} // namespace
+
+PaymentMethodData::PaymentMethodData() {}
+PaymentMethodData::PaymentMethodData(const PaymentMethodData& other) = default;
+PaymentMethodData::~PaymentMethodData() = default;
+
+bool PaymentMethodData::operator==(const PaymentMethodData& other) const {
+ return this->supported_methods == other.supported_methods &&
+ this->data == other.data &&
+ this->supported_networks == other.supported_networks &&
+ this->supported_types == other.supported_types;
+}
+
+bool PaymentMethodData::operator!=(const PaymentMethodData& other) const {
+ return !(*this == other);
+}
+
+bool PaymentMethodData::FromDictionaryValue(
+ const base::DictionaryValue& value) {
+ this->supported_methods.clear();
+ this->supported_networks.clear();
+ this->supported_types.clear();
+
+ const base::ListValue* supported_methods_list = nullptr;
+ // At least one supported method is required.
+ if (!value.GetList(kSupportedMethods, &supported_methods_list) ||
+ supported_methods_list->GetSize() == 0) {
+ return false;
+ }
+ for (size_t i = 0; i < supported_methods_list->GetSize(); ++i) {
+ base::string16 supported_method;
+ if (!supported_methods_list->GetString(i, &supported_method)) {
+ return false;
+ }
+ this->supported_methods.push_back(supported_method);
+ }
+
+ // Data is optional, but if a dictionary is present, save a stringified
+ // version and attempt to parse supportedNetworks/supportedTypes.
+ const base::DictionaryValue* data_dict = nullptr;
+ if (value.GetDictionary(kMethodDataData, &data_dict)) {
+ std::string json_data;
+ base::JSONWriter::Write(*data_dict, &json_data);
+ this->data = base::UTF8ToUTF16(json_data);
+ const base::ListValue* supported_networks_list = nullptr;
+ if (data_dict->GetList(kSupportedNetworks, &supported_networks_list)) {
+ for (size_t i = 0; i < supported_networks_list->GetSize(); ++i) {
+ base::string16 supported_network;
+ if (!supported_networks_list->GetString(i, &supported_network)) {
+ return false;
+ }
+ this->supported_networks.push_back(supported_network);
+ }
+ }
+ const base::ListValue* supported_types_list = nullptr;
+ if (data_dict->GetList(kSupportedTypes, &supported_types_list)) {
+ for (size_t i = 0; i < supported_types_list->GetSize(); ++i) {
+ base::string16 supported_type;
+ if (!supported_types_list->GetString(i, &supported_type)) {
+ return false;
+ }
+ this->supported_types.push_back(supported_type);
+ }
+ }
+ }
+ return true;
+}
+
+} // namespace payments
« no previous file with comments | « components/payments/core/payment_method_data.h ('k') | components/payments/core/payment_method_data_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698