• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "loader.h"
16 #include <dlfcn.h>
17 #include <fstream>
18 #include "pasteboard_hilog.h"
19 namespace OHOS::MiscServices {
Loader()20 Loader::Loader()
21 {
22 }
23 
~Loader()24 Loader::~Loader()
25 {
26 }
27 
LoadComponents()28 void Loader::LoadComponents()
29 {
30     Config config = LoadConfig();
31     for (auto &component : config.components) {
32         if (component.lib.empty()) {
33             continue;
34         }
35 
36         // no need to close the component, so we don't keep the handles
37         auto handle = dlopen(component.lib.c_str(), RTLD_LAZY);
38         if (handle == nullptr) {
39             PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "dlopen(%{public}s) failed(%{public}d)!",
40                 component.lib.c_str(), errno);
41             continue;
42         }
43 
44         if (component.constructor.empty()) {
45             continue;
46         }
47 
48         auto ctor = reinterpret_cast<Constructor>(dlsym(handle, component.constructor.c_str()));
49         if (ctor == nullptr) {
50             continue;
51         }
52         ctor(component.params.c_str());
53     }
54 }
55 
LoadConfig()56 Config Loader::LoadConfig()
57 {
58     Config config;
59     std::string context;
60     std::ifstream fin(CONF_FILE);
61     while (fin.good()) {
62         std::string line;
63         std::getline(fin, line);
64         context += line;
65     }
66     config.Unmarshall(context);
67     return config;
68 }
69 } // namespace OHOS::MiscServices