Support appcore plugin 76/292876/4
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 16 May 2023 07:29:08 +0000 (07:29 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 16 May 2023 08:21:38 +0000 (08:21 +0000)
Some product developer wants to change the behavior at the start & end stages
of the app-core. This patch supports the app-core plugin.

Change-Id: I8516ec0bb8bb9d47c7bfbb05f31ee3f239c4280f
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
CMakeLists.txt
tizen-cpp/app-core-cpp/CMakeLists.txt
tizen-cpp/app-core-cpp/app_core_base.cc
tizen-cpp/app-core-cpp/app_core_plugin_private.cc [new file with mode: 0644]
tizen-cpp/app-core-cpp/app_core_plugin_private.hh [new file with mode: 0644]

index 68f96dc..2f9fe37 100644 (file)
@@ -24,7 +24,7 @@ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -Wall -Werror")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} ")
 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
 SET(CMAKE_C_FLAGS_RELEASE "-O2")
-SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} ")
+SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -std=c++17")
 SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
 SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
 
index a6c23aa..61cd425 100644 (file)
@@ -31,4 +31,5 @@ INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_APP_CORE_CPP}.pc
 INSTALL(TARGETS ${TARGET_APP_CORE_CPP} DESTINATION ${LIB_INSTALL_DIR})
 INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION include/appcore_cpp
   FILES_MATCHING
+  PATTERN "*_private.hh" EXCLUDE
   PATTERN "*.hh")
index 9d22bb1..18e6e70 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "app-core-cpp/app_core_base.hh"
+
 #include <aul.h>
 #include <aul_app_lifecycle.h>
 #include <aul_watchdog.h>
@@ -48,7 +50,7 @@
 #include <utility>
 #include <vector>
 
-#include "app-core-cpp/app_core_base.hh"
+#include "app-core-cpp/app_core_plugin_private.hh"
 #include "common/glib_private.hh"
 #include "common/log_private.hh"
 
@@ -241,6 +243,8 @@ class AppCoreBase::Impl {
   void VerifyLanguage();
   void SetDefaultEvents();
   void UnsetDefaultEvents();
+  void PluginInit(int argc, char** argv);
+  void PluginFini();
 
  private:
   friend class AppCoreBase;
@@ -259,6 +263,7 @@ class AppCoreBase::Impl {
   IAppCore* core_delegator_ = nullptr;
   IMainLoop* loop_delegator_ = nullptr;
   guint signal_handler_source_ = 0;
+  std::unique_ptr<AppCorePlugin> plugin_;
 };
 
 AppCoreBase::EventBase::EventBase(Type type)
@@ -1249,6 +1254,10 @@ void AppCoreBase::Init(int argc, char** argv) {
 
   traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
 
+  traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:PLUGIN_INIT");
+  impl_->PluginInit(argc, argv);
+  traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+
   if (impl_->feature_ & FEATURE_BACKGROUND_MANAGEMENT)
     GLib::IdleAdd(Impl::InitSuspendCb, this);
 
@@ -1333,6 +1342,7 @@ void AppCoreBase::Dispose() {
   }
 
   RemoveSuspendTimer();
+  impl_->PluginFini();
   impl_->dirty_ = false;
   if (impl_->loop_delegator_)
     impl_->loop_delegator_->OnLoopFinish();
@@ -1348,4 +1358,19 @@ int AppCoreBase::GetFeature() const {
   return impl_->feature_;
 }
 
+void AppCoreBase::Impl::PluginInit(int argc, char** argv) {
+  plugin_.reset(AppCorePlugin::Load());
+  if (!plugin_)
+    return;
+
+  plugin_->Init(argc, argv);
+}
+
+void AppCoreBase::Impl::PluginFini() {
+  if (!plugin_)
+    return;
+
+  plugin_->Fini();
+}
+
 }  // namespace tizen_cpp
diff --git a/tizen-cpp/app-core-cpp/app_core_plugin_private.cc b/tizen-cpp/app-core-cpp/app_core_plugin_private.cc
new file mode 100644 (file)
index 0000000..dd09331
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2023 Samsung Electronics 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 "app-core-cpp/app_core_plugin_private.hh"
+
+#include <dlfcn.h>
+#include <unistd.h>
+
+#include <filesystem>
+#include <new>
+
+#include "common/log_private.hh"
+
+namespace tizen_cpp {
+
+namespace {
+
+constexpr const char kPathLibAppCorePlugin[] =
+    "/usr/share/appcore/plugins/libappcore-plugin.so";
+constexpr const char kAppCorePluginInit[] = "APP_CORE_PLUGIN_INIT";
+constexpr const char kAppCorePluginFini[] = "APP_CORE_PLUGIN_FINI";
+
+}  // namespace
+
+AppCorePlugin::AppCorePlugin(void* handle, PluginInitFunc init_func,
+    PluginFiniFunc fini_func)
+    : handle_(handle), init_func_(init_func), fini_func_(fini_func) {
+}
+
+AppCorePlugin::~AppCorePlugin() {
+  if (handle_)
+    dlclose(handle_);
+}
+
+AppCorePlugin* AppCorePlugin::Load() {
+  if (!std::filesystem::exists(kPathLibAppCorePlugin))
+    return nullptr;
+
+  void* handle = dlopen(kPathLibAppCorePlugin, RTLD_LAZY);
+  if (handle == nullptr) {
+    _E("dlopen() is failed. path(%s), error(%s)",
+        kPathLibAppCorePlugin, dlerror());
+    return nullptr;
+  }
+
+  auto init_func = reinterpret_cast<PluginInitFunc>(
+      dlsym(handle, kAppCorePluginInit));
+  auto fini_func = reinterpret_cast<PluginFiniFunc>(
+      dlsym(handle, kAppCorePluginFini));
+  if (init_func == nullptr && fini_func == nullptr) {
+    _W("There is no symbols");
+    dlclose(handle);
+    return nullptr;
+  }
+
+  _W("init_func(%p), fini_func(%p)", init_func, fini_func);
+  return new (std::nothrow) AppCorePlugin(handle, init_func, fini_func);
+}
+
+void AppCorePlugin::Init(int argc, char** argv) {
+  _I("[PLUGIN] INIT");
+  if (init_func_ == nullptr)
+    return;
+
+  init_func_(argc, argv);
+}
+
+void AppCorePlugin::Fini() {
+  _I("[PLUGIN] FINI");
+  if (fini_func_ == nullptr)
+    return;
+
+  fini_func_();
+}
+
+}  // namespace tizen_cpp
diff --git a/tizen-cpp/app-core-cpp/app_core_plugin_private.hh b/tizen-cpp/app-core-cpp/app_core_plugin_private.hh
new file mode 100644 (file)
index 0000000..dc74a73
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2023 Samsung Electronics 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 TIZEN_CPP_APP_CORE_CPP_APP_CORE_PLUGIN_PRIVATE_HH_
+#define TIZEN_CPP_APP_CORE_CPP_APP_CORE_PLUGIN_PRIVATE_HH_
+
+namespace tizen_cpp {
+
+class AppCorePlugin {
+ public:
+  using PluginInitFunc = int (*)(int, char**);
+  using PluginFiniFunc = void (*)();
+
+  AppCorePlugin(void* handle, PluginInitFunc init_func,
+      PluginFiniFunc fini_func);
+  ~AppCorePlugin();
+
+  static AppCorePlugin* Load();
+
+  void Init(int argc, char** argv);
+  void Fini();
+
+ private:
+  void* handle_ = nullptr;
+  PluginInitFunc init_func_ = nullptr;
+  PluginFiniFunc fini_func_ = nullptr;
+};
+
+}  // namespace tizen_cpp
+
+#endif  // TIZEN_CPP_APP_CORE_CPP_APP_CORE_PLUGIN_PRIVATE_HH_