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