1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "distro_filter.h"
17
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21
22 #include "log.h"
23 #include "utils.h"
24
25 namespace OHOS {
26 namespace AppPackingTool {
27 namespace {
28 const std::string API_VERSION = "apiVersion";
29 const std::string SCREEN_SHAPE = "screenShape";
30 const std::string SCREEN_DENSITY = "screenDensity";
31 const std::string SCREEN_WINDOW = "screenWindow";
32 const std::string COUNTRY_CODE = "countryCode";
33 const std::string POLICY = "policy";
34 const std::string VALUE = "value";
35 }
36
IsEmpty() const37 bool PolicyValue::IsEmpty() const
38 {
39 return policy.empty();
40 }
41
ParseFromJson(std::unique_ptr<PtJson> & root)42 bool PolicyValue::ParseFromJson(std::unique_ptr<PtJson>& root)
43 {
44 if (!root) {
45 LOGE("Json root is null!");
46 return false;
47 }
48 if (root->Contains(VALUE.c_str())) {
49 std::unique_ptr<PtJson> valuesObj;
50 if (root->GetArray(VALUE.c_str(), &valuesObj) != Result::SUCCESS) {
51 LOGE("apiVersion node get %s array node failed!", VALUE.c_str());
52 return false;
53 }
54 for (int32_t i = 0; i < valuesObj->GetSize(); i++) {
55 value.push_back(valuesObj->Get(i)->GetString());
56 }
57 }
58 if (root->Contains(POLICY.c_str())) {
59 if (root->GetString(POLICY.c_str(), &policy) != Result::SUCCESS) {
60 LOGE("App node get %s failed!", POLICY.c_str());
61 return false;
62 }
63 }
64 return true;
65 }
66
ParseFromJsonApiVersion(std::unique_ptr<PtJson> & root)67 bool PolicyValue::ParseFromJsonApiVersion(std::unique_ptr<PtJson>& root)
68 {
69 if (!root) {
70 LOGE("Json root is null!");
71 return false;
72 }
73 if (root->Contains(VALUE.c_str())) {
74 std::unique_ptr<PtJson> valuesObj;
75 if (root->GetArray(VALUE.c_str(), &valuesObj) != Result::SUCCESS) {
76 LOGE("apiVersion node get %s array node failed!", VALUE.c_str());
77 return false;
78 }
79 for (int32_t i = 0; i < valuesObj->GetSize(); i++) {
80 value.push_back(std::to_string(valuesObj->Get(i)->GetInt()));
81 }
82 }
83 if (root->Contains(POLICY.c_str())) {
84 if (root->GetString(POLICY.c_str(), &policy) != Result::SUCCESS) {
85 LOGE("App node get %s failed!", POLICY.c_str());
86 return false;
87 }
88 }
89 return true;
90 }
91
ParseApiVersion(std::unique_ptr<PtJson> & root)92 bool DistroFilter::ParseApiVersion(std::unique_ptr<PtJson>& root)
93 {
94 if (!root) {
95 LOGE("root node is null!");
96 return false;
97 }
98 if (root->Contains(API_VERSION.c_str())) {
99 std::unique_ptr<PtJson> apiVersionObj;
100 if (root->GetObject(API_VERSION.c_str(), &apiVersionObj) != Result::SUCCESS) {
101 LOGE("Json root get %s node failed!", API_VERSION.c_str());
102 return false;
103 }
104 if (!apiVersion.ParseFromJsonApiVersion(apiVersionObj)) {
105 LOGE("Parse apiVersionObj failed!");
106 return false;
107 }
108 }
109 return true;
110 }
111
ParseScreenShape(std::unique_ptr<PtJson> & root)112 bool DistroFilter::ParseScreenShape(std::unique_ptr<PtJson>& root)
113 {
114 if (!root) {
115 LOGE("root node is null!");
116 return false;
117 }
118 if (root->Contains(SCREEN_SHAPE.c_str())) {
119 std::unique_ptr<PtJson> screenShapeObj;
120 if (root->GetObject(SCREEN_SHAPE.c_str(), &screenShapeObj) != Result::SUCCESS) {
121 LOGE("Json root get %s node failed!", SCREEN_SHAPE.c_str());
122 return false;
123 }
124 if (!screenShape.ParseFromJson(screenShapeObj)) {
125 LOGE("Parse screenShape failed!");
126 return false;
127 }
128 }
129 return true;
130 }
131
ParseScreenDensity(std::unique_ptr<PtJson> & root)132 bool DistroFilter::ParseScreenDensity(std::unique_ptr<PtJson>& root)
133 {
134 if (!root) {
135 LOGE("root node is null!");
136 return false;
137 }
138 if (root->Contains(SCREEN_DENSITY.c_str())) {
139 std::unique_ptr<PtJson> screenDensityObj;
140 if (root->GetObject(SCREEN_DENSITY.c_str(), &screenDensityObj) != Result::SUCCESS) {
141 LOGE("Json root get %s node failed!", SCREEN_DENSITY.c_str());
142 return false;
143 }
144 if (!screenDensity.ParseFromJson(screenDensityObj)) {
145 LOGE("Parse screenDensity failed!");
146 return false;
147 }
148 }
149 return true;
150 }
151
ParseScreenWindow(std::unique_ptr<PtJson> & root)152 bool DistroFilter::ParseScreenWindow(std::unique_ptr<PtJson>& root)
153 {
154 if (!root) {
155 LOGE("root node is null!");
156 return false;
157 }
158 if (root->Contains(SCREEN_WINDOW.c_str())) {
159 std::unique_ptr<PtJson> screenWindowObj;
160 if (root->GetObject(SCREEN_WINDOW.c_str(), &screenWindowObj) != Result::SUCCESS) {
161 LOGE("Json root get %s node failed!", SCREEN_WINDOW.c_str());
162 return false;
163 }
164 if (!screenWindow.ParseFromJson(screenWindowObj)) {
165 LOGE("Parse screenWindow failed!");
166 return false;
167 }
168 }
169 return true;
170 }
171
ParseCountryCode(std::unique_ptr<PtJson> & root)172 bool DistroFilter::ParseCountryCode(std::unique_ptr<PtJson>& root)
173 {
174 if (!root) {
175 LOGE("root node is null!");
176 return false;
177 }
178 if (root->Contains(COUNTRY_CODE.c_str())) {
179 std::unique_ptr<PtJson> countryCodeObj;
180 if (root->GetObject(COUNTRY_CODE.c_str(), &countryCodeObj) != Result::SUCCESS) {
181 LOGE("Json root get %s node failed!", COUNTRY_CODE.c_str());
182 return false;
183 }
184 if (!countryCode.ParseFromJson(countryCodeObj)) {
185 LOGE("Parse countryCode failed!");
186 return false;
187 }
188 }
189 return true;
190 }
191
ParseFromJson(std::unique_ptr<PtJson> & root)192 bool DistroFilter::ParseFromJson(std::unique_ptr<PtJson>& root)
193 {
194 if (!root) {
195 LOGE("Json root is null!");
196 return false;
197 }
198 if (!ParseApiVersion(root)) {
199 LOGE("ParseApiVersion failed!");
200 return false;
201 }
202 if (!ParseScreenShape(root)) {
203 LOGE("ParseScreenShape failed!");
204 return false;
205 }
206 if (!ParseScreenDensity(root)) {
207 LOGE("ParseScreenDensity failed!");
208 return false;
209 }
210 if (!ParseScreenWindow(root)) {
211 LOGE("ParseScreenWindow failed!");
212 return false;
213 }
214 if (!ParseCountryCode(root)) {
215 LOGE("ParseCountryCode failed!");
216 return false;
217 }
218 return true;
219 }
220
IsEmpty() const221 bool DistroFilter::IsEmpty() const
222 {
223 if (apiVersion.IsEmpty() && screenShape.IsEmpty() && screenDensity.IsEmpty() &&
224 screenWindow.IsEmpty() && countryCode.IsEmpty()) {
225 return true;
226 }
227 return false;
228 }
229
Dump() const230 std::string DistroFilter::Dump() const
231 {
232 std::string dumpStr = "";
233 if (apiVersion.policy.empty() && screenShape.policy.empty() && screenDensity.policy.empty()
234 && screenWindow.policy.empty() && countryCode.policy.empty()) {
235 return dumpStr;
236 }
237 dumpStr = "distroFilter:";
238 if (!apiVersion.policy.empty()) {
239 std::string apiVersionStr = "apiVersion: policy is " + apiVersion.policy + ", value is "
240 + Utils::ListToString(apiVersion.value);
241 dumpStr += " " + apiVersionStr;
242 }
243 if (!screenShape.policy.empty()) {
244 std::string screenShapeStr = "screenShape: policy is " + screenShape.policy + ", value is "
245 + Utils::ListToString(screenShape.value);
246 dumpStr += " " + screenShapeStr;
247 }
248 if (!screenDensity.policy.empty()) {
249 std::string screenDensityStr = "screenDensity: policy is " + screenDensity.policy + ", value is "
250 + Utils::ListToString(screenDensity.value);
251 dumpStr += " " + screenDensityStr;
252 }
253 if (!screenWindow.policy.empty()) {
254 std::string screenWindowStr = "screenWindow: policy is " + screenWindow.policy + ", value is "
255 + Utils::ListToString(screenWindow.value);
256 dumpStr += " " + screenWindowStr;
257 }
258 if (!countryCode.policy.empty()) {
259 std::string countryCodeStr = "countryCode: policy is " + countryCode.policy + ", value is "
260 + Utils::ListToString(countryCode.value);
261 dumpStr += " " + countryCodeStr;
262 }
263 return dumpStr;
264 }
265 } // namespace AppPackingTool
266 } // namespace OHOS
267