WidgetHandle removal - part 2. Task order change
[framework/web/wrt-installer.git] / src / jobs / widget_install / job_widget_install.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 114878d..11e2b25
  * @version 1.0
  * @brief   Implementation file for main installer task
  */
+#include <memory>
+
 #include <dpl/noncopyable.h>
 #include <dpl/abstract_waitable_input_adapter.h>
 #include <dpl/abstract_waitable_output_adapter.h>
 #include <dpl/zip_input.h>
-#include <dpl/scoped_ptr.h>
 #include <dpl/binary_queue.h>
 #include <dpl/copy.h>
 #include <dpl/assert.h>
 #include <dpl/sstream.h>
+#include <dpl/wrt-dao-ro/common_dao_types.h>
 #include "root_parser.h"
 #include "widget_parser.h"
 #include "parser_runner.h"
 #include <widget_install/job_widget_install.h>
-#include <widget_install/task_parental_mode.h>
 #include <widget_install/task_unzip.h>
 #include <widget_install/task_certify.h>
 #include <widget_install/task_widget_config.h>
-#include <widget_install/task_db_update.h>
+#include <widget_install/task_file_manipulation.h>
 #include <widget_install/task_ace_check.h>
 #include <widget_install/task_smack.h>
-#include <widget_install/task_desktop_file.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_update_files.h>
+#include <widget_install/task_database.h>
+#include <widget_install/task_remove_backup.h>
+#include <widget_install/task_encrypt_resource.h>
+#include <widget_install/task_certificates.h>
+
+#ifdef LB_SUPPORT
+#include <widget_install/task_livebox_conf.h>
+#endif
+#include <widget_install/task_plugins_copy.h>
+
 #include <widget_install/widget_install_errors.h>
 #include <widget_install/widget_install_context.h>
 #include <string>
-#include <dpl/wrt-dao-rw/widget_dao.h> //TODO remove
+#include <sys/time.h>
+#include <ctime>
+#include <cstdlib>
+#include <limits.h>
+#include <regex.h>
 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
+#include <dpl/wrt-dao-ro/global_config.h>
 #include <dpl/wrt-dao-rw/global_dao.h> // TODO remove
-#include <aul.h>
 #include <dpl/localization/w3c_file_localization.h>
+#include <libiriwrapper.h>
+#include <pkg-manager/pkgmgr_signal.h>
+#include <app_manager.h>
 
 using namespace WrtDB;
 
 namespace // anonymous
 {
 const char * const CONFIG_XML = "config.xml";
+const char * const WITH_OSP_XML = "res/wgt/config.xml";
 
-struct PathAndFilePair
-{
-    std::string path;
-    std::string file;
-
-    PathAndFilePair(const std::string &p,
-            const std::string &f) :
-        path(p),
-        file(f)
-    {
-    }
-};
-
-PathAndFilePair SplitFileAndPath(const std::string &filePath)
-{
-    std::string::size_type position = filePath.rfind('/');
-
-    // Is this only a file without a path ?
-    if (position == std::string::npos) {
-        return PathAndFilePair(std::string(), filePath);
-    }
+//allowed: a-z, A-Z, 0-9
+const char* REG_TIZENID_PATTERN = "^[a-zA-Z0-9]{10}$";
+const int MAX_TIZENID_LENGTH = 10;
 
-    // This is full file-path pair
-    return PathAndFilePair(filePath.substr(0,
-                                           position),
-                           filePath.substr(position + 1));
-}
+static const DPL::String SETTING_VALUE_ENCRYPTION = L"encryption";
+static const DPL::String SETTING_VALUE_ENCRYPTION_ENABLE = L"enable";
 
 class InstallerTaskFail :
     public DPL::TaskDecl<InstallerTaskFail>
@@ -110,6 +112,20 @@ class InstallerTaskFail :
         AddStep(&InstallerTaskFail::StepFail);
     }
 };
+
+const std::string XML_EXTENSION = ".xml";
+
+bool hasExtension(const std::string& filename, const std::string& extension) {
+    LogDebug("Looking for extension " << extension << " in: "  << filename);
+    size_t fileLen = filename.length();
+    size_t extLen = extension.length();
+    if (fileLen < extLen) {
+        LogError("Filename " << filename << " is shorter than extension "
+                 << extension);
+        return false;
+    }
+    return (0 == filename.compare(fileLen-extLen, extLen, extension));
+}
 } // namespace anonymous
 
 namespace Jobs {
@@ -120,28 +136,112 @@ JobWidgetInstall::JobWidgetInstall(std::string const &widgetPath,
     JobContextBase<WidgetInstallationStruct>(installerStruct),
     m_exceptionCaught(Exceptions::Success)
 {
+    // Check installation type (config.xml or widget.wgt)
+    bool browserRequest = hasExtension(widgetPath, XML_EXTENSION);
+
+    LogInfo("Hosted app installation: " << browserRequest);
+
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    srand(time(NULL) + tv.tv_usec);
+
+    m_installerContext.m_quiet = m_jobStruct.m_quiet;
+
+    if(!browserRequest)
+    {
+        m_installerContext.widgetConfig.pType = checkPackageType(widgetPath);
+    }
+    else
+    {
+        m_installerContext.widgetConfig.pType = WrtDB::PKG_TYPE_TIZEN_WEBAPP;
+    }
+    LogDebug("widgetPath:" << widgetPath);
+
+    ConfigParserData configData = getWidgetDataFromXML(widgetPath, browserRequest,
+            m_installerContext.widgetConfig.pType);
+    WidgetUpdateInfo update = detectWidgetUpdate(configData);
+    bool needEncryption = detectResourceEncryption(configData);
+
     // Configure installation
-    ConfigureResult result = ConfigureInstallation(widgetPath);
+    ConfigureResult result = ConfigureInstallation(widgetPath, configData,
+                                                   update, browserRequest);
 
     if (result == ConfigureResult::Ok) {
         LogInfo("Configure installation succeeded");
 
+        AddTask(new TaskRecovery(m_installerContext));
+
         // Create installation tasks
-        AddTask(new TaskParentalMode(m_installerContext));
-        AddTask(new TaskUnzip(m_installerContext));
+        if (!m_installerContext.locations->browserRequest()) {
+            AddTask(new TaskUnzip(m_installerContext));
+        }
         AddTask(new TaskWidgetConfig(m_installerContext));
+        if (m_installerContext.locations->browserRequest()) {
+            AddTask(new TaskPrepareFiles(m_installerContext));
+        }
         AddTask(new TaskCertify(m_installerContext));
-        AddTask(new TaskDbUpdate(m_installerContext));
+        if (needEncryption) {
+            AddTask(new TaskEncryptResource(m_installerContext));
+        }
+        AddTask(new TaskFileManipulation(m_installerContext));
         // TODO: Update progress information for this task
 
-        AddTask(new TaskAceCheck(m_installerContext));
+        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.
         AddTask(new TaskSmack(m_installerContext));
 
-        AddTask(new TaskDesktopFile(m_installerContext));
-        AddTask(new TaskPrivateStorage(m_installerContext));
+        AddTask(new TaskManifestFile(m_installerContext));
+        AddTask(new TaskCertificates(m_installerContext));
+        if (m_installerContext.widgetConfig.pType ==
+                PKG_TYPE_TIZEN_WITHSVCAPP) {
+            AddTask(new TaskInstallOspsvc(m_installerContext));
+        }
+#ifdef LB_SUPPORT
+        AddTask(new TaskLiveboxConf(m_installerContext));
+#endif
+        AddTask(new TaskPluginsCopy(m_installerContext));
+
+        AddTask(new TaskDatabase(m_installerContext));
+        AddTask(new TaskAceCheck(m_installerContext));
+    } else if (result == ConfigureResult::Updated) {
+        LogInfo("Configure installation updated");
+        LogInfo("Widget Update");
+
+        if (!m_installerContext.locations->browserRequest()) {
+            AddTask(new TaskUnzip(m_installerContext));
+        }
+        AddTask(new TaskWidgetConfig(m_installerContext));
+        if (m_installerContext.locations->browserRequest()) {
+            AddTask(new TaskPrepareFiles(m_installerContext));
+        }
+
+        AddTask(new TaskCertify(m_installerContext));
+        AddTask(new TaskUpdateFiles(m_installerContext));
+
+        /* TODO : To backup file, save md5 values */
+        AddTask(new TaskSmack(m_installerContext));
+
+        AddTask(new TaskManifestFile(m_installerContext));
+        if (m_installerContext.widgetConfig.pType ==
+                PKG_TYPE_TIZEN_WITHSVCAPP) {
+            AddTask(new TaskInstallOspsvc(m_installerContext));
+        }
+        AddTask(new TaskRemoveBackupFiles(m_installerContext));
+#ifdef LB_SUPPORT
+        AddTask(new TaskLiveboxConf(m_installerContext));
+#endif
+        AddTask(new TaskPluginsCopy(m_installerContext));
+
+        AddTask(new TaskDatabase(m_installerContext));
+
+        AddTask(new TaskAceCheck(m_installerContext));
+        //TODO: remove widgetHandle from this task and move before database task
+        // by now widget handle is needed in ace check
+        // Any error in acecheck while update will break widget
+
     } else if (result == ConfigureResult::Deferred) {
         // Installation is deferred
         LogInfo("Configure installation deferred");
@@ -157,27 +257,94 @@ JobWidgetInstall::JobWidgetInstall(std::string const &widgetPath,
     }
 }
 
-DPL::Optional<WidgetHandle> JobWidgetInstall::getNewWidgetHandle() const
+std::string JobWidgetInstall::generateTizenId() {
+    std::string allowed("0123456789"
+        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+        "abcdefghijklmnopqrstuvwxyz");
+    std::string tizenId;
+    tizenId.resize(MAX_TIZENID_LENGTH);
+    for (int i = 0; i < MAX_TIZENID_LENGTH; ++i) {
+        tizenId[i] = allowed[rand() % allowed.length()];
+    }
+    return tizenId;
+}
+
+bool JobWidgetInstall::setTizenId(
+        const WrtDB::ConfigParserData &configInfo, ConfigureResult result)
 {
-    return m_installerContext.widgetHandle;
+    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;
+
+    } else {
+        std::string tizenId = 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 = generateTizenId();
+                    newPath.str("");
+                    newPath << path << tizenId;
+                    continue;
+                }
+                break;
+            }
+
+            m_installerContext.widgetConfig.pkgname =
+                DPL::FromUTF8String(tizenId);
+        }
+        LogInfo("tizen_id name was generated by WRT: " << tizenId);
+    }
+    regfree(&reg);
+
+    LogInfo("Tizen Id : " << m_installerContext.widgetConfig.pkgname);
+    LogInfo("W3C Widget GUID : " << m_installerContext.widgetConfig.guid);
+    return true;
 }
 
-bool JobWidgetInstall::getUnzipStartedFlag() const
+DPL::OptionalString JobWidgetInstall::getNewTizenId() const
 {
-    return m_installerContext.unzipStarted;
+    return m_installerContext.widgetConfig.pkgname;
 }
 
-bool JobWidgetInstall::getUnzipFinishedFlag() const
+void JobWidgetInstall::configureWidgetLocation(const std::string & widgetPath, bool browserRequest)
 {
-    return m_installerContext.unzipFinished;
+    m_installerContext.locations = WidgetLocation(DPL::ToUTF8String(*m_installerContext.widgetConfig.pkgname),
+            widgetPath, browserRequest, m_installerContext.widgetConfig.pType);
+
+    LogInfo("widgetSource " << widgetPath);
 }
 
 JobWidgetInstall::ConfigureResult JobWidgetInstall::ConfigureInstallation(
-        const std::string &widgetPath)
+        const std::string &widgetSource,
+        const WrtDB::ConfigParserData &configData,
+        const WidgetUpdateInfo &update,
+        bool browserRequest)
 {
-    // Detect widget update
-    WidgetUpdateInfo update = detectWidgetUpdate(widgetPath);
-
     LogInfo(
         "Widget install/update: incoming guid = '" <<
         update.incomingGUID << "'");
@@ -187,70 +354,90 @@ 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. Handle: " <<
-                update.existingWidgetInfo.existingHandle);
+        LogInfo("Widget info exists. PkgName: " <<
+                update.existingWidgetInfo.pkgname);
 
-        DPL::OStringStream pkgName;
-        DPL::OptionalString pkgname =
-            WidgetDAOReadOnly(update.existingWidgetInfo.existingHandle).getPkgname();
+        DPL::OptionalString pkgname = update.existingWidgetInfo.pkgname;
 
         if(pkgname.IsNull()) {
-            LogInfo("But widget package name doesn't exist");
+            LogInfo("But package name doesn't exist");
             return ConfigureResult::Failed;
         }
 
-        LogInfo("Widget model exists. Package name: " << pkgName);
-        if (aul_app_is_running(DPL::ToUTF8String(*pkgname).c_str())) {
+        LogInfo("Widget model exists. package name: " << pkgname);
+
+        // Check running state
+        int retval = APP_MANAGER_ERROR_NONE;
+        bool isRunning = false;
+        retval = app_manager_is_running(DPL::ToUTF8String(*pkgname).c_str(), &isRunning);
+        if (APP_MANAGER_ERROR_NONE != retval) {
+            LogError("Fail to get running state");
+            return ConfigureResult::Failed;
+        }
+
+        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");
-                LogInfo("Installation deferred: " << widgetPath);
-
-                GlobalDAO::AddDefferedWidgetPackageInstallation(
-                    DPL::FromUTF8String(widgetPath));
 
                 return ConfigureResult::Deferred;
             } else {
                 LogInfo(
                     "Widget is already running. Policy is not update according to WAC");
-                LogInfo("Installation aborted: " << widgetPath);
+                LogInfo("Installation aborted: " << widgetSource);
 
                 return ConfigureResult::Failed;
             }
         }
 
+        m_installerContext.widgetConfig.pkgname = pkgname;
         OptionalWidgetVersion existingVersion;
         existingVersion = update.existingWidgetInfo.existingVersion;
         OptionalWidgetVersion incomingVersion = update.incomingVersion;
 
         updateTypeCheckBit = CalcWidgetUpdatePolicy(existingVersion,
                                                     incomingVersion);
+        // Calc proceed flag
+        if ((m_jobStruct.updateMode & updateTypeCheckBit) > 0) {
+            LogInfo("Whether widget policy allow proceed ok");
+            ret = ConfigureResult::Updated;
+        }
+        else
+            return ConfigureResult::Failed;
     }
 
-    // Calc proceed flag
-    bool canProceed = (m_jobStruct.updateMode & updateTypeCheckBit) > 0;
-
-    LogInfo("Whether widget policy allow proceed: " << canProceed);
+    if (!setTizenId(configData, ret)) {
+        return ConfigureResult::Failed;
+    } else {
+        using namespace PackageManager;
+        LogInfo("Tizen Id: " << m_installerContext.widgetConfig.pkgname);
+
+        configureWidgetLocation(widgetSource, browserRequest);
+
+        // 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.widgetFilePath = widgetPath;
-    m_installerContext.tempWidgetPath = std::string();
-    m_installerContext.widgetConfig = WidgetRegisterInfo();
-    m_installerContext.unzipStarted = false;
-    m_installerContext.unzipFinished = false;
     m_installerContext.installStep = InstallerContext::INSTALL_START;
     m_installerContext.job = this;
     m_installerContext.existingWidgetInfo = update.existingWidgetInfo;
     m_installerContext.widgetConfig.shareHref = std::string();
 
     // Return result
-    return canProceed ? ConfigureResult::Ok : ConfigureResult::Failed;
+    return ret;
 }
 
 WidgetUpdateMode::Type JobWidgetInstall::CalcWidgetUpdatePolicy(
@@ -286,100 +473,130 @@ WidgetUpdateMode::Type JobWidgetInstall::CalcWidgetUpdatePolicy(
     }
 }
 
-WidgetUpdateInfo JobWidgetInstall::detectWidgetUpdate(
-        const std::string &widgetPath)
+ConfigParserData JobWidgetInstall::getWidgetDataFromXML(
+        const std::string &widgetSource,
+        bool fromBrowser, WrtDB::PkgType isOspsvc)
 {
-    LogInfo("Checking up widget package for config.xml...");
-
     Try
     {
-        // Open zip file
-        DPL::ScopedPtr<DPL::ZipInput> zipFile(
-            new DPL::ZipInput(widgetPath));
-
-        // Open config.xml file
-        DPL::ScopedPtr<DPL::ZipInput::File> configFile(
-            zipFile->OpenFile(CONFIG_XML));
-
-        // Extract config
-        DPL::BinaryQueue buffer;
-        DPL::AbstractWaitableInputAdapter inputAdapter(configFile.Get());
-        DPL::AbstractWaitableOutputAdapter outputAdapter(&buffer);
-        DPL::Copy(&inputAdapter, &outputAdapter);
-
         // Parse config
         ParserRunner parser;
         ConfigParserData configInfo;
 
-        parser.Parse(&buffer,
-                     ElementParserPtr(
-                         new RootParser<WidgetParser>(configInfo,
-                                                      DPL::FromUTF32String(
-                                                          L"widget"))));
-
-        // Check widget id
-        DPL::OptionalString widgetGUID = configInfo.widget_id;
-
-        if (widgetGUID.IsNull()) {
-            LogDebug("Installed widget has no GUID");
-            return WidgetUpdateInfo();
+        if (fromBrowser) {
+            parser.Parse(widgetSource,
+                         ElementParserPtr(
+                             new RootParser<WidgetParser>(configInfo,
+                                                          DPL::FromUTF32String(
+                                                              L"widget"))));
         }
+        else {
+            // Open zip file
+            std::unique_ptr<DPL::ZipInput> zipFile(
+                new DPL::ZipInput(widgetSource));
 
-        LogDebug("Installed widget GUID: " << *widgetGUID);
+            std::unique_ptr<DPL::ZipInput::File> configFile;
 
-        // Locate widget ID with this GUID
-        // Incoming widget version
-        OptionalWidgetVersion widgetVersion;
-        if (!configInfo.version.IsNull()) {
-            widgetVersion =
-                DPL::Optional<WidgetVersion>(
-                    WidgetVersion(*configInfo.version));
-        }
+            // Open config.xml file
+            if (isOspsvc == PKG_TYPE_TIZEN_WITHSVCAPP) {
+                configFile.reset(zipFile->OpenFile(WITH_OSP_XML));
+            } else {
+                configFile.reset(zipFile->OpenFile(CONFIG_XML));
+            }
 
-        try
-        {
-            // Search widget handle by GUID
-            WidgetDAO dao(widgetGUID);
-            return WidgetUpdateInfo(
-                widgetGUID,
-                widgetVersion,
-                WidgetUpdateInfo::ExistingWidgetInfo(
-                    dao.getHandle(), dao.getVersion()));
-        }
-        Catch(WidgetDAO::Exception::WidgetNotExist){
-            // GUID isn't installed
-            return WidgetUpdateInfo(
-                widgetGUID,
-                widgetVersion,
-                WidgetUpdateInfo::ExistingWidgetInfo());
+            // Extract config
+            DPL::BinaryQueue buffer;
+            DPL::AbstractWaitableInputAdapter inputAdapter(configFile.get());
+            DPL::AbstractWaitableOutputAdapter outputAdapter(&buffer);
+            DPL::Copy(&inputAdapter, &outputAdapter);
+            parser.Parse(&buffer,
+                         ElementParserPtr(
+                             new RootParser<WidgetParser>(configInfo,
+                                                          DPL::FromUTF32String(
+                                                              L"widget"))));
         }
+
+        return configInfo;
     }
     Catch(DPL::ZipInput::Exception::OpenFailed)
     {
-        LogDebug("Failed to open widget package");
-        return WidgetUpdateInfo();
+        LogError("Failed to open widget package");
+        return ConfigParserData();
     }
     Catch(DPL::ZipInput::Exception::OpenFileFailed)
     {
-        LogDebug("Failed to open config.xml file");
-        return WidgetUpdateInfo();
+        LogError("Failed to open config.xml file");
+        return ConfigParserData();
     }
     Catch(DPL::CopyFailed)
     {
-        LogDebug("Failed to extract config.xml file");
-        return WidgetUpdateInfo();
+        LogError("Failed to extract config.xml file");
+        return ConfigParserData();
     }
     Catch(ElementParser::Exception::ParseError)
     {
-        LogDebug("Failed to parse config.xml file");
+        LogError("Failed to parse config.xml file");
+        return ConfigParserData();
+    }
+}
+
+WidgetUpdateInfo JobWidgetInstall::detectWidgetUpdate(
+        const ConfigParserData &configInfo)
+{
+    LogInfo("Checking up widget package for config.xml...");
+
+    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 =
+            DPL::Optional<WidgetVersion>(
+                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());
+    }
 }
 
 void JobWidgetInstall::SendProgress()
 {
+    using namespace PackageManager;
     if (GetProgressFlag() != false) {
         if (getInstallerStruct().progressCallback != NULL) {
+            // send progress signal of pkgmgr
+            std::ostringstream percent;
+            percent << static_cast<int>(GetProgressPercent());
+            PkgmgrSignalSingleton::Instance().sendSignal(
+                        PKGMGR_PROGRESS_KEY,
+                        percent.str());
 
             LogDebug("Call widget install progressCallbak");
             getInstallerStruct().progressCallback(getInstallerStruct().userParam,
@@ -390,27 +607,47 @@ void JobWidgetInstall::SendProgress()
 
 void JobWidgetInstall::SendFinishedSuccess()
 {
+    using namespace PackageManager;
+    // TODO : sync should move to separate task.
+    sync();
+
+    // remove widget install information file
+    unlink(m_installerContext.installInfo.c_str());
+
     //inform widget info
     JobWidgetInstall::displayWidgetInfo();
 
-    DPL::Optional<WidgetHandle> handle = getNewWidgetHandle();
-    const WidgetHandle INVALID_WIDGET_HANDLE = 0;
+    DPL::OptionalString tizenId = getNewTizenId();
+
+    // send signal of pkgmgr
+    PkgmgrSignalSingleton::Instance().sendSignal(
+                PKGMGR_END_KEY,
+                PKGMGR_END_SUCCESS);
 
     LogDebug("Call widget install successfinishedCallback");
     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
-            !!handle ? *handle : INVALID_WIDGET_HANDLE, Exceptions::Success);
+            tizenId.IsNull() ? "" : DPL::ToUTF8String(*tizenId), Exceptions::Success);
 }
 
 void JobWidgetInstall::SendFinishedFailure()
 {
+    using namespace PackageManager;
+    // remove widget install information file
+    unlink(m_installerContext.installInfo.c_str());
+
     LogError("Error in installation step: " << m_exceptionCaught);
     LogError("Message: " << m_exceptionMessage);
-    DPL::Optional<WidgetHandle> handle = getNewWidgetHandle();
-    const WidgetHandle INVALID_WIDGET_HANDLE = 0;
+    DPL::OptionalString tizenId = getNewTizenId();
 
     LogDebug("Call widget install failure finishedCallback");
+
+    // send signal of pkgmgr
+    PkgmgrSignalSingleton::Instance().sendSignal(
+                PKGMGR_END_KEY,
+                PKGMGR_END_FAILURE);
+
     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
-            !!handle ? *handle : INVALID_WIDGET_HANDLE, m_exceptionCaught);
+            tizenId.IsNull() ? "" : DPL::ToUTF8String(*tizenId), m_exceptionCaught);
 }
 
 void JobWidgetInstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
@@ -421,24 +658,22 @@ void JobWidgetInstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
 
 void JobWidgetInstall::displayWidgetInfo()
 {
-    DPL::Optional<WidgetHandle> handle = getNewWidgetHandle();
-    Assert(!!handle);
-
-    WidgetDAO dao(*handle);
+    WidgetDAOReadOnly dao(m_installerContext.locations->getPkgname());
 
     std::ostringstream out;
     WidgetLocalizedInfo localizedInfo =
-        W3CFileLocalization::getLocalizedInfo(*handle);
+        W3CFileLocalization::getLocalizedInfo(dao.getHandle()); //TODO: pkgname arg
 
     out << std::endl <<
         "===================================== INSTALLED WIDGET INFO ========="\
         "============================";
     out << std::endl << "Name:                        " << localizedInfo.name;
+    out << std::endl << "PkgName:                     " << dao.getPkgname();
     WidgetSize size = dao.getPreferredSize();
     out << std::endl << "Width:                       " << size.width;
     out << std::endl << "Height:                      " << size.height;
     out << std::endl << "Start File:                  " <<
-        W3CFileLocalization::getStartFile(*handle);
+        W3CFileLocalization::getStartFile(dao.getHandle()); //TODO: pkgname arg
     out << std::endl << "Version:                     " << dao.getVersion();
     out << std::endl << "Licence:                     " <<
         localizedInfo.license;
@@ -453,7 +688,7 @@ void JobWidgetInstall::displayWidgetInfo()
         dao.isDistributorSigned();
     out << std::endl << "Widget trusted:              " << dao.isTrusted();
 
-    OptionalWidgetIcon icon = W3CFileLocalization::getIcon(*handle);
+    OptionalWidgetIcon icon = W3CFileLocalization::getIcon(dao.getHandle()); //TODO: pkgname arg
     DPL::OptionalString iconSrc =
         !!icon ? icon->src : DPL::OptionalString::Null;
     out << std::endl << "Icon:                        " << iconSrc;
@@ -481,12 +716,78 @@ void JobWidgetInstall::displayWidgetInfo()
         }
     }
 
-    out << std::endl << "Back Supported:              " <<
-        (dao.getBackSupported() ? "YES" : "NO");
-
     out << std::endl;
 
     LogInfo(out.str());
 }
+
+WrtDB::PackagingType JobWidgetInstall::checkPackageType(
+        const std::string &widgetSource)
+{
+    using namespace WrtDB;
+
+    PackagingType pType = PKG_TYPE_UNKNOWN;
+    std::unique_ptr<DPL::ZipInput> zipFile;
+
+    Try
+    {
+        // Open zip file
+        zipFile.reset(new DPL::ZipInput(widgetSource));
+
+    }
+    Catch(DPL::ZipInput::Exception::OpenFailed)
+    {
+        LogError("Failed to open widget package");
+        return PKG_TYPE_UNKNOWN;
+    }
+
+    Try
+    {
+        // Open config.xml file in package root
+        std::unique_ptr<DPL::ZipInput::File> configFile(
+                zipFile->OpenFile(CONFIG_XML));
+        pType = PKG_TYPE_TIZEN_WEBAPP;
+    }
+    Catch(DPL::ZipInput::Exception::OpenFileFailed)
+    {
+        LogWarning("Could not find ./config.xml");
+    }
+
+    Try
+    {
+        // Open config.xml file in package root
+        std::unique_ptr<DPL::ZipInput::File> configFile(
+                zipFile->OpenFile(WITH_OSP_XML));
+        if (pType == PKG_TYPE_TIZEN_WEBAPP) {
+            LogWarning("Two config.xml's found. Application type is unknown.");
+            return PKG_TYPE_UNKNOWN;
+        }
+
+        pType = PKG_TYPE_TIZEN_WITHSVCAPP;
+    }
+    Catch(DPL::ZipInput::Exception::OpenFileFailed)
+    {
+        LogWarning("Could not find ./res/wgt/config.xml");
+    }
+
+    if (pType == PKG_TYPE_UNKNOWN) {
+        LogWarning("config.xml not found. Application type is unknown.");
+    }
+    return pType;
+}
+
+bool JobWidgetInstall::detectResourceEncryption(const WrtDB::ConfigParserData &configData)
+{
+    FOREACH(it, configData.settingsList)
+    {
+        if (it->m_name == SETTING_VALUE_ENCRYPTION &&
+                it->m_value == SETTING_VALUE_ENCRYPTION_ENABLE) {
+            LogDebug("resource need encryption");
+            return true;
+        }
+    }
+    return false;
+}
+
 } //namespace WidgetInstall
 } //namespace Jobs