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
24 #include <dpl/foreach.h>
25 #include <dpl/errno_string.h>
26 #include <dpl/wrt-dao-rw/feature_dao.h>
27 #include <dpl/wrt-dao-ro/global_config.h>
28 #include <dpl/utils/wrt_utility.h>
29 #include <root_parser.h>
30 #include <widget_parser.h>
31 #include <parser_runner.h>
32 #include <libiriwrapper.h>
33 #include <widget_install/task_widget_config.h>
34 #include <widget_install/job_widget_install.h>
35 #include <widget_install/widget_install_errors.h>
36 #include <widget_install/widget_install_context.h>
37 #include <dpl/utils/file_utils.h>
38 #include <dpl/utils/mime_type_utils.h>
40 #include <dpl/utils/wrt_global_settings.h>
42 namespace { // anonymous
43 const WidgetHandle WIDGET_HANDLE_START_VALUE = 1000;
44 const DPL::String BR = DPL::FromUTF8String("<br>");
45 const std::string WIDGET_NOT_COMPATIBLE = "This widget is "
46 "not compatible with WRT.<br><br>";
47 const std::string QUESTION = "Do you want to install it anyway?";
48 } // namespace anonymous
51 namespace WidgetInstall {
52 void InstallerTaskWidgetPopupData::PopupData::addWidgetInfo(
53 const DPL::String &info)
58 TaskWidgetConfig::TaskWidgetConfig(InstallerContext& installContext) :
59 DPL::TaskDecl<TaskWidgetConfig>(this),
60 m_installContext(installContext),
61 WidgetInstallPopup(installContext)
63 AddStep(&TaskWidgetConfig::StepProcessConfigurationFile);
64 AddStep(&TaskWidgetConfig::ReadLocaleFolders);
65 AddStep(&TaskWidgetConfig::ProcessLocalizedStartFiles);
66 AddStep(&TaskWidgetConfig::ProcessLocalizedIcons);
67 AddStep(&TaskWidgetConfig::StepVerifyFeatures);
69 if (!GlobalSettings::TestModeEnabled() && !m_installContext.m_quiet) {
70 AddStep(&TaskWidgetConfig::StepCancelWidgetInstallationAfterVerifyFeatures);
71 AddStep(&TaskWidgetConfig::StepShowWidgetInfo);
72 AddStep(&TaskWidgetConfig::StepCancelWidgetInstallation);
73 AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
74 AddStep(&TaskWidgetConfig::StepCancelWidgetInstallationAfterMinVersion);
75 AddStep(&TaskWidgetConfig::StepDeletePopupWin);
79 void TaskWidgetConfig::StepProcessConfigurationFile()
84 LogInfo("path: " << m_installContext.tempWidgetRoot);
85 if (m_installContext.browserRequest) {
86 path = m_installContext.widgetSource;
88 path = m_installContext.tempWidgetRoot;
91 WidgetConfigurationManagerSingleton::Instance().processFile(
93 m_installContext.widgetConfig);
95 Catch(WidgetConfigurationManager::Exception::ProcessFailed)
97 LogError("Parsing failed.");
98 ReThrow(Exceptions::WidgetConfigFileInvalid);
102 // To get widget type for distribute WAC, TIZEN WebApp
103 setApplicationType();
105 Catch(WidgetConfigurationManager::Exception::ProcessFailed)
107 LogError("Config.xml has more than one namespace");
108 ReThrow(Exceptions::WidgetConfigFileInvalid);
111 m_installContext.job->UpdateProgress(
112 InstallerContext::INSTALL_WIDGET_CONFIG1,
113 "Parse elements of configuration file and save them");
116 void TaskWidgetConfig::ReadLocaleFolders()
118 LogDebug("Reading locale");
119 //Adding default locale
120 m_localeFolders.insert(L"");
122 std::string localePath = m_installContext.tempWidgetRoot + "/locales";
123 DIR* localeDir = opendir(localePath.c_str());
125 LogDebug("No /locales directory in the widget package.");
129 struct dirent* dirent;
130 struct stat statStruct;
133 if ((dirent = readdir(localeDir))) {
134 DPL::String dirName = DPL::FromUTF8String(dirent->d_name);
135 std::string absoluteDirName = localePath + "/";
136 absoluteDirName += dirent->d_name;
138 if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
139 LogError("stat() failed with " << DPL::GetErrnoString());
143 if (S_ISDIR(statStruct.st_mode)) {
144 //Yes, we ignore current, parent & hidden directories
145 if (dirName[0] != L'.') {
146 LogDebug("Adding locale directory \"" << dirName << "\"");
147 m_localeFolders.insert(dirName);
155 LogError("readdir() failed with " << DPL::GetErrnoString());
158 if (closedir(localeDir)) {
159 LogError("closedir() failed with " << DPL::GetErrnoString());
163 void TaskWidgetConfig::ProcessLocalizedStartFiles()
165 typedef DPL::String S;
167 m_installContext.widgetConfig.configInfo.startFile,
168 m_installContext.widgetConfig.configInfo.
169 startFileContentType,
170 m_installContext.widgetConfig.configInfo.startFileEncoding,
172 ProcessStartFile(S(L"index.htm"), S(L"text/html"));
173 ProcessStartFile(S(L"index.html"), S(L"text/html"));
174 ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
175 ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
176 ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
177 // TODO: (l.wrzosek) we need better check if in current locales widget is valid.
178 FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
179 if (it->propertiesForLocales.size() > 0) {
183 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
184 L"The Widget has no valid start file");
187 void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
188 const DPL::OptionalString& type,
189 const DPL::OptionalString& encoding,
190 bool typeForcedInConfig)
192 using namespace WrtDB;
195 WidgetRegisterInfo::LocalizedStartFile startFileData;
196 startFileData.path = *path;
198 FOREACH(i, m_localeFolders) {
199 DPL::String pathPrefix = *i;
200 if (!pathPrefix.empty()) {
201 pathPrefix = L"locales/" + pathPrefix + L"/";
204 DPL::String relativePath = pathPrefix + *path;
205 DPL::String absolutePath = DPL::FromUTF8String(
206 m_installContext.tempWidgetRoot) + L"/" + relativePath;
208 // get property data from packaged app
209 if (FileUtils::FileExists(absolutePath)) {
210 WidgetRegisterInfo::StartFileProperties startFileProperties;
212 startFileProperties.type = *type;
214 startFileProperties.type =
215 MimeTypeUtils::identifyFileMimeType(absolutePath);
218 //proceed only if MIME type is supported
219 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
220 startFileProperties.type))
223 startFileProperties.encoding = *encoding;
225 MimeTypeUtils::MimeAttributes attributes =
226 MimeTypeUtils::getMimeAttributes(
227 startFileProperties.type);
228 if (attributes.count(L"charset") > 0) {
229 startFileProperties.encoding =
230 attributes[L"charset"];
232 startFileProperties.encoding = L"UTF-8";
236 startFileData.propertiesForLocales[*i] =
240 //(there seems to be no similar requirement in .6,
241 //so let's throw only when mime type is
242 // provided explcitly in config.xml)
243 if (typeForcedInConfig) {
244 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
245 "Unsupported MIME type for start file.");
249 // set property data for hosted start url
250 // Hosted start url only support TIZEN WebApp
251 if (m_installContext.widgetConfig.type ==
252 APP_TYPE_TIZENWEBAPP)
254 const char *startPath =
255 DPL::ToUTF8String(startFileData.path).c_str();
256 if (strstr(startPath, "http") == startPath) {
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::ProcessLocalizedIcons()
279 using namespace WrtDB;
280 ProcessIcon(ConfigParserData::Icon(L"icon.svg"));
281 ProcessIcon(ConfigParserData::Icon(L"icon.ico"));
282 ProcessIcon(ConfigParserData::Icon(L"icon.png"));
283 ProcessIcon(ConfigParserData::Icon(L"icon.gif"));
284 ProcessIcon(ConfigParserData::Icon(L"icon.jpg"));
286 FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
292 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
295 bool isAnyIconValid = false;
296 //In case a default filename is passed as custom filename in config.xml, we
297 //need to keep a set of already processed filenames to avoid icon duplication
300 using namespace WrtDB;
302 if (m_processedIconSet.count(icon.src) > 0) {
305 m_processedIconSet.insert(icon.src);
307 LocaleSet localesAvailableForIcon;
309 FOREACH(i, m_localeFolders)
311 DPL::String pathPrefix = *i;
312 if (!pathPrefix.empty()) {
313 pathPrefix = L"locales/" + pathPrefix + L"/";
316 DPL::String relativePath = pathPrefix + icon.src;
317 DPL::String absolutePath;
319 if (m_installContext.browserRequest) {
320 // in case of browser installation
321 size_t pos = m_installContext.widgetSource.rfind("/");
322 absolutePath = DPL::FromUTF8String(
323 m_installContext.widgetSource.substr(0, pos));
325 absolutePath = DPL::FromUTF8String(
326 m_installContext.tempWidgetRoot);
328 absolutePath += L"/" + relativePath;
330 if (FileUtils::FileExists(absolutePath)) {
331 DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
333 if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
334 isAnyIconValid = true;
335 localesAvailableForIcon.insert(*i);
336 LogInfo("Icon absolutePath :" << absolutePath <<
337 ", assigned locale :" << *i << ", type: " << type);
344 WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
345 localesAvailableForIcon);
346 m_installContext.widgetConfig.localizationData.icons.push_back(
351 void TaskWidgetConfig::StepCancelWidgetInstallationAfterVerifyFeatures()
353 LogDebug("StepCancelWidgetInstallationAfterVerifyFeatures");
354 if (InfoPopupButton::WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
355 m_installCancel = WRT_POPUP_BUTTON;
357 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Widget not allowed");
361 void TaskWidgetConfig::StepCancelWidgetInstallation()
363 if (InfoPopupButton::WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
364 m_installCancel = WRT_POPUP_BUTTON;
366 ThrowMsg(Exceptions::NotAllowed, "Widget not allowed");
370 void TaskWidgetConfig::StepCancelWidgetInstallationAfterMinVersion()
372 if (InfoPopupButton::WRT_POPUP_BUTTON_CANCEL == m_installCancel) {
373 m_installCancel = WRT_POPUP_BUTTON;
375 ThrowMsg(Exceptions::NotAllowed, "WRT version incompatible.");
379 void TaskWidgetConfig::createInstallPopup(PopupType type, const std::string &label)
381 m_installContext.job->Pause();
385 bool ret = createPopup();
388 loadPopup( type, label);
393 void TaskWidgetConfig::StepDeletePopupWin()
398 void TaskWidgetConfig::StepShowWidgetInfo()
400 if (!m_popupData.widgetInfo.empty()) {
401 std::string label = DPL::ToUTF8String(m_popupData.widgetInfo);
402 createInstallPopup(PopupType::WIDGET_FEATURE_INFO, label);
403 m_installContext.job->UpdateProgress(
404 InstallerContext::INSTALL_WIDGET_CONFIG2,
405 "Show Widget Info Finished");
409 void TaskWidgetConfig::StepCheckMinVersionInfo()
411 if (!isMinVersionCompatible(
412 m_installContext.widgetConfig.type.appType,
413 m_installContext.widgetConfig.minVersion)) {
414 std::string label = WIDGET_NOT_COMPATIBLE + QUESTION;
415 createInstallPopup(PopupType::WIDGET_MIN_VERSION, label);
418 m_installContext.job->UpdateProgress(
419 InstallerContext::INSTALL_WIDGET_CONFIG2,
420 "Check MinVersion Finished");
423 void TaskWidgetConfig::StepVerifyFeatures()
425 using namespace WrtDB;
426 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
427 ConfigParserData::FeaturesList list = data.featuresList;
428 ConfigParserData::FeaturesList newList;
430 //in case of tests, this variable is unused
431 std::string featureInfo;
434 // check feature vender for permission
435 // WAC, TIZEN WebApp cannot use other feature
437 if (!isFeatureAllowed(m_installContext.widgetConfig.type.appType,
440 LogInfo("This application type not allowed to use this feature");
442 Exceptions::WidgetConfigFileInvalid,
444 m_installContext.widgetConfig.type.getApptypeToString() <<
445 "] cannot be allowed to use [" <<
446 DPL::ToUTF8String(it->name) + "] feature");
448 if (!WrtDB::FeatureDAOReadOnly::isFeatureInstalled(
449 DPL::ToUTF8String(it->name))) {
450 LogWarning("Feature not found. Checking if required :[" <<
451 DPL::ToUTF8String(it->name) << "]");
455 * WL-3210 The WRT MUST inform the user if a widget cannot be
456 * installed because one or more required features are not
459 std::ostringstream os;
460 os << "Widget cannot be installed, required feature is missing:["
461 << DPL::ToUTF8String(it->name) << "]";
462 if (!GlobalSettings::TestModeEnabled() && !isTizenWebApp()) {
463 std::string label = os.str();
464 createInstallPopup(PopupType::WIDGET_WRONG_FEATURE_INFO, label);
466 ThrowMsg(Exceptions::WidgetConfigFileInvalid, os.str());
470 featureInfo += DPL::ToUTF8String(it->name);
471 featureInfo += DPL::ToUTF8String(BR);
474 data.featuresList = newList;
475 if (!featureInfo.empty()) {
476 m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
479 m_installContext.job->UpdateProgress(
480 InstallerContext::INSTALL_WIDGET_CONFIG2,
481 "Widget Config step2 Finished");
484 void TaskWidgetConfig::setApplicationType()
486 using namespace WrtDB;
487 WidgetRegisterInfo* widgetInfo = &(m_installContext.widgetConfig);
488 ConfigParserData* configInfo = &(widgetInfo->configInfo);
490 FOREACH(iterator, configInfo->nameSpaces) {
491 LogInfo("namespace = [" << *iterator << "]");
492 AppType currentAppType = APP_TYPE_UNKNOWN;
494 if (*iterator == ConfigurationNamespace::W3CWidgetNamespaceName) {
498 ConfigurationNamespace::WacWidgetNamespaceNameForLinkElement ||
500 ConfigurationNamespace::WacWidgetNamespaceName)
502 currentAppType = APP_TYPE_WAC20;
503 } else if (*iterator == ConfigurationNamespace::TizenWebAppNamespaceName) {
504 currentAppType = APP_TYPE_TIZENWEBAPP;
507 if (widgetInfo->type == APP_TYPE_UNKNOWN) {
508 widgetInfo->type = currentAppType;
509 } else if (widgetInfo->type == currentAppType) {
512 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
513 "Config.xml has more than one namespace");
517 // If there is no define, type set to WAC 2.0
518 if (widgetInfo->type == APP_TYPE_UNKNOWN) {
519 widgetInfo->type = APP_TYPE_WAC20;
522 LogInfo("type = [" << widgetInfo->type.getApptypeToString() << "]");
525 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
526 DPL::String featureName)
528 using namespace WrtDB;
529 LogInfo("AppType = [" <<
530 WidgetType(appType).getApptypeToString() << "]");
531 LogInfo("FetureName = [" << featureName << "]");
533 AppType featureType = APP_TYPE_UNKNOWN;
534 const char* feature = DPL::ToUTF8String(featureName).c_str();
535 // check prefix of feature name
536 if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
537 // Tizen WebApp feature
538 featureType = APP_TYPE_TIZENWEBAPP;
539 } else if (strstr(feature, PluginsPrefix::WACPluginsPrefix) == feature) {
541 featureType = APP_TYPE_WAC20;
542 } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
543 // W3C standard feature
544 // Both WAC and TIZEN WebApp are possible to use W3C plugins
548 // unknown feature will be checked next step
552 if (appType == featureType) {
558 bool TaskWidgetConfig::parseVersionString(const std::string &version,
559 long &majorVersion, long &minorVersion, long µVersion) const
561 std::istringstream inputString(version);
562 inputString >> majorVersion;
563 if (inputString.bad() || inputString.fail()) {
564 LogWarning("Invalid minVersion format.");
567 inputString.get(); // skip period
568 inputString >> minorVersion;
569 if (inputString.bad() || inputString.fail()) {
570 LogWarning("Invalid minVersion format");
573 inputString.get(); // skip period
574 if (inputString.bad() || inputString.fail()) {
575 inputString >> microVersion;
581 bool TaskWidgetConfig::isMinVersionCompatible(WrtDB::AppType appType,
582 const DPL::OptionalString &widgetVersion) const
584 if (widgetVersion.IsNull() || (*widgetVersion).empty())
586 LogWarning("minVersion attribute is empty. WRT assumes platform "
587 "supports this widget.");
591 //Parse widget version
592 long majorWidget = 0, minorWidget = 0, microWidget = 0;
593 if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
594 minorWidget, microWidget)) {
595 LogWarning("Invalid format of widget version string.");
599 //Parse supported version
600 long majorSupported = 0, minorSupported = 0, microSupported = 0;
602 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
603 version = WrtDB::GlobalConfig::GetTizenVersion();
604 } else if (appType == WrtDB::AppType::APP_TYPE_WAC20) {
605 version = WrtDB::GlobalConfig::GetWACVersion();
607 LogWarning("Invaild AppType");
611 if (!parseVersionString(version,
612 majorSupported, minorSupported, microSupported)) {
613 LogWarning("Invalid format of WAC version string.");
617 if (majorWidget > majorSupported ||
618 minorWidget > minorSupported ||
619 microWidget > microSupported) {
620 LogInfo("Platform doesn't support this widget.");
626 bool TaskWidgetConfig::isTizenWebApp() const
629 if (m_installContext.widgetConfig.type.appType
630 == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
636 } //namespace WidgetInstall