[Common][Package] Avoid recalling app_info_create() for consecutive calls 75/280875/3
authorPiotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Tue, 6 Sep 2022 07:13:56 +0000 (09:13 +0200)
committerPiotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Tue, 6 Sep 2022 12:56:15 +0000 (14:56 +0200)
app_info_create occasionally when the device is freshly reboot takes
much time to gather data (over 1 second), which causes a serious delay
when the function is called several times in the row without caching the
result.
This modification makes a single call of app_info_create() at first try
and then only return previously fetched value.

[Verification] Code compiles without errors.
TCT application, package, deprecated - passrate didn't change.

Change-Id: Ifc989d5eb0f7435f1a6451613d289d6d3851e979

src/common/tools.cc
src/package/package_info_provider.cc
src/package/package_info_provider.h

index b39b2cd..2e4d02a 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "common/logger.h"
 #include "common/scope_exit.h"
+#include "common/current_application.h"
 
 namespace common {
 namespace tools {
@@ -421,22 +422,10 @@ PlatformResult GetPkgApiVersion(std::string* api_version) {
   static std::string cached_api_version;
   static int cached_pid = -1;
 
-  char* app_id = nullptr;
-  char* pkgid = nullptr;
   char* api_ver = nullptr;
-  app_info_h app_handle = nullptr;
   pkgmgrinfo_pkginfo_h pkginfo_handle = nullptr;
 
   SCOPE_EXIT {
-    if (app_id) {
-      free(app_id);
-    }
-    if (pkgid) {
-      free(pkgid);
-    }
-    if (app_handle) {
-      app_info_destroy(app_handle);
-    }
     if (pkginfo_handle) {
       pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_handle);
     }
@@ -446,22 +435,12 @@ PlatformResult GetPkgApiVersion(std::string* api_version) {
   if (cached_pid == pid) {
     *api_version = cached_api_version;  // Retrieve from local cache
   } else {
-    int ret = app_manager_get_app_id(pid, &app_id);
-    if (ret != APP_MANAGER_ERROR_NONE) {
-      return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get app id");
-    }
-
-    ret = app_info_create(app_id, &app_handle);
-    if (ret != APP_MANAGER_ERROR_NONE) {
-      return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get app info");
-    }
-
-    ret = app_info_get_package(app_handle, &pkgid);
-    if ((ret != APP_MANAGER_ERROR_NONE) || (pkgid == nullptr)) {
+    const char *pkgid =common::CurrentApplication::GetInstance().GetPackageId().c_str();
+    if (pkgid == nullptr) {
       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get pkg id");
     }
 
-    ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, getuid(), &pkginfo_handle);
+    int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, getuid(), &pkginfo_handle);
     if (ret != PMINFO_R_OK) {
       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get pkginfo_h");
     }
index 40f1a6b..5844fac 100644 (file)
@@ -29,6 +29,7 @@
 #include "common/logger.h"
 #include "common/scope_exit.h"
 #include "common/tools.h"
+#include "common/current_application.h"
 
 namespace extension {
 namespace package {
@@ -88,10 +89,9 @@ void PackageInfoProvider::GetPackagesInfo(picojson::object& out) {
 void PackageInfoProvider::GetPackageInfo(picojson::object& out) {
   ScopeLogger();
 
-  char* package_id = NULL;
+  std::string package_id;
   if (GetCurrentPackageId(&package_id)) {
-    GetPackageInfo(package_id, out);
-    free(package_id);
+    GetPackageInfo(package_id.c_str(), out);
   } else {
     LoggerE("Failed to get current package ID");
     REPORT_ERROR(out, NotFoundException("The package with the specified ID is not found"));
@@ -258,31 +258,12 @@ void PackageInfoProvider::GetDataSize(const std::string& id, picojson::object* o
   GetSize(id, PM_GET_DATA_SIZE, out);
 }
 
-bool PackageInfoProvider::GetCurrentPackageId(char** package_id) {
+bool PackageInfoProvider::GetCurrentPackageId(std::string* package_id) {
   ScopeLogger();
 
-  int ret = 0;
-  char* app_id = NULL;
-
-  int pid = getpid();
-  ret = app_manager_get_app_id(pid, &app_id);
-  if (ret != APP_MANAGER_ERROR_NONE) {
-    LoggerE("Failed to get app id: %d (%s)", ret, get_error_message(ret));
-    return false;
-  }
-
-  app_info_h handle;
-  ret = app_info_create(app_id, &handle);
-  free(app_id);
-  if (ret != APP_MANAGER_ERROR_NONE) {
-    LoggerE("Fail to get app info: %d (%s)", ret, get_error_message(ret));
-    return false;
-  }
-
-  ret = app_info_get_package(handle, package_id);
-  app_info_destroy(handle);
-  if ((ret != APP_MANAGER_ERROR_NONE) || (*package_id == NULL)) {
-    LoggerE("Fail to get pkg id: %d (%s)", ret, get_error_message(ret));
+  *package_id = common::CurrentApplication::GetInstance().GetPackageId();
+  if (package_id->empty()) {
+    LoggerE("Fail to get pkg id. Result is empty");
     return false;
   }
 
index 894b2b8..33009b6 100644 (file)
@@ -54,7 +54,7 @@ class PackageInfoProvider {
   static void GetDataSize(const std::string& id, picojson::object* out);
 
  private:
-  static bool GetCurrentPackageId(char** package_id);
+  static bool GetCurrentPackageId(std::string* package_id);
 };
 
 }  // namespace package