• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 };
37 
38 DummyExtensionModuleLoader::DummyExtensionModuleLoader() = default;
39 DummyExtensionModuleLoader::~DummyExtensionModuleLoader() = default;
40 
GetExtensionModuleLoader(const char * sharedLibrary)41 ExtensionModuleLoader& GetExtensionModuleLoader(const char* sharedLibrary)
42 {
43     if (sharedLibrary == nullptr) {
44         HILOG_ERROR("Name of shared extension library MUST NOT be null pointer");
45         return DummyExtensionModuleLoader::GetInstance();
46     }
47 
48     void* handle = dlopen(sharedLibrary, RTLD_LAZY);
49     if (handle == nullptr) {
50         HILOG_ERROR("Failed to open extension library %{public}s, reason: %{public}sn", sharedLibrary, dlerror());
51         return DummyExtensionModuleLoader::GetInstance();
52     }
53 
54     auto entry = reinterpret_cast<DynamicEntry>(dlsym(handle, EXTENSION_MODULE_ENTRY));
55     if (entry == nullptr) {
56         dlclose(handle);
57         HILOG_ERROR("Failed to get extension symbol %{public}s in %{public}s", EXTENSION_MODULE_ENTRY, sharedLibrary);
58         return DummyExtensionModuleLoader::GetInstance();
59     }
60 
61     auto loader = reinterpret_cast<ExtensionModuleLoader*>(entry());
62     if (loader == nullptr) {
63         dlclose(handle);
64         HILOG_ERROR("Failed to get extension module loader in %{public}s", sharedLibrary);
65         return DummyExtensionModuleLoader::GetInstance();
66     }
67 
68     return *loader;
69 }
70 } // namespace
71 
GetLoader(const char * sharedLibrary)72 ExtensionModuleLoader& ExtensionModuleLoader::GetLoader(const char* sharedLibrary)
73 {
74     static ExtensionModuleLoader& instance = GetExtensionModuleLoader(sharedLibrary);
75     return instance;
76 }
77 
Create(const std::unique_ptr<Runtime> & runtime) const78 Extension *ExtensionModuleLoader::Create(const std::unique_ptr<Runtime>& runtime) const
79 {
80     return nullptr;
81 }
82 }
83