Platform version check during wgt installation - fixed
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_widget_config.cpp
old mode 100755 (executable)
new mode 100644 (file)
index de2109f..5deefb4
  * @version 1.0
  * @brief   Implementation file for installer task widget config
  */
-#include <string>
+
 #include <sstream>
-#include <dpl/foreach.h>
+#include <string>
+#include <sys/stat.h>
+#include <dirent.h>
+
 #include <dpl/errno_string.h>
-#include <dpl/wrt-dao-rw/feature_dao.h>
-#include <dpl/wrt-dao-ro/global_config.h>
+#include <dpl/foreach.h>
+#include <dpl/localization/w3c_file_localization.h>
+#include <dpl/singleton_impl.h>
+#include <dpl/utils/mime_type_utils.h>
+#include <dpl/utils/wrt_global_settings.h>
 #include <dpl/utils/wrt_utility.h>
-#include <root_parser.h>
-#include <widget_parser.h>
-#include <parser_runner.h>
+#include <dpl/wrt-dao-ro/global_config.h>
+#include <dpl/wrt-dao-rw/feature_dao.h>
+
 #include <libiriwrapper.h>
-#include <widget_install/task_widget_config.h>
+#include <parser_runner.h>
+#include <root_parser.h>
+
 #include <widget_install/job_widget_install.h>
-#include <widget_install/widget_install_errors.h>
+#include <widget_install/task_widget_config.h>
 #include <widget_install/widget_install_context.h>
-#include <dpl/utils/file_utils.h>
-#include <dpl/utils/mime_type_utils.h>
-#include <sys/stat.h>
-#include <dpl/utils/wrt_global_settings.h>
+#include <widget_install/widget_install_errors.h>
+#include <widget_parser.h>
+#include <wrt_error.h>
+
 
 namespace { // anonymous
-const WidgetHandle WIDGET_HANDLE_START_VALUE = 1000;
 const DPL::String BR = DPL::FromUTF8String("<br>");
 const std::string WIDGET_NOT_COMPATIBLE = "This widget is "
         "not compatible with WRT.<br><br>";
 const std::string QUESTION = "Do you want to install it anyway?";
-} // namespace anonymous
+
+const char *const DEFAULT_LANGUAGE = "default";
+
+const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
+
+const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
+}
+
 
 namespace Jobs {
 namespace WidgetInstall {
@@ -57,20 +71,22 @@ void InstallerTaskWidgetPopupData::PopupData::addWidgetInfo(
 
 TaskWidgetConfig::TaskWidgetConfig(InstallerContext& installContext) :
     DPL::TaskDecl<TaskWidgetConfig>(this),
-    m_installContext(installContext),
-    WidgetInstallPopup(installContext)
+    WidgetInstallPopup(installContext),
+    m_installContext(installContext)
+
 {
     AddStep(&TaskWidgetConfig::StepProcessConfigurationFile);
     AddStep(&TaskWidgetConfig::ReadLocaleFolders);
     AddStep(&TaskWidgetConfig::ProcessLocalizedStartFiles);
+    AddStep(&TaskWidgetConfig::ProcessBackgroundPageFile);
     AddStep(&TaskWidgetConfig::ProcessLocalizedIcons);
     AddStep(&TaskWidgetConfig::StepVerifyFeatures);
+    AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
 
     if (!GlobalSettings::TestModeEnabled() && !m_installContext.m_quiet) {
         AddStep(&TaskWidgetConfig::StepCancelWidgetInstallationAfterVerifyFeatures);
         AddStep(&TaskWidgetConfig::StepShowWidgetInfo);
         AddStep(&TaskWidgetConfig::StepCancelWidgetInstallation);
-        AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
         AddStep(&TaskWidgetConfig::StepCancelWidgetInstallationAfterMinVersion);
         AddStep(&TaskWidgetConfig::StepDeletePopupWin);
     }
@@ -80,11 +96,12 @@ void TaskWidgetConfig::StepProcessConfigurationFile()
 {
     Try
     {
-        WidgetConfigurationManagerSingleton::Instance().processFile(
-                m_installContext.tempWidgetRoot,
-                m_installContext.widgetConfig);
+        std::string path = m_installContext.locations->getConfigurationDir();
+        LogInfo("path: " << path);
+
+        processFile(path, m_installContext.widgetConfig);
     }
-    Catch(WidgetConfigurationManager::Exception::ProcessFailed)
+    Catch(Exception::ConfigParseFailed)
     {
         LogError("Parsing failed.");
         ReThrow(Exceptions::WidgetConfigFileInvalid);
@@ -94,7 +111,7 @@ void TaskWidgetConfig::StepProcessConfigurationFile()
         // To get widget type for distribute WAC, TIZEN WebApp
         setApplicationType();
     }
-    Catch(WidgetConfigurationManager::Exception::ProcessFailed)
+    Catch(Exception::ConfigParseFailed)
     {
         LogError("Config.xml has more than one namespace");
         ReThrow(Exceptions::WidgetConfigFileInvalid);
@@ -111,7 +128,7 @@ void TaskWidgetConfig::ReadLocaleFolders()
     //Adding default locale
     m_localeFolders.insert(L"");
 
-    std::string localePath = m_installContext.tempWidgetRoot + "/locales";
+    std::string localePath = m_installContext.locations->getConfigurationDir() + "/locales";
     DIR* localeDir = opendir(localePath.c_str());
     if (!localeDir) {
         LogDebug("No /locales directory in the widget package.");
@@ -147,8 +164,9 @@ void TaskWidgetConfig::ReadLocaleFolders()
         LogError("readdir() failed with " << DPL::GetErrnoString());
     }
 
-    if (closedir(localeDir)) {
-        LogError("closedir() failed with " << DPL::GetErrnoString());
+    if (-1 == TEMP_FAILURE_RETRY(closedir(localeDir))) {
+        LogError("Failed to close dir: " << localePath << " with error: "
+                << DPL::GetErrnoString());
     }
 }
 
@@ -195,10 +213,10 @@ void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
 
             DPL::String relativePath = pathPrefix + *path;
             DPL::String absolutePath = DPL::FromUTF8String(
-                    m_installContext.tempWidgetRoot) + L"/" + relativePath;
+                    m_installContext.locations->getConfigurationDir()) + L"/" + relativePath;
 
             // get property data from packaged app
-            if (FileUtils::FileExists(absolutePath)) {
+            if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
                 WidgetRegisterInfo::StartFileProperties startFileProperties;
                 if (!!type) {
                     startFileProperties.type = *type;
@@ -240,7 +258,7 @@ void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
             } else {
                 // set property data for hosted start url
                 // Hosted start url only support TIZEN WebApp
-                if (m_installContext.widgetConfig.type ==
+                if (m_installContext.widgetConfig.webAppType ==
                         APP_TYPE_TIZENWEBAPP)
                 {
                     const char *startPath =
@@ -266,6 +284,21 @@ void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
     }
 }
 
+void TaskWidgetConfig::ProcessBackgroundPageFile()
+{
+    if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
+        // check whether file exists
+        DPL::String backgroundPagePath = DPL::FromUTF8String(
+            m_installContext.locations->getConfigurationDir()) + L"/" +
+            *m_installContext.widgetConfig.configInfo.backgroundPage;
+        //if no then cancel installation
+        if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
+            ThrowMsg(Exceptions::WidgetConfigFileInvalid,
+                    L"Given background page file not found in archive");
+        }
+    }
+}
+
 void TaskWidgetConfig::ProcessLocalizedIcons()
 {
     using namespace WrtDB;
@@ -283,6 +316,7 @@ void TaskWidgetConfig::ProcessLocalizedIcons()
 
 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
 {
+    LogInfo("enter");
     bool isAnyIconValid = false;
     //In case a default filename is passed as custom filename in config.xml, we
     //need to keep a set of already processed filenames to avoid icon duplication
@@ -306,9 +340,9 @@ void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
 
         DPL::String relativePath = pathPrefix + icon.src;
         DPL::String absolutePath = DPL::FromUTF8String(
-                m_installContext.tempWidgetRoot) + L"/" + relativePath;
+                m_installContext.locations->getConfigurationDir()) + L"/" + relativePath;
 
-        if (FileUtils::FileExists(absolutePath)) {
+        if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
             DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
 
             if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
@@ -390,10 +424,20 @@ void TaskWidgetConfig::StepShowWidgetInfo()
 void TaskWidgetConfig::StepCheckMinVersionInfo()
 {
     if (!isMinVersionCompatible(
-                m_installContext.widgetConfig.type.appType,
+                m_installContext.widgetConfig.webAppType.appType,
                 m_installContext.widgetConfig.minVersion)) {
-        std::string label = WIDGET_NOT_COMPATIBLE + QUESTION;
-        createInstallPopup(PopupType::WIDGET_MIN_VERSION, label);
+        if(!GlobalSettings::TestModeEnabled() && !m_installContext.m_quiet)
+        {
+            LogDebug("Platform version to low - launching");
+            std::string label = WIDGET_NOT_COMPATIBLE + QUESTION;
+            createInstallPopup(PopupType::WIDGET_MIN_VERSION, label);
+        }
+        else
+        {
+            LogError("Platform version lower than required -> cancelling installation");
+            ThrowMsg(Exceptions::NotAllowed,
+                    "Platform version does not meet requirements");
+        }
     }
 
     m_installContext.job->UpdateProgress(
@@ -415,14 +459,14 @@ void TaskWidgetConfig::StepVerifyFeatures()
         // check feature vender for permission
         // WAC, TIZEN WebApp cannot use other feature
 
-        if (!isFeatureAllowed(m_installContext.widgetConfig.type.appType,
+        if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
                               it->name))
         {
             LogInfo("This application type not allowed to use this feature");
             ThrowMsg(
                 Exceptions::WidgetConfigFileInvalid,
                 "This app type [" <<
-                m_installContext.widgetConfig.type.getApptypeToString() <<
+                m_installContext.widgetConfig.webAppType.getApptypeToString() <<
                 "] cannot be allowed to use [" <<
                 DPL::ToUTF8String(it->name) + "] feature");
         }
@@ -452,6 +496,10 @@ void TaskWidgetConfig::StepVerifyFeatures()
             featureInfo += DPL::ToUTF8String(BR);
         }
     }
+    if(!data.accessInfoSet.empty()) {
+        featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
+        featureInfo += DPL::ToUTF8String(BR);
+    }
     data.featuresList = newList;
     if (!featureInfo.empty()) {
         m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
@@ -485,9 +533,9 @@ void TaskWidgetConfig::setApplicationType()
             currentAppType = APP_TYPE_TIZENWEBAPP;
         }
 
-        if (widgetInfo->type == APP_TYPE_UNKNOWN) {
-            widgetInfo->type = currentAppType;
-        } else if (widgetInfo->type == currentAppType) {
+        if (widgetInfo->webAppType == APP_TYPE_UNKNOWN) {
+            widgetInfo->webAppType = currentAppType;
+        } else if (widgetInfo->webAppType == currentAppType) {
             continue;
         } else {
             ThrowMsg(Exceptions::WidgetConfigFileInvalid,
@@ -496,11 +544,11 @@ void TaskWidgetConfig::setApplicationType()
     }
 
     // If there is no define, type set to WAC 2.0
-    if (widgetInfo->type == APP_TYPE_UNKNOWN) {
-        widgetInfo->type = APP_TYPE_WAC20;
+    if (widgetInfo->webAppType == APP_TYPE_UNKNOWN) {
+        widgetInfo->webAppType = APP_TYPE_WAC20;
     }
 
-    LogInfo("type = [" << widgetInfo->type.getApptypeToString() << "]");
+    LogInfo("type = [" << widgetInfo->webAppType.getApptypeToString() << "]");
 }
 
 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
@@ -591,13 +639,15 @@ bool TaskWidgetConfig::isMinVersionCompatible(WrtDB::AppType appType,
 
     if (!parseVersionString(version,
                 majorSupported, minorSupported, microSupported)) {
-        LogWarning("Invalid format of WAC version string.");
+        LogWarning("Invalid format of platform version string.");
         return true;
     }
 
     if (majorWidget > majorSupported ||
-            minorWidget > minorSupported ||
-            microWidget > microSupported) {
+            (majorWidget == majorSupported && minorWidget > minorSupported) ||
+            (majorWidget == majorSupported && minorWidget == minorSupported
+                    && microWidget > microSupported))
+    {
         LogInfo("Platform doesn't support this widget.");
         return false;
     }
@@ -607,12 +657,196 @@ bool TaskWidgetConfig::isMinVersionCompatible(WrtDB::AppType appType,
 bool TaskWidgetConfig::isTizenWebApp() const
 {
     bool ret = FALSE;
-    if (m_installContext.widgetConfig.type.appType
+    if (m_installContext.widgetConfig.webAppType.appType
             == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
         ret = TRUE;
 
     return ret;
 }
 
+bool TaskWidgetConfig::parseConfigurationFileBrowser(WrtDB::ConfigParserData& configInfo,
+                                    const std::string& _currentPath, int* pErrCode)
+{
+    ParserRunner parser;
+    Try
+    {
+        parser.Parse(_currentPath, ElementParserPtr(new
+                                                  RootParser<
+                                                      WidgetParser>(
+                                                      configInfo,
+                                                      DPL::FromUTF32String(
+                                                          L"widget"))));
+    }
+    Catch(ElementParser::Exception::Base)
+    {
+        LogError("Invalid widget configuration file!");
+        *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
+        return false;
+    }
+    return true;
+}
+
+bool TaskWidgetConfig::parseConfigurationFileWidget(WrtDB::ConfigParserData& configInfo,
+                                    const std::string& _currentPath, int* pErrCode)
+{
+    ParserRunner parser;
+
+    //TODO: rewrite this madness
+    std::string cfgAbsPath;
+    DIR* dir = NULL;
+    struct dirent* ptr = NULL;
+
+    dir = opendir(_currentPath.c_str());
+    if (dir == NULL) {
+        *pErrCode = WRT_ERR_UNKNOWN;
+        return false;
+    }
+    bool has_config_xml = false;
+    errno = 0;
+    while ((ptr = readdir(dir)) != NULL) { //Find configuration file, based on its name
+        if (ptr->d_type == DT_REG) {
+            if (!strcmp(ptr->d_name, WRT_WIDGET_CONFIG_FILE_NAME)) {
+                std::string dName(ptr->d_name);
+                WrtUtilJoinPaths(cfgAbsPath, _currentPath, dName);
+
+                //Parse widget configuration file
+                LogDebug("Found config: " << cfgAbsPath);
+
+                Try
+                {
+                    parser.Parse(cfgAbsPath, ElementParserPtr(new
+                                                              RootParser<
+                                                                  WidgetParser>(
+                                                                  configInfo,
+                                                                  DPL
+                                                                      ::
+                                                                      FromUTF32String(
+                                                                      L"widget"))));
+                }
+                Catch(ElementParser::Exception::Base)
+                {
+                    LogError("Invalid widget configuration file!");
+                    //                    _rethrown_exception.Dump();
+                    *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
+                    if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
+                        LogError("Failed to close dir: " << _currentPath << " with error: "
+                                << DPL::GetErrnoString());
+                    }
+                    return false;
+                }
+
+                has_config_xml = true;
+                break;
+            }
+        }
+    }
+    if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
+        LogError("Failed to close dir: " << _currentPath << " with error: "
+                << DPL::GetErrnoString());
+    }
+
+    //We must have config.xml so leaveing if we doesn't
+    if (!has_config_xml) {
+        LogError("Invalid archive");
+        *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
+        return false;
+    }
+    return true;
+}
+
+bool TaskWidgetConfig::locateAndParseConfigurationFile(
+        const std::string& _currentPath,
+        WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
+        const std::string& baseFolder,
+        int* pErrCode)
+{
+    using namespace WrtDB;
+
+    if (!pErrCode) {
+        return false;
+    }
+
+    ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
+
+    // check if this installation from browser, or not.
+    size_t pos = _currentPath.rfind("/");
+    std::ostringstream infoPath;
+    infoPath << _currentPath.substr(pos+1);
+
+    if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
+        if (_currentPath.empty() || baseFolder.empty()) {
+            *pErrCode = WRT_ERR_INVALID_ARG;
+            return false;
+        }
+        // in case of general installation using wgt archive
+        if(!parseConfigurationFileWidget(configInfo, _currentPath, pErrCode))
+        {
+            return false;
+        }
+    } else {
+        // in case of browser installation
+        if(!parseConfigurationFileBrowser(configInfo, _currentPath, pErrCode))
+        {
+            return false;
+        }
+    }
+
+    if(!fillWidgetConfig(pWidgetConfigInfo, configInfo))
+    {
+        *pErrCode = WRT_WM_ERR_INVALID_ARCHIVE;
+        return false;
+    }
+    return true;
+}
+
+bool TaskWidgetConfig::fillWidgetConfig(WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
+                                        WrtDB::ConfigParserData& configInfo)
+{
+    if (!!configInfo.widget_id) {
+        if (!pWidgetConfigInfo.guid) {
+            pWidgetConfigInfo.guid = configInfo.widget_id;
+        } else {
+            if (pWidgetConfigInfo.guid != configInfo.widget_id) {
+                LogError("Invalid archive");
+                return false;
+            }
+        }
+    }
+    if (!!configInfo.tizenId) {
+        if (pWidgetConfigInfo.pkgname_NOTNULL != *configInfo.tizenId) {
+            LogError("Invalid archive - Tizen ID not same error");
+            return false;
+        }
+    }
+    if (!!configInfo.version) {
+        if (!pWidgetConfigInfo.version) {
+            pWidgetConfigInfo.version = configInfo.version;
+        } else {
+            if (pWidgetConfigInfo.version != configInfo.version) {
+                LogError("Invalid archive");
+                return false;
+            }
+        }
+    }
+    if (!!configInfo.minVersionRequired) {
+        pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
+    } else if (!!configInfo.tizenMinVersionRequired) {
+        pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
+    }
+    return true;
+}
+
+void TaskWidgetConfig::processFile(const std::string& path,
+        WrtDB::WidgetRegisterInfo &widgetConfiguration)
+{
+    int pErrCode;
+
+    if (!locateAndParseConfigurationFile(path, widgetConfiguration,
+                                         DEFAULT_LANGUAGE, &pErrCode)) {
+        LogWarning("Widget archive: Failed while parsing config file");
+        ThrowMsg(Exception::ConfigParseFailed, path);
+    }
+}
+
 } //namespace WidgetInstall
 } //namespace Jobs