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 "common/util/form_dump_mgr.h"
17
18 #include <utility>
19 #include <map>
20 #include "fms_log_wrapper.h"
21 #include "common/timer_mgr/form_timer_mgr.h"
22 #include "common/util/form_trust_mgr.h"
23 #include "data_center/form_cache_mgr.h"
24 #include "data_center/form_data_mgr.h"
25 #include "form_mgr/form_mgr_adapter.h"
26 #include "form_refresh/strategy/refresh_control_mgr.h"
27 #include "status_mgr_center/form_status.h"
28 #ifdef SUPPORT_POWER
29 #include "power_mgr_client.h"
30 #endif
31
32 namespace OHOS {
33 namespace AppExecFwk {
34 const std::string LINE_FEED = "\n";
35
36 const static std::unordered_map<FormFsmStatus, std::string> formStatusMap_ = {
37 { FormFsmStatus::INIT, "[ INIT ]\n" },
38 { FormFsmStatus::RENDERED, "[ RENDERED ]\n" },
39 { FormFsmStatus::RECYCLED, "[ RECYCLED ]\n" },
40 { FormFsmStatus::RENDERING, "[ RENDERING ]\n" },
41 { FormFsmStatus::RECYCLING_DATA, "[ RECYCLING_DATA ]\n" },
42 { FormFsmStatus::RECYCLING, "[ RECYCLING ]\n" },
43 { FormFsmStatus::RECOVERING, "[ RECOVERING ]\n" },
44 { FormFsmStatus::DELETING, "[ DELETING ]\n" },
45 };
46
47 const static std::unordered_map<Constants::FormLocation, std::string> formLocationMap_ = {
48 { Constants::FormLocation::OTHER, "[ OTHER ]\n" },
49 { Constants::FormLocation::DESKTOP, "[ DESKTOP ]\n" },
50 { Constants::FormLocation::FORM_CENTER, "[ FORM_CENTER ]\n" },
51 { Constants::FormLocation::FORM_MANAGER, "[ FORM_MANAGER ]\n" },
52 { Constants::FormLocation::NEGATIVE_SCREEN, "[ NEGATIVE_SCREEN ]\n" },
53 { Constants::FormLocation::FORM_CENTER_NEGATIVE_SCREEN, "[ FORM_CENTER_NEGATIVE_SCREEN ]\n" },
54 { Constants::FormLocation::FORM_MANAGER_NEGATIVE_SCREEN, "[ FORM_MANAGER_NEGATIVE_SCREEN ]\n" },
55 { Constants::FormLocation::SCREEN_LOCK, "[ SCREEN_LOCK ]\n" },
56 { Constants::FormLocation::AI_SUGGESTION, "[ AI_SUGGESTION ]\n" },
57 };
58
59 const static std::unordered_map<FormVisibilityType, std::string> formVisibilityTypeMap_ = {
60 { FormVisibilityType::UNKNOWN, "[ UNKNOWN ] \n" },
61 { FormVisibilityType::VISIBLE, "[ VISIBLE ] \n" },
62 { FormVisibilityType::INVISIBLE, "[ INVISIBLE ] \n" },
63 };
64
FormDumpMgr()65 FormDumpMgr::FormDumpMgr() {}
~FormDumpMgr()66 FormDumpMgr::~FormDumpMgr() {}
67 /**
68 * @brief Dump all of form storage infos.
69 * @param storageInfos Form storage infos
70 * @param formInfos Form storage dump info.
71 */
DumpStorageFormInfos(const std::vector<FormDBInfo> & storageInfos,std::string & formInfos) const72 void FormDumpMgr::DumpStorageFormInfos(const std::vector<FormDBInfo> &storageInfos, std::string &formInfos) const
73 {
74 formInfos += " Total Storage-Form count is " + std::to_string(storageInfos.size()) + "\n" + LINE_FEED;
75 for (const auto &info : storageInfos) {
76 formInfos += " FormId #" + std::to_string(info.formId) + "\n";
77 formInfos += " formName [" + info.formName + "]\n";
78 formInfos += " userId [" + std::to_string(info.userId) + "]\n";
79 formInfos += " bundleName [" + info.bundleName + "]\n";
80 formInfos += " moduleName [" + info.moduleName + "]\n";
81 formInfos += " abilityName [" + info.abilityName + "]\n";
82 formInfos += " formUserUids [";
83 for (const auto &uId : info.formUserUids) {
84 formInfos += " Uid[" + std::to_string(uId) + "] ";
85 }
86 formInfos += "]\n" + LINE_FEED;
87 }
88 }
89 /**
90 * @brief Dump all of temporary form infos.
91 * @param formRecordInfos Form record infos.
92 * @param formInfos Form dump infos.
93 */
DumpTemporaryFormInfos(const std::vector<FormRecord> & formRecordInfos,std::string & formInfos) const94 void FormDumpMgr::DumpTemporaryFormInfos(const std::vector<FormRecord> &formRecordInfos, std::string &formInfos) const
95 {
96 formInfos += " Total Temporary-Form count is " + std::to_string(formRecordInfos.size()) + "\n" + LINE_FEED;
97 for (const auto &info : formRecordInfos) {
98 formInfos += " FormId #" + std::to_string(info.formId) + "\n";
99 formInfos += " formName [" + info.formName + "]\n";
100 formInfos += " userId [" + std::to_string(info.userId) + "]\n";
101 formInfos += " bundleName [" + info.bundleName + "]\n";
102 formInfos += " moduleName [" + info.moduleName + "]\n";
103 formInfos += " abilityName [" + info.abilityName + "]\n";
104 formInfos += " type [" + std::string(info.uiSyntax == FormType::JS ? "JS" : "ArkTS") + "]\n";
105 formInfos += " isDynamic [" + std::to_string(info.isDynamic) + "]\n";
106 formInfos += " transparencyEnabled [" + std::to_string(info.transparencyEnabled) + "]\n";
107 formInfos += " formUserUids [";
108 for (const auto &uId : info.formUserUids) {
109 formInfos += " Uid[" + std::to_string(uId) + "] ";
110 }
111 formInfos += "]\n" + LINE_FEED;
112 }
113 }
114
DumpStaticBundleFormInfos(const std::vector<FormInfo> & bundleFormInfos,std::string & formInfos) const115 void FormDumpMgr::DumpStaticBundleFormInfos(const std::vector<FormInfo> &bundleFormInfos, std::string &formInfos) const
116 {
117 formInfos += " These are static-form-infos, it means un-added form's info will also be dumped\n" + LINE_FEED;
118 for (const auto &info : bundleFormInfos) {
119 formInfos += " bundleName #" + info.bundleName + "\n";
120 formInfos += " moduleName [" + info.moduleName + "]\n";
121 formInfos += " abilityName [" + info.abilityName + "]\n";
122 formInfos += " formName [" + info.name + "]\n";
123 formInfos += " type [" + std::string(info.uiSyntax == FormType::JS ? "JS" : "ArkTS") + "]\n";
124 formInfos += " isDynamic [" + std::to_string(info.isDynamic) + "]\n";
125 formInfos += " transparencyEnabled [" + std::to_string(info.transparencyEnabled) + "]\n" + LINE_FEED;
126 }
127 }
128
129 /**
130 * @brief Dump has form visible with bundleInfo.
131 * @param tokenId *Unique identifaication of application.
132 * @param bundleName Bundle name.
133 * @param userId User ID.
134 * @param instIndex Index of application instance.
135 * @param formInfos Form dump infos.
136 */
DumpHasFormVisible(const uint32_t tokenId,const std::string & bundleName,const int32_t userId,const int32_t instIndex,std::string & formInfos) const137 void FormDumpMgr::DumpHasFormVisible(
138 const uint32_t tokenId,
139 const std::string &bundleName,
140 const int32_t userId,
141 const int32_t instIndex,
142 std::string &formInfos) const
143 {
144 formInfos += "Query whether has visible forms by bundleInfo\n";
145 formInfos += " tokenId [" + std::to_string(tokenId) + "]\n";
146 formInfos += " bundleName [" + bundleName + "]\n";
147 formInfos += " userId [" + std::to_string(userId) + "]\n";
148 formInfos += " instIndex [" + std::to_string(instIndex) + "]\n";
149 formInfos += " hasFormVisible [" + std::to_string(FormMgrAdapter::GetInstance().HasFormVisible(tokenId)) + "]\n";
150 }
151
152 /**
153 * @brief Dump form infos.
154 * @param formRecordInfos Form record infos.
155 * @param formInfos Form dump infos.
156 */
DumpFormInfos(const std::vector<FormRecord> & formRecordInfos,std::string & formInfos) const157 void FormDumpMgr::DumpFormInfos(const std::vector<FormRecord> &formRecordInfos, std::string &formInfos) const
158 {
159 HILOG_INFO("call");
160 for (const auto &info : formRecordInfos) {
161 DumpFormInfo(info, formInfos);
162 formInfos += LINE_FEED;
163 }
164 }
165 /**
166 * @brief Dump form infos.
167 * @param formRecordInfo Form Host record info.
168 * @param formInfo Form dump info.
169 */
DumpFormHostInfo(const FormHostRecord & formHostRecord,std::string & formInfo) const170 void FormDumpMgr::DumpFormHostInfo(const FormHostRecord &formHostRecord, std::string &formInfo) const
171 {
172 HILOG_INFO("call");
173 formInfo += " ================FormHostRecord=================\n";
174 formInfo += " callerUid [" + std::to_string(formHostRecord.GetCallerUid()) + "]\n";
175 formInfo += " hostBundleName [" + formHostRecord.GetHostBundleName() + "]\n";
176 }
177
178 /**
179 * @brief Dump form infos.
180 * @param formRecordInfo Form record info.
181 * @param formInfo Form dump info.
182 */
DumpFormInfo(const FormRecord & formRecordInfo,std::string & formInfo) const183 void FormDumpMgr::DumpFormInfo(const FormRecord &formRecordInfo, std::string &formInfo) const
184 {
185 HILOG_INFO("call");
186 formInfo += " ================FormRecord=================\n";
187 formInfo += " FormId [" + std::to_string(formRecordInfo.formId) + "]\n";
188 formInfo += " formName [" + formRecordInfo.formName + "]\n";
189 formInfo += " bundleName [" + formRecordInfo.bundleName + "]\n";
190 formInfo += " moduleName [" + formRecordInfo.moduleName + "]\n";
191 formInfo += " abilityName [" + formRecordInfo.abilityName + "]\n";
192 formInfo += " isInited [" + std::to_string(formRecordInfo.isInited) + "]\n";
193 formInfo += " needRefresh [" + std::to_string(formRecordInfo.needRefresh) + "]\n";
194 formInfo += " isEnableUpdate [" + std::to_string(formRecordInfo.isEnableUpdate) + "]\n";
195 formInfo += " isCountTimerRefresh [" + std::to_string(formRecordInfo.isCountTimerRefresh) + "]\n";
196 formInfo += " specification [" + std::to_string(formRecordInfo.specification) + "]\n";
197 formInfo += " updateDuration [" + std::to_string(formRecordInfo.updateDuration) + "]\n";
198 formInfo += " updateAtHour [" + std::to_string(formRecordInfo.updateAtHour) + "]\n";
199 formInfo += " updateAtMin [" + std::to_string(formRecordInfo.updateAtMin) + "]\n";
200 formInfo += " formTempFlag [" + std::to_string(formRecordInfo.formTempFlag) + "]\n";
201 formInfo += " formVisibleNotify [" + std::to_string(formRecordInfo.formVisibleNotify) + "]\n";
202 formInfo += " formVisibleNotifyState [" + std::to_string(formRecordInfo.formVisibleNotifyState) + "]\n";
203 formInfo += " formSrc [" + formRecordInfo.formSrc + "]\n";
204 formInfo += " designWidth [" + std::to_string(formRecordInfo.formWindow.designWidth) + "]\n";
205 formInfo += " autoDesignWidth [" + std::to_string(formRecordInfo.formWindow.autoDesignWidth) + "]\n";
206 formInfo += " versionCode [" + std::to_string(formRecordInfo.versionCode) + "]\n";
207 formInfo += " versionName [" + formRecordInfo.versionName + "]\n";
208 formInfo += " compatibleVersion [" + std::to_string(formRecordInfo.compatibleVersion) + "]\n";
209 formInfo += " userId [" + std::to_string(formRecordInfo.userId) + "]\n";
210 formInfo += " type [" + std::string(formRecordInfo.uiSyntax == FormType::JS ? "JS" : "ArkTS") + "]\n";
211 formInfo += " isDynamic [" + std::to_string(formRecordInfo.isDynamic) + "]\n";
212 formInfo += " transparencyEnabled [" + std::to_string(formRecordInfo.transparencyEnabled) + "]\n";
213
214 if (!formRecordInfo.hapSourceDirs.empty()) {
215 formInfo += " hapSourceDirs [";
216 for (const auto &hapDir : formRecordInfo.hapSourceDirs) {
217 formInfo += " hapSourceDir[" + hapDir + "] ";
218 }
219 formInfo += "]\n";
220 }
221
222 if (!formRecordInfo.formUserUids.empty()) {
223 formInfo += " formUserUids [";
224 for (const auto &uId : formRecordInfo.formUserUids) {
225 formInfo += " Uid[" + std::to_string(uId) + "] ";
226 }
227 formInfo += "]\n";
228 }
229
230 AppendBundleFormInfo(formRecordInfo, formInfo);
231 AppendFormStatus(formRecordInfo.formId, formInfo);
232 AppendFormRefreshControlPoints(formInfo, formRecordInfo.enableForm, formRecordInfo.bundleName,
233 formRecordInfo.formId);
234 }
235
236 /**
237 * @brief Dump form subscribe info.
238 * @param subscribedKeys Form subscribe key info.
239 * @param count Form received data count
240 * @param formInfo Form dump info.
241 */
DumpFormSubscribeInfo(const std::vector<std::string> & subscribedKeys,const int64_t & count,std::string & formInfo) const242 void FormDumpMgr::DumpFormSubscribeInfo(
243 const std::vector<std::string> &subscribedKeys, const int64_t &count, std::string &formInfo) const
244 {
245 formInfo += " formDataProxy [ key [";
246 for (size_t i = 0; i < subscribedKeys.size(); i++) {
247 formInfo += " [" + subscribedKeys[i] + "]";
248 }
249 formInfo += " ] updatedCount [" + std::to_string(count) + "] ]\n";
250 formInfo += LINE_FEED;
251 }
252
AppendRunningFormInfos(const std::string & formHostBundleName,const std::vector<RunningFormInfo> & runningFormInfos,std::string & infosResult) const253 void FormDumpMgr::AppendRunningFormInfos(const std::string &formHostBundleName,
254 const std::vector<RunningFormInfo> &runningFormInfos,
255 std::string &infosResult) const
256 {
257 HILOG_INFO("call");
258 std::unordered_map<std::string, std::string> liveFormStatusMap;
259 FormMgrAdapter::GetInstance().GetLiveFormStatus(liveFormStatusMap);
260 for (const auto& info : runningFormInfos) {
261 if (info.hostBundleName == formHostBundleName) {
262 infosResult += " FormId [ " + std::to_string(info.formId) + " ] \n";
263 infosResult += " formName [ " + info.formName + " ]\n";
264 infosResult += " bundleName [ " + info.bundleName + " ] \n";
265 infosResult += " moduleName [ " + info.moduleName + " ] \n";
266 infosResult += " abilityName [ " + info.abilityName + " ] \n";
267 infosResult += " description [ " + info.description + " ] \n";
268 infosResult += " dimension [ " + std::to_string(info.dimension) + " ] \n";
269
270 infosResult += " formVisibility ";
271 std::string visibilityType = "[ UNKNOWN_TYPE ] \n";
272 const auto iter = formVisibilityTypeMap_.find(info.formVisiblity);
273 if (iter != formVisibilityTypeMap_.end()) {
274 visibilityType = iter->second;
275 }
276 infosResult += visibilityType;
277
278 infosResult += " FormUsageState ";
279 switch (info.formUsageState) {
280 case FormUsageState::USED:
281 infosResult += "[ USED ] \n";
282 break;
283 case FormUsageState::UNUSED:
284 infosResult += "[ UNUSED ] \n";
285 break;
286 default:
287 infosResult += "[ UNKNOWN_TYPE ] \n";
288 break;
289 }
290
291 AppendFormLocation(info.formLocation, infosResult);
292 AppendFormStatus(info.formId, infosResult);
293 FormRecord formRecord;
294 if (FormDataMgr::GetInstance().GetFormRecord(info.formId, formRecord)) {
295 AppendFormRefreshControlPoints(infosResult, formRecord.enableForm, info.bundleName, info.formId);
296 }
297 AppendBundleType(info.formBundleType, infosResult);
298 AppendLiveFormStatus(std::to_string(info.formId), liveFormStatusMap, infosResult);
299 infosResult += " \n";
300 }
301 }
302 }
303
AppendFormLocation(Constants::FormLocation formLocation,std::string & infosResult) const304 void FormDumpMgr::AppendFormLocation(Constants::FormLocation formLocation, std::string &infosResult) const
305 {
306 infosResult += " formLocation ";
307 std::string location = "[ UNKNOWN_TYPE ] \n";
308 const auto iter = formLocationMap_.find(formLocation);
309 if (iter != formLocationMap_.end()) {
310 location = iter->second;
311 }
312 infosResult += location;
313 }
314
315 /**
316 * @brief Dump Running form info.
317 * @param runningFormInfos Form Running Form infos.
318 * @param infosResult Running Form dump info.
319 */
DumpRunningFormInfos(const std::vector<RunningFormInfo> & runningFormInfos,std::string & infosResult) const320 void FormDumpMgr::DumpRunningFormInfos(const std::vector<RunningFormInfo> &runningFormInfos,
321 std::string &infosResult) const
322 {
323 HILOG_INFO("call");
324
325 std::unordered_map<std::string, int> countMap;
326
327 for (const auto& info : runningFormInfos) {
328 countMap[info.hostBundleName]++;
329 }
330
331 for (const auto& infoPair : countMap) {
332 infosResult += "hostBundleName [ " + infoPair.first + " ]\n";
333 infosResult += "Total running form count: [ " + std::to_string(infoPair.second) + " ] \n";
334 infosResult += " ================RunningFormInfo=================\n";
335
336 AppendRunningFormInfos(infoPair.first, runningFormInfos, infosResult);
337 }
338 }
339
AppendBundleFormInfo(const FormRecord & formRecordInfo,std::string & formInfo) const340 void FormDumpMgr::AppendBundleFormInfo(const FormRecord &formRecordInfo, std::string &formInfo) const
341 {
342 FormInfo bundleFormInfo;
343 if (FormInfoMgr::GetInstance().GetFormsInfoByRecord(formRecordInfo, bundleFormInfo) != ERR_OK) {
344 formInfo += " ERROR! Can not get formInfo from BMS! \n";
345 }
346 formInfo += " colorMode [" + std::to_string(static_cast<int32_t>(bundleFormInfo.colorMode)) + "]\n";
347 formInfo += " defaultDimension [" + std::to_string(bundleFormInfo.defaultDimension) + "]\n";
348 if (!bundleFormInfo.supportDimensions.empty()) {
349 formInfo += " supportDimensions [";
350 for (const auto &dimension : bundleFormInfo.supportDimensions) {
351 formInfo += " [" + std::to_string(dimension) + "] ";
352 }
353 formInfo += "]\n";
354 }
355 }
356
AppendFormStatus(const int64_t formId,std::string & formInfo) const357 void FormDumpMgr::AppendFormStatus(const int64_t formId, std::string &formInfo) const
358 {
359 formInfo += " formStatus ";
360 FormFsmStatus status = FormStatus::GetInstance().GetFormStatus(formId);
361 const auto iter = formStatusMap_.find(status);
362 std::string formStatus = "[ UNPROCESSABLE ]\n";
363 if (iter != formStatusMap_.end()) {
364 formStatus = iter->second;
365 }
366 formInfo += formStatus;
367 }
368
AppendFormRefreshControlPoints(std::string & formInfo,const bool enableForm,const std::string & bundleName,const int64_t formId) const369 void FormDumpMgr::AppendFormRefreshControlPoints(
370 std::string &formInfo, const bool enableForm, const std::string &bundleName, const int64_t formId) const
371 {
372 formInfo += " enableForm ";
373 formInfo += "[" + std::to_string(!enableForm) + "]\n";
374
375 #ifdef SUPPORT_POWER
376 formInfo += " screenOff ";
377 bool screenOnFlag = PowerMgr::PowerMgrClient::GetInstance().IsScreenOn();
378 formInfo += "[" + std::to_string(!screenOnFlag) + "]\n";
379 #endif
380
381 formInfo += " systemOverload ";
382 bool systemOverload = RefreshControlMgr::GetInstance().IsSystemOverload();
383 formInfo += "[" + std::to_string(systemOverload) + "]\n";
384
385 formInfo += " isInBlockList ";
386 bool isTrust = FormTrustMgr::GetInstance().IsTrust(bundleName);
387 formInfo += "[" + std::to_string(!isTrust) + "]\n";
388
389 formInfo += " refreshCount ";
390 int32_t timerRefreshedCount = FormTimerMgr::GetInstance().GetRefreshCount(formId);
391 formInfo += "[" + std::to_string(timerRefreshedCount) + "]\n";
392 }
393
AppendBundleType(const BundleType formBundleType,std::string & formInfo) const394 void FormDumpMgr::AppendBundleType(const BundleType formBundleType, std::string &formInfo) const
395 {
396 formInfo += " formBundleType ";
397 if (formBundleType == BundleType::APP) {
398 formInfo += "[ APP ]\n";
399 } else if (formBundleType == BundleType::ATOMIC_SERVICE) {
400 formInfo += "[ ATOMIC_SERVICE ]\n";
401 } else {
402 formInfo += "[ INVALID ]\n";
403 }
404 }
405
AppendLiveFormStatus(const std::string & formId,const std::unordered_map<std::string,std::string> & liveFormStatusMap,std::string & formInfo) const406 void FormDumpMgr::AppendLiveFormStatus(const std::string &formId,
407 const std::unordered_map<std::string, std::string> &liveFormStatusMap, std::string &formInfo) const
408 {
409 formInfo += " liveFormStatus ";
410 auto it = liveFormStatusMap.find(formId);
411 if (it == liveFormStatusMap.end()) {
412 formInfo += "[ INACTIVE ]\n";
413 return;
414 }
415
416 std::string status = it->second;
417 auto iter = Constants::LIVE_FORM_STATUS_MAP.find(status);
418 if (iter == Constants::LIVE_FORM_STATUS_MAP.end()) {
419 formInfo += "[ INACTIVE ]\n";
420 } else {
421 formInfo += "[ " + iter->second.activeState + " ]\n";
422 }
423 }
424 } // namespace AppExecFwk
425 } // namespace OHOS
426