1 /*
2  * Copyright (c) 2021-2024 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 #ifndef OHOS_FORM_FWK_FORM_FORM_DATA_MGR_H
17 #define OHOS_FORM_FWK_FORM_FORM_DATA_MGR_H
18 
19 #include <map>
20 #include <mutex>
21 #include <set>
22 #include <singleton.h>
23 #include <string>
24 
25 #include "bundle_pack_info.h"
26 #include "form_constants.h"
27 #include "form_host_record.h"
28 #include "form_id_key.h"
29 #include "form_info.h"
30 #include "form_instance.h"
31 #include "form_instances_filter.h"
32 #include "form_item_info.h"
33 #include "form_js_info.h"
34 #include "form_record.h"
35 #include "form_state_info.h"
36 #include "iremote_object.h"
37 #include "libxml/parser.h"
38 #include "libxml/tree.h"
39 #include "running_form_info.h"
40 
41 namespace OHOS {
42 namespace AppExecFwk {
43 using Want = AAFwk::Want;
44 
45 /**
46  * @class FormDataMgr
47  * form data manager.
48  */
49 class FormDataMgr final : public DelayedRefSingleton<FormDataMgr> {
50 DECLARE_DELAYED_REF_SINGLETON(FormDataMgr)
51 public:
52     DISALLOW_COPY_AND_MOVE(FormDataMgr);
53     /**
54      * @brief Allot form info by item info.
55      * @param formInfo Form item info.
56      * @param callingUid The UID of the proxy.
57      * @param userId User ID.
58      * @return Returns form record.
59      */
60     FormRecord AllotFormRecord(const FormItemInfo &formInfo, const int callingUid,
61         const int32_t userId = Constants::DEFAULT_USER_ID);
62     /**
63      * @brief Create form js info by form record.
64      * @param formId The Id of the form.
65      * @param record Form record.
66      * @param formInfo Js info.
67      */
68     void CreateFormJsInfo(const int64_t formId, const FormRecord &record, FormJsInfo &formInfo);
69     /**
70      * @brief Delete form js info by form record.
71      * @param formId The Id of the form.
72      * @return Returns true if this function is successfully called; returns false otherwise.
73      */
74     bool DeleteFormRecord(const int64_t formId);
75     /**
76      * @brief Clean removed forms for host.
77      * @param removedFormIds The id list of the forms.
78      */
79     void CleanHostRemovedForms(const std::vector<int64_t> &removedFormIds);
80     /**
81      * @brief Allot form host record by caller token.
82      * @param info The form item info.
83      * @param callerToken callerToken
84      * @param formId The Id of the form.
85      * @param callingUid The UID of the proxy.
86      * @return Returns true if this function is successfully called; returns false otherwise.
87      */
88     bool AllotFormHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
89         const int64_t formId, const int callingUid);
90     /**
91      * @brief Check temp form count is max.
92      * @return Returns ERR_OK if the temp form not reached; returns ERR_MAX_SYSTEM_TEMP_FORMS is reached.
93      */
94     int CheckTempEnoughForm() const;
95     /**
96      * @brief Check form count is max.
97      * @param currentUserId The current userId.
98      * @param callingUid The UID of the proxy.
99      * @return Returns true if this function is successfully called; returns false otherwise.
100      */
101     int CheckEnoughForm(const int callingUid, const int32_t currentUserId = Constants::DEFAULT_USER_ID) const;
102     /**
103      * @brief Delete temp form.
104      * @param formId The Id of the form.
105      * @return Returns true if this function is successfully called; returns false otherwise.
106      */
107     bool DeleteTempForm(const int64_t formId);
108     /**
109      * @brief Check temp form is exist.
110      * @param formId The Id of the form.
111      * @return Returns true if the temp form is exist; returns false is not exist.
112      */
113     bool ExistTempForm(const int64_t formId) const;
114     /**
115      * @brief Modify form temp flag by formId.
116      * @param formId The Id of the form.
117      * @param formTempFlag The form temp flag.
118      * @return Returns true if this function is successfully called; returns false otherwise.
119      */
120     bool ModifyFormTempFlag(const int64_t formId, const bool formTempFlag);
121     /**
122      * @brief Add form user uid from form record.
123      * @param formId The Id of the form.
124      * @param formUserUid The form user uid.
125      * @return Returns true if this function is successfully called; returns false otherwise.
126      */
127     bool AddFormUserUid(const int64_t formId, const int formUserUid);
128     /**
129      * @brief Delete form user uid from form record.
130      * @param formId The Id of the form.
131      * @param uid calling user id.
132      * @return Returns true if this function is successfully called; returns false otherwise.
133      */
134     bool DeleteFormUserUid(const int64_t formId, const int uid);
135     /**
136      * @brief Update form record.
137      * @param formId The Id of the form.
138      * @param formRecord The form record.
139      * @return Returns true if this function is successfully called; returns false otherwise.
140      */
141     bool UpdateFormRecord(const int64_t formId, const FormRecord &formRecord);
142     /**
143      * @brief Get form record.
144      * @param formId The Id of the form.
145      * @param formRecord The form record.
146      * @return Returns true if this function is successfully called; returns false otherwise.
147      */
148     bool GetFormRecord(const int64_t formId, FormRecord &formRecord) const;
149     /**
150      * @brief Get form record.
151      * @param bundleName Bundle name.
152      * @param formInfos The form record list.
153      * @return Returns true if this function is successfully called; returns false otherwise.
154      */
155     bool GetFormRecord(const std::string &bundleName, std::vector<FormRecord> &formInfos,
156         int32_t userId = Constants::INVALID_USER_ID) const;
157     /**
158      * @brief Get temporary form record.
159      * @param formTempRecords The temp form record.
160      * @return Returns true if this function is successfully called; returns false otherwise.
161      */
162     bool GetTempFormRecord(std::vector<FormRecord> &formTempRecords);
163     /**
164      * @brief Check form record is exist.
165      * @param formId The Id of the form.
166      * @return Returns true if the form record is exist; returns false is not exist.
167      */
168     bool ExistFormRecord(const int64_t formId) const;
169     /**
170      * @brief Has form user uids in form record.
171      * @param formId The Id of the form.
172      * @return Returns true if this form has form user uids; returns false is not has.
173      */
174     bool HasFormUserUids(const int64_t formId) const;
175     /**
176      * @brief Get form host record.
177      * @param formId The id of the form.
178      * @param formHostRecords The form host records.
179      */
180     void GetFormHostRecord(const int64_t formId, std::vector<FormHostRecord> &formHostRecords) const;
181     /**
182      * @brief Get form host remote object.
183      * @param formId The id of the form.
184      * @param formHostObjs The form host remote object.
185      */
186     void GetFormHostRemoteObj(const int64_t formId, std::vector<sptr<IRemoteObject>> &formHostObjs) const;
187     /**
188      * @brief Delete form host record.
189      * @param callerToken The client stub of the form host record.
190      * @param formId The id of the form.
191      * @return Returns true if this function is successfully called; returns false otherwise.
192      */
193     bool DeleteHostRecord(const sptr<IRemoteObject> &callerToken, const int64_t formId);
194     /**
195      * @brief Handle form host died.
196      * @param remoteHost Form host proxy object.
197      */
198     void HandleHostDied(const sptr<IRemoteObject> &remoteHost);
199     /**
200      * @brief Refresh enable or not.
201      * @param formId The Id of the form.
202      * @return true on enable, false on disable.
203      */
204     bool IsEnableRefresh(int64_t formId);
205     /**
206      * @brief update enable or not.
207      * @param formId The Id of the form.
208      * @return true on enable, false on disable.
209      */
210     bool IsEnableUpdate(int64_t formId);
211     /**
212      * @brief Check calling uid is valid.
213      * @param formUserUids The form user uids.
214      * @return Returns true if this user uid is valid; returns false otherwise.
215      */
216     bool IsCallingUidValid(const std::vector<int> &formUserUids) const;
217     /**
218      * @brief Generate udid.
219      * @return Returns true if this function is successfully called; returns false otherwise.
220      */
221     bool GenerateUdidHash();
222     /**
223      * @brief Padding udid hash.
224      * @param formId The form id.
225      * @return Padded form id.
226      */
227     int64_t PaddingUdidHash(int64_t formId);
228     /**
229      * @brief Generate form id.
230      * @return form id.
231      */
232     int64_t GenerateFormId();
233     /**
234      * @brief Get udid.
235      * @return udid.
236      */
237     int64_t GetUdidHash() const;
238     /**
239      * @brief Set udid.
240      * @param udidHash udid.
241      */
242     void SetUdidHash(const int64_t udidHash);
243 
244     /**
245      * @brief Get the matched form host record by client stub.
246      *
247      * @param callerToken The client stub of the form host record.
248      * @param formHostRecord The form host record.
249      * @return Returns true if this function is successfully called, returns false otherwise.
250      */
251     bool GetMatchedHostClient(const sptr<IRemoteObject> &callerToken, FormHostRecord &formHostRecord) const;
252 
253     /**
254      * @brief Set needRefresh for FormRecord.
255      * @param formId The Id of the form.
256      * @param needRefresh true or false.
257      */
258     void SetNeedRefresh(const int64_t formId, const bool needRefresh);
259 
260     /**
261      * @brief Set needAddForm for FormRecord.
262      * @param formId The Id of the form.
263      * @param needAddForm true or false.
264      */
265     void SetNeedAddForm(const int64_t formId, const bool needAddForm);
266 
267     /**
268      * @brief Set isCountTimerRefresh for FormRecord.
269      * @param formId The Id of the form.
270      * @param countTimerRefresh true or false.
271      */
272     void SetCountTimerRefresh(const int64_t formId, const bool countTimerRefresh);
273 
274     /**
275      * @brief Set timerRefresh for FormRecord.
276      * @param formId The Id of the form.
277      * @param timerRefresh true or false.
278      */
279     void SetTimerRefresh(const int64_t formId, const bool timerRefresh);
280 
281     /**
282      * @brief Get updated form info.
283      * @param record FormRecord.
284      * @param targetForms Target forms.
285      * @param updatedForm Updated form info.
286      * @return Returns true on success, false on failure.
287      */
288     bool GetUpdatedForm(const FormRecord &record, const std::vector<FormInfo> &targetForms, FormInfo &updatedForm);
289     /**
290      * @brief Set isEnableUpdate for FormRecord.
291      * @param formId The Id of the form.
292      * @param enableUpdate true or false.
293      */
294     void SetEnableUpdate(const int64_t formId, const bool enableUpdate);
295     /**
296      * @brief Set update info for FormRecord.
297      * @param formId The Id of the form.
298      * @param enableUpdate true or false.
299      * @param updateDuration Update duration.
300      * @param updateAtHour Update at hour.
301      * @param updateAtMin Update at minute.
302      */
303     void SetUpdateInfo(const int64_t formId, const bool enableUpdate, const long updateDuration,
304     const int updateAtHour, const int updateAtMin);
305     /**
306      * @brief Clean removed form records.
307      * @param bundleName BundleName.
308      * @param removedForms The id list of the forms.
309      */
310     void CleanRemovedFormRecords(const std::string &bundleName, std::set<int64_t> &removedForms);
311     /**
312      * @brief Clean removed temp form records.
313      * @param  bundleName BundleName.
314      * @param removedForms The id list of the forms.
315      */
316     void CleanRemovedTempFormRecords(const std::string &bundleName, const int32_t userId,
317         std::set<int64_t> &removedForms);
318     /**
319      * @brief Get recreate form records.
320      * @param reCreateForms The id list of the forms.
321      */
322     void GetReCreateFormRecordsByBundleName(const std::string &bundleName, std::set<int64_t> &reCreateForms);
323     /**
324      * @brief Set form isInited = true.
325      * @param formId The Id of the form.
326      * @param isInited isInited property
327      */
328     void SetFormCacheInited(const int64_t formId, const bool isInited);
329     /**
330      * @brief Set versionUpgrade.
331      * @param formId The Id of the form.
332      * @param versionUpgrade true or false
333      */
334     void SetVersionUpgrade(const int64_t formId, const bool versionUpgrade);
335     /**
336      * @brief Update form for host clients.
337      * @param formId The Id of the form.
338      * @param needRefresh true or false
339      */
340     void UpdateHostNeedRefresh(const int64_t formId, const bool needRefresh);
341     /**
342      * @brief Update form for host clients.
343      * @param formId The Id of the form.
344      * @param formRecord The form info.
345      * @return Returns true if form update, false if other.
346      */
347     bool UpdateHostForm(const int64_t formId, const FormRecord &formRecord);
348     /**
349      * @brief handle update form flag.
350      * @param formIDs The id of the forms.
351      * @param callerToken Caller ability token.
352      * @param flag form flag.
353      * @param isOnlyEnableUpdate form enable update form flag.
354      * @param refreshForms Refresh forms
355      * @return Returns ERR_OK on success, others on failure.
356      */
357     ErrCode UpdateHostFormFlag(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
358                                bool flag, bool isOnlyEnableUpdate, std::vector<int64_t> &refreshForms);
359     /**
360      * @brief Find matched form id.
361      * @param formId The form id.
362      * @return Matched form id.
363      */
364     int64_t FindMatchedFormId(const int64_t formId);
365     /**
366      * @brief Clear host data by uId.
367      * @param uId The caller uId.
368      */
369     void ClearHostDataByUId(const int uId);
370     /**
371      * @brief Get no host temp forms.
372      * @param uid The caller uid.
373      * @param noHostTempFormsMap no host temp forms.
374      * @param foundFormsMap Form Id list.
375      */
376     void GetNoHostTempForms(const int uid, std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
377         std::map<int64_t, bool> &foundFormsMap);
378 
379     /**
380      * @brief delete forms by userId.
381      *
382      * @param userId user ID.
383      * @param removedFormIds removed userId.
384      */
385     void DeleteFormsByUserId(const int32_t userId, std::vector<int64_t> &removedFormIds);
386     /**
387     * @brief Clear form records for st limit value test.
388     */
389     void ClearFormRecords();
390 
391     /**
392      * @brief handle get no host invalid temp forms.
393      * @param userId User ID.
394      * @param callingUid The UID of the proxy.
395      * @param matchedFormIds The set of the valid forms.
396      * @param noHostTempFormsMap The map of the no host forms.
397      * @param foundFormsMap The map of the found forms.
398      */
399     void GetNoHostInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
400                                    std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
401                                    std::map<int64_t, bool> &foundFormsMap);
402 
403     /**
404      * @brief handle delete no host temp forms.
405      * @param callingUid The UID of the proxy.
406      * @param noHostTempFormsMap The map of the no host forms.
407      * @param foundFormsMap The map of the found forms.
408      */
409     void BatchDeleteNoHostTempForms(int32_t callingUid, std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
410                                     std::map<int64_t, bool> &foundFormsMap);
411 
412     /**
413      * @brief delete invalid temp forms.
414      * @param userId User ID.
415      * @param callingUid The UID of the proxy.
416      * @param matchedFormIds The set of the valid forms.
417      * @param removedFormsMap The map of the removed invalid forms.
418      * @return Returns ERR_OK on success, others on failure.
419      */
420     int32_t DeleteInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
421                                    std::map<int64_t, bool> &removedFormsMap);
422 
423     /**
424      * @brief clear host data by invalid forms.
425      * @param callingUid The UID of the proxy.
426      * @param removedFormsMap The map of the removed invalid forms.
427      * @return Returns ERR_OK on success, others on failure.
428      */
429     int32_t ClearHostDataByInvalidForms(int32_t callingUid, std::map<int64_t, bool> &removedFormsMap);
430 
431     /**
432      * @brief delete publish forms temp data
433      * @param userId User ID.
434      * @param bundleName BundleName.
435      * @param validFormIds The set of the valid forms.
436      * @return Returns -
437      */
438     void DeleteInvalidPublishForms(int32_t userId, std::string bundleName, std::set<int64_t> &validFormIds);
439 
440     /**
441      * @brief Create form acquire data host record.
442      * @param requestCode The request code of this acquire form.
443      * @param info The form item info.
444      * @param callerToken  Caller ability token.
445      * @param callingUid The UID of the proxy.
446      * @return Returns true if this function is successfully called; returns false otherwise.
447      */
448     bool CreateFormAcquireDataRecord(int64_t requestCode, const FormItemInfo &info,
449                                      const sptr<IRemoteObject> &callerToken, int callingUid);
450 
451     /**
452      * @brief Create form state host record.
453      * @param provider The provider of the form state
454      * @param info The form item info.
455      * @param callerToken The UID of the proxy.
456      * @param callingUid The UID of the proxy.
457      * @return Returns true if this function is successfully called; returns false otherwise.
458      */
459     bool CreateFormStateRecord(std::string &provider, const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
460                                int callingUid);
461 
462     /**
463      * @brief acquire form state callback.
464      * @param state form state.
465      * @param provider provider info.
466      * @param want The want of onAcquireFormState.
467      * @return Returns true if this function is successfully called; returns false otherwise.
468      */
469     ErrCode AcquireFormStateBack(AppExecFwk::FormState state, const std::string &provider, const Want &want);
470 
471     /**
472      * @brief acquire form data callback.
473      * @param wantParams Indicates the data information acquired by the form.
474      * @param requestCode Indicates the requested id.
475      * @return Returns true if this function is successfully called; returns false otherwise.
476      */
477     ErrCode AcquireFormDataBack(const AAFwk::WantParams &wantParams, int64_t requestCode);
478 
479     /**
480      * @brief Notify the form is visible or not.
481      * @param formIds Indicates the ID of the forms.
482      * @param isVisible Visible or not.
483      * @param callerToken Host client.
484      * @return Returns ERR_OK on success, others on failure.
485      */
486     ErrCode NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
487                                const sptr<IRemoteObject> &callerToken);
488 
489     /**
490      * @brief set form record visible.
491      * @param matchedFormId form id.
492      * @param isVisible is visible.
493      * @return Returns true if this function is successfully called; returns false otherwise.
494      */
495     ErrCode SetRecordVisible(int64_t matchedFormId, bool isVisible);
496 
497     /**
498      * @brief add request publish form info.
499      * @param formId The form id of the form to publish.
500      * @param want The want of the form to publish.
501      * @param formProviderData The form data.
502      * @return Returns ERR_OK on success, others on failure.
503      */
504     ErrCode AddRequestPublishFormInfo(int64_t formId, const Want &want,
505                                       std::unique_ptr<FormProviderData> &formProviderData);
506 
507     /**
508      * @brief remove request publish form info.
509      * @param formId The form id of the form to publish.
510      * @return Returns true if this function is successfully called; returns false otherwise.
511      */
512     ErrCode RemoveRequestPublishFormInfo(int64_t formId);
513 
514     /**
515      * @brief check whether request publish form info.
516       * @param formId The form id of the form to publish.
517      * @return Returns true if this function is successfully called; returns false otherwise.
518      */
519     bool IsRequestPublishForm(int64_t formId);
520 
521     /**
522      * @brief get request publish form info.
523      * @param formId The form id of the form to publish.
524      * @param want The want of the form to publish.
525      * @param formProviderData The form data.
526      * @return Returns true if this function is successfully called; returns false otherwise.
527      */
528     ErrCode GetRequestPublishFormInfo(int64_t formId, Want &want,
529                                       std::unique_ptr<FormProviderData> &formProviderData);
530     /**
531      * @brief Get updated form info.
532      * @param record Indicates the form record.
533      * @param bundlePackInfo Indicates the BundlePackInfo object.
534      * @param abilityFormInfo Indicates the obtained abilityFormInfo object.
535      * @return Returns true on success, false on failure.
536      */
537     bool GetPackageForm(const FormRecord &record, const BundlePackInfo &bundlePackInfo,
538         AbilityFormInfo &abilityFormInfo);
539 
540     /**
541      * @brief Set form free install flag.
542      * @param formId Indicates the form ID.
543      * @param isNeedFreeInstall Indicates the free install flag is true or false.
544      * @return Returns true on success, false on failure.
545      */
546     bool SetRecordNeedFreeInstall(int64_t formId, bool isNeedFreeInstall);
547 
548     /**
549      * @brief StopRenderingForm.
550      * @param formId The form id.
551      */
552     void StopRenderingForm(int32_t formId);
553 
554     /**
555      * @brief update host forms
556      * @param updateFormIds
557      */
558     void UpdateHostForms(const std::vector<int64_t> &updateFormIds);
559 
560     /**
561      * @brief Checks that the form is valid.
562      * @param formId Indicates the form ID.
563      * @return Returns ERR_OK on success, others on failure.
564      */
565     ErrCode CheckInvalidForm(const int64_t formId);
566 
567     /**
568     * @brief get cast forms count.
569     * @param formCount Returns the number of the cast form.
570     * @return Return the cast forms number.
571     */
572     int32_t GetCastFormsCount(int32_t &formCount);
573 
574     /**
575     * @brief get temp forms count.
576     * @param formCount Returns the number of the temp form.
577     * @return Return the temp forms number.
578     */
579     int32_t GetTempFormsCount(int32_t &formCount);
580 
581     /**
582     * @brief get host forms count.
583     * @param bundleName Indicates form host bundleName.
584     * @param formCount Returns the number of the host form.
585     * @return Return the host forms number.
586     */
587     int32_t GetHostFormsCount(const std::string &bundleName, int32_t &formCount);
588 
589     /**
590     * @brief handle form add observer.
591     * @param hostBundleName the bundle name of form host.
592     * @param formId Indicates the form ID.
593     * @return Returns ERR_OK on success, others on failure.
594     */
595     ErrCode HandleFormAddObserver(const std::string hostBundleName, const int64_t formId);
596 
597     /**
598     * @brief handle form add observer.
599     * @param hostBundleName the bundle name of form host.
600     * @param runningFormInfo the running forms' infos of the specify application name.
601     * @return Returns ERR_OK on success, others on failure.
602     */
603     ErrCode HandleFormRemoveObserver(const std::string hostBundleName, const RunningFormInfo runningFormInfo);
604 
605     /**
606      * @brief Get the running form infos by form id.
607      * @param formId Indicates the form ID.
608      * @param runningFormInfos Return the running forms' infos of the specify application name.
609      * @return Returns ERR_OK on success, others on failure.
610      */
611     ErrCode GetRunningFormInfosByFormId(const int64_t formId, RunningFormInfo &runningFormInfo);
612 
613     /**
614      * @brief Get all running form infos.
615      * @param isUnusedIncluded Indicates whether to include unused forms.
616      * @param runningFormInfos Return the running forms' infos currently.
617      * @return Returns ERR_OK on success, others on failure.
618      */
619     ErrCode GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos);
620 
621     /**
622      * @brief Get the running form infos by bundle name.
623      * @param bundleName Application name.
624      * @param isUnusedIncluded Indicates whether to include unused forms.
625      * @param runningFormInfos Return the running forms' infos of the specify application name.
626      * @return Returns ERR_OK on success, others on failure.
627      */
628     ErrCode GetRunningFormInfosByBundleName(
629         const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos);
630 
631     /**
632      * @brief Get form instances by filter info.
633      * @param formInstancesFilter includes bundleName, moduleName, formName, abilityName to get formInstances.
634      * @param formInstances return formInstances
635      * @return return ERR_OK on get info success,other on failure.
636      */
637     ErrCode GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
638         std::vector<FormInstance> &formInstances);
639 
640     /**
641      * @brief Get form instance by formId.
642      * @param formId formId Indicates the unique id of form.
643      * @param formInstance return formInstance
644      * @return return ERR_OK on get info success, others on failure.
645      */
646     ErrCode GetFormInstanceById(const int64_t formId, FormInstance &formInstances);
647 
648     /**
649      * @brief Get form instance by formId, include form store in DB.
650      * @param formId formId Indicates the unique id of form.
651      * @param isUnusedIncluded Indicates whether to include unused forms.
652      * @param formInstance return formInstance
653      * @return return ERR_OK on get info success, others on failure.
654      */
655     ErrCode GetFormInstanceById(const int64_t formId, bool isUnusedIncluded, FormInstance &formInstances);
656 
657     /**
658      * @brief Set form config map.
659      * @param configMap form config map.
660      */
661     void SetConfigMap(const std::map<std::string, int32_t> &configMap);
662 
663     /**
664      * @brief Get form config param form map.
665      * @param key the param's name.
666      * @param value the return value.
667      */
668     void GetConfigParamFormMap(const std::string &key, int32_t &value) const;
669 
670     /**
671      * @brief Recycle all recyclable forms.
672      */
673     void RecycleAllRecyclableForms() const;
674 
675     /**
676      * @brief Recycle forms.
677      * @param formIds Indicates the ID of the forms.
678      * @param callingUid The uid of caller.
679      * @param want The want of forms to be recycled.
680      */
681     void RecycleForms(const std::vector<int64_t> &formIds, const int &callingUid, const Want &want) const;
682 
683     /**
684      * @brief Update the form cloud update duration by bundleName.
685      * @param bundleName The specified bundleName.
686      * @param duration The form cloud update duration.
687      */
688     void UpdateFormCloudUpdateDuration(const std::string &bundleName, int duration);
689 
690     /**
691      * @brief Remove the form cloud update duration by bundleName.
692      * @param bundleName The specified bundleName.
693      */
694     void RemoveFormCloudUpdateDuration(const std::string &bundleName);
695 
696     /**
697      * @brief Get the form cloud update duration by bundleName.
698      * @param bundleName The specified bundleName.
699      */
700     int GetFormCloudUpdateDuration(const std::string &bundleName) const;
701 
702     /**
703      * @brief Determine if the form cloud update duration exists by bundleName.
704      * @param bundleName The specified bundleName.
705      */
706     bool HasFormCloudUpdateDuration(const std::string &bundleName) const;
707 
708     /**
709      * @brief Update form location.
710      * @param formId The Id of the form.
711      * @param formLocation The form location.
712      * @param hostBundleName the bundle name of form host.
713      * @return Returns ERR_OK on success, others on failure.
714      */
715     ErrCode UpdateFormLocation(const int64_t &formId, const int32_t &formLocation);
716 
717     /**
718      * @brief Get the formRecords by refreshType.
719      * @param formRefreshType form refresh type.
720      * @param visibleFormRecords Return the visible forms' infos need to refresh.
721      * @param invisibleFormRecords Return the invisible forms' infos need to refresh.
722      * @return Returns ERR_OK on success, others on failure.
723      */
724     ErrCode GetRecordsByFormType(const int32_t formRefreshType,
725         std::vector<FormRecord> &visibleFormRecords, std::vector<FormRecord> &invisibleFormRecords);
726 
727     /**
728      * @brief Set form enable or disable.
729      * @param formId The Id of the form.
730      * @param enable Ture or false.
731      */
732     ErrCode SetFormEnable(const int64_t formId, const bool enable);
733 
734      /**
735      * @brief Set form refresh flag or in disable during.
736      * @param formId The Id of the form.
737      * @param enable Ture or false.
738      */
739     ErrCode SetRefreshDuringDisableForm(const int64_t formId, const bool enable);
740 
741      /**
742      * @brief Set form update flag or in disable during.
743      * @param formId The Id of the form.
744      * @param enable Ture or false.
745      */
746     ErrCode SetUpdateDuringDisableForm(const int64_t formId, const bool enable);
747 
748     /**
749      * @brief Enable forms style.
750      * @param formRecords The Id list of the formRecord.
751      * @param enable True is enable form, false is disable form.
752      */
753     void EnableForms(const std::vector<FormRecord> &&formRecords, const bool enable);
754 
755     /**
756      * @brief Get all formIds by userId.
757      * @param userId User ID.
758      * @param formIds The list of formId.
759      */
760     void GetFormIdsByUserId(int32_t userId, std::vector<int64_t> &formIds);
761 
762     /**
763      * @brief Recheck whether need clean form host.
764      * @param callerToken The client stub of the form host record.
765      */
766     bool RecheckWhetherNeedCleanFormHost(const sptr<IRemoteObject> &callerToken);
767 
768 private:
769     /**
770      * @brief Create form record.
771      * @param formInfo The form item info.
772      * @param callingUid The UID of the proxy.
773      * @param userId User ID.
774      * @return Form record.
775      */
776     FormRecord CreateFormRecord(const FormItemInfo &formInfo, const int callingUid,
777         const int32_t userId = Constants::DEFAULT_USER_ID) const;
778 
779     /**
780      * @brief Create host record.
781      * @param info The form item info.
782      * @param callerToken The UID of the proxy.
783      * @param callingUid The UID of the proxy.
784      * @param record The form host record.
785      * @return Returns true if this function is successfully called; returns false otherwise.
786      */
787     bool CreateHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
788         const int callingUid, FormHostRecord &record);
789 
790     /**
791      * @brief Parse update config.
792      * @param record The form record.
793      * @param info The form item info.
794      * @return None.
795      */
796     void ParseUpdateConfig(FormRecord &record, const FormItemInfo &info) const;
797 
798     /**
799      * @brief Parse update interval config.
800      * @param record The form record.
801      * @param configDuration interval duration.
802      */
803     void ParseIntervalConfig(FormRecord &record, const int configDuration) const;
804 
805     /**
806      * @brief Parse at time config.
807      * @param record The form record.
808      * @param info form item info.
809      */
810     void ParseAtTimerConfig(FormRecord &record, const FormItemInfo &info) const;
811 
812     /**
813      * @brief Get the temp forms from host and delete temp form in cache.
814      * @param record The form record.
815      * @param recordTempForms the temp forms.
816      */
817     void HandleHostDiedForTempForms(const FormHostRecord &record, std::vector<int64_t> &recordTempForms);
818 
819     /**
820      * @brief Check if two forms is same or not.
821      * @param record FormRecord.
822      * @param formInfo FormInfo.
823      * @return Returns true on success, false on failure.
824      */
825     bool IsSameForm(const FormRecord &record, const FormInfo &formInfo);
826 
827     /**
828      * @brief Check if two forms is same or not.
829      * @param record Indicates the form record.
830      * @param abilityFormInfo Indicates the AbilityFormInfo.
831      * @return Returns true on success, false on failure.
832      */
833     bool IsSameForm(const FormRecord &record, const AbilityFormInfo &abilityFormInfo);
834 
835     /**
836      * @brief check if form cached.
837      * @param record The form record.
838      * @return Returns ERR_OK on cached, others on not cached.
839      */
840     bool IsFormCached(const FormRecord record);
841 
842     /**
843     * @brief handle update form flag.
844     * @param formIDs The id of the forms.
845     * @param flag form flag.
846     * @param isOnlyEnableUpdate form enable update form flag.
847     * @param formHostRecord form host record.
848     * @param refreshForms Refresh forms
849     * @return Returns ERR_OK on success, others on failure.
850     */
851     ErrCode HandleUpdateHostFormFlag(const std::vector<int64_t> &formIds, bool flag, bool isOnlyEnableUpdate,
852                                      FormHostRecord &formHostRecord, std::vector<int64_t> &refreshForms);
853 
854     /**
855      * @brief Fill the basic running form info by form record.
856      * @param formRecord The form record.
857      * @param runningFormInfo Return the running forms' info.
858      */
859     void FillBasicRunningFormInfoByFormRecord(const FormRecord &formRecord, RunningFormInfo &runningFormInfo);
860 
861     /**
862     * @brief Get ability form info.
863     * @param record Indicates form record.
864     * @param abilities Indicates the ModuleAbilityInfo in FA model or ExtensionAbilities in stage model.
865     * @param abilityFormInfo Indicates the obtained abilityFormInfo object.
866     * @return Returns ERR_OK on success, others on failure.
867     */
868     template<typename T>
869     bool GetAbilityFormInfo(const FormRecord &record, const std::vector<T> &abilities,
870         AbilityFormInfo &abilityFormInfo);
871 
872 private:
873     void GetUnusedFormInstancesByFilter(
874         const FormInstancesFilter &formInstancesFilter, std::vector<FormInstance> &formInstances);
875     ErrCode GetUnusedFormInstanceById(const int64_t formId, FormInstance &formInstance);
876     void GetUnusedFormInfos(std::vector<RunningFormInfo> &runningFormInfos);
877     void GetUnusedFormInfos(const std::string &bundleName, std::vector<RunningFormInfo> &runningFormInfos);
878     mutable std::mutex formRecordMutex_;
879     mutable std::mutex formHostRecordMutex_;
880     mutable std::mutex formTempMutex_;
881     mutable std::mutex formStateRecordMutex_;
882     mutable std::mutex formRequestPublishFormsMutex_;
883     mutable std::mutex formAcquireDataRecordMutex_;
884     mutable std::mutex formConfigMapMutex_;
885     mutable std::mutex formCloudUpdateDurationMapMutex_;
886     std::map<int64_t, FormRecord> formRecords_;
887     std::vector<FormHostRecord> clientRecords_;
888     std::vector<int64_t> tempForms_;
889     std::map<std::string, FormHostRecord> formStateRecord_;
890     std::map<std::string, std::vector<sptr<IRemoteObject>>> formAddObservers_;
891     std::map<int32_t, FormHostRecord> formAcquireDataRecord_;
892     using FormRequestPublishFormInfo = std::pair<Want, std::unique_ptr<FormProviderData>>;
893     std::map<int64_t, FormRequestPublishFormInfo> formRequestPublishForms_;
894     int64_t udidHash_ = 0;
895     std::vector<sptr<IRemoteObject>> formObservers_;
896     std::map<std::string, int32_t> formConfigMap_;
897     std::unordered_map<std::string, int> formCloudUpdateDurationMap_;
898 };
899 } // namespace AppExecFwk
900 } // namespace OHOS
901 #endif // OHOS_FORM_FWK_FORM_FORM_DATA_MGR_H
902