Fixed can not install to sdcard
[framework/web/wrt-installer.git] / src / jobs / widget_install / job_widget_install.cpp
index 12f598c..692b458 100644 (file)
@@ -48,8 +48,6 @@
 #include <libiriwrapper.h>
 #include <pkg-manager/pkgmgr_signal.h>
 #include <app_manager.h>
-//#include <drm_client.h>
-#include <drm-oem-intel.h> //temporary code
 
 #include "root_parser.h"
 #include "widget_parser.h"
@@ -94,7 +92,7 @@ const size_t PACKAGE_ID_LENGTH = 10;
 static const DPL::String SETTING_VALUE_ENCRYPTION = L"encryption";
 static const DPL::String SETTING_VALUE_ENCRYPTION_ENABLE = L"enable";
 const DPL::String SETTING_VALUE_INSTALLTOEXT_NAME =
-    L"install-location-type";
+    L"install-location";
 const DPL::String SETTING_VALUE_INSTALLTOEXT_PREPER_EXT =
     L"prefer-external";
 
@@ -112,24 +110,6 @@ bool hasExtension(const std::string& filename, const std::string& extension)
     }
     return (0 == filename.compare(fileLen - extLen, extLen, extension));
 }
-
-bool checkTizenPkgIdExist(const std::string& tizenPkgId)
-{
-    std::string installPath =
-        std::string(GlobalConfig::GetUserInstalledWidgetPath()) +
-        "/" + tizenPkgId;
-    std::string preinstallPath =
-        std::string(GlobalConfig::GetUserPreloadedWidgetPath()) +
-        "/" + tizenPkgId;
-
-    struct stat dirStat;
-    if ((stat(installPath.c_str(), &dirStat) == 0) ||
-        (stat(preinstallPath.c_str(), &dirStat) == 0))
-    {
-        return true;
-    }
-    return false;
-}
 } // namespace anonymous
 
 namespace Jobs {
@@ -309,7 +289,7 @@ ConfigureResult JobWidgetInstall::PrePareInstallation(
     Try
     {
         std::string tempDir =
-            Jobs::WidgetInstall::createTempPath(m_jobStruct.m_preload);
+            Jobs::WidgetInstall::createTempPath(m_jobStruct.m_installMode == InstallMode::INSTALL_MODE_PRELOAD);
 
         m_isDRM = isDRMWidget(widgetPath);
         if (true == m_isDRM) {
@@ -383,7 +363,7 @@ void JobWidgetInstall::setTizenId(
         TizenPkgId pkgId = WidgetDAOReadOnly::generatePkgId();
         LogDebug("Checking if pkg id is unique");
         while (true) {
-            if (checkTizenPkgIdExist(DPL::ToUTF8String(pkgId))) {
+            if (!validateTizenPackageID(pkgId)) {
                 //path exist, chose another one
                 pkgId = WidgetDAOReadOnly::generatePkgId();
                 continue;
@@ -473,154 +453,150 @@ ConfigureResult JobWidgetInstall::ConfigureInstallation(
     const WrtDB::ConfigParserData &configData,
     const std::string &tempPath)
 {
-    WidgetUpdateInfo update = detectWidgetUpdate(
-            configData,
-            m_installerContext.
-                widgetConfig.webAppType,
-            m_installerContext.
-                widgetConfig.tzAppid);
-    ConfigureResult result = checkWidgetUpdate(update);
-
-    // Validate tizenId
-    regex_t reg;
-    if (regcomp(&reg, REG_TIZENID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
-        LogDebug("Regcomp failed");
+    ConfigureResult result = ConfigureResult::Failed;
+    WidgetUpdateInfo update;
+
+    // checking installed web application
+    Try {
+        // checking existing application is installed
+        WidgetDAOReadOnly dao(m_installerContext.widgetConfig.tzAppid);
+        // no excpetion means, it isn't update mode
+        getInstallerStruct().pkgmgrInterface->sendSignal(
+                PKGMGR_START_KEY,
+                PKGMGR_START_UPDATE);
+
+        update = detectWidgetUpdate(configData,
+                                    m_installerContext.widgetConfig.tzAppid);
+        result = checkWidgetUpdate(update);
+        if (result != ConfigureResult::Updated) {
+            // Already installed TizenAppId. return failed
+            return ConfigureResult::Failed_AlreadyInstalled;
+        }
+        m_installerContext.isUpdateMode = true;
     }
+    Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
+        result = ConfigureResult::Ok;
+        getInstallerStruct().pkgmgrInterface->sendSignal(
+                PKGMGR_START_KEY,
+                PKGMGR_START_INSTALL);
+        m_installerContext.isUpdateMode = false;
 
-    if ((regexec(&reg,
-                 DPL::ToUTF8String(m_installerContext.widgetConfig.tzAppid).
-                     c_str(),
-                     static_cast<size_t>(0), NULL, 0) == REG_NOMATCH) ||
-        (checkTizenPkgIdExist(DPL::ToUTF8String(m_installerContext.widgetConfig
-                                                    .tzPkgid)) &&
-         result != ConfigureResult::Updated))
-    {
-        //it is true when tizenId does not fit REG_TIZENID_PATTERN
-        LogError("tizen_id provided but not proper or pkgId directory exists");
-        //TODO(t.iwanek): appId is unique, what about installation of
-        // abcdefghij.test1 and abcdefghij.test2?
-        regfree(&reg);
-        return ConfigureResult::Failed_AlreadyInstalled;
+        if (!validateTizenApplicationID(
+            m_installerContext.widgetConfig.tzAppid))
+        {
+            LogError("tizen application ID is already used");
+            return ConfigureResult::Failed_InvalidConfig;
+        }
+        if (!validateTizenPackageID(m_installerContext.widgetConfig.tzPkgid)) {
+            LogError("tizen package ID is already used");
+            return ConfigureResult::Failed_AlreadyInstalled;
+        }
     }
-    regfree(&reg);
 
     configureWidgetLocation(widgetSource, tempPath);
 
     // Init installer context
     m_installerContext.installStep = InstallerContext::INSTALL_START;
     m_installerContext.job = this;
-    m_installerContext.existingWidgetInfo = update.existingWidgetInfo;
     m_installerContext.widgetConfig.shareHref = std::string();
 
     return result;
 }
 
-ConfigureResult JobWidgetInstall::checkWidgetUpdate(
-    const WidgetUpdateInfo &update)
+bool JobWidgetInstall::validateTizenApplicationID(
+    const WrtDB::TizenAppId &tizenAppId)
 {
-    LogInfo(
-        "Widget install/update: incoming guid = '" <<
-        update.incomingGUID << "'");
-    LogInfo(
-        "Widget install/update: incoming version = '" <<
-        update.incomingVersion << "'");
+    LogInfo("tizen application ID = [" << tizenAppId << "]");
 
-    // Check policy
-    WidgetUpdateMode::Type updateTypeCheckBit;
-
-    if (update.existingWidgetInfo.isExist == false) {
-        LogInfo("Widget info does not exist");
-        updateTypeCheckBit = WidgetUpdateMode::NotInstalled;
-
-        getInstallerStruct().pkgmgrInterface->sendSignal(
-                PKGMGR_START_KEY,
-                PKGMGR_START_INSTALL);
-
-    } else {
-        LogInfo("Widget info exists. appid: " <<
-                update.existingWidgetInfo.tzAppid);
-
-        getInstallerStruct().pkgmgrInterface->sendSignal(
-                PKGMGR_START_KEY,
-                PKGMGR_START_UPDATE);
+    regex_t reg;
+    if (regcomp(&reg, REG_TIZENID_PATTERN, REG_NOSUB | REG_EXTENDED) != 0) {
+        LogDebug("Regcomp failed");
+    }
 
-        TizenAppId tzAppid = update.existingWidgetInfo.tzAppid;
+    if (regexec(&reg, DPL::ToUTF8String(tizenAppId).c_str(), 0, NULL, 0)
+        == REG_NOMATCH)
+    {
+        regfree(&reg);
+        return false;
+    }
+    regfree(&reg);
+    return true;
+}
 
-        LogInfo("Widget model exists. tizen app id: " << tzAppid);
+bool JobWidgetInstall::validateTizenPackageID(
+    const WrtDB::TizenPkgId &tizenPkgId)
+{
+    std::string pkgId = DPL::ToUTF8String(tizenPkgId);
 
-        // Check running state
-        bool isRunning = false;
-        int retval = app_manager_is_running(DPL::ToUTF8String(
-                                            tzAppid).c_str(), &isRunning);
-        if (APP_MANAGER_ERROR_NONE != retval) {
-            LogError("Fail to get running state");
-            return ConfigureResult::Failed_WidgetRunning;
-        }
+    std::string installPath =
+        std::string(GlobalConfig::GetUserInstalledWidgetPath()) +
+        "/" + pkgId;
+    std::string preinstallPath =
+        std::string(GlobalConfig::GetUserPreloadedWidgetPath()) +
+        "/" + pkgId;
 
-        if (true == isRunning) {
-            // Must be deferred when update in progress
-            if (m_jobStruct.updateMode == WidgetUpdateMode::PolicyWac) {
-                LogInfo(
-                    "Widget is already running. Policy is update according to WAC");
+    struct stat dirStat;
+    if ((stat(installPath.c_str(), &dirStat) == 0) ||
+        (stat(preinstallPath.c_str(), &dirStat) == 0))
+    {
+        return false;
+    }
+    return true;
+}
 
-                return ConfigureResult::Deferred;
-            } else {
-                LogInfo(
-                    "Widget is already running. Policy is not update according to WAC");
+ConfigureResult JobWidgetInstall::checkWidgetUpdate(
+    const WidgetUpdateInfo &update)
+{
+    LogInfo("incoming version = '" << update.incomingVersion);
+    LogInfo("Tizen AppID = " << update.tzAppId);
+
+    // Check running state
+    bool isRunning = false;
+    int retval =
+        app_manager_is_running(DPL::ToUTF8String(update.tzAppId).c_str(),
+                               &isRunning);
+    if (APP_MANAGER_ERROR_NONE != retval || isRunning) {
+        LogError("Fail to get running state");
+        return ConfigureResult::Failed_WidgetRunning;
+    }
 
-                return ConfigureResult::Failed_WidgetRunning;
-            }
-        }
+    m_installerContext.widgetConfig.tzAppid = update.tzAppId;
 
-        m_installerContext.widgetConfig.tzAppid = tzAppid;
-        OptionalWidgetVersion existingVersion;
-        existingVersion = update.existingWidgetInfo.existingVersion;
-        OptionalWidgetVersion incomingVersion = update.incomingVersion;
-
-        updateTypeCheckBit = CalcWidgetUpdatePolicy(existingVersion,
-                                                    incomingVersion);
-        // Calc proceed flag
-        if ((m_jobStruct.updateMode & updateTypeCheckBit) > 0 ||
-            m_jobStruct.updateMode ==
-            WidgetUpdateMode::PolicyDirectoryForceInstall)
-        {
-            LogInfo("Whether widget policy allow proceed ok");
-            return ConfigureResult::Updated;
-        } else {
-            return ConfigureResult::Failed_LowerVersion;
-        }
+    if (isUpperVersion(update.existingVersion, update.incomingVersion) ||
+        m_jobStruct.m_installMode == InstallMode::INSTALL_MODE_DIRECTORY)
+    {
+        LogInfo("Whether widget policy allow proceed ok");
+        return ConfigureResult::Updated;
+    } else {
+        return ConfigureResult::Failed_LowerVersion;
     }
-    return ConfigureResult::Ok;
+
+    return ConfigureResult::Failed;
 }
 
-WidgetUpdateMode::Type JobWidgetInstall::CalcWidgetUpdatePolicy(
+bool JobWidgetInstall::isUpperVersion(
     const OptionalWidgetVersion &existingVersion,
-    const OptionalWidgetVersion &incomingVersion) const
+    const OptionalWidgetVersion &incomingVersion)
 {
-    // Widget is installed, check versions
+    LogInfo("Existing version = '" << *existingVersion);
+    LogInfo("Incoming version = '" << *incomingVersion);
+
     if (!existingVersion && !incomingVersion) {
-        return WidgetUpdateMode::ExistingVersionEqual;
+        return false;
     } else if (!existingVersion && !!incomingVersion) {
-        return WidgetUpdateMode::ExistingVersionNewer;
+        return false;
     } else if (!!existingVersion && !incomingVersion) {
-        return WidgetUpdateMode::ExistingVersionOlder;
+        return true;
     } else {
-        LogInfo("Existing widget: version = '" << *existingVersion << "'");
-
-        if (!existingVersion->IsWac() && !incomingVersion->IsWac()) {
-            return WidgetUpdateMode::BothVersionsNotStd;
-        } else if (!existingVersion->IsWac()) {
-            return WidgetUpdateMode::ExistingVersionNotStd;
-        } else if (!incomingVersion->IsWac()) {
-            return WidgetUpdateMode::IncomingVersionNotStd;
+        if (!existingVersion->IsWac() || !incomingVersion->IsWac()) {
+            return false;
         } else {
-            // Both versions are WAC-comparable. Do compare.
             if (*incomingVersion == *existingVersion) {
-                return WidgetUpdateMode::ExistingVersionEqual;
+                return false;
             } else if (*incomingVersion > *existingVersion) {
-                return WidgetUpdateMode::ExistingVersionOlder;
+                return true;
             } else {
-                return WidgetUpdateMode::ExistingVersionNewer;
+                return false;
             }
         }
     }
@@ -728,71 +704,22 @@ ConfigParserData JobWidgetInstall::getWidgetDataFromXML(
 
 WidgetUpdateInfo JobWidgetInstall::detectWidgetUpdate(
     const ConfigParserData &configInfo,
-    const WrtDB::WidgetType appType,
     const WrtDB::TizenAppId &tizenId)
 {
     LogInfo("Checking up widget package for config.xml...");
+    OptionalWidgetVersion incomingVersion;
 
-    DPL::OptionalString widgetGUID;
-    OptionalWidgetVersion widgetVersion;
-
-    // Check widget id
-    widgetGUID = configInfo.widget_id;
-
-    if (widgetGUID.IsNull()) {
-        LogWarning("Installed widget has no GUID");
-        return WidgetUpdateInfo();
-    }
-
-    LogDebug("Installed widget GUID: " << *widgetGUID);
-
-    // Locate widget ID with this GUID
-    // Incoming widget version
     if (!configInfo.version.IsNull()) {
-        widgetVersion =
+        incomingVersion =
             DPL::Optional<WidgetVersion>(
                 WidgetVersion(*configInfo.version));
     }
 
-    if (appType == APP_TYPE_WAC20) {
-        Try
-        {
-            // Search widget handle by GUID
-            WidgetDAOReadOnly dao(widgetGUID);
-            return WidgetUpdateInfo(
-                       widgetGUID,
-                       widgetVersion,
-                       WidgetUpdateInfo::ExistingWidgetInfo(
-                           dao.getTzAppId(), dao.getVersion()));
-        }
-        Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
-        {
-            // GUID isn't installed
-            return WidgetUpdateInfo(
-                       widgetGUID,
-                       widgetVersion,
-                       WidgetUpdateInfo::ExistingWidgetInfo());
-        }
-    } else {
-        Try
-        {
-            // Search widget handle by appId
-            WidgetDAOReadOnly dao(tizenId);
-            return WidgetUpdateInfo(
-                       widgetGUID,
-                       widgetVersion,
-                       WidgetUpdateInfo::ExistingWidgetInfo(
-                           dao.getTzAppId(), dao.getVersion()));
-        }
-        Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
-        {
-            // GUID isn't installed
-            return WidgetUpdateInfo(
-                       widgetGUID,
-                       widgetVersion,
-                       WidgetUpdateInfo::ExistingWidgetInfo());
-        }
-    }
+    WidgetDAOReadOnly dao(tizenId);
+    return WidgetUpdateInfo(
+        dao.getTzAppId(),
+        WidgetVersion(*dao.getVersion()),
+        incomingVersion);
 }
 
 void JobWidgetInstall::SendProgress()
@@ -836,10 +763,10 @@ void JobWidgetInstall::SendFinishedSuccess()
     sync();
 
     if (INSTALL_LOCATION_TYPE_EXTERNAL == m_installerContext.locationType) {
-        if (false == m_installerContext.existingWidgetInfo.isExist) {
-            WidgetInstallToExtSingleton::Instance().postInstallation(true);
-        } else {
+        if (m_installerContext.isUpdateMode) {
             WidgetInstallToExtSingleton::Instance().postUpgrade(true);
+        } else {
+            WidgetInstallToExtSingleton::Instance().postInstallation(true);
         }
         WidgetInstallToExtSingleton::Instance().deinitialize();
     }
@@ -964,8 +891,7 @@ WrtDB::PackagingType JobWidgetInstall::checkPackageType(
     const std::string &tempPath)
 {
     // Check installation type (direcotory/ or config.xml or widget.wgt)
-    if (WidgetUpdateMode::PolicyDirectoryForceInstall ==
-        m_jobStruct.updateMode)
+    if (m_jobStruct.m_installMode == InstallMode::INSTALL_MODE_DIRECTORY)
     {
         LogDebug("Install directly from directory");
         return PKG_TYPE_DIRECTORY_WEB_APP;
@@ -1102,7 +1028,7 @@ void JobWidgetInstall::setInstallLocationType(
 {
     m_installerContext.locationType = INSTALL_LOCATION_TYPE_NOMAL;
 
-    if (true == m_jobStruct.m_preload) {
+    if (m_jobStruct.m_installMode == InstallMode::INSTALL_MODE_PRELOAD) {
         m_installerContext.locationType =
             INSTALL_LOCATION_TYPE_PRELOAD;
     } else {
@@ -1120,54 +1046,17 @@ void JobWidgetInstall::setInstallLocationType(
     }
 }
 
-bool JobWidgetInstall::isDRMWidget(std::string widgetPath)
+bool JobWidgetInstall::isDRMWidget(std::string /*widgetPath*/)
 {
-    /* TODO :
-     * drm_bool_type_e is_drm_file = DRM_UNKNOWN;
-     * int ret = -1;
-     *
-     * ret = drm_is_drm_file(widgetPath.c_str(), &is_drm_file);
-     * if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_drm_file) {
-     */
-
-    /* blow code temporary code for drm. */
-    int ret = drm_oem_intel_isDrmFile(const_cast<char*>(widgetPath.c_str()));
-    if (1 == ret) {
-        return true;
-    } else {
-        return false;
-    }
+    /* TODO */
+    return false;
 }
 
-bool JobWidgetInstall::DecryptDRMWidget(std::string widgetPath,
-                                        std::string destPath)
+bool JobWidgetInstall::DecryptDRMWidget(std::string /*widgetPath*/,
+                                        std::string /*destPath*/)
 {
-    /* TODO :
-     * drm_trusted_sapps_decrypt_package_info_s package_info;
-     *
-     * strncpy(package_info.sadcf_filepath, widgetPath.c_str(),
-     *      sizeof(package_info.sadcf_filepath));
-     * strncpy(package_info.decrypt_filepath, destPath.c_str(),
-     *      sizeof(package_info.decrypt_filepath));
-     *
-     * drm_trusted_request_type_e requestType =
-     *  DRM_TRUSTED_REQ_TYPE_SAPPS_DECRYPT_PACKAGE;
-     *
-     * int ret = drm_trusted_handle_request(requestType,
-     *                                   (void *)&package_info, NULL);
-     * if (DRM_TRUSTED_RETURN_SUCCESS == ret) {
-     *  return true;
-     * } else {
-     *  return false;
-     * }
-     */
-    if (drm_oem_intel_decrypt_package(const_cast<char*>(widgetPath.c_str()),
-                                      const_cast<char*>(destPath.c_str())) != 0)
-    {
-        return true;
-    } else {
-        return false;
-    }
+    /* TODO */ 
+    return false;
 }
 } //namespace WidgetInstall
 } //namespace Jobs