Use TLS variable for getting status 46/301446/1
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 16 Nov 2023 03:50:42 +0000 (12:50 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 16 Nov 2023 03:50:42 +0000 (12:50 +0900)
If the aul_app_info_iter_fn callback function is called while calling
getting status API, aul library tries to get the status from the current
aul_app_info.

Change-Id: I26b60a9b81d9def74abe66234c9de726983344ce
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/pkginfo.cc
src/pkginfo_internal.h
src/status.cc

index b0503e7..2414dfd 100644 (file)
@@ -247,6 +247,7 @@ int SetAppInfo(aul_app_info* info, const tizen_base::Bundle& b) {
 }
 
 thread_local bool calling_appinfo_cb = false;
+thread_local aul_app_info* current_info = nullptr;
 int GetRunningAppInfoWithCb(int cmd, uid_t uid, aul_app_info_iter_fn cb,
     void* user_data) {
   if (cb == nullptr)
@@ -263,7 +264,9 @@ int GetRunningAppInfoWithCb(int cmd, uid_t uid, aul_app_info_iter_fn cb,
     if (SetAppInfo(&info, b) != 0)
       break;
 
+    current_info = &info;
     cb(&info, user_data);
+    current_info = nullptr;
   }
   calling_appinfo_cb = false;
 
@@ -296,6 +299,36 @@ extern "C" bool aul_is_calling_appinfo_cb() {
   return calling_appinfo_cb;
 }
 
+extern "C" int aul_app_get_status_from_current_appinfo(const char* appid,
+    int* status) {
+  if (appid == nullptr || status == nullptr)
+    return AUL_R_EINVAL;
+
+  if (current_info == nullptr)
+    return AUL_R_ERROR;
+
+  if (strcmp(appid, current_info->appid) != 0)
+    return AUL_R_ERROR;
+
+  *status = current_info->status;
+  return AUL_R_OK;
+}
+
+extern "C" int aul_app_get_status_bypid_from_current_appinfo(int pid,
+    int* status) {
+  if (pid < 1 || status == nullptr)
+    return AUL_R_EINVAL;
+
+  if (current_info == nullptr)
+    return AUL_R_ERROR;
+
+  if (current_info->pid != pid)
+    return AUL_R_ERROR;
+
+  *status = current_info->status;
+  return AUL_R_OK;
+}
+
 extern "C" API int aul_app_get_pid(const char* appid) {
   return aul_app_get_pid_for_uid(appid, getuid());
 }
index c47901a..0c7f598 100644 (file)
@@ -24,6 +24,10 @@ extern "C" {
 
 bool aul_is_calling_appinfo_cb();
 
+int aul_app_get_status_from_current_appinfo(const char *appid, int *status);
+
+int aul_app_get_status_bypid_from_current_appinfo(int pid, int *status);
+
 #ifdef __cplusplus
 }
 #endif
index 27d1b03..98ee7bf 100644 (file)
@@ -209,9 +209,10 @@ extern "C" API int aul_app_get_status_bypid_for_uid(int pid, uid_t uid) {
     return context.GetStatus();
 
   if (aul_is_calling_appinfo_cb()) {
-    _E("=====================================================================");
-    _E("=> Do not use this function within the aul_app_info_iter_fn callback.");
-    _E("=====================================================================");
+    int status = -1;
+    aul_app_get_status_bypid_from_current_appinfo(pid, &status);
+    if (status != -1)
+      return status;
   }
 
   return AppRequest(APP_GET_STATUS, uid)
@@ -230,11 +231,10 @@ extern "C" API int aul_app_get_status_for_uid(const char* appid, uid_t uid) {
   }
 
   if (aul_is_calling_appinfo_cb()) {
-    _E("=====================================================================");
-    _E("=> Do not use this function within the aul_app_info_iter_fn callback.");
-    _E("=> The status info is already included in the aul_app_info structure.");
-    _E("=> If you want focused pid information, use the aul_window_get_focused_pid() function.");
-    _E("=====================================================================");
+    int status = -1;
+    aul_app_get_status_from_current_appinfo(appid, &status);
+    if (status != -1 && status != STATUS_VISIBLE)
+      return status;
   }
 
   return AppRequest(APP_GET_STATUS_BY_APPID, uid)