• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_FORM_FWK_FORM_INFO_MGR_H
17 #define OHOS_FORM_FWK_FORM_INFO_MGR_H
18 
19 #include <shared_mutex>
20 #include <singleton.h>
21 #include <unordered_map>
22 
23 #include "appexecfwk_errors.h"
24 #include "bundle_info.h"
25 #include "data_center/database/form_db_info.h"
26 #include "form_info.h"
27 #include "form_info_filter.h"
28 #include "data_center/form_info/form_info_storage.h"
29 #include "data_center/form_record/form_record.h"
30 #include "resource_manager.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 class FormInfoHelper {
35 public:
36     static ErrCode LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector<FormInfo> &formInfos,
37         int32_t userId);
38 
39 private:
40     static ErrCode LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector<FormInfo> &formInfos);
41 
42     static ErrCode LoadStageFormConfigInfo(
43         const BundleInfo &bundleInfo, std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
44 
45     static std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager(const BundleInfo &bundleInfo);
46 
47     static ErrCode GetFormInfoDescription(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
48         FormInfo &formInfo);
49 
50     static ErrCode GetFormInfoDisplayName(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
51         FormInfo &formInfo);
52 
53     static bool LoadSharedModuleInfo(const BundleInfo &bundleInfo, HapModuleInfo &shared);
54 };
55 
56 class BundleFormInfo {
57 public:
58     explicit BundleFormInfo(const std::string &bundleName);
59 
60     ErrCode InitFromJson(const std::string &formInfoStoragesJson);
61 
62     ErrCode UpdateStaticFormInfos(int32_t userId);
63 
64     ErrCode Remove(int32_t userId);
65 
66     ErrCode AddDynamicFormInfo(const FormInfo &formInfo, int32_t userId);
67 
68     ErrCode RemoveDynamicFormInfo(const std::string &moduleName, const std::string &formName, int32_t userId);
69 
70     ErrCode RemoveAllDynamicFormsInfo(int32_t userId);
71 
72     bool Empty() const;
73 
74     ErrCode GetAllFormsInfo(std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
75 
76     uint32_t GetVersionCode(int32_t userId = Constants::INVALID_USER_ID);
77 
78     ErrCode GetFormsInfoByModule(const std::string &moduleName, std::vector<FormInfo> &formInfos,
79         int32_t userId = Constants::INVALID_USER_ID);
80 
81     ErrCode GetFormsInfoByFilter(
82         const FormInfoFilter &filter, std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
83 
84 private:
85     ErrCode UpdateFormInfoStorageLocked();
86 
87     void HandleFormInfosMaxLimit(std::vector<FormInfo> &inFormInfos,
88         std::vector<FormInfo> &outFormInfos, const std::vector<FormDBInfo> &formDBInfos);
89 
90     void GetAllUsedFormName(const std::vector<FormDBInfo> &formDBInfos,
91         const std::vector<FormInfo> &formInfos, std::set<std::string> &formDBNames);
92 
93     void ClearDistributedFormInfos(int32_t userId);
94 
95     std::string bundleName_ {};
96     mutable std::shared_timed_mutex formInfosMutex_ {};
97     std::vector<AAFwk::FormInfoStorage> formInfoStorages_ {};
98 };
99 
100 class FormInfoMgr final : public DelayedRefSingleton<FormInfoMgr> {
101 DECLARE_DELAYED_REF_SINGLETON(FormInfoMgr)
102 
103 public:
104     DISALLOW_COPY_AND_MOVE(FormInfoMgr);
105 
106     ErrCode Start();
107 
108     ErrCode UpdateStaticFormInfos(const std::string &bundleName, int32_t userId);
109 
110     ErrCode Remove(const std::string &bundleName, int32_t userId);
111 
112     ErrCode GetAllFormsInfo(std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
113 
114     ErrCode GetFormsInfoByBundle(
115         const std::string &bundleName, std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
116 
117     ErrCode GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName,
118         std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
119 
120     ErrCode GetFormsInfoByFilter(
121         const FormInfoFilter &filter, std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
122 
123     ErrCode GetFormsInfoByRecord(const FormRecord &formRecord, FormInfo &formInfo);
124 
125     ErrCode GetFormsInfoByModuleWithoutCheck(const std::string &bundleName, const std::string &moduleName,
126         std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
127 
128     ErrCode AddDynamicFormInfo(FormInfo &formInfo, int32_t userId);
129 
130     ErrCode RemoveDynamicFormInfo(const std::string &bundleName, const std::string &moduleName,
131                                   const std::string &formName, int32_t userId);
132 
133     ErrCode RemoveAllDynamicFormsInfo(const std::string &bundleName, int32_t userId);
134 
135     ErrCode ReloadFormInfos(int32_t userId);
136 
137     bool PublishFmsReadyEvent();
138 
139     bool HasReloadedFormInfos();
140 
141     ErrCode GetAppFormVisibleNotifyByBundleName(const std::string &bundleName,
142         int32_t providerUserId, bool &appFormVisibleNofify);
143     bool IsMultiAppForm(const FormInfo &formInfo);
144 
145 private:
146     std::shared_ptr<BundleFormInfo> GetOrCreateBundleFromInfo(const std::string &bundleName);
147     static bool IsCaller(const std::string& bundleName);
148     static bool CheckBundlePermission();
149     static ErrCode CheckDynamicFormInfo(FormInfo &formInfo, const BundleInfo &bundleInfo);
150     static ErrCode GetBundleVersionMap(std::map<std::string, std::uint32_t> &bundleVersionMap, int32_t userId);
151     void UpdateBundleFormInfos(std::map<std::string, std::uint32_t> &bundleVersionMap, int32_t userId);
152 
153     mutable std::shared_timed_mutex bundleFormInfoMapMutex_ {};
154     std::unordered_map<std::string, std::shared_ptr<BundleFormInfo>> bundleFormInfoMap_ {};
155     bool hasReloadedFormInfosState_ = false;
156     std::map<std::string, bool> appFormVisibleNotifyMap_;
157     std::mutex appFormVisibleNotifyMapMutex_;
158 };
159 }  // namespace AppExecFwk
160 }  // namespace OHOS
161 #endif // OHOS_FORM_FWK_FORM_INFO_MGR_H
162