Chromium Code Reviews| 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..eb276976f64644b00874343aac66b214a48bb9bd |
| --- /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 sections_size = list->GetSize(); |
| + for (size_t i = 0; i < sections_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(); |
|
palmer
2017/03/03 22:56:31
If I understand this correctly, the first time we
please use gerrit instead
2017/03/09 18:05:34
That is intentional. We want to specify as tight o
|
| + 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 |