Add unified-installer library
authorSangyoon Jang <jeremy.jang@samsung.com>
Wed, 22 Jan 2020 07:21:10 +0000 (16:21 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Wed, 22 Jan 2020 07:21:10 +0000 (16:21 +0900)
Added classes:
 - InstallerRunner
 - InstallerFactory

Change-Id: I6e205b7d9e1e9c1e378fb3b54f4d356a18f7cc1c
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
packaging/unified-backend.spec
src/unified/CMakeLists.txt
src/unified/installer_factory.cc [new file with mode: 0644]
src/unified/installer_factory.h [new file with mode: 0644]
src/unified/installer_runner.cc [new file with mode: 0644]
src/unified/installer_runner.h [new file with mode: 0644]
src/unified/unified-installer.pc.in [new file with mode: 0644]
src/unified/unified_backend.cc

index 126aadd6b3c848bf809da422a88ca122cf6bd51d..e7090d1495646174678f91f67c142d359faf2740 100644 (file)
@@ -26,6 +26,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EXTRA_LINKER_FLAGS}")
 
 # Targets
 SET(TARGET_UNIFIED_BACKEND "unified-backend")
+SET(TARGET_LIBNAME_UNIFIED "unified-installer")
 
 ADD_DEFINITIONS("-DPROJECT_TAG=\"UNIFIED_BACKEND\"")
 
index 6a937054f37f97db496edfab21d0610e3bf5c315..383e84decc8bc5eb19167d2509bcd420523136fa 100644 (file)
@@ -1,5 +1,5 @@
 Name:          unified-backend
-Summary:       Unified application installer backend
+Summary:       Unified Tizen package installer backend
 Version:       0.0.1
 Release:       1
 Group:         Application Framework/Package Management
@@ -22,6 +22,21 @@ BuildRequires: pkgconfig(wgt-installer)
 %description
 This is a package that installs the Unified backend of pkgmgr.
 
+%package -n unified-installer
+Summary:    Unified Tizen package installer library
+Group:      Application Framework/Package Management
+
+%description -n unified-installer
+This package contains unified-installer library
+
+%package -n unified-installer-devel
+Summary:  Unified Installer development files
+Group:    Application Framework/Package Management
+Requires: unified-installer = %{version}
+
+%description -n unified-installer-devel
+This package contains header files of unified-installer library
+
 %prep
 %setup -q
 
@@ -37,4 +52,11 @@ make %{?_smp_mflags}
 %files
 %manifest %{name}.manifest
 %license LICENSE
-%{_bindir}
+%{_bindir}/*
+
+%files -n unified-installer
+%{_libdir}/libunified-installer.so*
+
+%files -n unified-installer-devel
+%{_includedir}/app-installers/unified/*.h
+%{_libdir}/pkgconfig/unified-installer.pc
index 45307ee7ae86edb1292a98ca6d06bac105f61d44..e74495555e39ea3f291e39edfd3818b99c886b02 100644 (file)
@@ -1,11 +1,14 @@
 # Target - definition
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} UNIFIED_SRCS)
-
-ADD_EXECUTABLE(${TARGET_UNIFIED_BACKEND} ${UNIFIED_SRCS})
+ADD_LIBRARY(${TARGET_LIBNAME_UNIFIED} SHARED
+    ${UNIFIED_SRCS}
+)
+ADD_EXECUTABLE(${TARGET_UNIFIED_BACKEND} "unified_backend.cc")
 # Target - includes
+TARGET_INCLUDE_DIRECTORIES(${TARGET_LIBNAME_UNIFIED} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../")
 TARGET_INCLUDE_DIRECTORIES(${TARGET_UNIFIED_BACKEND} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../")
 
-APPLY_PKG_CONFIG(${TARGET_UNIFIED_BACKEND} PUBLIC
+APPLY_PKG_CONFIG(${TARGET_LIBNAME_UNIFIED} PUBLIC
   APP_INSTALLERS_DEPS
   MANIFEST_PARSER_DEPS
   MINIZIP_DEPS
@@ -15,9 +18,17 @@ APPLY_PKG_CONFIG(${TARGET_UNIFIED_BACKEND} PUBLIC
   WGT_INSTALLER_DEPS
 )
 
-# Target - in-package deps
+# Target
+TARGET_LINK_LIBRARIES(${TARGET_UNIFIED_BACKEND} PRIVATE ${TARGET_LIBNAME_UNIFIED})
 SET_TARGET_PROPERTIES(${TARGET_UNIFIED_BACKEND} PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE")
 SET_TARGET_PROPERTIES(${TARGET_UNIFIED_BACKEND} PROPERTIES LINK_FLAGS "-pie")
 
 # Install
+INSTALL(TARGETS ${TARGET_LIBNAME_UNIFIED} DESTINATION ${LIB_INSTALL_DIR})
 INSTALL(TARGETS ${TARGET_UNIFIED_BACKEND} DESTINATION ${BINDIR})
+
+# Export devel package
+CONFIGURE_FILE(unified-installer.pc.in unified-installer.pc @ONLY)
+INSTALL(FILES unified-installer.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
+INSTALL(DIRECTORY ./ DESTINATION ${INCLUDEDIR}/app-installers/unified/
+        FILES_MATCHING PATTERN "*.h")
diff --git a/src/unified/installer_factory.cc b/src/unified/installer_factory.cc
new file mode 100644 (file)
index 0000000..ce8513a
--- /dev/null
@@ -0,0 +1,91 @@
+// Copyright (c) 2020 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.
+
+#include "unified/installer_factory.h"
+
+#include <unzip.h>
+
+#include <manifest_parser/utils/logging.h>
+#include <hybrid/hybrid_installer.h>
+#include <tpk/tpk_app_query_interface.h>
+#include <tpk/tpk_installer.h>
+#include <wgt/utils/wgt_app_query_interface.h>
+#include <wgt/wgt_installer.h>
+
+#include <memory>
+#include <string>
+
+#include "common/pkgmgr_interface.h"
+#include "common/pkgmgr_query.h"
+
+namespace {
+
+std::map<const char*, const char*> kTypeMap = {
+  {"res/wgt/config.xml", "wgt"},
+  {"config.xml", "wgt"},
+  {"tizen-manifest.xml", "tpk"},
+};
+
+std::string GetPkgTypeFromPkgid(const std::string& pkgid, uid_t uid) {
+  ci::PkgQueryInterface query(pkgid, uid);
+  return query.Type();
+}
+
+std::string GetPkgTypeFromPath(const std::string& path) {
+  std::string type;
+  unzFile uf = unzOpen(path.c_str());
+  if (!uf) {
+    LOG(ERROR) << "Failed to open zip file: " << path;
+    return type;
+  }
+  for (auto it : kTypeMap) {
+    if (unzLocateFile(uf, it.first, 0) == UNZ_OK) {
+      LOG(DEBUG) << "Found: " << it.first;
+      type = it.second;
+      break;
+    }
+  }
+  if (type.empty())
+    LOG(ERROR) << "Cannot get pkg type";
+  unzClose(uf);
+  return type;
+}
+
+}  // namespace
+
+namespace common_installer {
+
+std::unique_ptr<AppInstaller> InstallerFactory::CreateInstaller(
+    PkgMgrPtr pkgmgr) {
+  std::string type = GetPkgTypeFromPkgid(pkgmgr->GetRequestInfo(),
+      pkgmgr->GetUid());
+  if (type.empty())
+    type = GetPkgTypeFromPath(pkgmgr->GetRequestInfo());
+
+  if (type.empty())
+    return {};
+
+  std::unique_ptr<ci::AppInstaller> installer;
+  if (type == "tpk") {
+    // should be fixed.
+    tpk::TpkAppQueryInterface* tpk_aqi = new tpk::TpkAppQueryInterface();
+    pkgmgr->SetAppQueryInterface(tpk_aqi);
+    installer.reset(new tpk::TpkInstaller(pkgmgr));
+  } else {
+    // should be fixed
+    wgt::WgtAppQueryInterface* wgt_aqi = new wgt::WgtAppQueryInterface();
+    pkgmgr->SetAppQueryInterface(wgt_aqi);
+    if (wgt_aqi->IsHybridApplication(pkgmgr->GetRequestInfo(),
+            pkgmgr->GetUid())) {
+      LOG(INFO) << "Hybrid package detected";
+      installer.reset(new hybrid::HybridInstaller(pkgmgr));
+    } else {
+      installer.reset(new wgt::WgtInstaller(pkgmgr));
+    }
+  }
+
+  return installer;
+}
+
+}  // namespace common_installer
diff --git a/src/unified/installer_factory.h b/src/unified/installer_factory.h
new file mode 100644 (file)
index 0000000..16c5cb0
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright (c) 2020 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 UNIFIED_INSTALLER_FACTORY_H_
+#define UNIFIED_INSTALLER_FACTORY_H_
+
+#include <common/pkgmgr_interface.h>
+
+#include <memory>
+
+namespace common_installer {
+
+class AppInstaller;
+
+class InstallerFactory {
+ public:
+  static std::unique_ptr<AppInstaller> CreateInstaller(PkgMgrPtr pkgmgr);
+};
+
+}  // namespace common_installer
+
+#endif  // UNIFIED_INSTALLER_FACTORY_H_
diff --git a/src/unified/installer_runner.cc b/src/unified/installer_runner.cc
new file mode 100644 (file)
index 0000000..fa3a070
--- /dev/null
@@ -0,0 +1,56 @@
+// Copyright (c) 2020 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.
+
+#include "unified/installer_runner.h"
+
+#include <common/app_installer.h>
+#include <common/pkgmgr_interface.h>
+#include <manifest_parser/utils/logging.h>
+
+#include <vector>
+
+#include "unified/installer_factory.h"
+
+namespace common_installer {
+
+InstallerRunner::InstallerRunner(PkgMgrPtr pkgmgr) : pkgmgr_(pkgmgr) {
+  if (!Init())
+    LOG(ERROR) << "Initialize failed!";
+}
+
+AppInstaller::Result InstallerRunner::Run() {
+  AppInstaller::Result result = AppInstaller::Result::OK;
+  std::vector<std::unique_ptr<AppInstaller>>::iterator it =
+      installers_.begin();
+  for (; it != installers_.end(); ++it) {
+    result = (*it)->Process();
+    if (result != AppInstaller::Result::OK)
+      break;
+  }
+  if (it != installers_.end() && result == AppInstaller::Result::ERROR) {
+    do {
+      if ((*it)->Undo() != AppInstaller::Result::OK)
+        result = AppInstaller::Result::UNDO_ERROR;
+    } while (it-- != installers_.begin());
+  } else {
+    --it;
+    do {
+      if ((*it)->Clean() != AppInstaller::Result::OK)
+        result = AppInstaller::Result::CLEANUP_ERROR;
+    } while (it-- != installers_.begin());
+  }
+
+  return result;
+}
+
+bool InstallerRunner::Init() {
+  std::unique_ptr<AppInstaller> installer =
+      InstallerFactory::CreateInstaller(pkgmgr_);
+  // initialize installer list. it should handle multiple request later.
+  installers_.emplace_back(std::move(installer));
+
+  return true;
+}
+
+}  // namespace common_installer
diff --git a/src/unified/installer_runner.h b/src/unified/installer_runner.h
new file mode 100644 (file)
index 0000000..c822ec4
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright (c) 2020 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 UNIFIED_INSTALLER_RUNNER_H_
+#define UNIFIED_INSTALLER_RUNNER_H_
+
+#include <common/app_installer.h>
+#include <common/pkgmgr_interface.h>
+
+#include <memory>
+#include <vector>
+
+namespace common_installer {
+
+class InstallerRunner {
+ public:
+  explicit InstallerRunner(PkgMgrPtr pkgmgr);
+
+  AppInstaller::Result Run();
+
+ private:
+  bool Init();
+
+  PkgMgrPtr pkgmgr_;
+  std::vector<std::unique_ptr<AppInstaller>> installers_;
+};
+
+}  // namespace common_installer
+
+#endif  // UNIFIED_INSTALLER_RUNNER_H_
diff --git a/src/unified/unified-installer.pc.in b/src/unified/unified-installer.pc.in
new file mode 100644 (file)
index 0000000..3a93c99
--- /dev/null
@@ -0,0 +1,11 @@
+prefix=@PREFIX@
+exec_prefix=@PREFIX@
+libdir=@LIBDIR@
+includedir=@INCLUDEDIR@
+
+Name: unified-installer
+Description: Unified Installer library
+Version: @FULLVER@
+Requires: app-installers tpk-installer wgt-installer pkgmgr tpk-manifest-handlers manifest-parser
+Libs: -L${libdir} -lunified-installer
+Cflags: -I${includedir}/app-installers/
index 99de3093d93501d6e2d4bf7dfd71d04e00e57a60..7127a6901c28f1f3f5004a1006e41316a1d6dd10 100644 (file)
-// Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+// Copyright (c) 2020 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.
 
-#include <unzip.h>
+#include <common/app_installer.h>
 
-#include <common/pkgmgr_interface.h>
-#include <common/pkgmgr_query.h>
-#include <manifest_parser/utils/logging.h>
-#include <hybrid/hybrid_installer.h>
-#include <tpk/tpk_app_query_interface.h>
-#include <tpk/tpk_installer.h>
-#include <wgt/wgt_app_query_interface.h>
-#include <wgt/wgt_installer.h>
-
-#include <map>
-#include <memory>
-#include <string>
-#include <utility>
+#include "unified/installer_factory.h"
+#include "unified/installer_runner.h"
 
 namespace ci = common_installer;
 
-namespace {
-
-std::map<const char*, const char*> kTypeMap = {
-  {"res/wgt/config.xml", "wgt"},
-  {"config.xml", "wgt"},
-  {"tizen-manifest.xml", "tpk"},
-};
-
-std::string GetPkgTypeFromPkgid(const std::string& pkgid, uid_t uid) {
-  ci::PkgQueryInterface query(pkgid, uid);
-  return query.Type();
-}
-
-std::string GetPkgTypeFromPath(const std::string& path) {
-  std::string type;
-  unzFile uf = unzOpen(path.c_str());
-  if (!uf) {
-    LOG(ERROR) << "Failed to open zip file: " << path;
-    return type;
-  }
-  for (auto it : kTypeMap) {
-    if (unzLocateFile(uf, it.first, 0) == UNZ_OK) {
-      LOG(DEBUG) << "Found: " << it.first;
-      type = it.second;
-      break;
-    }
-  }
-  if (type.empty())
-    LOG(ERROR) << "Cannot get pkg type";
-  unzClose(uf);
-  return type;
-}
-
-}  // namespace
-
 int main(int argc, char** argv) {
   ci::PkgmgrInstaller pkgmgr_installer;
   auto pkgmgr = ci::PkgMgrInterface::Create(argc, argv, &pkgmgr_installer);
 
-  std::string type = GetPkgTypeFromPkgid(pkgmgr->GetRequestInfo(),
-      pkgmgr->GetUid());
-  if (type.empty())
-    type = GetPkgTypeFromPath(pkgmgr->GetRequestInfo());
-
-  if (type.empty())
-    return 1;
-
-  std::vector<std::unique_ptr<ci::AppInstaller>> installer_list;
-
-  std::unique_ptr<ci::AppInstaller> installer;
-  if (type == "tpk") {
-    tpk::TpkAppQueryInterface tpk_aqi;
-    pkgmgr->SetAppQueryInterface(&tpk_aqi);
-    installer.reset(new tpk::TpkInstaller(pkgmgr));
-  } else {
-    wgt::WgtAppQueryInterface wgt_aqi;
-    pkgmgr->SetAppQueryInterface(&wgt_aqi);
-    if (wgt_aqi.IsHybridApplication(pkgmgr->GetRequestInfo(),
-            pkgmgr->GetUid())) {
-      LOG(INFO) << "Hybrid package detected";
-      installer.reset(new hybrid::HybridInstaller(pkgmgr));
-    } else {
-      installer.reset(new wgt::WgtInstaller(pkgmgr));
-    }
-  }
-
-  // initialize installer list. it should handle multiple request later.
-  installer_list.emplace_back(std::move(installer));
-
-  ci::AppInstaller::Result result = ci::AppInstaller::Result::OK;
-  std::vector<std::unique_ptr<ci::AppInstaller>>::iterator it =
-      installer_list.begin();
-  for (; it != installer_list.end(); ++it) {
-    result = (*it)->Process();
-    if (result != ci::AppInstaller::Result::OK)
-      break;
-  }
-  if (it != installer_list.end() && result == ci::AppInstaller::Result::ERROR) {
-    do {
-      if ((*it)->Undo() != ci::AppInstaller::Result::OK)
-        result = ci::AppInstaller::Result::UNDO_ERROR;
-    } while (it-- != installer_list.begin());
-  } else {
-    --it;
-    do {
-      if ((*it)->Clean() != ci::AppInstaller::Result::OK)
-        result = ci::AppInstaller::Result::CLEANUP_ERROR;
-    } while (it-- != installer_list.begin());
-  }
+  ci::InstallerRunner runner(pkgmgr);
+  ci::AppInstaller::Result result = runner.Run();
 
   return (result == ci::AppInstaller::Result::OK) ? 0 : 1;
 }