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