Change pkgname to appid for docomo request. PART1
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / job_widget_install.cpp
index 2dc371e..9e56959 100644 (file)
@@ -60,7 +60,6 @@
 #include <widget_install/task_ace_check.h>
 #include <widget_install/task_smack.h>
 #include <widget_install/task_manifest_file.h>
-#include <widget_install/task_private_storage.h>
 #include <widget_install/task_prepare_files.h>
 #include <widget_install/task_recovery.h>
 #include <widget_install/task_install_ospsvc.h>
 #include <widget_install/task_certificates.h>
 #include <widget_install/task_unzip.h>
 #include <widget_install/task_commons.h>
+
 #include <widget_install/task_plugins_copy.h>
 
 #include <widget_install/widget_install_errors.h>
 #include <widget_install/widget_install_context.h>
+#include <widget_install_to_external.h>
 
 using namespace WrtDB;
 
@@ -84,10 +85,15 @@ const char * const CONFIG_XML = "config.xml";
 const char * const WITH_OSP_XML = "res/wgt/config.xml";
 
 //allowed: a-z, A-Z, 0-9
-const char* REG_TIZENID_PATTERN = "^[a-zA-Z0-9]{10}$";
+const char* REG_TIZENID_PATTERN = "^[a-zA-Z0-9]{10}.[a-zA-Z0-9]{1,}$";
+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";
+const DPL::String SETTING_VALUE_INSTALLTOEXT_PREPER_EXT =
+    L"prefer-external";
 
 class InstallerTaskFail :
     public DPL::TaskDecl<InstallerTaskFail>
@@ -128,6 +134,22 @@ 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 {
@@ -153,24 +175,28 @@ JobWidgetInstall::JobWidgetInstall(std::string const &widgetPath,
 
         // Create installation tasks
         if (m_installerContext.widgetConfig.packagingType !=
-                WrtDB::PKG_TYPE_HOSTED_WEB_APP || !m_isDRM) {
+                WrtDB::PKG_TYPE_DIRECTORY_WEB_APP &&
+            m_installerContext.widgetConfig.packagingType !=
+                WrtDB::PKG_TYPE_HOSTED_WEB_APP &&
+            !m_isDRM)
+        {
             AddTask(new TaskUnzip(m_installerContext));
         }
 
         AddTask(new TaskWidgetConfig(m_installerContext));
-        if (m_installerContext.widgetConfig.packagingType ==
-                WrtDB::PKG_TYPE_HOSTED_WEB_APP ) {
+        if (m_installerContext.widgetConfig.packagingType  ==
+                WrtDB::PKG_TYPE_HOSTED_WEB_APP)
+        {
             AddTask(new TaskPrepareFiles(m_installerContext));
         }
         AddTask(new TaskCertify(m_installerContext));
         if (m_needEncryption) {
             AddTask(new TaskEncryptResource(m_installerContext));
         }
+
         AddTask(new TaskFileManipulation(m_installerContext));
         // TODO: Update progress information for this task
 
-        AddTask(new TaskPrivateStorage(m_installerContext));
-
         //This is sort of quick solution, because ACE verdicts are based upon
         //data from DAO (DB). So AceCheck for now has to be AFTER DbUpdate
         //task.
@@ -189,19 +215,28 @@ JobWidgetInstall::JobWidgetInstall(std::string const &widgetPath,
         LogInfo("Configure installation updated");
         LogInfo("Widget Update");
         if (m_installerContext.widgetConfig.packagingType !=
-                WrtDB::PKG_TYPE_HOSTED_WEB_APP || !m_isDRM) {
+                WrtDB::PKG_TYPE_HOSTED_WEB_APP &&
+            m_installerContext.widgetConfig.packagingType !=
+                WrtDB::PKG_TYPE_DIRECTORY_WEB_APP &&
+            !m_isDRM)
+        {
             AddTask(new TaskUnzip(m_installerContext));
         }
 
         AddTask(new TaskWidgetConfig(m_installerContext));
 
         if (m_installerContext.widgetConfig.packagingType ==
-                WrtDB::PKG_TYPE_HOSTED_WEB_APP ) {
+                WrtDB::PKG_TYPE_HOSTED_WEB_APP)
+        {
             AddTask(new TaskPrepareFiles(m_installerContext));
         }
 
         AddTask(new TaskCertify(m_installerContext));
-        AddTask(new TaskUpdateFiles(m_installerContext));
+        if (m_installerContext.widgetConfig.packagingType !=
+            WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
+        {
+            AddTask(new TaskUpdateFiles(m_installerContext));
+        }
 
         /* TODO : To backup file, save md5 values */
         AddTask(new TaskSmack(m_installerContext));
@@ -211,7 +246,11 @@ JobWidgetInstall::JobWidgetInstall(std::string const &widgetPath,
                 PKG_TYPE_HYBRID_WEB_APP) {
             AddTask(new TaskInstallOspsvc(m_installerContext));
         }
-        AddTask(new TaskRemoveBackupFiles(m_installerContext));
+        if (m_installerContext.widgetConfig.packagingType !=
+            WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
+        {
+            AddTask(new TaskRemoveBackupFiles(m_installerContext));
+        }
         AddTask(new TaskPluginsCopy(m_installerContext));
         AddTask(new TaskDatabase(m_installerContext));
         AddTask(new TaskAceCheck(m_installerContext));
@@ -242,9 +281,10 @@ JobWidgetInstall::ConfigureResult JobWidgetInstall::PrePareInstallation(
 
     Try
     {
-        std::string tempDir = Jobs::WidgetInstall::createTempPath();
+        std::string tempDir =
+            Jobs::WidgetInstall::createTempPath(m_jobStruct.m_preload);
 
-        bool m_isDRM = isDRMWidget(widgetPath);
+        m_isDRM = isDRMWidget(widgetPath);
         if (true == m_isDRM) {
             LogDebug("decrypt DRM widget");
             if(DecryptDRMWidget(widgetPath, tempDir)) {
@@ -257,15 +297,21 @@ JobWidgetInstall::ConfigureResult JobWidgetInstall::PrePareInstallation(
 
         m_installerContext.widgetConfig.packagingType =
             checkPackageType(widgetPath, tempDir);
-        ConfigParserData configData = getWidgetDataFromXML(widgetPath, tempDir,
-                m_installerContext.widgetConfig.packagingType, m_isDRM);
+        ConfigParserData configData = getWidgetDataFromXML(
+            widgetPath,
+            tempDir,
+            m_installerContext.widgetConfig.packagingType,
+            m_isDRM);
         LogDebug("widget packaging type : " <<
                 m_installerContext.widgetConfig.packagingType.pkgType);
-        WidgetUpdateInfo update = detectWidgetUpdate(configData);
+
+        setTizenId(configData);
+        setApplicationType(configData);
         m_needEncryption = detectResourceEncryption(configData);
+        setInstallLocationType(configData);
 
         // Configure installation
-        result = ConfigureInstallation(widgetPath, configData, update, tempDir);
+        result = ConfigureInstallation(widgetPath, configData, tempDir);
     }
     Catch(Exceptions::ExtractFileFailed)
     {
@@ -276,69 +322,102 @@ JobWidgetInstall::ConfigureResult JobWidgetInstall::PrePareInstallation(
     return result;
 }
 
-bool JobWidgetInstall::setTizenId(
-        const WrtDB::ConfigParserData &configInfo, ConfigureResult result)
+void JobWidgetInstall::setTizenId(
+        const WrtDB::ConfigParserData &configInfo)
 {
-    regex_t reg;
-    regcomp(&reg, REG_TIZENID_PATTERN, REG_NOSUB | REG_EXTENDED);
-    struct stat dirStat;
-    if(!!configInfo.tizenId) {
-        LogDebug("Setting tizenId provided in config.xml: " << configInfo.tizenId);
-        if ((regexec(&reg, DPL::ToUTF8String(*(configInfo.tizenId)).c_str(),
-             static_cast<size_t>(0), NULL, 0) != REG_NOERROR) ||
-            ((stat((std::string(GlobalConfig::GetUserInstalledWidgetPath()) + "/"
-                   + DPL::ToUTF8String(*(configInfo.tizenId))).c_str(), &dirStat) == 0)
-             && result != ConfigureResult::Updated))
-        {
-            //it is true when tizenId does not fit REG_TIZENID_PATTERN
-            LogError("tizen_id provided but not proper.");
-            regfree(&reg);
-            return false;
-        }
-        m_installerContext.widgetConfig.pkgname = configInfo.tizenId;
+    bool shouldMakeAppid = false;
+    using namespace PackageManager;
+    if(!!configInfo.tizenAppId) {
+        LogDebug("Setting tizenAppId provided in config.xml: " <<
+                configInfo.tizenAppId);
+
+        m_installerContext.widgetConfig.tzAppid = *configInfo.tizenAppId;
+        //check package id.
+        if(!!configInfo.tizenPkgId) {
+            LogDebug("Setting tizenPkgId provided in config.xml: " <<
+                    configInfo.tizenPkgId);
 
+            m_installerContext.widgetConfig.tzPkgid = *configInfo.tizenPkgId;
+        } else {
+            std::string appid = DPL::ToUTF8String(*configInfo.tizenAppId);
+            if(appid.length() > PACKAGE_ID_LENGTH) {
+                m_installerContext.widgetConfig.tzPkgid =
+                    DPL::FromUTF8String(appid.substr(PACKAGE_ID_LENGTH));
+            } else {
+                m_installerContext.widgetConfig.tzPkgid =
+                    *configInfo.tizenAppId;
+            }
+            shouldMakeAppid = true;
+        }
     } else {
-        WidgetPkgName tizenId = WidgetDAOReadOnly::generateTizenId();
-
-        // only for installation, not for update
-        if (result == ConfigureResult::Ok) {
-            //check if there is package with same name and if generate different name
-            std::string path = GlobalConfig::GetUserInstalledWidgetPath();
-            path += "/";
-
-            std::ostringstream newPath;
-            newPath << path << tizenId;
-
-            LogDebug("Checking if tizen id is unique");
-            while (true) {
-                if (stat(newPath.str().c_str(), &dirStat) == 0) {
-                    //path exist, chose another one
-                    tizenId = WidgetDAOReadOnly::generateTizenId();
-                    newPath.str("");
-                    newPath << path << tizenId;
-                    continue;
+        shouldMakeAppid = true;
+        TizenPkgId pkgId = WidgetDAOReadOnly::generatePkgId();
+        LogDebug("Checking if pkg id is unique");
+        while (true) {
+            if (checkTizenPkgIdExist(DPL::ToUTF8String(pkgId))) {
+                //path exist, chose another one
+                pkgId = WidgetDAOReadOnly::generatePkgId();
+                continue;
+            }
+            break;
+        }
+        m_installerContext.widgetConfig.tzPkgid = pkgId;
+        LogInfo("tizen_id name was generated by WRT: " <<
+                m_installerContext.widgetConfig.tzPkgid);
+
+    }
+
+    if(shouldMakeAppid == true) {
+        DPL::OptionalString name;
+        DPL::OptionalString defaultLocale = configInfo.defaultlocale;
+
+        FOREACH(localizedData, configInfo.localizedDataSet)
+        {
+            Locale i = localizedData->first;
+            if (!!defaultLocale) {
+                if (defaultLocale == i) {
+                    name = localizedData->second.name;
+                    break;
                 }
+
+            } else {
+                name = localizedData->second.name;
                 break;
             }
-
-            m_installerContext.widgetConfig.pkgname = tizenId;
         }
-        LogInfo("tizen_id name was generated by WRT: " << tizenId);
+        if (!!name) {
+            LogDebug("Name : " << name);
+            std::ostringstream appid;
+            appid << m_installerContext.widgetConfig.tzPkgid << "." << name;
+            LogDebug("tizen appid was generated by WRT : " << appid.str());
+            m_installerContext.widgetConfig.tzAppid =
+                DPL::FromUTF8String(appid.str());
+        } else {
+            m_installerContext.widgetConfig.tzAppid =
+                m_installerContext.widgetConfig.tzPkgid;
+        }
     }
-    regfree(&reg);
 
-    LogInfo("Tizen Id : " << m_installerContext.widgetConfig.pkgname);
+    // send start signal of pkgmgr
+    getInstallerStruct().pkgmgrInterface->setPkgname(DPL::ToUTF8String(
+                m_installerContext.widgetConfig.tzAppid));
+    getInstallerStruct().pkgmgrInterface->sendSignal(
+            PKGMGR_START_KEY,
+            PKGMGR_START_INSTALL);
+
+    LogInfo("Tizen App Id : " << m_installerContext.widgetConfig.tzAppid);
+    LogInfo("Tizen Pkg Id : " << m_installerContext.widgetConfig.tzPkgid);
     LogInfo("W3C Widget GUID : " << m_installerContext.widgetConfig.guid);
-    return true;
 }
 
 void JobWidgetInstall::configureWidgetLocation(const std::string & widgetPath,
                                                const std::string& tempPath)
 {
     m_installerContext.locations =
-        WidgetLocation(DPL::ToUTF8String(*m_installerContext.widgetConfig.pkgname),
+        WidgetLocation(DPL::ToUTF8String(m_installerContext.widgetConfig.tzAppid),
                 widgetPath, tempPath,
-                m_installerContext.widgetConfig.packagingType);
+                m_installerContext.widgetConfig.packagingType,
+                m_installerContext.locationType);
 
     LogInfo("widgetSource " << widgetPath);
 }
@@ -346,9 +425,46 @@ void JobWidgetInstall::configureWidgetLocation(const std::string & widgetPath,
 JobWidgetInstall::ConfigureResult JobWidgetInstall::ConfigureInstallation(
         const std::string &widgetSource,
         const WrtDB::ConfigParserData &configData,
-        const WidgetUpdateInfo &update,
         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");
+    }
+
+    if ((regexec(&reg,
+                    DPL::ToUTF8String(m_installerContext.widgetConfig.tzAppid).c_str(),
+                    static_cast<size_t>(0), NULL, 0) != REG_NOERROR) ||
+            (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.");
+        regfree(&reg);
+        return ConfigureResult::Failed;
+    }
+    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;
+}
+
+JobWidgetInstall::ConfigureResult JobWidgetInstall::checkWidgetUpdate(
+        const WidgetUpdateInfo &update)
+{
     LogInfo(
         "Widget install/update: incoming guid = '" <<
         update.incomingGUID << "'");
@@ -358,28 +474,22 @@ JobWidgetInstall::ConfigureResult JobWidgetInstall::ConfigureInstallation(
 
     // Check policy
     WidgetUpdateMode::Type updateTypeCheckBit;
-    JobWidgetInstall::ConfigureResult ret = ConfigureResult::Ok;
 
     if (update.existingWidgetInfo.isExist == false) {
         LogInfo("Widget info does not exist");
         updateTypeCheckBit = WidgetUpdateMode::NotInstalled;
     } else {
-        LogInfo("Widget info exists. PkgName: " <<
-                update.existingWidgetInfo.pkgname);
-
-        DPL::OptionalString pkgname = update.existingWidgetInfo.pkgname;
+        LogInfo("Widget info exists. appid: " <<
+                update.existingWidgetInfo.tzAppid);
 
-        if(pkgname.IsNull()) {
-            LogInfo("But package name doesn't exist");
-            return ConfigureResult::Failed;
-        }
+        TizenAppId tzAppid = update.existingWidgetInfo.tzAppid;
 
-        LogInfo("Widget model exists. package name: " << pkgname);
+        LogInfo("Widget model exists. tizen app id: " << tzAppid);
 
         // Check running state
         int retval = APP_MANAGER_ERROR_NONE;
         bool isRunning = false;
-        retval = app_manager_is_running(DPL::ToUTF8String(*pkgname).c_str(), &isRunning);
+        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;
@@ -395,13 +505,12 @@ JobWidgetInstall::ConfigureResult JobWidgetInstall::ConfigureInstallation(
             } else {
                 LogInfo(
                     "Widget is already running. Policy is not update according to WAC");
-                LogInfo("Installation aborted: " << widgetSource);
 
                 return ConfigureResult::Failed;
             }
         }
 
-        m_installerContext.widgetConfig.pkgname = pkgname;
+        m_installerContext.widgetConfig.tzAppid = tzAppid;
         OptionalWidgetVersion existingVersion;
         existingVersion = update.existingWidgetInfo.existingVersion;
         OptionalWidgetVersion incomingVersion = update.incomingVersion;
@@ -409,39 +518,17 @@ JobWidgetInstall::ConfigureResult JobWidgetInstall::ConfigureInstallation(
         updateTypeCheckBit = CalcWidgetUpdatePolicy(existingVersion,
                                                     incomingVersion);
         // Calc proceed flag
-        if ((m_jobStruct.updateMode & updateTypeCheckBit) > 0) {
+        if ((m_jobStruct.updateMode & updateTypeCheckBit) > 0 ||
+            m_jobStruct.updateMode ==
+                WidgetUpdateMode::PolicyDirectoryForceInstall)
+        {
             LogInfo("Whether widget policy allow proceed ok");
-            ret = ConfigureResult::Updated;
+            return ConfigureResult::Updated;
         }
         else
             return ConfigureResult::Failed;
     }
-
-    if (!setTizenId(configData, ret)) {
-        return ConfigureResult::Failed;
-    } else {
-        using namespace PackageManager;
-        LogInfo("Tizen Id: " << m_installerContext.widgetConfig.pkgname);
-
-        configureWidgetLocation(widgetSource, tempPath);
-
-        // send start signal of pkgmgr
-        PkgmgrSignalSingleton::Instance().setPkgname(
-                    DPL::ToUTF8String(
-                        *m_installerContext.widgetConfig.pkgname));
-        PkgmgrSignalSingleton::Instance().sendSignal(
-                    PKGMGR_START_KEY,
-                    PKGMGR_START_INSTALL);
-    }
-
-    // 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
-    return ret;
+    return ConfigureResult::Ok;
 }
 
 WidgetUpdateMode::Type JobWidgetInstall::CalcWidgetUpdatePolicy(
@@ -495,6 +582,12 @@ ConfigParserData JobWidgetInstall::getWidgetDataFromXML(
                         new RootParser<WidgetParser>(configInfo,
                             DPL::FromUTF32String(
                                 L"widget"))));
+        } else if (pkgType == PKG_TYPE_DIRECTORY_WEB_APP) {
+            parser.Parse(widgetSource + '/' + WITH_OSP_XML,
+                         ElementParserPtr(
+                             new RootParser<WidgetParser>(
+                             configInfo,
+                             DPL::FromUTF32String(L"widget"))));
         } else {
             if (!isDRM) {
                 std::unique_ptr<DPL::ZipInput> zipFile(
@@ -561,11 +654,18 @@ ConfigParserData JobWidgetInstall::getWidgetDataFromXML(
         LogError("Failed to parse config.xml file");
         return ConfigParserData();
     }
+    Catch(DPL::ZipInput::Exception::SeekFileFailed)
+    {
+        LogError("Failed to seek widget archive - corrupted package?");
+        return ConfigParserData();
+    }
     return configInfo;
 }
 
 WidgetUpdateInfo JobWidgetInstall::detectWidgetUpdate(
-        const ConfigParserData &configInfo)
+        const ConfigParserData &configInfo,
+        const WrtDB::WidgetType appType,
+        const WrtDB::TizenAppId &tizenId)
 {
     LogInfo("Checking up widget package for config.xml...");
 
@@ -590,23 +690,45 @@ WidgetUpdateInfo JobWidgetInstall::detectWidgetUpdate(
                 WidgetVersion(*configInfo.version));
     }
 
-    Try
-    {
-        // Search widget handle by GUID
-        WidgetDAOReadOnly dao(widgetGUID);
-        return WidgetUpdateInfo(
-            widgetGUID,
-            widgetVersion,
-            WidgetUpdateInfo::ExistingWidgetInfo(
-                *dao.getPkgname(), dao.getVersion()));
-    }
-    Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
-    {
-        // GUID isn't installed
-        return WidgetUpdateInfo(
-            widgetGUID,
-            widgetVersion,
-            WidgetUpdateInfo::ExistingWidgetInfo());
+    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());
+        }
+
     }
 }
 
@@ -618,7 +740,7 @@ void JobWidgetInstall::SendProgress()
             // send progress signal of pkgmgr
             std::ostringstream percent;
             percent << static_cast<int>(GetProgressPercent());
-            PkgmgrSignalSingleton::Instance().sendSignal(
+            getInstallerStruct().pkgmgrInterface->sendSignal(
                         PKGMGR_PROGRESS_KEY,
                         percent.str());
 
@@ -635,22 +757,32 @@ void JobWidgetInstall::SendFinishedSuccess()
     // TODO : sync should move to separate task.
     sync();
 
+
+    if (INSTALL_LOCATION_TYPE_EXTERNAL == m_installerContext.locationType) {
+        if (false == m_installerContext.existingWidgetInfo.isExist) {
+            WidgetInstallToExtSingleton::Instance().postInstallation(true);
+        } else {
+            WidgetInstallToExtSingleton::Instance().postUpgrade(true);
+        }
+        WidgetInstallToExtSingleton::Instance().deinitialize();
+    }
+
     // remove widget install information file
     unlink(m_installerContext.installInfo.c_str());
 
     //inform widget info
     JobWidgetInstall::displayWidgetInfo();
 
-    DPL::OptionalString & tizenId = m_installerContext.widgetConfig.pkgname;
+    TizenAppId& tizenId = m_installerContext.widgetConfig.tzAppid;
 
     // send signal of pkgmgr
-    PkgmgrSignalSingleton::Instance().sendSignal(
+    getInstallerStruct().pkgmgrInterface->sendSignal(
                 PKGMGR_END_KEY,
                 PKGMGR_END_SUCCESS);
 
     LogDebug("Call widget install successfinishedCallback");
     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
-            tizenId.IsNull() ? "" : DPL::ToUTF8String(*tizenId), Exceptions::Success);
+            DPL::ToUTF8String(tizenId), Exceptions::Success);
 }
 
 void JobWidgetInstall::SendFinishedFailure()
@@ -661,17 +793,17 @@ void JobWidgetInstall::SendFinishedFailure()
 
     LogError("Error in installation step: " << m_exceptionCaught);
     LogError("Message: " << m_exceptionMessage);
-    DPL::OptionalString & tizenId = m_installerContext.widgetConfig.pkgname;
+    TizenAppId & tizenId = m_installerContext.widgetConfig.tzAppid;
 
     LogDebug("Call widget install failure finishedCallback");
 
     // send signal of pkgmgr
-    PkgmgrSignalSingleton::Instance().sendSignal(
+    getInstallerStruct().pkgmgrInterface->sendSignal(
                 PKGMGR_END_KEY,
                 PKGMGR_END_FAILURE);
 
     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
-            tizenId.IsNull() ? "" : DPL::ToUTF8String(*tizenId), m_exceptionCaught);
+            DPL::ToUTF8String(tizenId), m_exceptionCaught);
 }
 
 void JobWidgetInstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
@@ -682,22 +814,22 @@ void JobWidgetInstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
 
 void JobWidgetInstall::displayWidgetInfo()
 {
-    WidgetDAOReadOnly dao(m_installerContext.locations->getPkgname());
+    WidgetDAOReadOnly dao(m_installerContext.widgetConfig.tzAppid);
 
     std::ostringstream out;
     WidgetLocalizedInfo localizedInfo =
-        W3CFileLocalization::getLocalizedInfo(*dao.getPkgname());
+        W3CFileLocalization::getLocalizedInfo(dao.getTzAppId());
 
     out << std::endl <<
         "===================================== INSTALLED WIDGET INFO ========="\
         "============================";
     out << std::endl << "Name:                        " << localizedInfo.name;
-    out << std::endl << "PkgName:                     " << dao.getPkgname();
+    out << std::endl << "AppId:                     " << dao.getTzAppId();
     WidgetSize size = dao.getPreferredSize();
     out << std::endl << "Width:                       " << size.width;
     out << std::endl << "Height:                      " << size.height;
     out << std::endl << "Start File:                  " <<
-        W3CFileLocalization::getStartFile(*dao.getPkgname());
+        W3CFileLocalization::getStartFile(dao.getTzAppId());
     out << std::endl << "Version:                     " << dao.getVersion();
     out << std::endl << "Licence:                     " <<
         localizedInfo.license;
@@ -712,9 +844,8 @@ void JobWidgetInstall::displayWidgetInfo()
         dao.isDistributorSigned();
     out << std::endl << "Widget trusted:              " << dao.isTrusted();
 
-    OptionalWidgetIcon icon = W3CFileLocalization::getIcon(*dao.getPkgname());
-    DPL::OptionalString iconSrc =
-        !!icon ? icon->src : DPL::OptionalString::Null;
+    OptionalWidgetIcon icon = W3CFileLocalization::getIcon(dao.getTzAppId());
+    DPL::OptionalString iconSrc = !!icon ? icon->src : DPL::OptionalString::Null;
     out << std::endl << "Icon:                        " << iconSrc;
 
     out << std::endl << "Preferences:";
@@ -749,7 +880,12 @@ WrtDB::PackagingType JobWidgetInstall::checkPackageType(
         const std::string &widgetSource,
         const std::string &tempPath)
 {
-    // Check installation type (config.xml or widget.wgt)
+    // Check installation type (direcotory/ or config.xml or widget.wgt)
+    if (WidgetUpdateMode::PolicyDirectoryForceInstall == m_jobStruct.updateMode)
+    {
+        LogDebug("Install directly from directory");
+        return PKG_TYPE_DIRECTORY_WEB_APP;
+    }
     if (hasExtension(widgetSource, XML_EXTENSION)) {
         LogInfo("Hosted app installation");
         return PKG_TYPE_HOSTED_WEB_APP;
@@ -779,6 +915,11 @@ WrtDB::PackagingType JobWidgetInstall::checkPackageType(
             LogDebug("Failed to open widget package");
             return PKG_TYPE_UNKNOWN;
         }
+        Catch(DPL::ZipInput::Exception::SeekFileFailed)
+        {
+            LogError("Failed to seek widget package file");
+            return PKG_TYPE_UNKNOWN;
+        }
 
         Try
         {
@@ -810,6 +951,49 @@ WrtDB::PackagingType JobWidgetInstall::checkPackageType(
     return PKG_TYPE_UNKNOWN;
 }
 
+void JobWidgetInstall::setApplicationType(
+        const WrtDB::ConfigParserData &configInfo)
+{
+
+    FOREACH(iterator, configInfo.nameSpaces) {
+        LogInfo("namespace = [" << *iterator << "]");
+        AppType currentAppType = APP_TYPE_UNKNOWN;
+
+        if (*iterator == ConfigurationNamespace::W3CWidgetNamespaceName) {
+            continue;
+        } else if (
+            *iterator ==
+            ConfigurationNamespace::WacWidgetNamespaceNameForLinkElement ||
+            *iterator ==
+            ConfigurationNamespace::WacWidgetNamespaceName)
+        {
+            currentAppType = APP_TYPE_WAC20;
+        } else if (*iterator ==
+                ConfigurationNamespace::TizenWebAppNamespaceName) {
+            currentAppType = APP_TYPE_TIZENWEBAPP;
+        }
+
+        if (m_installerContext.widgetConfig.webAppType ==
+                APP_TYPE_UNKNOWN) {
+            m_installerContext.widgetConfig.webAppType = currentAppType;
+        } else if (m_installerContext.widgetConfig.webAppType ==
+                currentAppType) {
+            continue;
+        } else {
+            ThrowMsg(Exceptions::WidgetConfigFileInvalid,
+                     "Config.xml has more than one namespace");
+        }
+    }
+
+    // If there is no define, type set to WAC 2.0
+    if (m_installerContext.widgetConfig.webAppType == APP_TYPE_UNKNOWN) {
+        m_installerContext.widgetConfig.webAppType = APP_TYPE_WAC20;
+    }
+
+    LogInfo("type = [" <<
+            m_installerContext.widgetConfig.webAppType.getApptypeToString() << "]");
+}
+
 bool JobWidgetInstall::detectResourceEncryption(const WrtDB::ConfigParserData &configData)
 {
     FOREACH(it, configData.settingsList)
@@ -823,6 +1007,28 @@ bool JobWidgetInstall::detectResourceEncryption(const WrtDB::ConfigParserData &c
     return false;
 }
 
+void JobWidgetInstall::setInstallLocationType(const
+        WrtDB::ConfigParserData &configData)
+{
+    m_installerContext.locationType = INSTALL_LOCATION_TYPE_NOMAL;
+
+    if (true == m_jobStruct.m_preload) {
+        m_installerContext.locationType =
+            INSTALL_LOCATION_TYPE_PRELOAD;
+    } else {
+        FOREACH(it, configData.settingsList)
+        {
+            if (it->m_name == SETTING_VALUE_INSTALLTOEXT_NAME &&
+                    it->m_value ==
+                    SETTING_VALUE_INSTALLTOEXT_PREPER_EXT) {
+                LogDebug("This widget will be installed to sd card");
+                m_installerContext.locationType =
+                    INSTALL_LOCATION_TYPE_EXTERNAL;
+            }
+        }
+    }
+}
+
 bool JobWidgetInstall::isDRMWidget(std::string widgetPath)
 {
     /* TODO :
@@ -845,7 +1051,7 @@ bool JobWidgetInstall::isDRMWidget(std::string widgetPath)
 bool JobWidgetInstall::DecryptDRMWidget(std::string widgetPath,
         std::string destPath)
 {
-    /* TODO : 
+    /* TODO :
     drm_trusted_sapps_decrypt_package_info_s package_info;
 
     strncpy(package_info.sadcf_filepath, widgetPath.c_str(),
@@ -865,7 +1071,7 @@ bool JobWidgetInstall::DecryptDRMWidget(std::string widgetPath,
     }
     */
     if (drm_oem_intel_decrypt_package(const_cast<char*>(widgetPath.c_str()),
-                const_cast<char*>(destPath.c_str())) != 0) { 
+                const_cast<char*>(destPath.c_str())) != 0) {
         return true;
     } else {
         return false;