1 /*
2  * Copyright (c) 2022 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 "hidump_helper.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
20 namespace {
21 const int32_t MIN_ARGS_SIZE = 1;
22 const int32_t MAX_ARGS_SIZE = 2;
23 const int32_t FIRST_PARAM = 0;
24 const int32_t SECOND_PARAM = 1;
25 const std::string ARGS_HELP = "-h";
26 const std::string ARGS_ABILITY = "-ability";
27 const std::string ARGS_ABILITY_LIST = "-ability-list";
28 const std::string ARGS_BUNDLE = "-bundle";
29 const std::string ARGS_BUNDLE_LIST = "-bundle-list";
30 const std::string ARGS_DEVICEID = "-device";
31 const std::string ILLEGAL_INFOMATION = "The arguments are illegal and you can enter '-h' for help.\n";
32 const std::string NO_INFOMATION = "no such infomation\n";
33 
34 const std::unordered_map<std::string, HidumpFlag> ARGS_MAP = {
35     { ARGS_HELP, HidumpFlag::GET_HELP },
36     { ARGS_ABILITY, HidumpFlag::GET_ABILITY },
37     { ARGS_ABILITY_LIST, HidumpFlag::GET_ABILITY_LIST },
38     { ARGS_BUNDLE, HidumpFlag::GET_BUNDLE },
39     { ARGS_BUNDLE_LIST, HidumpFlag::GET_BUNDLE_LIST },
40     { ARGS_DEVICEID, HidumpFlag::GET_DEVICEID },
41 };
42 }
43 
HidumpHelper(const std::weak_ptr<BundleDataMgr> & dataMgr)44 HidumpHelper::HidumpHelper(const std::weak_ptr<BundleDataMgr> &dataMgr)
45     : dataMgr_(dataMgr) {}
46 
Dump(const std::vector<std::string> & args,std::string & result)47 bool HidumpHelper::Dump(const std::vector<std::string>& args, std::string &result)
48 {
49     result.clear();
50     ErrCode errCode = ERR_OK;
51     int32_t argsSize = static_cast<int32_t>(args.size());
52     switch (argsSize) {
53         case MIN_ARGS_SIZE: {
54             errCode = ProcessOneParam(args[FIRST_PARAM], result);
55             break;
56         }
57         case MAX_ARGS_SIZE: {
58             errCode = ProcessTwoParam(args[FIRST_PARAM], args[SECOND_PARAM], result);
59             break;
60         }
61         default: {
62             errCode = ERR_APPEXECFWK_HIDUMP_INVALID_ARGS;
63             break;
64         }
65     }
66 
67     bool ret = false;
68     switch (errCode) {
69         case ERR_OK: {
70             ret = true;
71             break;
72         }
73         case ERR_APPEXECFWK_HIDUMP_INVALID_ARGS: {
74             ShowIllealInfomation(result);
75             ret = true;
76             break;
77         }
78         case ERR_APPEXECFWK_HIDUMP_UNKONW: {
79             result.append(NO_INFOMATION);
80             ret = true;
81             break;
82         }
83         case ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR: {
84             ret = false;
85             break;
86         }
87         default: {
88             break;
89         }
90     }
91 
92     return ret;
93 }
94 
ProcessOneParam(const std::string & args,std::string & result)95 ErrCode HidumpHelper::ProcessOneParam(const std::string& args, std::string &result)
96 {
97     HidumpParam hidumpParam;
98     auto operatorIter = ARGS_MAP.find(args);
99     if (operatorIter != ARGS_MAP.end()) {
100         hidumpParam.hidumpFlag = operatorIter->second;
101     }
102 
103     if (hidumpParam.hidumpFlag == HidumpFlag::GET_HELP) {
104         ShowHelp(result);
105         return ERR_OK;
106     }
107 
108     return ProcessDump(hidumpParam, result);
109 }
110 
ProcessTwoParam(const std::string & firstParam,const std::string & secondParam,std::string & result)111 ErrCode HidumpHelper::ProcessTwoParam(
112     const std::string& firstParam, const std::string& secondParam, std::string &result)
113 {
114     HidumpParam hidumpParam;
115     hidumpParam.args = secondParam;
116     auto operatorIter = ARGS_MAP.find(firstParam);
117     if (operatorIter != ARGS_MAP.end()) {
118         hidumpParam.hidumpFlag = operatorIter->second;
119     }
120 
121     switch (hidumpParam.hidumpFlag) {
122         case HidumpFlag::GET_ABILITY: {
123             hidumpParam.hidumpFlag = HidumpFlag::GET_ABILITY_BY_NAME;
124             break;
125         }
126         case HidumpFlag::GET_BUNDLE: {
127             hidumpParam.hidumpFlag = HidumpFlag::GET_BUNDLE_BY_NAME;
128             break;
129         }
130         default: {
131             break;
132         }
133     }
134 
135     return ProcessDump(hidumpParam, result);
136 }
137 
ProcessDump(const HidumpParam & hidumpParam,std::string & result)138 ErrCode HidumpHelper::ProcessDump(const HidumpParam& hidumpParam, std::string &result)
139 {
140     result.clear();
141     ErrCode errCode = ERR_APPEXECFWK_HIDUMP_ERROR;
142     switch (hidumpParam.hidumpFlag) {
143         case HidumpFlag::GET_ABILITY: {
144             errCode = GetAllAbilityInfo(result);
145             break;
146         }
147         case HidumpFlag::GET_ABILITY_LIST: {
148             errCode = GetAllAbilityNameList(result);
149             break;
150         }
151         case HidumpFlag::GET_ABILITY_BY_NAME: {
152             errCode = GetAbilityInfoByName(hidumpParam.args, result);
153             break;
154         }
155         case HidumpFlag::GET_BUNDLE: {
156             errCode = GetAllBundleInfo(result);
157             break;
158         }
159         case HidumpFlag::GET_BUNDLE_LIST: {
160             errCode = GetAllBundleNameList(result);
161             break;
162         }
163         case HidumpFlag::GET_BUNDLE_BY_NAME: {
164             errCode = GetBundleInfoByName(hidumpParam.args, result);
165             break;
166         }
167         case HidumpFlag::GET_DEVICEID: {
168             errCode = GetAllDeviced(result);
169             break;
170         }
171         default: {
172             errCode = ERR_APPEXECFWK_HIDUMP_INVALID_ARGS;
173             break;
174         }
175     }
176 
177     return errCode;
178 }
179 
GetAllAbilityInfo(std::string & result)180 ErrCode HidumpHelper::GetAllAbilityInfo(std::string &result)
181 {
182     auto shareDataMgr = dataMgr_.lock();
183     if (shareDataMgr == nullptr) {
184         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
185     }
186 
187     std::vector<BundleInfo> bundleInfos;
188     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
189         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
190         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
191         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
192         bundleInfos, Constants::ANY_USERID)) {
193         APP_LOGE("get bundleInfos failed");
194         return ERR_APPEXECFWK_HIDUMP_ERROR;
195     }
196 
197     for (auto &bundleInfo : bundleInfos) {
198         for (auto &abilityInfo :  bundleInfo.abilityInfos) {
199             result.append(abilityInfo.name);
200             result.append(":\n");
201             nlohmann::json jsonObject = abilityInfo;
202             result.append(jsonObject.dump(Constants::DUMP_INDENT));
203             result.append("\n");
204         }
205     }
206 
207     APP_LOGD("get all ability info success");
208     return ERR_OK;
209 }
210 
GetAllAbilityNameList(std::string & result)211 ErrCode HidumpHelper::GetAllAbilityNameList(std::string &result)
212 {
213     auto shareDataMgr = dataMgr_.lock();
214     if (shareDataMgr == nullptr) {
215         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
216     }
217 
218     std::vector<BundleInfo> bundleInfos;
219     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
220         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
221         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
222         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
223         bundleInfos, Constants::ANY_USERID)) {
224         APP_LOGE("get bundleInfos failed");
225         return ERR_APPEXECFWK_HIDUMP_ERROR;
226     }
227 
228     for (const auto &bundleInfo : bundleInfos) {
229         for (auto abilityInfo :  bundleInfo.abilityInfos) {
230             result.append(abilityInfo.name);
231             result.append("\n");
232         }
233     }
234 
235     APP_LOGD("get all ability list info success");
236     return ERR_OK;
237 }
238 
GetAbilityInfoByName(const std::string & name,std::string & result)239 ErrCode HidumpHelper::GetAbilityInfoByName(const std::string &name, std::string &result)
240 {
241     auto shareDataMgr = dataMgr_.lock();
242     if (shareDataMgr == nullptr) {
243         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
244     }
245 
246     std::vector<BundleInfo> bundleInfos;
247     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
248         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
249         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
250         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
251         bundleInfos, Constants::ANY_USERID)) {
252         APP_LOGE("get bundleInfos failed");
253         return ERR_APPEXECFWK_HIDUMP_ERROR;
254     }
255 
256     nlohmann::json jsonObject;
257     for (const auto &bundleInfo : bundleInfos) {
258         for (auto abilityInfo :  bundleInfo.abilityInfos) {
259             if (abilityInfo.name == name) {
260                 jsonObject[abilityInfo.bundleName][abilityInfo.moduleName] = abilityInfo;
261             }
262         }
263     }
264 
265     if (jsonObject.is_discarded() || jsonObject.empty()) {
266         APP_LOGE("get ability by abilityName failed");
267         return ERR_APPEXECFWK_HIDUMP_ERROR;
268     }
269 
270     result.append(jsonObject.dump(Constants::DUMP_INDENT));
271     result.append("\n");
272     return ERR_OK;
273 }
274 
GetAllBundleInfo(std::string & result)275 ErrCode HidumpHelper::GetAllBundleInfo(std::string &result)
276 {
277     auto shareDataMgr = dataMgr_.lock();
278     if (shareDataMgr == nullptr) {
279         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
280     }
281 
282     std::vector<BundleInfo> bundleInfos;
283     if (!shareDataMgr->GetBundleInfos(static_cast<int32_t>(
284         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
285         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
286         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE),
287         bundleInfos, Constants::ANY_USERID)) {
288         APP_LOGE("get bundleInfos failed");
289         return false;
290     }
291 
292     for (auto &info : bundleInfos) {
293         result.append(info.name);
294         result.append(":\n");
295         nlohmann::json jsonObject = info;
296         jsonObject["hapModuleInfos"] = info.hapModuleInfos;
297         result.append(jsonObject.dump(Constants::DUMP_INDENT));
298         result.append("\n");
299     }
300     APP_LOGD("get all bundle info success");
301     return ERR_OK;
302 }
303 
GetAllBundleNameList(std::string & result)304 ErrCode HidumpHelper::GetAllBundleNameList(std::string &result)
305 {
306     auto shareDataMgr = dataMgr_.lock();
307     if (shareDataMgr == nullptr) {
308         APP_LOGE("shareDataMgr is nullptr");
309         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
310     }
311 
312     std::vector<std::string> bundleNames;
313     if (!shareDataMgr->GetBundleList(bundleNames, Constants::ANY_USERID)) {
314         APP_LOGE("get bundle list failed");
315         return ERR_APPEXECFWK_HIDUMP_ERROR;
316     }
317 
318     for (auto &name : bundleNames) {
319         result.append(name);
320         result.append("\n");
321     }
322 
323     return ERR_OK;
324 }
325 
GetBundleInfoByName(const std::string & name,std::string & result)326 ErrCode HidumpHelper::GetBundleInfoByName(const std::string &name, std::string &result)
327 {
328     APP_LOGD("hidump bundle info begin");
329     auto shareDataMgr = dataMgr_.lock();
330     if (shareDataMgr == nullptr) {
331         return ERR_APPEXECFWK_HIDUMP_SERVICE_ERROR;
332     }
333 
334     BundleInfo bundleInfo;
335     if (!shareDataMgr->GetBundleInfo(name,
336         BundleFlag::GET_BUNDLE_WITH_ABILITIES |
337         BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION |
338         BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
339         BundleFlag::GET_BUNDLE_WITH_HASH_VALUE, bundleInfo, Constants::ANY_USERID)) {
340         APP_LOGE("get bundleInfo(%{public}s) failed", name.c_str());
341         return ERR_APPEXECFWK_HIDUMP_ERROR;
342     }
343 
344     result.append(name);
345     result.append(":\n");
346     nlohmann::json jsonObject = bundleInfo;
347     jsonObject["hapModuleInfos"] = bundleInfo.hapModuleInfos;
348     result.append(jsonObject.dump(Constants::DUMP_INDENT));
349     result.append("\n");
350     APP_LOGD("get %{public}s bundle info success", name.c_str());
351     return ERR_OK;
352 }
353 
GetAllDeviced(std::string & result)354 ErrCode HidumpHelper::GetAllDeviced(std::string &result)
355 {
356     result = "This command is deprecated. Please use `hidumper -s 4802 -a -getTrustlist` instead.";
357     return ERR_OK;
358 }
359 
ShowHelp(std::string & result)360 void HidumpHelper::ShowHelp(std::string &result)
361 {
362     result.append("Usage:dump  <command> [options]\n")
363     .append("Description:\n")
364     .append("-ability          ")
365     .append("dump all ability infomation in the system\n")
366     .append("-ability    [abilityName]\n")
367     .append("                  dump ability list information of the specified name in the system\n")
368     .append("-ability-list     ")
369     .append("dump list of all ability names in the system\n")
370     .append("-bundle           ")
371     .append("dump all bundle infomation in the system\n")
372     .append("-bundle     [bundleName]\n")
373     .append("                  dump bundle list information of the specified name in the system\n")
374     .append("-bundle-list      ")
375     .append("dump list of all bundle names in the system\n")
376     .append("-device           ")
377     .append("dump the list of devices involved in the ability infomation in the system\n");
378 }
379 
ShowIllealInfomation(std::string & result)380 void HidumpHelper::ShowIllealInfomation(std::string &result)
381 {
382     result.append(ILLEGAL_INFOMATION);
383 }
384 }  // namespace AppExecFwk
385 }  // namespace OHOS
386