1 /*
2 * Copyright (c) 2021-2023 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_data_mgr.h"
17
18 #include <cinttypes>
19 #include <type_traits>
20
21 #include "fms_log_wrapper.h"
22 #include "form_bms_helper.h"
23 #include "form_cache_mgr.h"
24 #include "form_constants.h"
25 #include "form_data_proxy_mgr.h"
26 #include "form_db_cache.h"
27 #include "form_mgr_errors.h"
28 #include "form_observer_record.h"
29 #include "form_provider_mgr.h"
30 #include "form_render_mgr.h"
31 #include "form_task_mgr.h"
32 #include "form_util.h"
33 #include "form_xml_parser.h"
34 #include "ipc_skeleton.h"
35 #include "js_form_state_observer_interface.h"
36 #include "running_form_info.h"
37
38 namespace OHOS {
39 namespace AppExecFwk {
FormDataMgr()40 FormDataMgr::FormDataMgr()
41 {
42 HILOG_INFO("create form data manager instance");
43 }
~FormDataMgr()44 FormDataMgr::~FormDataMgr()
45 {
46 HILOG_INFO("destroy form data manager instance");
47 }
48
49 /**
50 * @brief Allot form info by item info.
51 * @param formInfo Form item info.
52 * @param callingUid The UID of the proxy.
53 * @param userId User ID.
54 * @return Returns form record.
55 */
AllotFormRecord(const FormItemInfo & formInfo,const int callingUid,const int32_t userId)56 FormRecord FormDataMgr::AllotFormRecord(const FormItemInfo &formInfo, const int callingUid, const int32_t userId)
57 {
58 HILOG_INFO("allot form info");
59 if (formInfo.IsTemporaryForm() && !ExistTempForm(formInfo.GetFormId())) {
60 std::lock_guard<std::mutex> lock(formTempMutex_);
61 tempForms_.emplace_back(formInfo.GetFormId());
62 }
63 FormRecord record;
64 {
65 std::lock_guard<std::mutex> lock(formRecordMutex_);
66 if (formRecords_.empty()) { // formRecords_ is empty, create a new one
67 HILOG_DEBUG("form info not exist");
68 record = CreateFormRecord(formInfo, callingUid, userId);
69 formRecords_.emplace(formInfo.GetFormId(), record);
70 } else {
71 auto info = formRecords_.find(formInfo.GetFormId());
72 if (info == formRecords_.end()) {
73 HILOG_DEBUG("form info not find");
74 record = CreateFormRecord(formInfo, callingUid, userId);
75 formRecords_.emplace(formInfo.GetFormId(), record);
76 } else {
77 record = info->second;
78 }
79 }
80 }
81 HILOG_INFO("%{public}s end", __func__);
82 return record;
83 }
84 /**
85 * @brief Delete form js info by form record.
86 * @param formId The Id of the form.
87 * @return Returns true if this function is successfully called; returns false otherwise.
88 */
DeleteFormRecord(const int64_t formId)89 bool FormDataMgr::DeleteFormRecord(const int64_t formId)
90 {
91 HILOG_INFO("delete form record");
92 std::lock_guard<std::mutex> lock(formRecordMutex_);
93 auto iter = formRecords_.find(formId);
94 if (iter == formRecords_.end()) {
95 HILOG_ERROR("form record is not exist");
96 return false;
97 }
98 formRecords_.erase(iter);
99 return true;
100 }
101 /**
102 * @brief Allot form host record by caller token.
103 * @param info The form item info.
104 * @param callerToken callerToken
105 * @param formId The Id of the form.
106 * @param callingUid The UID of the proxy.
107 * @return Returns true if this function is successfully called; returns false otherwise.
108 */
AllotFormHostRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,const int64_t formId,const int callingUid)109 bool FormDataMgr::AllotFormHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
110 const int64_t formId, const int callingUid)
111 {
112 HILOG_INFO("allot form Host info");
113 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
114 for (auto &record : clientRecords_) {
115 if (callerToken == record.GetFormHostClient()) {
116 record.AddForm(formId);
117 HILOG_INFO("%{public}s end", __func__);
118 return true;
119 }
120 }
121 FormHostRecord hostRecord;
122 bool isCreated = CreateHostRecord(info, callerToken, callingUid, hostRecord);
123 if (isCreated) {
124 hostRecord.AddForm(formId);
125 clientRecords_.emplace_back(hostRecord);
126 HILOG_INFO("%{public}s end", __func__);
127 return true;
128 }
129 HILOG_INFO("%{public}s end", __func__);
130 return false;
131 }
132 /**
133 * @brief Create host record.
134 * @param info The form item info.
135 * @param callerToken The UID of the proxy.
136 * @param callingUid The UID of the proxy.
137 * @param record The form host record.
138 * @return Returns true if this function is successfully called; returns false otherwise.
139 */
CreateHostRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,const int callingUid,FormHostRecord & record)140 bool FormDataMgr::CreateHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
141 const int callingUid, FormHostRecord& record)
142 {
143 if (callerToken == nullptr) {
144 HILOG_ERROR("%{public}s, invalid param", __func__);
145 return false;
146 }
147
148 record = FormHostRecord::CreateRecord(info, callerToken, callingUid);
149 return true;
150 }
151 /**
152 * @brief Create form record.
153 * @param formInfo The form item info.
154 * @param callingUid The UID of the proxy.
155 * @param userId User ID.
156 * @return Form record.
157 */
CreateFormRecord(const FormItemInfo & formInfo,const int callingUid,const int32_t userId) const158 FormRecord FormDataMgr::CreateFormRecord(const FormItemInfo &formInfo, const int callingUid, const int32_t userId) const
159 {
160 HILOG_INFO("create form info");
161 FormRecord newRecord;
162 newRecord.formId = formInfo.GetFormId();
163 newRecord.userId = userId;
164 newRecord.providerUserId = FormUtil::GetCurrentAccountId();
165 newRecord.packageName = formInfo.GetPackageName();
166 newRecord.bundleName = formInfo.GetProviderBundleName();
167 newRecord.moduleName = formInfo.GetModuleName();
168 newRecord.abilityName = formInfo.GetAbilityName();
169 newRecord.formName = formInfo.GetFormName();
170 newRecord.specification = formInfo.GetSpecificationId();
171 newRecord.isEnableUpdate = formInfo.IsEnableUpdateFlag();
172 newRecord.formTempFlag = formInfo.IsTemporaryForm();
173 newRecord.formVisibleNotify = formInfo.IsFormVisibleNotify();
174 newRecord.jsFormCodePath = formInfo.GetHapSourceByModuleName(newRecord.moduleName);
175 newRecord.formSrc = formInfo.GetFormSrc();
176 newRecord.formWindow = formInfo.GetFormWindow();
177 newRecord.versionName = formInfo.GetVersionName();
178 newRecord.versionCode = formInfo.GetVersionCode();
179 newRecord.compatibleVersion = formInfo.GetCompatibleVersion();
180 newRecord.formVisibleNotifyState = 0;
181 newRecord.type = formInfo.GetType();
182 newRecord.uiSyntax = formInfo.GetUiSyntax();
183 newRecord.isDynamic = formInfo.IsDynamic();
184 newRecord.isSystemApp = formInfo.GetSystemAppFlag();
185 if (newRecord.isEnableUpdate) {
186 ParseUpdateConfig(newRecord, formInfo);
187 }
188 if (std::find(newRecord.formUserUids.begin(), newRecord.formUserUids.end(),
189 callingUid) == newRecord.formUserUids.end()) {
190 newRecord.formUserUids.emplace_back(callingUid);
191 }
192 newRecord.isDataProxy = formInfo.GetDataProxyFlag();
193
194 formInfo.GetHapSourceDirs(newRecord.hapSourceDirs);
195 HILOG_INFO("%{public}s end", __func__);
196 return newRecord;
197 }
198 /**
199 * @brief Create form js info by form record.
200 * @param formId The Id of the form.
201 * @param record Form record.
202 * @param formInfo Js form info.
203 */
CreateFormJsInfo(const int64_t formId,const FormRecord & record,FormJsInfo & formInfo)204 void FormDataMgr::CreateFormJsInfo(const int64_t formId, const FormRecord &record, FormJsInfo &formInfo)
205 {
206 formInfo.formId = formId;
207 formInfo.bundleName = record.bundleName;
208 formInfo.abilityName = record.abilityName;
209 formInfo.formName = record.formName;
210 formInfo.moduleName = record.moduleName;
211 formInfo.formTempFlag = record.formTempFlag;
212 formInfo.jsFormCodePath = record.jsFormCodePath;
213 formInfo.formSrc = record.formSrc;
214 formInfo.formWindow = record.formWindow;
215 formInfo.versionCode = record.versionCode;
216 formInfo.versionName = record.versionName;
217 formInfo.compatibleVersion = record.compatibleVersion;
218 formInfo.type = record.type;
219 formInfo.uiSyntax = record.uiSyntax;
220 formInfo.isDynamic = record.isDynamic;
221 }
222
SetConfigMap(const std::map<std::string,int32_t> & configMap)223 void FormDataMgr::SetConfigMap(const std::map<std::string, int32_t> &configMap)
224 {
225 std::lock_guard<std::mutex> lock(formConfigMapMutex_);
226 formConfigMap_ = configMap;
227 }
228
GetConfigParamFormMap(const std::string & key,int32_t & value) const229 void FormDataMgr::GetConfigParamFormMap(const std::string &key, int32_t &value) const
230 {
231 std::lock_guard<std::mutex> lock(formConfigMapMutex_);
232 if (formConfigMap_.empty()) {
233 HILOG_ERROR("the config map is empty.");
234 return;
235 }
236 auto iter = formConfigMap_.find(key);
237 if (iter == formConfigMap_.end()) {
238 HILOG_ERROR("no corresponding value found, use the default value.");
239 return;
240 }
241 value = iter->second;
242 HILOG_INFO("found config parameter, key: %{public}s, value:%{public}d.", key.c_str(), value);
243 }
244
245 /**
246 * @brief Check temp form count is max.
247 * @return Returns ERR_OK if the temp form not reached; returns ERR_MAX_SYSTEM_TEMP_FORMS is reached.
248 */
CheckTempEnoughForm() const249 int FormDataMgr::CheckTempEnoughForm() const
250 {
251 int32_t maxTempSize = Constants::MAX_TEMP_FORMS;
252 const std::string maxTempSizeKey = Constants::MAX_TEMP_FORM_SIZE;
253 GetConfigParamFormMap(maxTempSizeKey, maxTempSize);
254 maxTempSize = ((maxTempSize > Constants::MAX_TEMP_FORMS) || (maxTempSize < 0)) ?
255 Constants::MAX_TEMP_FORMS : maxTempSize;
256 HILOG_DEBUG("maxTempSize:%{public}d", maxTempSize);
257
258 std::lock_guard<std::mutex> lock(formTempMutex_);
259 if (static_cast<int32_t>(tempForms_.size()) >= maxTempSize) {
260 HILOG_WARN("already exist %{public}d temp forms in system", maxTempSize);
261 return ERR_APPEXECFWK_FORM_MAX_SYSTEM_TEMP_FORMS;
262 }
263 return ERR_OK;
264 }
265 /**
266 * @brief Check form count is max.
267 * @param callingUid The UID of the proxy.
268 * @param currentUserId The current userId.
269 * @return Returns true if this function is successfully called; returns false otherwise.
270 */
CheckEnoughForm(const int callingUid,const int32_t currentUserId) const271 int FormDataMgr::CheckEnoughForm(const int callingUid, const int32_t currentUserId) const
272 {
273 HILOG_INFO("Check enough form, callingUid: %{public}d, current userId: %{public}d", callingUid, currentUserId);
274
275 int callingUidFormCounts = 0;
276 int32_t maxFormsSize = Constants::MAX_FORMS;
277 const std::string maxFormsSizeKey = Constants::MAX_NORMAL_FORM_SIZE;
278 GetConfigParamFormMap(maxFormsSizeKey, maxFormsSize);
279 maxFormsSize = ((maxFormsSize > Constants::MAX_FORMS) || (maxFormsSize < 0)) ?
280 Constants::MAX_FORMS : maxFormsSize;
281 HILOG_DEBUG("maxFormsSize:%{public}d", maxFormsSize);
282
283 int32_t maxRecordPerApp = Constants::MAX_RECORD_PER_APP;
284 const std::string maxRecordPerAppKey = Constants::HOST_MAX_FORM_SIZE;
285 GetConfigParamFormMap(maxRecordPerAppKey, maxRecordPerApp);
286 maxRecordPerApp = ((maxRecordPerApp > Constants::MAX_RECORD_PER_APP) || (maxRecordPerApp < 0)) ?
287 Constants::MAX_RECORD_PER_APP : maxRecordPerApp;
288 HILOG_DEBUG("maxRecordPerApp:%{public}d", maxRecordPerApp);
289
290 if (maxRecordPerApp == 0) {
291 HILOG_ERROR("The maximum number of normal cards in pre host is 0");
292 return ERR_APPEXECFWK_FORM_MAX_FORMS_PER_CLIENT;
293 }
294
295 std::lock_guard<std::mutex> lock(formRecordMutex_);
296 if (static_cast<int32_t>(formRecords_.size()) >= maxFormsSize) {
297 HILOG_WARN("already use %{public}d forms, exceeds max form number", maxFormsSize);
298 return ERR_APPEXECFWK_FORM_MAX_SYSTEM_FORMS;
299 }
300
301 for (const auto &recordPair : formRecords_) {
302 FormRecord record = recordPair.second;
303 if ((record.providerUserId == FormUtil::GetCurrentAccountId()) && !record.formTempFlag) {
304 HILOG_DEBUG("Is called by the current active user");
305 for (const auto &userUid : record.formUserUids) {
306 if (userUid != callingUid) {
307 continue;
308 }
309 if (++callingUidFormCounts >= maxRecordPerApp) {
310 HILOG_WARN("already use %{public}d forms", maxRecordPerApp);
311 return ERR_APPEXECFWK_FORM_MAX_FORMS_PER_CLIENT;
312 }
313 break;
314 }
315 }
316 }
317 return ERR_OK;
318 }
319 /**
320 * @brief Delete temp form.
321 * @param formId The Id of the form.
322 * @return Returns true if this function is successfully called; returns false otherwise.
323 */
DeleteTempForm(const int64_t formId)324 bool FormDataMgr::DeleteTempForm(const int64_t formId)
325 {
326 std::lock_guard<std::mutex> lock(formTempMutex_);
327 auto iter = std::find(tempForms_.begin(), tempForms_.end(), formId);
328 if (iter == tempForms_.end()) {
329 HILOG_ERROR("%{public}s, temp form is not exist", __func__);
330 return false;
331 }
332 tempForms_.erase(iter);
333 return true;
334 }
335 /**
336 * @brief Check temp form is exist.
337 * @param formId The Id of the form.
338 * @return Returns true if the temp form is exist; returns false is not exist.
339 */
ExistTempForm(const int64_t formId) const340 bool FormDataMgr::ExistTempForm(const int64_t formId) const
341 {
342 std::lock_guard<std::mutex> lock(formTempMutex_);
343 return (std::find(tempForms_.begin(), tempForms_.end(), formId) != tempForms_.end());
344 }
345 /**
346 * @brief Check calling uid is valid.
347 * @param formUserUids The form user uids.
348 * @return Returns true if this user uid is valid; returns false otherwise.
349 */
IsCallingUidValid(const std::vector<int> & formUserUids) const350 bool FormDataMgr::IsCallingUidValid(const std::vector<int> &formUserUids) const
351 {
352 if (formUserUids.empty()) {
353 HILOG_ERROR("formUserUids is empty!");
354 return false;
355 }
356 for (const auto &userUid : formUserUids) {
357 if (userUid == IPCSkeleton::GetCallingUid()) {
358 return true;
359 }
360 }
361 HILOG_ERROR("Can not find the valid uid");
362 return false;
363 }
364 /**
365 * @brief Modify form temp flag by formId.
366 * @param formId The Id of the form.
367 * @param formTempFlag The form temp flag.
368 * @return Returns true if this function is successfully called; returns false otherwise.
369 */
ModifyFormTempFlag(const int64_t formId,const bool formTempFlag)370 bool FormDataMgr::ModifyFormTempFlag(const int64_t formId, const bool formTempFlag)
371 {
372 HILOG_INFO("%{public}s, modify form temp flag by formId", __func__);
373 std::lock_guard<std::mutex> lock(formRecordMutex_);
374 if (!(formRecords_.count(formId) > 0)) {
375 HILOG_ERROR("%{public}s, form info is not exist", __func__);
376 return false;
377 }
378 formRecords_[formId].formTempFlag = formTempFlag;
379 return true;
380 }
381 /**
382 * @brief Add form user uid from form record.
383 * @param formId The Id of the form.
384 * @param formRecord The form record.
385 * @return Returns true if this function is successfully called; returns false otherwise.
386 */
AddFormUserUid(const int64_t formId,const int formUserUid)387 bool FormDataMgr::AddFormUserUid(const int64_t formId, const int formUserUid)
388 {
389 HILOG_INFO("%{public}s, add form user uid by formId", __func__);
390 std::lock_guard<std::mutex> lock(formRecordMutex_);
391 if (!(formRecords_.count(formId) > 0)) {
392 HILOG_ERROR("%{public}s, form info is not exist", __func__);
393 return false;
394 }
395 if (std::find(formRecords_[formId].formUserUids.begin(), formRecords_[formId].formUserUids.end(),
396 formUserUid) == formRecords_[formId].formUserUids.end()) {
397 formRecords_[formId].formUserUids.emplace_back(formUserUid);
398 }
399 return true;
400 }
401 /**
402 * @brief Delete form user uid from form record.
403 * @param formId The Id of the form.
404 * @param uid calling user id.
405 * @return Returns true if this function is successfully called; returns false otherwise.
406 */
DeleteFormUserUid(const int64_t formId,const int uid)407 bool FormDataMgr::DeleteFormUserUid(const int64_t formId, const int uid)
408 {
409 HILOG_INFO("%{public}s, delete form user uid from form record", __func__);
410 std::lock_guard<std::mutex> lock(formRecordMutex_);
411 if (formRecords_.count(formId) > 0) {
412 auto iter = std::find(formRecords_.at(formId).formUserUids.begin(),
413 formRecords_.at(formId).formUserUids.end(), uid);
414 if (iter != formRecords_.at(formId).formUserUids.end()) {
415 formRecords_.at(formId).formUserUids.erase(iter);
416 }
417 return true;
418 } else {
419 HILOG_ERROR("%{public}s, form info not find", __func__);
420 return false;
421 }
422 }
423 /**
424 * @brief Update form record.
425 * @param formId The Id of the form.
426 * @param formRecord The form record.
427 * @return Returns true if this function is successfully called; returns false otherwise.
428 */
UpdateFormRecord(const int64_t formId,const FormRecord & formRecord)429 bool FormDataMgr::UpdateFormRecord(const int64_t formId, const FormRecord &formRecord)
430 {
431 HILOG_DEBUG("get form record by formId");
432 std::lock_guard<std::mutex> lock(formRecordMutex_);
433 auto info = formRecords_.find(formId);
434 if (info != formRecords_.end()) {
435 formRecords_[formId] = formRecord;
436 return true;
437 }
438 return false;
439 }
440 /**
441 * @brief Get form record.
442 * @param formId The Id of the form.
443 * @param formRecord The form record.
444 * @return Returns true if this function is successfully called; returns false otherwise.
445 */
GetFormRecord(const int64_t formId,FormRecord & formRecord) const446 bool FormDataMgr::GetFormRecord(const int64_t formId, FormRecord &formRecord) const
447 {
448 HILOG_DEBUG("get form record by formId");
449 std::lock_guard<std::mutex> lock(formRecordMutex_);
450 auto info = formRecords_.find(formId);
451 if (info == formRecords_.end()) {
452 HILOG_ERROR("form info not find");
453 return false;
454 }
455 formRecord = info->second;
456
457 HILOG_DEBUG("get form record successfully");
458 return true;
459 }
460 /**
461 * @brief Get form record.
462 * @param bundleName Bundle name.
463 * @param formInfos The form record.
464 * @return Returns true if this function is successfully called; returns false otherwise.
465 */
GetFormRecord(const std::string & bundleName,std::vector<FormRecord> & formInfos,int32_t userId) const466 bool FormDataMgr::GetFormRecord(const std::string &bundleName, std::vector<FormRecord> &formInfos, int32_t userId) const
467 {
468 HILOG_DEBUG("get form record by bundleName");
469 std::lock_guard<std::mutex> lock(formRecordMutex_);
470 for (auto itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
471 if (bundleName == itFormRecord->second.bundleName &&
472 (userId == Constants::INVALID_USER_ID || userId == itFormRecord->second.userId)) {
473 formInfos.emplace_back(itFormRecord->second);
474 }
475 }
476 if (formInfos.size() > 0) {
477 return true;
478 } else {
479 HILOG_DEBUG("form info not find");
480 return false;
481 }
482 }
483
484 /**
485 * @brief Get temporary form record.
486 * @param formTempRecords The temp form record.
487 * @return Returns true if this function is successfully called; returns false otherwise.
488 */
GetTempFormRecord(std::vector<FormRecord> & formTempRecords)489 bool FormDataMgr::GetTempFormRecord(std::vector<FormRecord> &formTempRecords)
490 {
491 HILOG_INFO("Get temporary form record");
492 std::lock_guard<std::mutex> lock(formRecordMutex_);
493 std::map<int64_t, FormRecord>::iterator itFormRecord;
494 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
495 if (itFormRecord->second.formTempFlag) {
496 formTempRecords.emplace_back(itFormRecord->second);
497 }
498 }
499 if (!formTempRecords.empty()) {
500 return true;
501 } else {
502 HILOG_INFO("The count of temporary form is zero");
503 return false;
504 }
505 }
506 /**
507 * @brief Check form record is exist.
508 * @param formId The Id of the form.
509 * @return Returns true if the form record is exist; returns false is not exist.
510 */
ExistFormRecord(const int64_t formId) const511 bool FormDataMgr::ExistFormRecord(const int64_t formId) const
512 {
513 HILOG_INFO("%{public}s, check form record is exist", __func__);
514 std::lock_guard<std::mutex> lock(formRecordMutex_);
515 return (formRecords_.count(formId) > 0);
516 }
517 /**
518 * @brief Has form user uids in form record.
519 * @param formId The Id of the form.
520 * @return Returns true if this form has form user uids; returns false is not has.
521 */
HasFormUserUids(const int64_t formId) const522 bool FormDataMgr::HasFormUserUids(const int64_t formId) const
523 {
524 HILOG_INFO("%{public}s, check form has user uids", __func__);
525 FormRecord record;
526 if (GetFormRecord(formId, record)) {
527 return record.formUserUids.empty() ? false : true;
528 }
529 return false;
530 }
531 /**
532 * @brief Get form host record.
533 * @param formId The id of the form.
534 * @param formHostRecord The form host record.
535 */
GetFormHostRecord(const int64_t formId,std::vector<FormHostRecord> & formHostRecords) const536 void FormDataMgr::GetFormHostRecord(const int64_t formId, std::vector<FormHostRecord> &formHostRecords) const
537 {
538 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
539 for (auto &record : clientRecords_) {
540 if (record.Contains(formId)) {
541 formHostRecords.emplace_back(record);
542 }
543 }
544 HILOG_DEBUG("%{public}s, get form host record by formId, size is %{public}zu", __func__, formHostRecords.size());
545 }
GetFormHostRemoteObj(const int64_t formId,std::vector<sptr<IRemoteObject>> & formHostObjs) const546 void FormDataMgr::GetFormHostRemoteObj(const int64_t formId, std::vector<sptr<IRemoteObject>> &formHostObjs) const
547 {
548 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
549 for (auto &record : clientRecords_) {
550 if (record.Contains(formId)) {
551 formHostObjs.emplace_back(record.GetFormHostClient());
552 }
553 }
554 HILOG_DEBUG("Get form host remote object by formId, size is %{public}zu", formHostObjs.size());
555 }
556 /**
557 * @brief Delete form host record.
558 * @param callerToken The client stub of the form host record.
559 * @param formId The id of the form.
560 * @return Returns true if this function is successfully called; returns false otherwise.
561 */
DeleteHostRecord(const sptr<IRemoteObject> & callerToken,const int64_t formId)562 bool FormDataMgr::DeleteHostRecord(const sptr<IRemoteObject> &callerToken, const int64_t formId)
563 {
564 HILOG_INFO("delete form host record");
565 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
566 std::vector<FormHostRecord>::iterator iter;
567 for (iter = clientRecords_.begin(); iter != clientRecords_.end(); ++iter) {
568 if (callerToken == iter->GetFormHostClient()) {
569 iter->DelForm(formId);
570 if (iter->IsEmpty()) {
571 iter->CleanResource();
572 iter = clientRecords_.erase(iter);
573 FormRenderMgr::GetInstance().CleanFormHost(callerToken);
574 }
575 break;
576 }
577 }
578 HILOG_INFO("%{public}s end", __func__);
579 return true;
580 }
581 /**
582 * @brief Clean removed forms form host.
583 * @param removedFormIds The id list of the forms.
584 */
CleanHostRemovedForms(const std::vector<int64_t> & removedFormIds)585 void FormDataMgr::CleanHostRemovedForms(const std::vector<int64_t> &removedFormIds)
586 {
587 HILOG_INFO("%{public}s start, delete form host record by formId list", __func__);
588 std::vector<int64_t> matchedIds;
589 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
590 std::vector<FormHostRecord>::iterator itHostRecord;
591 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
592 for (const int64_t& formId : removedFormIds) {
593 if (itHostRecord->Contains(formId)) {
594 matchedIds.emplace_back(formId);
595 itHostRecord->DelForm(formId);
596 }
597 }
598 if (!matchedIds.empty()) {
599 HILOG_INFO("%{public}s, OnFormUninstalled called", __func__);
600 itHostRecord->OnFormUninstalled(matchedIds);
601 }
602 }
603
604 HILOG_INFO("%{public}s end", __func__);
605 }
606
UpdateHostForms(const std::vector<int64_t> & updateFormIds)607 void FormDataMgr::UpdateHostForms(const std::vector<int64_t> &updateFormIds)
608 {
609 HILOG_INFO("%{public}s start, update form host record by formId list", __func__);
610 std::vector<int64_t> matchedIds;
611 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
612 for (FormHostRecord &record : clientRecords_) {
613 for (const int64_t &formId : updateFormIds) {
614 if (record.Contains(formId)) {
615 matchedIds.emplace_back(formId);
616 }
617 }
618 if (!matchedIds.empty()) {
619 HILOG_INFO("%{public}s, OnFormUninstalled called", __func__);
620 record.OnFormUninstalled(matchedIds);
621 matchedIds.clear();
622 }
623 }
624 HILOG_INFO("%{public}s end", __func__);
625 }
626
627 /**
628 * @brief Handle form host died.
629 * @param remoteHost Form host proxy object.
630 */
HandleHostDied(const sptr<IRemoteObject> & remoteHost)631 void FormDataMgr::HandleHostDied(const sptr<IRemoteObject> &remoteHost)
632 {
633 std::vector<int64_t> recordTempForms;
634 {
635 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
636 std::vector<FormHostRecord>::iterator itHostRecord;
637 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end();) {
638 if (remoteHost == itHostRecord->GetFormHostClient()) {
639 HandleHostDiedForTempForms(*itHostRecord, recordTempForms);
640 HILOG_INFO("find died client, remove it");
641 itHostRecord->CleanResource();
642 itHostRecord = clientRecords_.erase(itHostRecord);
643 break;
644 } else {
645 itHostRecord++;
646 }
647 }
648 }
649 {
650 std::lock_guard<std::mutex> lock(formRecordMutex_);
651 std::map<int64_t, FormRecord>::iterator itFormRecord;
652 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end();) {
653 int64_t formId = itFormRecord->first;
654 // if temp form, remove it
655 if (std::find(recordTempForms.begin(), recordTempForms.end(), formId) != recordTempForms.end()) {
656 FormRecord formRecord = itFormRecord->second;
657 itFormRecord = formRecords_.erase(itFormRecord);
658 FormProviderMgr::GetInstance().NotifyProviderFormDelete(formId, formRecord);
659 FormDataProxyMgr::GetInstance().UnsubscribeFormData(formId);
660 } else {
661 itFormRecord++;
662 }
663 }
664 }
665 {
666 std::lock_guard<std::mutex> lock(formStateRecordMutex_);
667 std::map<std::string, FormHostRecord>::iterator itFormStateRecord;
668 for (itFormStateRecord = formStateRecord_.begin(); itFormStateRecord != formStateRecord_.end();) {
669 if (remoteHost == itFormStateRecord->second.GetFormHostClient()) {
670 HILOG_INFO("find died client, remove it from formStateRecord_");
671 itFormStateRecord->second.CleanResource();
672 itFormStateRecord = formStateRecord_.erase(itFormStateRecord);
673 break;
674 } else {
675 itFormStateRecord++;
676 }
677 }
678 }
679 FormRenderMgr::GetInstance().CleanFormHost(remoteHost);
680 }
681
682 /**
683 * @brief Get the temp forms from host and delete temp form in cache.
684 * @param record The form record.
685 * @param recordTempForms gotten the temp forms.
686 */
HandleHostDiedForTempForms(const FormHostRecord & record,std::vector<int64_t> & recordTempForms)687 void FormDataMgr::HandleHostDiedForTempForms(const FormHostRecord &record, std::vector<int64_t> &recordTempForms)
688 {
689 std::lock_guard<std::mutex> lock(formTempMutex_);
690 std::vector<int64_t>::iterator itForm;
691 for (itForm = tempForms_.begin(); itForm != tempForms_.end();) {
692 if (record.Contains(*itForm)) {
693 recordTempForms.emplace_back(*itForm);
694 itForm = tempForms_.erase(itForm);
695 } else {
696 itForm++;
697 }
698 }
699 }
700
701 /**
702 * @brief Refresh enable or not.
703 * @param formId The Id of the form.
704 * @return true on enable, false on disable.
705 */
IsEnableRefresh(int64_t formId)706 bool FormDataMgr::IsEnableRefresh(int64_t formId)
707 {
708 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
709 for (auto &record : clientRecords_) {
710 if (record.IsEnableRefresh(formId)) {
711 return true;
712 }
713 }
714
715 return false;
716 }
717
718 /**
719 * @brief update enable or not.
720 * @param formId The Id of the form.
721 * @return true on enable, false on disable.
722 */
IsEnableUpdate(int64_t formId)723 bool FormDataMgr::IsEnableUpdate(int64_t formId)
724 {
725 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
726 for (auto &record : clientRecords_) {
727 if (record.IsEnableUpdate(formId)) {
728 return true;
729 }
730 }
731 return false;
732 }
733
PaddingUdidHash(const int64_t formId)734 int64_t FormDataMgr::PaddingUdidHash(const int64_t formId)
735 {
736 if (!GenerateUdidHash()) {
737 return -1;
738 }
739 // Compatible with int form id.
740 uint64_t unsignedFormId = static_cast<uint64_t>(formId);
741 if ((unsignedFormId & 0xffffffff00000000L) == 0) {
742 uint64_t unsignedUdidHash = static_cast<uint64_t>(udidHash_);
743 uint64_t unsignedUdidHashFormId = unsignedUdidHash | unsignedFormId;
744 int64_t udidHashFormId = static_cast<int64_t>(unsignedUdidHashFormId);
745 return udidHashFormId;
746 }
747 return formId;
748 }
749
750 /**
751 * @brief Generate form id.
752 * @return form id.
753 */
GenerateFormId()754 int64_t FormDataMgr::GenerateFormId()
755 {
756 // generate by udidHash_
757 if (!GenerateUdidHash()) {
758 HILOG_ERROR("%{public}s fail, generateFormId no invalid udidHash_", __func__);
759 return -1;
760 }
761 return FormUtil::GenerateFormId(udidHash_);
762 }
763 /**
764 * @brief Generate udid.
765 * @return Returns true if this function is successfully called; returns false otherwise.
766 */
GenerateUdidHash()767 bool FormDataMgr::GenerateUdidHash()
768 {
769 if (udidHash_ != Constants::INVALID_UDID_HASH) {
770 return true;
771 }
772
773 bool genUdid = FormUtil::GenerateUdidHash(udidHash_);
774 if (!genUdid) {
775 HILOG_ERROR("%{public}s, Failed to generate udid.", __func__);
776 return false;
777 }
778
779 return true;
780 }
781 /**
782 * @brief Get udid.
783 * @return udid.
784 */
GetUdidHash() const785 int64_t FormDataMgr::GetUdidHash() const
786 {
787 return udidHash_;
788 }
789 /**
790 * @brief Set udid.
791 * @param udidHash udid.
792 */
SetUdidHash(const int64_t udidHash)793 void FormDataMgr::SetUdidHash(const int64_t udidHash)
794 {
795 udidHash_ = udidHash;
796 }
797
798 /**
799 * @brief Get the matched form host record by client stub.
800 *
801 * @param callerToken The client stub of the form host record.
802 * @param formHostRecord The form host record.
803 * @return Returns true if this function is successfully called, returns false otherwise.
804 */
GetMatchedHostClient(const sptr<IRemoteObject> & callerToken,FormHostRecord & formHostRecord) const805 bool FormDataMgr::GetMatchedHostClient(const sptr<IRemoteObject> &callerToken, FormHostRecord &formHostRecord) const
806 {
807 HILOG_DEBUG("get the matched form host record by client stub.");
808 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
809 for (const FormHostRecord &record : clientRecords_) {
810 if (callerToken == record.GetFormHostClient()) {
811 formHostRecord = record;
812 return true;
813 }
814 }
815
816 HILOG_ERROR("form host record not find.");
817 return false;
818 }
819
820 /**
821 * @brief Set needRefresh for FormRecord.
822 * @param formId The Id of the form.
823 * @param needRefresh true or false.
824 */
SetNeedRefresh(const int64_t formId,const bool needRefresh)825 void FormDataMgr::SetNeedRefresh(const int64_t formId, const bool needRefresh)
826 {
827 std::lock_guard<std::mutex> lock(formRecordMutex_);
828 auto itFormRecord = formRecords_.find(formId);
829 if (itFormRecord == formRecords_.end()) {
830 HILOG_ERROR("%{public}s, form info not find", __func__);
831 return;
832 }
833 itFormRecord->second.needRefresh = needRefresh;
834 }
835
836 /**
837 * @brief Set isCountTimerRefresh for FormRecord.
838 * @param formId The Id of the form.
839 * @param countTimerRefresh true or false.
840 */
SetCountTimerRefresh(const int64_t formId,const bool countTimerRefresh)841 void FormDataMgr::SetCountTimerRefresh(const int64_t formId, const bool countTimerRefresh)
842 {
843 std::lock_guard<std::mutex> lock(formRecordMutex_);
844 auto itFormRecord = formRecords_.find(formId);
845 if (itFormRecord == formRecords_.end()) {
846 HILOG_ERROR("%{public}s, form info not find", __func__);
847 return;
848 }
849 itFormRecord->second.isCountTimerRefresh = countTimerRefresh;
850 }
851
852 /**
853 * @brief Set isTimerRefresh for FormRecord.
854 * @param formId The Id of the form.
855 * @param timerRefresh true or false.
856 */
SetTimerRefresh(const int64_t formId,const bool timerRefresh)857 void FormDataMgr::SetTimerRefresh(const int64_t formId, const bool timerRefresh)
858 {
859 std::lock_guard<std::mutex> lock(formRecordMutex_);
860 auto itFormRecord = formRecords_.find(formId);
861 if (itFormRecord == formRecords_.end()) {
862 HILOG_ERROR("%{public}s, form info not find", __func__);
863 return;
864 }
865 itFormRecord->second.isTimerRefresh = timerRefresh;
866 }
867
868 /**
869 * @brief Get updated form.
870 * @param record FormRecord.
871 * @param targetForms Target forms.
872 * @param updatedForm Updated formnfo.
873 * @return Returns true on success, false on failure.
874 */
GetUpdatedForm(const FormRecord & record,const std::vector<FormInfo> & targetForms,FormInfo & updatedForm)875 bool FormDataMgr::GetUpdatedForm(const FormRecord &record, const std::vector<FormInfo> &targetForms,
876 FormInfo &updatedForm)
877 {
878 if (targetForms.empty()) {
879 HILOG_ERROR("%{public}s error, targetForms is empty.", __func__);
880 return false;
881 }
882
883 for (const FormInfo &item : targetForms) {
884 if (IsSameForm(record, item)) {
885 updatedForm = item;
886 HILOG_DEBUG("%{public}s, find matched form.", __func__);
887 return true;
888 }
889 }
890 return false;
891 }
892 /**
893 * @brief Set isEnableUpdate for FormRecord.
894 * @param formId The Id of the form.
895 * @param enableUpdate true or false.
896 */
SetEnableUpdate(const int64_t formId,const bool enableUpdate)897 void FormDataMgr::SetEnableUpdate(const int64_t formId, const bool enableUpdate)
898 {
899 std::lock_guard<std::mutex> lock(formRecordMutex_);
900 auto itFormRecord = formRecords_.find(formId);
901 if (itFormRecord == formRecords_.end()) {
902 HILOG_ERROR("%{public}s, form info not find", __func__);
903 return;
904 }
905 itFormRecord->second.isEnableUpdate = enableUpdate;
906 }
907 /**
908 * @brief Set update info for FormRecord.
909 * @param formId The Id of the form.
910 * @param enableUpdate true or false.
911 * @param updateDuration Update duration.
912 * @param updateAtHour Update at hour.
913 * @param updateAtMin Update at minute.
914 */
SetUpdateInfo(const int64_t formId,const bool enableUpdate,const long updateDuration,const int updateAtHour,const int updateAtMin)915 void FormDataMgr::SetUpdateInfo(
916 const int64_t formId,
917 const bool enableUpdate,
918 const long updateDuration,
919 const int updateAtHour,
920 const int updateAtMin)
921 {
922 std::lock_guard<std::mutex> lock(formRecordMutex_);
923 auto itFormRecord = formRecords_.find(formId);
924 if (itFormRecord == formRecords_.end()) {
925 HILOG_ERROR("%{public}s, form info not find", __func__);
926 return;
927 }
928
929 itFormRecord->second.isEnableUpdate = enableUpdate;
930 itFormRecord->second.updateDuration = updateDuration;
931 itFormRecord->second.updateAtHour = updateAtHour;
932 itFormRecord->second.updateAtMin = updateAtMin;
933 }
934 /**
935 * @brief Check if two forms is same or not.
936 * @param record FormRecord.
937 * @param formInfo FormInfo.
938 * @return Returns true on success, false on failure.
939 */
IsSameForm(const FormRecord & record,const FormInfo & formInfo)940 bool FormDataMgr::IsSameForm(const FormRecord &record, const FormInfo &formInfo)
941 {
942 if (record.bundleName == formInfo.bundleName
943 && record.moduleName == formInfo.moduleName
944 && record.abilityName == formInfo.abilityName
945 && record.formName == formInfo.name
946 && std::find(formInfo.supportDimensions.begin(), formInfo.supportDimensions.end(), record.specification)
947 != formInfo.supportDimensions.end()) {
948 return true;
949 }
950
951 return false;
952 }
953 /**
954 * @brief Clean removed form records.
955 * @param removedForms The id list of the forms.
956 */
CleanRemovedFormRecords(const std::string & bundleName,std::set<int64_t> & removedForms)957 void FormDataMgr::CleanRemovedFormRecords(const std::string &bundleName, std::set<int64_t> &removedForms)
958 {
959 HILOG_INFO("%{public}s, clean removed form records", __func__);
960 std::lock_guard<std::mutex> lock(formRecordMutex_);
961 std::map<int64_t, FormRecord>::iterator itFormRecord;
962 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end();) {
963 auto itForm = std::find(removedForms.begin(), removedForms.end(), itFormRecord->first);
964 if (itForm != removedForms.end()) {
965 FormCacheMgr::GetInstance().DeleteData(itFormRecord->first);
966 FormRenderMgr::GetInstance().StopRenderingForm(itFormRecord->first, itFormRecord->second);
967 itFormRecord = formRecords_.erase(itFormRecord);
968 } else {
969 itFormRecord++;
970 }
971 }
972 }
973 /**
974 * @brief Clean removed temp form records.
975 * @param bundleName BundleName.
976 * @param removedForms The id list of the forms.
977 */
CleanRemovedTempFormRecords(const std::string & bundleName,const int32_t userId,std::set<int64_t> & removedForms)978 void FormDataMgr::CleanRemovedTempFormRecords(const std::string &bundleName, const int32_t userId,
979 std::set<int64_t> &removedForms)
980 {
981 HILOG_INFO("%{public}s, clean removed form records", __func__);
982 std::set<int64_t> removedTempForms;
983 {
984 std::lock_guard<std::mutex> lock(formRecordMutex_);
985 std::map<int64_t, FormRecord>::iterator itFormRecord;
986 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end();) {
987 if ((itFormRecord->second.formTempFlag) && (bundleName == itFormRecord->second.bundleName)
988 && (userId == itFormRecord->second.providerUserId)) {
989 removedTempForms.emplace(itFormRecord->second.formId);
990 FormRenderMgr::GetInstance().StopRenderingForm(itFormRecord->first, itFormRecord->second);
991 itFormRecord = formRecords_.erase(itFormRecord);
992 } else {
993 itFormRecord++;
994 }
995 }
996 }
997
998 if (removedTempForms.size() > 0) {
999 std::lock_guard<std::mutex> lock(formTempMutex_);
1000 std::vector<int64_t>::iterator itTemp;
1001 for (itTemp = tempForms_.begin(); itTemp != tempForms_.end();) {
1002 if (removedTempForms.find(*itTemp) != removedTempForms.end()) {
1003 itTemp = tempForms_.erase(itTemp);
1004 } else {
1005 itTemp++;
1006 }
1007 }
1008 removedForms.merge(removedTempForms);
1009 }
1010 }
1011 /**
1012 * @brief Get recreate form records.
1013 * @param reCreateForms The id list of the forms.
1014 */
GetReCreateFormRecordsByBundleName(const std::string & bundleName,std::set<int64_t> & reCreateForms)1015 void FormDataMgr::GetReCreateFormRecordsByBundleName(const std::string &bundleName, std::set<int64_t> &reCreateForms)
1016 {
1017 std::lock_guard<std::mutex> lock(formRecordMutex_);
1018 std::map<int64_t, FormRecord>::iterator itFormRecord;
1019 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
1020 if (bundleName == itFormRecord->second.bundleName) {
1021 reCreateForms.emplace(itFormRecord->second.formId);
1022 }
1023 }
1024 }
1025 /**
1026 * @brief Set form isInited flag.
1027 * @param formId The Id of the form.
1028 * @param isInited isInited property
1029 */
SetFormCacheInited(const int64_t formId,bool isInited)1030 void FormDataMgr::SetFormCacheInited(const int64_t formId, bool isInited)
1031 {
1032 std::lock_guard<std::mutex> lock(formRecordMutex_);
1033 auto itFormRecord = formRecords_.find(formId);
1034 if (itFormRecord == formRecords_.end()) {
1035 HILOG_ERROR("%{public}s, form info not find", __func__);
1036 return;
1037 }
1038 itFormRecord->second.isInited = isInited;
1039 itFormRecord->second.needRefresh = !isInited;
1040 }
1041 /**
1042 * @brief Set versionUpgrade.
1043 * @param formId The Id of the form.
1044 * @param versionUpgrade true or false
1045 */
SetVersionUpgrade(const int64_t formId,const bool versionUpgrade)1046 void FormDataMgr::SetVersionUpgrade(const int64_t formId, const bool versionUpgrade)
1047 {
1048 std::lock_guard<std::mutex> lock(formRecordMutex_);
1049 auto itFormRecord = formRecords_.find(formId);
1050 if (itFormRecord == formRecords_.end()) {
1051 HILOG_ERROR("%{public}s, form info not find", __func__);
1052 return;
1053 }
1054 itFormRecord->second.versionUpgrade = versionUpgrade;
1055 }
1056 /**
1057 * @brief Update form for host clients.
1058 * @param formId The Id of the form.
1059 * @param needRefresh true or false
1060 */
UpdateHostNeedRefresh(const int64_t formId,const bool needRefresh)1061 void FormDataMgr::UpdateHostNeedRefresh(const int64_t formId, const bool needRefresh)
1062 {
1063 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
1064 std::vector<FormHostRecord>::iterator itHostRecord;
1065 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
1066 if (itHostRecord->Contains(formId)) {
1067 itHostRecord->SetNeedRefresh(formId, needRefresh);
1068 }
1069 }
1070 }
1071
1072 /**
1073 * @brief Update form for host clients.
1074 * @param formId The Id of the form.
1075 * @param formRecord The form info.
1076 * @return Returns true if form update, false if other.
1077 */
UpdateHostForm(const int64_t formId,const FormRecord & formRecord)1078 bool FormDataMgr::UpdateHostForm(const int64_t formId, const FormRecord &formRecord)
1079 {
1080 bool isUpdated = false;
1081 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
1082 std::vector<FormHostRecord>::iterator itHostRecord;
1083 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
1084 bool enableRefresh = formRecord.isVisible || itHostRecord->IsEnableUpdate(formId) ||
1085 itHostRecord->IsEnableRefresh(formId);
1086 HILOG_INFO("formId:%{public}" PRId64 " enableRefresh:%{public}d", formId, enableRefresh);
1087 if (enableRefresh) {
1088 // update form
1089 itHostRecord->OnUpdate(formId, formRecord);
1090 // set needRefresh
1091 itHostRecord->SetNeedRefresh(formId, false);
1092 isUpdated = true;
1093 }
1094 }
1095 return isUpdated;
1096 }
1097
HandleUpdateHostFormFlag(const std::vector<int64_t> & formIds,bool flag,bool isOnlyEnableUpdate,FormHostRecord & formHostRecord,std::vector<int64_t> & refreshForms)1098 ErrCode FormDataMgr::HandleUpdateHostFormFlag(const std::vector<int64_t> &formIds, bool flag, bool isOnlyEnableUpdate,
1099 FormHostRecord &formHostRecord, std::vector<int64_t> &refreshForms)
1100 {
1101 for (const int64_t formId : formIds) {
1102 if (formId <= 0) {
1103 HILOG_WARN("%{public}s, formId %{public}" PRId64 " is less than 0", __func__, formId);
1104 continue;
1105 }
1106
1107 int64_t matchedFormId = FindMatchedFormId(formId);
1108 if (!formHostRecord.Contains(matchedFormId)) {
1109 HILOG_WARN("%{public}s, form %{public}" PRId64 "is not owned by this client, don't need to update flag",
1110 __func__, formId);
1111 continue;
1112 }
1113
1114 if (isOnlyEnableUpdate) {
1115 // new API: this flag is used only to control enable update
1116 formHostRecord.SetEnableUpdate(matchedFormId, flag);
1117 formHostRecord.SetEnableRefresh(matchedFormId, false);
1118 } else {
1119 // old API: this flag is used to control enable update and visible update
1120 formHostRecord.SetEnableRefresh(matchedFormId, flag);
1121 }
1122
1123 // set disable
1124 if (!flag) {
1125 HILOG_DEBUG("flag is disable");
1126 continue;
1127 }
1128 FormRecord formRecord;
1129 if (GetFormRecord(matchedFormId, formRecord)) {
1130 if (formRecord.needRefresh) {
1131 HILOG_DEBUG("formRecord need refresh");
1132 refreshForms.emplace_back(matchedFormId);
1133 continue;
1134 }
1135 } else {
1136 HILOG_WARN("not exist such form:%{public}" PRId64 "", matchedFormId);
1137 continue;
1138 }
1139
1140 // if set enable flag, should check whether to refresh form
1141 if (!formHostRecord.IsNeedRefresh(matchedFormId)) {
1142 HILOG_DEBUG("host need not refresh");
1143 continue;
1144 }
1145
1146 if (IsFormCached(formRecord)) {
1147 HILOG_DEBUG("form cached");
1148 formHostRecord.OnUpdate(matchedFormId, formRecord);
1149 formHostRecord.SetNeedRefresh(matchedFormId, false);
1150 } else {
1151 HILOG_DEBUG("form no cache");
1152 refreshForms.emplace_back(matchedFormId);
1153 continue;
1154 }
1155 }
1156 return ERR_OK;
1157 }
1158
1159 /**
1160 * @brief handle update form flag.
1161 * @param formIDs The id of the forms.
1162 * @param callerToken Caller ability token.
1163 * @param flag form flag.
1164 * @param isOnlyEnableUpdate form enable update form flag.
1165 * @param refreshForms Refresh forms
1166 * @return Returns ERR_OK on success, others on failure.
1167 */
UpdateHostFormFlag(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,bool flag,bool isOnlyEnableUpdate,std::vector<int64_t> & refreshForms)1168 ErrCode FormDataMgr::UpdateHostFormFlag(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
1169 bool flag, bool isOnlyEnableUpdate, std::vector<int64_t> &refreshForms)
1170 {
1171 HILOG_DEBUG(" start, flag: %{public}d", flag);
1172 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
1173 std::vector<FormHostRecord>::iterator itHostRecord;
1174 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
1175 if (callerToken == itHostRecord->GetFormHostClient()) {
1176 HandleUpdateHostFormFlag(formIds, flag, isOnlyEnableUpdate, *itHostRecord, refreshForms);
1177 HILOG_DEBUG(" end.");
1178 return ERR_OK;
1179 }
1180 }
1181 HILOG_ERROR("can't find target client");
1182 return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1183 }
1184 /**
1185 * @brief Find matched form id.
1186 * @param formId The form id.
1187 * @return Matched form id.
1188 */
FindMatchedFormId(const int64_t formId)1189 int64_t FormDataMgr::FindMatchedFormId(const int64_t formId)
1190 {
1191 uint64_t unsignedFormId = static_cast<uint64_t>(formId);
1192 if ((unsignedFormId & 0xffffffff00000000L) != 0) {
1193 return formId;
1194 }
1195 std::lock_guard<std::mutex> lock(formRecordMutex_);
1196 std::map<int64_t, FormRecord>::iterator itFormRecord;
1197 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
1198 uint64_t unsignedFormId = static_cast<uint64_t>(formId);
1199 uint64_t unsignedItFormRecordFirst = static_cast<uint64_t>(itFormRecord->first);
1200 if ((unsignedItFormRecordFirst & 0x00000000ffffffffL) == (unsignedFormId & 0x00000000ffffffffL)) {
1201 return itFormRecord->first;
1202 }
1203 }
1204 return formId;
1205 }
1206
1207 /**
1208 * @brief Clear host data by uId.
1209 * @param uId The caller uId.
1210 */
ClearHostDataByUId(const int uId)1211 void FormDataMgr::ClearHostDataByUId(const int uId)
1212 {
1213 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
1214 std::vector<FormHostRecord>::iterator itHostRecord;
1215 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end();) {
1216 if (itHostRecord->GetCallerUid() == uId) {
1217 itHostRecord->CleanResource();
1218 itHostRecord = clientRecords_.erase(itHostRecord);
1219 } else {
1220 itHostRecord++;
1221 }
1222 }
1223 }
1224 /**
1225 * @brief Get no host temp forms.
1226 * @param uid The caller uid.
1227 * @param noHostTempFormsMap no host temp forms.
1228 * @param foundFormsMap Form Id list.
1229 */
GetNoHostTempForms(const int uid,std::map<FormIdKey,std::set<int64_t>> & noHostTempFormsMap,std::map<int64_t,bool> & foundFormsMap)1230 void FormDataMgr::GetNoHostTempForms(
1231 const int uid, std::map<FormIdKey,
1232 std::set<int64_t>> &noHostTempFormsMap,
1233 std::map<int64_t, bool> &foundFormsMap)
1234 {
1235 std::lock_guard<std::mutex> lock(formRecordMutex_);
1236 std::map<int64_t, FormRecord>::iterator itFormRecord;
1237 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
1238 if (!itFormRecord->second.formTempFlag) {
1239 continue; // Not temp form, skip
1240 }
1241
1242 auto itUid = std::find(itFormRecord->second.formUserUids.begin(),
1243 itFormRecord->second.formUserUids.end(), uid);
1244 if (itUid == itFormRecord->second.formUserUids.end()) {
1245 foundFormsMap.emplace(itFormRecord->second.formId, false);
1246 continue;
1247 }
1248
1249 itFormRecord->second.formUserUids.erase(itUid);
1250 if (!itFormRecord->second.formUserUids.empty()) {
1251 continue;
1252 }
1253
1254 FormIdKey formIdKey(itFormRecord->second.bundleName, itFormRecord->second.abilityName);
1255 auto itIdsSet = noHostTempFormsMap.find(formIdKey);
1256 if (itIdsSet == noHostTempFormsMap.end()) {
1257 std::set<int64_t> formIdsSet;
1258 formIdsSet.emplace(itFormRecord->second.formId);
1259 noHostTempFormsMap.emplace(formIdKey, formIdsSet);
1260 } else {
1261 itIdsSet->second.emplace(itFormRecord->second.formId);
1262 }
1263 }
1264 }
1265 /**
1266 * @brief Parse update config.
1267 * @param record The form record.
1268 * @param info The form item info.
1269 */
ParseUpdateConfig(FormRecord & record,const FormItemInfo & info) const1270 void FormDataMgr::ParseUpdateConfig(FormRecord &record, const FormItemInfo &info) const
1271 {
1272 int configDuration = info.GetUpdateDuration();
1273 if (configDuration > 0) {
1274 ParseIntervalConfig(record, configDuration);
1275 } else {
1276 ParseAtTimerConfig(record, info);
1277 }
1278 }
1279
1280 /**
1281 * @brief Parse update interval config.
1282 * @param record The form record.
1283 * @param configDuration interval duration.
1284 */
ParseIntervalConfig(FormRecord & record,const int configDuration) const1285 void FormDataMgr::ParseIntervalConfig(FormRecord &record, const int configDuration) const
1286 {
1287 HILOG_INFO("%{public}s, configDuration:%{public}d", __func__, configDuration);
1288 if (configDuration <= Constants::MIN_CONFIG_DURATION) {
1289 record.updateDuration = Constants::MIN_PERIOD;
1290 } else if (configDuration >= Constants::MAX_CONFIG_DURATION) {
1291 record.updateDuration = Constants::MAX_PERIOD;
1292 } else {
1293 record.updateDuration = configDuration * Constants::TIME_CONVERSION;
1294 }
1295 HILOG_INFO("%{public}s end", __func__);
1296 }
1297
1298 /**
1299 * @brief Parse at time config.
1300 * @param record The form record.
1301 * @param info form item info.
1302 */
ParseAtTimerConfig(FormRecord & record,const FormItemInfo & info) const1303 void FormDataMgr::ParseAtTimerConfig(FormRecord &record, const FormItemInfo &info) const
1304 {
1305 record.isEnableUpdate = false;
1306 record.updateDuration = 0;
1307 std::string configAtTime = info.GetScheduledUpdateTime();
1308 HILOG_INFO("%{public}s, parseAsUpdateAt updateAt:%{public}s", __func__, configAtTime.c_str());
1309 if (configAtTime.empty()) {
1310 return;
1311 }
1312
1313 std::vector<std::string> temp = FormUtil::StringSplit(configAtTime, Constants::TIME_DELIMETER);
1314 if (temp.size() != Constants::UPDATE_AT_CONFIG_COUNT) {
1315 HILOG_ERROR("%{public}s, invalid config", __func__);
1316 return;
1317 }
1318 int hour = -1;
1319 int min = -1;
1320 hour = std::stoi(temp[0]);
1321 min = std::stoi(temp[1]);
1322 if (hour < Constants::MIN_TIME || hour > Constants::MAX_HOUR || min < Constants::MIN_TIME || min >
1323 Constants::MAX_MINUTE) {
1324 HILOG_ERROR("%{public}s, time is invalid", __func__);
1325 return;
1326 }
1327 record.updateAtHour = hour;
1328 record.updateAtMin = min;
1329 record.isEnableUpdate = true;
1330 }
1331 /**
1332 * @brief check if form cached.
1333 * @param record The form record.
1334 * @return Returns ERR_OK on cached, others on not cached.
1335 */
IsFormCached(const FormRecord record)1336 bool FormDataMgr::IsFormCached(const FormRecord record)
1337 {
1338 if (record.versionUpgrade) {
1339 return false;
1340 }
1341 return FormCacheMgr::GetInstance().NeedAcquireProviderData(record.formId);
1342 }
1343
1344 /**
1345 * @brief Create form state host record.
1346 * @param provider The provider of the form state
1347 * @param info The form item info.
1348 * @param callerToken The UID of the proxy.
1349 * @param callingUid The UID of the proxy.
1350 * @return Returns true if this function is successfully called; returns false otherwise.
1351 */
CreateFormStateRecord(std::string & provider,const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,int callingUid)1352 bool FormDataMgr::CreateFormStateRecord(std::string &provider, const FormItemInfo &info,
1353 const sptr<IRemoteObject> &callerToken, int callingUid)
1354 {
1355 std::lock_guard<std::mutex> lock(formStateRecordMutex_);
1356 auto iter = formStateRecord_.find(provider);
1357 if (iter != formStateRecord_.end()) {
1358 if (iter->second.GetFormHostClient() != callerToken) {
1359 iter->second.SetFormHostClient(callerToken);
1360 }
1361 return true;
1362 }
1363
1364 FormHostRecord hostRecord;
1365 bool isCreated = CreateHostRecord(info, callerToken, callingUid, hostRecord);
1366 if (isCreated) {
1367 formStateRecord_.emplace(provider, hostRecord);
1368 return true;
1369 }
1370
1371 return false;
1372 }
1373
CreateFormAcquireDataRecord(int64_t requestCode,const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,int callingUid)1374 bool FormDataMgr::CreateFormAcquireDataRecord(int64_t requestCode, const FormItemInfo &info,
1375 const sptr<IRemoteObject> &callerToken, int callingUid)
1376 {
1377 std::lock_guard<std::mutex> lock(formAcquireDataRecordMutex_);
1378 auto iter = formAcquireDataRecord_.find(requestCode);
1379 if (iter != formAcquireDataRecord_.end()) {
1380 if (iter->second.GetFormHostClient() != callerToken) {
1381 iter->second.SetFormHostClient(callerToken);
1382 }
1383 return true;
1384 }
1385
1386 FormHostRecord hostRecord;
1387 bool isCreated = CreateHostRecord(info, callerToken, callingUid, hostRecord);
1388 if (isCreated) {
1389 formAcquireDataRecord_.emplace(requestCode, hostRecord);
1390 return true;
1391 }
1392
1393 return false;
1394 }
1395
AcquireFormDataBack(const AAFwk::WantParams & wantParams,int64_t requestCode)1396 ErrCode FormDataMgr::AcquireFormDataBack(const AAFwk::WantParams &wantParams, int64_t requestCode)
1397 {
1398 std::lock_guard<std::mutex> lock(formAcquireDataRecordMutex_);
1399 auto iter = formAcquireDataRecord_.find(requestCode);
1400 if (iter == formAcquireDataRecord_.end()) {
1401 HILOG_ERROR("filed to get form state host record");
1402 return ERR_APPEXECFWK_FORM_GET_HOST_FAILED;
1403 }
1404 iter->second.OnAcquireFormData(wantParams, requestCode);
1405 iter->second.CleanResource();
1406 formAcquireDataRecord_.erase(iter);
1407 return ERR_OK;
1408 }
1409
1410 /**
1411 * @brief acquire form state callback.
1412 * @param state form state.
1413 * @param provider provider info.
1414 * @param want The want of onAcquireFormState.
1415 * @return Returns true if this function is successfully called; returns false otherwise.
1416 */
AcquireFormStateBack(AppExecFwk::FormState state,const std::string & provider,const Want & want)1417 ErrCode FormDataMgr::AcquireFormStateBack(AppExecFwk::FormState state, const std::string &provider,
1418 const Want &want)
1419 {
1420 std::lock_guard<std::mutex> lock(formStateRecordMutex_);
1421 auto iter = formStateRecord_.find(provider);
1422 if (iter == formStateRecord_.end()) {
1423 HILOG_ERROR("filed to get form state host record");
1424 return ERR_APPEXECFWK_FORM_GET_HOST_FAILED;
1425 }
1426 iter->second.OnAcquireState(state, want);
1427 iter->second.CleanResource();
1428 formStateRecord_.erase(iter);
1429 return ERR_OK;
1430 }
1431
1432 /**
1433 * @brief Notify the form is visible or not.
1434 * @param formIds Indicates the ID of the forms.
1435 * @param isVisible Visible or not.
1436 * @param callerToken Host client.
1437 * @return Returns ERR_OK on success, others on failure.
1438 */
NotifyFormsVisible(const std::vector<int64_t> & formIds,bool isVisible,const sptr<IRemoteObject> & callerToken)1439 ErrCode FormDataMgr::NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
1440 const sptr<IRemoteObject> &callerToken)
1441 {
1442 if (formIds.empty() || callerToken == nullptr) {
1443 HILOG_ERROR("%{public}s failed, formIds empty.", __func__);
1444 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1445 }
1446
1447 std::vector<int64_t> foundFormIds {};
1448 {
1449 HILOG_INFO("%{public}s, get the matched form host record by client stub.", __func__);
1450 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
1451 for (const FormHostRecord &record : clientRecords_) {
1452 if (callerToken != record.GetFormHostClient()) {
1453 continue;
1454 }
1455 for (const int64_t formId : formIds) {
1456 int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
1457 if (CheckInvalidForm(formId) != ERR_OK) {
1458 continue;
1459 }
1460 if (!record.Contains(matchedFormId)) {
1461 HILOG_ERROR("%{public}s fail, form is not self-owned, form:%{public}" PRId64 ".", __func__,
1462 matchedFormId);
1463 } else {
1464 foundFormIds.push_back(matchedFormId);
1465 }
1466 }
1467 break;
1468 }
1469 }
1470
1471 if (foundFormIds.empty()) {
1472 HILOG_ERROR("%{public}s failed, no valid forms found.", __func__);
1473 return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1474 }
1475
1476 for (auto matchedFormId : foundFormIds) {
1477 SetRecordVisible(matchedFormId, isVisible);
1478 }
1479 return ERR_OK;
1480 }
1481
1482 /**
1483 * @brief set form record visible.
1484 * @param matchedFormId form id.
1485 * @param isVisible is visible.
1486 * @return Returns true if this function is successfully called; returns false otherwise.
1487 */
SetRecordVisible(int64_t matchedFormId,bool isVisible)1488 ErrCode FormDataMgr::SetRecordVisible(int64_t matchedFormId, bool isVisible)
1489 {
1490 HILOG_INFO("%{public}s, set form record visible", __func__);
1491 std::lock_guard<std::mutex> lock(formRecordMutex_);
1492 auto info = formRecords_.find(matchedFormId);
1493 if (info == formRecords_.end()) {
1494 HILOG_ERROR("%{public}s, form info not find", __func__);
1495 return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1496 }
1497 info->second.isVisible = isVisible;
1498 HILOG_DEBUG("set isVisible to %{public}d, formId: %{public}" PRId64 " ", isVisible, matchedFormId);
1499 return ERR_OK;
1500 }
1501
1502 /**
1503 * @brief delete forms by userId.
1504 *
1505 * @param userId user ID.
1506 * @param removedFormIds removed userId.
1507 */
DeleteFormsByUserId(const int32_t userId,std::vector<int64_t> & removedFormIds)1508 void FormDataMgr::DeleteFormsByUserId(const int32_t userId, std::vector<int64_t> &removedFormIds)
1509 {
1510 HILOG_INFO("%{public}s, delete forms by userId", __func__);
1511
1512 // handle formRecords_
1513 std::vector<int64_t> removedTempForms;
1514 {
1515 std::lock_guard<std::mutex> lock(formRecordMutex_);
1516 auto itFormRecord = formRecords_.begin();
1517 while (itFormRecord != formRecords_.end()) {
1518 if (userId == itFormRecord->second.providerUserId) {
1519 if (itFormRecord->second.formTempFlag) {
1520 removedTempForms.emplace_back(itFormRecord->second.formId);
1521 }
1522 removedFormIds.emplace_back(itFormRecord->second.formId);
1523 itFormRecord = formRecords_.erase(itFormRecord);
1524 } else {
1525 ++itFormRecord;
1526 }
1527 }
1528 }
1529
1530 // handle tempForms_
1531 if (removedTempForms.size() > 0) {
1532 std::lock_guard<std::mutex> lock(formTempMutex_);
1533 std::vector<int64_t>::iterator itTemp;
1534 for (itTemp = tempForms_.begin();itTemp != tempForms_.end();) {
1535 if (std::find(removedTempForms.begin(), removedTempForms.end(), *itTemp) != removedTempForms.end()) {
1536 itTemp = tempForms_.erase(itTemp);
1537 } else {
1538 itTemp++;
1539 }
1540 }
1541 }
1542 }
1543 /**
1544 * @brief Clear form records for st limit value test.
1545 */
ClearFormRecords()1546 void FormDataMgr::ClearFormRecords()
1547 {
1548 {
1549 std::lock_guard<std::mutex> lock(formRecordMutex_);
1550 formRecords_.clear();
1551 }
1552 {
1553 std::lock_guard<std::mutex> lock(formTempMutex_);
1554 tempForms_.clear();
1555 }
1556 }
1557
1558 /**
1559 * @brief handle get no host invalid temp forms.
1560 * @param userId User ID.
1561 * @param callingUid The UID of the proxy.
1562 * @param matchedFormIds The set of the valid forms.
1563 * @param noHostTempFormsMap The map of the no host forms.
1564 * @param foundFormsMap The map of the found forms.
1565 */
GetNoHostInvalidTempForms(int32_t userId,int32_t callingUid,std::set<int64_t> & matchedFormIds,std::map<FormIdKey,std::set<int64_t>> & noHostTempFormsMap,std::map<int64_t,bool> & foundFormsMap)1566 void FormDataMgr::GetNoHostInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
1567 std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
1568 std::map<int64_t, bool> &foundFormsMap)
1569 {
1570 std::lock_guard<std::mutex> lock(formRecordMutex_);
1571 for (auto &formRecordInfo : formRecords_) {
1572 int64_t formId = formRecordInfo.first;
1573 FormRecord &formRecord = formRecordInfo.second;
1574
1575 // Checks the user id and the temp flag.
1576 if (!formRecord.formTempFlag || (userId != formRecord.providerUserId)) {
1577 continue;
1578 }
1579 // check UID
1580 auto iter = std::find(formRecord.formUserUids.begin(), formRecord.formUserUids.end(), callingUid);
1581 if (iter == formRecord.formUserUids.end()) {
1582 continue;
1583 }
1584 // check valid form set
1585 if (matchedFormIds.find(formId) != matchedFormIds.end()) {
1586 continue;
1587 }
1588
1589 HILOG_DEBUG("found invalid form: %{public}" PRId64 "", formId);
1590 formRecord.formUserUids.erase(iter);
1591 if (formRecord.formUserUids.empty()) {
1592 FormIdKey formIdKey(formRecord.bundleName, formRecord.abilityName);
1593 auto itIdsSet = noHostTempFormsMap.find(formIdKey);
1594 if (itIdsSet == noHostTempFormsMap.end()) {
1595 std::set<int64_t> formIdsSet;
1596 formIdsSet.emplace(formId);
1597 noHostTempFormsMap.emplace(formIdKey, formIdsSet);
1598 } else {
1599 itIdsSet->second.emplace(formId);
1600 }
1601 } else {
1602 foundFormsMap.emplace(formId, false);
1603 }
1604 }
1605 }
1606
1607 /**
1608 * @brief handle delete no host temp forms.
1609 * @param callingUid The UID of the proxy.
1610 * @param noHostTempFormsMap The map of the no host forms.
1611 * @param foundFormsMap The map of the found forms.
1612 */
BatchDeleteNoHostTempForms(int32_t callingUid,std::map<FormIdKey,std::set<int64_t>> & noHostTempFormsMap,std::map<int64_t,bool> & foundFormsMap)1613 void FormDataMgr::BatchDeleteNoHostTempForms(int32_t callingUid, std::map<FormIdKey,
1614 std::set<int64_t>> &noHostTempFormsMap,
1615 std::map<int64_t, bool> &foundFormsMap)
1616 {
1617 std::set<FormIdKey> removableModuleSet;
1618 for (auto &noHostTempForm : noHostTempFormsMap) {
1619 FormIdKey formIdKey = noHostTempForm.first;
1620 std::set<int64_t> &formIdsSet = noHostTempForm.second;
1621 std::string bundleName = formIdKey.bundleName;
1622 std::string abilityName = formIdKey.abilityName;
1623 FormProviderMgr::GetInstance().NotifyProviderFormsBatchDelete(bundleName, abilityName, formIdsSet);
1624 for (int64_t formId: formIdsSet) {
1625 foundFormsMap.emplace(formId, true);
1626 StopRenderingForm(formId);
1627 DeleteFormRecord(formId);
1628 DeleteTempForm(formId);
1629 }
1630 }
1631 }
1632
1633 /**
1634 * @brief StopRenderingForm.
1635 * @param formId The form id.
1636 */
StopRenderingForm(int32_t formId)1637 void FormDataMgr::StopRenderingForm(int32_t formId)
1638 {
1639 FormRecord formrecord;
1640 GetFormRecord(formId, formrecord);
1641 FormRenderMgr::GetInstance().StopRenderingForm(formId, formrecord);
1642 }
1643
1644 /**
1645 * @brief delete invalid temp forms.
1646 * @param userId User ID.
1647 * @param callingUid The UID of the proxy.
1648 * @param matchedFormIds The set of the valid forms.
1649 * @param removedFormsMap The map of the removed invalid forms.
1650 * @return Returns ERR_OK on success, others on failure.
1651 */
DeleteInvalidTempForms(int32_t userId,int32_t callingUid,std::set<int64_t> & matchedFormIds,std::map<int64_t,bool> & removedFormsMap)1652 int32_t FormDataMgr::DeleteInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
1653 std::map<int64_t, bool> &removedFormsMap)
1654 {
1655 HILOG_INFO("DeleteInvalidTempForms start, userId = %{public}d, callingUid = %{public}d", userId, callingUid);
1656 std::map<int64_t, bool> foundFormsMap {};
1657 std::map<FormIdKey, std::set<int64_t>> noHostTempFormsMap {};
1658 GetNoHostInvalidTempForms(userId, callingUid, matchedFormIds, noHostTempFormsMap, foundFormsMap);
1659 BatchDeleteNoHostTempForms(callingUid, noHostTempFormsMap, foundFormsMap);
1660 HILOG_DEBUG("foundFormsMap size: %{public}zu", foundFormsMap.size());
1661 HILOG_DEBUG("noHostTempFormsMap size: %{public}zu", noHostTempFormsMap.size());
1662
1663 if (!foundFormsMap.empty()) {
1664 removedFormsMap.insert(foundFormsMap.begin(), foundFormsMap.end());
1665 }
1666 HILOG_INFO("DeleteInvalidTempForms done");
1667 return ERR_OK;
1668 }
1669
1670 /**
1671 * @brief delete publish forms temp data
1672 * @param userId User ID.
1673 * @param bundleName BundleName.
1674 * @param validFormIds The set of the valid forms.
1675 */
DeleteInvalidPublishForms(int32_t userId,std::string bundleName,std::set<int64_t> & validFormIds)1676 void FormDataMgr::DeleteInvalidPublishForms(int32_t userId, std::string bundleName, std::set<int64_t> &validFormIds)
1677 {
1678 HILOG_INFO("DeleteInvalidPublishForms start, userId = %{public}d, bundleName = %{public}s",
1679 userId, bundleName.c_str());
1680
1681 int32_t deleteNum = 0;
1682 std::lock_guard<std::mutex> lock(formRequestPublishFormsMutex_);
1683 for (auto iter = formRequestPublishForms_.begin(); iter != formRequestPublishForms_.end();) {
1684 int64_t formId = iter->first;
1685 // check valid form set
1686 if (validFormIds.find(formId) != validFormIds.end()) {
1687 ++iter;
1688 continue;
1689 }
1690
1691 Want want = iter->second.first;
1692 if (bundleName != want.GetStringParam(Constants::PARAM_BUNDLE_NAME_KEY)) {
1693 ++iter;
1694 continue;
1695 }
1696 if (userId != want.GetIntParam(Constants::PARAM_FORM_USER_ID, -1)) {
1697 ++iter;
1698 continue;
1699 }
1700 ++deleteNum;
1701 iter = formRequestPublishForms_.erase(iter);
1702 }
1703
1704 HILOG_INFO("DeleteInvalidPublishForms done, delete num is %{public}d", deleteNum);
1705 }
1706
1707 /**
1708 * @brief clear host data by invalid forms.
1709 * @param callingUid The UID of the proxy.
1710 * @param removedFormsMap The map of the removed invalid forms.
1711 * @return Returns ERR_OK on success, others on failure.
1712 */
ClearHostDataByInvalidForms(int32_t callingUid,std::map<int64_t,bool> & removedFormsMap)1713 int32_t FormDataMgr::ClearHostDataByInvalidForms(int32_t callingUid, std::map<int64_t, bool> &removedFormsMap)
1714 {
1715 HILOG_INFO("DeleteInvalidForms host start");
1716 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
1717 std::vector<FormHostRecord>::iterator itHostRecord;
1718 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end();) {
1719 if (itHostRecord->GetCallerUid() != callingUid) {
1720 itHostRecord++;
1721 continue;
1722 }
1723 for (auto &removedForm : removedFormsMap) {
1724 if (itHostRecord->Contains(removedForm.first)) {
1725 itHostRecord->DelForm(removedForm.first);
1726 }
1727 }
1728 if (itHostRecord->IsEmpty()) {
1729 itHostRecord->CleanResource();
1730 itHostRecord = clientRecords_.erase(itHostRecord);
1731 } else {
1732 itHostRecord++;
1733 }
1734 }
1735 HILOG_INFO("DeleteInvalidForms host done");
1736 return ERR_OK;
1737 }
1738
AddRequestPublishFormInfo(int64_t formId,const Want & want,std::unique_ptr<FormProviderData> & formProviderData)1739 ErrCode FormDataMgr::AddRequestPublishFormInfo(int64_t formId, const Want &want,
1740 std::unique_ptr<FormProviderData> &formProviderData)
1741 {
1742 HILOG_INFO("add request publish form info, formId: %{public}" PRId64 "", formId);
1743 std::lock_guard<std::mutex> lock(formRequestPublishFormsMutex_);
1744
1745 auto insertResult = formRequestPublishForms_.insert(
1746 std::make_pair(formId, std::make_pair(want, std::move(formProviderData))));
1747 if (!insertResult.second) {
1748 HILOG_ERROR("Failed to emplace requestPublishFormInfo");
1749 return ERR_APPEXECFWK_FORM_COMMON_CODE;
1750 }
1751 return ERR_OK;
1752 }
1753
RemoveRequestPublishFormInfo(int64_t formId)1754 ErrCode FormDataMgr::RemoveRequestPublishFormInfo(int64_t formId)
1755 {
1756 HILOG_INFO("remove request publish form info, formId: %{public}" PRId64 "", formId);
1757 std::lock_guard<std::mutex> lock(formRequestPublishFormsMutex_);
1758 formRequestPublishForms_.erase(formId);
1759 return ERR_OK;
1760 }
1761
IsRequestPublishForm(int64_t formId)1762 bool FormDataMgr::IsRequestPublishForm(int64_t formId)
1763 {
1764 std::lock_guard<std::mutex> lock(formRequestPublishFormsMutex_);
1765 return formRequestPublishForms_.find(formId) != formRequestPublishForms_.end();
1766 }
1767
GetRequestPublishFormInfo(int64_t formId,Want & want,std::unique_ptr<FormProviderData> & formProviderData)1768 ErrCode FormDataMgr::GetRequestPublishFormInfo(int64_t formId, Want &want,
1769 std::unique_ptr<FormProviderData> &formProviderData)
1770 {
1771 HILOG_INFO("get request publish form info, formId: %{public}" PRId64 "", formId);
1772 std::lock_guard<std::mutex> lock(formRequestPublishFormsMutex_);
1773 auto result = formRequestPublishForms_.find(formId);
1774 if (result == formRequestPublishForms_.end()) {
1775 HILOG_INFO("request publish form id not found, formId: %{public}" PRId64 "", formId);
1776 return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1777 }
1778
1779 want = result->second.first;
1780 formProviderData = std::move(result->second.second);
1781 formRequestPublishForms_.erase(result);
1782 return ERR_OK;
1783 }
1784
GetPackageForm(const FormRecord & record,const BundlePackInfo & bundlePackInfo,AbilityFormInfo & abilityFormInfo)1785 bool FormDataMgr::GetPackageForm(const FormRecord &record, const BundlePackInfo &bundlePackInfo,
1786 AbilityFormInfo &abilityFormInfo)
1787 {
1788 HILOG_INFO("%{public}s start, moduleName is %{public}s", __func__, record.moduleName.c_str());
1789 std::vector<PackageModule> modules = bundlePackInfo.summary.modules;
1790 for (const auto &cfg : modules) {
1791 HILOG_INFO("%{public}s, try module %{public}s", __func__, cfg.distro.moduleName.c_str());
1792 if (record.moduleName != cfg.distro.moduleName) {
1793 continue;
1794 }
1795 HILOG_INFO("%{public}s, has the same module", __func__);
1796 std::vector<ModuleAbilityInfo> abilities = cfg.abilities;
1797 std::vector<ExtensionAbilities> extensionAbilities = cfg.extensionAbilities;
1798 if (!abilities.empty()) {
1799 return GetAbilityFormInfo(record, abilities, abilityFormInfo);
1800 }
1801 if (!extensionAbilities.empty()) {
1802 return GetAbilityFormInfo(record, extensionAbilities, abilityFormInfo);
1803 }
1804 HILOG_WARN("%{public}s, no ability in module:%{public}s", __func__, record.moduleName.c_str());
1805 return false;
1806 }
1807 return false;
1808 }
1809
1810 template<typename T>
GetAbilityFormInfo(const FormRecord & record,const std::vector<T> & abilities,AbilityFormInfo & abilityFormInfo)1811 bool FormDataMgr::GetAbilityFormInfo(const FormRecord &record, const std::vector<T> &abilities,
1812 AbilityFormInfo &abilityFormInfo)
1813 {
1814 for (const T &abilityInfo : abilities) {
1815 if (abilityInfo.name != record.abilityName) {
1816 continue;
1817 }
1818 std::vector<AbilityFormInfo> forms = abilityInfo.forms;
1819 for (auto &item : forms) {
1820 if (IsSameForm(record, item)) {
1821 abilityFormInfo = item;
1822 HILOG_INFO("%{public}s find matched abilityFormInfo", __func__);
1823 return true;
1824 }
1825 }
1826 }
1827 HILOG_INFO("%{public}s, no matched abilityFormInfo, module is %{public}s", __func__, record.moduleName.c_str());
1828 return false;
1829 }
1830
IsSameForm(const FormRecord & record,const AbilityFormInfo & abilityFormInfo)1831 bool FormDataMgr::IsSameForm(const FormRecord &record, const AbilityFormInfo &abilityFormInfo)
1832 {
1833 auto dimensionIter = Constants::DIMENSION_MAP.find(static_cast<Constants::Dimension>(record.specification));
1834 if (dimensionIter == Constants::DIMENSION_MAP.end()) {
1835 HILOG_ERROR("%{public}s, specification:%{public}d is invalid", __func__, record.specification);
1836 return false;
1837 }
1838 auto dimension = dimensionIter->second;
1839 auto supportDimensions = abilityFormInfo.supportDimensions;
1840 if (record.formName == abilityFormInfo.name &&
1841 std::find(supportDimensions.begin(), supportDimensions.end(), dimension) != supportDimensions.end()) {
1842 return true;
1843 }
1844
1845 HILOG_INFO("%{public}s, no same form, record is %{public}s, dimension is %{public}s, abilityFormInfo is %{public}s",
1846 __func__, record.formName.c_str(), dimension.c_str(), abilityFormInfo.name.c_str());
1847
1848 return false;
1849 }
1850
SetRecordNeedFreeInstall(int64_t formId,bool isNeedFreeInstall)1851 bool FormDataMgr::SetRecordNeedFreeInstall(int64_t formId, bool isNeedFreeInstall)
1852 {
1853 HILOG_INFO("%{public}s, get form record by formId", __func__);
1854 std::lock_guard<std::mutex> lock(formRecordMutex_);
1855 auto item = formRecords_.find(formId);
1856 if (item == formRecords_.end()) {
1857 HILOG_ERROR("%{public}s, form record not found", __func__);
1858 return false;
1859 }
1860 item->second.needFreeInstall = isNeedFreeInstall;
1861 HILOG_INFO("%{public}s, set form record need free install flag successfully", __func__);
1862 return true;
1863 }
1864
CheckInvalidForm(const int64_t formId)1865 ErrCode FormDataMgr::CheckInvalidForm(const int64_t formId)
1866 {
1867 // Checks if the formid is valid.
1868 if (formId <= 0) {
1869 HILOG_ERROR("Invalid form id.");
1870 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1871 }
1872
1873 // Gets the corresponding userId by formId.
1874 FormRecord formRecord;
1875 int64_t matchedFormId = FindMatchedFormId(formId);
1876 if (!GetFormRecord(matchedFormId, formRecord)) {
1877 HILOG_ERROR("No matching formRecord was found for the form id.");
1878 return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
1879 }
1880
1881 // Checks for cross-user operations.
1882 if (formRecord.providerUserId != FormUtil::GetCurrentAccountId()) {
1883 HILOG_ERROR("The form id corresponds to a card that is not for the currently active user.");
1884 return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1885 }
1886 return ERR_OK;
1887 }
1888
GetRunningFormInfosByFormId(const int64_t formId,RunningFormInfo & runningFormInfo)1889 ErrCode FormDataMgr::GetRunningFormInfosByFormId(const int64_t formId, RunningFormInfo &runningFormInfo)
1890 {
1891 HILOG_DEBUG("start");
1892
1893 // Checks if the formid is valid.
1894 if (formId <= 0) {
1895 HILOG_ERROR("Invalid form id.");
1896 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1897 }
1898
1899 FormRecord formRecord;
1900 int64_t matchedFormId = FindMatchedFormId(formId);
1901 if (!GetFormRecord(matchedFormId, formRecord)) {
1902 HILOG_ERROR("No matching formRecord was found for the form id.");
1903 return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
1904 }
1905
1906 if (formRecord.providerUserId != FormUtil::GetCurrentAccountId()) {
1907 HILOG_ERROR("The form id corresponds to a card that is not for the currently active user.");
1908 return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1909 }
1910
1911 runningFormInfo.formId = matchedFormId;
1912 runningFormInfo.formName = formRecord.formName;
1913 runningFormInfo.dimension = formRecord.specification;
1914 runningFormInfo.bundleName = formRecord.bundleName;
1915 runningFormInfo.moduleName = formRecord.moduleName;
1916 runningFormInfo.abilityName = formRecord.abilityName;
1917 std::vector<FormHostRecord> formHostRecords;
1918 GetFormHostRecord(matchedFormId, formHostRecords);
1919 if (formHostRecords.empty()) {
1920 HILOG_ERROR("fail, clientHost is empty");
1921 return ERR_APPEXECFWK_FORM_COMMON_CODE;
1922 }
1923 runningFormInfo.hostBundleName = formHostRecords.begin()->GetHostBundleName();
1924 runningFormInfo.formVisiblity = formRecord.isVisible ? FormVisibilityType::VISIBLE : FormVisibilityType::INVISIBLE;
1925 return ERR_OK;
1926 }
1927
HandleFormAddObserver(const std::string hostBundleName,const int64_t formId)1928 ErrCode FormDataMgr::HandleFormAddObserver(const std::string hostBundleName, const int64_t formId)
1929 {
1930 HILOG_DEBUG("start");
1931 RunningFormInfo runningFormInfo;
1932 ErrCode ret = GetRunningFormInfosByFormId(formId, runningFormInfo);
1933 if (ret != ERR_OK) {
1934 return ret;
1935 }
1936 // if there is a full observer.
1937 FormObserverRecord::GetInstance().onFormAdd("all", runningFormInfo);
1938 // If there is a listener for the current host.
1939 FormObserverRecord::GetInstance().onFormAdd(hostBundleName, runningFormInfo);
1940 return ERR_OK;
1941 }
1942
HandleFormRemoveObserver(const std::string hostBundleName,const RunningFormInfo runningFormInfo)1943 ErrCode FormDataMgr::HandleFormRemoveObserver(const std::string hostBundleName, const RunningFormInfo runningFormInfo)
1944 {
1945 HILOG_DEBUG("start");
1946 // if there is a full observer.
1947 FormObserverRecord::GetInstance().onFormRemove("all", runningFormInfo);
1948 // If there is a listener for the current host.
1949 FormObserverRecord::GetInstance().onFormRemove(hostBundleName, runningFormInfo);
1950 return ERR_OK;
1951 }
1952
GetTempFormsCount(int32_t & formCount)1953 int32_t FormDataMgr::GetTempFormsCount(int32_t &formCount)
1954 {
1955 std::lock_guard<std::mutex> lock(formTempMutex_);
1956 formCount = static_cast<int32_t>(tempForms_.size());
1957 HILOG_DEBUG("%{public}s, current exist %{public}d temp forms in system", __func__, formCount);
1958 return ERR_OK;
1959 }
1960
GetCastFormsCount(int32_t & formCount)1961 int32_t FormDataMgr::GetCastFormsCount(int32_t &formCount)
1962 {
1963 std::lock_guard<std::mutex> lock(formRecordMutex_);
1964 for (const auto &recordPair : formRecords_) {
1965 FormRecord record = recordPair.second;
1966 if (!record.formTempFlag) {
1967 formCount++;
1968 }
1969 }
1970 HILOG_DEBUG("%{public}s, current exist %{public}d cast forms in system", __func__, formCount);
1971 return ERR_OK;
1972 }
1973
GetHostFormsCount(const std::string & bundleName,int32_t & formCount)1974 int32_t FormDataMgr::GetHostFormsCount(const std::string &bundleName, int32_t &formCount)
1975 {
1976 if (bundleName.empty()) {
1977 return ERR_OK;
1978 }
1979 std::lock_guard<std::mutex> lock(formHostRecordMutex_);
1980 for (auto &record : clientRecords_) {
1981 if (record.GetHostBundleName() == bundleName) {
1982 formCount = record.GetFormsCount();
1983 break;
1984 }
1985 }
1986 HILOG_DEBUG("%{public}s, current exist %{public}d cast forms in host", __func__, formCount);
1987 return ERR_OK;
1988 }
1989
GetFormInstancesByFilter(const FormInstancesFilter & formInstancesFilter,std::vector<FormInstance> & formInstances)1990 ErrCode FormDataMgr::GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
1991 std::vector<FormInstance> &formInstances)
1992 {
1993 HILOG_DEBUG("get form instances by filter");
1994 std::lock_guard<std::mutex> lock(formRecordMutex_);
1995 std::map<int64_t, FormRecord>::iterator itFormRecord;
1996 if (formInstancesFilter.bundleName.empty()) {
1997 HILOG_ERROR("formInstancesFilter.bundleName is null");
1998 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1999 }
2000
2001 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
2002 if (formInstancesFilter.bundleName == itFormRecord->second.bundleName) {
2003 bool Needgetformhostrecordflag = true;
2004 if (!formInstancesFilter.moduleName.empty() &&
2005 formInstancesFilter.moduleName != itFormRecord->second.moduleName) {
2006 Needgetformhostrecordflag = false;
2007 } else if (!formInstancesFilter.abilityName.empty() &&
2008 formInstancesFilter.abilityName != itFormRecord->second.abilityName) {
2009 Needgetformhostrecordflag = false;
2010 } else if (!formInstancesFilter.formName.empty() &&
2011 formInstancesFilter.formName != itFormRecord->second.formName) {
2012 Needgetformhostrecordflag = false;
2013 }
2014 std::vector<FormHostRecord> formHostRecords;
2015 GetFormHostRecord(itFormRecord->second.formId, formHostRecords);
2016 if (Needgetformhostrecordflag) {
2017 FormInstance instance;
2018 for (auto formHostRecord : formHostRecords) {
2019 instance.formHostName = formHostRecord.GetHostBundleName();
2020 instance.formId = itFormRecord->second.formId;
2021 instance.specification = itFormRecord->second.specification;
2022 instance.formVisiblity = static_cast<FormVisibilityType>(itFormRecord->second.formVisibleNotifyState);
2023 instance.bundleName = itFormRecord->second.bundleName;
2024 instance.moduleName = itFormRecord->second.moduleName;
2025 instance.abilityName = itFormRecord->second.abilityName;
2026 instance.formName = itFormRecord->second.formName;
2027 formInstances.emplace_back(instance);
2028 }
2029 }
2030 }
2031 }
2032 if (formInstances.size() == 0) {
2033 return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED;
2034 }
2035 return ERR_OK;
2036 }
2037
GetFormInstanceById(const int64_t formId,FormInstance & formInstance)2038 ErrCode FormDataMgr::GetFormInstanceById(const int64_t formId, FormInstance &formInstance)
2039 {
2040 HILOG_DEBUG("get form instance by formId");
2041 std::lock_guard<std::mutex> lock(formRecordMutex_);
2042 if (formId <= 0) {
2043 HILOG_ERROR("formId is invalid");
2044 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2045 }
2046 auto info = formRecords_.find(formId);
2047 if (info != formRecords_.end()) {
2048 FormRecord formRecord = info->second;
2049 std::vector<FormHostRecord> formHostRecords;
2050 GetFormHostRecord(formId, formHostRecords);
2051 if (formHostRecords.empty()) {
2052 HILOG_ERROR("fail, clientHost is empty");
2053 return ERR_APPEXECFWK_FORM_COMMON_CODE;
2054 }
2055 formInstance.formHostName = formHostRecords.begin()->GetHostBundleName();
2056 formInstance.formId = formRecord.formId;
2057 formInstance.specification = formRecord.specification;
2058 formInstance.formVisiblity = static_cast<FormVisibilityType>(formRecord.formVisibleNotifyState);
2059 formInstance.bundleName = formRecord.bundleName;
2060 formInstance.moduleName = formRecord.moduleName;
2061 formInstance.abilityName = formRecord.abilityName;
2062 formInstance.formName = formRecord.formName;
2063 } else {
2064 return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED;
2065 }
2066 HILOG_DEBUG("get form instance successfully");
2067 return ERR_OK;
2068 }
2069
GetFormInstanceById(const int64_t formId,bool isIncludeUnused,FormInstance & formInstance)2070 ErrCode FormDataMgr::GetFormInstanceById(const int64_t formId, bool isIncludeUnused, FormInstance &formInstance)
2071 {
2072 HILOG_DEBUG("get form instance by formId");
2073 if (formId <= 0) {
2074 HILOG_ERROR("formId is invalid");
2075 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2076 }
2077 FormRecord formRecord;
2078 bool formRecordExist = false;
2079 {
2080 std::lock_guard<std::mutex> lock(formRecordMutex_);
2081 auto info = formRecords_.find(formId);
2082 if (info != formRecords_.end()) {
2083 formRecord = info->second;
2084 formRecordExist = true;
2085 }
2086 }
2087 if (formRecordExist) {
2088 std::vector<FormHostRecord> formHostRecords;
2089 GetFormHostRecord(formId, formHostRecords);
2090 if (formHostRecords.empty()) {
2091 HILOG_ERROR("fail, clientHost is empty");
2092 return ERR_APPEXECFWK_FORM_COMMON_CODE;
2093 }
2094 formInstance.formHostName = formHostRecords.begin()->GetHostBundleName();
2095 formInstance.formId = formRecord.formId;
2096 formInstance.specification = formRecord.specification;
2097 formInstance.formVisiblity = static_cast<FormVisibilityType>(formRecord.formVisibleNotifyState);
2098 formInstance.bundleName = formRecord.bundleName;
2099 formInstance.moduleName = formRecord.moduleName;
2100 formInstance.abilityName = formRecord.abilityName;
2101 formInstance.formName = formRecord.formName;
2102 } else if (isIncludeUnused == true) {
2103 FormRecord dbRecord;
2104 ErrCode getDbRet = FormDbCache::GetInstance().GetDBRecord(formId, dbRecord);
2105 if (getDbRet == ERR_OK) {
2106 formInstance.formHostName = "";
2107 formInstance.formId = formId;
2108 formInstance.specification = dbRecord.specification;
2109 formInstance.formVisiblity = static_cast<FormVisibilityType>(dbRecord.formVisibleNotifyState);
2110 formInstance.bundleName = dbRecord.bundleName;
2111 formInstance.moduleName = dbRecord.moduleName;
2112 formInstance.abilityName = dbRecord.abilityName;
2113 formInstance.formName = dbRecord.formName;
2114 }
2115 } else {
2116 return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED;
2117 }
2118 HILOG_DEBUG("get form instance successfully");
2119 return ERR_OK;
2120 }
2121
GetRunningFormInfos(std::vector<RunningFormInfo> & runningFormInfos)2122 ErrCode FormDataMgr::GetRunningFormInfos(std::vector<RunningFormInfo> &runningFormInfos)
2123 {
2124 HILOG_DEBUG("start");
2125 std::lock_guard<std::mutex> lock(formRecordMutex_);
2126 for (auto record : formRecords_) {
2127 if ((!record.second.formTempFlag) &&
2128 ((FormUtil::GetCurrentAccountId() == record.second.providerUserId) ||
2129 (record.second.providerUserId == Constants::DEFAULT_USER_ID))) {
2130 RunningFormInfo info;
2131 info.formId = record.first;
2132 info.formName = record.second.formName;
2133 info.dimension = record.second.specification;
2134 info.bundleName = record.second.bundleName;
2135 info.moduleName = record.second.moduleName;
2136 info.abilityName = record.second.abilityName;
2137 info.formVisiblity = static_cast<FormVisibilityType>(record.second.formVisibleNotifyState);
2138 std::vector<FormHostRecord> formHostRecords;
2139 GetFormHostRecord(record.first, formHostRecords);
2140 if (formHostRecords.empty()) {
2141 HILOG_ERROR("fail, clientHost is empty");
2142 return ERR_APPEXECFWK_FORM_COMMON_CODE;
2143 }
2144 info.hostBundleName = formHostRecords.begin()->GetHostBundleName();
2145 runningFormInfos.emplace_back(info);
2146 }
2147 }
2148 return ERR_OK;
2149 }
2150
GetRunningFormInfosByBundleName(const std::string & bundleName,std::vector<RunningFormInfo> & runningFormInfos)2151 ErrCode FormDataMgr::GetRunningFormInfosByBundleName(const std::string &bundleName,
2152 std::vector<RunningFormInfo> &runningFormInfos)
2153 {
2154 HILOG_DEBUG("start");
2155
2156 if (bundleName.empty()) {
2157 HILOG_ERROR("bundleName is empty.");
2158 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2159 }
2160
2161 std::lock_guard<std::mutex> lock(formRecordMutex_);
2162 for (auto record : formRecords_) {
2163 for (auto uid : record.second.formUserUids) {
2164 std::string hostBundleName = "";
2165 auto ret = FormBmsHelper::GetInstance().GetBundleNameByUid(uid, hostBundleName);
2166 if (ret != ERR_OK) {
2167 return ret;
2168 }
2169 bool flag = (!record.second.formTempFlag) &&
2170 ((FormUtil::GetCurrentAccountId() == record.second.providerUserId) ||
2171 (record.second.providerUserId == Constants::DEFAULT_USER_ID));
2172 if (hostBundleName == bundleName && flag) {
2173 RunningFormInfo info;
2174 info.formId = record.first;
2175 info.formName = record.second.formName;
2176 info.dimension = record.second.specification;
2177 info.bundleName = record.second.bundleName;
2178 info.hostBundleName = bundleName;
2179 info.moduleName = record.second.moduleName;
2180 info.abilityName = record.second.abilityName;
2181 info.formVisiblity = static_cast<FormVisibilityType>(record.second.formVisibleNotifyState);
2182 runningFormInfos.emplace_back(info);
2183 }
2184 }
2185 }
2186 if (runningFormInfos.size() == 0) {
2187 return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED;
2188 }
2189 return ERR_OK;
2190 }
2191 } // namespace AppExecFwk
2192 } // namespace OHOS
2193