Fix WgtArchiveInfo 98/226598/5
authorSangyoon Jang <jeremy.jang@samsung.com>
Wed, 4 Mar 2020 05:29:28 +0000 (14:29 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 9 Mar 2020 06:08:15 +0000 (15:08 +0900)
There is a new base class for WgtArchiveInfo.

Requires:
 - https://review.tizen.org/gerrit/c/platform/core/appfw/app-installers/+/226596

Change-Id: I3f0a5524b2267e979da6231e7bf589d086f37d3a
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/lib/wgt_archive_info.cc
src/lib/wgt_archive_info.h
src/lib/wgt_pkgmgr.cc

index 095b60d..4f5a18b 100644 (file)
@@ -4,10 +4,10 @@
 
 #include "lib/wgt_archive_info.h"
 
-#include <package-manager-plugin.h>
-#include <pkgmgr-info.h>
 #include <vconf.h>
 
+#include <boost/filesystem/path.hpp>
+
 #include <wgt_manifest_handlers/widget_config_parser.h>
 #include <wgt_manifest_handlers/application_icons_handler.h>
 #include <wgt_manifest_handlers/application_manifest_constants.h>
@@ -17,7 +17,6 @@
 
 #include <common/utils/file_util.h>
 
-#include <cstdio>
 #include <cstdlib>
 #include <fstream>
 #include <string>
@@ -32,17 +31,19 @@ const char kVconfLanguageKey[] = VCONFKEY_LANGSET;
 const char kConfigFileName[] = "config.xml";
 const char kHybridConfigFileName[] = "res/wgt/config.xml";
 
-bool ExtractPackageArchive(const char* file_path, const char* file,
-    const bf::path& tmp_dir) {
-  if (!ci::ExtractToTmpDir(file_path, tmp_dir, file)) {
+bool ExtractPackageArchive(const std::string& file_path,
+    const std::string& file, const bf::path& tmp_dir) {
+  if (!ci::ExtractToTmpDir(file_path.c_str(), tmp_dir, file.c_str())) {
     LOG(ERROR) << "Failed to extract";
     return false;
   }
   return true;
 }
 
-bool GetPackageInfo(const wgt::parse::WidgetConfigParser& parser,
-    package_manager_pkg_detail_info_t* info) {
+}  // namespace
+
+bool WgtArchiveInfo::GetPackageInfo(
+    const wgt::parse::WidgetConfigParser& parser) {
   auto widget_info =
       std::static_pointer_cast<const wgt::parse::WidgetInfo>(
           parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
@@ -54,23 +55,18 @@ bool GetPackageInfo(const wgt::parse::WidgetConfigParser& parser,
     return false;
   }
 
-  snprintf(info->pkg_type, sizeof(info->pkg_type), "wgt");
-  snprintf(info->version, sizeof(info->version), "%s",
-      widget_info->version().c_str());
-  snprintf(info->author, sizeof(info->version), "%s",
-      widget_info->author().c_str());
-
-  snprintf(info->pkg_name, sizeof(info->pkg_name), "%s",
-      app_info->package().c_str());
-  snprintf(info->pkgid, sizeof(info->pkgid), "%s", app_info->package().c_str());
-  snprintf(info->api_version, sizeof(info->api_version), "%s",
-      app_info->required_version().c_str());
+  type_ = "wgt";
+  name_ = app_info->package().c_str();
+  pkgid_ = app_info->package().c_str();
+  version_ =  widget_info->version().c_str();
+  api_version_ = app_info->required_version().c_str();
+  author_ = widget_info->author().c_str();
 
   return true;
 }
 
-bool GetPrivilegesInfo(const wgt::parse::WidgetConfigParser& parser,
-    package_manager_pkg_detail_info_t* info) {
+bool WgtArchiveInfo::GetPrivilegesInfo(
+    const wgt::parse::WidgetConfigParser& parser) {
   auto privileges_info =
       std::static_pointer_cast<const wgt::parse::PermissionsInfo>(
           parser.GetManifestData(wgt::parse::PermissionsInfo::Key()));
@@ -78,16 +74,59 @@ bool GetPrivilegesInfo(const wgt::parse::WidgetConfigParser& parser,
     return false;
 
   const auto& privileges = privileges_info->GetAPIPermissions();
-  for (auto& priv : privileges) {
-    info->privilege_list = g_list_append(info->privilege_list,
-        strdup(priv.c_str()));
+  for (auto& priv : privileges)
+    privileges_.emplace_back("wgt", priv);
+
+  return true;
+}
+
+bool WgtArchiveInfo::GetIconInfo(
+    const wgt::parse::WidgetConfigParser& parser) {
+  auto icons_info =
+      std::static_pointer_cast<const wgt::parse::ApplicationIconsInfo>(
+          parser.GetManifestData(wgt::parse::ApplicationIconsInfo::Key()));
+  if (!icons_info || icons_info->icons().empty())
+    return false;
+
+  icon_ = icons_info->icons().front().path();
+
+  return true;
+}
+
+bool WgtArchiveInfo::ReadIcon(const bf::path& icon, const bf::path& tmp_dir) {
+  bf::path icon_path = tmp_dir / icon;
+
+  LOG(INFO) << "Icon file path: " << icon_path;
+
+  if (!bf::exists(icon_path)) {
+    LOG(WARNING) << "Icon file doesn't actually exist, skip reading icon";
+    return true;
   }
 
+  std::ifstream ifs(icon_path.c_str(),
+      std::ifstream::in | std::ifstream::binary);
+  ifs.seekg(0, ifs.end);
+  std::streamoff len = ifs.tellg();
+  ifs.seekg(0, ifs.beg);
+
+  if (len <= 0)
+    return false;
+
+  icon_buf_.resize(len / sizeof(unsigned char));
+  ifs.read(reinterpret_cast<char*>(icon_buf_.data()), len);
+  if (len != icon_buf_.size()) {
+    LOG(ERROR) << "Reading icon failed, icon size is: " << len
+               << ", but read size is: " << icon_buf_.size();
+    return false;
+  }
+
+  LOG(INFO) << "Reading icon file, " << icon_buf_.size() << " bytes";
+
   return true;
 }
 
-bool GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
-    const char* locale, package_manager_pkg_detail_info_t* info) {
+bool WgtArchiveInfo::GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
+    const char* locale) {
   auto widget_info =
       std::static_pointer_cast<const wgt::parse::WidgetInfo>(
           parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
@@ -98,19 +137,19 @@ bool GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
   const auto& labels = widget_info->name_set();
   if (labels.find(locale) != labels.end()) {
     name = labels.find(locale)->second;
-    snprintf(info->label, sizeof(info->label), "%s", name.c_str());
+    label_ = name;
     return true;
   } else if (labels.find("") != labels.end()) {
     name = labels.find("")->second;
-    snprintf(info->label, sizeof(info->label), "%s", name.c_str());
+    label_ = name;
     return true;
   }
 
   return false;
 }
 
-bool GetDescriptionInfo(const wgt::parse::WidgetConfigParser& parser,
-    const char* locale, package_manager_pkg_detail_info_t* info) {
+bool WgtArchiveInfo::GetDescriptionInfo(
+    const wgt::parse::WidgetConfigParser& parser, const char* locale) {
   auto widget_info =
       std::static_pointer_cast<const wgt::parse::WidgetInfo>(
           parser.GetManifestData(wgt::parse::WidgetInfo::Key()));
@@ -121,75 +160,29 @@ bool GetDescriptionInfo(const wgt::parse::WidgetConfigParser& parser,
   const auto& descriptions = widget_info->description_set();
   if (descriptions.find(locale) != descriptions.end()) {
     desc = descriptions.find(locale)->second;
-    snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
-        desc.c_str());
+    description_ = desc;
     return true;
   } else if (descriptions.find("") != descriptions.end()) {
     desc = descriptions.find("")->second;
-    snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
-        desc.c_str());
+    description_ = desc;
     return true;
   }
 
   return false;
 }
 
-bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
-    package_manager_pkg_detail_info_t* info) {
-  bf::path icon_path = tmp_dir / icon;
-
-  LOG(INFO) << "Icon file path: " << icon_path;
-
-  if (!bf::exists(icon_path)) {
-    LOG(WARNING) << "Icon file doesn't actually exist, skip reading icon";
-    return true;
-  }
-
-  std::ifstream ifs(icon_path.c_str(),
-      std::ifstream::in | std::ifstream::binary);
-  ifs.seekg(0, ifs.end);
-  int len = ifs.tellg();
-  ifs.seekg(0, ifs.beg);
-
-  if (len <= 0)
-    return false;
-
-  char* buf = static_cast<char*>(malloc(sizeof(char) * len));
-
-  LOG(INFO) << "Reading icon file, " << len << " bytes";
-  ifs.read(buf, len);
-
-  info->icon_buf = buf;
-  info->icon_size = len;
-
-  return true;
-}
-
-std::string GetIconInfo(const wgt::parse::WidgetConfigParser& parser) {
-  auto icons_info =
-      std::static_pointer_cast<const wgt::parse::ApplicationIconsInfo>(
-          parser.GetManifestData(wgt::parse::ApplicationIconsInfo::Key()));
-  if (!icons_info || icons_info->icons().empty())
-    return {};
-
-  return std::string(icons_info->icons().front().path());
-}
-
-}  // namespace
-
-bool WgtArchiveInfo::GetArchiveInfo(const char* file_path,
-    package_manager_pkg_detail_info_t* info) {
+bool WgtArchiveInfo::LoadArchiveInfo() {
   bf::path tmp_dir = ci::GenerateTmpDir("/tmp");
   if (!ci::CreateDir(tmp_dir))
     return false;
   LOG(DEBUG) << "Unpack at temporary dir: " << tmp_dir;
   bool is_hybrid = false;
-  if (!ExtractPackageArchive(file_path, kHybridConfigFileName, tmp_dir))
+  if (!ExtractPackageArchive(path_, kHybridConfigFileName, tmp_dir))
     return false;
   if (bf::exists(tmp_dir / kHybridConfigFileName)) {
     is_hybrid = true;
   } else {
-    if (!ExtractPackageArchive(file_path, kConfigFileName, tmp_dir))
+    if (!ExtractPackageArchive(path_, kConfigFileName, tmp_dir))
       return false;
   }
 
@@ -205,25 +198,26 @@ bool WgtArchiveInfo::GetArchiveInfo(const char* file_path,
     return false;
   }
 
-  if (!GetPackageInfo(parser, info)) {
+  if (!GetPackageInfo(parser)) {
     LOG(ERROR) << "Failed to get package info";
     bf::remove_all(tmp_dir);
     return false;
   }
-  if (!GetPrivilegesInfo(parser, info))
+  if (!GetPrivilegesInfo(parser))
     LOG(WARNING) << "Failed to get privileges info";
-  std::string icon = GetIconInfo(parser);
-  if (!icon.empty()) {
+  if (!GetIconInfo(parser))
+    LOG(WARNING) << "Failed to get icon info";
+  if (!icon_.empty()) {
     std::string icon_path;
     if (is_hybrid)
-      icon_path = "res/wgt/" + icon;
+      icon_path = "res/wgt/" + icon_;
     else
-      icon_path = icon;
-    if (!ExtractPackageArchive(file_path, icon_path.c_str(), tmp_dir)) {
+      icon_path = icon_;
+    if (!ExtractPackageArchive(path_, icon_path, tmp_dir)) {
       bf::remove_all(tmp_dir);
       return false;
     }
-    if (!ReadIcon(icon_path, tmp_dir, info)) {
+    if (!ReadIcon(icon_path, tmp_dir)) {
       LOG(WARNING) << "Failed to get icon info";
       bf::remove_all(tmp_dir);
       return false;
@@ -233,9 +227,9 @@ bool WgtArchiveInfo::GetArchiveInfo(const char* file_path,
   char* locale = vconf_get_str(kVconfLanguageKey);
   if (!locale)
     locale = strdup("");
-  if (!GetLabelInfo(parser, locale, info))
+  if (!GetLabelInfo(parser, locale))
     LOG(WARNING) << "Failed to get label info";
-  if (!GetDescriptionInfo(parser, locale, info))
+  if (!GetDescriptionInfo(parser, locale))
     LOG(WARNING) << "Failed to get description info";
 
   free(locale);
index e2bf8d1..085bb47 100644 (file)
@@ -5,17 +5,31 @@
 #ifndef LIB_WGT_ARCHIVE_INFO_H_
 #define LIB_WGT_ARCHIVE_INFO_H_
 
-#include <package-manager-plugin.h>
+#include <boost/filesystem/path.hpp>
 
+#include <common/archive_info.h>
 #include <manifest_parser/utils/logging.h>
+#include <wgt_manifest_handlers/widget_config_parser.h>
 
-#include <common/utils/singleton.h>
+#include <string>
 
-class WgtArchiveInfo : public common_installer::Singleton<WgtArchiveInfo> {
-  CRTP_DECLARE_DEFAULT_CONSTRUCTOR_CLASS(WgtArchiveInfo)
+namespace bf = boost::filesystem;
+
+class WgtArchiveInfo : public common_installer::ArchiveInfo {
  public:
-  bool GetArchiveInfo(const char* file_path,
-    package_manager_pkg_detail_info_t* info);
+  explicit WgtArchiveInfo(std::string path)
+      : common_installer::ArchiveInfo(path) { }
+  bool LoadArchiveInfo() override;
+
+ private:
+  bool GetPackageInfo(const wgt::parse::WidgetConfigParser& parser);
+  bool GetPrivilegesInfo(const wgt::parse::WidgetConfigParser& parser);
+  bool GetIconInfo(const wgt::parse::WidgetConfigParser& parser);
+  bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir);
+  bool GetLabelInfo(const wgt::parse::WidgetConfigParser& parser,
+      const char* locale);
+  bool GetDescriptionInfo(const wgt::parse::WidgetConfigParser& parser,
+      const char* locale);
 
   SCOPE_LOG_TAG(WgtArchiveInfo)
 };
index 129c185..62dbad1 100644 (file)
@@ -12,7 +12,10 @@ namespace {
 
 int GetPackageArchiveInfo(const char* path,
     package_manager_pkg_detail_info_t* info) {
-  if (!WgtArchiveInfo::Instance().GetArchiveInfo(path, info))
+  WgtArchiveInfo archive_info(path);
+  if (!archive_info.LoadArchiveInfo())
+    return -1;
+  if (!archive_info.GetPkgDetailInfo(info))
     return -1;
   return 0;
 }