1 /*
2 * Copyright (c) 2021-2024 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_tag_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 TAG_LOGE(AAFwkTag::EXT, "null sharedLibrary");
51 return DummyExtensionModuleLoader::GetInstance();
52 }
53
54 void* handle = dlopen(sharedLibrary, RTLD_LAZY);
55 if (handle == nullptr) {
56 TAG_LOGE(AAFwkTag::EXT, "Failed to open extension library %{public}s, reason: %{public}sn", sharedLibrary,
57 dlerror());
58 return DummyExtensionModuleLoader::GetInstance();
59 }
60
61 auto entry = reinterpret_cast<DynamicEntry>(dlsym(handle, EXTENSION_MODULE_ENTRY));
62 if (entry == nullptr) {
63 dlclose(handle);
64 TAG_LOGE(AAFwkTag::EXT, "Failed to get extension symbol %{public}s in %{public}s", EXTENSION_MODULE_ENTRY,
65 sharedLibrary);
66 return DummyExtensionModuleLoader::GetInstance();
67 }
68
69 auto loader = reinterpret_cast<ExtensionModuleLoader*>(entry());
70 if (loader == nullptr) {
71 dlclose(handle);
72 TAG_LOGE(AAFwkTag::EXT, "Failed to get extension module loader in %{public}s", sharedLibrary);
73 return DummyExtensionModuleLoader::GetInstance();
74 }
75
76 return *loader;
77 }
78 } // namespace
79
GetLoader(const char * sharedLibrary)80 ExtensionModuleLoader& ExtensionModuleLoader::GetLoader(const char* sharedLibrary)
81 {
82 return GetExtensionModuleLoader(sharedLibrary);
83 }
84
Create(const std::unique_ptr<Runtime> & runtime) const85 Extension *ExtensionModuleLoader::Create(const std::unique_ptr<Runtime>& runtime) const
86 {
87 return nullptr;
88 }
89 }
90