[M120][WRTjs][Service] Refactor the dbus logic for wrt-service 23/305423/1
authorChunling Ye <chunling.ye@samsung.com>
Wed, 27 Dec 2023 02:18:13 +0000 (10:18 +0800)
committerDongHyun Song <dh81.song@samsung.com>
Thu, 1 Feb 2024 09:48:07 +0000 (09:48 +0000)
Move the dbus related logic to WrtServiceManager, and then
waitting all init work have done, start RegisterDbusHandler.

Reference:
https://review.tizen.org/gerrit/#/c/platform/framework/web/chromium-efl/+/304105/

Change-Id: I277242e5e3a421b46383cff3740e33cc35b5a2b5
Signed-off-by: Chunling Ye <chunling.ye@samsung.com>
(cherry picked from commit cf81c129f223890fa12bcb1a6ce8d6a5b3fe23dc)

wrt/src/service/wrt_service.cc
wrt/src/service/wrt_service_manager.cc
wrt/src/service/wrt_service_manager.h

index 056292e8b120a142c800424dc4996c82a5a511cf..cd7961cac7c23ba9b204f840be8db69eb7f8375a 100644 (file)
 #include "content/common/paths_efl.h"
 #include "electron/shell/app/node_main.h"
 #include "tizen_src/ewk/efl_integration/command_line_efl.h"
-#include "wrt/src/base/string_utils.h"
 #include "wrt/src/common/application_data.h"
 #include "wrt/src/service/wrt_service_manager.h"
 
-#if BUILDFLAG(IS_TIZEN_TV)
-#include "wrt/src/browser/tv/encrypted_file_handler.h"
-#include "wrt/src/common/constants.h"
-#endif
-
 void _register_wrt();
 void _register_wrt_xwalk_extension();
 
 namespace wrt {
 
-namespace {
-
-uv_thread_t dbus_thread;
-
-void DbusStartService(const char*, const char* internal_id,
-    const InterProcessCommunication::Message& message) {
-  auto service_manager = WRTServiceManager::Get();
-  if (service_manager->IsQuitting())
-    return;
-
-  auto service_id = ApplicationData::GetServiceID(internal_id);
-  std::string pkg_path = ApplicationData::GetPackagePath(service_id);
-  service_manager->StartService(internal_id, message.Sender(), pkg_path);
-  message.Reply("Success");
-}
-
-void DbusStopService(const char*, const char* internal_id,
-    const InterProcessCommunication::Message& message) {
-  auto service_manager = WRTServiceManager::Get();
-  if (service_manager->IsQuitting())
-    return;
-
-  if (service_manager->DidStartService(internal_id)) {
-    service_manager->StopService(internal_id);
-    message.Reply("Success");
-  } else {
-    LOG(WARNING) << internal_id << " isn't running";
-    message.Reply("Failed");
-  }
-}
-
-void DbusSetAppcontrolData(const char*, const char* argument,
-    const InterProcessCommunication::Message& message) {
-  auto service_manager = WRTServiceManager::Get();
-  if (service_manager->IsQuitting())
-    return;
-
-  if (!argument) {
-    LOG(ERROR) << "There is no argument of SetAppcontrolData";
-    return;
-  }
-  auto appcontrol_argv = base::SplitString(std::string(argument), ":",
-                             base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
-  if (appcontrol_argv.size() != 2) {
-    LOG(ERROR) << "Argument should be {app_id}:{app_control_data}";
-    return;
-  }
-  service_manager->SetAppcontrolData(appcontrol_argv[0], appcontrol_argv[1]);
-}
-
-void DbusBuiltinService(const char*, const char* argument,
-    const InterProcessCommunication::Message& message) {
-  auto service_manager = WRTServiceManager::Get();
-  if (service_manager->IsQuitting())
-    return;
-
-  if (!argument) {
-    LOG(ERROR) << "There is no argument of BuiltinService";
-    return;
-  }
-  auto service_argv = base::SplitString(std::string(argument), ":",
-                          base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
-  if (service_argv.size() != 3) {
-    LOG(ERROR) << "Argument should be {service_name}:{app_id}:{app_path}";
-    return;
-  }
-  auto& service_name = service_argv[0];
-  auto& app_id = service_argv[1];
-  auto& app_path = service_argv[2];
-  LOG(INFO) << "Service Name: " << service_name << ", " << app_id << "("
-            << app_path << ")";
-  service_manager->RunBuiltinService(service_name, app_id, app_path);
-}
-
-void DbusDelayShutdown(const char*, const char* argument,
-    const InterProcessCommunication::Message& message) {
-  auto service_manager = WRTServiceManager::Get();
-  if (service_manager->IsQuitting())
-    return;
-
-  service_manager->DelayShutdown(180);
-  LOG(INFO) << "Delay shutdown count to 180";
-  message.Reply("Success");
-}
-
-void DbusCloseConnection(const char*, const char*,
-    const InterProcessCommunication::Message& message) {
-  LOG(INFO) << "Closing connection on CloseDBusConnection";
-  message.CloseDBusConnection();
-}
-
-void DbusKillLauncherProcess(const char*, const char* argument,
-    const InterProcessCommunication::Message& message) {
-  WRTServiceManager::Get()->KillLauncherProcess(argument);
-}
-
-#if BUILDFLAG(IS_TIZEN_TV)
-void DbusDecryptFile(const char*, const char* argument,
-    const InterProcessCommunication::Message& message) {
-  if (WRTServiceManager::Get()->IsQuitting())
-    return;
-
-  if (!argument) {
-    message.Reply("");
-    LOG(ERROR) << "There is no argument of DecryptFile";
-    return;
-  }
-  auto service_argv =
-      utils::SplitString(std::string(argument), kInternalIdAndFileDelim);
-  auto& internal_id = service_argv[0];
-  auto& file_path = service_argv[1];
-
-  auto service_id = ApplicationData::GetServiceID(internal_id);
-  auto pkg_path = WRTService::GetAppPkgPath(internal_id);
-
-  auto decrypted_src = EncryptedFileHandler::DecryptFile(
-                           service_id, pkg_path, base::FilePath(file_path));
-  if (!decrypted_src.GetLength()) {
-    message.Reply("");
-    return;
-  }
-  auto decryted_buffer =
-      std::string(decrypted_src.GetBuffer(), decrypted_src.GetLength());
-  message.Reply(decryted_buffer.c_str());
-}
-
-void DbusReleaseAppDrm(const char*, const char* service_id,
-    const InterProcessCommunication::Message& message) {
-  EncryptedFileHandler::ReleaseHandle(service_id);
-}
-#endif
-
-int RegisterDbusHandler() {
-  uv_thread_create(&dbus_thread, [](void* data) {
-    InterProcessCommunication ipc(true);
-    ipc.SetExitOnDisconnect(false);
-
-    // This daemon handler is called by ui app.
-    ipc.AddMessageHandler("StartService", DbusStartService);
-    ipc.AddMessageHandler("StopService", DbusStopService);
-
-    // by wrt-service-launcher before StartService
-    ipc.AddMessageHandler("SetAppcontrolData", DbusSetAppcontrolData);
-
-    // by installer (wgt-backend)
-    ipc.AddMessageHandler("BuiltinService", DbusBuiltinService);
-
-    ipc.AddMessageHandler("DelayShutdown", DbusDelayShutdown);
-    ipc.AddMessageHandler("CloseDBusConnection", DbusCloseConnection);
-    ipc.AddMessageHandler("KillLauncherProcess", DbusKillLauncherProcess);
-
-#if BUILDFLAG(IS_TIZEN_TV)
-    ipc.AddMessageHandler("DecryptFile", DbusDecryptFile);
-    ipc.AddMessageHandler("ReleaseAppDrm", DbusReleaseAppDrm);
-#endif
-
-    ipc.Listen();
-  }, nullptr);
-  return 0;
-}
-
-}  // namespace
-
 // static
 WRTService::ServiceType WRTService::GetServiceModel() {
   return WRTServiceManager::Get()->GetServiceModel();
@@ -289,7 +120,6 @@ int WRTService::Main(int argc, char** argv) {
     service_manager->SetServiceModel(WRTService::GLOBAL);
     if (prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
       LOG(ERROR) << "prctl(PR_SET_PDEATHSIG, SIGKILL) has failed";
-    RegisterDbusHandler();
 #if BUILDFLAG(IS_TIZEN_TV)
   } else if (std::string(argv[0]).find("wrt-node") != std::string::npos) {
     uv_spawn_enable(1);
@@ -315,7 +145,7 @@ int WRTService::Main(int argc, char** argv) {
   electron::NodeMain(sizeof(arguments) / sizeof(char*), arguments);
   if (IsGlobalService()) {
     service_manager->SendMessage("CloseDBusConnection", "");
-    uv_thread_join(&dbus_thread);
+    service_manager->CloseDbusHandler();
     LOG(INFO) << "DbusHandler thread is gone";
   }
   service_manager->Shutdown();
index 1fbd34ecebe33fc161615af1e6dede3d9fa61bfa..905d79c77888c72bf385059e09f7a13471a35371 100644 (file)
 #include "base/strings/string_split.h"
 #include "base/threading/platform_thread.h"
 #include "tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension_manager.h"
+#include "wrt/src/base/string_utils.h"
 #include "wrt/src/browser/api/wrt_api_web_runtime.h"
 #include "wrt/src/service/wrt_service.h"
 
-#if BUILDFLAG(IS_TIZEN_TV) && TIZEN_VERSION_AT_LEAST(7, 0, 0)
+#if BUILDFLAG(IS_TIZEN_TV)
+#if TIZEN_VERSION_AT_LEAST(7, 0, 0)
 #include <boost-api.h>
 #endif
 
+#include "wrt/src/browser/tv/encrypted_file_handler.h"
+#include "wrt/src/common/constants.h"
+#endif
+
 namespace wrt {
 
 namespace {
@@ -55,17 +61,149 @@ void RequestAsBackgroundProcess() {
   boost_request_by_pid(getpid(), (const boost_info_t*)&app_info);
 }
 #endif
-}  // namespace
 
-WRTServiceManager::WRTServiceManager() {
-  uv_mutex_init(&mutex_);
-  service_model_ = WRTService::GLOBAL;
-  ecore_init();
-  CreateUvHandlers();
-  DelayShutdown();
-  uv_init_threadpool();
+void DbusStartService(const char*,
+                      const char* internal_id,
+                      const InterProcessCommunication::Message& message) {
+  auto service_manager = WRTServiceManager::Get();
+  if (service_manager->IsQuitting())
+    return;
+
+  auto service_id = ApplicationData::GetServiceID(internal_id);
+  std::string pkg_path = ApplicationData::GetPackagePath(service_id);
+  service_manager->StartService(internal_id, message.Sender(), pkg_path);
+  message.Reply("Success");
+}
+
+void DbusStopService(const char*,
+                     const char* internal_id,
+                     const InterProcessCommunication::Message& message) {
+  auto service_manager = WRTServiceManager::Get();
+  if (service_manager->IsQuitting())
+    return;
+
+  if (service_manager->DidStartService(internal_id)) {
+    service_manager->StopService(internal_id);
+    message.Reply("Success");
+  } else {
+    LOG(WARNING) << internal_id << " isn't running";
+    message.Reply("Failed");
+  }
+}
+
+void DbusSetAppcontrolData(const char*,
+                           const char* argument,
+                           const InterProcessCommunication::Message& message) {
+  auto service_manager = WRTServiceManager::Get();
+  if (service_manager->IsQuitting())
+    return;
+
+  if (!argument) {
+    LOG(ERROR) << "There is no argument of SetAppcontrolData";
+    return;
+  }
+  auto appcontrol_argv = base::SplitString(
+      std::string(argument), ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
+  if (appcontrol_argv.size() != 2) {
+    LOG(ERROR) << "Argument should be {app_id}:{app_control_data}";
+    return;
+  }
+  service_manager->SetAppcontrolData(appcontrol_argv[0], appcontrol_argv[1]);
+}
+
+void DbusBuiltinService(const char*,
+                        const char* argument,
+                        const InterProcessCommunication::Message& message) {
+  auto service_manager = WRTServiceManager::Get();
+  if (service_manager->IsQuitting())
+    return;
+
+  if (!argument) {
+    LOG(ERROR) << "There is no argument of BuiltinService";
+    return;
+  }
+  auto service_argv = base::SplitString(
+      std::string(argument), ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
+  if (service_argv.size() != 3) {
+    LOG(ERROR) << "Argument should be {service_name}:{app_id}:{app_path}";
+    return;
+  }
+  auto& service_name = service_argv[0];
+  auto& app_id = service_argv[1];
+  auto& app_path = service_argv[2];
+  LOG(INFO) << "Service Name: " << service_name << ", " << app_id << "("
+            << app_path << ")";
+  service_manager->RunBuiltinService(service_name, app_id, app_path);
+}
+
+void DbusDelayShutdown(const char*,
+                       const char* argument,
+                       const InterProcessCommunication::Message& message) {
+  auto service_manager = WRTServiceManager::Get();
+  if (service_manager->IsQuitting())
+    return;
+
+  service_manager->DelayShutdown(180);
+  LOG(INFO) << "Delay shutdown count to 180";
+  message.Reply("Success");
 }
 
+void DbusCloseConnection(const char*,
+                         const char*,
+                         const InterProcessCommunication::Message& message) {
+  LOG(INFO) << "Closing connection on CloseDBusConnection";
+  message.CloseDBusConnection();
+}
+
+void DbusKillLauncherProcess(
+    const char*,
+    const char* argument,
+    const InterProcessCommunication::Message& message) {
+  WRTServiceManager::Get()->KillLauncherProcess(argument);
+}
+
+#if BUILDFLAG(IS_TIZEN_TV)
+void DbusDecryptFile(const char*,
+                     const char* argument,
+                     const InterProcessCommunication::Message& message) {
+  if (WRTServiceManager::Get()->IsQuitting())
+    return;
+
+  if (!argument) {
+    message.Reply("");
+    LOG(ERROR) << "There is no argument of DecryptFile";
+    return;
+  }
+  auto service_argv =
+      utils::SplitString(std::string(argument), kInternalIdAndFileDelim);
+  auto& internal_id = service_argv[0];
+  auto& file_path = service_argv[1];
+
+  auto service_id = ApplicationData::GetServiceID(internal_id);
+  auto pkg_path = WRTService::GetAppPkgPath(internal_id);
+
+  auto decrypted_src = EncryptedFileHandler::DecryptFile(
+      service_id, pkg_path, base::FilePath(file_path));
+  if (!decrypted_src.GetLength()) {
+    message.Reply("");
+    return;
+  }
+  auto decryted_buffer =
+      std::string(decrypted_src.GetBuffer(), decrypted_src.GetLength());
+  message.Reply(decryted_buffer.c_str());
+}
+
+void DbusReleaseAppDrm(const char*,
+                       const char* service_id,
+                       const InterProcessCommunication::Message& message) {
+  EncryptedFileHandler::ReleaseHandle(service_id);
+}
+#endif
+
+}  // namespace
+
+WRTServiceManager::WRTServiceManager() {}
+
 WRTServiceManager::~WRTServiceManager() {}
 
 WRTServiceManager* WRTServiceManager::Get(bool can_create) {
@@ -76,6 +214,42 @@ WRTServiceManager* WRTServiceManager::Get(bool can_create) {
   return manager;
 }
 
+void WRTServiceManager::CloseDbusHandler() {
+  uv_thread_join(&dbus_thread_);
+}
+
+int WRTServiceManager::RegisterDbusHandler() {
+  uv_thread_create(
+      &dbus_thread_,
+      [](void* data) {
+        InterProcessCommunication ipc(true);
+        ipc.SetExitOnDisconnect(false);
+
+        // This daemon handler is called by ui app.
+        ipc.AddMessageHandler("StartService", DbusStartService);
+        ipc.AddMessageHandler("StopService", DbusStopService);
+
+        // by wrt-service-launcher before StartService
+        ipc.AddMessageHandler("SetAppcontrolData", DbusSetAppcontrolData);
+
+        // by installer (wgt-backend)
+        ipc.AddMessageHandler("BuiltinService", DbusBuiltinService);
+
+        ipc.AddMessageHandler("DelayShutdown", DbusDelayShutdown);
+        ipc.AddMessageHandler("CloseDBusConnection", DbusCloseConnection);
+        ipc.AddMessageHandler("KillLauncherProcess", DbusKillLauncherProcess);
+
+#if BUILDFLAG(IS_TIZEN_TV)
+        ipc.AddMessageHandler("DecryptFile", DbusDecryptFile);
+        ipc.AddMessageHandler("ReleaseAppDrm", DbusReleaseAppDrm);
+#endif
+
+        ipc.Listen();
+      },
+      nullptr);
+  return 0;
+}
+
 void WRTServiceManager::Shutdown() {
   LOG(INFO) << "WRTServiceManager is in shutdown!";
   XWalkExtensionManager::GetInstance()->Shutdown();
@@ -185,15 +359,19 @@ void WRTServiceManager::InitAulHandler(int argc, char** argv) {
 }
 
 void WRTServiceManager::InitServiceApp(int argc, char** argv) {
-  uv_mutex_lock(&mutex_);
+  uv_mutex_init(&mutex_);
   ppid_ = getppid();
   if (!CheckParentProcessValid(ppid_))
     ppid_ = -1;
-  uv_mutex_unlock(&mutex_);
-
+  ecore_init();
+  CreateUvHandlers();
+  DelayShutdown();
+  uv_init_threadpool();
   uv_timer_start(&timeout_handle_, OnTimeout, kEventTimeout, kEventTimeout);
 
   InitAulHandler(argc, argv);
+  if (service_model_ == WRTService::GLOBAL)
+    RegisterDbusHandler();
 }
 
 WRTService::ServiceType WRTServiceManager::GetServiceModel() {
index 61c8f3492b1b658955cf7dd3d64fd772037e58dc..e915751f0f3fbf2103b3ea9366ef7b6bf6fdd517 100644 (file)
@@ -23,6 +23,7 @@ class WRTServiceManager {
   static WRTServiceManager* Get(bool can_create = true);
 
   void CheckTimeout();
+  void CloseDbusHandler();
   void DelayShutdown(int exit_count = 120);
   void FinalizeService(const std::string& internal_id);
   void InitServiceApp(int argc, char** argv);
@@ -74,6 +75,7 @@ class WRTServiceManager {
   void CreateUvHandlers();
   bool CheckParentProcessValid(int pid);
   bool HandleMessageQueue();
+  int RegisterDbusHandler();
 
   uv_async_t handle_adder_;
   uv_async_t handle_remover_;
@@ -95,8 +97,9 @@ class WRTServiceManager {
   std::string internal_id_;
   std::string removed_internal_id;
 
+  uv_thread_t dbus_thread_;
   uv_timer_t timeout_handle_ = {};
-  WRTService::ServiceType service_model_;
+  WRTService::ServiceType service_model_ = WRTService::NONE;
   unsigned int exit_count_;
   bool quit_requested_ = false;
   int ppid_ = 0;