[Release] wrt-installer_0.1.92
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_widget_config.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file    task_widget_config.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task widget config
21  */
22
23 #include <map>
24 #include <sstream>
25 #include <string>
26 #include <sys/stat.h>
27 #include <dirent.h>
28
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-ro/config_parser_data.h>
38 #include <dpl/wrt-dao-rw/feature_dao.h>
39
40 #include <libiriwrapper.h>
41 #include <parser_runner.h>
42 #include <root_parser.h>
43
44 #include <widget_install/job_widget_install.h>
45 #include <widget_install/task_widget_config.h>
46 #include <widget_install/widget_install_context.h>
47 #include <widget_install/widget_install_errors.h>
48 #include <widget_parser.h>
49 #include <web_provider_plugin_info.h>
50 #include <web_provider_livebox_info.h>
51 #include <manifest.h>
52
53 namespace { // anonymous
54 const DPL::String BR = DPL::FromUTF8String("<br>");
55 const std::string WIDGET_NOT_COMPATIBLE = "This widget is "
56                                           "not compatible with WRT.<br><br>";
57 const std::string QUESTION = "Do you want to install it anyway?";
58
59 const char *const DEFAULT_LANGUAGE = "default";
60
61 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
62
63 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
64
65 const char WRT_WIDGETS_XML_SCHEMA[] = "/usr/etc/wrt-installer/widgets.xsd";
66 }
67
68 namespace Jobs {
69 namespace WidgetInstall {
70 void InstallerTaskWidgetPopupData::PopupData::addWidgetInfo(
71     const DPL::String &info)
72 {
73     widgetInfo = info;
74 }
75
76 TaskWidgetConfig::TaskWidgetConfig(InstallerContext& installContext) :
77     DPL::TaskDecl<TaskWidgetConfig>(this),
78     m_installContext(installContext)
79 {
80     AddStep(&TaskWidgetConfig::StepProcessConfigurationFile);
81     AddStep(&TaskWidgetConfig::ReadLocaleFolders);
82     AddStep(&TaskWidgetConfig::ProcessLocalizedStartFiles);
83     AddStep(&TaskWidgetConfig::ProcessBackgroundPageFile);
84     AddStep(&TaskWidgetConfig::ProcessLocalizedIcons);
85     AddStep(&TaskWidgetConfig::ProcessWidgetInstalledPath);
86     AddStep(&TaskWidgetConfig::ProcessAppControlInfo);
87     AddStep(&TaskWidgetConfig::ProcessSecurityModel);
88     AddStep(&TaskWidgetConfig::StepVerifyFeatures);
89     AddStep(&TaskWidgetConfig::StepCheckMinVersionInfo);
90 }
91
92 void TaskWidgetConfig::StepProcessConfigurationFile()
93 {
94     Try
95     {
96         std::string path = m_installContext.locations->getConfigurationDir();
97         LogInfo("path: " << path);
98
99         processFile(path, m_installContext.widgetConfig);
100     }
101     Catch(Exception::ConfigParseFailed)
102     {
103         LogError("Parsing failed.");
104         ReThrow(Exceptions::WidgetConfigFileInvalid);
105     }
106
107     m_installContext.job->UpdateProgress(
108         InstallerContext::INSTALL_WIDGET_CONFIG1,
109         "Parse elements of configuration file and save them");
110 }
111
112 void TaskWidgetConfig::ReadLocaleFolders()
113 {
114     LogDebug("Reading locale");
115     //Adding default locale
116     m_localeFolders.insert(L"");
117
118     std::string localePath =
119         m_installContext.locations->getConfigurationDir() + "/locales";
120     DIR* localeDir = opendir(localePath.c_str());
121     if (!localeDir) {
122         LogDebug("No /locales directory in the widget package.");
123         return;
124     }
125
126     struct stat statStruct;
127     struct dirent dirent;
128     struct dirent *result;
129     int return_code;
130     errno = 0;
131     for (return_code = readdir_r(localeDir, &dirent, &result);
132             result != NULL && return_code == 0;
133             return_code = readdir_r(localeDir, &dirent, &result))
134     {
135         DPL::String dirName = DPL::FromUTF8String(dirent.d_name);
136         std::string absoluteDirName = localePath + "/";
137         absoluteDirName += dirent.d_name;
138
139         if (stat(absoluteDirName.c_str(), &statStruct) != 0) {
140             LogError("stat() failed with " << DPL::GetErrnoString());
141             continue;
142         }
143
144         if (S_ISDIR(statStruct.st_mode)) {
145             //Yes, we ignore current, parent & hidden directories
146             if (dirName[0] != L'.') {
147                 LogDebug("Adding locale directory \"" << dirName << "\"");
148                 m_localeFolders.insert(dirName);
149             }
150         }
151     }
152
153     if (return_code != 0 || errno != 0) {
154         LogError("readdir_r() failed with " << DPL::GetErrnoString());
155     }
156
157     if (-1 == TEMP_FAILURE_RETRY(closedir(localeDir))) {
158         LogError("Failed to close dir: " << localePath << " with error: "
159                                          << DPL::GetErrnoString());
160     }
161 }
162
163 void TaskWidgetConfig::ProcessLocalizedStartFiles()
164 {
165     typedef DPL::String S;
166     ProcessStartFile(
167         m_installContext.widgetConfig.configInfo.startFile,
168         m_installContext.widgetConfig.configInfo.
169             startFileContentType,
170         m_installContext.widgetConfig.configInfo.startFileEncoding,
171         true);
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: 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) {
180             return;
181         }
182     }
183     ThrowMsg(Exceptions::WidgetConfigFileInvalid,
184              L"The Widget has no valid start file");
185 }
186
187 void TaskWidgetConfig::ProcessStartFile(const DPL::OptionalString& path,
188                                         const DPL::OptionalString& type,
189                                         const DPL::OptionalString& encoding,
190                                         bool typeForcedInConfig)
191 {
192     using namespace WrtDB;
193
194     if (!!path) {
195         WidgetRegisterInfo::LocalizedStartFile startFileData;
196         startFileData.path = *path;
197
198         FOREACH(i, m_localeFolders) {
199             DPL::String pathPrefix = *i;
200             if (!pathPrefix.empty()) {
201                 pathPrefix = L"locales/" + pathPrefix + L"/";
202             }
203
204             DPL::String relativePath = pathPrefix + *path;
205             DPL::String absolutePath = DPL::FromUTF8String(
206                     m_installContext.locations->getConfigurationDir()) + L"/" +
207                 relativePath;
208
209             // get property data from packaged app
210             if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
211                 WidgetRegisterInfo::StartFileProperties startFileProperties;
212                 if (!!type) {
213                     startFileProperties.type = *type;
214                 } else {
215                     startFileProperties.type =
216                         MimeTypeUtils::identifyFileMimeType(absolutePath);
217                 }
218
219                 //proceed only if MIME type is supported
220                 if (MimeTypeUtils::isMimeTypeSupportedForStartFile(
221                         startFileProperties.type))
222                 {
223                     if (!!encoding) {
224                         startFileProperties.encoding = *encoding;
225                     } else {
226                         MimeTypeUtils::MimeAttributes attributes =
227                             MimeTypeUtils::getMimeAttributes(
228                                 startFileProperties.type);
229                         if (attributes.count(L"charset") > 0) {
230                             startFileProperties.encoding =
231                                 attributes[L"charset"];
232                         } else {
233                             startFileProperties.encoding = L"UTF-8";
234                         }
235                     }
236
237                     startFileData.propertiesForLocales[*i] =
238                         startFileProperties;
239                 } else {
240                     //9.1.16.5.content.8
241                     //(there seems to be no similar requirement in .6,
242                     //so let's throw only when mime type is
243                     // provided explcitly in config.xml)
244                     if (typeForcedInConfig) {
245                         ThrowMsg(Exceptions::WidgetConfigFileInvalid,
246                                  "Unsupported MIME type for start file.");
247                     }
248                 }
249             } else {
250                 // set property data for hosted start url
251                 // Hosted start url only support TIZEN WebApp
252                 if (m_installContext.widgetConfig.webAppType ==
253                     APP_TYPE_TIZENWEBAPP)
254                 {
255                     std::string startPath = DPL::ToUTF8String(
256                             startFileData.path);
257
258                     if (strstr(startPath.c_str(),
259                                "http") == startPath.c_str())
260                     {
261                         WidgetRegisterInfo::StartFileProperties
262                             startFileProperties;
263                         if (!!type) {
264                             startFileProperties.type = *type;
265                         }
266                         if (!!encoding) {
267                             startFileProperties.encoding = *encoding;
268                         }
269                         startFileData.propertiesForLocales[*i] =
270                             startFileProperties;
271                     }
272                 }
273             }
274         }
275
276         m_installContext.widgetConfig.localizationData.startFiles.push_back(
277             startFileData);
278     }
279 }
280
281 void TaskWidgetConfig::ProcessBackgroundPageFile()
282 {
283     if (!!m_installContext.widgetConfig.configInfo.backgroundPage) {
284         // check whether file exists
285         DPL::String backgroundPagePath = DPL::FromUTF8String(
286                 m_installContext.locations->getConfigurationDir()) + L"/" +
287             *m_installContext.widgetConfig.configInfo.backgroundPage;
288         //if no then cancel installation
289         if (!WrtUtilFileExists(DPL::ToUTF8String(backgroundPagePath))) {
290             ThrowMsg(Exceptions::WidgetConfigFileInvalid,
291                      L"Given background page file not found in archive");
292         }
293     }
294 }
295
296 void TaskWidgetConfig::ProcessLocalizedIcons()
297 {
298     using namespace WrtDB;
299     ProcessIcon(ConfigParserData::Icon(L"icon.svg"));
300     ProcessIcon(ConfigParserData::Icon(L"icon.ico"));
301     ProcessIcon(ConfigParserData::Icon(L"icon.png"));
302     ProcessIcon(ConfigParserData::Icon(L"icon.gif"));
303     ProcessIcon(ConfigParserData::Icon(L"icon.jpg"));
304
305     FOREACH(i, m_installContext.widgetConfig.configInfo.iconsList)
306     {
307         ProcessIcon(*i);
308     }
309 }
310
311 void TaskWidgetConfig::ProcessIcon(const WrtDB::ConfigParserData::Icon& icon)
312 {
313     LogInfo("enter");
314     bool isAnyIconValid = false;
315     //In case a default filename is passed as custom filename in config.xml, we
316     //need to keep a set of already processed filenames to avoid icon
317     // duplication
318     //in database.
319
320     using namespace WrtDB;
321
322     if (m_processedIconSet.count(icon.src) > 0) {
323         return;
324     }
325     m_processedIconSet.insert(icon.src);
326
327     LocaleSet localesAvailableForIcon;
328
329     FOREACH(i, m_localeFolders)
330     {
331         DPL::String pathPrefix = *i;
332         if (!pathPrefix.empty()) {
333             pathPrefix = L"locales/" + pathPrefix + L"/";
334         }
335
336         DPL::String relativePath = pathPrefix + icon.src;
337         DPL::String absolutePath = DPL::FromUTF8String(
338                 m_installContext.locations->getConfigurationDir()) + L"/" +
339             relativePath;
340
341         if (WrtUtilFileExists(DPL::ToUTF8String(absolutePath))) {
342             DPL::String type = MimeTypeUtils::identifyFileMimeType(absolutePath);
343
344             if (MimeTypeUtils::isMimeTypeSupportedForIcon(type)) {
345                 isAnyIconValid = true;
346                 localesAvailableForIcon.insert(*i);
347                 LogInfo("Icon absolutePath :" << absolutePath <<
348                         ", assigned locale :" << *i << ", type: " << type);
349             }
350         }
351     }
352
353     if (isAnyIconValid) {
354         WidgetRegisterInfo::LocalizedIcon localizedIcon(icon,
355                                                         localesAvailableForIcon);
356         m_installContext.widgetConfig.localizationData.icons.push_back(
357             localizedIcon);
358     }
359 }
360
361 void TaskWidgetConfig::ProcessWidgetInstalledPath()
362 {
363     LogDebug("ProcessWidgetInstalledPath");
364     m_installContext.widgetConfig.widgetInstalledPath =
365         DPL::FromUTF8String(
366             m_installContext.locations->getPackageInstallationDir());
367 }
368
369 void TaskWidgetConfig::ProcessAppControlInfo()
370 {
371     LogDebug("ProcessAppControlInfo");
372     using namespace WrtDB;
373
374     WrtDB::ConfigParserData::AppControlInfo::Disposition disposition =
375         WrtDB::ConfigParserData::AppControlInfo::Disposition::WINDOW;
376     FOREACH(it, m_installContext.widgetConfig.configInfo.settingsList) {
377         if (!strcmp(DPL::ToUTF8String(it->m_name).c_str(), "nodisplay") &&
378             !strcmp(DPL::ToUTF8String(it->m_value).c_str(), "true"))
379         {
380             disposition =
381                 WrtDB::ConfigParserData::AppControlInfo::Disposition::INLINE;
382         }
383     }
384
385     std::map<std::string, int> srcMap;
386     int index = 0;
387     // index = 0 is reserved for start file
388     FOREACH(startFileIt, m_installContext.widgetConfig.localizationData.startFiles)
389     {
390         if (!startFileIt->propertiesForLocales.empty()) {
391             std::string src = DPL::ToUTF8String(startFileIt->path);
392              if (srcMap.find(src) == srcMap.end()) {
393                 LogDebug("Insert [" << src << "," << index << "]");
394                 srcMap.insert(std::pair<std::string, int>(src, index++));
395             }
396         }
397     }
398
399     FOREACH(appControlIt, m_installContext.widgetConfig.configInfo.appControlList)
400     {
401         appControlIt->m_disposition = disposition;
402         std::string src = DPL::ToUTF8String(appControlIt->m_src);
403         LogDebug("src = [" << src << "]");
404         std::map<std::string, int>::iterator findIt = srcMap.find(src);
405         if (findIt == srcMap.end()) {
406             LogDebug("Insert [" << src << "," << index << "]");
407             srcMap.insert(std::pair<std::string, int>(src, index));
408             appControlIt->m_index = index++;
409         } else {
410             LogDebug("Exist  [" << src << "," << findIt->second << "]");
411             appControlIt->m_index = findIt->second;
412         }
413     }
414 }
415
416 void TaskWidgetConfig::ProcessSecurityModel()
417 {
418     // 0104.  If the "required_version" specified in the Web Application's
419     // configuration is 2.2 or higher and if the Web Application's
420     // configuration is "CSP-compatible configuration", then the WRT MUST be
421     // set to "CSP-based security mode". Otherwise, the WRT MUST be set to
422     // "WARP-based security mode".
423     // 0105.  A Web Application configuration is "CSP-compatible configuration"
424     // if the configuration includes one or more of
425     // <tizen:content-security-policy> /
426     // <tizen:content-security-policy-report-only> /
427     // <tizen:allow-navigation> elements.
428
429     bool isSecurityModelV1 = false;
430     bool isSecurityModelV2 = false;
431     WrtDB::ConfigParserData &data = m_installContext.widgetConfig.configInfo;
432
433     if (!data.cspPolicy.IsNull() ||
434         !data.cspPolicyReportOnly.IsNull() ||
435         !data.allowNavigationInfoList.empty())
436     {
437         data.accessInfoSet.clear();
438     }
439
440     // WARP is V1
441     if (!data.accessInfoSet.empty()) {
442         isSecurityModelV1 = true;
443     }
444
445     // CSP & allow-navigation is V2
446     if (!data.cspPolicy.IsNull() ||
447         !data.cspPolicyReportOnly.IsNull() ||
448         !data.allowNavigationInfoList.empty())
449     {
450         isSecurityModelV2 = true;
451     }
452
453     if (isSecurityModelV1 && isSecurityModelV2) {
454         LogError("Security model is conflict");
455         ThrowMsg(Exceptions::NotAllowed, "Security model is conflict");
456     } else if (isSecurityModelV1) {
457         data.securityModelVersion =
458             WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
459     } else if (isSecurityModelV2) {
460         data.securityModelVersion =
461             WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V2;
462     } else {
463         data.securityModelVersion =
464             WrtDB::ConfigParserData::SecurityModelVersion::SECURITY_MODEL_V1;
465     }
466
467     m_installContext.job->UpdateProgress(
468         InstallerContext::INSTALL_WIDGET_CONFIG2,
469         "Finished process security model");
470 }
471
472 void TaskWidgetConfig::StepCheckMinVersionInfo()
473 {
474     if (!isMinVersionCompatible(
475             m_installContext.widgetConfig.webAppType.appType,
476             m_installContext.widgetConfig.minVersion))
477     {
478         LogError(
479             "Platform version lower than required -> cancelling installation");
480         ThrowMsg(Exceptions::NotAllowed,
481                  "Platform version does not meet requirements");
482     }
483
484     m_installContext.job->UpdateProgress(
485         InstallerContext::INSTALL_WIDGET_CONFIG2,
486         "Check MinVersion Finished");
487 }
488
489 void TaskWidgetConfig::StepVerifyFeatures()
490 {
491     using namespace WrtDB;
492     ConfigParserData &data = m_installContext.widgetConfig.configInfo;
493     ConfigParserData::FeaturesList list = data.featuresList;
494     ConfigParserData::FeaturesList newList;
495
496     //in case of tests, this variable is unused
497     std::string featureInfo;
498     FOREACH(it, list)
499     {
500         // check feature vender for permission
501         // WAC, TIZEN WebApp cannot use other feature
502
503         if (!isFeatureAllowed(m_installContext.widgetConfig.webAppType.appType,
504                               it->name))
505         {
506             LogInfo("This application type not allowed to use this feature");
507             ThrowMsg(
508                 Exceptions::WidgetConfigFileInvalid,
509                 "This app type [" <<
510                 m_installContext.widgetConfig.webAppType.getApptypeToString()
511                                   <<
512                 "] cannot be allowed to use [" <<
513                 DPL::ToUTF8String(it->name) + "] feature");
514         } else {
515             newList.insert(*it);
516             featureInfo += DPL::ToUTF8String(it->name);
517             featureInfo += DPL::ToUTF8String(BR);
518         }
519     }
520     if (!data.accessInfoSet.empty()) {
521         featureInfo += WINDGET_INSTALL_NETWORK_ACCESS;
522         featureInfo += DPL::ToUTF8String(BR);
523     }
524     data.featuresList = newList;
525     if (!featureInfo.empty()) {
526         m_popupData.addWidgetInfo(DPL::FromUTF8String(featureInfo));
527     }
528
529     m_installContext.job->UpdateProgress(
530         InstallerContext::INSTALL_WIDGET_CONFIG2,
531         "Widget Config step2 Finished");
532 }
533
534 void TaskWidgetConfig::StepVerifyLivebox()
535 {
536     using namespace WrtDB;
537     ConfigParserData &data = m_installContext.widgetConfig.configInfo;
538     ConfigParserData::LiveboxList liveBoxList = data.m_livebox;
539
540     if (liveBoxList.size() <= 0) {
541         return;
542     }
543
544     FOREACH (it, liveBoxList) {
545         std::string boxType;
546
547         if ((**it).m_type.empty()) {
548             boxType = web_provider_livebox_get_default_type();
549         } else {
550             boxType = DPL::ToUTF8String((**it).m_type);
551         }
552
553         LogInfo("livebox type: " << boxType);
554
555         ConfigParserData::LiveboxInfo::BoxSizeList boxSizeList =
556             (**it).m_boxInfo.m_boxSize;
557         char** boxSize = static_cast<char**>(
558             malloc(sizeof(char*)* boxSizeList.size()));
559
560         int boxSizeCnt = 0;
561         FOREACH (m, boxSizeList) {
562             boxSize[boxSizeCnt++] = strdup(DPL::ToUTF8String((*m).m_size).c_str());
563         }
564
565         bool chkSize = web_provider_plugin_check_supported_size(
566             boxType.c_str(), boxSize, boxSizeCnt);
567
568         for(int i = 0; i < boxSizeCnt; i++) {
569             free(boxSize[i]);
570         }
571         free(boxSize);
572
573         if(!chkSize) {
574             LogError("Invalid boxSize");
575             ThrowMsg(Exceptions::WidgetConfigFileInvalid, "Invalid boxSize");
576         }
577     }
578 }
579
580 bool TaskWidgetConfig::isFeatureAllowed(WrtDB::AppType appType,
581                                         DPL::String featureName)
582 {
583     using namespace WrtDB;
584     LogInfo("AppType = [" <<
585             WidgetType(appType).getApptypeToString() << "]");
586     LogInfo("FetureName = [" << featureName << "]");
587
588     AppType featureType = APP_TYPE_UNKNOWN;
589     std::string featureStr = DPL::ToUTF8String(featureName);
590     const char* feature = featureStr.c_str();
591
592     // check prefix of  feature name
593     if (strstr(feature, PluginsPrefix::TIZENPluginsPrefix) == feature) {
594         // Tizen WebApp feature
595         featureType = APP_TYPE_TIZENWEBAPP;
596     } else if (strstr(feature, PluginsPrefix::WACPluginsPrefix) == feature) {
597         // WAC 2.0 feature
598         featureType = APP_TYPE_WAC20;
599     } else if (strstr(feature, PluginsPrefix::W3CPluginsPrefix) == feature) {
600         // W3C standard feature
601         // Both WAC and TIZEN WebApp are possible to use W3C plugins
602         return true;
603     } else {
604         // unknown feature
605         // unknown feature will be checked next step
606         return true;
607     }
608
609     if (appType == featureType) {
610         return true;
611     }
612     return false;
613 }
614
615 bool TaskWidgetConfig::parseVersionString(const std::string &version,
616                                           long &majorVersion,
617                                           long &minorVersion,
618                                           long &microVersion) const
619 {
620     std::istringstream inputString(version);
621     inputString >> majorVersion;
622     if (inputString.bad() || inputString.fail()) {
623         LogWarning("Invalid minVersion format.");
624         return false;
625     }
626     inputString.get(); // skip period
627     inputString >> minorVersion;
628     if (inputString.bad() || inputString.fail()) {
629         LogWarning("Invalid minVersion format");
630         return false;
631     } else {
632         inputString.get(); // skip period
633         if (inputString.bad() || inputString.fail()) {
634             inputString >> microVersion;
635         }
636     }
637     return true;
638 }
639
640 bool TaskWidgetConfig::isMinVersionCompatible(
641     WrtDB::AppType appType,
642     const DPL::OptionalString &
643     widgetVersion) const
644 {
645     if (widgetVersion.IsNull() || (*widgetVersion).empty()) {
646         if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
647             return false;
648         } else {
649             LogWarning("minVersion attribute is empty. WRT assumes platform "
650                     "supports this widget.");
651             return true;
652         }
653     }
654
655     //Parse widget version
656     long majorWidget = 0, minorWidget = 0, microWidget = 0;
657     if (!parseVersionString(DPL::ToUTF8String(*widgetVersion), majorWidget,
658                             minorWidget, microWidget))
659     {
660         LogWarning("Invalid format of widget version string.");
661         return false;
662     }
663
664     //Parse supported version
665     long majorSupported = 0, minorSupported = 0, microSupported = 0;
666     std::string version;
667     if (appType == WrtDB::AppType::APP_TYPE_TIZENWEBAPP) {
668         version = WrtDB::GlobalConfig::GetTizenVersion();
669     } else if (appType == WrtDB::AppType::APP_TYPE_WAC20) {
670         version = WrtDB::GlobalConfig::GetWACVersion();
671     } else {
672         LogWarning("Invaild AppType");
673         return false;
674     }
675
676     if (!parseVersionString(version,
677                             majorSupported, minorSupported, microSupported))
678     {
679         LogWarning("Invalid format of platform version string.");
680         return true;
681     }
682
683     if (majorWidget > majorSupported ||
684         (majorWidget == majorSupported && minorWidget > minorSupported) ||
685         (majorWidget == majorSupported && minorWidget == minorSupported
686          && microWidget > microSupported))
687     {
688         LogInfo("Platform doesn't support this widget.");
689         return false;
690     }
691     return true;
692 }
693
694 bool TaskWidgetConfig::isTizenWebApp() const
695 {
696     bool ret = FALSE;
697     if (m_installContext.widgetConfig.webAppType.appType
698         == WrtDB::AppType::APP_TYPE_TIZENWEBAPP)
699     {
700         ret = TRUE;
701     }
702
703     return ret;
704 }
705
706 bool TaskWidgetConfig::parseConfigurationFileBrowser(
707     WrtDB::ConfigParserData& configInfo,
708     const std::string& _currentPath)
709 {
710     ParserRunner parser;
711     Try
712     {
713         parser.Parse(_currentPath, ElementParserPtr(new
714                                                     RootParser<
715                                                         WidgetParser>(
716                                                         configInfo,
717                                                         DPL::FromUTF32String(
718                                                             L"widget"))));
719     }
720     Catch(ElementParser::Exception::Base)
721     {
722         LogError("Invalid widget configuration file!");
723         return false;
724     }
725     return true;
726 }
727
728 bool TaskWidgetConfig::parseConfigurationFileWidget(
729     WrtDB::ConfigParserData& configInfo,
730     const std::string& _currentPath)
731 {
732     std::string configFilePath;
733     WrtUtilJoinPaths(configFilePath, _currentPath, WRT_WIDGET_CONFIG_FILE_NAME);
734     if (!WrtUtilFileExists(configFilePath))
735     {
736         LogError("Archive does not contain configuration file");
737         return false;
738     }
739
740     LogDebug("Configuration file: " << configFilePath);
741
742     Try
743     {
744         ParserRunner parser;
745 #ifdef SCHEMA_VALIDATION_ENABLED
746         if(!parser.Validate(configFilePath, WRT_WIDGETS_XML_SCHEMA))
747         {
748             LogError("Invalid configuration file - schema validation failed");
749             return false;
750         }
751 #endif
752         parser.Parse(configFilePath,
753                      ElementParserPtr(new RootParser<WidgetParser>(
754                                           configInfo,
755                                           DPL::FromUTF32String(L"widget"))));
756         return true;
757     }
758     Catch (ElementParser::Exception::Base)
759     {
760         LogError("Invalid configuration file!");
761         return false;
762     }
763 }
764
765 bool TaskWidgetConfig::locateAndParseConfigurationFile(
766     const std::string& _currentPath,
767     WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
768     const std::string& baseFolder)
769 {
770     using namespace WrtDB;
771
772     ConfigParserData& configInfo = pWidgetConfigInfo.configInfo;
773
774     // check if this installation from browser, or not.
775     size_t pos = _currentPath.rfind("/");
776     std::ostringstream infoPath;
777     infoPath << _currentPath.substr(pos + 1);
778
779     if (infoPath.str() != WRT_WIDGET_CONFIG_FILE_NAME) {
780         if (_currentPath.empty() || baseFolder.empty()) {
781             return false;
782         }
783         // in case of general installation using wgt archive
784         if (!parseConfigurationFileWidget(configInfo, _currentPath))
785         {
786             return false;
787         }
788     } else {
789         // in case of browser installation
790         if (!parseConfigurationFileBrowser(configInfo, _currentPath))
791         {
792             return false;
793         }
794     }
795
796     if (!fillWidgetConfig(pWidgetConfigInfo, configInfo)) {
797         return false;
798     }
799     return true;
800 }
801
802 bool TaskWidgetConfig::fillWidgetConfig(
803     WrtDB::WidgetRegisterInfo& pWidgetConfigInfo,
804     WrtDB::ConfigParserData& configInfo)
805 {
806     if (!!configInfo.widget_id) {
807         if (!pWidgetConfigInfo.guid) {
808             pWidgetConfigInfo.guid = configInfo.widget_id;
809         } else {
810             if (pWidgetConfigInfo.guid != configInfo.widget_id) {
811                 LogError("Invalid archive");
812                 return false;
813             }
814         }
815     }
816     if (!!configInfo.tizenAppId) {
817         if (DPL::ToUTF8String(pWidgetConfigInfo.tzAppid).compare(
818                 DPL::ToUTF8String(*configInfo.tizenAppId)) < 0)
819         {
820             LogError("Invalid archive - Tizen App ID not same error");
821             return false;
822         }
823     }
824     if (!!configInfo.tizenPkgId) {
825         if (pWidgetConfigInfo.tzPkgid != *configInfo.tizenPkgId) {
826             LogError("Invalid archive - Tizen Pkg ID not same error");
827             return false;
828         }
829     }
830     if (!!configInfo.version) {
831         if (!pWidgetConfigInfo.version) {
832             pWidgetConfigInfo.version = configInfo.version;
833         } else {
834             if (pWidgetConfigInfo.version != configInfo.version) {
835                 LogError("Invalid archive");
836                 return false;
837             }
838         }
839     }
840     if (!!configInfo.minVersionRequired) {
841         pWidgetConfigInfo.minVersion = configInfo.minVersionRequired;
842     } else if (!!configInfo.tizenMinVersionRequired) {
843         pWidgetConfigInfo.minVersion = configInfo.tizenMinVersionRequired;
844     }
845     return true;
846 }
847
848 void TaskWidgetConfig::processFile(
849     const std::string& path,
850     WrtDB::WidgetRegisterInfo &
851     widgetConfiguration)
852 {
853     if (!locateAndParseConfigurationFile(path, widgetConfiguration,
854                                          DEFAULT_LANGUAGE))
855     {
856         LogWarning("Widget archive: Failed while parsing config file");
857         ThrowMsg(Exception::ConfigParseFailed, path);
858     }
859 }
860 } //namespace WidgetInstall
861 } //namespace Jobs