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/content/android/utility/payment_manifest_parser.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/json/json_reader.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/values.h" | |
15 #include "mojo/public/cpp/bindings/strong_binding.h" | |
16 | |
17 namespace payments { | |
18 | |
19 // static | |
20 void PaymentManifestParser::Create( | |
21 mojom::PaymentManifestParserRequest request) { | |
22 mojo::MakeStrongBinding(base::MakeUnique<PaymentManifestParser>(), | |
23 std::move(request)); | |
24 } | |
25 | |
26 // static | |
27 std::vector<mojom::PaymentManifestSectionPtr> | |
28 PaymentManifestParser::ParseIntoVector(const std::string& input) { | |
29 std::vector<mojom::PaymentManifestSectionPtr> output; | |
30 std::unique_ptr<base::Value> value(base::JSONReader::Read(input)); | |
31 if (!value) | |
32 return output; | |
33 | |
34 std::unique_ptr<base::DictionaryValue> dict = | |
35 base::DictionaryValue::From(std::move(value)); | |
36 if (!dict) | |
37 return output; | |
38 | |
39 base::ListValue* list = nullptr; | |
40 if (!dict->GetList("android", &list) || !list) | |
41 return output; | |
42 | |
43 const char* const kVersion = "version"; | |
44 const char* const kFingerprints = "sha256_cert_fingerprints"; | |
45 size_t sections_size = list->GetSize(); | |
46 for (size_t i = 0; i < sections_size; ++i) { | |
47 base::DictionaryValue* item = nullptr; | |
48 if (!list->GetDictionary(i, &item) || !item) { | |
49 output.clear(); | |
50 return output; | |
51 } | |
52 | |
53 mojom::PaymentManifestSectionPtr section = | |
54 mojom::PaymentManifestSection::New(); | |
55 section->version = 0; | |
56 | |
57 if (!item->GetString("package", §ion->package_name) || | |
58 section->package_name.empty()) { | |
59 output.clear(); | |
60 return output; | |
61 } | |
62 | |
63 if (section->package_name == "*") { | |
64 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
| |
65 if (!item->HasKey(kVersion) && !item->HasKey(kFingerprints)) { | |
66 output.push_back(std::move(section)); | |
67 } | |
68 return output; | |
69 } | |
70 | |
71 if (item->HasKey(kVersion)) { | |
72 int version = 0; | |
73 if (!item->GetInteger(kVersion, &version)) { | |
74 output.clear(); | |
75 return output; | |
76 } | |
77 | |
78 section->version = static_cast<int64_t>(version); | |
79 } | |
80 | |
81 if (item->HasKey(kFingerprints)) { | |
82 base::ListValue* fingerprints = nullptr; | |
83 if (!item->GetList(kFingerprints, &fingerprints) || !fingerprints) { | |
84 output.clear(); | |
85 return output; | |
86 } | |
87 | |
88 size_t fingerprints_size = fingerprints->GetSize(); | |
89 for (size_t j = 0; j < fingerprints_size; ++j) { | |
90 std::string fingerprint; | |
91 if (!fingerprints->GetString(j, &fingerprint) || fingerprint.empty()) { | |
92 output.clear(); | |
93 return output; | |
94 } | |
95 | |
96 section->sha256_cert_fingerprints.push_back(fingerprint); | |
97 } | |
98 } | |
99 | |
100 output.push_back(std::move(section)); | |
101 } | |
102 | |
103 return output; | |
104 } | |
105 | |
106 PaymentManifestParser::PaymentManifestParser() {} | |
107 | |
108 PaymentManifestParser::~PaymentManifestParser() {} | |
109 | |
110 void PaymentManifestParser::Parse(const std::string& content, | |
111 const ParseCallback& callback) { | |
112 callback.Run(ParseIntoVector(content)); | |
113 } | |
114 | |
115 } // namespace payments | |
OLD | NEW |