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
29 #include <dpl/errno_string.h>
30 #include <dpl/foreach.h>
31 #include <dpl/localization/w3c_file_localization.h>
32 #include <dpl/singleton_impl.h>
33 #include <dpl/utils/mime_type_utils.h>
34 #include <dpl/utils/wrt_global_settings.h>
35 #include <dpl/utils/wrt_utility.h>
36 #include <dpl/wrt-dao-ro/global_config.h>
37 #include <dpl/wrt-dao-rw/feature_dao.h>
39 #include <libiriwrapper.h>
40 #include <parser_runner.h>
41 #include <root_parser.h>
43 #include <widget_install/job_widget_install.h>
44 #include <widget_install/task_widget_config.h>
45 #include <widget_install/widget_install_context.h>
46 #include <widget_install/widget_install_errors.h>
47 #include <widget_parser.h>
49 namespace { // anonymous
50 const DPL::String BR = DPL::FromUTF8String("<br>");
51 const std::string WIDGET_NOT_COMPATIBLE = "This widget is "
52 "not compatible with WRT.<br><br>";
53 const std::string QUESTION = "Do you want to install it anyway?";
55 const char *const DEFAULT_LANGUAGE = "default";
57 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
59 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
63 namespace WidgetInstall {
64 void InstallerTaskWidgetPopupData::PopupData::addWidgetInfo(
65 const DPL::String &info)
70 TaskWidgetConfig::TaskWidgetConfig(InstallerContext& installContext) :
71 DPL::TaskDecl<TaskWidgetConfig>(this),
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::ProcessAppControlInfo);
81 AddStep(&TaskWidgetConfig::ProcessSecurityModel);
82 AddStep(&TaskWidgetConfig::StepVerifyFeatures);
83 AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
86 void TaskWidgetConfig::StepProcessConfigurationFile()
90 std::string path = m_installContext.locations->getConfigurationDir();
91 LogInfo("path: " << path);
93 processFile(path, m_installContext.widgetConfig);
95 Catch(Exception::ConfigParseFailed)
97 LogError("Parsing failed.");
98 ReThrow(Exceptions::WidgetConfigFileInvalid);
101 m_installContext.job->UpdateProgress(
102 InstallerContext::INSTALL_WIDGET_CONFIG1,
103 "Parse elements of configuration file and save them");
106 void TaskWidgetConfig::ReadLocaleFolders()
108 LogDebug("Reading locale");
109 //Adding default locale
110 m_localeFolders.insert(L"");
112 std::string localePath =
113 m_installContext.locations->getConfigurationDir() + "/locales";
114 DIR* localeDir = opendir(localePath.c_str());
116 LogDebug("No /locales directory in the widget package.");
122 struct stat statStruct;
123 struct dirent dirent;
124 struct dirent *result;
127 for (return_code = readdir_r(localeDir, &dirent, &result);
128 result != NULL && return_code == 0;
129 return_code = readdir_r(localeDir, &dirent, &result))
131 DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
132 std::string absoluteDirName = localePath + "/";
133 absoluteDirName += dirent.d_name;
135 if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
136 LogError("stat() failed with " << DPL::GetErrnoString());
140 if (S_ISDIR(statStruct.st_mode)) {
141 //Yes, we ignore current, parent & hidden directories
142 if (dirName[0] != L'.') {
143 LogDebug("Adding locale directory \"" << dirName << "\"");
144 m_localeFolders.insert(dirName);
149 if (return_code != 0 || errno != 0) {
150 LogError("readdir_r() failed with " << DPL::GetErrnoString());
153 if (-1 == TEMP_FAILURE_RETRY(closedir(localeDir))) {
154 LogError("Failed to close dir: " << localePath << " with error: "
155 << DPL::GetErrnoString());
159 void TaskWidgetConfig::ProcessLocalizedStartFiles()
161 typedef DPL::String S;
163 m_installContext.widgetConfig.configInfo.startFile,
164 m_installContext.widgetConfig.configInfo.
165 startFileContentType,
166 m_installContext.widgetConfig.configInfo.startFileEncoding,
168 ProcessStartFile(S(L"index.htm"), S(L"text/html"));
169 ProcessStartFile(S(L"index.html"), S(L"text/html"));
170 ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
171 ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
172 ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
173 // TODO: we need better check if in current locales widget is valid
174 FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
175 if (it->propertiesForLocales.size() > 0) {
179 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
180 L"The Widget has no valid start file");
183 void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
184 const DPL::OptionalString& type,
185 const DPL::OptionalString& encoding,
186 bool typeForcedInConfig)
188 using namespace WrtDB;
191 WidgetRegisterInfo::LocalizedStartFile startFileData;
192 startFileData.path = *path;
194 FOREACH(i, m_localeFolders) {
195 DPL::String pathPrefix = *i;
196 if (!pathPrefix.empty()) {
197 pathPrefix = L"locales/" + pathPrefix + L"/";
200 DPL::String relativePath = pathPrefix + *path;
201 DPL::String absolutePath = DPL::FromUTF8String(
202 m_installContext.locations->getConfigurationDir()) + L"/" +
205 // get property data from packaged app
206 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
207 WidgetRegisterInfo::StartFileProperties startFileProperties;
209 startFileProperties.type = *type;
211 startFileProperties.type =
212 MimeTypeUtils::identifyFileMimeType(absolutePath);
215 //proceed only if MIME type is supported
216 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
217 startFileProperties.type))
220 startFileProperties.encoding = *encoding;
222 MimeTypeUtils::MimeAttributes attributes =
223 MimeTypeUtils::getMimeAttributes(
224 startFileProperties.type);
225 if (attributes.count(L"charset") > 0) {
226 startFileProperties.encoding =
227 attributes[L"charset"];
229 startFileProperties.encoding = L"UTF-8";
233 startFileData.propertiesForLocales[*i] =
237 //(there seems to be no similar requirement in .6,
238 //so let's throw only when mime type is
239 // provided explcitly in config.xml)
240 if (typeForcedInConfig) {
241 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
242 "Unsupported MIME type for start file.");
246 // set property data for hosted start url
247 // Hosted start url only support TIZEN WebApp
248 if (m_installContext.widgetConfig.webAppType ==
249 APP_TYPE_TIZENWEBAPP)
251 std::string startPath = DPL::ToUTF8String(
254 if (strstr(startPath.c_str(),
255 "http") == startPath.c_str())
257 WidgetRegisterInfo::StartFileProperties
260 startFileProperties.type = *type;
263 startFileProperties.encoding = *encoding;
265 startFileData.propertiesForLocales[*i] =
272 m_installContext.widgetConfig.localizationData.startFiles.push_back(
277 void TaskWidgetConfig::ProcessBackgroundPageFile()
279 if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
280 // check whether file exists
281 DPL::String backgroundPagePath = DPL::FromUTF8String(
282 m_installContext.locations->getConfigurationDir()) + L"/" +
283 *m_installContext.widgetConfig.configInfo.backgroundPage;
284 //if no then cancel installation
285 if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
286 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
287 L"Given background page file not found in archive");
292 void TaskWidgetConfig::ProcessLocalizedIcons()
294 using namespace WrtDB;
295 ProcessIcon(ConfigParserData::Icon(L"icon.svg"));
296 ProcessIcon(ConfigParserData::Icon(L"icon.ico"));
297 ProcessIcon(ConfigParserData::Icon(L"icon.png"));
298 ProcessIcon(ConfigParserData::Icon(L"icon.gif"));
299 ProcessIcon(ConfigParserData::Icon(L"icon.jpg"));
301 FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
307 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
310 bool isAnyIconValid = false;
311 //In case a default filename is passed as custom filename in config.xml, we
312 //need to keep a set of already processed filenames to avoid icon
316 using namespace WrtDB;
318 if (m_processedIconSet.count(icon.src) > 0) {
321 m_processedIconSet.insert(icon.src);
323 LocaleSet localesAvailableForIcon;
325 FOREACH(i, m_localeFolders)
327 DPL::String pathPrefix = *i;
328 if (!pathPrefix.empty()) {
329 pathPrefix = L"locales/" + pathPrefix + L"/";
332 DPL::String relativePath = pathPrefix + icon.src;
333 DPL::String absolutePath = DPL::FromUTF8String(
334 m_installContext.locations->getConfigurationDir()) + L"/" +
337 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
338 DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
340 if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
341 isAnyIconValid = true;
342 localesAvailableForIcon.insert(*i);
343 LogInfo("Icon absolutePath :" << absolutePath <<
344 ", assigned locale :" << *i << ", type: " << type);
349 if (isAnyIconValid) {
350 WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
351 localesAvailableForIcon);
352 m_installContext.widgetConfig.localizationData.icons.push_back(
357 void TaskWidgetConfig::ProcessWidgetInstalledPath()
359 LogDebug("ProcessWidgetInstalledPath");
360 m_installContext.widgetConfig.widgetInstalledPath =
362 m_installContext.locations->getPackageInstallationDir());
365 void TaskWidgetConfig::ProcessAppControlInfo()
367 LogDebug("ProcessAppControlInfo");
368 using namespace WrtDB;
370 WrtDB::ConfigParserData::AppControlInfo::Disposition disposition =
371 WrtDB::ConfigParserData::AppControlInfo::Disposition::WINDOW;
372 FOREACH(it, m_installContext.widgetConfig.configInfo.settingsList) {
373 if (!strcmp(DPL::ToUTF8String(it->m_name).c_str(), "nodisplay") &&
374 !strcmp(DPL::ToUTF8String(it->m_value).c_str(), "true"))
377 WrtDB::ConfigParserData::AppControlInfo::Disposition::INLINE;
381 std::map<std::string, int> srcMap;
383 // index = 0 is reserved for start file
384 FOREACH(startFileIt, m_installContext.widgetConfig.localizationData.startFiles)
386 if (!startFileIt->propertiesForLocales.empty()) {
387 std::string src = DPL::ToUTF8String(startFileIt->path);
388 if (srcMap.find(src) == srcMap.end()) {
389 LogDebug("Insert [" << src << "," << index << "]");
390 srcMap.insert(std::pair<std::string, int>(src, index++));
395 FOREACH(appControlIt, m_installContext.widgetConfig.configInfo.appControlList)
397 appControlIt->m_disposition = disposition;
398 std::string src = DPL::ToUTF8String(appControlIt->m_src);
399 LogDebug("src = [" << src << "]");
400 std::map<std::string, int>::iterator findIt = srcMap.find(src);
401 if (findIt == srcMap.end()) {
402 LogDebug("Insert [" << src << "," << index << "]");
403 srcMap.insert(std::pair<std::string, int>(src, index));
404 appControlIt->m_index = index++;
406 LogDebug("Exist [" << src << "," << findIt->second << "]");
407 appControlIt->m_index = findIt->second;
412 void TaskWidgetConfig::ProcessSecurityModel()
414 bool isSecurityModelV1 = false;
415 bool isSecurityModelV2 = false;
416 WrtDB::ConfigParserData &data = m_installContext.widgetConfig.configInfo;
419 if (!data.accessInfoSet.empty()) {
420 isSecurityModelV1 = true;
424 if (!data.cspPolicy.IsNull() || !data.cspPolicyReportOnly.IsNull()) {
425 isSecurityModelV2 = true;
428 // allow-navigation is V2
429 if (!data.allowNavigationInfoList.empty()) {
430 isSecurityModelV2 = true;
433 if (isSecurityModelV1 && isSecurityModelV2) {
434 LogError("Security model is conflict");
435 ThrowMsg(Exceptions::NotAllowed, "Security model is conflict");
436 } else if (isSecurityModelV1) {
437 data.securityModelVersion =
438 WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
439 } else if (isSecurityModelV2) {
440 data.securityModelVersion =
441 WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V2;
443 data.securityModelVersion =
444 WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
447 m_installContext.job->UpdateProgress(
448 InstallerContext::INSTALL_WIDGET_CONFIG2,
449 "Finished process security model");
452 void TaskWidgetConfig::StepCheckMinVersionInfo()
454 if (!isMinVersionCompatible(
455 m_installContext.widgetConfig.webAppType.appType,
456 m_installContext.widgetConfig.minVersion))
459 "Platform version lower than required -> cancelling installation");
460 ThrowMsg(Exceptions::NotAllowed,
461 "Platform version does not meet requirements");
464 m_installContext.job->UpdateProgress(
465 InstallerContext::INSTALL_WIDGET_CONFIG2,
466 "Check MinVersion Finished");
469 void TaskWidgetConfig::StepVerifyFeatures()
471 using namespace WrtDB;
472 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
473 ConfigParserData::FeaturesList list = data.featuresList;
474 ConfigParserData::FeaturesList newList;
476 //in case of tests, this variable is unused
477 std::string featureInfo;
480 // check feature vender for permission
481 // WAC, TIZEN WebApp cannot use other feature
483 if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
486 LogInfo("This application type not allowed to use this feature");
488 Exceptions::WidgetConfigFileInvalid,
490 m_installContext.widgetConfig.webAppType.getApptypeToString()
492 "] cannot be allowed to use [" <<
493 DPL::ToUTF8String(it->name) + "] feature");
496 featureInfo += DPL::ToUTF8String(it->name);
497 featureInfo += DPL::ToUTF8String(BR);
500 if (!data.accessInfoSet.empty()) {
501 featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
502 featureInfo += DPL::ToUTF8String(BR);
504 data.featuresList = newList;
505 if (!featureInfo.empty()) {
506 m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
509 m_installContext.job->UpdateProgress(
510 InstallerContext::INSTALL_WIDGET_CONFIG2,
511 "Widget Config step2 Finished");
514 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
515 DPL::String featureName)
517 using namespace WrtDB;
518 LogInfo("AppType = [" <<
519 WidgetType(appType).getApptypeToString() << "]");
520 LogInfo("FetureName = [" << featureName << "]");
522 AppType featureType = APP_TYPE_UNKNOWN;
523 std::string featureStr = DPL::ToUTF8String(featureName);
524 const char* feature = featureStr.c_str();
526 // check prefix of feature name
527 if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
528 // Tizen WebApp feature
529 featureType = APP_TYPE_TIZENWEBAPP;
530 } else if (strstr(feature, PluginsPrefix::WACPluginsPrefix) == feature) {
532 featureType = APP_TYPE_WAC20;
533 } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
534 // W3C standard feature
535 // Both WAC and TIZEN WebApp are possible to use W3C plugins
539 // unknown feature will be checked next step
543 if (appType == featureType) {
549 bool TaskWidgetConfig::parseVersionString(const std::string &version,
552 long µVersion) const
554 std::istringstream inputString(version);
555 inputString >> majorVersion;
556 if (inputString.bad() || inputString.fail()) {
557 LogWarning("Invalid minVersion format.");
560 inputString.get(); // skip period
561 inputString >> minorVersion;
562 if (inputString.bad() || inputString.fail()) {
563 LogWarning("Invalid minVersion format");
566 inputString.get(); // skip period
567 if (inputString.bad() || inputString.fail()) {
568 inputString >> microVersion;
574 bool TaskWidgetConfig::isMinVersionCompatible(
575 WrtDB::AppType appType,
576 const DPL::OptionalString &
579 if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
580 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
583 LogWarning("minVersion attribute is empty. WRT assumes platform "
584 "supports this widget.");
589 //Parse widget version
590 long majorWidget = 0, minorWidget = 0, microWidget = 0;
591 if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
592 minorWidget, microWidget))
594 LogWarning("Invalid format of widget version string.");
598 //Parse supported version
599 long majorSupported = 0, minorSupported = 0, microSupported = 0;
601 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
602 version = WrtDB::GlobalConfig::GetTizenVersion();
603 } else if (appType == WrtDB::AppType::APP_TYPE_WAC20) {
604 version = WrtDB::GlobalConfig::GetWACVersion();
606 LogWarning("Invaild AppType");
610 if (!parseVersionString(version,
611 majorSupported, minorSupported, microSupported))
613 LogWarning("Invalid format of platform version string.");
617 if (majorWidget > majorSupported ||
618 (majorWidget == majorSupported && minorWidget > minorSupported) ||
619 (majorWidget == majorSupported && minorWidget == minorSupported
620 && microWidget > microSupported))
622 LogInfo("Platform doesn't support this widget.");
628 bool TaskWidgetConfig::isTizenWebApp() const
631 if (m_installContext.widgetConfig.webAppType.appType
632 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
640 bool TaskWidgetConfig::parseConfigurationFileBrowser(
641 WrtDB::ConfigParserData& configInfo,
642 const std::string& _currentPath)
647 parser.Parse(_currentPath, ElementParserPtr(new
651 DPL::FromUTF32String(
654 Catch(ElementParser::Exception::Base)
656 LogError("Invalid widget configuration file!");
662 bool TaskWidgetConfig::parseConfigurationFileWidget(
663 WrtDB::ConfigParserData& configInfo,
664 const std::string& _currentPath)
666 std::string configFilePath;
667 WrtUtilJoinPaths(configFilePath, _currentPath, WRT_WIDGET_CONFIG_FILE_NAME);
668 if (!WrtUtilFileExists(configFilePath))
670 LogError("Archive does not contain configuration file");
674 LogDebug("Configuration file: " << configFilePath);
679 parser.Parse(configFilePath,
680 ElementParserPtr(new RootParser<WidgetParser>(
682 DPL::FromUTF32String(L"widget"))));
685 Catch (ElementParser::Exception::Base)
687 LogError("Invalid configuration file!");
692 bool TaskWidgetConfig::locateAndParseConfigurationFile(
693 const std::string& _currentPath,
694 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
695 const std::string& baseFolder)
697 using namespace WrtDB;
699 ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
701 // check if this installation from browser, or not.
702 size_t pos = _currentPath.rfind("/");
703 std::ostringstream infoPath;
704 infoPath << _currentPath.substr(pos + 1);
706 if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
707 if (_currentPath.empty() || baseFolder.empty()) {
710 // in case of general installation using wgt archive
711 if (!parseConfigurationFileWidget(configInfo, _currentPath))
716 // in case of browser installation
717 if (!parseConfigurationFileBrowser(configInfo, _currentPath))
723 if (!fillWidgetConfig(pWidgetConfigInfo, configInfo)) {
729 bool TaskWidgetConfig::fillWidgetConfig(
730 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
731 WrtDB::ConfigParserData& configInfo)
733 if (!!configInfo.widget_id) {
734 if (!pWidgetConfigInfo.guid) {
735 pWidgetConfigInfo.guid = configInfo.widget_id;
737 if (pWidgetConfigInfo.guid != configInfo.widget_id) {
738 LogError("Invalid archive");
743 if (!!configInfo.tizenAppId) {
744 if (DPL::ToUTF8String(pWidgetConfigInfo.tzAppid).compare(
745 DPL::ToUTF8String(*configInfo.tizenAppId)) < 0)
747 LogError("Invalid archive - Tizen App ID not same error");
751 if (!!configInfo.tizenPkgId) {
752 if (pWidgetConfigInfo.tzPkgid != *configInfo.tizenPkgId) {
753 LogError("Invalid archive - Tizen Pkg ID not same error");
757 if (!!configInfo.version) {
758 if (!pWidgetConfigInfo.version) {
759 pWidgetConfigInfo.version = configInfo.version;
761 if (pWidgetConfigInfo.version != configInfo.version) {
762 LogError("Invalid archive");
767 if (!!configInfo.minVersionRequired) {
768 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
769 } else if (!!configInfo.tizenMinVersionRequired) {
770 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
775 void TaskWidgetConfig::processFile(
776 const std::string& path,
777 WrtDB::WidgetRegisterInfo &
780 if (!locateAndParseConfigurationFile(path, widgetConfiguration,
783 LogWarning("Widget archive: Failed while parsing config file");
784 ThrowMsg(Exception::ConfigParseFailed, path);
787 } //namespace WidgetInstall