• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "extension_module_loader.h"
17 
18 #include <dlfcn.h>
19 
20 #include "hilog_wrapper.h"
21 
22 namespace OHOS::AbilityRuntime {
23 namespace {
24 constexpr char EXTENSION_MODULE_ENTRY[] = "OHOS_EXTENSION_GetExtensionModule";
25 
26 using DynamicEntry = void* (*)();
27 
28 class DummyExtensionModuleLoader final : public ExtensionModuleLoader, public Singleton<DummyExtensionModuleLoader> {
29     DECLARE_SINGLETON(DummyExtensionModuleLoader);
30 
31 public:
Create(const std::unique_ptr<Runtime> & runtime) const32     Extension* Create(const std::unique_ptr<Runtime>& runtime) const override
33     {
34         return nullptr;
35     }
36 
GetParams()37     std::map<std::string, std::string> GetParams() override
38     {
39         std::map<std::string, std::string> params;
40         return params;
41     }
42 };
43 
44 DummyExtensionModuleLoader::DummyExtensionModuleLoader() = default;
45 DummyExtensionModuleLoader::~DummyExtensionModuleLoader() = default;
46 
GetExtensionModuleLoader(const char * sharedLibrary)47 ExtensionModuleLoader& GetExtensionModuleLoader(const char* sharedLibrary)
48 {
49     if (sharedLibrary == nullptr) {
50         HILOG_ERROR("Name of shared extension library MUST NOT be null pointer");
51         return DummyExtensionModuleLoader::GetInstance();
52     }
53 
54     void* handle = dlopen(sharedLibrary, RTLD_LAZY);
55     if (handle == nullptr) {
56         HILOG_ERROR("Failed to open extension library %{public}s, reason: %{public}sn", sharedLibrary, dlerror());
57         return DummyExtensionModuleLoader::GetInstance();
58     }
59 
60     auto entry = reinterpret_cast<DynamicEntry>(dlsym(handle, EXTENSION_MODULE_ENTRY));
61     if (entry == nullptr) {
62         dlclose(handle);
63         HILOG_ERROR("Failed to get extension symbol %{public}s in %{public}s", EXTENSION_MODULE_ENTRY, sharedLibrary);
64         return DummyExtensionModuleLoader::GetInstance();
65     }
66 
67     auto loader = reinterpret_cast<ExtensionModuleLoader*>(entry());
68     if (loader == nullptr) {
69         dlclose(handle);
70         HILOG_ERROR("Failed to get extension module loader in %{public}s", sharedLibrary);
71         return DummyExtensionModuleLoader::GetInstance();
72     }
73 
74     return *loader;
75 }
76 } // namespace
77 
GetLoader(const char * sharedLibrary)78 ExtensionModuleLoader& ExtensionModuleLoader::GetLoader(const char* sharedLibrary)
79 {
80     return GetExtensionModuleLoader(sharedLibrary);
81 }
82 
Create(const std::unique_ptr<Runtime> & runtime) const83 Extension *ExtensionModuleLoader::Create(const std::unique_ptr<Runtime>& runtime) const
84 {
85     return nullptr;
86 }
87 }
88