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 "frameworks/bridge/js_frontend/engine/common/js_engine_loader.h"
17
18 #include <dlfcn.h>
19
20 #include "base/log/log.h"
21 #include "base/utils/singleton.h"
22 #include "base/utils/system_properties.h"
23 #include "core/common/ace_application_info.h"
24
25 namespace OHOS::Ace::Framework {
26 namespace {
27
28 constexpr char JS_ENGINE_ENTRY[] = "OHOS_ACE_GetJsEngineLoader";
29
30 using DynamicEntry = void* (*)();
31
32 class DummyJsEngineLoader final : public JsEngineLoader, public Singleton<DummyJsEngineLoader> {
33 DECLARE_SINGLETON(DummyJsEngineLoader);
34
35 public:
CreateJsEngine(int32_t instanceId) const36 RefPtr<JsEngine> CreateJsEngine(int32_t instanceId) const override
37 {
38 return nullptr;
39 }
40
CreateJsEngineUsingSharedRuntime(int32_t instanceId,void * runtime) const41 RefPtr<JsEngine> CreateJsEngineUsingSharedRuntime(int32_t instanceId, void* runtime) const override
42 {
43 return nullptr;
44 }
45
CreateCanvasBridge() const46 RefPtr<BaseCanvasBridge> CreateCanvasBridge() const override
47 {
48 return nullptr;
49 }
50
CreateXComponentBridge() const51 RefPtr<BaseXComponentBridge> CreateXComponentBridge() const override
52 {
53 return nullptr;
54 }
55 };
56
57 DummyJsEngineLoader::DummyJsEngineLoader() = default;
58 DummyJsEngineLoader::~DummyJsEngineLoader() = default;
59
GetJsEngineLoader(const char * sharedLibrary)60 JsEngineLoader& GetJsEngineLoader(const char* sharedLibrary)
61 {
62 if (sharedLibrary == nullptr) {
63 LOGE("Name of shared library MUST NOT be null pointer");
64 return DummyJsEngineLoader::GetInstance();
65 }
66
67 void* handle = dlopen(sharedLibrary, RTLD_LAZY);
68 if (handle == nullptr) {
69 LOGE("Failed to open shared library %{public}s, reason: %{public}sn", sharedLibrary, dlerror());
70 return DummyJsEngineLoader::GetInstance();
71 }
72
73 auto entry = reinterpret_cast<DynamicEntry>(dlsym(handle, JS_ENGINE_ENTRY));
74 if (entry == nullptr) {
75 dlclose(handle);
76 LOGE("Failed to get symbol %{public}s in %{public}s", JS_ENGINE_ENTRY, sharedLibrary);
77 return DummyJsEngineLoader::GetInstance();
78 }
79
80 auto loader = reinterpret_cast<JsEngineLoader*>(entry());
81 if (loader == nullptr) {
82 dlclose(handle);
83 LOGE("Failed to get js engine loader in %{public}s", sharedLibrary);
84 return DummyJsEngineLoader::GetInstance();
85 }
86
87 return *loader;
88 }
89
90 } // namespace
91
Get(const char * sharedLibrary)92 JsEngineLoader& JsEngineLoader::Get(const char* sharedLibrary)
93 {
94 static JsEngineLoader& instance = GetJsEngineLoader(sharedLibrary);
95 return instance;
96 }
97
GetDeclarative(const char * sharedLibrary)98 JsEngineLoader& JsEngineLoader::GetDeclarative(const char* sharedLibrary)
99 {
100 static JsEngineLoader& instance = GetJsEngineLoader(sharedLibrary);
101 return instance;
102 }
103
104 } // namespace OHOS::Ace::Framework
105