Fix app event functions 56/238356/2
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 13 Jul 2020 05:00:35 +0000 (14:00 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 13 Jul 2020 05:06:44 +0000 (14:06 +0900)
To listen all applciation status events, the aul_app_event_create() is
modified. The appid parameter of the aul_app_event_create() is removed.
And, the aul_app_event_create_with_appid() is added.

Modifies:
 - aul_app_event_create()

Adds:
 - aul_app_event_create_with_appid();

Requires:
 - https://review.tizen.org/gerrit/#/c/platform/core/appfw/aul-1/+/238356/

Change-Id: I7d9b9b669d0c718ccf46669bccaf0bbe9b1f8ab0
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
aul/api/aul_app_event.cc
aul/api/aul_app_event.h
aul/app_manager/app_event.cc
aul/app_manager/app_event.hh
aul/common/common.hh [new file with mode: 0644]

index 77fce7c..d8d5e08 100644 (file)
@@ -36,6 +36,15 @@ class AppEventStub : public AppEvent,
       user_data_(user_data) {
   }
 
+  AppEventStub(aul_app_event_launched_cb launched_cb,
+      aul_app_event_terminated_cb terminated_cb,
+      void* user_data)
+    : AppEvent(this),
+      launched_cb_(launched_cb),
+      terminated_cb_(terminated_cb),
+      user_data_(user_data) {
+  }
+
  void OnAppLaunched(const AppContext* context) override {
    AppContext* ctx = const_cast<AppContext*>(context);
    launched_cb_(static_cast<aul_app_context_h>(ctx), user_data_);
@@ -54,11 +63,11 @@ class AppEventStub : public AppEvent,
 
 }  // namespace
 
-extern "C" API int aul_app_event_create(const char *app_id,
+extern "C" API int aul_app_event_create_with_appid(const char* app_id,
     aul_app_event_launched_cb launched_cb,
     aul_app_event_terminated_cb terminated_cb,
-    void *user_data,
-    aul_app_event_h *app_event) {
+    voiduser_data,
+    aul_app_event_happ_event) {
   if (!app_id || !launched_cb || !terminated_cb || !app_event) {
     _E("Invalid parameter");
     return AUL_R_EINVAL;
@@ -76,6 +85,27 @@ extern "C" API int aul_app_event_create(const char *app_id,
   return AUL_R_OK;
 }
 
+extern "C" API int aul_app_event_create(aul_app_event_launched_cb launched_cb,
+    aul_app_event_terminated_cb terminated_cb,
+    void* user_data,
+    aul_app_event_h* app_event) {
+  if (!launched_cb || !terminated_cb || !app_event) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  AppEventStub* handle = nullptr;
+  try {
+    handle = new AppEventStub(launched_cb, terminated_cb, user_data);
+  } catch (Exception& e) {
+    return e.GetErrorCode();
+  }
+
+  *app_event = static_cast<aul_app_event_h>(handle);
+
+  return AUL_R_OK;
+}
+
 extern "C" API int aul_app_event_destroy(aul_app_event_h app_event) {
   if (!app_event) {
     _E("Invalid parameter");
index 8f71383..5bc0404 100644 (file)
@@ -32,12 +32,17 @@ typedef void (*aul_app_event_launched_cb)(aul_app_context_h app_context,
 typedef void (*aul_app_event_terminated_cb)(aul_app_context_h app_context,
                void *user_data);
 
-int aul_app_event_create(const char *app_id,
+int aul_app_event_create_with_appid(const char *app_id,
                aul_app_event_launched_cb launched_cb,
                aul_app_event_terminated_cb terminated_cb,
                void *user_data,
                aul_app_event_h *app_event);
 
+int aul_app_event_create(aul_app_event_launched_cb launched_cb,
+               aul_app_event_terminated_cb terminated_cb,
+               void *user_data,
+               aul_app_event_h *app_event);
+
 int aul_app_event_destroy(aul_app_event_h app_event);
 
 #ifdef __cplusplus
index b250283..7738606 100644 (file)
  * limitations under the License.
  */
 
+#include <unistd.h>
+#include <sys/types.h>
+
+#include <bundle_cpp.h>
+
 #include "aul/app_manager/app_event.hh"
+#include "aul/common/common.hh"
 #include "aul/common/exception.hh"
 #include "aul/common/log_private.hh"
 
@@ -22,33 +28,54 @@ namespace aul {
 
 AppEvent::AppEvent(std::string app_id, AppEvent::IEvent* ev)
   : app_id_(std::move(app_id)), ev_(ev) {
-  int ret = aul_listen_app_status(app_id_.c_str(), OnAppStatusCb,
+  std::string endpoint = "app_status_event:" + app_id;
+  if (getuid() >= REGULAR_UID_MIN)
+    endpoint += ":" + std::to_string(getuid());
+
+  int ret = aul_app_com_create(endpoint.c_str(), nullptr, OnAppStatusCb,
+      this, &handle_);
+  if (ret != AUL_R_OK) {
+    _E("aul_app_com_create(%s) is failed. error(%d)", endpoint.c_str(), ret);
+    THROW(ret);
+  }
+}
+
+AppEvent::AppEvent(AppEvent::IEvent* ev) : ev_(ev) {
+  std::string endpoint = "app_status_event";
+  if (getuid() >= REGULAR_UID_MIN)
+    endpoint += ":" + std::to_string(getuid());
+
+  int ret = aul_app_com_create(endpoint.c_str(), nullptr, OnAppStatusCb,
       this, &handle_);
   if (ret != AUL_R_OK) {
-    _E("Failed to listen app status event. error(%d)", ret);
+    _E("aul_app_com_create(%s) is failed. error(%d)", endpoint.c_str(), ret);
     THROW(ret);
   }
 }
 
 AppEvent::~AppEvent() {
   if (handle_)
-    aul_ignore_app_status(handle_);
+    aul_app_com_leave(handle_);
 }
 
-int AppEvent::OnAppStatusCb(aul_app_info* info, int ctx_status, void* data) {
+int AppEvent::OnAppStatusCb(const char* endpoint, aul_app_com_result_e res,
+    bundle* envelope, void* user_data) {
+  tizen_base::Bundle b(envelope);
   AppContext::Builder builder;
-  builder.SetAppId(info->appid);
-  builder.SetPkgId(info->pkgid);
-  builder.SetInstId(info->instance_id);
-  builder.SetExec(info->app_path);
-  builder.SetPid(info->pid);
-  builder.SetStatus(info->status);
-  builder.SetIsSubApp(static_cast<bool>(info->is_sub_app));
+  builder.SetAppId(b.GetString(AUL_K_APPID));
+  builder.SetPkgId(b.GetString(AUL_K_PKGID));
+  builder.SetInstId(b.GetString(AUL_K_INSTANCE_ID));
+  builder.SetExec(b.GetString(AUL_K_EXEC));
+  builder.SetPid(std::stoi(b.GetString(AUL_K_PID)));
+  builder.SetStatus(std::stoi(b.GetString(AUL_K_STATUS)));
+  builder.SetIsSubApp(static_cast<bool>(
+        std::stoi(b.GetString(AUL_K_IS_SUBAPP))));
   AppContext app_context = builder.Build();
 
-  auto* handle = static_cast<AppEvent*>(data);
+  auto* handle = static_cast<AppEvent*>(user_data);
   IEvent* ev = handle->ev_;
-  if (ctx_status == STATUS_TERMINATE)
+  int context_status = std::stoi(b.GetString("__CONTEXT_STATUS__"));
+  if (context_status == STATUS_TERMINATE)
     ev->OnAppTerminated(&app_context);
   else
     ev->OnAppLaunched(&app_context);
index cfc19ca..4cd6b10 100644 (file)
  * limitations under the License.
  */
 
-#ifndef AUL_APP_MANAGER__APP_EVENT_HH_
+#ifndef AUL_APP_MANAGER_APP_EVENT_HH_
 #define AUL_APP_MANAGER_APP_EVENT_HH_
 
 #include <string>
 
 #include "aul/app_manager/app_context.hh"
 #include "include/aul.h"
+#include "include/aul_app_com.h"
 
 namespace aul {
 
@@ -33,15 +34,17 @@ class AppEvent {
   };
 
   AppEvent(std::string app_id, IEvent* ev);
+  AppEvent(IEvent* ev);
   virtual ~AppEvent();
 
  private:
-  static int OnAppStatusCb(aul_app_info* info, int ctx_status, void* data);
+  static int OnAppStatusCb(const char* endpoint, aul_app_com_result_e res,
+      bundle* envelope, void* user_data);
 
  private:
   std::string app_id_;
   IEvent* ev_;
-  status_listen_h handle_ = nullptr;
+  aul_app_com_connection_h handle_ = nullptr;
 };
 
 }  // namespace aul
diff --git a/aul/common/common.hh b/aul/common/common.hh
new file mode 100644 (file)
index 0000000..73f587d
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 AUL_COMMON_COMMON_HH_
+#define AUL_COMMON_COMMON_HH_
+
+#undef REGULAR_UID_MIN
+#define REGULAR_UID_MIN 5000
+
+#endif  // AUL_COMMON_COMMON_HH_