Fix update detection in app-installers 07/41807/3
authorTomasz Iwanek <t.iwanek@samsung.com>
Wed, 10 Jun 2015 12:49:03 +0000 (14:49 +0200)
committerTomasz Iwanek <t.iwanek@samsung.com>
Thu, 18 Jun 2015 10:50:44 +0000 (12:50 +0200)
app-installers will need to detect update by itself to set
correct step for installation process. Therefore this patch
integrates previous workaround into common library code.

This will not requires changes in pkgmgr.

Introducing AppQueryInterface for each backend to provide.
This class will decide if update installation flow should
be forced or not. If pkgmgr backendlib would be developed
then this interface will require refactoring probably.

Please, note that tpk will need to provide own
AppQueryInterface instance.

Change-Id: Ic8d09b72227310fa4eb76bce882302c4291495d8

src/common/app_query_interface.h [new file with mode: 0644]
src/common/pkgmgr_interface.cc
src/common/pkgmgr_interface.h
src/wgt/CMakeLists.txt
src/wgt/update_workaround.h [deleted file]
src/wgt/wgt_app_query_interface.cc [moved from src/wgt/update_workaround.cc with 77% similarity]
src/wgt/wgt_app_query_interface.h [new file with mode: 0644]
src/wgt/wgt_backend.cc

diff --git a/src/common/app_query_interface.h b/src/common/app_query_interface.h
new file mode 100644 (file)
index 0000000..3ab40c1
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMMON_APP_QUERY_INTERFACE_H_
+#define COMMON_APP_QUERY_INTERFACE_H_
+
+namespace common_installer {
+
+class AppQueryInterface {
+ public:
+  virtual ~AppQueryInterface() { }
+  virtual bool IsAppInstalledByArgv(int argc, char** argv) = 0;
+};
+
+}  // namespace common_installer
+
+#endif  // COMMON_APP_QUERY_INTERFACE_H_
index 9df7915..5e0ca3c 100644 (file)
@@ -6,6 +6,8 @@
 
 #include <memory>
 
+#include "common/app_query_interface.h"
+
 namespace common_installer {
 
 PkgMgrPtr PkgMgrInterface::instance_;
@@ -14,11 +16,11 @@ PkgMgrPtr PkgMgrInterface::Instance() {
   return instance_;
 }
 
-int PkgMgrInterface::Init(int argc, char** argv) {
+int PkgMgrInterface::Init(int argc, char** argv, AppQueryInterface* interface) {
   if (instance_)
     return 0;
 
-  PkgMgrPtr tmp(new PkgMgrInterface());
+  PkgMgrPtr tmp(new PkgMgrInterface(interface));
   int result = tmp->InitInternal(argc, argv);
 
   instance_ = tmp;
@@ -39,6 +41,11 @@ int PkgMgrInterface::InitInternal(int argc, char** argv) {
     LOG(ERROR) << "Cannot receive request. Invalid arguments?";
     // no need to free pkgmgr_installer here. it will be freed in DTOR.
   }
+
+  is_app_installed_ = false;
+  if (query_interface_)
+    is_app_installed_ = query_interface_->IsAppInstalledByArgv(argc, argv);
+
   return result;
 }
 
@@ -50,9 +57,11 @@ PkgMgrInterface::~PkgMgrInterface() {
 PkgMgrInterface::Type PkgMgrInterface::GetRequestType() const {
   switch (pkgmgr_installer_get_request_type(pi_)) {
     case PKGMGR_REQ_INSTALL:
-      return PkgMgrInterface::Type::Install;
-    case PKGMGR_REQ_UPGRADE:
-      return PkgMgrInterface::Type::Update;
+      if (!is_app_installed_) {
+        return PkgMgrInterface::Type::Install;
+      } else {
+        return PkgMgrInterface::Type::Update;
+      }
     case PKGMGR_REQ_UNINSTALL:
       return PkgMgrInterface::Type::Uninstall;
     case PKGMGR_REQ_REINSTALL:
index f7dc605..07e2e94 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <memory>
 
+#include "common/app_query_interface.h"
 #include "utils/macros.h"
 #include "utils/logging.h"
 
@@ -53,7 +54,8 @@ class PkgMgrInterface {
 
   /** Initialize PkgMgrInterface.
    */
-  static int Init(int argc, char** argv);
+  static int Init(int argc, char** argv,
+        AppQueryInterface* interface = nullptr);
 
   /** Get Raw pointer to pkgmgr_installer object
    *
@@ -67,10 +69,14 @@ class PkgMgrInterface {
   ~PkgMgrInterface();
 
  private:
-  PkgMgrInterface() :pi_(nullptr) {}
+  explicit PkgMgrInterface(AppQueryInterface* interface)
+      : pi_(nullptr),
+        query_interface_(interface) {}
   int InitInternal(int argc, char** argv);
 
   pkgmgr_installer* pi_;
+  bool is_app_installed_;
+  AppQueryInterface* query_interface_;
   static PkgMgrPtr instance_;
 
   SCOPE_LOG_TAG(PkgMgrInterface)
index 44d6380..39641d9 100644 (file)
@@ -2,7 +2,7 @@
 SET(SRCS
   step/step_create_symbolic_link.cc
   step/step_parse.cc
-  update_workaround.cc
+  wgt_app_query_interface.cc
   wgt_backend.cc
   wgt_backend_data.cc
 )
diff --git a/src/wgt/update_workaround.h b/src/wgt/update_workaround.h
deleted file mode 100644 (file)
index bb455af..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by an apache 2.0 license that can be
-// found in the LICENSE file.
-
-#ifndef WGT_UPDATE_WORKAROUND_H_
-#define WGT_UPDATE_WORKAROUND_H_
-
-namespace workaround {
-
-void OverwriteArgvIfNeeded(int argc, char** argv);
-
-}  // namespace workaround
-
-#endif  // WGT_UPDATE_WORKAROUND_H_
similarity index 77%
rename from src/wgt/update_workaround.cc
rename to src/wgt/wgt_app_query_interface.cc
index 944cc02..190c717 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an apache 2.0 license that can be
 // found in the LICENSE file.
 
-#include "wgt/update_workaround.h"
+#include "wgt/wgt_app_query_interface.h"
 
 #include <unistd.h>
 #include <sys/types.h>
@@ -32,19 +32,17 @@ namespace bs = boost::system;
 
 namespace {
 
-std::pair<std::string, int> GetInstallationPackagePath(int argc, char** argv) {
-  int index = -1;
+std::string GetInstallationPackagePath(int argc, char** argv) {
   std::string path;
-  for (unsigned i = 0; i < argc; ++i) {
+  for (int i = 0; i < argc; ++i) {
     if (!strcmp(argv[i], "-i")) {
-      index = i;
       if (i + 1 < argc) {
         path = argv[i + 1];
+        break;
       }
-      break;
     }
   }
-  return std::make_pair(path, index);
+  return path;
 }
 
 std::string GetAppIdFromPath(const std::string& path) {
@@ -94,32 +92,20 @@ bool IsPackageInstalled(const std::string& pkg_id) {
   return true;
 }
 
-void OverwriteArgvForUpdate(int index, char** argv) {
-  // overwriting argv to fake update
-  argv[index][1] = 'a';
-}
-
 }  // namespace
 
-namespace workaround {
+namespace wgt {
 
-void OverwriteArgvIfNeeded(int argc, char** argv) {
-  std::string path;
-  int index;
-  std::tie(path, index) =
-      GetInstallationPackagePath(argc, argv);
+bool WgtAppQueryInterface::IsAppInstalledByArgv(int argc, char** argv) {
+  std::string path = GetInstallationPackagePath(argc, argv);
   if (path.empty()) {
     // not the installaton
-    return;
+    return false;
   }
   std::string pkg_id = GetAppIdFromPath(path);
   if (pkg_id.empty())
-    return;
-  bool should_hack = IsPackageInstalled(pkg_id);
-  if (should_hack) {
-    LOG(INFO) << "Update detected - hacking argv to update installation";
-    OverwriteArgvForUpdate(index, argv);
-  }
+    return false;
+  return IsPackageInstalled(pkg_id);
 }
 
-}  // namespace workaround
+}  // namespace wgt
diff --git a/src/wgt/wgt_app_query_interface.h b/src/wgt/wgt_app_query_interface.h
new file mode 100644 (file)
index 0000000..965ce7f
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache 2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef WGT_WGT_APP_QUERY_INTERFACE_H_
+#define WGT_WGT_APP_QUERY_INTERFACE_H_
+
+#include "common/app_query_interface.h"
+
+namespace wgt {
+
+class WgtAppQueryInterface : public common_installer::AppQueryInterface {
+ public:
+  bool IsAppInstalledByArgv(int argc, char** argv) override;
+};
+
+}  // namespace wgt
+
+#endif  // WGT_WGT_APP_QUERY_INTERFACE_H_
index 69c2963..031960f 100644 (file)
 #include "common/step/step_update_app.h"
 #include "common/step/step_update_security.h"
 
-#include "wgt/update_workaround.h"
-
 #include "wgt/step/step_create_symbolic_link.h"
 #include "wgt/step/step_parse.h"
+#include "wgt/wgt_app_query_interface.h"
 
 namespace ci = common_installer;
 
 int main(int argc, char** argv) {
-  // TODO(t.iwanek): remove this when update installation detection is ready
-  workaround::OverwriteArgvIfNeeded(argc, argv);
-
-  int result = ci::PkgMgrInterface::Init(argc, argv);
+  wgt::WgtAppQueryInterface query_interface;
+  int result = ci::PkgMgrInterface::Init(argc, argv, &query_interface);
   if (result) {
     LOG(ERROR) << "Cannot connect to PkgMgrInstaller";
     return -result;