From ff78a04f7c2a3f91f5d0ebfb1dc3a68d5e739026 Mon Sep 17 00:00:00 2001 From: Jaemin Ryu Date: Tue, 19 Apr 2016 10:12:03 +0900 Subject: [PATCH] PackageManager: make it singleton Change-Id: Ia1ee58b1af50ce8587299ee324d576b6d84a883c Signed-off-by: Jaemin Ryu --- libs/application.cpp | 19 +-------- policy/application.hxx | 3 -- server/application.cpp | 33 ++++++--------- server/packman.cpp | 107 +++++++++---------------------------------------- server/packman.h | 26 +++++++----- 5 files changed, 50 insertions(+), 138 deletions(-) diff --git a/libs/application.cpp b/libs/application.cpp index a4aec35..80a51d4 100644 --- a/libs/application.cpp +++ b/libs/application.cpp @@ -93,24 +93,6 @@ std::vector Application::getInstalledPackageList() } } -bool Application::isApplicationInstallationEnabled(const std::string& appid) -{ - try { - return context->methodCall("Application::isApplicationInstallationEnabled", appid); - } catch (runtime::Exception& e) { - return false; - } -} - -bool Application::isApplicationUninstallationEnabled(const std::string& appid) -{ - try { - return context->methodCall("Application::isApplicationInstallationEnabled", appid); - } catch (runtime::Exception& e) { - return false; - } -} - bool Application::isApplicationRunning(const std::string& appid) { try { @@ -200,4 +182,5 @@ int Application::wipeApplicationData(const std::string& appid) return -1; } } + } // namespace DevicePolicyManager diff --git a/policy/application.hxx b/policy/application.hxx index 9f6f49b..a223547 100644 --- a/policy/application.hxx +++ b/policy/application.hxx @@ -44,9 +44,6 @@ public: int installPackage(const std::string& pkgpath); int uninstallPackage(const std::string& pkgid); - bool isApplicationInstallationEnabled(const std::string& appid); - bool isApplicationUninstallationEnabled(const std::string& appid); - bool isApplicationInstalled(const std::string& appid); bool isApplicationRunning(const std::string& appid); bool isPackageInstalled(const std::string& pkgid); diff --git a/server/application.cpp b/server/application.cpp index a026f13..cae6d93 100644 --- a/server/application.cpp +++ b/server/application.cpp @@ -45,8 +45,6 @@ Application::Application(PolicyControlContext& ctxt) : context.registerParametricMethod(this, (int)(Application::enableApplication)(std::string)); context.registerParametricMethod(this, (int)(Application::getApplicationState)(std::string)); context.registerParametricMethod(this, (int)(Application::setApplicationState)(std::string, int)); - context.registerParametricMethod(this, (bool)(Application::isApplicationInstallationEnabled)(std::string)); - context.registerParametricMethod(this, (bool)(Application::isApplicationUninstallationEnabled)(std::string)); context.registerParametricMethod(this, (int)(Application::startApplication)(std::string)); context.registerParametricMethod(this, (int)(Application::stopApplication)(std::string)); context.registerParametricMethod(this, (int)(Application::wipeApplicationData)(std::string)); @@ -79,23 +77,14 @@ bool Application::getApplicationUninstallationMode() std::vector Application::getInstalledPackageList() { try { - return PackageManager::getInstalledPackageList(context.getPeerUid()); + PackageManager& packman = PackageManager::instance(); + return packman.getInstalledPackageList(context.getPeerUid()); } catch (runtime::Exception& e) { ERROR("Failed to retrieve package info installed in the devioce"); return std::vector(); } } -bool Application::isApplicationInstallationEnabled(const std::string& appid) -{ - return true; -} - -bool Application::isApplicationUninstallationEnabled(const std::string& appid) -{ - return true; -} - bool Application::isApplicationInstalled(const std::string& appid) { try { @@ -128,7 +117,8 @@ bool Application::isPackageInstalled(const std::string& pkgid) int Application::installPackage(const std::string& pkgpath) { try { - PackageManager::installPackage(pkgpath, context.getPeerUid()); + PackageManager& packman = PackageManager::instance(); + packman.installPackage(pkgpath, context.getPeerUid()); } catch (runtime::Exception& e) { ERROR("Exception in Package Id"); return -1; @@ -140,7 +130,8 @@ int Application::installPackage(const std::string& pkgpath) int Application::uninstallPackage(const std::string& pkgid) { try { - PackageManager::uninstallPackage(pkgid, context.getPeerUid()); + PackageManager& packman = PackageManager::instance(); + packman.uninstallPackage(pkgid, context.getPeerUid()); } catch (runtime::Exception& e) { ERROR("Exception in Package Id"); return -1; @@ -157,8 +148,8 @@ int Application::disableApplication(const std::string& appid) launchpad.terminate(appid); // Notify user that the app hass terminated due to the policy } - - PackageManager::deactivatePackage(appid, context.getPeerUid()); + PackageManager& packman = PackageManager::instance(); + packman.deactivatePackage(appid, context.getPeerUid()); } catch (runtime::Exception& e) { return -1; } @@ -169,7 +160,8 @@ int Application::disableApplication(const std::string& appid) int Application::enableApplication(const std::string& appid) { try { - PackageManager::activatePackage(appid, context.getPeerUid()); + PackageManager& packman = PackageManager::instance(); + packman.activatePackage(appid, context.getPeerUid()); } catch (runtime::Exception& e) { return -1; } @@ -211,9 +203,10 @@ int Application::stopApplication(const std::string& appid) int Application::wipeApplicationData(const std::string& appid) { try { - PackageManager::wipePackageData(appid, context.getPeerUid()); + PackageManager& packman = PackageManager::instance(); + packman.wipePackageData(appid, context.getPeerUid()); } catch (runtime::Exception& e) { - ERROR("Exception in Package Id"); + ERROR("Exception on wiping package data"); return -1; } diff --git a/server/packman.cpp b/server/packman.cpp index da329e7..2075485 100644 --- a/server/packman.cpp +++ b/server/packman.cpp @@ -56,13 +56,6 @@ int PackageEventCallback(uid_t uid, int id, const char* type, const char* name, return 0; } - void PackageSizeCollector(pkgmgr_client* pc, const char *pkgid, const pkg_size_info_t *info, void *data) - { - pkg_size_info_t *i = static_cast(data); - - *i = *info; - } - } // namespace PackageInfo::PackageInfo(const std::string& pkgid, uid_t uid) : @@ -149,44 +142,24 @@ std::string AppInfo::getPackageName() const return name; } -long long AppInfo::getCodeSize() const +PackageManager::PackageManager() : + nativeHandle(nullptr) { - pkgmgr_client *pc = ::pkgmgr_client_new(PC_REQUEST); - if (pc == nullptr) { - ERROR("Error in pkgmgr_client_new"); - throw runtime::Exception("Unexpected operation"); - } - - pkg_size_info_t size; - int ret = ::pkgmgr_client_usr_get_package_size_info(pc, appid.c_str(), PackageSizeCollector, &size, user); - if (ret != PKGMGR_R_OK) { - ::pkgmgr_client_free(pc); - ERROR("Error in pkgmgr_client_get_package_size_info"); - throw runtime::Exception("Unexpected operation"); + nativeHandle = ::pkgmgr_client_new(PC_REQUEST); + if (nativeHandle == nullptr) { + throw runtime::Exception("No package manager instance"); } - - ::pkgmgr_client_free(pc); - return size.app_size; } -long long AppInfo::getDataSize() const +PackageManager::~PackageManager() { - pkgmgr_client *pc = ::pkgmgr_client_new(PC_REQUEST); - if (pc == nullptr) { - ERROR("Error in pkgmgr_client_new"); - return -1; - } - - pkg_size_info_t size; - int ret = ::pkgmgr_client_usr_get_package_size_info(pc, appid.c_str(), PackageSizeCollector, &size, user); - if (ret != PKGMGR_R_OK) { - ::pkgmgr_client_free(pc); - ERROR("Error in pkgmgr_client_get_package_size_info"); - return -1; - } + ::pkgmgr_client_free(nativeHandle); +} - ::pkgmgr_client_free(pc); - return size.data_size; +PackageManager& PackageManager::instance() +{ + static PackageManager __instance__; + return __instance__; } PackageInfo PackageManager::getPackageInfo(const std::string& pkgid, const uid_t user) @@ -198,56 +171,30 @@ void PackageManager::activatePackage(const std::string& pkgid, const uid_t user) { PackageInfo package(pkgid, user); - pkgmgr_client *pc = ::pkgmgr_client_new(PC_REQUEST); - if (pc == nullptr) { - throw runtime::Exception("Package Manager Error"); - } - - if (::pkgmgr_client_usr_activate(pc, package.getType().c_str(), pkgid.c_str(), user) != PKGMGR_R_OK) { - ::pkgmgr_client_free(pc); - + if (::pkgmgr_client_usr_activate(nativeHandle, package.getType().c_str(), pkgid.c_str(), user) != PKGMGR_R_OK) { + ERROR("Failed to activate package: " + pkgid); throw runtime::Exception("Operation failed"); } - - ::pkgmgr_client_free(pc); } void PackageManager::deactivatePackage(const std::string& pkgid, const uid_t user) { PackageInfo package(pkgid, user); - pkgmgr_client *pc = ::pkgmgr_client_new(PC_REQUEST); - if (pc == nullptr) { - throw runtime::Exception("Package Manager Error"); - } - - std::cout << "Package Type: " << package.getType() << std::endl; - int ret = pkgmgr_client_usr_deactivate(pc, package.getType().c_str(), pkgid.c_str(), user); + int ret = pkgmgr_client_usr_deactivate(nativeHandle, package.getType().c_str(), pkgid.c_str(), user); if (ret != PKGMGR_R_OK) { - ::pkgmgr_client_free(pc); ERROR("Failed to deactivate package: " + std::to_string(ret)); throw runtime::Exception("Operation failed"); } - - ::pkgmgr_client_free(pc); } void PackageManager::installPackage(const std::string& pkgpath, const uid_t user) { - pkgmgr_client *pc = ::pkgmgr_client_new(PC_REQUEST); - if (pc == nullptr) { - ERROR("Error in pkgmgr_client_new"); - throw runtime::Exception("Package manager exception"); - } - - int ret = ::pkgmgr_client_usr_install(pc, NULL, NULL, pkgpath.c_str(), NULL, PM_QUIET, PackageEventCallback, nullptr, user); + int ret = ::pkgmgr_client_usr_install(nativeHandle, NULL, NULL, pkgpath.c_str(), NULL, PM_QUIET, PackageEventCallback, nullptr, user); if (ret != PKGMGR_R_OK) { - ::pkgmgr_client_free(pc); ERROR("Error in pkgmgr_client_install"); throw runtime::Exception("Package installation failed"); } - - // pkgmgr_client handle will be destroyed in the callback when package installation is completed } void PackageManager::uninstallPackage(const std::string& pkgid, const uid_t user) @@ -257,38 +204,22 @@ void PackageManager::uninstallPackage(const std::string& pkgid, const uid_t user PackageInfo pkgInfo(pkgid); pkgtype = pkgInfo.getType(); - pkgmgr_client *pc = ::pkgmgr_client_new(PC_REQUEST); - if (pc == nullptr) { - throw runtime::Exception("Package manager exception"); - ERROR("Error in pkgmgr_client_new"); - } - - int ret = ::pkgmgr_client_usr_uninstall(pc, pkgtype.c_str(), pkgid.c_str(), PM_QUIET, PackageEventCallback, nullptr, user); + int ret = ::pkgmgr_client_usr_uninstall(nativeHandle, pkgtype.c_str(), pkgid.c_str(), PM_QUIET, PackageEventCallback, nullptr, user); if (ret < PKGMGR_R_OK) { - ::pkgmgr_client_free(pc); ERROR("Error in pkgmgr_client_uninstall"); throw runtime::Exception("Package manager exception"); } - - // pkgmgr_client handle will be destroyed in the callback when package installation is completed } void PackageManager::wipePackageData(const std::string& pkgid, const uid_t user) { PackageInfo package(pkgid); - pkgmgr_client *pc = ::pkgmgr_client_new(PC_REQUEST); - if (pc == nullptr) { - throw runtime::Exception("Invalid package manager"); - } - - int ret = ::pkgmgr_client_usr_clear_user_data(pc, package.getType().c_str(), pkgid.c_str(), PM_QUIET, user); + int ret = ::pkgmgr_client_usr_clear_user_data(nativeHandle, package.getType().c_str(), pkgid.c_str(), PM_QUIET, user); if (ret != PKGMGR_R_OK) { - ::pkgmgr_client_free(pc); + ERROR("Error in pkgmgr_clear_user_data"); throw runtime::Exception("Operation failed"); } - - ::pkgmgr_client_free(pc); } std::vector PackageManager::getInstalledPackageList(const uid_t user) diff --git a/server/packman.h b/server/packman.h index cd9f7f5..3a360db 100644 --- a/server/packman.h +++ b/server/packman.h @@ -47,8 +47,6 @@ std::string getPackageName() const; std::string getPackageType() const; std::string getVersion() const; - long long getCodeSize() const; - long long getDataSize() const; private: uid_t user; @@ -58,12 +56,22 @@ class PackageManager { public: - static PackageInfo getPackageInfo(const std::string& pkgid, const uid_t user); - static void activatePackage(const std::string& pkgid, const uid_t user); - static void deactivatePackage(const std::string& pkgid, const uid_t user); - static void installPackage(const std::string& pkgpath, const uid_t user); - static void uninstallPackage(const std::string& pkgid, const uid_t user); - static void wipePackageData(const std::string& pkgid, const uid_t user); - static std::vector getInstalledPackageList(const uid_t user); + PackageInfo getPackageInfo(const std::string& pkgid, const uid_t user); + void activatePackage(const std::string& pkgid, const uid_t user); + void deactivatePackage(const std::string& pkgid, const uid_t user); + void installPackage(const std::string& pkgpath, const uid_t user); + void uninstallPackage(const std::string& pkgid, const uid_t user); + void wipePackageData(const std::string& pkgid, const uid_t user); + + std::vector getInstalledPackageList(const uid_t user); + + static PackageManager& instance(); + +private: + PackageManager(); + ~PackageManager(); + +private: + pkgmgr_client *nativeHandle; }; #endif // __DPM_PACKMAN_H__ -- 2.7.4