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_process_config.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Implementation file for installer task widget config
27 #include <dpl/errno_string.h>
28 #include <dpl/foreach.h>
29 #include <dpl/localization/w3c_file_localization.h>
30 #include <dpl/singleton_impl.h>
31 #include <dpl/utils/mime_type_utils.h>
32 #include <dpl/utils/wrt_global_settings.h>
33 #include <dpl/utils/wrt_utility.h>
34 #include <dpl/wrt-dao-ro/global_config.h>
35 #include <dpl/wrt-dao-ro/config_parser_data.h>
36 #include <dpl/wrt-dao-rw/feature_dao.h>
38 #include <widget_install/job_widget_install.h>
39 #include <widget_install/task_process_config.h>
40 #include <widget_install/widget_install_context.h>
41 #include <widget_install/widget_install_errors.h>
42 #include <widget_parser.h>
43 #include <web_provider_plugin_info.h>
44 #include <web_provider_livebox_info.h>
47 #include <installer_log.h>
49 namespace { // anonymous
50 const DPL::String BR = DPL::FromUTF8String("<br>");
51 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
55 namespace WidgetInstall {
57 TaskProcessConfig::TaskProcessConfig(InstallerContext& installContext) :
58 DPL::TaskDecl<TaskProcessConfig>(this),
59 m_installContext(installContext)
61 AddStep(&TaskProcessConfig::StartStep);
62 AddStep(&TaskProcessConfig::ReadLocaleFolders);
63 AddStep(&TaskProcessConfig::StepFillWidgetConfig);
64 AddStep(&TaskProcessConfig::ProcessLocalizedStartFiles);
65 AddStep(&TaskProcessConfig::ProcessBackgroundPageFile);
66 AddStep(&TaskProcessConfig::ProcessLocalizedIcons);
67 AddStep(&TaskProcessConfig::ProcessWidgetInstalledPath);
68 AddStep(&TaskProcessConfig::ProcessAppControlInfo);
69 AddStep(&TaskProcessConfig::ProcessSecurityModel);
70 AddStep(&TaskProcessConfig::StepVerifyFeatures);
71 AddStep(&TaskProcessConfig::StepVerifyLivebox);
72 AddStep(&TaskProcessConfig::StepCheckMinVersionInfo);
73 AddStep(&TaskProcessConfig::EndStep);
76 void TaskProcessConfig::StepFillWidgetConfig()
78 if (!fillWidgetConfig(m_installContext.widgetConfig,
79 m_installContext.widgetConfig.configInfo))
81 _E("Widget configuration is illformed");
82 ThrowMsg(Exception::ConfigParseFailed, "Widget configuration is illformed");
86 void TaskProcessConfig::ReadLocaleFolders()
89 //Adding default locale
90 m_localeFolders.insert(L"");
92 std::string localePath =
93 m_installContext.locations->getSourceDir() + "/locales";
95 DIR* localeDir = opendir(localePath.c_str());
97 _D("No /locales directory in the widget package.");
101 struct stat statStruct;
102 struct dirent dirent;
103 struct dirent *result;
106 for (return_code = readdir_r(localeDir, &dirent, &result);
107 result != NULL && return_code == 0;
108 return_code = readdir_r(localeDir, &dirent, &result))
110 DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
111 std::string absoluteDirName = localePath + "/";
112 absoluteDirName += dirent.d_name;
114 if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
115 _E("stat() failed with %s", DPL::GetErrnoString().c_str());
119 if (S_ISDIR(statStruct.st_mode)) {
120 //Yes, we ignore current, parent & hidden directories
121 if (dirName[0] != L'.') {
122 _D("Adding locale directory \"%ls\"", dirName.c_str());
123 m_localeFolders.insert(dirName);
128 if (return_code != 0 || errno != 0) {
129 _E("readdir_r() failed with %s", DPL::GetErrnoString().c_str());
132 if (-1 == closedir(localeDir)) {
133 _E("Failed to close dir: %s with error: %s", localePath.c_str(), DPL::GetErrnoString().c_str());
136 m_installContext.job->UpdateProgress(InstallerContext::INSTALL_WIDGET_CONFIG1, "Read locale folders");
139 void TaskProcessConfig::ProcessLocalizedStartFiles()
141 typedef DPL::String S;
143 m_installContext.widgetConfig.configInfo.startFile,
144 m_installContext.widgetConfig.configInfo.
145 startFileContentType,
146 m_installContext.widgetConfig.configInfo.startFileEncoding,
148 ProcessStartFile(S(L"index.htm"), S(L"text/html"));
149 ProcessStartFile(S(L"index.html"), S(L"text/html"));
150 ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
151 ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
152 ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
153 // TODO: we need better check if in current locales widget is valid
154 FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
155 if (it->propertiesForLocales.size() > 0) {
159 ThrowMsg(Exceptions::InvalidStartFile,
160 "The Widget has no valid start file");
163 void TaskProcessConfig::ProcessStartFile(const DPL::OptionalString& path,
164 const DPL::OptionalString& type,
165 const DPL::OptionalString& encoding,
166 bool typeForcedInConfig)
168 using namespace WrtDB;
171 WidgetRegisterInfo::LocalizedStartFile startFileData;
172 startFileData.path = *path;
174 FOREACH(i, m_localeFolders) {
175 DPL::String pathPrefix = *i;
176 if (!pathPrefix.empty()) {
177 pathPrefix = L"locales/" + pathPrefix + L"/";
180 DPL::String relativePath = pathPrefix + *path;
181 DPL::String absolutePath = DPL::FromUTF8String(
182 m_installContext.locations->getSourceDir()) + L"/" +
184 _D("absolutePath : %ls", absolutePath.c_str());
186 // get property data from packaged app
187 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
188 WidgetRegisterInfo::StartFileProperties startFileProperties;
190 startFileProperties.type = *type;
192 startFileProperties.type =
193 MimeTypeUtils::identifyFileMimeType(absolutePath);
196 //proceed only if MIME type is supported
197 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
198 startFileProperties.type))
201 startFileProperties.encoding = *encoding;
203 MimeTypeUtils::MimeAttributes attributes =
204 MimeTypeUtils::getMimeAttributes(
205 startFileProperties.type);
206 if (attributes.count(L"charset") > 0) {
207 startFileProperties.encoding =
208 attributes[L"charset"];
210 startFileProperties.encoding = L"UTF-8";
214 startFileData.propertiesForLocales[*i] =
218 //(there seems to be no similar requirement in .6,
219 //so let's throw only when mime type is
220 // provided explcitly in config.xml)
221 if (typeForcedInConfig) {
222 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
223 "Unsupported MIME type for start file.");
227 // set property data for hosted start url
228 // Hosted start url only support TIZEN WebApp
229 if (m_installContext.widgetConfig.webAppType ==
230 APP_TYPE_TIZENWEBAPP)
232 std::string startPath = DPL::ToUTF8String(
235 if (strstr(startPath.c_str(),
236 "http") == startPath.c_str())
238 WidgetRegisterInfo::StartFileProperties
241 startFileProperties.type = *type;
244 startFileProperties.encoding = *encoding;
246 startFileData.propertiesForLocales[*i] =
253 m_installContext.widgetConfig.localizationData.startFiles.push_back(
258 void TaskProcessConfig::ProcessBackgroundPageFile()
260 if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
261 // check whether file exists
262 DPL::String backgroundPagePath = DPL::FromUTF8String(
263 m_installContext.locations->getSourceDir()) + L"/" +
264 *m_installContext.widgetConfig.configInfo.backgroundPage;
265 //if no then cancel installation
266 if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
267 ThrowMsg(Exceptions::WidgetConfigFileInvalid,
268 L"Given background page file not found in archive");
273 void TaskProcessConfig::ProcessLocalizedIcons()
275 using namespace WrtDB;
276 FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
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"));
287 void TaskProcessConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
290 bool isAnyIconValid = false;
291 //In case a default filename is passed as custom filename in config.xml, we
292 //need to keep a set of already processed filenames to avoid icon
296 using namespace WrtDB;
298 if (m_processedIconSet.count(icon.src) > 0) {
301 m_processedIconSet.insert(icon.src);
303 LocaleSet localesAvailableForIcon;
305 FOREACH(i, m_localeFolders)
307 DPL::String pathPrefix = *i;
308 if (!pathPrefix.empty()) {
309 pathPrefix = L"locales/" + pathPrefix + L"/";
312 DPL::String relativePath = pathPrefix + icon.src;
313 DPL::String absolutePath = DPL::FromUTF8String(
314 m_installContext.locations->getSourceDir()) + L"/" +
317 if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
318 DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
320 if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
321 isAnyIconValid = true;
322 localesAvailableForIcon.insert(*i);
323 _D("Icon absolutePath: %ls, assigned locale: %ls, type: %ls",
324 absolutePath.c_str(), (*i).c_str(), type.c_str());
329 if (isAnyIconValid) {
330 WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
331 localesAvailableForIcon);
332 m_installContext.widgetConfig.localizationData.icons.push_back(
337 void TaskProcessConfig::ProcessWidgetInstalledPath()
339 _D("ProcessWidgetInstalledPath");
340 m_installContext.widgetConfig.widgetInstalledPath =
342 m_installContext.locations->getPackageInstallationDir());
345 void TaskProcessConfig::ProcessAppControlInfo()
347 _D("ProcessAppControlInfo");
348 using namespace WrtDB;
350 // In case of dispostion is inline, set the seperate execute
352 // 0 index is reserved by default execute
353 FOREACH(it, m_installContext.widgetConfig.configInfo.appControlList) {
354 if (it->m_disposition ==
355 ConfigParserData::AppControlInfo::Disposition::INLINE)
357 it->m_index = index++;
364 void TaskProcessConfig::ProcessSecurityModel()
366 // 0104. If the "required_version" specified in the Web Application's
367 // configuration is 2.2 or higher and if the Web Application's
368 // configuration is "CSP-compatible configuration", then the WRT MUST be
369 // set to "CSP-based security mode". Otherwise, the WRT MUST be set to
370 // "WARP-based security mode".
371 // 0105. A Web Application configuration is "CSP-compatible configuration"
372 // if the configuration includes one or more of
373 // <tizen:content-security-policy> /
374 // <tizen:content-security-policy-report-only> /
375 // <tizen:allow-navigation> elements.
377 bool isSecurityModelV1 = false;
378 bool isSecurityModelV2 = false;
379 WrtDB::ConfigParserData &data = m_installContext.widgetConfig.configInfo;
381 if (!data.cspPolicy.IsNull() ||
382 !data.cspPolicyReportOnly.IsNull() ||
383 !data.allowNavigationInfoList.empty())
385 data.accessInfoSet.clear();
389 if (!data.accessInfoSet.empty()) {
390 isSecurityModelV1 = true;
393 // CSP & allow-navigation is V2
394 if (!data.cspPolicy.IsNull() ||
395 !data.cspPolicyReportOnly.IsNull() ||
396 !data.allowNavigationInfoList.empty())
398 isSecurityModelV2 = true;
401 if (isSecurityModelV1 && isSecurityModelV2) {
402 _E("Security model is conflict");
403 ThrowMsg(Exceptions::NotAllowed, "Security model is conflict");
404 } else if (isSecurityModelV1) {
405 data.securityModelVersion =
406 WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
407 } else if (isSecurityModelV2) {
408 data.securityModelVersion =
409 WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V2;
411 data.securityModelVersion =
412 WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
415 m_installContext.job->UpdateProgress(
416 InstallerContext::INSTALL_WIDGET_CONFIG2,
417 "Finished process security model");
420 void TaskProcessConfig::StepCheckMinVersionInfo()
422 if (!isMinVersionCompatible(
423 m_installContext.widgetConfig.webAppType.appType,
424 m_installContext.widgetConfig.minVersion))
426 _E("Platform version lower than required -> cancelling installation");
427 ThrowMsg(Exceptions::NotAllowed,
428 "Platform version does not meet requirements");
431 m_installContext.job->UpdateProgress(
432 InstallerContext::INSTALL_WIDGET_CONFIG2,
433 "Check MinVersion Finished");
436 void TaskProcessConfig::StepVerifyFeatures()
438 using namespace WrtDB;
439 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
440 ConfigParserData::FeaturesList list = data.featuresList;
441 ConfigParserData::FeaturesList newList;
443 //in case of tests, this variable is unused
444 std::string featureInfo;
447 // check feature vender for permission
448 // WAC, TIZEN WebApp cannot use other feature
450 if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
453 _D("This application type not allowed to use this feature");
455 Exceptions::WidgetConfigFileInvalid,
457 m_installContext.widgetConfig.webAppType.getApptypeToString()
459 "] cannot be allowed to use [" <<
460 DPL::ToUTF8String(it->name) + "] feature");
463 featureInfo += DPL::ToUTF8String(it->name);
464 featureInfo += DPL::ToUTF8String(BR);
467 if (!data.accessInfoSet.empty()) {
468 featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
469 featureInfo += DPL::ToUTF8String(BR);
471 data.featuresList = newList;
473 m_installContext.job->UpdateProgress(
474 InstallerContext::INSTALL_WIDGET_CONFIG2,
475 "Widget Config step2 Finished");
478 void TaskProcessConfig::StepVerifyLivebox()
480 using namespace WrtDB;
481 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
482 ConfigParserData::LiveboxList liveBoxList = data.m_livebox;
484 if (liveBoxList.size() <= 0) {
488 FOREACH (it, liveBoxList) {
491 if ((**it).m_liveboxId.find(m_installContext.widgetConfig.tzAppid) != 0) {
492 _E("Invalid app-widget id (doesn't begin with application id)");
493 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Invalid app-widget id (doesn't begin with application id)");
496 if ((**it).m_type.empty()) {
497 boxType = web_provider_livebox_get_default_type();
499 boxType = DPL::ToUTF8String((**it).m_type);
502 _D("livebox type: %s", boxType.c_str());
504 ConfigParserData::LiveboxInfo::BoxSizeList boxSizeList =
505 (**it).m_boxInfo.m_boxSize;
506 char** boxSize = static_cast<char**>(
507 malloc(sizeof(char*)* boxSizeList.size()));
510 FOREACH (m, boxSizeList) {
511 boxSize[boxSizeCnt++] = strdup(DPL::ToUTF8String((*m).m_size).c_str());
514 bool chkSize = web_provider_plugin_check_supported_size(
515 boxType.c_str(), boxSize, boxSizeCnt);
517 for(int i = 0; i < boxSizeCnt; i++) {
523 _E("Invalid boxSize");
524 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Invalid boxSize");
529 bool TaskProcessConfig::isFeatureAllowed(WrtDB::AppType appType,
530 DPL::String featureName)
532 using namespace WrtDB;
533 _D("AppType = [%s]", WidgetType(appType).getApptypeToString().c_str());
534 _D("FetureName = [%ls]", featureName.c_str());
536 AppType featureType = APP_TYPE_UNKNOWN;
537 std::string featureStr = DPL::ToUTF8String(featureName);
538 const char* feature = featureStr.c_str();
540 // check prefix of feature name
541 if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
542 // Tizen WebApp feature
543 featureType = APP_TYPE_TIZENWEBAPP;
544 } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
545 // W3C standard feature
546 // Both WAC and TIZEN WebApp are possible to use W3C plugins
550 // unknown feature will be checked next step
554 if (appType == featureType) {
560 bool TaskProcessConfig::parseVersionString(const std::string &version,
563 long µVersion) const
565 std::istringstream inputString(version);
566 inputString >> majorVersion;
567 if (inputString.bad() || inputString.fail()) {
568 _W("Invalid minVersion format.");
571 inputString.get(); // skip period
572 inputString >> minorVersion;
573 if (inputString.bad() || inputString.fail()) {
574 _W("Invalid minVersion format");
577 inputString.get(); // skip period
578 if (inputString.bad() || inputString.fail()) {
579 inputString >> microVersion;
585 bool TaskProcessConfig::isMinVersionCompatible(
586 WrtDB::AppType appType,
587 const DPL::OptionalString &
590 if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
591 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
594 _W("minVersion attribute is empty. WRT assumes platform "
595 "supports this widget.");
600 //Parse widget version
601 long majorWidget = 0, minorWidget = 0, microWidget = 0;
602 if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
603 minorWidget, microWidget))
605 _W("Invalid format of widget version string.");
609 //Parse supported version
610 long majorSupported = 0, minorSupported = 0, microSupported = 0;
612 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
613 version = WrtDB::GlobalConfig::GetTizenVersion();
615 _W("Invaild AppType");
619 if (!parseVersionString(version,
620 majorSupported, minorSupported, microSupported))
622 _W("Invalid format of platform version string.");
626 if (majorWidget > majorSupported ||
627 (majorWidget == majorSupported && minorWidget > minorSupported) ||
628 (majorWidget == majorSupported && minorWidget == minorSupported
629 && microWidget > microSupported))
631 _D("Platform doesn't support this widget.");
637 bool TaskProcessConfig::isTizenWebApp() const
639 if (m_installContext.widgetConfig.webAppType.appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
646 bool TaskProcessConfig::fillWidgetConfig(
647 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
648 WrtDB::ConfigParserData& configInfo)
650 pWidgetConfigInfo.guid = configInfo.widget_id;
652 if (!!configInfo.version) {
653 if (!pWidgetConfigInfo.version) {
654 pWidgetConfigInfo.version = configInfo.version;
656 if (pWidgetConfigInfo.version != configInfo.version) {
657 _E("Invalid archive");
662 if (!!configInfo.minVersionRequired) {
663 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
664 } else if (!!configInfo.tizenMinVersionRequired) {
665 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
670 void TaskProcessConfig::StartStep()
672 _D("--------- <TaskProcessConfig> : START ----------");
675 void TaskProcessConfig::EndStep()
677 _D("--------- <TaskProcessConfig> : END ----------");
680 } //namespace WidgetInstall