Fix static analysis issue
[platform/core/appfw/app-installers.git] / src / common / pkgmgr_registration.cc
index 827c9ee..71c2d64 100644 (file)
@@ -13,6 +13,7 @@
 #include <vector>
 #include <utility>
 
+#include "common/utils/glist_range.h"
 #include "common/utils/manifest_util.h"
 
 namespace bf = boost::filesystem;
@@ -22,6 +23,8 @@ namespace {
 
 using CertInfo = common_installer::CertificateInfo;
 
+const char kWidgetApp[] = "widgetapp";
+
 bool RegisterCertificate(pkgmgr_instcertinfo_h handle,
 const ValidationCore::CertificatePtr& certPtr, pkgmgr_instcert_type type) {
     if (certPtr) {
@@ -39,17 +42,17 @@ typedef Property<ValidationCore::CertificatePtr> CertInfo::*PropertyOfCertInf;
 
 const std::vector<std::pair<PropertyOfCertInf, pkgmgr_instcert_type>>
     kCertTypePairs = {
-  {&CertInfo::author_certificate, PM_SET_AUTHOR_SIGNER_CERT},
-  {&CertInfo::author_intermediate_certificate, PM_SET_AUTHOR_INTERMEDIATE_CERT},
-  {&CertInfo::author_root_certificate, PM_SET_AUTHOR_ROOT_CERT},
-  {&CertInfo::distributor_certificate, PM_SET_DISTRIBUTOR_SIGNER_CERT},
-  {&CertInfo::distributor_intermediate_certificate,
+  {&CertInfo::auth_cert, PM_SET_AUTHOR_SIGNER_CERT},
+  {&CertInfo::auth_im_cert, PM_SET_AUTHOR_INTERMEDIATE_CERT},
+  {&CertInfo::auth_root_cert, PM_SET_AUTHOR_ROOT_CERT},
+  {&CertInfo::dist_cert, PM_SET_DISTRIBUTOR_SIGNER_CERT},
+  {&CertInfo::dist_im_cert,
       PM_SET_DISTRIBUTOR_INTERMEDIATE_CERT},
-  {&CertInfo::distributor_root_certificate, PM_SET_DISTRIBUTOR_ROOT_CERT},
-  {&CertInfo::distributor2_certificate, PM_SET_DISTRIBUTOR2_SIGNER_CERT},
-  {&CertInfo::distributor2_intermediate_certificate,
+  {&CertInfo::dist_root_cert, PM_SET_DISTRIBUTOR_ROOT_CERT},
+  {&CertInfo::dist2_cert, PM_SET_DISTRIBUTOR2_SIGNER_CERT},
+  {&CertInfo::dist2_im_cert,
       PM_SET_DISTRIBUTOR2_INTERMEDIATE_CERT},
-  {&CertInfo::distributor2_root_certificate, PM_SET_DISTRIBUTOR2_ROOT_CERT}
+  {&CertInfo::dist2_root_cert, PM_SET_DISTRIBUTOR2_ROOT_CERT}
 };
 
 bool RegisterCertificates(
@@ -60,7 +63,7 @@ bool RegisterCertificates(
     LOG(ERROR) << "Cannot create pkgmgr_instcertinfo_h";
     return false;
   }
-  for (auto pair : kCertTypePairs) {
+  for (const auto& pair : kCertTypePairs) {
      auto cert = (cert_info.*(pair.first)).get();
      auto type = pair.second;
      if (!RegisterCertificate(handle, cert, type))
@@ -76,27 +79,17 @@ bool RegisterCertificates(
   return true;
 }
 
-// "preload" : pre-installed package at the first boot state.
-//             this package exists in both readonly and readwrite.
-// "system" : this package is "preload" and is not removable.
-// "update" : this package is "preload" and is updated by downloadable update.
-// "removable" : this package can be removed.
-// "readonly" : this package exists in readonly location.
-bool AssignPackageTags(manifest_x* manifest) {
-  if (!manifest)
-    return false;
-
-  // preload, removalbe and readonly : in parse_preload step.
-  if (manifest->preload && !strcmp(manifest->preload, "true")) {
-    if (manifest->removable && !strcmp(manifest->removable, "false"))
-      manifest->system = strdup("true");
-    else
-      manifest->system = strdup("false");
-  } else {
-    manifest->system = strdup("false");
+void AdjustWidgetNodisplayAttr(manifest_x* manifest) {
+  for (auto& app : GListRange<application_x*>(manifest->application)) {
+    if (!strcmp(app->component_type, kWidgetApp)) {
+      free(app->nodisplay);
+      // The nodisplay attribute of widget application should be true,
+      // but nodisplay of widget-service framework uses this attribute
+      // as defined at manifest file. So we need to adjust this value
+      // just before register in pkgmgr db.
+      app->nodisplay = strdup("true");
+    }
   }
-
-  return true;
 }
 
 }  // namespace
@@ -107,14 +100,24 @@ bool RegisterAppInPkgmgr(manifest_x* manifest,
                          const std::string& pkgid,
                          const CertificateInfo& cert_info,
                          uid_t uid,
+                         Storage storage,
                          RequestMode request_mode,
                          const boost::filesystem::path& tep_path) {
   // Fill "non-xml" elements
   if (!tep_path.empty())
     manifest->tep_name = strdup(tep_path.c_str());
 
-  if (!AssignPackageTags(manifest))
-    return false;
+  if (storage == Storage::EXTENDED) {
+    bf::path ext_path = bf::path(GetExtendedRootAppPath(uid)) / pkgid;
+    manifest->external_path = strdup(ext_path.c_str());
+  } else if (storage == Storage::EXTERNAL) {
+    App2ExtDynamicService service;
+    std::string image_path = service.GetExternalImagePath(pkgid.c_str(), uid);
+    if (!image_path.empty())
+      manifest->external_path = strdup(image_path.c_str());
+  }
+
+  AdjustWidgetNodisplayAttr(manifest);
 
   int ret = request_mode != RequestMode::GLOBAL ?
       pkgmgr_parser_process_usr_manifest_x_for_installation(manifest, uid) :
@@ -136,9 +139,19 @@ bool UpgradeAppInPkgmgr(manifest_x* manifest,
                         const std::string& pkgid,
                         const CertificateInfo& cert_info,
                         uid_t uid,
+                        Storage storage,
                         RequestMode request_mode) {
-  if (!AssignPackageTags(manifest))
-    return false;
+  if (storage == Storage::EXTENDED) {
+    bf::path ext_path = bf::path(GetExtendedRootAppPath(uid)) / pkgid;
+    manifest->external_path = strdup(ext_path.c_str());
+  } else if (storage == Storage::EXTERNAL) {
+    App2ExtDynamicService service;
+    std::string image_path = service.GetExternalImagePath(pkgid.c_str(), uid);
+    if (!image_path.empty())
+      manifest->external_path = strdup(image_path.c_str());
+  }
+
+  AdjustWidgetNodisplayAttr(manifest);
 
   int ret = request_mode != RequestMode::GLOBAL ?
        pkgmgr_parser_process_usr_manifest_x_for_upgrade(manifest, uid) :
@@ -158,9 +171,11 @@ bool UpgradeAppInPkgmgr(manifest_x* manifest,
 
 bool UnregisterAppInPkgmgrForPkgId(const std::string& pkgid, uid_t uid,
                                    RequestMode request_mode) {
-  manifest_x* manifest =
-     static_cast<manifest_x*>(calloc(1, sizeof(manifest_x)));
-  PkgmgrGenerateManifestInfoFromDB(manifest, pkgid, uid);
+  manifest_x* manifest = PkgmgrGenerateManifestInfoFromDB(pkgid, uid);
+  if (!manifest) {
+    LOG(ERROR) << "Failed to get manifest_x from DB";
+    return false;
+  }
   bool res = UnregisterAppInPkgmgr(manifest, pkgid, uid, request_mode);
   pkgmgr_parser_free_manifest_xml(manifest);
   return res;
@@ -200,6 +215,31 @@ bool UpdateTepInfoInPkgmgr(const bf::path& tep_path, const std::string& pkgid,
   return true;
 }
 
+bool UpdateInstalledStorageInPkgmgr(Storage storage, const bf::path& ext_path,
+                                    const std::string& pkgid, uid_t uid) {
+  // FIXME: refactor this
+  INSTALL_LOCATION install_location;
+  const char* path;
+  if (storage == Storage::INTERNAL) {
+    install_location = INSTALL_INTERNAL;
+    path = "";
+  } else if (storage == Storage::EXTERNAL) {
+    install_location = INSTALL_EXTERNAL;
+    path = ext_path.string().c_str();
+  } else {
+    install_location = INSTALL_EXTENDED;
+    path = ext_path.string().c_str();
+  }
+  int ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(
+      pkgid.c_str(), install_location, path, uid);
+  if (ret != 0) {
+    LOG(ERROR) << "Failed to update installed storage info: " << pkgid;
+    return false;
+  }
+
+  return true;
+}
+
 bool DisablePkgInPkgmgr(const std::string& pkgid, uid_t uid,
                         RequestMode request_mode) {
   int ret = request_mode != RequestMode::GLOBAL ?
@@ -214,7 +254,7 @@ bool DisablePkgInPkgmgr(const std::string& pkgid, uid_t uid,
 }
 
 bool EnablePkgInPkgmgr(const std::string& pkgid, uid_t uid,
-                        RequestMode request_mode) {
+    RequestMode request_mode) {
   int ret = request_mode != RequestMode::GLOBAL ?
         pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid.c_str(), uid, 0) :
         pkgmgr_parser_update_pkg_disable_info_in_db(pkgid.c_str(), 0);
@@ -226,4 +266,42 @@ bool EnablePkgInPkgmgr(const std::string& pkgid, uid_t uid,
   return true;
 }
 
+bool RegisterPluginInfo(manifest_x* manifest,
+                        uid_t uid,
+                        RequestMode request_mode) {
+  int ret = request_mode != RequestMode::GLOBAL ?
+      pkgmgr_parser_register_pkg_plugin_info_in_usr_db(manifest, uid) :
+      pkgmgr_parser_register_pkg_plugin_info_in_db(manifest);
+  if (ret) {
+    LOG(ERROR) << "Failed to insert plugin info, error code=" << ret;
+    return false;
+  }
+  return true;
+}
+
+bool UpdatePluginInfo(manifest_x* manifest,
+                      uid_t uid, RequestMode request_mode) {
+  int ret = request_mode != RequestMode::GLOBAL ?
+      pkgmgr_parser_update_pkg_plugin_info_in_usr_db(manifest, uid) :
+      pkgmgr_parser_update_pkg_plugin_info_in_db(manifest);
+  if (ret) {
+    LOG(ERROR) << "Failed to insert update plugin info, error code=" << ret;
+    return false;
+  }
+  return true;
+}
+
+bool UnregisterPluginInfo(const std::string& pkgid,
+                          uid_t uid,
+                          RequestMode request_mode) {
+  int ret = request_mode != RequestMode::GLOBAL ?
+      pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(pkgid.c_str(), uid) :
+      pkgmgr_parser_unregister_pkg_plugin_info_in_db(pkgid.c_str());
+  if (ret) {
+    LOG(ERROR) << "Failed to remove plugin info, error code=" << ret;
+    return false;
+  }
+  return true;
+}
+
 }  // namespace common_installer