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::StepVerifyFeatures);
82 AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
85 void TaskWidgetConfig::StepProcessConfigurationFile()
89 std::string path = m_installContext.locations->getConfigurationDir();
90 LogInfo("path: " << path);
92 processFile(path, m_installContext.widgetConfig);
94 Catch(Exception::ConfigParseFailed)
96 LogError("Parsing failed.");
97 ReThrow(Exceptions::WidgetConfigFileInvalid);
100 m_installContext.job->UpdateProgress(
101 InstallerContext::INSTALL_WIDGET_CONFIG1,
102 "Parse elements of configuration file and save them");
105 void TaskWidgetConfig::ReadLocaleFolders()
107 LogDebug("Reading locale");
108 //Adding default locale
109 m_localeFolders.insert(L"");
111 std::string localePath =
112 m_installContext.locations->getConfigurationDir() + "/locales";
113 DIR* localeDir = opendir(localePath.c_str());
115 LogDebug("No /locales directory in the widget package.");
121 struct stat statStruct;
122 struct dirent dirent;
123 struct dirent *result;
126 for (return_code = readdir_r(localeDir, &dirent, &result);
127 result != NULL && return_code == 0;
128 return_code = readdir_r(localeDir, &dirent, &result))
130 DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
131 std::string absoluteDirName = localePath + "/";
132 absoluteDirName += dirent.d_name;
134 if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
135 LogError("stat() failed with " << DPL::GetErrnoString());
139 if (S_ISDIR(statStruct.st_mode)) {
140 //Yes, we ignore current, parent & hidden directories
141 if (dirName[0] != L'.') {
142 LogDebug("Adding locale directory \"" << dirName << "\"");
143 m_localeFolders.insert(dirName);
148 if (return_code != 0 || errno != 0) {
149 LogError("readdir_r() failed with " << DPL::GetErrnoString());
152 if (-1 == TEMP_FAILURE_RETRY(closedir(localeDir))) {
153 LogError("Failed to close dir: " << localePath << " with error: "
154 << DPL::GetErrnoString());
158 void TaskWidgetConfig::ProcessLocalizedStartFiles()
160 typedef DPL::String S;
162 m_installContext.widgetConfig.configInfo.startFile,
163 m_installContext.widgetConfig.configInfo.
164 startFileContentType,
165 m_installContext.widgetConfig.configInfo.startFileEncoding,
167 ProcessStartFile(S(L"index.htm"), S(L"text/html"));
168 ProcessStartFile(S(L"index.html"), S(L"text/html"));
169 ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
170 ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
171 ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
172 // TODO: we need better check if in current locales widget is valid
173 FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
174 if (it->propertiesForLocales.size() > 0) {
178 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
179 L"The Widget has no valid start file");
182 void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
183 const DPL::OptionalString& type,
184 const DPL::OptionalString& encoding,
185 bool typeForcedInConfig)
187 using namespace WrtDB;
190 WidgetRegisterInfo::LocalizedStartFile startFileData;
191 startFileData.path = *path;
193 FOREACH(i, m_localeFolders) {
194 DPL::String pathPrefix = *i;
195 if (!pathPrefix.empty()) {
196 pathPrefix = L"locales/" + pathPrefix + L"/";
199 DPL::String relativePath = pathPrefix + *path;
200 DPL::String absolutePath = DPL::FromUTF8String(
201 m_installContext.locations->getConfigurationDir()) + L"/" +
204 // get property data from packaged app
205 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
206 WidgetRegisterInfo::StartFileProperties startFileProperties;
208 startFileProperties.type = *type;
210 startFileProperties.type =
211 MimeTypeUtils::identifyFileMimeType(absolutePath);
214 //proceed only if MIME type is supported
215 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
216 startFileProperties.type))
219 startFileProperties.encoding = *encoding;
221 MimeTypeUtils::MimeAttributes attributes =
222 MimeTypeUtils::getMimeAttributes(
223 startFileProperties.type);
224 if (attributes.count(L"charset") > 0) {
225 startFileProperties.encoding =
226 attributes[L"charset"];
228 startFileProperties.encoding = L"UTF-8";
232 startFileData.propertiesForLocales[*i] =
236 //(there seems to be no similar requirement in .6,
237 //so let's throw only when mime type is
238 // provided explcitly in config.xml)
239 if (typeForcedInConfig) {
240 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
241 "Unsupported MIME type for start file.");
245 // set property data for hosted start url
246 // Hosted start url only support TIZEN WebApp
247 if (m_installContext.widgetConfig.webAppType ==
248 APP_TYPE_TIZENWEBAPP)
250 std::string startPath = DPL::ToUTF8String(
253 if (strstr(startPath.c_str(),
254 "http") == startPath.c_str())
256 WidgetRegisterInfo::StartFileProperties
259 startFileProperties.type = *type;
262 startFileProperties.encoding = *encoding;
264 startFileData.propertiesForLocales[*i] =
271 m_installContext.widgetConfig.localizationData.startFiles.push_back(
276 void TaskWidgetConfig::ProcessBackgroundPageFile()
278 if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
279 // check whether file exists
280 DPL::String backgroundPagePath = DPL::FromUTF8String(
281 m_installContext.locations->getConfigurationDir()) + L"/" +
282 *m_installContext.widgetConfig.configInfo.backgroundPage;
283 //if no then cancel installation
284 if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
285 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
286 L"Given background page file not found in archive");
291 void TaskWidgetConfig::ProcessLocalizedIcons()
293 using namespace WrtDB;
294 ProcessIcon(ConfigParserData::Icon(L"icon.svg"));
295 ProcessIcon(ConfigParserData::Icon(L"icon.ico"));
296 ProcessIcon(ConfigParserData::Icon(L"icon.png"));
297 ProcessIcon(ConfigParserData::Icon(L"icon.gif"));
298 ProcessIcon(ConfigParserData::Icon(L"icon.jpg"));
300 FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
306 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
309 bool isAnyIconValid = false;
310 //In case a default filename is passed as custom filename in config.xml, we
311 //need to keep a set of already processed filenames to avoid icon
315 using namespace WrtDB;
317 if (m_processedIconSet.count(icon.src) > 0) {
320 m_processedIconSet.insert(icon.src);
322 LocaleSet localesAvailableForIcon;
324 FOREACH(i, m_localeFolders)
326 DPL::String pathPrefix = *i;
327 if (!pathPrefix.empty()) {
328 pathPrefix = L"locales/" + pathPrefix + L"/";
331 DPL::String relativePath = pathPrefix + icon.src;
332 DPL::String absolutePath = DPL::FromUTF8String(
333 m_installContext.locations->getConfigurationDir()) + L"/" +
336 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
337 DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
339 if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
340 isAnyIconValid = true;
341 localesAvailableForIcon.insert(*i);
342 LogInfo("Icon absolutePath :" << absolutePath <<
343 ", assigned locale :" << *i << ", type: " << type);
348 if (isAnyIconValid) {
349 WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
350 localesAvailableForIcon);
351 m_installContext.widgetConfig.localizationData.icons.push_back(
356 void TaskWidgetConfig::ProcessWidgetInstalledPath()
358 LogDebug("ProcessWidgetInstalledPath");
359 m_installContext.widgetConfig.widgetInstalledPath =
361 m_installContext.locations->getPackageInstallationDir());
364 void TaskWidgetConfig::ProcessAppControlInfo()
366 LogDebug("ProcessAppControlInfo");
367 using namespace WrtDB;
369 WrtDB::ConfigParserData::AppControlInfo::Disposition disposition =
370 WrtDB::ConfigParserData::AppControlInfo::Disposition::WINDOW;
371 FOREACH(it, m_installContext.widgetConfig.configInfo.settingsList) {
372 if (!strcmp(DPL::ToUTF8String(it->m_name).c_str(), "nodisplay") &&
373 !strcmp(DPL::ToUTF8String(it->m_value).c_str(), "true"))
376 WrtDB::ConfigParserData::AppControlInfo::Disposition::INLINE;
380 std::map<std::string, int> srcMap;
382 // index = 0 is reserved for start file
383 FOREACH(startFileIt, m_installContext.widgetConfig.localizationData.startFiles)
385 if (!startFileIt->propertiesForLocales.empty()) {
386 std::string src = DPL::ToUTF8String(startFileIt->path);
387 if (srcMap.find(src) == srcMap.end()) {
388 LogDebug("Insert [" << src << "," << index << "]");
389 srcMap.insert(std::pair<std::string, int>(src, index++));
394 FOREACH(appControlIt, m_installContext.widgetConfig.configInfo.appControlList)
396 appControlIt->m_disposition = disposition;
397 std::string src = DPL::ToUTF8String(appControlIt->m_src);
398 LogDebug("src = [" << src << "]");
399 std::map<std::string, int>::iterator findIt = srcMap.find(src);
400 if (findIt == srcMap.end()) {
401 LogDebug("Insert [" << src << "," << index << "]");
402 srcMap.insert(std::pair<std::string, int>(src, index));
403 appControlIt->m_index = index++;
405 LogDebug("Exist [" << src << "," << findIt->second << "]");
406 appControlIt->m_index = findIt->second;
411 void TaskWidgetConfig::StepCheckMinVersionInfo()
413 if (!isMinVersionCompatible(
414 m_installContext.widgetConfig.webAppType.appType,
415 m_installContext.widgetConfig.minVersion))
418 "Platform version lower than required -> cancelling installation");
419 ThrowMsg(Exceptions::NotAllowed,
420 "Platform version does not meet requirements");
423 m_installContext.job->UpdateProgress(
424 InstallerContext::INSTALL_WIDGET_CONFIG2,
425 "Check MinVersion Finished");
428 void TaskWidgetConfig::StepVerifyFeatures()
430 using namespace WrtDB;
431 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
432 ConfigParserData::FeaturesList list = data.featuresList;
433 ConfigParserData::FeaturesList newList;
435 //in case of tests, this variable is unused
436 std::string featureInfo;
439 // check feature vender for permission
440 // WAC, TIZEN WebApp cannot use other feature
442 if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
445 LogInfo("This application type not allowed to use this feature");
447 Exceptions::WidgetConfigFileInvalid,
449 m_installContext.widgetConfig.webAppType.getApptypeToString()
451 "] cannot be allowed to use [" <<
452 DPL::ToUTF8String(it->name) + "] feature");
455 featureInfo += DPL::ToUTF8String(it->name);
456 featureInfo += DPL::ToUTF8String(BR);
459 if (!data.accessInfoSet.empty()) {
460 featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
461 featureInfo += DPL::ToUTF8String(BR);
463 data.featuresList = newList;
464 if (!featureInfo.empty()) {
465 m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
468 m_installContext.job->UpdateProgress(
469 InstallerContext::INSTALL_WIDGET_CONFIG2,
470 "Widget Config step2 Finished");
473 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
474 DPL::String featureName)
476 using namespace WrtDB;
477 LogInfo("AppType = [" <<
478 WidgetType(appType).getApptypeToString() << "]");
479 LogInfo("FetureName = [" << featureName << "]");
481 AppType featureType = APP_TYPE_UNKNOWN;
482 std::string featureStr = DPL::ToUTF8String(featureName);
483 const char* feature = featureStr.c_str();
485 // check prefix of feature name
486 if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
487 // Tizen WebApp feature
488 featureType = APP_TYPE_TIZENWEBAPP;
489 } else if (strstr(feature, PluginsPrefix::WACPluginsPrefix) == feature) {
491 featureType = APP_TYPE_WAC20;
492 } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
493 // W3C standard feature
494 // Both WAC and TIZEN WebApp are possible to use W3C plugins
498 // unknown feature will be checked next step
502 if (appType == featureType) {
508 bool TaskWidgetConfig::parseVersionString(const std::string &version,
511 long µVersion) const
513 std::istringstream inputString(version);
514 inputString >> majorVersion;
515 if (inputString.bad() || inputString.fail()) {
516 LogWarning("Invalid minVersion format.");
519 inputString.get(); // skip period
520 inputString >> minorVersion;
521 if (inputString.bad() || inputString.fail()) {
522 LogWarning("Invalid minVersion format");
525 inputString.get(); // skip period
526 if (inputString.bad() || inputString.fail()) {
527 inputString >> microVersion;
533 bool TaskWidgetConfig::isMinVersionCompatible(
534 WrtDB::AppType appType,
535 const DPL::OptionalString &
538 if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
539 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
542 LogWarning("minVersion attribute is empty. WRT assumes platform "
543 "supports this widget.");
548 //Parse widget version
549 long majorWidget = 0, minorWidget = 0, microWidget = 0;
550 if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
551 minorWidget, microWidget))
553 LogWarning("Invalid format of widget version string.");
557 //Parse supported version
558 long majorSupported = 0, minorSupported = 0, microSupported = 0;
560 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
561 version = WrtDB::GlobalConfig::GetTizenVersion();
562 } else if (appType == WrtDB::AppType::APP_TYPE_WAC20) {
563 version = WrtDB::GlobalConfig::GetWACVersion();
565 LogWarning("Invaild AppType");
569 if (!parseVersionString(version,
570 majorSupported, minorSupported, microSupported))
572 LogWarning("Invalid format of platform version string.");
576 if (majorWidget > majorSupported ||
577 (majorWidget == majorSupported && minorWidget > minorSupported) ||
578 (majorWidget == majorSupported && minorWidget == minorSupported
579 && microWidget > microSupported))
581 LogInfo("Platform doesn't support this widget.");
587 bool TaskWidgetConfig::isTizenWebApp() const
590 if (m_installContext.widgetConfig.webAppType.appType
591 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
599 bool TaskWidgetConfig::parseConfigurationFileBrowser(
600 WrtDB::ConfigParserData& configInfo,
601 const std::string& _currentPath)
606 parser.Parse(_currentPath, ElementParserPtr(new
610 DPL::FromUTF32String(
613 Catch(ElementParser::Exception::Base)
615 LogError("Invalid widget configuration file!");
621 bool TaskWidgetConfig::parseConfigurationFileWidget(
622 WrtDB::ConfigParserData& configInfo,
623 const std::string& _currentPath)
625 std::string configFilePath;
626 WrtUtilJoinPaths(configFilePath, _currentPath, WRT_WIDGET_CONFIG_FILE_NAME);
627 if (!WrtUtilFileExists(configFilePath))
629 LogError("Archive does not contain configuration file");
633 LogDebug("Configuration file: " << configFilePath);
638 parser.Parse(configFilePath,
639 ElementParserPtr(new RootParser<WidgetParser>(
641 DPL::FromUTF32String(L"widget"))));
644 Catch (ElementParser::Exception::Base)
646 LogError("Invalid configuration file!");
651 bool TaskWidgetConfig::locateAndParseConfigurationFile(
652 const std::string& _currentPath,
653 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
654 const std::string& baseFolder)
656 using namespace WrtDB;
658 ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
660 // check if this installation from browser, or not.
661 size_t pos = _currentPath.rfind("/");
662 std::ostringstream infoPath;
663 infoPath << _currentPath.substr(pos + 1);
665 if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
666 if (_currentPath.empty() || baseFolder.empty()) {
669 // in case of general installation using wgt archive
670 if (!parseConfigurationFileWidget(configInfo, _currentPath))
675 // in case of browser installation
676 if (!parseConfigurationFileBrowser(configInfo, _currentPath))
682 if (!fillWidgetConfig(pWidgetConfigInfo, configInfo)) {
688 bool TaskWidgetConfig::fillWidgetConfig(
689 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
690 WrtDB::ConfigParserData& configInfo)
692 if (!!configInfo.widget_id) {
693 if (!pWidgetConfigInfo.guid) {
694 pWidgetConfigInfo.guid = configInfo.widget_id;
696 if (pWidgetConfigInfo.guid != configInfo.widget_id) {
697 LogError("Invalid archive");
702 if (!!configInfo.tizenAppId) {
703 if (DPL::ToUTF8String(pWidgetConfigInfo.tzAppid).compare(
704 DPL::ToUTF8String(*configInfo.tizenAppId)) < 0)
706 LogError("Invalid archive - Tizen App ID not same error");
710 if (!!configInfo.tizenPkgId) {
711 if (pWidgetConfigInfo.tzPkgid != *configInfo.tizenPkgId) {
712 LogError("Invalid archive - Tizen Pkg ID not same error");
716 if (!!configInfo.version) {
717 if (!pWidgetConfigInfo.version) {
718 pWidgetConfigInfo.version = configInfo.version;
720 if (pWidgetConfigInfo.version != configInfo.version) {
721 LogError("Invalid archive");
726 if (!!configInfo.minVersionRequired) {
727 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
728 } else if (!!configInfo.tizenMinVersionRequired) {
729 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
734 void TaskWidgetConfig::processFile(
735 const std::string& path,
736 WrtDB::WidgetRegisterInfo &
739 if (!locateAndParseConfigurationFile(path, widgetConfiguration,
742 LogWarning("Widget archive: Failed while parsing config file");
743 ThrowMsg(Exception::ConfigParseFailed, path);
746 } //namespace WidgetInstall