1 /*
2  * Copyright (c) 2021-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 "form_dump_mgr.h"
17 
18 #include "fms_log_wrapper.h"
19 #include "form_cache_mgr.h"
20 #include "form_mgr_adapter.h"
21 #include <utility>
22 #include <map>
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 const std::string LINE_FEED = "\n";
27 
FormDumpMgr()28 FormDumpMgr::FormDumpMgr() {}
~FormDumpMgr()29 FormDumpMgr::~FormDumpMgr() {}
30 /**
31  * @brief Dump all of form storage infos.
32  * @param storageInfos Form storage infos
33  * @param formInfos Form storage dump info.
34  */
DumpStorageFormInfos(const std::vector<FormDBInfo> & storageInfos,std::string & formInfos) const35 void FormDumpMgr::DumpStorageFormInfos(const std::vector<FormDBInfo> &storageInfos, std::string &formInfos) const
36 {
37     formInfos += "  Total Storage-Form count is " + std::to_string(storageInfos.size()) + "\n" + LINE_FEED;
38     for (const auto &info : storageInfos) {
39         formInfos += "  FormId #" + std::to_string(info.formId) + "\n";
40         formInfos += "    formName [" + info.formName + "]\n";
41         formInfos += "    userId [" + std::to_string(info.userId) + "]\n";
42         formInfos += "    bundleName [" + info.bundleName + "]\n";
43         formInfos += "    moduleName [" + info.moduleName + "]\n";
44         formInfos += "    abilityName [" + info.abilityName + "]\n";
45         formInfos += "    formUserUids [";
46         for (const auto &uId : info.formUserUids) {
47             formInfos += " Uid[" + std::to_string(uId) + "] ";
48         }
49         formInfos += "]\n" + LINE_FEED;
50     }
51 }
52 /**
53  * @brief Dump all of temporary form infos.
54  * @param formRecordInfos Form record infos.
55  * @param formInfos Form dump infos.
56  */
DumpTemporaryFormInfos(const std::vector<FormRecord> & formRecordInfos,std::string & formInfos) const57 void FormDumpMgr::DumpTemporaryFormInfos(const std::vector<FormRecord> &formRecordInfos, std::string &formInfos) const
58 {
59     formInfos += "  Total Temporary-Form count is " + std::to_string(formRecordInfos.size()) + "\n" + LINE_FEED;
60     for (const auto &info : formRecordInfos) {
61         formInfos += "  FormId #" + std::to_string(info.formId) + "\n";
62         formInfos += "    formName [" + info.formName + "]\n";
63         formInfos += "    userId [" + std::to_string(info.userId) + "]\n";
64         formInfos += "    bundleName [" + info.bundleName + "]\n";
65         formInfos += "    moduleName [" + info.moduleName + "]\n";
66         formInfos += "    abilityName [" + info.abilityName + "]\n";
67         formInfos += "    type [" + std::string(info.uiSyntax == FormType::JS ? "JS" : "ArkTS") + "]\n";
68         formInfos += "    isDynamic [" + std::to_string(info.isDynamic) + "]\n";
69         formInfos += "    transparencyEnabled [" + std::to_string(info.transparencyEnabled) + "]\n";
70         formInfos += "    formUserUids [";
71         for (const auto &uId : info.formUserUids) {
72             formInfos += " Uid[" + std::to_string(uId) + "] ";
73         }
74         formInfos += "]\n" + LINE_FEED;
75     }
76 }
77 
DumpStaticBundleFormInfos(const std::vector<FormInfo> & bundleFormInfos,std::string & formInfos) const78 void FormDumpMgr::DumpStaticBundleFormInfos(const std::vector<FormInfo> &bundleFormInfos, std::string &formInfos) const
79 {
80     formInfos += "  These are static-form-infos, it means un-added form's info will also be dumped\n" + LINE_FEED;
81     for (const auto &info : bundleFormInfos) {
82         formInfos += "  bundleName #" + info.bundleName + "\n";
83         formInfos += "    moduleName [" + info.moduleName + "]\n";
84         formInfos += "    abilityName [" + info.abilityName + "]\n";
85         formInfos += "    formName [" + info.name + "]\n";
86         formInfos += "    type [" + std::string(info.uiSyntax == FormType::JS ? "JS" : "ArkTS") + "]\n";
87         formInfos += "    isDynamic [" + std::to_string(info.isDynamic) + "]\n";
88         formInfos += "    transparencyEnabled [" + std::to_string(info.transparencyEnabled) + "]\n" + LINE_FEED;
89     }
90 }
91 
92 /**
93  * @brief Dump has form visible with bundleInfo.
94  * @param tokenId *Unique identifaication of application.
95  * @param bundleName Bundle name.
96  * @param userId User ID.
97  * @param instIndex Index of application instance.
98  * @param formInfos Form dump infos.
99  */
DumpHasFormVisible(const uint32_t tokenId,const std::string & bundleName,const int32_t userId,const int32_t instIndex,std::string & formInfos) const100 void FormDumpMgr::DumpHasFormVisible(
101     const uint32_t tokenId,
102     const std::string &bundleName,
103     const int32_t userId,
104     const int32_t instIndex,
105     std::string &formInfos) const
106 {
107     formInfos += "Query whether has visible forms by bundleInfo\n";
108     formInfos += "  tokenId [" + std::to_string(tokenId) + "]\n";
109     formInfos += "    bundleName [" + bundleName + "]\n";
110     formInfos += "    userId [" + std::to_string(userId) + "]\n";
111     formInfos += "    instIndex [" + std::to_string(instIndex) + "]\n";
112     formInfos += "    hasFormVisible [" + std::to_string(FormMgrAdapter::GetInstance().HasFormVisible(tokenId)) + "]\n";
113 }
114 
115 /**
116  * @brief Dump form infos.
117  * @param formRecordInfos Form record infos.
118  * @param formInfos Form dump infos.
119  */
DumpFormInfos(const std::vector<FormRecord> & formRecordInfos,std::string & formInfos) const120 void FormDumpMgr::DumpFormInfos(const std::vector<FormRecord> &formRecordInfos, std::string &formInfos) const
121 {
122     HILOG_INFO("call");
123     for (const auto &info : formRecordInfos) {
124         DumpFormInfo(info, formInfos);
125         formInfos += LINE_FEED;
126     }
127 }
128 /**
129  * @brief Dump form infos.
130  * @param formRecordInfo Form Host record info.
131  * @param formInfo Form dump info.
132  */
DumpFormHostInfo(const FormHostRecord & formHostRecord,std::string & formInfo) const133 void FormDumpMgr::DumpFormHostInfo(const FormHostRecord &formHostRecord, std::string &formInfo) const
134 {
135     HILOG_INFO("call");
136     formInfo += "  ================FormHostRecord=================\n";
137     formInfo += "  callerUid [" + std::to_string(formHostRecord.GetCallerUid()) + "]\n";
138     formInfo += "  hostBundleName [" + formHostRecord.GetHostBundleName() + "]\n";
139 }
140 
141 /**
142  * @brief Dump form infos.
143  * @param formRecordInfo Form record info.
144  * @param formInfo Form dump info.
145  */
DumpFormInfo(const FormRecord & formRecordInfo,std::string & formInfo) const146 void FormDumpMgr::DumpFormInfo(const FormRecord &formRecordInfo, std::string &formInfo) const
147 {
148     HILOG_INFO("call");
149     formInfo += "  ================FormRecord=================\n";
150     formInfo += "  FormId [" + std::to_string(formRecordInfo.formId) + "]\n";
151     formInfo += "    formName [" + formRecordInfo.formName + "]\n";
152     formInfo += "    bundleName [" + formRecordInfo.bundleName + "]\n";
153     formInfo += "    moduleName [" + formRecordInfo.moduleName + "]\n";
154     formInfo += "    abilityName [" + formRecordInfo.abilityName + "]\n";
155     formInfo += "    isInited [" + std::to_string(formRecordInfo.isInited) + "]\n";
156     formInfo += "    needRefresh [" + std::to_string(formRecordInfo.needRefresh) + "]\n";
157     formInfo += "    isEnableUpdate [" + std::to_string(formRecordInfo.isEnableUpdate) + "]\n";
158     formInfo += "    isCountTimerRefresh [" + std::to_string(formRecordInfo.isCountTimerRefresh) + "]\n";
159     formInfo += "    specification [" + std::to_string(formRecordInfo.specification) + "]\n";
160     formInfo += "    updateDuration [" + std::to_string(formRecordInfo.updateDuration) + "]\n";
161     formInfo += "    updateAtHour [" + std::to_string(formRecordInfo.updateAtHour) + "]\n";
162     formInfo += "    updateAtMin [" + std::to_string(formRecordInfo.updateAtMin) + "]\n";
163     formInfo += "    formTempFlag [" + std::to_string(formRecordInfo.formTempFlag) + "]\n";
164     formInfo += "    formVisibleNotify [" + std::to_string(formRecordInfo.formVisibleNotify) + "]\n";
165     formInfo += "    formVisibleNotifyState [" + std::to_string(formRecordInfo.formVisibleNotifyState) + "]\n";
166     formInfo += "    formSrc [" + formRecordInfo.formSrc + "]\n";
167     formInfo += "    designWidth [" + std::to_string(formRecordInfo.formWindow.designWidth) + "]\n";
168     formInfo += "    autoDesignWidth [" + std::to_string(formRecordInfo.formWindow.autoDesignWidth) + "]\n";
169     formInfo += "    versionCode [" + std::to_string(formRecordInfo.versionCode) + "]\n";
170     formInfo += "    versionName [" + formRecordInfo.versionName + "]\n";
171     formInfo += "    compatibleVersion [" + std::to_string(formRecordInfo.compatibleVersion) + "]\n";
172     formInfo += "    userId [" + std::to_string(formRecordInfo.userId) + "]\n";
173     formInfo += "    type [" + std::string(formRecordInfo.uiSyntax == FormType::JS ? "JS" : "ArkTS") + "]\n";
174     formInfo += "    isDynamic [" + std::to_string(formRecordInfo.isDynamic) + "]\n";
175     formInfo += "    transparencyEnabled [" + std::to_string(formRecordInfo.transparencyEnabled) + "]\n";
176 
177     if (!formRecordInfo.hapSourceDirs.empty()) {
178         formInfo += "    hapSourceDirs [";
179         for (const auto &hapDir : formRecordInfo.hapSourceDirs) {
180             formInfo += " hapSourceDir[" + hapDir + "] ";
181         }
182         formInfo += "]\n";
183     }
184 
185     if (!formRecordInfo.formUserUids.empty()) {
186         formInfo += "    formUserUids [";
187         for (const auto &uId : formRecordInfo.formUserUids) {
188             formInfo += " Uid[" + std::to_string(uId) + "] ";
189         }
190         formInfo += "]\n";
191     }
192 
193     AppendBundleFormInfo(formRecordInfo, formInfo);
194     AppendRecycleStatus(formRecordInfo.recycleStatus, formInfo);
195 }
196 
197 /**
198  * @brief Dump form subscribe info.
199  * @param subscribedKeys Form subscribe key info.
200  * @param count Form received data count
201  * @param formInfo Form dump info.
202  */
DumpFormSubscribeInfo(const std::vector<std::string> & subscribedKeys,const int64_t & count,std::string & formInfo) const203 void FormDumpMgr::DumpFormSubscribeInfo(
204     const std::vector<std::string> &subscribedKeys, const int64_t &count, std::string &formInfo) const
205 {
206     formInfo += "    formDataProxy [ key [";
207     for (size_t i = 0; i < subscribedKeys.size(); i++) {
208         formInfo += " [" + subscribedKeys[i] + "]";
209     }
210     formInfo += " ] updatedCount [" + std::to_string(count) + "] ]\n";
211     formInfo += LINE_FEED;
212 }
213 
AppendRunningFormInfos(const std::string & formHostBundleName,const std::vector<RunningFormInfo> & runningFormInfos,std::string & infosResult) const214 void FormDumpMgr::AppendRunningFormInfos(const std::string &formHostBundleName,
215     const std::vector<RunningFormInfo> &runningFormInfos,
216     std::string &infosResult) const
217 {
218     HILOG_INFO("call");
219 
220     for (const auto& info : runningFormInfos) {
221         if (info.hostBundleName == formHostBundleName) {
222             infosResult += "  FormId [ " + std::to_string(info.formId) + " ] \n";
223             infosResult += "    formName [ " + info.formName + " ]\n";
224             infosResult += "    bundleName [ " + info.bundleName + " ] \n";
225             infosResult += "    moduleName [ " + info.moduleName + " ] \n";
226             infosResult += "    abilityName [ " + info.abilityName + " ] \n";
227             infosResult += "    description [ " + info.description + " ] \n";
228             infosResult += "    dimension [ " + std::to_string(info.dimension) + " ] \n";
229 
230             infosResult += "    formVisibility ";
231             switch (info.formVisiblity) {
232                 case FormVisibilityType::UNKNOWN:
233                     infosResult += "[ UNKNOWN ] \n";
234                     break;
235                 case FormVisibilityType::VISIBLE:
236                     infosResult += "[ VISIBLE ] \n";
237                     break;
238                 case FormVisibilityType::INVISIBLE:
239                     infosResult += "[ INVISIBLE ] \n";
240                     break;
241                 default:
242                     infosResult += "[ UNKNOWN_TYPE ] \n";
243                     break;
244             }
245 
246             infosResult += "    FormUsageState ";
247             switch (info.formUsageState) {
248                 case FormUsageState::USED:
249                     infosResult += "[ USED ] \n";
250                     break;
251                 case FormUsageState::UNUSED:
252                     infosResult += "[ UNUSED ] \n";
253                     break;
254                 default:
255                     infosResult += "[ UNKNOWN_TYPE ] \n";
256                     break;
257             }
258 
259             AppendFormLocation(info.formLocation, infosResult);
260             AppendRecycleStatus(info.recycleStatus, infosResult);
261             AppendBundleType(info.formBundleType, infosResult);
262             infosResult += " \n";
263         }
264     }
265 }
266 
AppendFormLocation(Constants::FormLocation formLocation,std::string & infosResult) const267 void FormDumpMgr::AppendFormLocation(Constants::FormLocation formLocation, std::string &infosResult) const
268 {
269     infosResult += "    formLocation ";
270     switch (formLocation) {
271         case Constants::FormLocation::OTHER:
272             infosResult += "[ OTHER ] \n";
273             break;
274         case Constants::FormLocation::DESKTOP:
275             infosResult += "[ DESKTOP ] \n";
276             break;
277         case Constants::FormLocation::FORM_CENTER:
278             infosResult += "[ FORM_CENTER ] \n";
279             break;
280         case Constants::FormLocation::FORM_MANAGER:
281             infosResult += "[ FORM_MANAGER ] \n";
282             break;
283         case Constants::FormLocation::NEGATIVE_SCREEN:
284             infosResult += "[ NEGATIVE_SCREEN ] \n";
285             break;
286         case Constants::FormLocation::FORM_CENTER_NEGATIVE_SCREEN:
287             infosResult += "[ FORM_CENTER_NEGATIVE_SCREEN ] \n";
288             break;
289         case Constants::FormLocation::FORM_MANAGER_NEGATIVE_SCREEN:
290             infosResult += "[ FORM_MANAGER_NEGATIVE_SCREEN ] \n";
291             break;
292         case Constants::FormLocation::SCREEN_LOCK:
293             infosResult += "[ SCREEN_LOCK ] \n";
294             break;
295         case Constants::FormLocation::AI_SUGGESTION:
296             infosResult += "[ AI_SUGGESTION ] \n";
297             break;
298         default:
299             infosResult += "[ UNKNOWN_TYPE ] \n";
300             break;
301     }
302 }
303 
304 /**
305  * @brief Dump Running form info.
306  * @param runningFormInfos Form Running Form infos.
307  * @param infosResult Running Form dump info.
308  */
DumpRunningFormInfos(const std::vector<RunningFormInfo> & runningFormInfos,std::string & infosResult) const309 void FormDumpMgr::DumpRunningFormInfos(const std::vector<RunningFormInfo> &runningFormInfos,
310     std::string &infosResult) const
311 {
312     HILOG_INFO("call");
313 
314     std::unordered_map<std::string, int> countMap;
315 
316     for (const auto& info : runningFormInfos) {
317         countMap[info.hostBundleName]++;
318     }
319 
320     for (const auto& infoPair : countMap) {
321         infosResult += "hostBundleName [ " + infoPair.first + " ]\n";
322         infosResult += "Total running form count: [ " + std::to_string(infoPair.second)  + " ] \n";
323         infosResult += "  ================RunningFormInfo=================\n";
324 
325         AppendRunningFormInfos(infoPair.first, runningFormInfos, infosResult);
326     }
327 }
328 
AppendBundleFormInfo(const FormRecord & formRecordInfo,std::string & formInfo) const329 void FormDumpMgr::AppendBundleFormInfo(const FormRecord &formRecordInfo, std::string &formInfo) const
330 {
331     FormInfo bundleFormInfo;
332     if (FormInfoMgr::GetInstance().GetFormsInfoByRecord(formRecordInfo, bundleFormInfo) != ERR_OK) {
333         formInfo += "    ERROR! Can not get formInfo from BMS! \n";
334     }
335     formInfo += "    colorMode [" + std::to_string(static_cast<int32_t>(bundleFormInfo.colorMode)) + "]\n";
336     formInfo += "    defaultDimension [" + std::to_string(bundleFormInfo.defaultDimension) + "]\n";
337     if (!bundleFormInfo.supportDimensions.empty()) {
338         formInfo += "    supportDimensions [";
339         for (const auto &dimension : bundleFormInfo.supportDimensions) {
340             formInfo += " [" + std::to_string(dimension) + "] ";
341         }
342         formInfo += "]\n";
343     }
344 }
345 
AppendRecycleStatus(const RecycleStatus recycleStatus,std::string & formInfo) const346 void FormDumpMgr::AppendRecycleStatus(const RecycleStatus recycleStatus, std::string &formInfo) const
347 {
348     formInfo += "    recycleStatus ";
349     if (recycleStatus == RecycleStatus::RECYCLABLE) {
350         formInfo += "[ RECYCLABLE ]\n";
351     } else if (recycleStatus == RecycleStatus::RECYCLED) {
352         formInfo += "[ RECYCLED ]\n";
353     } else {
354         formInfo += "[ NON_RECYCLABLE ]\n";
355     }
356 }
357 
AppendBundleType(const BundleType formBundleType,std::string & formInfo) const358 void FormDumpMgr::AppendBundleType(const BundleType formBundleType, std::string &formInfo) const
359 {
360     formInfo += "    formBundleType ";
361     if (formBundleType == BundleType::APP) {
362         formInfo += "[ APP ]\n";
363     } else if (formBundleType == BundleType::ATOMIC_SERVICE) {
364         formInfo += "[ ATOMIC_SERVICE ]\n";
365     } else {
366         formInfo += "[ INVALID ]\n";
367     }
368 }
369 }  // namespace AppExecFwk
370 }  // namespace OHOS
371