From c858858f2c3afa46a0123d33de020eb514f00729 Mon Sep 17 00:00:00 2001 From: jiangwensai Date: Mon, 17 Jan 2022 06:39:46 +0000 Subject: [PATCH] IssueNo: #I4R3SI:Add extension so loader. Description: Add extension so loader. Sig: SIG_ApplicationFramework Feature or Bugfix: Bugfix Binary Source: No Signed-off-by: jiangwensai Change-Id: I954977a15840236cff2ee82db5c397d19aed0feb --- frameworks/kits/ability/native/BUILD.gn | 25 ++++++ .../native/include/extension_module_loader.h | 43 ++++++++++ .../include/service_extension_module_loader.h | 35 ++++++++ .../native/src/extension_module_loader.cpp | 82 +++++++++++++++++++ .../src/service_extension_module_loader.cpp | 32 ++++++++ ohos.build | 14 +++- 6 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 frameworks/kits/ability/native/include/extension_module_loader.h create mode 100644 frameworks/kits/ability/native/include/service_extension_module_loader.h create mode 100644 frameworks/kits/ability/native/src/extension_module_loader.cpp create mode 100644 frameworks/kits/ability/native/src/service_extension_module_loader.cpp diff --git a/frameworks/kits/ability/native/BUILD.gn b/frameworks/kits/ability/native/BUILD.gn index 86aba42b525..307cff3f247 100644 --- a/frameworks/kits/ability/native/BUILD.gn +++ b/frameworks/kits/ability/native/BUILD.gn @@ -146,6 +146,7 @@ ohos_shared_library("abilitykit_native") { "${SUBSYSTEM_DIR}/src/extension.cpp", "${SUBSYSTEM_DIR}/src/extension_base.cpp", "${SUBSYSTEM_DIR}/src/extension_impl.cpp", + "${SUBSYSTEM_DIR}/src/extension_module_loader.cpp", "${SUBSYSTEM_DIR}/src/form_extension.cpp", #"${SUBSYSTEM_DIR}/src/dummy_data_ability_predicates.cpp", @@ -292,3 +293,27 @@ ohos_shared_library("dummy_classes") { subsystem_name = "aafwk" part_name = "aafwk_standard" } + +ohos_shared_library("service_extension_module") { + include_dirs = + [ "//foundation/aafwk/standard/frameworks/kits/ability/native/include" ] + + sources = [ "//foundation/aafwk/standard/frameworks/kits/ability/native/src/service_extension_module_loader.cpp" ] + + configs = [ ":ability_config" ] + public_configs = [ ":ability_public_config" ] + + deps = [ + ":abilitykit_native", + "//foundation/appexecfwk/standard/common:libappexecfwk_common", + "//utils/native/base:utils", + ] + + external_deps = [ + "aafwk_standard:runtime", + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "aafwk" + part_name = "aafwk_standard" +} diff --git a/frameworks/kits/ability/native/include/extension_module_loader.h b/frameworks/kits/ability/native/include/extension_module_loader.h new file mode 100644 index 00000000000..da16234de54 --- /dev/null +++ b/frameworks/kits/ability/native/include/extension_module_loader.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ABILITYRUNTIME_OHOS_EXTENSION_MODULE_LOADER_H +#define FOUNDATION_ABILITYRUNTIME_OHOS_EXTENSION_MODULE_LOADER_H + +#include "extension.h" +#include "runtime.h" +#include "singleton.h" + +namespace OHOS::AbilityRuntime { +/** + * @brief Load extension lib. + */ +class ExtensionModuleLoader { +public: + virtual ~ExtensionModuleLoader() = default; + + static ExtensionModuleLoader& GetLoader(const char* sharedLibrary = nullptr); + + /** + * @brief Create Extension. + * + * @param runtime The runtime. + * @return The Extension instance. + */ + virtual Extension *Create(const std::unique_ptr& runtime) const = 0; +}; +} // namespace OHOS::AbilityRuntime + +#endif // FOUNDATION_ABILITYRUNTIME_OHOS_EXTENSION_MODULE_LOADER_H diff --git a/frameworks/kits/ability/native/include/service_extension_module_loader.h b/frameworks/kits/ability/native/include/service_extension_module_loader.h new file mode 100644 index 00000000000..2e602394e3c --- /dev/null +++ b/frameworks/kits/ability/native/include/service_extension_module_loader.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ABILITYRUTIME_SERVICE_EXTENSION_MODULE_LOADER_H +#define FOUNDATION_ABILITYRUTIME_SERVICE_EXTENSION_MODULE_LOADER_H + +#include "extension_module_loader.h" + +namespace OHOS::AbilityRuntime { +class ServiceExtensionModuleLoader : public ExtensionModuleLoader, public Singleton { + DECLARE_SINGLETON(ServiceExtensionModuleLoader); + +public: + /** + * @brief Create Extension. + * + * @param runtime The runtime. + * @return The Extension instance. + */ + virtual Extension *Create(const std::unique_ptr& runtime) const override; +}; +} +#endif // FOUNDATION_ABILITYRUTIME_SERVICE_EXTENSION_MODULE_LOADER_H \ No newline at end of file diff --git a/frameworks/kits/ability/native/src/extension_module_loader.cpp b/frameworks/kits/ability/native/src/extension_module_loader.cpp new file mode 100644 index 00000000000..8e70d7b6f99 --- /dev/null +++ b/frameworks/kits/ability/native/src/extension_module_loader.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "extension_module_loader.h" + +#include + +#include "hilog_wrapper.h" + +namespace OHOS::AbilityRuntime { +namespace { +constexpr char EXTENSION_MODULE_ENTRY[] = "OHOS_EXTENSION_GetExtensionModule"; + +using DynamicEntry = void* (*)(); + +class DummyExtensionModuleLoader final : public ExtensionModuleLoader, public Singleton { + DECLARE_SINGLETON(DummyExtensionModuleLoader); + +public: + Extension* Create(const std::unique_ptr& runtime) const override + { + return nullptr; + } +}; + +DummyExtensionModuleLoader::DummyExtensionModuleLoader() = default; +DummyExtensionModuleLoader::~DummyExtensionModuleLoader() = default; + +ExtensionModuleLoader& GetExtensionModuleLoader(const char* sharedLibrary) +{ + if (sharedLibrary == nullptr) { + HILOG_ERROR("Name of shared extension library MUST NOT be null pointer"); + return DummyExtensionModuleLoader::GetInstance(); + } + + void* handle = dlopen(sharedLibrary, RTLD_LAZY); + if (handle == nullptr) { + HILOG_ERROR("Failed to open extension library %{public}s, reason: %{public}sn", sharedLibrary, dlerror()); + return DummyExtensionModuleLoader::GetInstance(); + } + + auto entry = reinterpret_cast(dlsym(handle, EXTENSION_MODULE_ENTRY)); + if (entry == nullptr) { + dlclose(handle); + HILOG_ERROR("Failed to get extension symbol %{public}s in %{public}s", EXTENSION_MODULE_ENTRY, sharedLibrary); + return DummyExtensionModuleLoader::GetInstance(); + } + + auto loader = reinterpret_cast(entry()); + if (loader == nullptr) { + dlclose(handle); + HILOG_ERROR("Failed to get extension module loader in %{public}s", sharedLibrary); + return DummyExtensionModuleLoader::GetInstance(); + } + + return *loader; +} +} // namespace + +ExtensionModuleLoader& ExtensionModuleLoader::GetLoader(const char* sharedLibrary) +{ + static ExtensionModuleLoader& instance = GetExtensionModuleLoader(sharedLibrary); + return instance; +} + +Extension *ExtensionModuleLoader::Create(const std::unique_ptr& runtime) const +{ + return nullptr; +} +} diff --git a/frameworks/kits/ability/native/src/service_extension_module_loader.cpp b/frameworks/kits/ability/native/src/service_extension_module_loader.cpp new file mode 100644 index 00000000000..c7992f786a8 --- /dev/null +++ b/frameworks/kits/ability/native/src/service_extension_module_loader.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "service_extension_module_loader.h" +#include "service_extension.h" + +namespace OHOS::AbilityRuntime { +ServiceExtensionModuleLoader::ServiceExtensionModuleLoader() = default; +ServiceExtensionModuleLoader::~ServiceExtensionModuleLoader() = default; + +Extension *ServiceExtensionModuleLoader::Create(const std::unique_ptr& runtime) const +{ + return ServiceExtension::Create(runtime); +} + +extern "C" __attribute__((visibility("default"))) void* OHOS_EXTENSION_GetExtensionModule() +{ + return &ServiceExtensionModuleLoader::GetInstance(); +} +} // namespace OHOS::AbilityRuntime \ No newline at end of file diff --git a/ohos.build b/ohos.build index 4173475df06..bb3a8412012 100755 --- a/ohos.build +++ b/ohos.build @@ -5,8 +5,8 @@ "//foundation/aafwk/standard/services:services_target", "//foundation/aafwk/standard/tools:tools_target", "//foundation/aafwk/standard/interfaces/innerkits:innerkits_target", - "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/aafwk/standard/frameworks/kits/ability/native:static_subscriber_ipc", + "//foundation/aafwk/standard/frameworks/kits/ability/native:service_extension_module", "//foundation/aafwk/standard/sa_profile:aafwk_sa_profile", "//foundation/aafwk/standard/interfaces/kits/napi:napi_packages", "//foundation/aafwk/standard/frameworks/kits/ability/ability_runtime:ability_context_native", @@ -96,6 +96,18 @@ ] }, "name": "//foundation/aafwk/standard/interfaces/innerkits/runtime:runtime" + }, + { + "header": { + "header_base": "//foundation/aafwk/standard/frameworks/kits/ability/native/include/", + "header_files": [ + "extension.h", + "extension_base.h", + "service_extension.h", + "extension_module_loader.h" + ] + }, + "name": "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native" } ], "test_list": [ -- Gitee