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>
44 #include <web_provider_plugin_info.h>
45 #include <web_provider_livebox_info.h>
49 #include <installer_log.h>
51 namespace { // anonymous
52 const DPL::String BR = DPL::FromUTF8String("<br>");
53 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
57 namespace WidgetInstall {
59 TaskProcessConfig::TaskProcessConfig(InstallerContext& installContext) :
60 DPL::TaskDecl<TaskProcessConfig>(this),
61 m_installContext(installContext)
63 AddStep(&TaskProcessConfig::StartStep);
64 AddStep(&TaskProcessConfig::ReadLocaleFolders);
65 AddStep(&TaskProcessConfig::StepFillWidgetConfig);
66 AddStep(&TaskProcessConfig::ProcessLocalizedStartFiles);
67 AddStep(&TaskProcessConfig::ProcessBackgroundPageFile);
68 AddStep(&TaskProcessConfig::ProcessLocalizedIcons);
69 AddStep(&TaskProcessConfig::ProcessWidgetInstalledPath);
70 AddStep(&TaskProcessConfig::ProcessAppControlInfo);
71 AddStep(&TaskProcessConfig::ProcessSecurityModel);
72 AddStep(&TaskProcessConfig::StepVerifyFeatures);
73 AddStep(&TaskProcessConfig::StepVerifyLivebox);
74 AddStep(&TaskProcessConfig::StepCheckMinVersionInfo);
75 AddStep(&TaskProcessConfig::EndStep);
78 void TaskProcessConfig::StepFillWidgetConfig()
80 if (!fillWidgetConfig(m_installContext.widgetConfig,
81 m_installContext.widgetConfig.configInfo))
83 _E("Widget configuration is illformed");
84 ThrowMsg(Exception::ConfigParseFailed, "Widget configuration is illformed");
88 void TaskProcessConfig::ReadLocaleFolders()
91 //Adding default locale
92 m_localeFolders.insert(L"");
94 std::string localePath =
95 m_installContext.locations->getConfigurationDir() + "/locales";
96 DIR* localeDir = opendir(localePath.c_str());
98 _D("No /locales directory in the widget package.");
102 struct stat statStruct;
103 struct dirent dirent;
104 struct dirent *result;
107 for (return_code = readdir_r(localeDir, &dirent, &result);
108 result != NULL && return_code == 0;
109 return_code = readdir_r(localeDir, &dirent, &result))
111 DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
112 std::string absoluteDirName = localePath + "/";
113 absoluteDirName += dirent.d_name;
115 if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
116 _E("stat() failed with %s", DPL::GetErrnoString().c_str());
120 if (S_ISDIR(statStruct.st_mode)) {
121 //Yes, we ignore current, parent & hidden directories
122 if (dirName[0] != L'.') {
123 _D("Adding locale directory \"%ls\"", dirName.c_str());
124 m_localeFolders.insert(dirName);
129 if (return_code != 0 || errno != 0) {
130 _E("readdir_r() failed with %s", DPL::GetErrnoString().c_str());
133 if (-1 == closedir(localeDir)) {
134 _E("Failed to close dir: %s with error: %s", localePath.c_str(), DPL::GetErrnoString().c_str());
137 m_installContext.job->UpdateProgress(InstallerContext::INSTALL_WIDGET_CONFIG1, "Read locale folders");
140 void TaskProcessConfig::ProcessLocalizedStartFiles()
142 typedef DPL::String S;
144 m_installContext.widgetConfig.configInfo.startFile,
145 m_installContext.widgetConfig.configInfo.
146 startFileContentType,
147 m_installContext.widgetConfig.configInfo.startFileEncoding,
149 ProcessStartFile(S(L"index.htm"), S(L"text/html"));
150 ProcessStartFile(S(L"index.html"), S(L"text/html"));
151 ProcessStartFile(S(L"index.svg"), S(L"image/svg+xml"));
152 ProcessStartFile(S(L"index.xhtml"), S(L"application/xhtml+xml"));
153 ProcessStartFile(S(L"index.xht"), S(L"application/xhtml+xml"));
154 // TODO: we need better check if in current locales widget is valid
155 FOREACH(it, m_installContext.widgetConfig.localizationData.startFiles) {
156 if (it->propertiesForLocales.size() > 0) {
160 ThrowMsg(Exceptions::InvalidStartFile,
161 "The Widget has no valid start file");
164 void TaskProcessConfig::ProcessStartFile(const DPL::OptionalString& path,
165 const DPL::OptionalString& type,
166 const DPL::OptionalString& encoding,
167 bool typeForcedInConfig)
169 using namespace WrtDB;
172 WidgetRegisterInfo::LocalizedStartFile startFileData;
173 startFileData.path = *path;
175 FOREACH(i, m_localeFolders) {
176 DPL::String pathPrefix = *i;
177 if (!pathPrefix.empty()) {
178 pathPrefix = L"locales/" + pathPrefix + L"/";
181 DPL::String relativePath = pathPrefix + *path;
182 DPL::String absolutePath = DPL::FromUTF8String(
183 m_installContext.locations->getConfigurationDir()) + L"/" +
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->getConfigurationDir()) + 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->getConfigurationDir()) + 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()
481 using namespace WrtDB;
482 ConfigParserData &data = m_installContext.widgetConfig.configInfo;
483 ConfigParserData::LiveboxList liveBoxList = data.m_livebox;
485 if (liveBoxList.size() <= 0) {
489 FOREACH (it, liveBoxList) {
492 if ((**it).m_liveboxId.find(m_installContext.widgetConfig.tzAppid) != 0) {
493 _E("Invalid app-widget id (doesn't begin with application id)");
494 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Invalid app-widget id (doesn't begin with application id)");
497 if ((**it).m_type.empty()) {
498 boxType = web_provider_livebox_get_default_type();
500 boxType = DPL::ToUTF8String((**it).m_type);
503 _D("livebox type: %s", boxType.c_str());
505 ConfigParserData::LiveboxInfo::BoxSizeList boxSizeList =
506 (**it).m_boxInfo.m_boxSize;
507 char** boxSize = static_cast<char**>(
508 malloc(sizeof(char*)* boxSizeList.size()));
511 FOREACH (m, boxSizeList) {
512 boxSize[boxSizeCnt++] = strdup(DPL::ToUTF8String((*m).m_size).c_str());
515 bool chkSize = web_provider_plugin_check_supported_size(
516 boxType.c_str(), boxSize, boxSizeCnt);
518 for(int i = 0; i < boxSizeCnt; i++) {
524 _E("Invalid boxSize");
525 ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Invalid boxSize");
531 bool TaskProcessConfig::isFeatureAllowed(WrtDB::AppType appType,
532 DPL::String featureName)
534 using namespace WrtDB;
535 _D("AppType = [%s]", WidgetType(appType).getApptypeToString().c_str());
536 _D("FetureName = [%ls]", featureName.c_str());
538 AppType featureType = APP_TYPE_UNKNOWN;
539 std::string featureStr = DPL::ToUTF8String(featureName);
540 const char* feature = featureStr.c_str();
542 // check prefix of feature name
543 if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
544 // Tizen WebApp feature
545 featureType = APP_TYPE_TIZENWEBAPP;
546 } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
547 // W3C standard feature
548 // Both WAC and TIZEN WebApp are possible to use W3C plugins
552 // unknown feature will be checked next step
556 if (appType == featureType) {
562 bool TaskProcessConfig::parseVersionString(const std::string &version,
565 long µVersion) const
567 std::istringstream inputString(version);
568 inputString >> majorVersion;
569 if (inputString.bad() || inputString.fail()) {
570 _W("Invalid minVersion format.");
573 inputString.get(); // skip period
574 inputString >> minorVersion;
575 if (inputString.bad() || inputString.fail()) {
576 _W("Invalid minVersion format");
579 inputString.get(); // skip period
580 if (inputString.bad() || inputString.fail()) {
581 inputString >> microVersion;
587 bool TaskProcessConfig::isMinVersionCompatible(
588 WrtDB::AppType appType,
589 const DPL::OptionalString &
592 if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
593 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
596 _W("minVersion attribute is empty. WRT assumes platform "
597 "supports this widget.");
602 //Parse widget version
603 long majorWidget = 0, minorWidget = 0, microWidget = 0;
604 if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
605 minorWidget, microWidget))
607 _W("Invalid format of widget version string.");
611 //Parse supported version
612 long majorSupported = 0, minorSupported = 0, microSupported = 0;
614 if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
615 version = WrtDB::GlobalConfig::GetTizenVersion();
617 _W("Invaild AppType");
621 if (!parseVersionString(version,
622 majorSupported, minorSupported, microSupported))
624 _W("Invalid format of platform version string.");
628 if (majorWidget > majorSupported ||
629 (majorWidget == majorSupported && minorWidget > minorSupported) ||
630 (majorWidget == majorSupported && minorWidget == minorSupported
631 && microWidget > microSupported))
633 _D("Platform doesn't support this widget.");
639 bool TaskProcessConfig::isTizenWebApp() const
641 if (m_installContext.widgetConfig.webAppType.appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
648 bool TaskProcessConfig::fillWidgetConfig(
649 WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
650 WrtDB::ConfigParserData& configInfo)
652 pWidgetConfigInfo.guid = configInfo.widget_id;
654 if (!!configInfo.version) {
655 if (!pWidgetConfigInfo.version) {
656 pWidgetConfigInfo.version = configInfo.version;
658 if (pWidgetConfigInfo.version != configInfo.version) {
659 _E("Invalid archive");
664 if (!!configInfo.minVersionRequired) {
665 pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
666 } else if (!!configInfo.tizenMinVersionRequired) {
667 pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
672 void TaskProcessConfig::StartStep()
674 _D("--------- <TaskProcessConfig> : START ----------");
677 void TaskProcessConfig::EndStep()
679 _D("--------- <TaskProcessConfig> : END ----------");
682 } //namespace WidgetInstall