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