1 /* 2 * Copyright (c) 2025 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_DISTRIBUTED_MGR_H 17 #define OHOS_FORM_FWK_FORM_DISTRIBUTED_MGR_H 18 19 #include <map> 20 #include <shared_mutex> 21 #include <singleton.h> 22 #include <string> 23 #include "nlohmann/json.hpp" 24 25 #include "data_center/database/form_rdb_data_mgr.h" 26 27 namespace OHOS { 28 namespace AppExecFwk { 29 struct DistributedModule { 30 int32_t userId = -1; 31 std::string entryModule; 32 std::string uiModule; 33 std::string extraValue; 34 }; 35 36 /** 37 * @class FormDistributedMgr 38 * Form distributed manager. 39 */ 40 class FormDistributedMgr final : public DelayedRefSingleton<FormDistributedMgr> { 41 DECLARE_DELAYED_REF_SINGLETON(FormDistributedMgr) 42 public: 43 DISALLOW_COPY_AND_MOVE(FormDistributedMgr); 44 45 /** 46 * @brief pre-processing when fms start. 47 */ 48 void Start(); 49 50 /** 51 * @brief Init form bundle distributed mgr. 52 * @return True for sucessful init, false for failed init. 53 */ 54 bool Init(); 55 56 /** 57 * @brief Get whether bundle is distributed. 58 * @param bundleName Bundle name to be check. 59 * @param userId calling userId to be check. 60 * @return True for distributed, false for not distributed. 61 */ 62 bool IsBundleDistributed(const std::string &bundleName, int32_t userId); 63 64 /** 65 * @brief Set whether bundle is distributed. 66 * @param bundleName Bundle name to be set. 67 * @param isDistributed True for distributed, false for not distributed. 68 * @param distributedModule distributed data. 69 */ 70 void SetBundleDistributedStatus( 71 const std::string &bundleName, bool isDistributed, const DistributedModule &distributedModule); 72 73 /** 74 * @brief Get distributed app ui moduleName. 75 * @param bundleName Bundle name. 76 * @param userId calling userId. 77 * @return ui moduleName. 78 */ 79 std::string GetUiModuleName(const std::string &bundleName, int32_t userId); 80 81 private: 82 /** 83 * @brief check whether bundle distributed mgr is init. 84 * @return True for inited, false for not init. 85 */ 86 bool IsBundleDistributedInit(); 87 88 void AlterTableAddColumn(); 89 90 void LoadDataFromDb(); 91 92 void SaveDataToDb(const std::string &bundleName, const DistributedModule &distributedModule); 93 94 void DeleteDataInDb(const std::string &bundleName, int32_t userId); 95 96 private: 97 bool isInitialized_ = false; 98 std::unordered_map<std::string, DistributedModule> distributedBundleMap_; 99 mutable std::shared_mutex bundleDistributedMapMutex_; 100 }; 101 } // namespace AppExecFwk 102 } // namespace OHOS 103 104 #endif // OHOS_FORM_FWK_FORM_DISTRIBUTED_MGR_H