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