Fix query IsHybridApplication()
[platform/core/appfw/wgt-backend.git] / src / wgt / wgt_app_query_interface.cc
index b45389c..7b2eee4 100644 (file)
@@ -11,7 +11,8 @@
 #include <boost/filesystem/path.hpp>
 #include <boost/system/error_code.hpp>
 
-#include <common/pkgmgr_registration.h>
+#include <common/pkgmgr_interface.h>
+#include <common/pkgmgr_query.h>
 #include <common/recovery_file.h>
 #include <common/request.h>
 #include <common/utils/file_util.h>
 #include <wgt_manifest_handlers/widget_handler.h>
 
 #include <memory>
+#include <utility>
 #include <string>
 #include <vector>
 
-
 namespace bf = boost::filesystem;
 namespace bs = boost::system;
 namespace ci = common_installer;
@@ -36,20 +37,6 @@ namespace {
 const char kHybridConfigLocation[] = "res/wgt/config.xml";
 const char kTizenManifestLocation[] = "tizen-manifest.xml";
 
-std::string GetInstallationRequestInfo(int argc, char** argv) {
-  std::string path;
-  for (int i = 0; i < argc; ++i) {
-    if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "-r") ||
-        !strcmp(argv[i], "-d") || !strcmp(argv[i], "-b")) {
-      if (i + 1 < argc) {
-        path = argv[i + 1];
-        break;
-      }
-    }
-  }
-  return path;
-}
-
 std::string GetPkgIdFromPath(const std::string& path) {
   if (!bf::exists(path))
     return {};
@@ -60,32 +47,32 @@ std::string GetPkgIdFromPath(const std::string& path) {
     return {};
   if (!common_installer::ExtractToTmpDir(path.c_str(), tmp_path,
       "config.xml")) {
-    bf::remove_all(tmp_path, code);
+    ci::RemoveAll(tmp_path);
     return {};
   }
   bf::path config_path = tmp_path / "config.xml";
-  std::vector<parser::ManifestHandler*> handlers = {
-    new wgt::parse::WidgetHandler(),
-    new wgt::parse::TizenApplicationHandler()
+  std::vector<std::shared_ptr<parser::ManifestHandler>> handlers = {
+    std::make_shared<wgt::parse::WidgetHandler>(),
+    std::make_shared<wgt::parse::TizenApplicationHandler>()
   };
   std::unique_ptr<parser::ManifestHandlerRegistry> registry(
       new parser::ManifestHandlerRegistry(handlers));
   std::unique_ptr<parser::ManifestParser> parser(
       new parser::ManifestParser(std::move(registry)));
   if (!parser->ParseManifest(config_path)) {
-    bf::remove_all(tmp_path, code);
+    ci::RemoveAll(tmp_path);
     return {};
   }
   auto info = std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
       parser->GetManifestData(
           wgt::application_widget_keys::kTizenApplicationKey));
   if (!info) {
-    bf::remove_all(tmp_path, code);
+    ci::RemoveAll(tmp_path);
     return {};
   }
   std::string pkg_id = info->package();
 
-  bf::remove_all(tmp_path, code);
+  ci::RemoveAll(tmp_path);
   return pkg_id;
 }
 
@@ -100,32 +87,39 @@ std::string ReadPkgidFromRecovery(const std::string& recovery_path) {
 
 namespace wgt {
 
-bool WgtAppQueryInterface::IsAppInstalledByArgv(int argc, char** argv) {
-  std::string path = GetInstallationRequestInfo(argc, argv);
-  if (path.empty())
-    return false;
-  std::string pkg_id = GetPkgIdFromPath(path);
+bool WgtAppQueryInterface::IsPkgInstalled(const std::string& arg, uid_t uid) {
+  // argument from commandline is package id
+  if (ci::QueryIsPackageInstalled(arg, ci::GetRequestMode(uid), uid))
+    return true;
+
+  // argument from commandline is path to file
+  std::string pkg_id = GetPkgIdFromPath(arg);
   if (pkg_id.empty())
     return false;
-  return ci::IsPackageInstalled(pkg_id, ci::GetRequestMode());
+  return ci::QueryIsPackageInstalled(pkg_id, ci::GetRequestMode(uid), uid);
 }
 
-bool WgtAppQueryInterface::IsHybridApplication(int argc, char** argv) {
-  std::string arg = GetInstallationRequestInfo(argc, argv);
-  if (arg.find("apps_rw/recovery-") != std::string::npos)
-    arg = ReadPkgidFromRecovery(arg);
-  if (ci::IsPackageInstalled(arg, ci::GetRequestMode())) {
-    bf::path package_directory(ci::GetRootAppPath(false));
-    if (bf::exists(package_directory / arg / kTizenManifestLocation) &&
-        bf::exists(package_directory / arg / kHybridConfigLocation))
+bool WgtAppQueryInterface::IsHybridApplication(const std::string& arg,
+    uid_t uid) {
+  std::string info;
+  bool is_recovery = arg.find("wgt-recovery-") != std::string::npos;
+  if (is_recovery)
+    info = ReadPkgidFromRecovery(arg);
+  else
+    info = arg;
+
+  if (ci::QueryIsPackageInstalled(info, ci::GetRequestMode(uid), uid)) {
+    bf::path package_directory(ci::GetRootAppPath(false, uid));
+    if (bf::exists(package_directory / info / kTizenManifestLocation) &&
+        bf::exists(package_directory / info / kHybridConfigLocation))
       return true;
-  } else {
+  } else if (!is_recovery) {
     bool tizen_manifest_found = false;
     bool config_xml_found = false;
-    if (!ci::CheckPathInZipArchive(arg.c_str(), kTizenManifestLocation,
+    if (!ci::CheckPathInZipArchive(info.c_str(), kTizenManifestLocation,
                                    &tizen_manifest_found))
       return false;
-    if (!ci::CheckPathInZipArchive(arg.c_str(), kHybridConfigLocation,
+    if (!ci::CheckPathInZipArchive(info.c_str(), kHybridConfigLocation,
                                    &config_xml_found))
       return false;
     if (tizen_manifest_found && config_xml_found)
@@ -134,4 +128,8 @@ bool WgtAppQueryInterface::IsHybridApplication(int argc, char** argv) {
   return false;
 }
 
+std::string WgtAppQueryInterface::GetPkgId(const std::string& arg) {
+  return GetPkgIdFromPath(arg);
+}
+
 }  // namespace wgt