Coding style
[platform/core/appfw/wgt-backend.git] / src / wgt / step / configuration / step_parse.cc
1 /* 2014, Copyright © Intel Coporation, license APACHE-2.0, see LICENSE file */
2 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a apache 2.0 license that can be
4 // found in the LICENSE file.
5
6 #include "wgt/step/configuration/step_parse.h"
7
8 #include <boost/filesystem/path.hpp>
9
10 #include <common/app_installer.h>
11 #include <common/paths.h>
12 #include <common/installer_context.h>
13 #include <common/step/step.h>
14 #include <common/utils/glist_range.h>
15 #include <manifest_parser/utils/version_number.h>
16 #include <wgt_manifest_handlers/account_handler.h>
17 #include <wgt_manifest_handlers/app_control_handler.h>
18 #include <wgt_manifest_handlers/application_icons_handler.h>
19 #include <wgt_manifest_handlers/application_manifest_constants.h>
20 #include <wgt_manifest_handlers/background_category_handler.h>
21 #include <wgt_manifest_handlers/category_handler.h>
22 #include <wgt_manifest_handlers/content_handler.h>
23 #include <wgt_manifest_handlers/ime_handler.h>
24 #include <wgt_manifest_handlers/metadata_handler.h>
25 #include <wgt_manifest_handlers/service_handler.h>
26 #include <wgt_manifest_handlers/setting_handler.h>
27 #include <wgt_manifest_handlers/tizen_application_handler.h>
28 #include <wgt_manifest_handlers/widget_handler.h>
29 #include <wgt_manifest_handlers/w3c_pc_utils.h>
30
31 #include <pkgmgr/pkgmgr_parser.h>
32
33 #include <string.h>
34
35 #include <chrono>
36 #include <cstdio>
37 #include <cstdlib>
38 #include <memory>
39 #include <set>
40 #include <string>
41 #include <vector>
42 #include <utility>
43
44 #include "wgt/wgt_backend_data.h"
45
46 namespace bf = boost::filesystem;
47
48 namespace {
49
50 const char kCategoryWearableClock[] =
51     "http://tizen.org/category/wearable_clock";
52 const char kCategoryWatchClock[] = "com.samsung.wmanager.WATCH_CLOCK";
53
54 const std::string kManifestVersion = "1.0.0";
55 const char kTizenPackageXmlNamespace[] = "http://tizen.org/ns/packages";
56 const char kImeCategoryName[] = "http://tizen.org/category/ime";
57 const char kDownloadableFontCategoryName[] =
58     "http://tizen.org/category/downloadable_font";
59 const char kTTSCategoryName[] = "http://tizen.org/category/tts";
60
61 const char kResWgt[] = "res/wgt";
62 const char kConfigFileName[] = "config.xml";
63
64 GList* GenerateMetadataListX(const wgt::parse::MetaDataInfo& meta_info) {
65   GList* list = nullptr;
66   for (auto& meta : meta_info.metadata()) {
67     metadata_x* new_meta =
68         static_cast<metadata_x*>(calloc(1, sizeof(metadata_x)));
69     new_meta->key = strdup(meta.first.c_str());
70     if (!meta.second.empty())
71       new_meta->value = strdup(meta.second.c_str());
72     list = g_list_append(list, new_meta);
73   }
74   return list;
75 }
76
77 void AppendWidgetMetadata(GList** metadatas,
78     const std::vector<std::pair<std::string, std::string>> metadata) {
79   GList* list = *metadatas;
80   for (auto& meta : metadata) {
81     metadata_x* new_meta =
82         static_cast<metadata_x*>(calloc(1, sizeof(metadata_x)));
83     new_meta->key = strdup(meta.first.c_str());
84     if (!meta.second.empty())
85       new_meta->value = strdup(meta.second.c_str());
86
87     list = g_list_append(list, new_meta);
88   }
89
90   *metadatas = list;
91 }
92
93 void SetApplicationXDefaults(application_x* application) {
94   application->effectimage_type = strdup("image");
95   application->enabled = strdup("true");
96   application->guestmode_visibility = strdup("true");
97   application->hwacceleration = strdup("default");
98   application->indicatordisplay = strdup("true");
99   application->launchcondition = strdup("false");
100   application->permission_type = strdup("normal");
101   application->process_pool = strdup("false");
102   application->recentimage = strdup("false");
103   application->screenreader = strdup("use-system-setting");
104   application->submode = strdup("false");
105   application->support_disable = strdup("false");
106   application->ui_gadget = strdup("false");
107   application->multiple = strdup("false");
108 }
109
110 template<typename T>
111 void AppendLabel(T* root, const std::string& label,
112                  const std::string& locale) {
113   label_x* label_item = reinterpret_cast<label_x*>(calloc(1, sizeof(label_x)));
114   label_item->name = strdup(label.c_str());
115   label_item->text = strdup(label.c_str());
116   label_item->lang = !locale.empty() ?
117       strdup(locale.c_str()) : strdup(DEFAULT_LOCALE);
118   root->label = g_list_append(root->label, label_item);
119 }
120 }  // namespace
121
122 namespace wgt {
123 namespace configuration {
124
125 namespace app_keys = wgt::application_widget_keys;
126 namespace sc = std::chrono;
127
128 StepParse::StepParse(common_installer::InstallerContext* context,
129                      ConfigLocation config_location,
130                      bool check_start_file)
131     : Step(context),
132       config_location_(config_location),
133       check_start_file_(check_start_file) {
134 }
135
136 std::set<std::string> StepParse::ExtractPrivileges(
137     std::shared_ptr<const wgt::parse::PermissionsInfo> perm_info) const {
138   return perm_info->GetAPIPermissions();
139 }
140
141 std::string StepParse::GetPackageVersion(
142      const std::string& manifest_version) {
143   if (manifest_version.empty()) {
144     return kManifestVersion;
145   }
146   std::string version = manifest_version.substr(0,
147       manifest_version.find_first_not_of("1234567890."));
148
149   return version;
150 }
151
152 bool StepParse::FillInstallationInfo(manifest_x* manifest) {
153   manifest->root_path = strdup(
154       (context_->root_application_path.get() / manifest->package).c_str());
155   manifest->installed_time =
156       strdup(std::to_string(sc::system_clock::to_time_t(
157           sc::system_clock::now())).c_str());
158   return true;
159 }
160
161 bool StepParse::FillIconPaths(manifest_x* manifest) {
162   auto app_info =
163       GetManifestDataForKey<const wgt::parse::TizenApplicationInfo>(
164              app_keys::kTizenApplicationKey);
165   if (!app_info) {
166     LOG(ERROR) << "Application info manifest data has not been found.";
167     return false;
168   }
169   auto icons_info =
170     GetManifestDataForKey<const wgt::parse::ApplicationIconsInfo>(
171            app_keys::kIconsKey);
172   if (!icons_info) {
173     icons_info.reset(new wgt::parse::ApplicationIconsInfo());
174   }
175   wgt::parse::LocalizedApplicationIconsInfo localized_list =
176       wgt::parse::GetLocalizedIconList(*icons_info, widget_path_);
177   // We need to generate icon for each locale and icons are already set into
178   // lookup order. There isn't said that all icons should be received from
179   // one <icon> tag position so we iterate utils we run out of icons creating
180   // any icon element that are possible for given locale.
181   std::set<std::string> found_locales;
182   for (auto& application_icon : localized_list) {
183     const std::string& locale = application_icon.locale();
184     if (found_locales.find(locale) != found_locales.end())
185       continue;
186     found_locales.insert(locale);
187
188     icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
189     bf::path icon_path = context_->root_application_path.get()
190         / app_info->package() / "res" / "wgt" / application_icon.path();
191     icon->text = strdup(icon_path.c_str());
192     if (!locale.empty())
193       icon->lang = strdup(locale.c_str());
194     else
195       icon->lang = strdup(DEFAULT_LOCALE);
196     manifest->icon = g_list_append(manifest->icon, icon);
197   }
198   return true;
199 }
200
201 bool StepParse::FillWidgetInfo(manifest_x* manifest) {
202   auto wgt_info =
203       GetManifestDataForKey<const wgt::parse::WidgetInfo>(
204              app_keys::kWidgetKey);
205
206   if (!wgt_info.get()) {
207     LOG(ERROR) << "Widget info manifest data has not been found.";
208     return false;
209   }
210
211   const std::string& version = wgt_info->version();
212
213   manifest->ns = strdup(kTizenPackageXmlNamespace);
214   manifest->version = strdup(GetPackageVersion(version).c_str());
215
216   for (auto& item : wgt_info->description_set()) {
217     description_x* description = reinterpret_cast<description_x*>
218         (calloc(1, sizeof(description_x)));
219     description->text = strdup(item.second.c_str());
220     description->lang = !item.first.empty() ?
221         strdup(item.first.c_str()) : strdup(DEFAULT_LOCALE);
222     manifest->description = g_list_append(manifest->description, description);
223   }
224
225   for (auto& item : wgt_info->name_set()) {
226     AppendLabel(manifest, item.second, item.first);
227   }
228
229   manifest->type = strdup("wgt");
230   manifest->appsetting = strdup("false");
231   manifest->nodisplay_setting = strdup("false");
232   if (context_->is_preload_request.get())
233     manifest->preload = strdup("true");
234   else
235     manifest->preload = strdup("false");
236   manifest->installed_storage = strdup("installed_internal");
237
238   // For wgt package use the long name
239   application_x* app =
240       reinterpret_cast<application_x*>(manifest->application->data);
241   for (auto& item : wgt_info->name_set()) {
242     AppendLabel(app, item.second, item.first);
243   }
244
245   author_x* author = reinterpret_cast<author_x*>(calloc(1, sizeof(author_x)));
246   if (!wgt_info->author().empty())
247     author->text = strdup(wgt_info->author().c_str());
248   if (!wgt_info->author_email().empty())
249     author->email = strdup(wgt_info->author_email().c_str());
250   if (!wgt_info->author_href().empty())
251     author->href = strdup(wgt_info->author_href().c_str());
252   author->lang = strdup(DEFAULT_LOCALE);
253   manifest->author = g_list_append(manifest->author, author);
254
255   auto settings_info =
256       GetManifestDataForKey<const wgt::parse::SettingInfo>(
257              wgt::application_widget_keys::kTizenSettingKey);
258   if (settings_info) {
259     switch (settings_info->install_location()) {
260     case wgt::parse::SettingInfo::InstallLocation::AUTO: {
261       manifest->installlocation = strdup("auto");
262       break;
263     }
264     case wgt::parse::SettingInfo::InstallLocation::INTERNAL: {
265       manifest->installlocation = strdup("internal-only");
266       break;
267     }
268     case wgt::parse::SettingInfo::InstallLocation::EXTERNAL: {
269       manifest->installlocation = strdup("prefer-external");
270       break;
271     }
272     }
273   } else {
274     manifest->installlocation = strdup("auto");
275   }
276
277   return true;
278 }
279
280 bool StepParse::FillMainApplicationInfo(manifest_x* manifest) {
281   auto app_info =
282       GetManifestDataForKey<const wgt::parse::TizenApplicationInfo>(
283              app_keys::kTizenApplicationKey);
284   if (!app_info) {
285     LOG(ERROR) << "Application info manifest data has not been found.";
286     return false;
287   }
288   bool has_watch_category = false;
289   bool has_ime = false;
290   bool has_downloadable_font = false;
291   bool has_tts = false;
292   auto category_info =
293       GetManifestDataForKey<const wgt::parse::CategoryInfoList>(
294              app_keys::kTizenCategoryKey);
295
296   if (category_info) {
297     has_watch_category = std::find_if(category_info->categories.begin(),
298                                        category_info->categories.end(),
299                                        [](const std::string& category) {
300       return category == kCategoryWearableClock ||
301              category == kCategoryWatchClock;
302     }) != category_info->categories.end();
303     has_ime = std::find(category_info->categories.begin(),
304                                        category_info->categories.end(),
305                                        kImeCategoryName)
306         != category_info->categories.end();
307     has_downloadable_font = std::find(category_info->categories.begin(),
308                                        category_info->categories.end(),
309                                        kDownloadableFontCategoryName)
310         != category_info->categories.end();
311     has_tts = std::find(category_info->categories.begin(),
312                                        category_info->categories.end(),
313                                        kTTSCategoryName)
314         != category_info->categories.end();
315   }
316
317   // application data
318   application_x* application = reinterpret_cast<application_x*>(
319       calloc(1, sizeof(application_x)));
320   application->component_type =
321       has_watch_category ? strdup("watchapp") : strdup("uiapp");
322   application->mainapp = strdup("true");
323   application->appid = strdup(app_info->id().c_str());
324   auto settings_info =
325       GetManifestDataForKey<const wgt::parse::SettingInfo>(
326              wgt::application_widget_keys::kTizenSettingKey);
327
328   bool no_display = settings_info ? settings_info->no_display() : false;
329   bool has_no_display_category =
330       has_watch_category || has_ime || has_tts || has_downloadable_font;
331
332   application->nodisplay = (has_no_display_category || no_display) ?
333       strdup("true") : strdup("false");
334   application->taskmanage = has_no_display_category ? strdup("false") :
335       strdup("true");
336
337   SetApplicationXDefaults(application);
338   if (has_watch_category)
339     application->ambient_support =
340         strdup(app_info->ambient_support() ? "true" : "false");
341   else
342     application->ambient_support = strdup("false");
343   application->package = strdup(app_info->package().c_str());
344
345   application->exec =
346       strdup((context_->root_application_path.get() / app_info->package()
347               / "bin" / application->appid).c_str());
348   application->type = strdup("webapp");
349   application->onboot = strdup("false");
350   application->autorestart = strdup("false");
351
352   application->launch_mode = strdup(app_info->launch_mode().c_str());
353   for (auto& icon : GListRange<icon_x*>(manifest->icon)) {
354     icon_x* app_icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
355     app_icon->text = strdup(icon->text);
356     app_icon->lang = strdup(icon->lang);
357     application->icon = g_list_append(application->icon, app_icon);
358   }
359   // guarantees that the main app will be at the begining of the list
360   manifest->application = g_list_insert(manifest->application, application, 0);
361
362   manifest->package = strdup(app_info->package().c_str());
363   manifest->mainapp_id = strdup(app_info->id().c_str());
364   return true;
365 }
366
367 bool StepParse::FillServiceApplicationInfo(manifest_x* manifest) {
368   auto service_list =
369       GetManifestDataForKey<const wgt::parse::ServiceList>(
370              app_keys::kTizenServiceKey);
371   if (!service_list)
372     return true;
373   for (auto& service_info : service_list->services) {
374     application_x* application = reinterpret_cast<application_x*>
375         (calloc(1, sizeof(application_x)));
376     application->component_type = strdup("svcapp");
377     application->mainapp = strdup("false");
378     application->appid = strdup(service_info.id().c_str());
379     application->exec =
380         strdup((context_->root_application_path.get() / manifest->package
381                 / "bin" / application->appid).c_str());
382     application->type = strdup("webapp");
383     application->onboot =
384         service_info.on_boot() ? strdup("true") : strdup("false");
385     application->autorestart =
386         service_info.auto_restart() ? strdup("true") : strdup("false");
387     application->nodisplay = strdup("false");
388     application->taskmanage = strdup("true");
389     SetApplicationXDefaults(application);
390     application->ambient_support = strdup("false");
391     application->package = strdup(manifest->package);
392
393     for (auto& pair : service_info.names()) {
394       AppendLabel(application, pair.second, pair.first);
395     }
396
397     if (!service_info.icon().empty()) {
398       bf::path icon_path = context_->root_application_path.get()
399           / manifest->package / "res" / "wgt" / service_info.icon();
400       icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
401       icon->text = strdup(icon_path.c_str());
402       icon->lang = strdup(DEFAULT_LOCALE);
403       application->icon = g_list_append(application->icon, icon);
404     }
405
406     for (auto& category : service_info.categories()) {
407       application->category = g_list_append(application->category,
408                                             strdup(category.c_str()));
409     }
410
411     for (auto& pair : service_info.metadata_set()) {
412       metadata_x* item = reinterpret_cast<metadata_x*>(
413           calloc(1, sizeof(metadata_x)));
414       item->key = strdup(pair.first.c_str());
415       if (!pair.second.empty())
416         item->value = strdup(pair.second.c_str());
417       application->metadata = g_list_append(application->metadata, item);
418     }
419
420     manifest->application = g_list_append(manifest->application, application);
421   }
422   return true;
423 }
424
425 bool StepParse::FillWidgetApplicationInfo(manifest_x* manifest) {
426   auto appwidget_info =
427       GetManifestDataForKey<const wgt::parse::AppWidgetInfo>(
428              wgt::application_widget_keys::kTizenAppWidgetFullKey);
429   if (!appwidget_info)
430     return true;
431   for (auto& app_widget : appwidget_info->app_widgets()) {
432     application_x* application = reinterpret_cast<application_x*>
433         (calloc(1, sizeof(application_x)));
434     application->component_type = strdup("widgetapp");
435     application->mainapp = strdup("false");
436     application->appid = strdup(app_widget.id.c_str());
437     application->exec =
438         strdup((context_->root_application_path.get() / manifest->package
439                 / "bin" / application->appid).c_str());
440     application->type = strdup("webapp");
441     application->nodisplay = strdup("true");
442     application->taskmanage = strdup("false");
443     SetApplicationXDefaults(application);
444     application->ambient_support = strdup("false");
445     application->package = strdup(manifest->package);
446
447     if (!app_widget.label.default_value.empty()) {
448       AppendLabel(application, app_widget.label.default_value, std::string());
449     }
450
451     for (auto& pair : app_widget.label.lang_value_map) {
452       AppendLabel(application, pair.second, pair.first);
453     }
454
455     if (!app_widget.icon_src.empty()) {
456       icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
457       icon->text = strdup(app_widget.icon_src.c_str());
458       icon->lang = strdup(DEFAULT_LOCALE);
459       application->icon = g_list_append(application->icon, icon);
460     }
461
462     if (!app_widget.metadata.empty())
463       AppendWidgetMetadata(&application->metadata, app_widget.metadata);
464
465     manifest->application = g_list_append(manifest->application, application);
466   }
467   return true;
468 }
469
470
471 bool StepParse::FillBackgroundCategoryInfo(manifest_x* manifest) {
472   auto manifest_data = parser_->GetManifestData(
473       app_keys::kTizenBackgroundCategoryKey);
474   std::shared_ptr<const wgt::parse::BackgroundCategoryInfoList> bc_list =
475       std::static_pointer_cast<const wgt::parse::BackgroundCategoryInfoList>(
476           manifest_data);
477
478   if (!bc_list)
479     return true;
480
481   application_x* app =
482       reinterpret_cast<application_x*>(manifest->application->data);
483
484   for (auto& background_category : bc_list->background_categories) {
485     app->background_category = g_list_append(
486         app->background_category, strdup(background_category.value().c_str()));
487   }
488
489   return true;
490 }
491
492 bool StepParse::FillAppControl(manifest_x* manifest) {
493   auto app_info_list =
494       GetManifestDataForKey<const wgt::parse::AppControlInfoList>(
495              app_keys::kTizenApplicationAppControlsKey);
496
497   application_x* app =
498       reinterpret_cast<application_x*>(manifest->application->data);
499   if (app_info_list) {
500     for (const auto& control : app_info_list->controls) {
501       appcontrol_x* app_control =
502           static_cast<appcontrol_x*>(calloc(1, sizeof(appcontrol_x)));
503       app_control->operation = strdup(control.operation().c_str());
504       app_control->mime = strdup(control.mime().c_str());
505       app_control->uri = strdup(control.uri().c_str());
506       app->appcontrol = g_list_append(app->appcontrol, app_control);
507     }
508   }
509   return true;
510 }
511
512 bool StepParse::FillPrivileges(manifest_x* manifest) {
513   auto perm_info =
514       GetManifestDataForKey<const wgt::parse::PermissionsInfo>(
515              app_keys::kTizenPermissionsKey);
516   std::set<std::string> privileges;
517   if (perm_info)
518     privileges = ExtractPrivileges(perm_info);
519
520   for (auto& priv : privileges) {
521     manifest->privileges =
522         g_list_append(manifest->privileges, strdup(priv.c_str()));
523   }
524   return true;
525 }
526
527 bool StepParse::FillCategories(manifest_x* manifest) {
528   auto category_info =
529       GetManifestDataForKey<const wgt::parse::CategoryInfoList>(
530              app_keys::kTizenCategoryKey);
531   if (!category_info)
532     return true;
533
534   application_x* app =
535       reinterpret_cast<application_x*>(manifest->application->data);
536   // there is one app atm
537   for (auto& category : category_info->categories) {
538     app->category = g_list_append(app->category, strdup(category.c_str()));
539   }
540   return true;
541 }
542
543 bool StepParse::FillMetadata(manifest_x* manifest) {
544   auto meta_info =
545       GetManifestDataForKey<const wgt::parse::MetaDataInfo>(
546              app_keys::kTizenMetaDataKey);
547   if (!meta_info)
548     return true;
549
550   for (application_x* app : GListRange<application_x*>(manifest->application)) {
551     app->metadata = GenerateMetadataListX(*meta_info);
552   }
553   return true;
554 }
555
556 bool StepParse::FillAppWidget() {
557   // This is needed to store preview icons which are not saved into manifest_x
558   WgtBackendData* backend_data =
559       static_cast<WgtBackendData*>(context_->backend_data.get());
560
561   auto appwidget_info =
562       GetManifestDataForKey<const wgt::parse::AppWidgetInfo>(
563              wgt::application_widget_keys::kTizenAppWidgetFullKey);
564   if (appwidget_info)
565     backend_data->appwidgets.set(*appwidget_info);
566   return true;
567 }
568
569 bool StepParse::FillAccounts(manifest_x* manifest) {
570   auto account_info =
571       GetManifestDataForKey<const wgt::parse::AccountInfo>(
572              app_keys::kAccountKey);
573   if (!account_info)
574     return true;
575   common_installer::AccountInfo info;
576   for (auto& account : account_info->accounts()) {
577     common_installer::SingleAccountInfo single_info;
578     single_info.capabilities = account.capabilities;
579     single_info.icon_paths = account.icon_paths;
580     single_info.multiple_account_support = account.multiple_account_support;
581     single_info.names = account.names;
582     // wgt can contain only one app so this assumes mainapp_id is valid here
583     single_info.appid = manifest->mainapp_id;
584     info.set_account(single_info);
585   }
586   context_->manifest_plugins_data.get().account_info.set(info);
587   return true;
588 }
589
590 bool StepParse::FillImeInfo() {
591   auto ime_info =
592       GetManifestDataForKey<const wgt::parse::ImeInfo>(
593              app_keys::kTizenImeKey);
594   if (!ime_info)
595     return true;
596
597   common_installer::ImeInfo info;
598   info.setUuid(ime_info->uuid());
599
600   const auto &languages = ime_info->languages();
601   for (const auto &language : languages)
602     info.AddLanguage(language);
603
604   context_->manifest_plugins_data.get().ime_info.set(std::move(info));
605   return true;
606 }
607
608 bool StepParse::FillExtraManifestInfo(manifest_x* manifest) {
609   return FillAccounts(manifest) && FillImeInfo() && FillAppWidget();
610 }
611
612 bool StepParse::FillManifestX(manifest_x* manifest) {
613   // Fill data for main application
614   if (!FillIconPaths(manifest))
615     return false;
616   if (!FillMainApplicationInfo(manifest))
617     return false;
618   if (!FillWidgetInfo(manifest))
619     return false;
620   if (!FillInstallationInfo(manifest))
621     return false;
622   if (!FillPrivileges(manifest))
623     return false;
624   if (!FillAppControl(manifest))
625     return false;
626   if (!FillCategories(manifest))
627     return false;
628   if (!FillMetadata(manifest))
629     return false;
630   if (!FillBackgroundCategoryInfo(manifest))
631     return false;
632
633   // Fill data for other applications
634   if (!FillAdditionalApplications(manifest))
635     return false;
636
637   // Fill extra data, other than manifest_x structure
638   if (!FillExtraManifestInfo(manifest))
639     return false;
640
641   return true;
642 }
643
644
645 bool StepParse::FillAdditionalApplications(manifest_x* manifest) {
646   if (!FillServiceApplicationInfo(manifest))
647     return false;
648   if (!FillWidgetApplicationInfo(manifest))
649     return false;
650   return true;
651 }
652
653 bool StepParse::LocateConfigFile() {
654   switch (config_location_) {
655     case ConfigLocation::PACKAGE:
656       return StepParse::Check(context_->unpacked_dir_path.get());
657     case ConfigLocation::INSTALLED:
658       return StepParse::Check(context_->pkg_path.get() / kResWgt);
659     case ConfigLocation::RECOVERY:
660       if (StepParse::Check(common_installer::GetBackupPathForPackagePath(
661           context_->root_application_path.get()
662               / context_->pkgid.get()) / kResWgt))
663         return true;
664       if (StepParse::Check(
665           context_->root_application_path.get()
666               / context_->pkgid.get() / kResWgt))
667         return true;
668       return false;
669     case ConfigLocation::RESOURCE_WGT:
670       return StepParse::Check(context_->unpacked_dir_path.get() / kResWgt);
671     default:
672       LOG(ERROR) << "Unknown config location";
673       return false;
674   }
675 }
676
677 common_installer::Step::Status StepParse::process() {
678   if (!LocateConfigFile()) {
679     LOG(ERROR) << "No config.xml";
680     return common_installer::Step::Status::MANIFEST_NOT_FOUND;
681   }
682
683   parser_.reset(new wgt::parse::WidgetConfigParser());
684   if (!parser_->ParseManifest(widget_path_ / kConfigFileName)) {
685     LOG(ERROR) << "[Parse] Parse failed. " <<  parser_->GetErrorMessage();
686     return common_installer::Step::Status::PARSE_ERROR;
687   }
688
689   WgtBackendData* backend_data =
690     static_cast<WgtBackendData*>(context_->backend_data.get());
691
692   if (check_start_file_) {
693     if (!parser_->CheckValidStartFile()) {
694       LOG(ERROR) << parser_->GetErrorMessage();
695       return common_installer::Step::Status::PARSE_ERROR;
696     }
697     if (!parser_->CheckValidServicesStartFiles()) {
698       LOG(ERROR) << parser_->GetErrorMessage();
699       return common_installer::Step::Status::PARSE_ERROR;
700     }
701   } else {
702     // making backup of content data and services content data
703     auto content_info =
704       GetManifestDataForKey<const wgt::parse::ContentInfo>(
705               wgt::application_widget_keys::kTizenContentKey);
706     auto service_list =
707       GetManifestDataForKey<const wgt::parse::ServiceList>(
708               wgt::application_widget_keys::kTizenServiceKey);
709     if (content_info)
710       backend_data->content.set(*content_info);
711     if (service_list)
712       backend_data->service_list.set(*service_list);
713   }
714
715   manifest_x* manifest =
716       static_cast<manifest_x*>(calloc(1, sizeof(manifest_x)));
717   if (!FillManifestX(manifest)) {
718     LOG(ERROR) << "[Parse] Storing manifest_x failed. "
719                <<  parser_->GetErrorMessage();
720     return common_installer::Step::Status::PARSE_ERROR;
721   }
722
723   // Copy data from ManifestData to InstallerContext
724   auto info =
725       GetManifestDataForKey<const wgt::parse::TizenApplicationInfo>(
726               wgt::application_widget_keys::kTizenApplicationKey);
727   auto wgt_info =
728       GetManifestDataForKey<const wgt::parse::WidgetInfo>(
729               wgt::application_widget_keys::kTizenWidgetKey);
730
731   std::string name;
732   const auto& name_set = wgt_info->name_set();
733   if (name_set.find("") != name_set.end())
734     name = name_set.find("")->second;
735   if (name_set.begin() != name_set.end())
736     name = name_set.begin()->second;
737
738   std::string short_name;
739   const auto& short_name_set = wgt_info->short_name_set();
740   if (short_name_set.find("") != short_name_set.end())
741     short_name = short_name_set.find("")->second;
742   if (short_name_set.begin() != short_name_set.end())
743     short_name = short_name_set.begin()->second;
744
745   const std::string& package_version = wgt_info->version();
746   const std::string& required_api_version = info->required_version();
747
748   manifest->api_version = strdup(required_api_version.c_str());
749
750   context_->pkgid.set(manifest->package);
751
752   // write pkgid for recovery file
753   if (context_->recovery_info.get().recovery_file) {
754     context_->recovery_info.get().recovery_file->set_pkgid(manifest->package);
755     context_->recovery_info.get().recovery_file->WriteAndCommitFileContent();
756   }
757
758   auto perm_info =
759       GetManifestDataForKey<const wgt::parse::PermissionsInfo>(
760               wgt::application_widget_keys::kTizenPermissionsKey);
761   parser::PermissionSet permissions;
762   if (perm_info)
763      permissions = perm_info->GetAPIPermissions();
764
765   auto settings_info =
766       GetManifestDataForKey<const wgt::parse::SettingInfo>(
767               wgt::application_widget_keys::kTizenSettingKey);
768   if (settings_info)
769     backend_data->settings.set(*settings_info);
770
771   LOG(DEBUG) << " Read data -[ ";
772   LOG(DEBUG) << "App id: " << info->id();
773   LOG(DEBUG) << "  package     = " <<  info->package();
774   LOG(DEBUG) << "  id          = " <<  info->id();
775   LOG(DEBUG) << "  name        = " <<  name;
776   LOG(DEBUG) << "  short_name  = " <<  short_name;
777   LOG(DEBUG) << "  aplication version     = " <<  package_version;
778   LOG(DEBUG) << "  api_version = " <<  info->required_version();
779   LOG(DEBUG) << "  launch_mode = " <<  info->launch_mode();
780   LOG(DEBUG) << "  privileges -[";
781   for (const auto& p : permissions) {
782     LOG(DEBUG) << "    " << p;
783   }
784   LOG(DEBUG) << "  ]-";
785   LOG(DEBUG) << "]-";
786
787   if (context_->manifest_data.get())
788     pkgmgr_parser_free_manifest_xml(context_->manifest_data.get());
789
790   context_->manifest_data.set(manifest);
791   return common_installer::Step::Status::OK;
792 }
793
794 bool StepParse::Check(const boost::filesystem::path& widget_path) {
795   LOG(DEBUG) << "unpacked widget path: " << widget_path;
796
797   widget_path_ = widget_path;
798
799   boost::filesystem::path config = widget_path / kConfigFileName;
800   LOG(DEBUG) << "config.xml path: " << config;
801   if (!boost::filesystem::exists(config))
802     return false;
803   return true;
804 }
805
806 }  // namespace configuration
807 }  // namespace wgt