Revert "Remove suspend event handler" 34/306134/1
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 16 Feb 2024 08:21:53 +0000 (17:21 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 16 Feb 2024 08:21:56 +0000 (17:21 +0900)
This reverts commit ba23ef0c127fc808c7ba6c5ad2d6b2b548009e4d.

Change-Id: I3733a22d5332ba757f98bb81f3e91c28856490dd

tizen-cpp/app-core-cpp/app_core_base.cc
tizen-cpp/app-core-cpp/suspend_event_private.cc [new file with mode: 0644]
tizen-cpp/app-core-cpp/suspend_event_private.hh [new file with mode: 0644]

index be5275c05d3621095294034ae841c6e642504f37..d7a854f8aa21b814d1c30c34b8a3abbbb8638a4b 100644 (file)
@@ -53,6 +53,7 @@
 #include "app-core-cpp/app_core_plugin_private.hh"
 #include "app-core-cpp/exit_handler_private.hh"
 #include "app-core-cpp/sigterm_handler_private.hh"
+#include "app-core-cpp/suspend_event_private.hh"
 #include "common/glib_private.hh"
 #include "common/log_private.hh"
 #include "common/log_tracer.hh"
@@ -83,7 +84,7 @@ class AppCoreBase::EventBase::Impl {
   int val_ = -1;
 };
 
-class AppCoreBase::Impl {
+class AppCoreBase::Impl : public SuspendEvent::IEventListener {
  public:
   explicit Impl(AppCoreBase* parent) : parent_(parent) {}
 
@@ -100,7 +101,7 @@ class AppCoreBase::Impl {
   void AppendLangs(const std::string& lang, std::vector<std::string>* lang_set,
       std::map<std::string, std::set<std::string>>* table);
   void ChangeLang();
-  void HandleThawEvent();
+  void OnFreezerSignal();
 
   template <class T>
   void InvokeCallback(T event, IEvent::Type type) {
@@ -117,6 +118,8 @@ class AppCoreBase::Impl {
     }
   }
 
+  void InitSuspend();
+  void OnSuspend(pid_t pid, int status) override;
   static gboolean InvokeLangChangeCb(gpointer data);
   static void OnLowBatteryCb(keynode_t* key, void* data);
   static void OnTimeZoneChangedCb(keynode_t* key, void* data);
@@ -147,6 +150,7 @@ class AppCoreBase::Impl {
   IMainLoop* loop_delegator_ = nullptr;
   guint signal_handler_source_ = 0;
   std::unique_ptr<AppCorePlugin> plugin_;
+  std::unique_ptr<SuspendEvent> suspend_event_;
 };
 
 AppCoreBase::EventBase::EventBase(Type type)
@@ -215,7 +219,7 @@ void AppCoreBase::RaiseEvent(const std::string& event, IEvent::Type type) {
   impl_->InvokeCallback(event, type);
 }
 
-void AppCoreBase::Impl::HandleThawEvent() {
+void AppCoreBase::Impl::OnFreezerSignal() {
   if (!allowed_bg_ && suspended_state_) {
     parent_->RemoveSuspendTimer();
     InvokeCallback(SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND,
@@ -225,6 +229,10 @@ void AppCoreBase::Impl::HandleThawEvent() {
   }
 }
 
+void AppCoreBase::Impl::InitSuspend() {
+  suspend_event_.reset(new SuspendEvent(this));
+}
+
 gboolean AppCoreBase::Impl::InvokeLangChangeCb(gpointer data) {
   AppCoreBase* base = reinterpret_cast<AppCoreBase*>(data);
   base->impl_->sid_ = 0;
@@ -232,6 +240,10 @@ gboolean AppCoreBase::Impl::InvokeLangChangeCb(gpointer data) {
   return G_SOURCE_REMOVE;
 }
 
+void AppCoreBase::Impl::OnSuspend(pid_t pid, int status) {
+  if (pid == getpid() && status == 0) OnFreezerSignal();
+}
+
 void AppCoreBase::Impl::LanguageChangeCb(keynode_t* key, void* user_data) {
   AppCoreBase* base = reinterpret_cast<AppCoreBase*>(user_data);
   if (base->impl_->sid_) {
@@ -371,10 +383,6 @@ int AppCoreBase::OnReceive(aul_type type, tizen_base::Bundle b) {
       FlushMemory();
     }
     break;
-  case AUL_THAW:
-    _D("[APP %d]     AUL event: AUL_THAW", getpid());
-    impl_->HandleThawEvent();
-    break;
   case AUL_UPDATE_REQUESTED:
     _D("[APP %d]     AUL event: AUL_UPDATE_REQUESTED", getpid());
     impl_->InvokeCallback(0, IEvent::Type::UPDATE_REQUESTED);
@@ -933,6 +941,7 @@ void AppCoreBase::Init(int argc, char** argv) {
   impl_->PluginInit(argc, argv);
   traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
 
+  impl_->InitSuspend();
   traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:SET_SYSTEM_EVENT");
   if (!impl_->dirty_) {
     impl_->dirty_ = true;
diff --git a/tizen-cpp/app-core-cpp/suspend_event_private.cc b/tizen-cpp/app-core-cpp/suspend_event_private.cc
new file mode 100644 (file)
index 0000000..c2378a8
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2024 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/suspend_event_private.hh"
+
+#include "common/log_private.hh"
+
+namespace {
+
+constexpr const char RESOURCED_FREEZER_PATH[] =
+    "/Org/Tizen/ResourceD/Freezer";
+constexpr const char RESOURCED_FREEZER_INTERFACE[] =
+    "org.tizen.resourced.freezer";
+constexpr const char RESOURCED_FREEZER_SIGNAL[] =
+    "FreezerState";
+
+}  // namespace
+
+namespace tizen_cpp {
+
+SuspendEvent::SuspendEvent(IEventListener* listener) : listener_(listener) {
+  cancellable_ = g_cancellable_new();
+  if (cancellable_ == nullptr) {
+    _E("g_cancellable_new() is failed");
+    return;
+  }
+
+  g_bus_get(G_BUS_TYPE_SYSTEM, cancellable_, GAsyncReadyCb, this);
+}
+
+SuspendEvent::~SuspendEvent() {
+  if (source_ != 0)
+    g_dbus_connection_signal_unsubscribe(conn_, source_);
+
+  if (conn_ != nullptr)
+    g_object_unref(conn_);
+
+  if (cancellable_ != nullptr) {
+    g_cancellable_cancel(cancellable_);
+    g_object_unref(cancellable_);
+  }
+}
+
+void SuspendEvent::GAsyncReadyCb(GObject* source_object, GAsyncResult* res,
+                                 gpointer user_data) {
+  auto* event = static_cast<SuspendEvent*>(user_data);
+  GError* error = nullptr;
+  event->conn_ = g_bus_get_finish(res, &error);
+  if (event->conn_ == nullptr) {
+    _E("g_bus_get_finish() is failed. error(%s)", error ? error->message : "");
+    g_clear_error(&error);
+    return;
+  }
+
+  event->source_ = g_dbus_connection_signal_subscribe(
+      event->conn_, nullptr, RESOURCED_FREEZER_INTERFACE,
+      RESOURCED_FREEZER_SIGNAL, RESOURCED_FREEZER_PATH, nullptr,
+      G_DBUS_SIGNAL_FLAGS_NONE, GDBusSignalCb, user_data, nullptr);
+  if (event->source_ == 0) {
+    _E("g_dbus_connection_signal_subscribe() is failed");
+    return;
+  }
+
+  _D("Suspend signal subscribed");
+}
+
+void SuspendEvent::GDBusSignalCb(GDBusConnection* conn,
+                                 const gchar* sender_name,
+                                 const gchar* object_path,
+                                 const gchar* interface_name,
+                                 const gchar* signal_name, GVariant* parameters,
+                                 gpointer user_data) {
+  if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) != 0)
+    return;
+
+  gint pid = -1;
+  gint status = 0;
+  g_variant_get(parameters, "(ii)", &status, &pid);
+  auto* event = static_cast<SuspendEvent*>(user_data);
+  event->listener_->OnSuspend(pid, status);
+}
+
+}  // namespace tizen_cpp
diff --git a/tizen-cpp/app-core-cpp/suspend_event_private.hh b/tizen-cpp/app-core-cpp/suspend_event_private.hh
new file mode 100644 (file)
index 0000000..46cbe3e
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2024 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_SUSPEND_EVENT_PRIVATE_HH_
+#define TIZEN_CPP_APP_CORE_CPP_SUSPEND_EVENT_PRIVATE_HH_
+
+#include <gio/gio.h>
+#include <glib.h>
+#include <sys/types.h>
+
+namespace tizen_cpp {
+
+class SuspendEvent {
+ public:
+  class IEventListener {
+   public:
+    virtual ~IEventListener() = default;
+    virtual void OnSuspend(pid_t pid, int status) = 0;
+  };
+
+  explicit SuspendEvent(IEventListener* listener);
+  ~SuspendEvent();
+
+ private:
+  static void GAsyncReadyCb(GObject* source_object, GAsyncResult* res,
+                            gpointer user_data);
+  static void GDBusSignalCb(GDBusConnection* conn, const gchar* sender_name,
+                            const gchar* object_path,
+                            const gchar* interface_name,
+                            const gchar* signal_name, GVariant* parameters,
+                            gpointer user_data);
+
+ private:
+  IEventListener* listener_;
+  GDBusConnection* conn_ = nullptr;
+  GCancellable* cancellable_ = nullptr;
+  guint source_ = 0;
+};
+
+}  // namespace tizen_cpp
+
+#endif  // TIZEN_CPP_APP_CORE_CPP_SUSPEND_EVENT_PRIVATE_HH_