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 "form_info.h"
26 #include "form_info_filter.h"
27 #include "form_info_storage.h"
28 #include "form_record.h"
29 #include "resource_manager.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 class FormInfoHelper {
34 public:
35     static ErrCode LoadFormConfigInfoByBundleName(const std::string &bundleName, std::vector<FormInfo> &formInfos,
36         int32_t userId);
37 
38 private:
39     static ErrCode LoadAbilityFormConfigInfo(const BundleInfo &bundleInfo, std::vector<FormInfo> &formInfos);
40 
41     static ErrCode LoadStageFormConfigInfo(const BundleInfo &bundleInfo, std::vector<FormInfo> &formInfos);
42 
43     static std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager(const BundleInfo &bundleInfo);
44 
45     static ErrCode GetFormInfoDescription(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
46         FormInfo &formInfo);
47 
48     static ErrCode GetFormInfoDisplayName(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
49         FormInfo &formInfo);
50 };
51 
52 class BundleFormInfo {
53 public:
54     explicit BundleFormInfo(const std::string &bundleName);
55 
56     ErrCode InitFromJson(const std::string &formInfoStoragesJson);
57 
58     ErrCode UpdateStaticFormInfos(int32_t userId);
59 
60     ErrCode Remove(int32_t userId);
61 
62     ErrCode AddDynamicFormInfo(const FormInfo &formInfo, int32_t userId);
63 
64     ErrCode RemoveDynamicFormInfo(const std::string &moduleName, const std::string &formName, int32_t userId);
65 
66     ErrCode RemoveAllDynamicFormsInfo(int32_t userId);
67 
68     bool Empty() const;
69 
70     ErrCode GetAllFormsInfo(std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
71 
72     uint32_t GetVersionCode(int32_t userId = Constants::INVALID_USER_ID);
73 
74     ErrCode GetFormsInfoByModule(const std::string &moduleName, std::vector<FormInfo> &formInfos,
75         int32_t userId = Constants::INVALID_USER_ID);
76 
77     ErrCode GetFormsInfoByFilter(
78         const FormInfoFilter &filter, std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
79 
80 private:
81     ErrCode UpdateFormInfoStorageLocked();
82 
83     std::string bundleName_ {};
84     mutable std::shared_timed_mutex formInfosMutex_ {};
85     std::vector<AAFwk::FormInfoStorage> formInfoStorages_ {};
86 };
87 
88 class FormInfoMgr final : public DelayedRefSingleton<FormInfoMgr> {
89 DECLARE_DELAYED_REF_SINGLETON(FormInfoMgr)
90 
91 public:
92     DISALLOW_COPY_AND_MOVE(FormInfoMgr);
93 
94     ErrCode Start();
95 
96     ErrCode UpdateStaticFormInfos(const std::string &bundleName, int32_t userId);
97 
98     ErrCode Remove(const std::string &bundleName, int32_t userId);
99 
100     ErrCode GetAllFormsInfo(std::vector<FormInfo> &formInfos);
101 
102     ErrCode GetFormsInfoByBundle(
103         const std::string &bundleName, std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
104 
105     ErrCode GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName,
106         std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
107 
108     ErrCode GetFormsInfoByFilter(
109         const FormInfoFilter &filter, std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
110 
111     ErrCode GetFormsInfoByRecord(const FormRecord &formRecord, FormInfo &formInfo);
112 
113     ErrCode GetFormsInfoByModuleWithoutCheck(const std::string &bundleName, const std::string &moduleName,
114         std::vector<FormInfo> &formInfos, int32_t userId = Constants::INVALID_USER_ID);
115 
116     ErrCode AddDynamicFormInfo(FormInfo &formInfo, int32_t userId);
117 
118     ErrCode RemoveDynamicFormInfo(const std::string &bundleName, const std::string &moduleName,
119                                   const std::string &formName, int32_t userId);
120 
121     ErrCode RemoveAllDynamicFormsInfo(const std::string &bundleName, int32_t userId);
122 
123     ErrCode ReloadFormInfos(int32_t userId);
124 
125     bool HasReloadedFormInfos();
126 
127 private:
128     std::shared_ptr<BundleFormInfo> GetOrCreateBundleFromInfo(const std::string &bundleName);
129     static bool IsCaller(const std::string& bundleName);
130     static bool CheckBundlePermission();
131     static ErrCode CheckDynamicFormInfo(FormInfo &formInfo, const BundleInfo &bundleInfo);
132     static ErrCode GetBundleVersionMap(std::map<std::string, std::uint32_t> &bundleVersionMap, int32_t userId);
133 
134     mutable std::shared_timed_mutex bundleFormInfoMapMutex_ {};
135     std::unordered_map<std::string, std::shared_ptr<BundleFormInfo>> bundleFormInfoMap_ {};
136     bool hasReloadedFormInfosState_ = false;
137 };
138 }  // namespace AppExecFwk
139 }  // namespace OHOS
140 #endif // OHOS_FORM_FWK_FORM_INFO_MGR_H
141