1 /*
2 * Copyright (c) 2023 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 "static_subscriber_extension_module_loader.h"
17
18 #include "event_log_wrapper.h"
19 #include "static_subscriber_extension.h"
20 #include "js_static_subscriber_extension.h"
21
22 #include <dlfcn.h>
23
24 constexpr char STS_STATIC_SUBSCRIBER_EXT_LIB_NAME[] = "libstatic_subscriber_extension_ani.z.so";
25 static constexpr char STS_STATIC_SUBSCRIBER_EXT_CREATE_FUNC[] = "OHOS_STS_StaticSubscriberExtension_Creation";
26
27 namespace OHOS {
28 namespace EventFwk {
29
30 typedef StaticSubscriberExtension* (*CREATE_FUNC)(const std::unique_ptr<AbilityRuntime::Runtime>& runtime);
31
CreateStsExtension(const std::unique_ptr<AbilityRuntime::Runtime> & runtime)32 __attribute__((no_sanitize("cfi"))) StaticSubscriberExtension* CreateStsExtension(
33 const std::unique_ptr<AbilityRuntime::Runtime>& runtime)
34 {
35 void *handle = dlopen(STS_STATIC_SUBSCRIBER_EXT_LIB_NAME, RTLD_LAZY);
36 if (handle == nullptr) {
37 EVENT_LOGE("open sts_static_subscriber_extension library %{public}s failed, reason: %{public}sn",
38 STS_STATIC_SUBSCRIBER_EXT_LIB_NAME, dlerror());
39 return new (std::nothrow) StaticSubscriberExtension();
40 }
41
42 auto func = reinterpret_cast<CREATE_FUNC>(dlsym(handle, STS_STATIC_SUBSCRIBER_EXT_CREATE_FUNC));
43 if (func == nullptr) {
44 dlclose(handle);
45 EVENT_LOGE("get sts_static_subscriber_extension symbol %{public}s in %{public}s failed",
46 STS_STATIC_SUBSCRIBER_EXT_CREATE_FUNC, STS_STATIC_SUBSCRIBER_EXT_LIB_NAME);
47 return new (std::nothrow) StaticSubscriberExtension();
48 }
49
50 auto instance = func(runtime);
51 if (instance == nullptr) {
52 dlclose(handle);
53 EVENT_LOGE("get sts_static_subscriber_extension instance in %{public}s failed",
54 STS_STATIC_SUBSCRIBER_EXT_CREATE_FUNC);
55 return new (std::nothrow) StaticSubscriberExtension();
56 }
57 return instance;
58 }
59
60 StaticSubscriberExtensionModuleLoader::StaticSubscriberExtensionModuleLoader() = default;
61 StaticSubscriberExtensionModuleLoader::~StaticSubscriberExtensionModuleLoader() = default;
62
Create(const std::unique_ptr<AbilityRuntime::Runtime> & runtime) const63 AbilityRuntime::Extension* StaticSubscriberExtensionModuleLoader::Create(
64 const std::unique_ptr<AbilityRuntime::Runtime>& runtime) const
65 {
66 EVENT_LOGD("Create module loader.");
67 if (!runtime) {
68 return StaticSubscriberExtension::Create(runtime);
69 }
70
71 EVENT_LOGI("Create runtime");
72 switch (runtime->GetLanguage()) {
73 case AbilityRuntime::Runtime::Language::JS:
74 return JsStaticSubscriberExtension::Create(runtime);
75 case AbilityRuntime::Runtime::Language::ETS:
76 return CreateStsExtension(runtime);
77 default:
78 return StaticSubscriberExtension::Create(runtime);
79 }
80 }
81
GetParams()82 std::map<std::string, std::string> StaticSubscriberExtensionModuleLoader::GetParams()
83 {
84 EVENT_LOGD("Get params.");
85 std::map<std::string, std::string> params;
86 // type means extension type in ExtensionAbilityType of extension_ability_info.h, 7 means static_subscriber.
87 params.insert(std::pair<std::string, std::string>("type", "7"));
88 // extension name
89 params.insert(std::pair<std::string, std::string>("name", "StaticSubscriberExtension"));
90 return params;
91 }
92
OHOS_EXTENSION_GetExtensionModule()93 extern "C" __attribute__((visibility("default"))) void* OHOS_EXTENSION_GetExtensionModule()
94 {
95 return &StaticSubscriberExtensionModuleLoader::GetInstance();
96 }
97 } // namespace EventFwk
98 } // namespace OHOS
99