• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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_item_info.h"
31 #include "form_js_info.h"
32 #include "form_record.h"
33 #include "form_state_info.h"
34 #include "iremote_object.h"
35 
36 namespace OHOS {
37 namespace AppExecFwk {
38 using Want = AAFwk::Want;
39 
40 /**
41  * @class FormDataMgr
42  * form data manager.
43  */
44 class FormDataMgr final : public DelayedRefSingleton<FormDataMgr> {
45 DECLARE_DELAYED_REF_SINGLETON(FormDataMgr)
46 public:
47     DISALLOW_COPY_AND_MOVE(FormDataMgr);
48     /**
49      * @brief Allot form info by item info.
50      * @param formInfo Form item info.
51      * @param callingUid The UID of the proxy.
52      * @param userId User ID.
53      * @return Returns form record.
54      */
55     FormRecord AllotFormRecord(const FormItemInfo &formInfo, const int callingUid,
56         const int32_t userId = Constants::DEFAULT_USER_ID);
57     /**
58      * @brief Create form js info by form record.
59      * @param formId The Id of the form.
60      * @param record Form record.
61      * @param formInfo Js info.
62      */
63     void CreateFormJsInfo(const int64_t formId, const FormRecord &record, FormJsInfo &formInfo);
64     /**
65      * @brief Delete form js info by form record.
66      * @param formId The Id of the form.
67      * @return Returns true if this function is successfully called; returns false otherwise.
68      */
69     bool DeleteFormRecord(const int64_t formId);
70     /**
71      * @brief Clean removed forms for host.
72      * @param removedFormIds The id list of the forms.
73      */
74     void CleanHostRemovedForms(const std::vector<int64_t> &removedFormIds);
75     /**
76      * @brief Allot form host record by caller token.
77      * @param info The form item info.
78      * @param callerToken callerToken
79      * @param formId The Id of the form.
80      * @param callingUid The UID of the proxy.
81      * @return Returns true if this function is successfully called; returns false otherwise.
82      */
83     bool AllotFormHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
84         const int64_t formId, const int callingUid);
85     /**
86      * @brief Check temp form count is max.
87      * @return Returns ERR_OK if the temp form not reached; returns ERR_MAX_SYSTEM_TEMP_FORMS is reached.
88      */
89     int CheckTempEnoughForm() const;
90     /**
91      * @brief Check form count is max.
92      * @param currentUserId The current userId.
93      * @param callingUid The UID of the proxy.
94      * @return Returns true if this function is successfully called; returns false otherwise.
95      */
96     int CheckEnoughForm(const int callingUid, const int32_t currentUserId = Constants::DEFAULT_USER_ID) const;
97     /**
98      * @brief Delete temp form.
99      * @param formId The Id of the form.
100      * @return Returns true if this function is successfully called; returns false otherwise.
101      */
102     bool DeleteTempForm(const int64_t formId);
103     /**
104      * @brief Check temp form is exist.
105      * @param formId The Id of the form.
106      * @return Returns true if the temp form is exist; returns false is not exist.
107      */
108     bool ExistTempForm(const int64_t formId) const;
109     /**
110      * @brief Modify form temp flag by formId.
111      * @param formId The Id of the form.
112      * @param formTempFlag The form temp flag.
113      * @return Returns true if this function is successfully called; returns false otherwise.
114      */
115     bool ModifyFormTempFlag(const int64_t formId, const bool formTempFlag);
116     /**
117      * @brief Add form user uid from form record.
118      * @param formId The Id of the form.
119      * @param formUserUid The form user uid.
120      * @return Returns true if this function is successfully called; returns false otherwise.
121      */
122     bool AddFormUserUid(const int64_t formId, const int formUserUid);
123     /**
124      * @brief Delete form user uid from form record.
125      * @param formId The Id of the form.
126      * @param uid calling user id.
127      * @return Returns true if this function is successfully called; returns false otherwise.
128      */
129     bool DeleteFormUserUid(const int64_t formId, const int uid);
130     /**
131      * @brief Update form record.
132      * @param formId The Id of the form.
133      * @param formRecord The form record.
134      * @return Returns true if this function is successfully called; returns false otherwise.
135      */
136     bool UpdateFormRecord(const int64_t formId, const FormRecord &formRecord);
137     /**
138      * @brief Get form record.
139      * @param formId The Id of the form.
140      * @param formRecord The form record.
141      * @return Returns true if this function is successfully called; returns false otherwise.
142      */
143     bool GetFormRecord(const int64_t formId, FormRecord &formRecord) const;
144     /**
145      * @brief Get form record.
146      * @param bundleName Bundle name.
147      * @param formInfos The form record list.
148      * @return Returns true if this function is successfully called; returns false otherwise.
149      */
150     bool GetFormRecord(const std::string &bundleName, std::vector<FormRecord> &formInfos);
151     /**
152      * @brief Check form record is exist.
153      * @param formId The Id of the form.
154      * @return Returns true if the form record is exist; returns false is not exist.
155      */
156     bool ExistFormRecord(const int64_t formId) const;
157     /**
158      * @brief Has form user uids in form record.
159      * @param formId The Id of the form.
160      * @return Returns true if this form has form user uids; returns false is not has.
161      */
162     bool HasFormUserUids(const int64_t formId) const;
163     /**
164      * @brief Get form host record.
165      * @param formId The id of the form.
166      * @param formHostRecords The form host records.
167      */
168     void GetFormHostRecord(const int64_t formId, std::vector<FormHostRecord> &formHostRecords) const;
169     /**
170      * @brief Get form host remote object.
171      * @param formId The id of the form.
172      * @param formHostObjs The form host remote object.
173      */
174     void GetFormHostRemoteObj(const int64_t formId, std::vector<sptr<IRemoteObject>> &formHostObjs) const;
175     /**
176      * @brief Delete form host record.
177      * @param callerToken The client stub of the form host record.
178      * @param formId The id of the form.
179      * @return Returns true if this function is successfully called; returns false otherwise.
180      */
181     bool DeleteHostRecord(const sptr<IRemoteObject> &callerToken, const int64_t formId);
182     /**
183      * @brief Handle form host died.
184      * @param remoteHost Form host proxy object.
185      */
186     void HandleHostDied(const sptr<IRemoteObject> &remoteHost);
187     /**
188      * @brief Refresh enable or not.
189      * @param formId The Id of the form.
190      * @return true on enable, false on disable.
191      */
192     bool IsEnableRefresh(int64_t formId);
193     /**
194      * @brief update enable or not.
195      * @param formId The Id of the form.
196      * @return true on enable, false on disable.
197      */
198     bool IsEnableUpdate(int64_t formId);
199     /**
200      * @brief Check calling uid is valid.
201      * @param formUserUids The form user uids.
202      * @return Returns true if this user uid is valid; returns false otherwise.
203      */
204     bool IsCallingUidValid(const std::vector<int> &formUserUids) const;
205     /**
206      * @brief Generate udid.
207      * @return Returns true if this function is successfully called; returns false otherwise.
208      */
209     bool GenerateUdidHash();
210     /**
211      * @brief Padding udid hash.
212      * @param formId The form id.
213      * @return Padded form id.
214      */
215     int64_t PaddingUdidHash(int64_t formId);
216     /**
217      * @brief Generate form id.
218      * @return form id.
219      */
220     int64_t GenerateFormId();
221     /**
222      * @brief Get udid.
223      * @return udid.
224      */
225     int64_t GetUdidHash() const;
226     /**
227      * @brief Set udid.
228      * @param udidHash udid.
229      */
230     void SetUdidHash(const int64_t udidHash);
231 
232     /**
233      * @brief Get the matched form host record by client stub.
234      *
235      * @param callerToken The client stub of the form host record.
236      * @param formHostRecord The form host record.
237      * @return Returns true if this function is successfully called, returns false otherwise.
238      */
239     bool GetMatchedHostClient(const sptr<IRemoteObject> &callerToken, FormHostRecord &formHostRecord) const;
240 
241     /**
242      * @brief Set needRefresh for FormRecord.
243      * @param formId The Id of the form.
244      * @param needRefresh true or false.
245      */
246     void SetNeedRefresh(const int64_t formId, const bool needRefresh);
247     /**
248      * @brief Set isCountTimerRefresh for FormRecord.
249      * @param formId The Id of the form.
250      * @param countTimerRefresh true or false.
251      */
252     void SetCountTimerRefresh(const int64_t formId, const bool countTimerRefresh);
253 
254     /**
255      * @brief Set timerRefresh for FormRecord.
256      * @param formId The Id of the form.
257      * @param timerRefresh true or false.
258      */
259     void SetTimerRefresh(const int64_t formId, const bool timerRefresh);
260 
261     /**
262      * @brief Get updated form info.
263      * @param record FormRecord.
264      * @param targetForms Target forms.
265      * @param updatedForm Updated form info.
266      * @return Returns true on success, false on failure.
267      */
268     bool GetUpdatedForm(const FormRecord &record, const std::vector<FormInfo> &targetForms, FormInfo &updatedForm);
269     /**
270      * @brief Set isEnableUpdate for FormRecord.
271      * @param formId The Id of the form.
272      * @param enableUpdate true or false.
273      */
274     void SetEnableUpdate(const int64_t formId, const bool enableUpdate);
275     /**
276      * @brief Set update info for FormRecord.
277      * @param formId The Id of the form.
278      * @param enableUpdate true or false.
279      * @param updateDuration Update duration.
280      * @param updateAtHour Update at hour.
281      * @param updateAtMin Update at minute.
282      */
283     void SetUpdateInfo(const int64_t formId, const bool enableUpdate, const long updateDuration,
284     const int updateAtHour, const int updateAtMin);
285     /**
286      * @brief Clean removed form records.
287      * @param bundleName BundleName.
288      * @param removedForms The id list of the forms.
289      */
290     void CleanRemovedFormRecords(const std::string &bundleName, std::set<int64_t> &removedForms);
291     /**
292      * @brief Clean removed temp form records.
293      * @param  bundleName BundleName.
294      * @param removedForms The id list of the forms.
295      */
296     void CleanRemovedTempFormRecords(const std::string &bundleName, const int32_t userId,
297         std::set<int64_t> &removedForms);
298     /**
299      * @brief Get recreate form records.
300      * @param reCreateForms The id list of the forms.
301      */
302     void GetReCreateFormRecordsByBundleName(const std::string &bundleName, std::set<int64_t> &reCreateForms);
303     /**
304      * @brief Set form isInited = true.
305      * @param formId The Id of the form.
306      * @param isInited isInited property
307      */
308     void SetFormCacheInited(const int64_t formId, const bool isInited);
309     /**
310      * @brief Set versionUpgrade.
311      * @param formId The Id of the form.
312      * @param versionUpgrade true or false
313      */
314     void SetVersionUpgrade(const int64_t formId, const bool versionUpgrade);
315     /**
316      * @brief Update form for host clients.
317      * @param formId The Id of the form.
318      * @param needRefresh true or false
319      */
320     void UpdateHostNeedRefresh(const int64_t formId, const bool needRefresh);
321     /**
322      * @brief Update form for host clients.
323      * @param formId The Id of the form.
324      * @param formRecord The form info.
325      * @return Returns true if form update, false if other.
326      */
327     bool UpdateHostForm(const int64_t formId, const FormRecord &formRecord);
328     /**
329      * @brief handle update form flag.
330      * @param formIDs The id of the forms.
331      * @param callerToken Caller ability token.
332      * @param flag form flag.
333      * @param isOnlyEnableUpdate form enable update form flag.
334      * @param refreshForms Refresh forms
335      * @return Returns ERR_OK on success, others on failure.
336      */
337     ErrCode UpdateHostFormFlag(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
338                                bool flag, bool isOnlyEnableUpdate, std::vector<int64_t> &refreshForms);
339     /**
340      * @brief Find matched form id.
341      * @param formId The form id.
342      * @return Matched form id.
343      */
344     int64_t FindMatchedFormId(const int64_t formId);
345     /**
346      * @brief Clear host data by uId.
347      * @param uId The caller uId.
348      */
349     void ClearHostDataByUId(const int uId);
350     /**
351      * @brief Get no host temp forms.
352      * @param uid The caller uid.
353      * @param noHostTempFormsMap no host temp forms.
354      * @param foundFormsMap Form Id list.
355      */
356     void GetNoHostTempForms(const int uid, std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
357         std::map<int64_t, bool> &foundFormsMap);
358 
359     /**
360      * @brief delete forms by userId.
361      *
362      * @param userId user ID.
363      * @param removedFormIds removed userId.
364      */
365     void DeleteFormsByUserId(const int32_t userId, std::vector<int64_t> &removedFormIds);
366     /**
367     * @brief Clear form records for st limit value test.
368     */
369     void ClearFormRecords();
370 
371     /**
372      * @brief handle get no host invalid temp forms.
373      * @param userId User ID.
374      * @param callingUid The UID of the proxy.
375      * @param matchedFormIds The set of the valid forms.
376      * @param noHostTempFormsMap The map of the no host forms.
377      * @param foundFormsMap The map of the found forms.
378      */
379     void GetNoHostInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
380                                    std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
381                                    std::map<int64_t, bool> &foundFormsMap);
382 
383     /**
384      * @brief handle delete no host temp forms.
385      * @param callingUid The UID of the proxy.
386      * @param noHostTempFormsMap The map of the no host forms.
387      * @param foundFormsMap The map of the found forms.
388      */
389     void BatchDeleteNoHostTempForms(int32_t callingUid, std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap,
390                                     std::map<int64_t, bool> &foundFormsMap);
391 
392     /**
393      * @brief delete invalid temp forms.
394      * @param userId User ID.
395      * @param callingUid The UID of the proxy.
396      * @param matchedFormIds The set of the valid forms.
397      * @param removedFormsMap The map of the removed invalid forms.
398      * @return Returns ERR_OK on success, others on failure.
399      */
400     int32_t DeleteInvalidTempForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
401                                    std::map<int64_t, bool> &removedFormsMap);
402 
403     /**
404      * @brief clear host data by invalid forms.
405      * @param callingUid The UID of the proxy.
406      * @param removedFormsMap The map of the removed invalid forms.
407      * @return Returns ERR_OK on success, others on failure.
408      */
409     int32_t ClearHostDataByInvalidForms(int32_t callingUid, std::map<int64_t, bool> &removedFormsMap);
410 
411     /**
412      * @brief delete publish forms temp data
413      * @param userId User ID.
414      * @param bundleName BundleName.
415      * @param validFormIds The set of the valid forms.
416      * @return Returns -
417      */
418     void DeleteInvalidPublishForms(int32_t userId, std::string bundleName, std::set<int64_t> &validFormIds);
419 
420     /**
421      * @brief Create form state host record.
422      * @param provider The provider of the form state
423      * @param info The form item info.
424      * @param callerToken The UID of the proxy.
425      * @param callingUid The UID of the proxy.
426      * @return Returns true if this function is successfully called; returns false otherwise.
427      */
428     bool CreateFormStateRecord(std::string &provider, const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
429                                int callingUid);
430 
431     /**
432      * @brief acquire form state callback.
433      * @param state form state.
434      * @param provider provider info.
435      * @param want The want of onAcquireFormState.
436      * @return Returns true if this function is successfully called; returns false otherwise.
437      */
438     ErrCode AcquireFormStateBack(AppExecFwk::FormState state, const std::string &provider, const Want &want);
439 
440     /**
441      * @brief Notify the form is visible or not.
442      * @param formIds Indicates the ID of the forms.
443      * @param isVisible Visible or not.
444      * @param callerToken Host client.
445      * @return Returns ERR_OK on success, others on failure.
446      */
447     ErrCode NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
448                                const sptr<IRemoteObject> &callerToken);
449 
450     /**
451      * @brief set form record visible.
452      * @param matchedFormId form id.
453      * @param isVisible is visible.
454      * @return Returns true if this function is successfully called; returns false otherwise.
455      */
456     ErrCode SetRecordVisible(int64_t matchedFormId, bool isVisible);
457 
458     /**
459      * @brief add request publish form info.
460      * @param formId The form id of the form to publish.
461      * @param want The want of the form to publish.
462      * @param formProviderData The form data.
463      * @return Returns ERR_OK on success, others on failure.
464      */
465     ErrCode AddRequestPublishFormInfo(int64_t formId, const Want &want,
466                                       std::unique_ptr<FormProviderData> &formProviderData);
467 
468     /**
469      * @brief remove request publish form info.
470      * @param formId The form id of the form to publish.
471      * @return Returns true if this function is successfully called; returns false otherwise.
472      */
473     ErrCode RemoveRequestPublishFormInfo(int64_t formId);
474 
475     /**
476      * @brief check whether request publish form info.
477       * @param formId The form id of the form to publish.
478      * @return Returns true if this function is successfully called; returns false otherwise.
479      */
480     bool IsRequestPublishForm(int64_t formId);
481 
482     /**
483      * @brief get request publish form info.
484      * @param formId The form id of the form to publish.
485      * @param want The want of the form to publish.
486      * @param formProviderData The form data.
487      * @return Returns true if this function is successfully called; returns false otherwise.
488      */
489     ErrCode GetRequestPublishFormInfo(int64_t formId, Want &want,
490                                       std::unique_ptr<FormProviderData> &formProviderData);
491     /**
492      * @brief Get updated form info.
493      * @param record Indicates the form record.
494      * @param bundlePackInfo Indicates the BundlePackInfo object.
495      * @param abilityFormInfo Indicates the obtained abilityFormInfo object.
496      * @return Returns true on success, false on failure.
497      */
498     bool GetPackageForm(const FormRecord &record, const BundlePackInfo &bundlePackInfo,
499         AbilityFormInfo &abilityFormInfo);
500 
501     /**
502      * @brief Set form free install flag.
503      * @param formId Indicates the form ID.
504      * @param isNeedFreeInstall Indicates the free install flag is true or false.
505      * @return Returns true on success, false on failure.
506      */
507     bool SetRecordNeedFreeInstall(int64_t formId, bool isNeedFreeInstall);
508 
509     /**
510      * @brief StopRenderingForm.
511      * @param formId The form id.
512      */
513     void StopRenderingForm(int32_t formId);
514 
515     /**
516      * @brief update host forms
517      * @param updateFormIds
518      */
519     void UpdateHostForms(const std::vector<int64_t> &updateFormIds);
520 private:
521     /**
522      * @brief Create form record.
523      * @param formInfo The form item info.
524      * @param callingUid The UID of the proxy.
525      * @param userId User ID.
526      * @return Form record.
527      */
528     FormRecord CreateFormRecord(const FormItemInfo &formInfo, const int callingUid,
529         const int32_t userId = Constants::DEFAULT_USER_ID) const;
530     /**
531      * @brief Create host record.
532      * @param info The form item info.
533      * @param callerToken The UID of the proxy.
534      * @param callingUid The UID of the proxy.
535      * @param record The form host record.
536      * @return Returns true if this function is successfully called; returns false otherwise.
537      */
538     bool CreateHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
539         const int callingUid, FormHostRecord &record);
540     /**
541      * @brief Parse update config.
542      * @param record The form record.
543      * @param info The form item info.
544      * @return None.
545      */
546     void ParseUpdateConfig(FormRecord &record, const FormItemInfo &info) const;
547     /**
548      * @brief Parse update interval config.
549      * @param record The form record.
550      * @param configDuration interval duration.
551      */
552     void ParseIntervalConfig(FormRecord &record, const int configDuration) const;
553     /**
554      * @brief Parse at time config.
555      * @param record The form record.
556      * @param info form item info.
557      */
558     void ParseAtTimerConfig(FormRecord &record, const FormItemInfo &info) const;
559     /**
560      * @brief Get the temp forms from host and delete temp form in cache.
561      * @param record The form record.
562      * @param recordTempForms the temp forms.
563      */
564     void HandleHostDiedForTempForms(const FormHostRecord &record, std::vector<int64_t> &recordTempForms);
565     /**
566      * @brief Check if two forms is same or not.
567      * @param record FormRecord.
568      * @param formInfo FormInfo.
569      * @return Returns true on success, false on failure.
570      */
571     bool IsSameForm(const FormRecord &record, const FormInfo &formInfo);
572 
573     /**
574      * @brief Check if two forms is same or not.
575      * @param record Indicates the form record.
576      * @param abilityFormInfo Indicates the AbilityFormInfo.
577      * @return Returns true on success, false on failure.
578      */
579     bool IsSameForm(const FormRecord &record, const AbilityFormInfo &abilityFormInfo);
580 
581     /**
582      * @brief check if form cached.
583      * @param record The form record.
584      * @return Returns ERR_OK on cached, others on not cached.
585      */
586     bool IsFormCached(const FormRecord record);
587 
588     /**
589     * @brief handle update form flag.
590     * @param formIDs The id of the forms.
591     * @param flag form flag.
592     * @param isOnlyEnableUpdate form enable update form flag.
593     * @param formHostRecord form host record.
594     * @param refreshForms Refresh forms
595     * @return Returns ERR_OK on success, others on failure.
596     */
597     ErrCode HandleUpdateHostFormFlag(const std::vector<int64_t> &formIds, bool flag, bool isOnlyEnableUpdate,
598                                      FormHostRecord &formHostRecord, std::vector<int64_t> &refreshForms);
599 
600     /**
601     * @brief Get ability form info.
602     * @param record Indicates form record.
603     * @param abilities Indicates the ModuleAbilityInfo in FA model or ExtensionAbilities in stage model.
604     * @param abilityFormInfo Indicates the obtained abilityFormInfo object.
605     * @return Returns ERR_OK on success, others on failure.
606     */
607     template<typename T>
608     bool GetAbilityFormInfo(const FormRecord &record, const std::vector<T> &abilities,
609         AbilityFormInfo &abilityFormInfo);
610 
611 private:
612     mutable std::recursive_mutex formRecordMutex_;
613     mutable std::recursive_mutex formHostRecordMutex_;
614     mutable std::recursive_mutex formTempMutex_;
615     mutable std::recursive_mutex formStateRecordMutex_;
616     mutable std::recursive_mutex formRequestPublishFormsMutex_;
617     std::map<int64_t, FormRecord> formRecords_;
618     std::vector<FormHostRecord> clientRecords_;
619     std::vector<int64_t> tempForms_;
620     std::map<std::string, FormHostRecord> formStateRecord_;
621     using FormRequestPublishFormInfo = std::pair<Want, std::unique_ptr<FormProviderData>>;
622     std::map<int64_t, FormRequestPublishFormInfo> formRequestPublishForms_;
623     int64_t udidHash_ = 0;
624 };
625 } // namespace AppExecFwk
626 } // namespace OHOS
627 #endif // OHOS_FORM_FWK_FORM_FORM_DATA_MGR_H