| Index: components/payments/content/android/utility/payment_manifest_parser.cc
|
| diff --git a/components/payments/content/android/utility/payment_manifest_parser.cc b/components/payments/content/android/utility/payment_manifest_parser.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9a795de6c36ae19ea7e293df91629a87a96bc538
|
| --- /dev/null
|
| +++ b/components/payments/content/android/utility/payment_manifest_parser.cc
|
| @@ -0,0 +1,115 @@
|
| +// 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/content/android/utility/payment_manifest_parser.h"
|
| +
|
| +#include <stddef.h>
|
| +
|
| +#include <memory>
|
| +#include <utility>
|
| +
|
| +#include "base/json/json_reader.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/values.h"
|
| +#include "mojo/public/cpp/bindings/strong_binding.h"
|
| +
|
| +namespace payments {
|
| +
|
| +// static
|
| +void PaymentManifestParser::Create(
|
| + mojom::PaymentManifestParserRequest request) {
|
| + mojo::MakeStrongBinding(base::MakeUnique<PaymentManifestParser>(),
|
| + std::move(request));
|
| +}
|
| +
|
| +// static
|
| +std::vector<mojom::PaymentManifestSectionPtr>
|
| +PaymentManifestParser::ParseIntoVector(const std::string& input) {
|
| + std::vector<mojom::PaymentManifestSectionPtr> output;
|
| + std::unique_ptr<base::Value> value(base::JSONReader::Read(input));
|
| + if (!value)
|
| + return output;
|
| +
|
| + std::unique_ptr<base::DictionaryValue> dict =
|
| + base::DictionaryValue::From(std::move(value));
|
| + if (!dict)
|
| + return output;
|
| +
|
| + base::ListValue* list = nullptr;
|
| + if (!dict->GetList("android", &list) || !list)
|
| + return output;
|
| +
|
| + const char* const kVersion = "version";
|
| + const char* const kFingerprints = "sha256_cert_fingerprints";
|
| + size_t size = list->GetSize();
|
| + for (size_t i = 0; i < size; ++i) {
|
| + base::DictionaryValue* item = nullptr;
|
| + if (!list->GetDictionary(i, &item) || !item) {
|
| + output.clear();
|
| + return output;
|
| + }
|
| +
|
| + mojom::PaymentManifestSectionPtr section =
|
| + mojom::PaymentManifestSection::New();
|
| + section->version = 0;
|
| +
|
| + if (!item->GetString("package", §ion->package_name) ||
|
| + section->package_name.empty()) {
|
| + output.clear();
|
| + return output;
|
| + }
|
| +
|
| + if (section->package_name == "*") {
|
| + output.clear();
|
| + if (!item->HasKey(kVersion) && !item->HasKey(kFingerprints)) {
|
| + output.push_back(std::move(section));
|
| + }
|
| + return output;
|
| + }
|
| +
|
| + if (item->HasKey(kVersion)) {
|
| + int version = 0;
|
| + if (!item->GetInteger(kVersion, &version)) {
|
| + output.clear();
|
| + return output;
|
| + }
|
| +
|
| + section->version = static_cast<int64_t>(version);
|
| + }
|
| +
|
| + if (item->HasKey(kFingerprints)) {
|
| + base::ListValue* fingerprints = nullptr;
|
| + if (!item->GetList(kFingerprints, &fingerprints) || !fingerprints) {
|
| + output.clear();
|
| + return output;
|
| + }
|
| +
|
| + size_t fingerprints_size = fingerprints->GetSize();
|
| + for (size_t j = 0; j < fingerprints_size; ++j) {
|
| + std::string fingerprint;
|
| + if (!fingerprints->GetString(j, &fingerprint) || fingerprint.empty()) {
|
| + output.clear();
|
| + return output;
|
| + }
|
| +
|
| + section->sha256_cert_fingerprints.push_back(fingerprint);
|
| + }
|
| + }
|
| +
|
| + output.push_back(std::move(section));
|
| + }
|
| +
|
| + return output;
|
| +}
|
| +
|
| +PaymentManifestParser::PaymentManifestParser() {}
|
| +
|
| +PaymentManifestParser::~PaymentManifestParser() {}
|
| +
|
| +void PaymentManifestParser::Parse(const std::string& content,
|
| + const ParseCallback& callback) {
|
| + callback.Run(ParseIntoVector(content));
|
| +}
|
| +
|
| +} // namespace payments
|
|
|