forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharc_features_parser.cc
153 lines (129 loc) · 4.96 KB
/
arc_features_parser.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2018 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/arc/arc_features_parser.h"
#include <memory>
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/post_task.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/values.h"
namespace arc {
namespace {
constexpr const base::FilePath::CharType kArcFeaturesJsonFile[] =
FILE_PATH_LITERAL("/etc/arc/features.json");
base::Optional<ArcFeatures> ParseFeaturesJson(base::StringPiece input_json) {
ArcFeatures arc_features;
int error_code;
std::string error_msg;
std::unique_ptr<base::Value> json_value =
base::JSONReader::ReadAndReturnError(input_json, base::JSON_PARSE_RFC,
&error_code, &error_msg);
if (!json_value || !json_value->is_dict()) {
LOG(ERROR) << "Error parsing feature JSON: " << error_msg;
return base::nullopt;
}
// Parse each item under features.
const base::Value* feature_list =
json_value->FindKeyOfType("features", base::Value::Type::LIST);
if (!feature_list) {
LOG(ERROR) << "No feature list in JSON.";
return base::nullopt;
}
for (auto& feature_item : feature_list->GetList()) {
const base::Value* feature_name =
feature_item.FindKeyOfType("name", base::Value::Type::STRING);
const base::Value* feature_version =
feature_item.FindKeyOfType("version", base::Value::Type::INTEGER);
if (!feature_name || feature_name->GetString().empty()) {
LOG(ERROR) << "Missing name in the feature.";
return base::nullopt;
}
if (!feature_version) {
LOG(ERROR) << "Missing version in the feature.";
return base::nullopt;
}
arc_features.feature_map.emplace(feature_name->GetString(),
feature_version->GetInt());
}
// Parse each item under unavailable_features.
const base::Value* unavailable_feature_list = json_value->FindKeyOfType(
"unavailable_features", base::Value::Type::LIST);
if (!unavailable_feature_list) {
LOG(ERROR) << "No unavailable feature list in JSON.";
return base::nullopt;
}
for (auto& feature_item : unavailable_feature_list->GetList()) {
if (!feature_item.is_string()) {
LOG(ERROR) << "Item in the unavailable feature list is not a string.";
return base::nullopt;
}
if (feature_item.GetString().empty()) {
LOG(ERROR) << "Missing name in the feature.";
return base::nullopt;
}
arc_features.unavailable_features.emplace_back(feature_item.GetString());
}
// Parse each item under properties.
const base::Value* properties =
json_value->FindKeyOfType("properties", base::Value::Type::DICTIONARY);
if (!properties) {
LOG(ERROR) << "No properties in JSON.";
return base::nullopt;
}
for (const auto& item : properties->DictItems()) {
if (!item.second.is_string()) {
LOG(ERROR) << "Item in the properties mapping is not a string.";
return base::nullopt;
}
arc_features.build_props.emplace(item.first, item.second.GetString());
}
// Parse the Play Store version
const base::Value* play_version = json_value->FindKeyOfType(
"play_store_version", base::Value::Type::STRING);
if (!play_version) {
LOG(ERROR) << "No Play Store version in JSON.";
return base::nullopt;
}
arc_features.play_store_version = play_version->GetString();
return arc_features;
}
base::Optional<ArcFeatures> ReadOnFileThread(const base::FilePath& file_path) {
DCHECK(!file_path.empty());
base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
std::string input_json;
{
base::ScopedBlockingCall scoped_blocking_call(
base::BlockingType::MAY_BLOCK);
if (!base::ReadFileToString(file_path, &input_json)) {
PLOG(ERROR) << "Cannot read file " << file_path.value()
<< " into string.";
return base::nullopt;
}
}
if (input_json.empty()) {
LOG(ERROR) << "Input JSON is empty in file " << file_path.value();
return base::nullopt;
}
return ParseFeaturesJson(input_json);
}
} // namespace
ArcFeatures::ArcFeatures() = default;
ArcFeatures::ArcFeatures(ArcFeatures&& other) = default;
ArcFeatures::~ArcFeatures() = default;
ArcFeatures& ArcFeatures::operator=(ArcFeatures&& other) = default;
void ArcFeaturesParser::GetArcFeatures(
base::OnceCallback<void(base::Optional<ArcFeatures>)> callback) {
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&ReadOnFileThread, base::FilePath(kArcFeaturesJsonFile)),
std::move(callback));
}
base::Optional<ArcFeatures> ArcFeaturesParser::ParseFeaturesJsonForTesting(
base::StringPiece input_json) {
return ParseFeaturesJson(input_json);
}
} // namespace arc