1 /*
2 * Copyright (c) 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 #define LOG_TAG "Bootstrap"
16 #include "bootstrap.h"
17
18 #include <dlfcn.h>
19
20 #include "access_check/app_access_check_config_manager.h"
21 #include "app_id_mapping/app_id_mapping_config_manager.h"
22 #include "backup_manager.h"
23 #include "backuprule/backup_rule_manager.h"
24 #include "checker/checker_manager.h"
25 #include "cloud/cloud_config_manager.h"
26 #include "config_factory.h"
27 #include "device_sync_app/device_sync_app_manager.h"
28 #include "directory/directory_manager.h"
29 #include "log_print.h"
30 #include "thread/thread_manager.h"
31 namespace OHOS {
32 namespace DistributedData {
GetInstance()33 Bootstrap &Bootstrap::GetInstance()
34 {
35 static Bootstrap bootstrap;
36 return bootstrap;
37 }
38
GetProcessLabel()39 std::string Bootstrap::GetProcessLabel()
40 {
41 auto *global = ConfigFactory::GetInstance().GetGlobalConfig();
42 if (global == nullptr || global->processLabel.empty()) {
43 return DEFAULT_LABEL;
44 }
45 return global->processLabel;
46 }
47
GetMetaDBName()48 std::string Bootstrap::GetMetaDBName()
49 {
50 auto *global = ConfigFactory::GetInstance().GetGlobalConfig();
51 if (global == nullptr || global->metaData.empty()) {
52 return DEFAULT_META;
53 }
54 return global->metaData;
55 }
56
LoadComponents()57 void Bootstrap::LoadComponents()
58 {
59 auto *comps = ConfigFactory::GetInstance().GetComponentConfig();
60 if (comps == nullptr) {
61 return;
62 }
63 for (auto &comp : *comps) {
64 if (comp.lib.empty()) {
65 continue;
66 }
67 // no need to close the component, so we don't keep the handles
68 auto handle = dlopen(comp.lib.c_str(), RTLD_LAZY);
69 if (handle == nullptr) {
70 ZLOGE("dlopen(%{public}s) failed(%{public}d)!", comp.lib.c_str(), errno);
71 continue;
72 }
73
74 if (comp.constructor.empty()) {
75 continue;
76 }
77
78 auto ctor = reinterpret_cast<Constructor>(dlsym(handle, comp.constructor.c_str()));
79 if (ctor == nullptr) {
80 ZLOGE("dlsym(%{public}s) failed(%{public}d)!", comp.constructor.c_str(), errno);
81 continue;
82 }
83 ctor(comp.params.c_str());
84 }
85 }
86
LoadCheckers()87 void Bootstrap::LoadCheckers()
88 {
89 auto *checkers = ConfigFactory::GetInstance().GetCheckerConfig();
90 if (checkers == nullptr) {
91 return;
92 }
93 CheckerManager::GetInstance().LoadCheckers(checkers->checkers);
94 for (const auto &trust : checkers->trusts) {
95 auto *checker = CheckerManager::GetInstance().GetChecker(trust.checker);
96 if (checker == nullptr) {
97 continue;
98 }
99 checker->SetTrustInfo(trust);
100 }
101 for (const auto &distrust : checkers->distrusts) {
102 auto *checker = CheckerManager::GetInstance().GetChecker(distrust.checker);
103 if (checker == nullptr) {
104 continue;
105 }
106 checker->SetDistrustInfo(distrust);
107 }
108 for (const auto &switches : checkers->switches) {
109 auto *checker = CheckerManager::GetInstance().GetChecker(switches.checker);
110 if (checker == nullptr) {
111 continue;
112 }
113 checker->SetSwitchesInfo(switches);
114 }
115 for (const auto &dynamicStore : checkers->dynamicStores) {
116 auto *checker = CheckerManager::GetInstance().GetChecker(dynamicStore.checker);
117 if (checker == nullptr) {
118 continue;
119 }
120 checker->AddDynamicStore(dynamicStore);
121 }
122 for (const auto &staticStore : checkers->staticStores) {
123 auto *checker = CheckerManager::GetInstance().GetChecker(staticStore.checker);
124 if (checker == nullptr) {
125 continue;
126 }
127 checker->AddStaticStore(staticStore);
128 }
129 }
130
LoadBackup(std::shared_ptr<ExecutorPool> executors)131 void Bootstrap::LoadBackup(std::shared_ptr<ExecutorPool> executors)
132 {
133 auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig();
134 if (backupRules == nullptr) {
135 return;
136 }
137 BackupRuleManager::GetInstance().LoadBackupRules(backupRules->rules);
138
139 BackupManager::BackupParam backupParam = { backupRules->schedularDelay,
140 backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber};
141 BackupManager::GetInstance().SetBackupParam(backupParam);
142 BackupManager::GetInstance().Init();
143 BackupManager::GetInstance().BackSchedule(std::move(executors));
144 }
145
LoadNetworks()146 void Bootstrap::LoadNetworks()
147 {
148 }
LoadDirectory()149 void Bootstrap::LoadDirectory()
150 {
151 auto *config = ConfigFactory::GetInstance().GetDirectoryConfig();
152 if (config == nullptr) {
153 return;
154 }
155 std::vector<DirectoryManager::Strategy> strategies(config->strategy.size());
156 for (size_t i = 0; i < config->strategy.size(); ++i) {
157 strategies[i] = config->strategy[i];
158 }
159 auto typeSize = config->storeTypes.size();
160 std::vector<DirectoryManager::StoreType> storeTypes(typeSize);
161 for (size_t i = 0; i < typeSize; ++i) {
162 storeTypes[i] = config->storeTypes[i];
163 }
164 DirectoryManager::GetInstance().Initialize(strategies, storeTypes);
165 }
166
LoadCloud()167 void Bootstrap::LoadCloud()
168 {
169 auto *config = ConfigFactory::GetInstance().GetCloudConfig();
170 if (config == nullptr) {
171 return;
172 }
173 std::vector<CloudConfigManager::Info> infos;
174 for (auto &info : config->mapper) {
175 infos.push_back({ info.localBundle, info.cloudBundle });
176 }
177 CloudConfigManager::GetInstance().Initialize(infos);
178 }
179
LoadAppIdMappings()180 void Bootstrap::LoadAppIdMappings()
181 {
182 auto *appIdMapping = ConfigFactory::GetInstance().GetAppIdMappingConfig();
183 if (appIdMapping == nullptr) {
184 return;
185 }
186 std::vector<AppIdMappingConfigManager::AppMappingInfo> infos;
187 for (auto &info : *appIdMapping) {
188 infos.push_back({ info.srcAppId, info.dstAppId });
189 }
190 AppIdMappingConfigManager::GetInstance().Initialize(infos);
191 }
192
LoadThread()193 void Bootstrap::LoadThread()
194 {
195 auto *config = ConfigFactory::GetInstance().GetThreadConfig();
196 if (config == nullptr) {
197 return;
198 }
199 ThreadManager::GetInstance().Initialize(config->minThreadNum, config->maxThreadNum, config->ipcThreadNum);
200 }
201
LoadDeviceSyncAppWhiteLists()202 void Bootstrap::LoadDeviceSyncAppWhiteLists()
203 {
204 auto *deviceSyncAppWhiteLists = ConfigFactory::GetInstance().GetDeviceSyncAppWhiteListConfig();
205 if (deviceSyncAppWhiteLists == nullptr) {
206 return;
207 }
208 std::vector<DeviceSyncAppManager::WhiteList> infos;
209 for (const auto &info : deviceSyncAppWhiteLists->whiteLists) {
210 infos.push_back({ info.appId, info.bundleName, info.version });
211 }
212 DeviceSyncAppManager::GetInstance().Initialize(infos);
213 }
214
LoadSyncTrustedApp()215 void Bootstrap::LoadSyncTrustedApp()
216 {
217 auto *config = ConfigFactory::GetInstance().GetSyncAppsConfig();
218 if (config == nullptr) {
219 return;
220 }
221 std::vector<AppAccessCheckConfigManager::AppMappingInfo> infos;
222 for (const auto &info : config->trusts) {
223 infos.push_back({ info.bundleName, info.appId });
224 }
225 AppAccessCheckConfigManager::GetInstance().Initialize(infos);
226 }
227 } // namespace DistributedData
228 } // namespace OHOS