Refactor method to get resource package information 58/304358/16
authorIlho Kim <ilho159.kim@samsung.com>
Tue, 16 Jan 2024 08:52:14 +0000 (17:52 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 23 Jan 2024 00:04:41 +0000 (09:04 +0900)
Change-Id: I4ff2ca8e55b220b2e6b567e36836323d216518cf
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/lib/amd_res_info.cc
src/lib/amd_res_info.h
src/lib/launch/step_prepare_starting_app.cc
src/lib/res_info/res_app_info.cc
src/lib/res_info/res_app_info.hh
src/lib/res_info/res_mount_package_info.cc [new file with mode: 0644]
src/lib/res_info/res_mount_package_info.hh [new file with mode: 0644]

index 0d6e06d..5439eae 100644 (file)
@@ -332,82 +332,3 @@ int _resinfo_init(void) {
 
   return 0;
 }
-
-static int ResInfoGetGadgetResPackagesInfo(uid_t uid, GList** pkgids,
-  GList** allowed_paths, GList** global_paths) {
-  std::shared_ptr<amd::ResInfo> resinfo = amd::ResInfoManager::GetInst()
-      .GetUserResInfo(uid);
-  if (!resinfo)
-    return -1;
-
-  pkgmgrinfo_version_compare_type ver_comp;
-  std::unordered_map<std::string, std::shared_ptr<amd::ResPkgInfo>> gadget_map;
-  for (const auto& [_, res_pkginfo] : resinfo->GetGadgetResPkgs()) {
-    auto found = gadget_map.find(res_pkginfo->GetResType());
-    if (found != gadget_map.end()) {
-      if (pkgmgrinfo_compare_package_version(
-          found->second->GetResVersion().c_str(),
-          res_pkginfo->GetResVersion().c_str(),
-          &ver_comp) != PMINFO_R_OK)
-          continue;
-
-      if (ver_comp != PMINFO_VERSION_NEW)
-        continue;
-
-      gadget_map.erase(found);
-    }
-
-    gadget_map[res_pkginfo->GetResType()] = res_pkginfo;
-  }
-
-  for (const auto& [_, res_pkginfo] : gadget_map) {
-    *pkgids = g_list_append(*pkgids, strdup(res_pkginfo->GetPkgId().c_str()));
-    std::string allowed_path = res_pkginfo->GetRootPath() + "/res/allowed/";
-    *allowed_paths = g_list_append(*allowed_paths,
-        strdup(allowed_path.c_str()));
-    std::string global_path = res_pkginfo->GetRootPath() + "/res/global/";
-    *global_paths = g_list_append(*global_paths, strdup(global_path.c_str()));
-  }
-
-  return 0;
-}
-
-int _resinfo_get_res_packages_info(const char* appid, uid_t uid,
-    GList** pkgids, GList** allowed_paths, GList** global_paths) {
-  if (!strcmp(appid, "org.tizen.NUIGadgetViewer")) {
-    return ResInfoGetGadgetResPackagesInfo(uid, pkgids, allowed_paths,
-        global_paths);
-  }
-
-  std::shared_ptr<amd::ResInfo> resinfo = amd::ResInfoManager::GetInst()
-      .GetUserResInfo(uid);
-  if (!resinfo)
-    return -1;
-
-  std::shared_ptr<amd::ResAppInfo> res_appinfo = resinfo->GetResAppInfo(appid);
-  if (!res_appinfo)
-    return 0;
-
-  res_appinfo->ClearResControlMountInfo();
-  res_appinfo->UpdateResControlMountInfo(resinfo->GetResPkgs());
-
-  std::set<std::string> pkgid_s;
-  std::vector<std::string> allowed_p;
-  std::vector<std::string> global_p;
-  res_appinfo->GetMountPackagesInfo(&pkgid_s, &allowed_p, &global_p);
-
-  for (const auto& iter : pkgid_s)
-    *pkgids = g_list_append(*pkgids, strdup(iter.c_str()));
-
-  for (const std::string& root_path : allowed_p) {
-    std::string p = root_path + "/res/allowed/";
-    *allowed_paths = g_list_append(*allowed_paths, strdup(p.c_str()));
-  }
-
-  for (const std::string& root_path : global_p) {
-    std::string p = root_path + "/res/global/";
-    *global_paths = g_list_append(*global_paths, strdup(p.c_str()));
-  }
-
-  return 0;
-}
index 6a2191b..30adea0 100644 (file)
@@ -25,9 +25,6 @@ extern "C" {
 
 int _resinfo_init(void);
 
-int _resinfo_get_res_packages_info(const char *appid, uid_t uid,
-               GList **pkgids, GList **allowed_paths, GList **global_paths);
-
 #ifdef __cplusplus
 }
 #endif
index f9c9e5c..50a2926 100644 (file)
@@ -42,6 +42,8 @@
 #include "lib/app_info/app_info_manager.hh"
 #include "lib/app_status/app_status_manager.hh"
 #include "lib/common/log_private.hh"
+#include "lib/res_info/res_info_manager.hh"
+#include "lib/res_info/res_mount_package_info.hh"
 
 namespace amd {
 namespace {
@@ -56,18 +58,19 @@ int GetStatusFromLaunchContext(LaunchContext* context) {
   return context->GetAppStatus()->GetStatus();
 }
 
-void AddDataToBundle(GList* list, bundle* b, const char* key) {
+template <typename T>
+void AddDataToBundle(const T& list, bundle* b, const char* key) {
   bundle_del(b, key);
-  guint length = g_list_length(list);
+  int length = list.size();
   if (length == 0)
     return;
 
   std::vector<const char*> paths(length);
   int index = 0;
-  GList* iter = list;
-  while (iter != nullptr) {
-    paths[index++] = static_cast<char*>(iter->data);
-    iter = g_list_next(iter);
+  auto iter = list.begin();
+  while (iter != list.end()) {
+    paths[index++] = static_cast<const char*>(iter->c_str());
+    iter++;
   }
 
   bundle_add_str_array(b, key, paths.data(), length);
@@ -631,24 +634,23 @@ int StepPrepareStartingApp::CheckAndSetCallerInfo(LaunchContext* context) {
 }
 
 int StepPrepareStartingApp::SetResPackagePaths(LaunchContext* context) {
-  GList* res_pkgids = nullptr;
-  GList* allowed_paths = nullptr;
-  GList* global_paths = nullptr;
-  if (_resinfo_get_res_packages_info(context->GetAppId().c_str(),
-        context->GetTargetUid(), &res_pkgids, &allowed_paths,
-        &global_paths) < 0) {
+  std::optional<ResMountPackageInfo> info;
+  if (ResMountPackageInfo::GetMountPackageInfo(
+      context->GetAppId(), context->GetTargetUid(), &info) < 0) {
     _E("Failed to get res package paths");
     return -1;
   }
 
-  AddDataToBundle(res_pkgids, context->GetBundle(), AUL_K_MOUNT_RES_PKGIDS);
-  AddDataToBundle(allowed_paths, context->GetBundle(),
+  if (!info)
+    return 0;
+
+  AddDataToBundle(info->GetPkgIds(), context->GetBundle(),
+      AUL_K_MOUNT_RES_PKGIDS);
+  AddDataToBundle(info->GetAllowedPaths(), context->GetBundle(),
       AUL_K_MOUNT_ALLOWED_RES_DIR);
-  AddDataToBundle(global_paths, context->GetBundle(),
+  AddDataToBundle(info->GetGlobalPaths(), context->GetBundle(),
       AUL_K_MOUNT_GLOBAL_RES_DIR);
-  g_list_free_full(res_pkgids, free);
-  g_list_free_full(allowed_paths, free);
-  g_list_free_full(global_paths, free);
+
   return 0;
 }
 
index d07e6e0..8323c67 100644 (file)
@@ -104,11 +104,11 @@ std::shared_ptr<ResAppInfo> ResAppInfo::CreateResAppInfo(
       std::move(res_control_list), std::move(privilege_set)));
 }
 
-const std::string& ResAppInfo::GetAppId() {
+const std::string& ResAppInfo::GetAppId() const {
   return appid_;
 }
 
-const std::vector<ResControl>& ResAppInfo::GetResControlList() {
+const std::vector<ResControl>& ResAppInfo::GetResControlList() const {
   return res_control_list_;
 }
 
@@ -190,19 +190,4 @@ void ResAppInfo::UpdateResControlMountInfo(
   }
 }
 
-void ResAppInfo::GetMountPackagesInfo(std::set<std::string>* pkgids,
-      std::vector<std::string>* allowed_paths,
-      std::vector<std::string>* global_paths) {
-  for (const auto& res_control : res_control_list_) {
-    if (res_control.allowed_package) {
-      pkgids->insert(res_control.allowed_package->GetPkgId());
-      allowed_paths->push_back(res_control.allowed_package->GetRootPath());
-    }
-    if (res_control.global_package) {
-      pkgids->insert(res_control.global_package->GetPkgId());
-      global_paths->push_back(res_control.global_package->GetRootPath());
-    }
-  }
-}
-
 }  // namespace amd
index bef70d9..7e0e511 100644 (file)
@@ -66,16 +66,12 @@ class ResAppInfo {
   static std::shared_ptr<ResAppInfo> CreateResAppInfo(
       const pkgmgrinfo_appinfo_h handle, uid_t uid);
 
-  const std::string& GetAppId();
-  const std::vector<ResControl>& GetResControlList();
+  const std::string& GetAppId() const;
+  const std::vector<ResControl>& GetResControlList() const;
   void ClearResControlMountInfo();
   void UpdateResControlMountInfo(
       const std::unordered_map<std::string,
           std::shared_ptr<ResPkgInfo>>& res_pkgs);
-  void UpdateResControlMountInfo(std::shared_ptr<ResPkgInfo> res_pkginfo);
-  void GetMountPackagesInfo(std::set<std::string>* pkgids,
-      std::vector<std::string>* allowed_paths,
-      std::vector<std::string>* global_paths);
 
  private:
   ResAppInfo(std::string appid, std::string pkgid,
@@ -86,6 +82,8 @@ class ResAppInfo {
             res_control_list_(std::move(res_control_list)),
             privilege_set_(std::move(privilege_set)) {}
 
+  void UpdateResControlMountInfo(std::shared_ptr<ResPkgInfo> res_pkginfo);
+
   std::string appid_;
   std::string pkgid_;
   std::vector<ResControl> res_control_list_;
diff --git a/src/lib/res_info/res_mount_package_info.cc b/src/lib/res_info/res_mount_package_info.cc
new file mode 100644 (file)
index 0000000..0ae986a
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2024 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.
+ */
+
+#include "lib/res_info/res_mount_package_info.hh"
+
+#include <unordered_map>
+
+#include "lib/common/log_private.hh"
+#include "lib/res_info/res_info.hh"
+#include "lib/res_info/res_info_manager.hh"
+#include "lib/res_info/res_pkg_info.hh"
+
+namespace amd {
+
+constexpr const char ALLOWED_SUFFIX[] = "/res/allowed";
+constexpr const char GLOBAL_SUFFIX[] = "/res/global";
+
+ResMountPackageInfo::ResMountPackageInfo(
+    const std::vector<std::shared_ptr<ResPkgInfo>>& allowed_packages,
+    const std::vector<std::shared_ptr<ResPkgInfo>>& global_packages) {
+  for (const auto& pkg : allowed_packages) {
+    pkgids_.insert(pkg->GetPkgId());
+    allowed_paths_.emplace_back(pkg->GetRootPath() + ALLOWED_SUFFIX);
+  }
+  for (const auto& pkg : global_packages) {
+    pkgids_.insert(pkg->GetPkgId());
+    global_paths_.emplace_back(pkg->GetRootPath() + GLOBAL_SUFFIX);
+  }
+}
+
+static int ResInfoGetGadgetResPackagesInfo(uid_t uid,
+    std::optional<ResMountPackageInfo>* info) {
+  std::shared_ptr<ResInfo> resinfo = ResInfoManager::GetInst()
+      .GetUserResInfo(uid);
+  if (!resinfo)
+    return -1;
+
+  pkgmgrinfo_version_compare_type ver_comp;
+  std::unordered_map<std::string, std::shared_ptr<ResPkgInfo>> gadget_map;
+  for (const auto& [_, res_pkginfo] : resinfo->GetGadgetResPkgs()) {
+    auto found = gadget_map.find(res_pkginfo->GetResType());
+    if (found != gadget_map.end()) {
+      if (pkgmgrinfo_compare_package_version(
+          found->second->GetResVersion().c_str(),
+          res_pkginfo->GetResVersion().c_str(),
+          &ver_comp) != PMINFO_R_OK)
+          continue;
+
+      if (ver_comp != PMINFO_VERSION_NEW)
+        continue;
+
+      gadget_map.erase(found);
+    }
+
+    gadget_map[res_pkginfo->GetResType()] = res_pkginfo;
+  }
+
+  if (gadget_map.empty())
+    return 0;
+
+  std::vector<std::shared_ptr<ResPkgInfo>> packages;
+  for (auto& [_, res_pkginfo] : gadget_map)
+    packages.emplace_back(std::move(res_pkginfo));
+
+  *info = ResMountPackageInfo(packages, packages);
+
+  return 0;
+}
+
+int ResMountPackageInfo::GetMountPackageInfo(const std::string& appid,
+    uid_t uid, std::optional<ResMountPackageInfo>* info) {
+  if (appid == "org.tizen.NUIGadgetViewer") {
+    return ResInfoGetGadgetResPackagesInfo(uid, info);
+  }
+
+  std::shared_ptr<ResInfo> resinfo = ResInfoManager::GetInst()
+      .GetUserResInfo(uid);
+  if (!resinfo)
+    return -1;
+
+  std::shared_ptr<ResAppInfo> res_appinfo = resinfo->GetResAppInfo(appid);
+  if (!res_appinfo)
+    return 0;
+
+  res_appinfo->ClearResControlMountInfo();
+  res_appinfo->UpdateResControlMountInfo(resinfo->GetResPkgs());
+
+  std::vector<std::shared_ptr<ResPkgInfo>> allowed_packages;
+  std::vector<std::shared_ptr<ResPkgInfo>> global_packages;
+  for (const auto& res_control : res_appinfo->GetResControlList()) {
+    if (res_control.GetAllowedPackage())
+      allowed_packages.push_back(res_control.GetAllowedPackage());
+    if (res_control.GetGlobalPackage())
+      global_packages.push_back(res_control.GetGlobalPackage());
+  }
+
+  *info = ResMountPackageInfo(allowed_packages, global_packages);
+
+  return 0;
+}
+
+}  // namespace amd
diff --git a/src/lib/res_info/res_mount_package_info.hh b/src/lib/res_info/res_mount_package_info.hh
new file mode 100644 (file)
index 0000000..285c6e7
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2024 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 LIB_RES_INFO_RES_MOUNT_PACKAGE_INFO_HH_
+#define LIB_RES_INFO_RES_MOUNT_PACKAGE_INFO_HH_
+
+#include <pkgmgr-info.h>
+
+#include <set>
+#include <string>
+#include <vector>
+
+#include "lib/res_info/res_pkg_info.hh"
+
+namespace amd {
+
+class ResMountPackageInfo {
+ public:
+  ResMountPackageInfo() {}
+  ResMountPackageInfo(
+      const std::vector<std::shared_ptr<ResPkgInfo>>& allowed_packages,
+      const std::vector<std::shared_ptr<ResPkgInfo>>& global_packages);
+
+  static int GetMountPackageInfo(const std::string& appid, uid_t uid,
+      std::optional<ResMountPackageInfo>* info);
+
+  const std::set<std::string>& GetPkgIds() const { return pkgids_; }
+  const std::vector<std::string>& GetAllowedPaths() const {
+    return allowed_paths_;
+  }
+  const std::vector<std::string>& GetGlobalPaths() const {
+    return global_paths_;
+  }
+
+ private:
+  std::set<std::string> pkgids_;
+  std::vector<std::string> allowed_paths_;
+  std::vector<std::string> global_paths_;
+};
+
+}  // namespace amd
+
+#endif  // LIB_RES_INFO_RES_MOUNT_PACKAGE_INFO_HH_