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         // no need to close the component, so we don't keep the handles
65         auto handle = dlopen(comp.lib.c_str(), RTLD_LAZY);
66         if (handle == nullptr) {
67             ZLOGE("dlopen(%{public}s) failed(%{public}d)!", comp.lib.c_str(), errno);
68             continue;
69         }
70 
71         if (comp.constructor.empty()) {
72             continue;
73         }
74 
75         auto ctor = reinterpret_cast<Constructor>(dlsym(handle, comp.constructor.c_str()));
76         if (ctor == nullptr) {
77             ZLOGE("dlsym(%{public}s) failed(%{public}d)!", comp.constructor.c_str(), errno);
78             continue;
79         }
80         ctor(comp.params.c_str());
81     }
82 }
83 
LoadCheckers()84 void Bootstrap::LoadCheckers()
85 {
86     auto *checkers = ConfigFactory::GetInstance().GetCheckerConfig();
87     if (checkers == nullptr) {
88         return;
89     }
90     CheckerManager::GetInstance().LoadCheckers(checkers->checkers);
91     for (const auto &trust : checkers->trusts) {
92         auto *checker = CheckerManager::GetInstance().GetChecker(trust.checker);
93         if (checker == nullptr) {
94             continue;
95         }
96         checker->SetTrustInfo(trust);
97     }
98     for (const auto &distrust : checkers->distrusts) {
99         auto *checker = CheckerManager::GetInstance().GetChecker(distrust.checker);
100         if (checker == nullptr) {
101             continue;
102         }
103         checker->SetDistrustInfo(distrust);
104     }
105     for (const auto &switches : checkers->switches) {
106         auto *checker = CheckerManager::GetInstance().GetChecker(switches.checker);
107         if (checker == nullptr) {
108             continue;
109         }
110         checker->SetSwitchesInfo(switches);
111     }
112     for (const auto &dynamicStore : checkers->dynamicStores) {
113         auto *checker = CheckerManager::GetInstance().GetChecker(dynamicStore.checker);
114         if (checker == nullptr) {
115             continue;
116         }
117         checker->AddDynamicStore(dynamicStore);
118     }
119     for (const auto &staticStore : checkers->staticStores) {
120         auto *checker = CheckerManager::GetInstance().GetChecker(staticStore.checker);
121         if (checker == nullptr) {
122             continue;
123         }
124         checker->AddStaticStore(staticStore);
125     }
126 }
127 
LoadBackup(std::shared_ptr<ExecutorPool> executors)128 void Bootstrap::LoadBackup(std::shared_ptr<ExecutorPool> executors)
129 {
130     auto *backupRules = ConfigFactory::GetInstance().GetBackupConfig();
131     if (backupRules == nullptr) {
132         return;
133     }
134     BackupRuleManager::GetInstance().LoadBackupRules(backupRules->rules);
135 
136     BackupManager::BackupParam backupParam = { backupRules->schedularDelay,
137         backupRules->schedularInternal, backupRules->backupInternal, backupRules->backupNumber};
138     BackupManager::GetInstance().SetBackupParam(backupParam);
139     BackupManager::GetInstance().Init();
140     BackupManager::GetInstance().BackSchedule(std::move(executors));
141 }
142 
LoadNetworks()143 void Bootstrap::LoadNetworks()
144 {
145 }
LoadDirectory()146 void Bootstrap::LoadDirectory()
147 {
148     auto *config = ConfigFactory::GetInstance().GetDirectoryConfig();
149     if (config == nullptr) {
150         return;
151     }
152     std::vector<DirectoryManager::Strategy> strategies(config->strategy.size());
153     for (size_t i = 0; i < config->strategy.size(); ++i) {
154         strategies[i] = config->strategy[i];
155     }
156     DirectoryManager::GetInstance().Initialize(strategies);
157 }
158 
LoadCloud()159 void Bootstrap::LoadCloud()
160 {
161     auto *config = ConfigFactory::GetInstance().GetCloudConfig();
162     if (config == nullptr) {
163         return;
164     }
165     std::vector<CloudConfigManager::Info> infos;
166     for (auto &info : config->mapper) {
167         infos.push_back({ info.localBundle, info.cloudBundle });
168     }
169     CloudConfigManager::GetInstance().Initialize(infos);
170 }
171 
LoadAppIdMappings()172 void Bootstrap::LoadAppIdMappings()
173 {
174     auto *appIdMapping = ConfigFactory::GetInstance().GetAppIdMappingConfig();
175     if (appIdMapping == nullptr) {
176         return;
177     }
178     std::vector<AppIdMappingConfigManager::AppMappingInfo> infos;
179     for (auto &info : *appIdMapping) {
180         infos.push_back({ info.srcAppId, info.dstAppId });
181     }
182     AppIdMappingConfigManager::GetInstance().Initialize(infos);
183 }
184 } // namespace DistributedData
185 } // namespace OHOS
186