1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "form_data_mgr.h"
17
18 #include <cinttypes>
19 #include <type_traits>
20
21 #include "form_cache_mgr.h"
22 #include "form_constants.h"
23 #include "form_mgr_errors.h"
24 #include "form_provider_mgr.h"
25 #include "form_render_mgr.h"
26 #include "form_util.h"
27 #include "hilog_wrapper.h"
28 #include "ipc_skeleton.h"
29
30 namespace OHOS {
31 namespace AppExecFwk {
FormDataMgr()32 FormDataMgr::FormDataMgr()
33 {
34 HILOG_INFO("create form data manager instance");
35 }
~FormDataMgr()36 FormDataMgr::~FormDataMgr()
37 {
38 HILOG_INFO("destroy form data manager instance");
39 }
40
41 /**
42 * @brief Allot form info by item info.
43 * @param formInfo Form item info.
44 * @param callingUid The UID of the proxy.
45 * @param userId User ID.
46 * @return Returns form record.
47 */
AllotFormRecord(const FormItemInfo & formInfo,const int callingUid,const int32_t userId)48 FormRecord FormDataMgr::AllotFormRecord(const FormItemInfo &formInfo, const int callingUid, const int32_t userId)
49 {
50 HILOG_INFO("%{public}s, allot form info", __func__);
51 if (formInfo.IsTemporaryForm() && !ExistTempForm(formInfo.GetFormId())) {
52 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
53 tempForms_.emplace_back(formInfo.GetFormId());
54 }
55 FormRecord record;
56 {
57 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
58 if (formRecords_.empty()) { // formRecords_ is empty, create a new one
59 HILOG_DEBUG("%{public}s, form info not exist", __func__);
60 record = CreateFormRecord(formInfo, callingUid, userId);
61 formRecords_.emplace(formInfo.GetFormId(), record);
62 } else {
63 auto info = formRecords_.find(formInfo.GetFormId());
64 if (info == formRecords_.end()) {
65 HILOG_DEBUG("%{public}s, form info not find", __func__);
66 record = CreateFormRecord(formInfo, callingUid, userId);
67 formRecords_.emplace(formInfo.GetFormId(), record);
68 } else {
69 record = info->second;
70 }
71 }
72 }
73 HILOG_INFO("%{public}s end", __func__);
74 return record;
75 }
76 /**
77 * @brief Delete form js info by form record.
78 * @param formId The Id of the form.
79 * @return Returns true if this function is successfully called; returns false otherwise.
80 */
DeleteFormRecord(const int64_t formId)81 bool FormDataMgr::DeleteFormRecord(const int64_t formId)
82 {
83 HILOG_INFO("%{public}s, delete form info", __func__);
84 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
85 auto iter = formRecords_.find(formId);
86 if (iter == formRecords_.end()) {
87 HILOG_ERROR("%{public}s, form info is not exist", __func__);
88 return false;
89 }
90 formRecords_.erase(iter);
91 return true;
92 }
93 /**
94 * @brief Allot form host record by caller token.
95 * @param info The form item info.
96 * @param callerToken callerToken
97 * @param formId The Id of the form.
98 * @param callingUid The UID of the proxy.
99 * @return Returns true if this function is successfully called; returns false otherwise.
100 */
AllotFormHostRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,const int64_t formId,const int callingUid)101 bool FormDataMgr::AllotFormHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
102 const int64_t formId, const int callingUid)
103 {
104 HILOG_INFO("%{public}s, allot form Host info", __func__);
105 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
106 for (auto &record : clientRecords_) {
107 if (callerToken == record.GetFormHostClient()) {
108 record.AddForm(formId);
109 HILOG_INFO("%{public}s end", __func__);
110 return true;
111 }
112 }
113 FormHostRecord hostRecord;
114 bool isCreated = CreateHostRecord(info, callerToken, callingUid, hostRecord);
115 if (isCreated) {
116 hostRecord.AddForm(formId);
117 clientRecords_.emplace_back(hostRecord);
118 HILOG_INFO("%{public}s end", __func__);
119 return true;
120 }
121 HILOG_INFO("%{public}s end", __func__);
122 return false;
123 }
124 /**
125 * @brief Create host record.
126 * @param info The form item info.
127 * @param callerToken The UID of the proxy.
128 * @param callingUid The UID of the proxy.
129 * @param record The form host record.
130 * @return Returns true if this function is successfully called; returns false otherwise.
131 */
CreateHostRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,const int callingUid,FormHostRecord & record)132 bool FormDataMgr::CreateHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
133 const int callingUid, FormHostRecord& record)
134 {
135 if (callerToken == nullptr) {
136 HILOG_ERROR("%{public}s, invalid param", __func__);
137 return false;
138 }
139
140 record = FormHostRecord::CreateRecord(info, callerToken, callingUid);
141 return true;
142 }
143 /**
144 * @brief Create form record.
145 * @param formInfo The form item info.
146 * @param callingUid The UID of the proxy.
147 * @param userId User ID.
148 * @return Form record.
149 */
CreateFormRecord(const FormItemInfo & formInfo,const int callingUid,const int32_t userId) const150 FormRecord FormDataMgr::CreateFormRecord(const FormItemInfo &formInfo, const int callingUid, const int32_t userId) const
151 {
152 HILOG_INFO("%{public}s, create form info", __func__);
153 FormRecord newRecord;
154 newRecord.formId = formInfo.GetFormId();
155 newRecord.userId = userId;
156 newRecord.packageName = formInfo.GetPackageName();
157 newRecord.bundleName = formInfo.GetProviderBundleName();
158 newRecord.moduleName = formInfo.GetModuleName();
159 newRecord.abilityName = formInfo.GetAbilityName();
160 newRecord.formName = formInfo.GetFormName();
161 newRecord.specification = formInfo.GetSpecificationId();
162 newRecord.isEnableUpdate = formInfo.IsEnableUpdateFlag();
163 newRecord.formTempFlag = formInfo.IsTemporaryForm();
164 newRecord.formVisibleNotify = formInfo.IsFormVisibleNotify();
165 newRecord.jsFormCodePath = formInfo.GetHapSourceByModuleName(newRecord.moduleName);
166 newRecord.formSrc = formInfo.GetFormSrc();
167 newRecord.formWindow = formInfo.GetFormWindow();
168 newRecord.versionName = formInfo.GetVersionName();
169 newRecord.versionCode = formInfo.GetVersionCode();
170 newRecord.compatibleVersion = formInfo.GetCompatibleVersion();
171 newRecord.formVisibleNotifyState = 0;
172 newRecord.type = formInfo.GetType();
173 newRecord.uiSyntax = formInfo.GetUiSyntax();
174 if (newRecord.isEnableUpdate) {
175 ParseUpdateConfig(newRecord, formInfo);
176 }
177 if (std::find(newRecord.formUserUids.begin(), newRecord.formUserUids.end(),
178 callingUid) == newRecord.formUserUids.end()) {
179 newRecord.formUserUids.emplace_back(callingUid);
180 }
181
182 formInfo.GetHapSourceDirs(newRecord.hapSourceDirs);
183 HILOG_INFO("%{public}s end", __func__);
184 return newRecord;
185 }
186 /**
187 * @brief Create form js info by form record.
188 * @param formId The Id of the form.
189 * @param record Form record.
190 * @param formInfo Js form info.
191 */
CreateFormJsInfo(const int64_t formId,const FormRecord & record,FormJsInfo & formInfo)192 void FormDataMgr::CreateFormJsInfo(const int64_t formId, const FormRecord &record, FormJsInfo &formInfo)
193 {
194 formInfo.formId = formId;
195 formInfo.bundleName = record.bundleName;
196 formInfo.abilityName = record.abilityName;
197 formInfo.formName = record.formName;
198 formInfo.moduleName = record.moduleName;
199 formInfo.formTempFlag = record.formTempFlag;
200 formInfo.jsFormCodePath = record.jsFormCodePath;
201 formInfo.formSrc = record.formSrc;
202 formInfo.formWindow = record.formWindow;
203 formInfo.versionCode = record.versionCode;
204 formInfo.versionName = record.versionName;
205 formInfo.compatibleVersion = record.compatibleVersion;
206 formInfo.type = record.type;
207 formInfo.uiSyntax = record.uiSyntax;
208 }
209 /**
210 * @brief Check temp form count is max.
211 * @return Returns ERR_OK if the temp form not reached; returns ERR_MAX_SYSTEM_TEMP_FORMS is reached.
212 */
CheckTempEnoughForm() const213 int FormDataMgr::CheckTempEnoughForm() const
214 {
215 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
216 if (tempForms_.size() >= Constants::MAX_TEMP_FORMS) {
217 HILOG_WARN("%{public}s, already exist %{public}d temp forms in system", __func__, Constants::MAX_TEMP_FORMS);
218 return ERR_APPEXECFWK_FORM_MAX_SYSTEM_TEMP_FORMS;
219 }
220 return ERR_OK;
221 }
222 /**
223 * @brief Check form count is max.
224 * @param callingUid The UID of the proxy.
225 * @param currentUserId The current userId.
226 * @return Returns true if this function is successfully called; returns false otherwise.
227 */
CheckEnoughForm(const int callingUid,const int32_t currentUserId) const228 int FormDataMgr::CheckEnoughForm(const int callingUid, const int32_t currentUserId) const
229 {
230 HILOG_INFO("%{public}s, callingUid: %{public}d, current userId: %{public}d", __func__, callingUid, currentUserId);
231
232 int formsInSystem = 0;
233 int callingUidFormCounts = 0;
234 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
235 for (const auto &recordPair : formRecords_) {
236 FormRecord record = recordPair.second;
237 if ((record.userId == currentUserId) && !record.formTempFlag) {
238 if (++formsInSystem >= Constants::MAX_FORMS) {
239 HILOG_WARN("%{public}s, already exist %{public}d forms in system", __func__, Constants::MAX_FORMS);
240 return ERR_APPEXECFWK_FORM_MAX_SYSTEM_FORMS;
241 }
242 for (const auto &userUid : record.formUserUids) {
243 if (userUid != callingUid) {
244 continue;
245 }
246 if (++callingUidFormCounts >= Constants::MAX_RECORD_PER_APP) {
247 HILOG_WARN("%{public}s, already use %{public}d forms", __func__, Constants::MAX_RECORD_PER_APP);
248 return ERR_APPEXECFWK_FORM_MAX_FORMS_PER_CLIENT;
249 }
250 break;
251 }
252 }
253 }
254 return ERR_OK;
255 }
256 /**
257 * @brief Delete temp form.
258 * @param formId The Id of the form.
259 * @return Returns true if this function is successfully called; returns false otherwise.
260 */
DeleteTempForm(const int64_t formId)261 bool FormDataMgr::DeleteTempForm(const int64_t formId)
262 {
263 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
264 auto iter = std::find(tempForms_.begin(), tempForms_.end(), formId);
265 if (iter == tempForms_.end()) {
266 HILOG_ERROR("%{public}s, temp form is not exist", __func__);
267 return false;
268 }
269 tempForms_.erase(iter);
270 return true;
271 }
272 /**
273 * @brief Check temp form is exist.
274 * @param formId The Id of the form.
275 * @return Returns true if the temp form is exist; returns false is not exist.
276 */
ExistTempForm(const int64_t formId) const277 bool FormDataMgr::ExistTempForm(const int64_t formId) const
278 {
279 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
280 return (std::find(tempForms_.begin(), tempForms_.end(), formId) != tempForms_.end());
281 }
282 /**
283 * @brief Check calling uid is valid.
284 * @param formUserUids The form user uids.
285 * @return Returns true if this user uid is valid; returns false otherwise.
286 */
IsCallingUidValid(const std::vector<int> & formUserUids) const287 bool FormDataMgr::IsCallingUidValid(const std::vector<int> &formUserUids) const
288 {
289 if (formUserUids.empty()) {
290 return false;
291 }
292 for (const auto &userUid : formUserUids) {
293 if (userUid == IPCSkeleton::GetCallingUid()) {
294 return true;
295 }
296 }
297 return false;
298 }
299 /**
300 * @brief Modify form temp flag by formId.
301 * @param formId The Id of the form.
302 * @param formTempFlag The form temp flag.
303 * @return Returns true if this function is successfully called; returns false otherwise.
304 */
ModifyFormTempFlag(const int64_t formId,const bool formTempFlag)305 bool FormDataMgr::ModifyFormTempFlag(const int64_t formId, const bool formTempFlag)
306 {
307 HILOG_INFO("%{public}s, modify form temp flag by formId", __func__);
308 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
309 if (!ExistFormRecord(formId)) {
310 HILOG_ERROR("%{public}s, form info is not exist", __func__);
311 return false;
312 }
313 formRecords_[formId].formTempFlag = formTempFlag;
314 return true;
315 }
316 /**
317 * @brief Add form user uid from form record.
318 * @param formId The Id of the form.
319 * @param formRecord The form record.
320 * @return Returns true if this function is successfully called; returns false otherwise.
321 */
AddFormUserUid(const int64_t formId,const int formUserUid)322 bool FormDataMgr::AddFormUserUid(const int64_t formId, const int formUserUid)
323 {
324 HILOG_INFO("%{public}s, add form user uid by formId", __func__);
325 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
326 if (!ExistFormRecord(formId)) {
327 HILOG_ERROR("%{public}s, form info is not exist", __func__);
328 return false;
329 }
330 if (std::find(formRecords_[formId].formUserUids.begin(), formRecords_[formId].formUserUids.end(),
331 formUserUid) == formRecords_[formId].formUserUids.end()) {
332 formRecords_[formId].formUserUids.emplace_back(formUserUid);
333 }
334 return true;
335 }
336 /**
337 * @brief Delete form user uid from form record.
338 * @param formId The Id of the form.
339 * @param uid calling user id.
340 * @return Returns true if this function is successfully called; returns false otherwise.
341 */
DeleteFormUserUid(const int64_t formId,const int uid)342 bool FormDataMgr::DeleteFormUserUid(const int64_t formId, const int uid)
343 {
344 HILOG_INFO("%{public}s, delete form user uid from form record", __func__);
345 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
346 if (ExistFormRecord(formId)) {
347 auto iter = std::find(formRecords_.at(formId).formUserUids.begin(),
348 formRecords_.at(formId).formUserUids.end(), uid);
349 if (iter != formRecords_.at(formId).formUserUids.end()) {
350 formRecords_.at(formId).formUserUids.erase(iter);
351 }
352 return true;
353 } else {
354 HILOG_ERROR("%{public}s, form info not find", __func__);
355 return false;
356 }
357 }
358 /**
359 * @brief Update form record.
360 * @param formId The Id of the form.
361 * @param formRecord The form record.
362 * @return Returns true if this function is successfully called; returns false otherwise.
363 */
UpdateFormRecord(const int64_t formId,const FormRecord & formRecord)364 bool FormDataMgr::UpdateFormRecord(const int64_t formId, const FormRecord &formRecord)
365 {
366 HILOG_INFO("%{public}s, get form record by formId", __func__);
367 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
368 auto info = formRecords_.find(formId);
369 if (info != formRecords_.end()) {
370 formRecords_[formId] = formRecord;
371 return true;
372 }
373 return false;
374 }
375 /**
376 * @brief Get form record.
377 * @param formId The Id of the form.
378 * @param formRecord The form record.
379 * @return Returns true if this function is successfully called; returns false otherwise.
380 */
GetFormRecord(const int64_t formId,FormRecord & formRecord) const381 bool FormDataMgr::GetFormRecord(const int64_t formId, FormRecord &formRecord) const
382 {
383 HILOG_INFO("%{public}s, get form record by formId", __func__);
384 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
385 auto info = formRecords_.find(formId);
386 if (info == formRecords_.end()) {
387 HILOG_ERROR("%{public}s, form info not find", __func__);
388 return false;
389 }
390 formRecord = info->second;
391
392 HILOG_INFO("%{public}s, get form record successfully", __func__);
393 return true;
394 }
395 /**
396 * @brief Get form record.
397 * @param bundleName Bundle name.
398 * @param formInfos The form record.
399 * @return Returns true if this function is successfully called; returns false otherwise.
400 */
GetFormRecord(const std::string & bundleName,std::vector<FormRecord> & formInfos)401 bool FormDataMgr::GetFormRecord(const std::string &bundleName, std::vector<FormRecord> &formInfos)
402 {
403 HILOG_INFO("%{public}s, get form record by bundleName", __func__);
404 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
405 std::map<int64_t, FormRecord>::iterator itFormRecord;
406 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
407 if (bundleName == itFormRecord->second.bundleName) {
408 formInfos.emplace_back(itFormRecord->second);
409 }
410 }
411 if (formInfos.size() > 0) {
412 return true;
413 } else {
414 HILOG_INFO("%{public}s, form info not find", __func__);
415 return false;
416 }
417 }
418 /**
419 * @brief Check form record is exist.
420 * @param formId The Id of the form.
421 * @return Returns true if the form record is exist; returns false is not exist.
422 */
ExistFormRecord(const int64_t formId) const423 bool FormDataMgr::ExistFormRecord(const int64_t formId) const
424 {
425 HILOG_INFO("%{public}s, check form record is exist", __func__);
426 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
427 return (formRecords_.count(formId) > 0);
428 }
429 /**
430 * @brief Has form user uids in form record.
431 * @param formId The Id of the form.
432 * @return Returns true if this form has form user uids; returns false is not has.
433 */
HasFormUserUids(const int64_t formId) const434 bool FormDataMgr::HasFormUserUids(const int64_t formId) const
435 {
436 HILOG_INFO("%{public}s, check form has user uids", __func__);
437 FormRecord record;
438 if (GetFormRecord(formId, record)) {
439 return record.formUserUids.empty() ? false : true;
440 }
441 return false;
442 }
443 /**
444 * @brief Get form host record.
445 * @param formId The id of the form.
446 * @param formHostRecord The form host record.
447 */
GetFormHostRecord(const int64_t formId,std::vector<FormHostRecord> & formHostRecords) const448 void FormDataMgr::GetFormHostRecord(const int64_t formId, std::vector<FormHostRecord> &formHostRecords) const
449 {
450 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
451 for (auto &record : clientRecords_) {
452 if (record.Contains(formId)) {
453 formHostRecords.emplace_back(record);
454 }
455 }
456 HILOG_DEBUG("%{public}s, get form host record by formId, size is %{public}zu", __func__, formHostRecords.size());
457 }
GetFormHostRemoteObj(const int64_t formId,std::vector<sptr<IRemoteObject>> & formHostObjs) const458 void FormDataMgr::GetFormHostRemoteObj(const int64_t formId, std::vector<sptr<IRemoteObject>> &formHostObjs) const
459 {
460 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
461 for (auto &record : clientRecords_) {
462 if (record.Contains(formId)) {
463 formHostObjs.emplace_back(record.GetFormHostClient());
464 }
465 }
466 HILOG_DEBUG("Get form host remote object by formId, size is %{public}zu", formHostObjs.size());
467 }
468 /**
469 * @brief Delete form host record.
470 * @param callerToken The client stub of the form host record.
471 * @param formId The id of the form.
472 * @return Returns true if this function is successfully called; returns false otherwise.
473 */
DeleteHostRecord(const sptr<IRemoteObject> & callerToken,const int64_t formId)474 bool FormDataMgr::DeleteHostRecord(const sptr<IRemoteObject> &callerToken, const int64_t formId)
475 {
476 HILOG_INFO("%{public}s start, delete form host record", __func__);
477 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
478 std::vector<FormHostRecord>::iterator iter;
479 for (iter = clientRecords_.begin(); iter != clientRecords_.end(); ++iter) {
480 if (callerToken == iter->GetFormHostClient()) {
481 iter->DelForm(formId);
482 if (iter->IsEmpty()) {
483 iter->CleanResource();
484 iter = clientRecords_.erase(iter);
485 FormRenderMgr::GetInstance().CleanFormHost(callerToken);
486 }
487 break;
488 }
489 }
490 HILOG_INFO("%{public}s end", __func__);
491 return true;
492 }
493 /**
494 * @brief Clean removed forms form host.
495 * @param removedFormIds The id list of the forms.
496 */
CleanHostRemovedForms(const std::vector<int64_t> & removedFormIds)497 void FormDataMgr::CleanHostRemovedForms(const std::vector<int64_t> &removedFormIds)
498 {
499 HILOG_INFO("%{public}s start, delete form host record by formId list", __func__);
500 std::vector<int64_t> matchedIds;
501 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
502 std::vector<FormHostRecord>::iterator itHostRecord;
503 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
504 for (const int64_t& formId : removedFormIds) {
505 if (itHostRecord->Contains(formId)) {
506 matchedIds.emplace_back(formId);
507 itHostRecord->DelForm(formId);
508 }
509 }
510 if (!matchedIds.empty()) {
511 HILOG_INFO("%{public}s, OnFormUninstalled called", __func__);
512 itHostRecord->OnFormUninstalled(matchedIds);
513 }
514 }
515
516 HILOG_INFO("%{public}s end", __func__);
517 }
518
UpdateHostForms(const std::vector<int64_t> & updateFormIds)519 void FormDataMgr::UpdateHostForms(const std::vector<int64_t> &updateFormIds)
520 {
521 HILOG_INFO("%{public}s start, update form host record by formId list", __func__);
522 std::vector<int64_t> matchedIds;
523 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
524 for (FormHostRecord &record : clientRecords_) {
525 for (const int64_t &formId : updateFormIds) {
526 if (record.Contains(formId)) {
527 matchedIds.emplace_back(formId);
528 }
529 }
530 if (!matchedIds.empty()) {
531 HILOG_INFO("%{public}s, OnFormUninstalled called", __func__);
532 record.OnFormUninstalled(matchedIds);
533 matchedIds.clear();
534 }
535 }
536 HILOG_INFO("%{public}s end", __func__);
537 }
538
539 /**
540 * @brief Handle form host died.
541 * @param remoteHost Form host proxy object.
542 */
HandleHostDied(const sptr<IRemoteObject> & remoteHost)543 void FormDataMgr::HandleHostDied(const sptr<IRemoteObject> &remoteHost)
544 {
545 std::vector<int64_t> recordTempForms;
546 {
547 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
548 std::vector<FormHostRecord>::iterator itHostRecord;
549 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end();) {
550 if (remoteHost == itHostRecord->GetFormHostClient()) {
551 HandleHostDiedForTempForms(*itHostRecord, recordTempForms);
552 HILOG_INFO("find died client, remove it");
553 itHostRecord->CleanResource();
554 itHostRecord = clientRecords_.erase(itHostRecord);
555 break;
556 } else {
557 itHostRecord++;
558 }
559 }
560 }
561 {
562 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
563 std::map<int64_t, FormRecord>::iterator itFormRecord;
564 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end();) {
565 int64_t formId = itFormRecord->first;
566 // if temp form, remove it
567 if (std::find(recordTempForms.begin(), recordTempForms.end(), formId) != recordTempForms.end()) {
568 FormRecord formRecord = itFormRecord->second;
569 itFormRecord = formRecords_.erase(itFormRecord);
570 FormProviderMgr::GetInstance().NotifyProviderFormDelete(formId, formRecord);
571 } else {
572 itFormRecord++;
573 }
574 }
575 }
576 {
577 std::lock_guard<std::recursive_mutex> lock(formStateRecordMutex_);
578 std::map<std::string, FormHostRecord>::iterator itFormStateRecord;
579 for (itFormStateRecord = formStateRecord_.begin(); itFormStateRecord != formStateRecord_.end();) {
580 if (remoteHost == itFormStateRecord->second.GetFormHostClient()) {
581 HILOG_INFO("find died client, remove it from formStateRecord_");
582 itFormStateRecord->second.CleanResource();
583 itFormStateRecord = formStateRecord_.erase(itFormStateRecord);
584 break;
585 } else {
586 itFormStateRecord++;
587 }
588 }
589 }
590 FormRenderMgr::GetInstance().CleanFormHost(remoteHost);
591 }
592
593 /**
594 * @brief Get the temp forms from host and delete temp form in cache.
595 * @param record The form record.
596 * @param recordTempForms gotten the temp forms.
597 */
HandleHostDiedForTempForms(const FormHostRecord & record,std::vector<int64_t> & recordTempForms)598 void FormDataMgr::HandleHostDiedForTempForms(const FormHostRecord &record, std::vector<int64_t> &recordTempForms)
599 {
600 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
601 std::vector<int64_t>::iterator itForm;
602 for (itForm = tempForms_.begin(); itForm != tempForms_.end();) {
603 if (record.Contains(*itForm)) {
604 recordTempForms.emplace_back(*itForm);
605 itForm = tempForms_.erase(itForm);
606 } else {
607 itForm++;
608 }
609 }
610 }
611
612 /**
613 * @brief Refresh enable or not.
614 * @param formId The Id of the form.
615 * @return true on enable, false on disable.
616 */
IsEnableRefresh(int64_t formId)617 bool FormDataMgr::IsEnableRefresh(int64_t formId)
618 {
619 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
620 for (auto &record : clientRecords_) {
621 if (record.IsEnableRefresh(formId)) {
622 return true;
623 }
624 }
625
626 return false;
627 }
628
629 /**
630 * @brief update enable or not.
631 * @param formId The Id of the form.
632 * @return true on enable, false on disable.
633 */
IsEnableUpdate(int64_t formId)634 bool FormDataMgr::IsEnableUpdate(int64_t formId)
635 {
636 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
637 for (auto &record : clientRecords_) {
638 if (record.IsEnableUpdate(formId)) {
639 return true;
640 }
641 }
642 return false;
643 }
644
PaddingUdidHash(const int64_t formId)645 int64_t FormDataMgr::PaddingUdidHash(const int64_t formId)
646 {
647 if (!GenerateUdidHash()) {
648 return -1;
649 }
650 // Compatible with int form id.
651 uint64_t unsignedFormId = static_cast<uint64_t>(formId);
652 if ((unsignedFormId & 0xffffffff00000000L) == 0) {
653 uint64_t unsignedUdidHash = static_cast<uint64_t>(udidHash_);
654 uint64_t unsignedUdidHashFormId = unsignedUdidHash | unsignedFormId;
655 int64_t udidHashFormId = static_cast<int64_t>(unsignedUdidHashFormId);
656 return udidHashFormId;
657 }
658 return formId;
659 }
660
661 /**
662 * @brief Generate form id.
663 * @return form id.
664 */
GenerateFormId()665 int64_t FormDataMgr::GenerateFormId()
666 {
667 // generate by udidHash_
668 if (!GenerateUdidHash()) {
669 HILOG_ERROR("%{public}s fail, generateFormId no invalid udidHash_", __func__);
670 return -1;
671 }
672 return FormUtil::GenerateFormId(udidHash_);
673 }
674 /**
675 * @brief Generate udid.
676 * @return Returns true if this function is successfully called; returns false otherwise.
677 */
GenerateUdidHash()678 bool FormDataMgr::GenerateUdidHash()
679 {
680 if (udidHash_ != Constants::INVALID_UDID_HASH) {
681 return true;
682 }
683
684 bool genUdid = FormUtil::GenerateUdidHash(udidHash_);
685 if (!genUdid) {
686 HILOG_ERROR("%{public}s, Failed to generate udid.", __func__);
687 return false;
688 }
689
690 return true;
691 }
692 /**
693 * @brief Get udid.
694 * @return udid.
695 */
GetUdidHash() const696 int64_t FormDataMgr::GetUdidHash() const
697 {
698 return udidHash_;
699 }
700 /**
701 * @brief Set udid.
702 * @param udidHash udid.
703 */
SetUdidHash(const int64_t udidHash)704 void FormDataMgr::SetUdidHash(const int64_t udidHash)
705 {
706 udidHash_ = udidHash;
707 }
708
709 /**
710 * @brief Get the matched form host record by client stub.
711 *
712 * @param callerToken The client stub of the form host record.
713 * @param formHostRecord The form host record.
714 * @return Returns true if this function is successfully called, returns false otherwise.
715 */
GetMatchedHostClient(const sptr<IRemoteObject> & callerToken,FormHostRecord & formHostRecord) const716 bool FormDataMgr::GetMatchedHostClient(const sptr<IRemoteObject> &callerToken, FormHostRecord &formHostRecord) const
717 {
718 HILOG_INFO("%{public}s, get the matched form host record by client stub.", __func__);
719 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
720 for (const FormHostRecord &record : clientRecords_) {
721 if (callerToken == record.GetFormHostClient()) {
722 formHostRecord = record;
723 return true;
724 }
725 }
726
727 HILOG_ERROR("%{public}s, form host record not find.", __func__);
728 return false;
729 }
730
731 /**
732 * @brief Set needRefresh for FormRecord.
733 * @param formId The Id of the form.
734 * @param needRefresh true or false.
735 */
SetNeedRefresh(const int64_t formId,const bool needRefresh)736 void FormDataMgr::SetNeedRefresh(const int64_t formId, const bool needRefresh)
737 {
738 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
739 auto itFormRecord = formRecords_.find(formId);
740 if (itFormRecord == formRecords_.end()) {
741 HILOG_ERROR("%{public}s, form info not find", __func__);
742 return;
743 }
744 itFormRecord->second.needRefresh = needRefresh;
745 }
746
747 /**
748 * @brief Set isCountTimerRefresh for FormRecord.
749 * @param formId The Id of the form.
750 * @param countTimerRefresh true or false.
751 */
SetCountTimerRefresh(const int64_t formId,const bool countTimerRefresh)752 void FormDataMgr::SetCountTimerRefresh(const int64_t formId, const bool countTimerRefresh)
753 {
754 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
755 auto itFormRecord = formRecords_.find(formId);
756 if (itFormRecord == formRecords_.end()) {
757 HILOG_ERROR("%{public}s, form info not find", __func__);
758 return;
759 }
760 itFormRecord->second.isCountTimerRefresh = countTimerRefresh;
761 }
762
763 /**
764 * @brief Set isTimerRefresh for FormRecord.
765 * @param formId The Id of the form.
766 * @param timerRefresh true or false.
767 */
SetTimerRefresh(const int64_t formId,const bool timerRefresh)768 void FormDataMgr::SetTimerRefresh(const int64_t formId, const bool timerRefresh)
769 {
770 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
771 auto itFormRecord = formRecords_.find(formId);
772 if (itFormRecord == formRecords_.end()) {
773 HILOG_ERROR("%{public}s, form info not find", __func__);
774 return;
775 }
776 itFormRecord->second.isTimerRefresh = timerRefresh;
777 }
778
779 /**
780 * @brief Get updated form.
781 * @param record FormRecord.
782 * @param targetForms Target forms.
783 * @param updatedForm Updated formnfo.
784 * @return Returns true on success, false on failure.
785 */
GetUpdatedForm(const FormRecord & record,const std::vector<FormInfo> & targetForms,FormInfo & updatedForm)786 bool FormDataMgr::GetUpdatedForm(const FormRecord &record, const std::vector<FormInfo> &targetForms,
787 FormInfo &updatedForm)
788 {
789 if (targetForms.empty()) {
790 HILOG_ERROR("%{public}s error, targetForms is empty.", __func__);
791 return false;
792 }
793
794 for (const FormInfo &item : targetForms) {
795 if (IsSameForm(record, item)) {
796 updatedForm = item;
797 HILOG_DEBUG("%{public}s, find matched form.", __func__);
798 return true;
799 }
800 }
801 return false;
802 }
803 /**
804 * @brief Set isEnableUpdate for FormRecord.
805 * @param formId The Id of the form.
806 * @param enableUpdate true or false.
807 */
SetEnableUpdate(const int64_t formId,const bool enableUpdate)808 void FormDataMgr::SetEnableUpdate(const int64_t formId, const bool enableUpdate)
809 {
810 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
811 auto itFormRecord = formRecords_.find(formId);
812 if (itFormRecord == formRecords_.end()) {
813 HILOG_ERROR("%{public}s, form info not find", __func__);
814 return;
815 }
816 itFormRecord->second.isEnableUpdate = enableUpdate;
817 }
818 /**
819 * @brief Set update info for FormRecord.
820 * @param formId The Id of the form.
821 * @param enableUpdate true or false.
822 * @param updateDuration Update duration.
823 * @param updateAtHour Update at hour.
824 * @param updateAtMin Update at minute.
825 */
SetUpdateInfo(const int64_t formId,const bool enableUpdate,const long updateDuration,const int updateAtHour,const int updateAtMin)826 void FormDataMgr::SetUpdateInfo(
827 const int64_t formId,
828 const bool enableUpdate,
829 const long updateDuration,
830 const int updateAtHour,
831 const int updateAtMin)
832 {
833 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
834 auto itFormRecord = formRecords_.find(formId);
835 if (itFormRecord == formRecords_.end()) {
836 HILOG_ERROR("%{public}s, form info not find", __func__);
837 return;
838 }
839
840 itFormRecord->second.isEnableUpdate = enableUpdate;
841 itFormRecord->second.updateDuration = updateDuration;
842 itFormRecord->second.updateAtHour = updateAtHour;
843 itFormRecord->second.updateAtMin = updateAtMin;
844 }
845 /**
846 * @brief Check if two forms is same or not.
847 * @param record FormRecord.
848 * @param formInfo FormInfo.
849 * @return Returns true on success, false on failure.
850 */
IsSameForm(const FormRecord & record,const FormInfo & formInfo)851 bool FormDataMgr::IsSameForm(const FormRecord &record, const FormInfo &formInfo)
852 {
853 if (record.bundleName == formInfo.bundleName
854 && record.moduleName == formInfo.moduleName
855 && record.abilityName == formInfo.abilityName
856 && record.formName == formInfo.name
857 && std::find(formInfo.supportDimensions.begin(), formInfo.supportDimensions.end(), record.specification)
858 != formInfo.supportDimensions.end()) {
859 return true;
860 }
861
862 return false;
863 }
864 /**
865 * @brief Clean removed form records.
866 * @param removedForms The id list of the forms.
867 */
CleanRemovedFormRecords(const std::string & bundleName,std::set<int64_t> & removedForms)868 void FormDataMgr::CleanRemovedFormRecords(const std::string &bundleName, std::set<int64_t> &removedForms)
869 {
870 HILOG_INFO("%{public}s, clean removed form records", __func__);
871 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
872 std::map<int64_t, FormRecord>::iterator itFormRecord;
873 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end();) {
874 auto itForm = std::find(removedForms.begin(), removedForms.end(), itFormRecord->first);
875 if (itForm != removedForms.end()) {
876 FormRenderMgr::GetInstance().StopRenderingForm(itFormRecord->first, itFormRecord->second);
877 itFormRecord = formRecords_.erase(itFormRecord);
878 } else {
879 itFormRecord++;
880 }
881 }
882 }
883 /**
884 * @brief Clean removed temp form records.
885 * @param bundleName BundleName.
886 * @param removedForms The id list of the forms.
887 */
CleanRemovedTempFormRecords(const std::string & bundleName,const int32_t userId,std::set<int64_t> & removedForms)888 void FormDataMgr::CleanRemovedTempFormRecords(const std::string &bundleName, const int32_t userId,
889 std::set<int64_t> &removedForms)
890 {
891 HILOG_INFO("%{public}s, clean removed form records", __func__);
892 std::set<int64_t> removedTempForms;
893 {
894 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
895 std::map<int64_t, FormRecord>::iterator itFormRecord;
896 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end();) {
897 if ((itFormRecord->second.formTempFlag) && (bundleName == itFormRecord->second.bundleName)
898 && (userId == itFormRecord->second.userId)) {
899 removedTempForms.emplace(itFormRecord->second.formId);
900 FormRenderMgr::GetInstance().StopRenderingForm(itFormRecord->first, itFormRecord->second);
901 itFormRecord = formRecords_.erase(itFormRecord);
902 } else {
903 itFormRecord++;
904 }
905 }
906 }
907
908 if (removedTempForms.size() > 0) {
909 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
910 std::vector<int64_t>::iterator itTemp;
911 for (itTemp = tempForms_.begin(); itTemp != tempForms_.end();) {
912 if (removedTempForms.find(*itTemp) != removedTempForms.end()) {
913 itTemp = tempForms_.erase(itTemp);
914 } else {
915 itTemp++;
916 }
917 }
918 removedForms.merge(removedTempForms);
919 }
920 }
921 /**
922 * @brief Get recreate form records.
923 * @param reCreateForms The id list of the forms.
924 */
GetReCreateFormRecordsByBundleName(const std::string & bundleName,std::set<int64_t> & reCreateForms)925 void FormDataMgr::GetReCreateFormRecordsByBundleName(const std::string &bundleName, std::set<int64_t> &reCreateForms)
926 {
927 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
928 std::map<int64_t, FormRecord>::iterator itFormRecord;
929 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
930 if (bundleName == itFormRecord->second.bundleName) {
931 reCreateForms.emplace(itFormRecord->second.formId);
932 }
933 }
934 }
935 /**
936 * @brief Set form isInited flag.
937 * @param formId The Id of the form.
938 * @param isInited isInited property
939 */
SetFormCacheInited(const int64_t formId,bool isInited)940 void FormDataMgr::SetFormCacheInited(const int64_t formId, bool isInited)
941 {
942 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
943 auto itFormRecord = formRecords_.find(formId);
944 if (itFormRecord == formRecords_.end()) {
945 HILOG_ERROR("%{public}s, form info not find", __func__);
946 return;
947 }
948 itFormRecord->second.isInited = isInited;
949 itFormRecord->second.needRefresh = !isInited;
950 }
951 /**
952 * @brief Set versionUpgrade.
953 * @param formId The Id of the form.
954 * @param versionUpgrade true or false
955 */
SetVersionUpgrade(const int64_t formId,const bool versionUpgrade)956 void FormDataMgr::SetVersionUpgrade(const int64_t formId, const bool versionUpgrade)
957 {
958 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
959 auto itFormRecord = formRecords_.find(formId);
960 if (itFormRecord == formRecords_.end()) {
961 HILOG_ERROR("%{public}s, form info not find", __func__);
962 return;
963 }
964 itFormRecord->second.versionUpgrade = versionUpgrade;
965 }
966 /**
967 * @brief Update form for host clients.
968 * @param formId The Id of the form.
969 * @param needRefresh true or false
970 */
UpdateHostNeedRefresh(const int64_t formId,const bool needRefresh)971 void FormDataMgr::UpdateHostNeedRefresh(const int64_t formId, const bool needRefresh)
972 {
973 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
974 std::vector<FormHostRecord>::iterator itHostRecord;
975 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
976 if (itHostRecord->Contains(formId)) {
977 itHostRecord->SetNeedRefresh(formId, needRefresh);
978 }
979 }
980 }
981
982 /**
983 * @brief Update form for host clients.
984 * @param formId The Id of the form.
985 * @param formRecord The form info.
986 * @return Returns true if form update, false if other.
987 */
UpdateHostForm(const int64_t formId,const FormRecord & formRecord)988 bool FormDataMgr::UpdateHostForm(const int64_t formId, const FormRecord &formRecord)
989 {
990 bool isUpdated = false;
991 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
992 std::vector<FormHostRecord>::iterator itHostRecord;
993 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
994 bool enableRefresh = formRecord.isVisible || itHostRecord->IsEnableUpdate(formId) ||
995 itHostRecord->IsEnableRefresh(formId);
996 HILOG_INFO("formId:%{public}" PRId64 " enableRefresh:%{public}d", formId, enableRefresh);
997 if (enableRefresh) {
998 // update form
999 itHostRecord->OnUpdate(formId, formRecord);
1000 // set needRefresh
1001 itHostRecord->SetNeedRefresh(formId, false);
1002 isUpdated = true;
1003 }
1004 }
1005 return isUpdated;
1006 }
1007
HandleUpdateHostFormFlag(const std::vector<int64_t> & formIds,bool flag,bool isOnlyEnableUpdate,FormHostRecord & formHostRecord,std::vector<int64_t> & refreshForms)1008 ErrCode FormDataMgr::HandleUpdateHostFormFlag(const std::vector<int64_t> &formIds, bool flag, bool isOnlyEnableUpdate,
1009 FormHostRecord &formHostRecord, std::vector<int64_t> &refreshForms)
1010 {
1011 for (const int64_t formId : formIds) {
1012 if (formId <= 0) {
1013 HILOG_WARN("%{public}s, formId %{public}" PRId64 " is less than 0", __func__, formId);
1014 continue;
1015 }
1016
1017 int64_t matchedFormId = FindMatchedFormId(formId);
1018 if (!formHostRecord.Contains(matchedFormId)) {
1019 HILOG_WARN("%{public}s, form %{public}" PRId64 "is not owned by this client, don't need to update flag",
1020 __func__, formId);
1021 continue;
1022 }
1023
1024 if (isOnlyEnableUpdate) {
1025 // new API: this flag is used only to control enable update
1026 formHostRecord.SetEnableUpdate(matchedFormId, flag);
1027 formHostRecord.SetEnableRefresh(matchedFormId, false);
1028 } else {
1029 // old API: this flag is used to control enable update and visible update
1030 formHostRecord.SetEnableRefresh(matchedFormId, flag);
1031 }
1032
1033 // set disable
1034 if (!flag) {
1035 HILOG_INFO("%{public}s, flag is disable", __func__);
1036 continue;
1037 }
1038 FormRecord formRecord;
1039 if (GetFormRecord(matchedFormId, formRecord)) {
1040 if (formRecord.needRefresh) {
1041 HILOG_INFO("%{public}s, formRecord need refresh", __func__);
1042 refreshForms.emplace_back(matchedFormId);
1043 continue;
1044 }
1045 } else {
1046 HILOG_WARN("%{public}s, not exist such form:%{public}" PRId64 "", __func__, matchedFormId);
1047 continue;
1048 }
1049
1050 // if set enable flag, should check whether to refresh form
1051 if (!formHostRecord.IsNeedRefresh(matchedFormId)) {
1052 HILOG_INFO("%{public}s, host need not refresh", __func__);
1053 continue;
1054 }
1055
1056 if (IsFormCached(formRecord)) {
1057 HILOG_INFO("%{public}s, form cached", __func__);
1058 formHostRecord.OnUpdate(matchedFormId, formRecord);
1059 formHostRecord.SetNeedRefresh(matchedFormId, false);
1060 } else {
1061 HILOG_INFO("%{public}s, form no cache", __func__);
1062 refreshForms.emplace_back(matchedFormId);
1063 continue;
1064 }
1065 }
1066 return ERR_OK;
1067 }
1068
1069 /**
1070 * @brief handle update form flag.
1071 * @param formIDs The id of the forms.
1072 * @param callerToken Caller ability token.
1073 * @param flag form flag.
1074 * @param isOnlyEnableUpdate form enable update form flag.
1075 * @param refreshForms Refresh forms
1076 * @return Returns ERR_OK on success, others on failure.
1077 */
UpdateHostFormFlag(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,bool flag,bool isOnlyEnableUpdate,std::vector<int64_t> & refreshForms)1078 ErrCode FormDataMgr::UpdateHostFormFlag(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
1079 bool flag, bool isOnlyEnableUpdate, std::vector<int64_t> &refreshForms)
1080 {
1081 HILOG_INFO("%{public}s start, flag: %{public}d", __func__, flag);
1082 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
1083 std::vector<FormHostRecord>::iterator itHostRecord;
1084 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end(); itHostRecord++) {
1085 if (callerToken == itHostRecord->GetFormHostClient()) {
1086 HandleUpdateHostFormFlag(formIds, flag, isOnlyEnableUpdate, *itHostRecord, refreshForms);
1087 HILOG_INFO("%{public}s end.", __func__);
1088 return ERR_OK;
1089 }
1090 }
1091 HILOG_ERROR("%{public}s, can't find target client", __func__);
1092 return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1093 }
1094 /**
1095 * @brief Find matched form id.
1096 * @param formId The form id.
1097 * @return Matched form id.
1098 */
FindMatchedFormId(const int64_t formId)1099 int64_t FormDataMgr::FindMatchedFormId(const int64_t formId)
1100 {
1101 uint64_t unsignedFormId = static_cast<uint64_t>(formId);
1102 if ((unsignedFormId & 0xffffffff00000000L) != 0) {
1103 return formId;
1104 }
1105 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
1106 std::map<int64_t, FormRecord>::iterator itFormRecord;
1107 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
1108 uint64_t unsignedFormId = static_cast<uint64_t>(formId);
1109 uint64_t unsignedItFormRecordFirst = static_cast<uint64_t>(itFormRecord->first);
1110 if ((unsignedItFormRecordFirst & 0x00000000ffffffffL) == (unsignedFormId & 0x00000000ffffffffL)) {
1111 return itFormRecord->first;
1112 }
1113 }
1114 return formId;
1115 }
1116
1117 /**
1118 * @brief Clear host data by uId.
1119 * @param uId The caller uId.
1120 */
ClearHostDataByUId(const int uId)1121 void FormDataMgr::ClearHostDataByUId(const int uId)
1122 {
1123 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
1124 std::vector<FormHostRecord>::iterator itHostRecord;
1125 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end();) {
1126 if (itHostRecord->GetCallerUid() == uId) {
1127 itHostRecord->CleanResource();
1128 itHostRecord = clientRecords_.erase(itHostRecord);
1129 } else {
1130 itHostRecord++;
1131 }
1132 }
1133 }
1134 /**
1135 * @brief Get no host temp forms.
1136 * @param uid The caller uid.
1137 * @param noHostTempFormsMap no host temp forms.
1138 * @param foundFormsMap Form Id list.
1139 */
GetNoHostTempForms(const int uid,std::map<FormIdKey,std::set<int64_t>> & noHostTempFormsMap,std::map<int64_t,bool> & foundFormsMap)1140 void FormDataMgr::GetNoHostTempForms(
1141 const int uid, std::map<FormIdKey,
1142 std::set<int64_t>> &noHostTempFormsMap,
1143 std::map<int64_t, bool> &foundFormsMap)
1144 {
1145 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
1146 std::map<int64_t, FormRecord>::iterator itFormRecord;
1147 for (itFormRecord = formRecords_.begin(); itFormRecord != formRecords_.end(); itFormRecord++) {
1148 if (!itFormRecord->second.formTempFlag) {
1149 continue; // Not temp form, skip
1150 }
1151
1152 auto itUid = std::find(itFormRecord->second.formUserUids.begin(),
1153 itFormRecord->second.formUserUids.end(), uid);
1154 if (itUid == itFormRecord->second.formUserUids.end()) {
1155 foundFormsMap.emplace(itFormRecord->second.formId, false);
1156 continue;
1157 }
1158
1159 itFormRecord->second.formUserUids.erase(itUid);
1160 if (!itFormRecord->second.formUserUids.empty()) {
1161 continue;
1162 }
1163
1164 FormIdKey formIdKey(itFormRecord->second.bundleName, itFormRecord->second.abilityName);
1165 auto itIdsSet = noHostTempFormsMap.find(formIdKey);
1166 if (itIdsSet == noHostTempFormsMap.end()) {
1167 std::set<int64_t> formIdsSet;
1168 formIdsSet.emplace(itFormRecord->second.formId);
1169 noHostTempFormsMap.emplace(formIdKey, formIdsSet);
1170 } else {
1171 itIdsSet->second.emplace(itFormRecord->second.formId);
1172 }
1173 }
1174 }
1175 /**
1176 * @brief Parse update config.
1177 * @param record The form record.
1178 * @param info The form item info.
1179 */
ParseUpdateConfig(FormRecord & record,const FormItemInfo & info) const1180 void FormDataMgr::ParseUpdateConfig(FormRecord &record, const FormItemInfo &info) const
1181 {
1182 int configDuration = info.GetUpdateDuration();
1183 if (configDuration > 0) {
1184 ParseIntervalConfig(record, configDuration);
1185 } else {
1186 ParseAtTimerConfig(record, info);
1187 }
1188 }
1189
1190 /**
1191 * @brief Parse update interval config.
1192 * @param record The form record.
1193 * @param configDuration interval duration.
1194 */
ParseIntervalConfig(FormRecord & record,const int configDuration) const1195 void FormDataMgr::ParseIntervalConfig(FormRecord &record, const int configDuration) const
1196 {
1197 HILOG_INFO("%{public}s, configDuration:%{public}d", __func__, configDuration);
1198 if (configDuration <= Constants::MIN_CONFIG_DURATION) {
1199 record.updateDuration = Constants::MIN_PERIOD;
1200 } else if (configDuration >= Constants::MAX_CONFIG_DURATION) {
1201 record.updateDuration = Constants::MAX_PERIOD;
1202 } else {
1203 record.updateDuration = configDuration * Constants::TIME_CONVERSION;
1204 }
1205 HILOG_INFO("%{public}s end", __func__);
1206 }
1207
1208 /**
1209 * @brief Parse at time config.
1210 * @param record The form record.
1211 * @param info form item info.
1212 */
ParseAtTimerConfig(FormRecord & record,const FormItemInfo & info) const1213 void FormDataMgr::ParseAtTimerConfig(FormRecord &record, const FormItemInfo &info) const
1214 {
1215 record.isEnableUpdate = false;
1216 record.updateDuration = 0;
1217 std::string configAtTime = info.GetScheduledUpdateTime();
1218 HILOG_INFO("%{public}s, parseAsUpdateAt updateAt:%{public}s", __func__, configAtTime.c_str());
1219 if (configAtTime.empty()) {
1220 return;
1221 }
1222
1223 std::vector<std::string> temp = FormUtil::StringSplit(configAtTime, Constants::TIME_DELIMETER);
1224 if (temp.size() != Constants::UPDATE_AT_CONFIG_COUNT) {
1225 HILOG_ERROR("%{public}s, invalid config", __func__);
1226 return;
1227 }
1228 int hour = -1;
1229 int min = -1;
1230 hour = std::stoi(temp[0]);
1231 min = std::stoi(temp[1]);
1232 if (hour < Constants::MIN_TIME || hour > Constants::MAX_HOUR || min < Constants::MIN_TIME || min >
1233 Constants::MAX_MINUTE) {
1234 HILOG_ERROR("%{public}s, time is invalid", __func__);
1235 return;
1236 }
1237 record.updateAtHour = hour;
1238 record.updateAtMin = min;
1239 record.isEnableUpdate = true;
1240 }
1241 /**
1242 * @brief check if form cached.
1243 * @param record The form record.
1244 * @return Returns ERR_OK on cached, others on not cached.
1245 */
IsFormCached(const FormRecord record)1246 bool FormDataMgr::IsFormCached(const FormRecord record)
1247 {
1248 if (record.versionUpgrade) {
1249 return false;
1250 }
1251 return FormCacheMgr::GetInstance().IsExist(record.formId);
1252 }
1253
1254 /**
1255 * @brief Create form state host record.
1256 * @param provider The provider of the form state
1257 * @param info The form item info.
1258 * @param callerToken The UID of the proxy.
1259 * @param callingUid The UID of the proxy.
1260 * @return Returns true if this function is successfully called; returns false otherwise.
1261 */
CreateFormStateRecord(std::string & provider,const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,int callingUid)1262 bool FormDataMgr::CreateFormStateRecord(std::string &provider, const FormItemInfo &info,
1263 const sptr<IRemoteObject> &callerToken, int callingUid)
1264 {
1265 std::lock_guard<std::recursive_mutex> lock(formStateRecordMutex_);
1266 auto iter = formStateRecord_.find(provider);
1267 if (iter != formStateRecord_.end()) {
1268 if (iter->second.GetFormHostClient() != callerToken) {
1269 iter->second.SetFormHostClient(callerToken);
1270 }
1271 return true;
1272 }
1273
1274 FormHostRecord hostRecord;
1275 bool isCreated = CreateHostRecord(info, callerToken, callingUid, hostRecord);
1276 if (isCreated) {
1277 formStateRecord_.emplace(provider, hostRecord);
1278 return true;
1279 }
1280
1281 return false;
1282 }
1283
1284 /**
1285 * @brief acquire form state callback.
1286 * @param state form state.
1287 * @param provider provider info.
1288 * @param want The want of onAcquireFormState.
1289 * @return Returns true if this function is successfully called; returns false otherwise.
1290 */
AcquireFormStateBack(AppExecFwk::FormState state,const std::string & provider,const Want & want)1291 ErrCode FormDataMgr::AcquireFormStateBack(AppExecFwk::FormState state, const std::string &provider,
1292 const Want &want)
1293 {
1294 std::lock_guard<std::recursive_mutex> lock(formStateRecordMutex_);
1295 auto iter = formStateRecord_.find(provider);
1296 if (iter == formStateRecord_.end()) {
1297 HILOG_ERROR("filed to get form state host record");
1298 return ERR_APPEXECFWK_FORM_GET_HOST_FAILED;
1299 }
1300 iter->second.OnAcquireState(state, want);
1301 iter->second.CleanResource();
1302 formStateRecord_.erase(iter);
1303 return ERR_OK;
1304 }
1305
1306 /**
1307 * @brief Notify the form is visible or not.
1308 * @param formIds Indicates the ID of the forms.
1309 * @param isVisible Visible or not.
1310 * @param callerToken Host client.
1311 * @return Returns ERR_OK on success, others on failure.
1312 */
NotifyFormsVisible(const std::vector<int64_t> & formIds,bool isVisible,const sptr<IRemoteObject> & callerToken)1313 ErrCode FormDataMgr::NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
1314 const sptr<IRemoteObject> &callerToken)
1315 {
1316 if (formIds.empty() || callerToken == nullptr) {
1317 HILOG_ERROR("%{public}s failed, formIds empty.", __func__);
1318 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1319 }
1320
1321 std::vector<int64_t> foundFormIds {};
1322 {
1323 HILOG_INFO("%{public}s, get the matched form host record by client stub.", __func__);
1324 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
1325 for (const FormHostRecord &record : clientRecords_) {
1326 if (callerToken != record.GetFormHostClient()) {
1327 continue;
1328 }
1329 for (int64_t formId : formIds) {
1330 int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
1331 if (!record.Contains(matchedFormId)) {
1332 HILOG_ERROR("%{public}s fail, form is not self-owned, form:%{public}" PRId64 ".", __func__,
1333 matchedFormId);
1334 } else {
1335 foundFormIds.push_back(matchedFormId);
1336 }
1337 }
1338 break;
1339 }
1340 }
1341
1342 if (foundFormIds.empty()) {
1343 HILOG_ERROR("%{public}s failed, no valid forms found.", __func__);
1344 return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1345 }
1346
1347 for (auto matchedFormId : foundFormIds) {
1348 SetRecordVisible(matchedFormId, isVisible);
1349 }
1350 return ERR_OK;
1351 }
1352
1353 /**
1354 * @brief set form record visible.
1355 * @param matchedFormId form id.
1356 * @param isVisible is visible.
1357 * @return Returns true if this function is successfully called; returns false otherwise.
1358 */
SetRecordVisible(int64_t matchedFormId,bool isVisible)1359 ErrCode FormDataMgr::SetRecordVisible(int64_t matchedFormId, bool isVisible)
1360 {
1361 HILOG_INFO("%{public}s, set form record visible", __func__);
1362 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
1363 auto info = formRecords_.find(matchedFormId);
1364 if (info == formRecords_.end()) {
1365 HILOG_ERROR("%{public}s, form info not find", __func__);
1366 return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1367 }
1368 info->second.isVisible = isVisible;
1369 HILOG_DEBUG("set isVisible to %{public}d, formId: %{public}" PRId64 " ", isVisible, matchedFormId);
1370 return ERR_OK;
1371 }
1372
1373 /**
1374 * @brief delete forms by userId.
1375 *
1376 * @param userId user ID.
1377 * @param removedFormIds removed userId.
1378 */
DeleteFormsByUserId(const int32_t userId,std::vector<int64_t> & removedFormIds)1379 void FormDataMgr::DeleteFormsByUserId(const int32_t userId, std::vector<int64_t> &removedFormIds)
1380 {
1381 HILOG_INFO("%{public}s, delete forms by userId", __func__);
1382
1383 // handle formRecords_
1384 std::vector<int64_t> removedTempForms;
1385 {
1386 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
1387 auto itFormRecord = formRecords_.begin();
1388 while (itFormRecord != formRecords_.end()) {
1389 if (userId == itFormRecord->second.userId) {
1390 if (itFormRecord->second.formTempFlag) {
1391 removedTempForms.emplace_back(itFormRecord->second.formId);
1392 }
1393 removedFormIds.emplace_back(itFormRecord->second.formId);
1394 itFormRecord = formRecords_.erase(itFormRecord);
1395 } else {
1396 ++itFormRecord;
1397 }
1398 }
1399 }
1400
1401 // handle tempForms_
1402 if (removedTempForms.size() > 0) {
1403 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
1404 std::vector<int64_t>::iterator itTemp;
1405 for (itTemp = tempForms_.begin();itTemp != tempForms_.end();) {
1406 if (std::find(removedTempForms.begin(), removedTempForms.end(), *itTemp) != removedTempForms.end()) {
1407 itTemp = tempForms_.erase(itTemp);
1408 } else {
1409 itTemp++;
1410 }
1411 }
1412 }
1413 }
1414 /**
1415 * @brief Clear form records for st limit value test.
1416 */
ClearFormRecords()1417 void FormDataMgr::ClearFormRecords()
1418 {
1419 {
1420 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
1421 formRecords_.clear();
1422 }
1423 {
1424 std::lock_guard<std::recursive_mutex> lock(formTempMutex_);
1425 tempForms_.clear();
1426 }
1427 }
1428
1429 /**
1430 * @brief handle get no host invalid temp forms.
1431 * @param userId User ID.
1432 * @param callingUid The UID of the proxy.
1433 * @param matchedFormIds The set of the valid forms.
1434 * @param noHostTempFormsMap The map of the no host forms.
1435 * @param foundFormsMap The map of the found forms.
1436 */
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)1437 void FormDataMgr::GetNoHostInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
1438 std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
1439 std::map<int64_t, bool> &foundFormsMap)
1440 {
1441 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
1442 for (auto &formRecordInfo : formRecords_) {
1443 int64_t formId = formRecordInfo.first;
1444 FormRecord &formRecord = formRecordInfo.second;
1445
1446 // check userID and temp flag
1447 if (userId != formRecord.userId || !formRecord.formTempFlag) {
1448 continue;
1449 }
1450 // check UID
1451 auto iter = std::find(formRecord.formUserUids.begin(), formRecord.formUserUids.end(), callingUid);
1452 if (iter == formRecord.formUserUids.end()) {
1453 continue;
1454 }
1455 // check valid form set
1456 if (matchedFormIds.find(formId) != matchedFormIds.end()) {
1457 continue;
1458 }
1459
1460 HILOG_DEBUG("found invalid form: %{public}" PRId64 "", formId);
1461 formRecord.formUserUids.erase(iter);
1462 if (formRecord.formUserUids.empty()) {
1463 FormIdKey formIdKey(formRecord.bundleName, formRecord.abilityName);
1464 auto itIdsSet = noHostTempFormsMap.find(formIdKey);
1465 if (itIdsSet == noHostTempFormsMap.end()) {
1466 std::set<int64_t> formIdsSet;
1467 formIdsSet.emplace(formId);
1468 noHostTempFormsMap.emplace(formIdKey, formIdsSet);
1469 } else {
1470 itIdsSet->second.emplace(formId);
1471 }
1472 } else {
1473 foundFormsMap.emplace(formId, false);
1474 }
1475 }
1476 }
1477
1478 /**
1479 * @brief handle delete no host temp forms.
1480 * @param callingUid The UID of the proxy.
1481 * @param noHostTempFormsMap The map of the no host forms.
1482 * @param foundFormsMap The map of the found forms.
1483 */
BatchDeleteNoHostTempForms(int32_t callingUid,std::map<FormIdKey,std::set<int64_t>> & noHostTempFormsMap,std::map<int64_t,bool> & foundFormsMap)1484 void FormDataMgr::BatchDeleteNoHostTempForms(int32_t callingUid, std::map<FormIdKey,
1485 std::set<int64_t>> &noHostTempFormsMap,
1486 std::map<int64_t, bool> &foundFormsMap)
1487 {
1488 std::set<FormIdKey> removableModuleSet;
1489 for (auto &noHostTempForm : noHostTempFormsMap) {
1490 FormIdKey formIdKey = noHostTempForm.first;
1491 std::set<int64_t> &formIdsSet = noHostTempForm.second;
1492 std::string bundleName = formIdKey.bundleName;
1493 std::string abilityName = formIdKey.abilityName;
1494 FormProviderMgr::GetInstance().NotifyProviderFormsBatchDelete(bundleName, abilityName, formIdsSet);
1495 for (int64_t formId: formIdsSet) {
1496 foundFormsMap.emplace(formId, true);
1497 StopRenderingForm(formId);
1498 DeleteFormRecord(formId);
1499 DeleteTempForm(formId);
1500 }
1501 }
1502 }
1503
1504 /**
1505 * @brief StopRenderingForm.
1506 * @param formId The form id.
1507 */
StopRenderingForm(int32_t formId)1508 void FormDataMgr::StopRenderingForm(int32_t formId)
1509 {
1510 FormRecord formrecord;
1511 GetFormRecord(formId, formrecord);
1512 FormRenderMgr::GetInstance().StopRenderingForm(formId, formrecord);
1513 }
1514
1515 /**
1516 * @brief delete invalid temp forms.
1517 * @param userId User ID.
1518 * @param callingUid The UID of the proxy.
1519 * @param matchedFormIds The set of the valid forms.
1520 * @param removedFormsMap The map of the removed invalid forms.
1521 * @return Returns ERR_OK on success, others on failure.
1522 */
DeleteInvalidTempForms(int32_t userId,int32_t callingUid,std::set<int64_t> & matchedFormIds,std::map<int64_t,bool> & removedFormsMap)1523 int32_t FormDataMgr::DeleteInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
1524 std::map<int64_t, bool> &removedFormsMap)
1525 {
1526 HILOG_INFO("DeleteInvalidTempForms start, userId = %{public}d, callingUid = %{public}d", userId, callingUid);
1527 std::map<int64_t, bool> foundFormsMap {};
1528 std::map<FormIdKey, std::set<int64_t>> noHostTempFormsMap {};
1529 GetNoHostInvalidTempForms(userId, callingUid, matchedFormIds, noHostTempFormsMap, foundFormsMap);
1530 BatchDeleteNoHostTempForms(callingUid, noHostTempFormsMap, foundFormsMap);
1531 HILOG_DEBUG("foundFormsMap size: %{public}zu", foundFormsMap.size());
1532 HILOG_DEBUG("noHostTempFormsMap size: %{public}zu", noHostTempFormsMap.size());
1533
1534 if (!foundFormsMap.empty()) {
1535 removedFormsMap.insert(foundFormsMap.begin(), foundFormsMap.end());
1536 }
1537 HILOG_INFO("DeleteInvalidTempForms done");
1538 return ERR_OK;
1539 }
1540
1541 /**
1542 * @brief delete publish forms temp data
1543 * @param userId User ID.
1544 * @param bundleName BundleName.
1545 * @param validFormIds The set of the valid forms.
1546 */
DeleteInvalidPublishForms(int32_t userId,std::string bundleName,std::set<int64_t> & validFormIds)1547 void FormDataMgr::DeleteInvalidPublishForms(int32_t userId, std::string bundleName, std::set<int64_t> &validFormIds)
1548 {
1549 HILOG_INFO("DeleteInvalidPublishForms start, userId = %{public}d, bundleName = %{public}s",
1550 userId, bundleName.c_str());
1551
1552 int32_t deleteNum = 0;
1553 std::lock_guard<std::recursive_mutex> lock(formRequestPublishFormsMutex_);
1554 for (auto iter = formRequestPublishForms_.begin(); iter != formRequestPublishForms_.end();) {
1555 int64_t formId = iter->first;
1556 // check valid form set
1557 if (validFormIds.find(formId) != validFormIds.end()) {
1558 ++iter;
1559 continue;
1560 }
1561
1562 Want want = iter->second.first;
1563 if (bundleName != want.GetStringParam(Constants::PARAM_BUNDLE_NAME_KEY)) {
1564 ++iter;
1565 continue;
1566 }
1567 if (userId != want.GetIntParam(Constants::PARAM_FORM_USER_ID, -1)) {
1568 ++iter;
1569 continue;
1570 }
1571 ++deleteNum;
1572 iter = formRequestPublishForms_.erase(iter);
1573 }
1574
1575 HILOG_INFO("DeleteInvalidPublishForms done, delete num is %{public}d", deleteNum);
1576 }
1577
1578 /**
1579 * @brief clear host data by invalid forms.
1580 * @param callingUid The UID of the proxy.
1581 * @param removedFormsMap The map of the removed invalid forms.
1582 * @return Returns ERR_OK on success, others on failure.
1583 */
ClearHostDataByInvalidForms(int32_t callingUid,std::map<int64_t,bool> & removedFormsMap)1584 int32_t FormDataMgr::ClearHostDataByInvalidForms(int32_t callingUid, std::map<int64_t, bool> &removedFormsMap)
1585 {
1586 HILOG_INFO("DeleteInvalidForms host start");
1587 std::lock_guard<std::recursive_mutex> lock(formHostRecordMutex_);
1588 std::vector<FormHostRecord>::iterator itHostRecord;
1589 for (itHostRecord = clientRecords_.begin(); itHostRecord != clientRecords_.end();) {
1590 if (itHostRecord->GetCallerUid() != callingUid) {
1591 itHostRecord++;
1592 continue;
1593 }
1594 for (auto &removedForm : removedFormsMap) {
1595 if (itHostRecord->Contains(removedForm.first)) {
1596 itHostRecord->DelForm(removedForm.first);
1597 }
1598 }
1599 if (itHostRecord->IsEmpty()) {
1600 itHostRecord->CleanResource();
1601 itHostRecord = clientRecords_.erase(itHostRecord);
1602 } else {
1603 itHostRecord++;
1604 }
1605 }
1606 HILOG_INFO("DeleteInvalidForms host done");
1607 return ERR_OK;
1608 }
1609
AddRequestPublishFormInfo(int64_t formId,const Want & want,std::unique_ptr<FormProviderData> & formProviderData)1610 ErrCode FormDataMgr::AddRequestPublishFormInfo(int64_t formId, const Want &want,
1611 std::unique_ptr<FormProviderData> &formProviderData)
1612 {
1613 HILOG_INFO("add request publish form info, formId: %{public}" PRId64 "", formId);
1614 std::lock_guard<std::recursive_mutex> lock(formRequestPublishFormsMutex_);
1615
1616 auto insertResult = formRequestPublishForms_.insert(
1617 std::make_pair(formId, std::make_pair(want, std::move(formProviderData))));
1618 if (!insertResult.second) {
1619 HILOG_ERROR("Failed to emplace requestPublishFormInfo");
1620 return ERR_APPEXECFWK_FORM_COMMON_CODE;
1621 }
1622 return ERR_OK;
1623 }
1624
RemoveRequestPublishFormInfo(int64_t formId)1625 ErrCode FormDataMgr::RemoveRequestPublishFormInfo(int64_t formId)
1626 {
1627 HILOG_INFO("remove request publish form info, formId: %{public}" PRId64 "", formId);
1628 std::lock_guard<std::recursive_mutex> lock(formRequestPublishFormsMutex_);
1629 formRequestPublishForms_.erase(formId);
1630 return ERR_OK;
1631 }
1632
IsRequestPublishForm(int64_t formId)1633 bool FormDataMgr::IsRequestPublishForm(int64_t formId)
1634 {
1635 std::lock_guard<std::recursive_mutex> lock(formRequestPublishFormsMutex_);
1636 return formRequestPublishForms_.find(formId) != formRequestPublishForms_.end();
1637 }
1638
GetRequestPublishFormInfo(int64_t formId,Want & want,std::unique_ptr<FormProviderData> & formProviderData)1639 ErrCode FormDataMgr::GetRequestPublishFormInfo(int64_t formId, Want &want,
1640 std::unique_ptr<FormProviderData> &formProviderData)
1641 {
1642 HILOG_INFO("get request publish form info, formId: %{public}" PRId64 "", formId);
1643 std::lock_guard<std::recursive_mutex> lock(formRequestPublishFormsMutex_);
1644 auto result = formRequestPublishForms_.find(formId);
1645 if (result == formRequestPublishForms_.end()) {
1646 HILOG_INFO("request publish form id not found, formId: %{public}" PRId64 "", formId);
1647 return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1648 }
1649
1650 want = result->second.first;
1651 formProviderData = std::move(result->second.second);
1652 formRequestPublishForms_.erase(result);
1653 return ERR_OK;
1654 }
1655
GetPackageForm(const FormRecord & record,const BundlePackInfo & bundlePackInfo,AbilityFormInfo & abilityFormInfo)1656 bool FormDataMgr::GetPackageForm(const FormRecord &record, const BundlePackInfo &bundlePackInfo,
1657 AbilityFormInfo &abilityFormInfo)
1658 {
1659 HILOG_INFO("%{public}s start, moduleName is %{public}s", __func__, record.moduleName.c_str());
1660 std::vector<PackageModule> modules = bundlePackInfo.summary.modules;
1661 for (const auto &cfg : modules) {
1662 HILOG_INFO("%{public}s, try module %{public}s", __func__, cfg.distro.moduleName.c_str());
1663 if (record.moduleName != cfg.distro.moduleName) {
1664 continue;
1665 }
1666 HILOG_INFO("%{public}s, has the same module", __func__);
1667 std::vector<ModuleAbilityInfo> abilities = cfg.abilities;
1668 std::vector<ExtensionAbilities> extensionAbilities = cfg.extensionAbilities;
1669 if (!abilities.empty()) {
1670 return GetAbilityFormInfo(record, abilities, abilityFormInfo);
1671 }
1672 if (!extensionAbilities.empty()) {
1673 return GetAbilityFormInfo(record, extensionAbilities, abilityFormInfo);
1674 }
1675 HILOG_WARN("%{public}s, no ability in module:%{public}s", __func__, record.moduleName.c_str());
1676 return false;
1677 }
1678 return false;
1679 }
1680
1681 template<typename T>
GetAbilityFormInfo(const FormRecord & record,const std::vector<T> & abilities,AbilityFormInfo & abilityFormInfo)1682 bool FormDataMgr::GetAbilityFormInfo(const FormRecord &record, const std::vector<T> &abilities,
1683 AbilityFormInfo &abilityFormInfo)
1684 {
1685 for (const T &abilityInfo : abilities) {
1686 if (abilityInfo.name != record.abilityName) {
1687 continue;
1688 }
1689 std::vector<AbilityFormInfo> forms = abilityInfo.forms;
1690 for (auto &item : forms) {
1691 if (IsSameForm(record, item)) {
1692 abilityFormInfo = item;
1693 HILOG_INFO("%{public}s find matched abilityFormInfo", __func__);
1694 return true;
1695 }
1696 }
1697 }
1698 HILOG_INFO("%{public}s, no matched abilityFormInfo, module is %{public}s", __func__, record.moduleName.c_str());
1699 return false;
1700 }
1701
IsSameForm(const FormRecord & record,const AbilityFormInfo & abilityFormInfo)1702 bool FormDataMgr::IsSameForm(const FormRecord &record, const AbilityFormInfo &abilityFormInfo)
1703 {
1704 auto dimensionIter = Constants::DIMENSION_MAP.find(static_cast<Constants::Dimension>(record.specification));
1705 if (dimensionIter == Constants::DIMENSION_MAP.end()) {
1706 HILOG_ERROR("%{public}s, specification:%{public}d is invalid", __func__, record.specification);
1707 return false;
1708 }
1709 auto dimension = dimensionIter->second;
1710 auto supportDimensions = abilityFormInfo.supportDimensions;
1711 if (record.formName == abilityFormInfo.name &&
1712 std::find(supportDimensions.begin(), supportDimensions.end(), dimension) != supportDimensions.end()) {
1713 return true;
1714 }
1715
1716 HILOG_INFO("%{public}s, no same form, record is %{public}s, dimension is %{public}s, abilityFormInfo is %{public}s",
1717 __func__, record.formName.c_str(), dimension.c_str(), abilityFormInfo.name.c_str());
1718
1719 return false;
1720 }
1721
SetRecordNeedFreeInstall(int64_t formId,bool isNeedFreeInstall)1722 bool FormDataMgr::SetRecordNeedFreeInstall(int64_t formId, bool isNeedFreeInstall)
1723 {
1724 HILOG_INFO("%{public}s, get form record by formId", __func__);
1725 std::lock_guard<std::recursive_mutex> lock(formRecordMutex_);
1726 auto item = formRecords_.find(formId);
1727 if (item == formRecords_.end()) {
1728 HILOG_ERROR("%{public}s, form record not found", __func__);
1729 return false;
1730 }
1731 item->second.needFreeInstall = isNeedFreeInstall;
1732 HILOG_INFO("%{public}s, set form record need free install flag successfully", __func__);
1733 return true;
1734 }
1735 } // namespace AppExecFwk
1736 } // namespace OHOS
1737