2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file task_widget_config.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Implementation file for installer task widget config
28 #include <dpl/errno_string.h>
29 #include <dpl/foreach.h>
30 #include <dpl/localization/w3c_file_localization.h>
31 #include <dpl/singleton_impl.h>
32 #include <dpl/utils/mime_type_utils.h>
33 #include <dpl/utils/wrt_global_settings.h>
34 #include <dpl/utils/wrt_utility.h>
35 #include <dpl/wrt-dao-ro/global_config.h>
36 #include <dpl/wrt-dao-rw/feature_dao.h>
38 #include <libiriwrapper.h>
39 #include <parser_runner.h>
40 #include <root_parser.h>
42 #include <widget_install/job_widget_install.h>
43 #include <widget_install/task_widget_config.h>
44 #include <widget_install/widget_install_context.h>
45 #include <widget_install/widget_install_errors.h>
46 #include <widget_parser.h>
48 namespace { // anonymous
49 const DPL::String BR = DPL::FromUTF8String("<br>");
50 const std::string WIDGET_NOT_COMPATIBLE = "This widget is "
51 "not compatible with WRT.<br><br>";
52 const std::string QUESTION = "Do you want to install it anyway?";
54 const char *const DEFAULT_LANGUAGE = "default";
56 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
58 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
62 namespace WidgetInstall {
63 void InstallerTaskWidgetPopupData::PopupData::addWidgetInfo(
64 const DPL::String &info)
69 TaskWidgetConfig::TaskWidgetConfig(InstallerContext& installContext) :
70 DPL::TaskDecl<TaskWidgetConfig>(this),
71 WidgetInstallPopup(installContext),
72 m_installContext(installContext)
74 AddStep(&TaskWidgetConfig::StepProcessConfigurationFile);
75 AddStep(&TaskWidgetConfig::ReadLocaleFolders);
76 AddStep(&TaskWidgetConfig::ProcessLocalizedStartFiles);
77 AddStep(&TaskWidgetConfig::ProcessBackgroundPageFile);
78 AddStep(&TaskWidgetConfig::ProcessLocalizedIcons);
79 AddStep(&TaskWidgetConfig::ProcessWidgetInstalledPath);
80 AddStep(&TaskWidgetConfig::StepVerifyFeatures);
81 AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
83 if (!GlobalSettings::TestModeEnabled() && !m_installContext.m_quiet) {
86 StepCancelWidgetInstallationAfterVerifyFeatures);
87 AddStep(&TaskWidgetConfig::StepShowWidgetInfo);
88 AddStep(&TaskWidgetConfig::StepCancelWidgetInstallation);
89 AddStep(&TaskWidgetConfig::StepCancelWidgetInstallationAfterMinVersion);
90 AddStep(&TaskWidgetConfig::StepDeletePopupWin);
94 void TaskWidgetConfig::StepProcessConfigurationFile()
98 std::string path = m_installContext.locations->getConfigurationDir();
99 LogInfo("path: " << path);
101 processFile(path, m_installContext.widgetConfig);
103 Catch(Exception::ConfigParseFailed)
105 LogError("Parsing failed.");
106 ReThrow(Exceptions::WidgetConfigFileInvalid);
109 m_installContext.job->UpdateProgress(
110 InstallerContext::INSTALL_WIDGET_CONFIG1,
111 "Parse elements of configuration file and save them");
114 void TaskWidgetConfig::ReadLocaleFolders()
116 LogDebug("Reading locale");
117 //Adding default locale
118 m_localeFolders.insert(L"");
120 std::string localePath =
121 m_installContext.locations->getConfigurationDir() + "/locales";
122 DIR* localeDir = opendir(localePath.c_str());
124 LogDebug("No /locales directory in the widget package.");
130 struct stat statStruct;
131 struct dirent dirent;
132 struct dirent *result;
135 for (return_code = readdir_r(localeDir, &dirent, &result);
136 result != NULL && return_code == 0;
137 return_code = readdir_r(localeDir, &dirent, &result))
139 DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
140 std::string absoluteDirName = localePath + "/";
141 absoluteDirName += dirent.d_name;
143 if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
144 LogError("stat() failed with " << DPL::GetErrnoString());
148 if (S_ISDIR(statStruct.st_mode)) {
149 //Yes, we ignore current, parent & hidden directories
150 if (dirName[0] != L'.') {
151 LogDebug("Adding locale directory \"" << dirName << "\"");
152 m_localeFolders.insert(dirName);
157 if (return_code != 0 || errno != 0) {
158 LogError("readdir_r() failed with " << DPL::GetErrnoString());
161 if (-1 == TEMP_FAILURE_RETRY(closedir(localeDir))) {
162 LogError("Failed to close dir: " << localePath << " with error: "
163 << DPL::GetErrnoString());
167 void TaskWidgetConfig::ProcessLocalizedStartFiles()
169 typedef DPL::String S;
171 m_installContext.widgetConfig.configInfo.startFile,
172 m_installContext.widgetConfig.configInfo.
173 startFileContentType,
174 m_installContext.widgetConfig.configInfo.startFileEncoding,
176 ProcessStartFile(S(L"index.htm"), S(L"text/html"));
177 ProcessStartFile(S(L"index.html"), S(L"text/html"));
178 ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
179 ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
180 ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
181 // TODO: (l.wrzosek) we need better check if in current locales widget is
183 FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
184 if (it->propertiesForLocales.size() > 0) {
188 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
189 L"The Widget has no valid start file");
192 void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
193 const DPL::OptionalString& type,
194 const DPL::OptionalString& encoding,
195 bool typeForcedInConfig)
197 using namespace WrtDB;
200 WidgetRegisterInfo::LocalizedStartFile startFileData;
201 startFileData.path = *path;
203 FOREACH(i, m_localeFolders) {
204 DPL::String pathPrefix = *i;
205 if (!pathPrefix.empty()) {
206 pathPrefix = L"locales/" + pathPrefix + L"/";
209 DPL::String relativePath = pathPrefix + *path;
210 DPL::String absolutePath = DPL::FromUTF8String(
211 m_installContext.locations->getConfigurationDir()) + L"/" +
214 // get property data from packaged app
215 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
216 WidgetRegisterInfo::StartFileProperties startFileProperties;
218 startFileProperties.type = *type;
220 startFileProperties.type =
221 MimeTypeUtils::identifyFileMimeType(absolutePath);
224 //proceed only if MIME type is supported
225 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
226 startFileProperties.type))
229 startFileProperties.encoding = *encoding;
231 MimeTypeUtils::MimeAttributes attributes =
232 MimeTypeUtils::getMimeAttributes(
233 startFileProperties.type);
234 if (attributes.count(L"charset") > 0) {
235 startFileProperties.encoding =
236 attributes[L"charset"];
238 startFileProperties.encoding = L"UTF-8";
242 startFileData.propertiesForLocales[*i] =
246 //(there seems to be no similar requirement in .6,
247 //so let's throw only when mime type is
248 // provided explcitly in config.xml)
249 if (typeForcedInConfig) {
250 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
251 "Unsupported MIME type for start file.");
255 // set property data for hosted start url
256 // Hosted start url only support TIZEN WebApp
257 if (m_installContext.widgetConfig.webAppType ==
258 APP_TYPE_TIZENWEBAPP)
260 std::string startPath = DPL::ToUTF8String(
263 if (strstr(startPath.c_str(),
264 "http") == startPath.c_str())
266 WidgetRegisterInfo::StartFileProperties
269 startFileProperties.type = *type;
272 startFileProperties.encoding = *encoding;
274 startFileData.propertiesForLocales[*i] =
281 m_installContext.widgetConfig.localizationData.startFiles.push_back(
286 void TaskWidgetConfig::ProcessBackgroundPageFile()
288 if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
289 // check whether file exists
290 DPL::String backgroundPagePath = DPL::FromUTF8String(
291 m_installContext.locations->getConfigurationDir()) + L"/" +
292 *m_installContext.widgetConfig.configInfo.backgroundPage;
293 //if no then cancel installation
294 if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
295 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
296 L"Given background page file not found in archive");
301 void TaskWidgetConfig::ProcessLocalizedIcons()
303 using namespace WrtDB;
304 ProcessIcon(ConfigParserData::Icon(L"icon.svg"));
305 ProcessIcon(ConfigParserData::Icon(L"icon.ico"));
306 ProcessIcon(ConfigParserData::Icon(L"icon.png"));
307 ProcessIcon(ConfigParserData::Icon(L"icon.gif"));
308 ProcessIcon(ConfigParserData::Icon(L"icon.jpg"));
310 FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
316 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
319 bool isAnyIconValid = false;
320 //In case a default filename is passed as custom filename in config.xml, we
321 //need to keep a set of already processed filenames to avoid icon
325 using namespace WrtDB;
327 if (m_processedIconSet.count(icon.src) > 0) {
330 m_processedIconSet.insert(icon.src);
332 LocaleSet localesAvailableForIcon;
334 FOREACH(i, m_localeFolders)
336 DPL::String pathPrefix = *i;
337 if (!pathPrefix.empty()) {
338 pathPrefix = L"locales/" + pathPrefix + L"/";
341 DPL::String relativePath = pathPrefix + icon.src;
342 DPL::String absolutePath = DPL::FromUTF8String(
343 m_installContext.locations->getConfigurationDir()) + L"/" +
346 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
347 DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
349 if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
350 isAnyIconValid = true;
351 localesAvailableForIcon.insert(*i);
352 LogInfo("Icon absolutePath :" << absolutePath <<
353 ", assigned locale :" << *i << ", type: " << type);
358 if (isAnyIconValid) {
359 WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
360 localesAvailableForIcon);
361 m_installContext.widgetConfig.localizationData.icons.push_back(
366 void TaskWidgetConfig::ProcessWidgetInstalledPath()
368 LogDebug("ProcessWidgetInstalledPath");
369 m_installContext.widgetConfig.widgetInstalledPath =
371 m_installContext.locations->getPackageInstallationDir());
374 void TaskWidgetConfig::StepCancelWidgetInstallationAfterVerifyFeatures()
376 LogDebug("StepCancelWidgetInstallationAfterVerifyFeatures");
377 if (InfoPopupButton::WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
378 m_installCancel = WRT_POPUP_BUTTON;
380 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Widget not allowed");
384 void TaskWidgetConfig::StepCancelWidgetInstallation()
386 if (InfoPopupButton::WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
387 m_installCancel = WRT_POPUP_BUTTON;
389 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
393 void TaskWidgetConfig::StepCancelWidgetInstallationAfterMinVersion()
395 if (InfoPopupButton::WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
396 m_installCancel = WRT_POPUP_BUTTON;
398 ThrowMsg(Exceptions::NotAllowed, "WRT version incompatible.");
402 void TaskWidgetConfig::createInstallPopup(PopupType type,
403 const std::string &label)
405 m_installContext.job->Pause();
410 bool ret = createPopup();
412 loadPopup(type, label);
417 void TaskWidgetConfig::StepDeletePopupWin()
422 void TaskWidgetConfig::StepShowWidgetInfo()
424 if (!m_popupData.widgetInfo.empty()) {
425 std::string label = DPL::ToUTF8String(m_popupData.widgetInfo);
426 createInstallPopup(PopupType::WIDGET_FEATURE_INFO, label);
427 m_installContext.job->UpdateProgress(
428 InstallerContext::INSTALL_WIDGET_CONFIG2,
429 "Show Widget Info Finished");
433 void TaskWidgetConfig::StepCheckMinVersionInfo()
435 if (!isMinVersionCompatible(
436 m_installContext.widgetConfig.webAppType.appType,
437 m_installContext.widgetConfig.minVersion))
439 if (!GlobalSettings::TestModeEnabled() && !m_installContext.m_quiet) {
440 LogDebug("Platform version to low - launching");
441 std::string label = WIDGET_NOT_COMPATIBLE + QUESTION;
442 createInstallPopup(PopupType::WIDGET_MIN_VERSION, label);
445 "Platform version lower than required -> cancelling installation");
446 ThrowMsg(Exceptions::NotAllowed,
447 "Platform version does not meet requirements");
451 m_installContext.job->UpdateProgress(
452 InstallerContext::INSTALL_WIDGET_CONFIG2,
453 "Check MinVersion Finished");
456 void TaskWidgetConfig::StepVerifyFeatures()
458 using namespace WrtDB;
459 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
460 ConfigParserData::FeaturesList list = data.featuresList;
461 ConfigParserData::FeaturesList newList;
463 //in case of tests, this variable is unused
464 std::string featureInfo;
467 // check feature vender for permission
468 // WAC, TIZEN WebApp cannot use other feature
470 if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
473 LogInfo("This application type not allowed to use this feature");
475 Exceptions::WidgetConfigFileInvalid,
477 m_installContext.widgetConfig.webAppType.getApptypeToString()
479 "] cannot be allowed to use [" <<
480 DPL::ToUTF8String(it->name) + "] feature");
483 featureInfo += DPL::ToUTF8String(it->name);
484 featureInfo += DPL::ToUTF8String(BR);
487 if (!data.accessInfoSet.empty()) {
488 featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
489 featureInfo += DPL::ToUTF8String(BR);
491 data.featuresList = newList;
492 if (!featureInfo.empty()) {
493 m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
496 m_installContext.job->UpdateProgress(
497 InstallerContext::INSTALL_WIDGET_CONFIG2,
498 "Widget Config step2 Finished");
501 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
502 DPL::String featureName)
504 using namespace WrtDB;
505 LogInfo("AppType = [" <<
506 WidgetType(appType).getApptypeToString() << "]");
507 LogInfo("FetureName = [" << featureName << "]");
509 AppType featureType = APP_TYPE_UNKNOWN;
510 std::string featureStr = DPL::ToUTF8String(featureName);
511 const char* feature = featureStr.c_str();
513 // check prefix of feature name
514 if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
515 // Tizen WebApp feature
516 featureType = APP_TYPE_TIZENWEBAPP;
517 } else if (strstr(feature, PluginsPrefix::WACPluginsPrefix) == feature) {
519 featureType = APP_TYPE_WAC20;
520 } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
521 // W3C standard feature
522 // Both WAC and TIZEN WebApp are possible to use W3C plugins
526 // unknown feature will be checked next step
530 if (appType == featureType) {
536 bool TaskWidgetConfig::parseVersionString(const std::string &version,
539 long µVersion) const
541 std::istringstream inputString(version);
542 inputString >> majorVersion;
543 if (inputString.bad() || inputString.fail()) {
544 LogWarning("Invalid minVersion format.");
547 inputString.get(); // skip period
548 inputString >> minorVersion;
549 if (inputString.bad() || inputString.fail()) {
550 LogWarning("Invalid minVersion format");
553 inputString.get(); // skip period
554 if (inputString.bad() || inputString.fail()) {
555 inputString >> microVersion;
561 bool TaskWidgetConfig::isMinVersionCompatible(
562 WrtDB::AppType appType,
563 const DPL::OptionalString &
566 if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
567 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
570 LogWarning("minVersion attribute is empty. WRT assumes platform "
571 "supports this widget.");
576 //Parse widget version
577 long majorWidget = 0, minorWidget = 0, microWidget = 0;
578 if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
579 minorWidget, microWidget))
581 LogWarning("Invalid format of widget version string.");
585 //Parse supported version
586 long majorSupported = 0, minorSupported = 0, microSupported = 0;
588 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
589 version = WrtDB::GlobalConfig::GetTizenVersion();
590 } else if (appType == WrtDB::AppType::APP_TYPE_WAC20) {
591 version = WrtDB::GlobalConfig::GetWACVersion();
593 LogWarning("Invaild AppType");
597 if (!parseVersionString(version,
598 majorSupported, minorSupported, microSupported))
600 LogWarning("Invalid format of platform version string.");
604 if (majorWidget > majorSupported ||
605 (majorWidget == majorSupported && minorWidget > minorSupported) ||
606 (majorWidget == majorSupported && minorWidget == minorSupported
607 && microWidget > microSupported))
609 LogInfo("Platform doesn't support this widget.");
615 bool TaskWidgetConfig::isTizenWebApp() const
618 if (m_installContext.widgetConfig.webAppType.appType
619 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
627 bool TaskWidgetConfig::parseConfigurationFileBrowser(
628 WrtDB::ConfigParserData& configInfo,
629 const std::string& _currentPath)
634 parser.Parse(_currentPath, ElementParserPtr(new
638 DPL::FromUTF32String(
641 Catch(ElementParser::Exception::Base)
643 LogError("Invalid widget configuration file!");
649 bool TaskWidgetConfig::parseConfigurationFileWidget(
650 WrtDB::ConfigParserData& configInfo,
651 const std::string& _currentPath)
653 std::string configFilePath;
654 WrtUtilJoinPaths(configFilePath, _currentPath, WRT_WIDGET_CONFIG_FILE_NAME);
655 if (!WrtUtilFileExists(configFilePath))
657 LogError("Archive does not contain configuration file");
661 LogDebug("Configuration file: " << configFilePath);
666 parser.Parse(configFilePath,
667 ElementParserPtr(new RootParser<WidgetParser>(
669 DPL::FromUTF32String(L"widget"))));
672 Catch (ElementParser::Exception::Base)
674 LogError("Invalid configuration file!");
679 bool TaskWidgetConfig::locateAndParseConfigurationFile(
680 const std::string& _currentPath,
681 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
682 const std::string& baseFolder)
684 using namespace WrtDB;
686 ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
688 // check if this installation from browser, or not.
689 size_t pos = _currentPath.rfind("/");
690 std::ostringstream infoPath;
691 infoPath << _currentPath.substr(pos + 1);
693 if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
694 if (_currentPath.empty() || baseFolder.empty()) {
697 // in case of general installation using wgt archive
698 if (!parseConfigurationFileWidget(configInfo, _currentPath))
703 // in case of browser installation
704 if (!parseConfigurationFileBrowser(configInfo, _currentPath))
710 if (!fillWidgetConfig(pWidgetConfigInfo, configInfo)) {
716 bool TaskWidgetConfig::fillWidgetConfig(
717 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
718 WrtDB::ConfigParserData& configInfo)
720 if (!!configInfo.widget_id) {
721 if (!pWidgetConfigInfo.guid) {
722 pWidgetConfigInfo.guid = configInfo.widget_id;
724 if (pWidgetConfigInfo.guid != configInfo.widget_id) {
725 LogError("Invalid archive");
730 if (!!configInfo.tizenAppId) {
731 if (DPL::ToUTF8String(pWidgetConfigInfo.tzAppid).compare(
732 DPL::ToUTF8String(*configInfo.tizenAppId)) < 0)
734 LogError("Invalid archive - Tizen App ID not same error");
738 if (!!configInfo.tizenPkgId) {
739 if (pWidgetConfigInfo.tzPkgid != *configInfo.tizenPkgId) {
740 LogError("Invalid archive - Tizen Pkg ID not same error");
744 if (!!configInfo.version) {
745 if (!pWidgetConfigInfo.version) {
746 pWidgetConfigInfo.version = configInfo.version;
748 if (pWidgetConfigInfo.version != configInfo.version) {
749 LogError("Invalid archive");
754 if (!!configInfo.minVersionRequired) {
755 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
756 } else if (!!configInfo.tizenMinVersionRequired) {
757 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
762 void TaskWidgetConfig::processFile(
763 const std::string& path,
764 WrtDB::WidgetRegisterInfo &
767 if (!locateAndParseConfigurationFile(path, widgetConfiguration,
770 LogWarning("Widget archive: Failed while parsing config file");
771 ThrowMsg(Exception::ConfigParseFailed, path);
774 } //namespace WidgetInstall