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.
6 #include "wgt/step/configuration/step_parse.h"
8 #include <boost/filesystem/path.hpp>
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/metadata_handler.h>
24 #include <wgt_manifest_handlers/service_handler.h>
25 #include <wgt_manifest_handlers/setting_handler.h>
26 #include <wgt_manifest_handlers/tizen_application_handler.h>
27 #include <wgt_manifest_handlers/widget_handler.h>
28 #include <wgt_manifest_handlers/ime_handler.h>
30 #include <pkgmgr/pkgmgr_parser.h>
42 #include "wgt/wgt_backend_data.h"
44 namespace bf = boost::filesystem;
48 const char kCategoryWearableClock[] =
49 "http://tizen.org/category/wearable_clock";
50 const char kCategoryWatchClock[] = "com.samsung.wmanager.WATCH_CLOCK";
52 const std::string kManifestVersion = "1.0.0";
53 const char kTizenPackageXmlNamespace[] = "http://tizen.org/ns/packages";
54 const char kImeCategoryName[] = "http://tizen.org/category/ime";
56 const char kResWgt[] = "res/wgt";
58 GList* GenerateMetadataListX(const wgt::parse::MetaDataInfo& meta_info) {
59 GList* list = nullptr;
60 for (auto& meta : meta_info.metadata()) {
61 metadata_x* new_meta =
62 static_cast<metadata_x*>(calloc(1, sizeof(metadata_x)));
63 new_meta->key = strdup(meta.first.c_str());
64 if (!meta.second.empty())
65 new_meta->value = strdup(meta.second.c_str());
66 list = g_list_append(list, new_meta);
71 void SetApplicationXDefaults(application_x* application) {
72 application->effectimage_type = strdup("image");
73 application->enabled = strdup("true");
74 application->guestmode_visibility = strdup("true");
75 application->hwacceleration = strdup("default");
76 application->indicatordisplay = strdup("true");
77 application->launchcondition = strdup("false");
78 application->permission_type = strdup("normal");
79 application->process_pool = strdup("false");
80 application->recentimage = strdup("false");
81 application->screenreader = strdup("use-system-setting");
82 application->submode = strdup("false");
83 application->support_disable = strdup("false");
84 application->ui_gadget = strdup("false");
85 application->multiple = strdup("false");
89 void AppendLabel(T* root, const std::string& label,
90 const std::string& locale) {
91 label_x* label_item = reinterpret_cast<label_x*>(calloc(1, sizeof(label_x)));
92 label_item->name = strdup(label.c_str());
93 label_item->text = strdup(label.c_str());
94 label_item->lang = !locale.empty() ?
95 strdup(locale.c_str()) : strdup(DEFAULT_LOCALE);
96 root->label = g_list_append(root->label, label_item);
101 namespace configuration {
103 namespace app_keys = wgt::application_widget_keys;
104 namespace sc = std::chrono;
106 StepParse::StepParse(common_installer::InstallerContext* context,
107 ConfigLocation config_location,
108 bool check_start_file)
110 config_location_(config_location),
111 check_start_file_(check_start_file) {
114 std::set<std::string> StepParse::ExtractPrivileges(
115 std::shared_ptr<const wgt::parse::PermissionsInfo> perm_info) const {
116 return perm_info->GetAPIPermissions();
119 std::string StepParse::GetPackageVersion(
120 const std::string& manifest_version) {
121 if (manifest_version.empty()) {
122 return kManifestVersion;
124 std::string version = manifest_version.substr(0,
125 manifest_version.find_first_not_of("1234567890."));
127 utils::VersionNumber version_number(version);
129 if (!version_number.IsValidTizenPackageVersion()) {
130 LOG(WARNING) << "Version number: " << manifest_version
131 << " is not valid version number for tizen package. "
132 << "Default value will be used.";
133 return kManifestVersion;
136 return version_number.ToString();
139 bool StepParse::FillInstallationInfo(manifest_x* manifest) {
140 manifest->root_path = strdup(
141 (context_->root_application_path.get() / manifest->package).c_str());
142 manifest->installed_time =
143 strdup(std::to_string(sc::system_clock::to_time_t(
144 sc::system_clock::now())).c_str());
148 bool StepParse::FillIconPaths(manifest_x* manifest) {
150 GetManifestDataForKey<const wgt::parse::TizenApplicationInfo>(
151 app_keys::kTizenApplicationKey);
153 LOG(ERROR) << "Application info manifest data has not been found.";
157 GetManifestDataForKey<const wgt::parse::ApplicationIconsInfo>(
158 app_keys::kIconsKey);
159 if (icons_info.get()) {
160 for (auto& application_icon : icons_info->icons()) {
161 icon_x* icon = reinterpret_cast<icon_x*> (calloc(1, sizeof(icon_x)));
162 bf::path icon_path = context_->root_application_path.get()
163 / app_info->package() / "res" / "wgt" / application_icon.path();
164 icon->text = strdup(icon_path.c_str());
165 icon->lang = strdup(DEFAULT_LOCALE);
166 manifest->icon = g_list_append(manifest->icon, icon);
172 bool StepParse::FillWidgetInfo(manifest_x* manifest) {
174 GetManifestDataForKey<const wgt::parse::WidgetInfo>(
175 app_keys::kWidgetKey);
177 if (!wgt_info.get()) {
178 LOG(ERROR) << "Widget info manifest data has not been found.";
182 const std::string& version = wgt_info->version();
184 manifest->ns = strdup(kTizenPackageXmlNamespace);
185 manifest->version = strdup(GetPackageVersion(version).c_str());
187 for (auto& item : wgt_info->description_set()) {
188 description_x* description = reinterpret_cast<description_x*>
189 (calloc(1, sizeof(description_x)));
190 description->text = strdup(item.second.c_str());
191 description->lang = !item.first.empty() ?
192 strdup(item.first.c_str()) : strdup(DEFAULT_LOCALE);
193 manifest->description = g_list_append(manifest->description, description);
196 for (auto& item : wgt_info->name_set()) {
197 AppendLabel(manifest, item.second, item.first);
200 manifest->type = strdup("wgt");
201 manifest->appsetting = strdup("false");
202 manifest->nodisplay_setting = strdup("false");
203 manifest->preload = strdup("false");
204 manifest->installed_storage = strdup("installed_internal");
206 // For wgt package use the long name
208 reinterpret_cast<application_x*>(manifest->application->data);
209 for (auto& item : wgt_info->name_set()) {
210 AppendLabel(app, item.second, item.first);
213 author_x* author = reinterpret_cast<author_x*>(calloc(1, sizeof(author_x)));
214 if (!wgt_info->author().empty())
215 author->text = strdup(wgt_info->author().c_str());
216 if (!wgt_info->author_email().empty())
217 author->email = strdup(wgt_info->author_email().c_str());
218 if (!wgt_info->author_href().empty())
219 author->href = strdup(wgt_info->author_href().c_str());
220 author->lang = strdup(DEFAULT_LOCALE);
221 manifest->author = g_list_append(manifest->author, author);
224 GetManifestDataForKey<const wgt::parse::SettingInfo>(
225 wgt::application_widget_keys::kTizenSettingKey);
227 switch (settings_info->install_location()) {
228 case wgt::parse::SettingInfo::InstallLocation::AUTO: {
229 manifest->installlocation = strdup("auto");
232 case wgt::parse::SettingInfo::InstallLocation::INTERNAL: {
233 manifest->installlocation = strdup("internal-only");
236 case wgt::parse::SettingInfo::InstallLocation::EXTERNAL: {
237 manifest->installlocation = strdup("prefer-external");
242 manifest->installlocation = strdup("auto");
248 bool StepParse::FillMainApplicationInfo(manifest_x* manifest) {
250 GetManifestDataForKey<const wgt::parse::TizenApplicationInfo>(
251 app_keys::kTizenApplicationKey);
253 LOG(ERROR) << "Application info manifest data has not been found.";
256 bool has_watch_catergory = false;
257 bool has_ime = false;
259 GetManifestDataForKey<const wgt::parse::CategoryInfoList>(
260 app_keys::kTizenCategoryKey);
263 has_watch_catergory = std::find_if(category_info->categories.begin(),
264 category_info->categories.end(),
265 [](const std::string& category) {
266 return category == kCategoryWearableClock ||
267 category == kCategoryWatchClock;
268 }) != category_info->categories.end();
269 has_ime = std::find(category_info->categories.begin(),
270 category_info->categories.end(),
272 != category_info->categories.end();
276 application_x* application = reinterpret_cast<application_x*>(
277 calloc(1, sizeof(application_x)));
278 application->component_type =
279 has_watch_catergory ? strdup("watchapp") : strdup("uiapp");
280 application->mainapp = strdup("true");
281 application->appid = strdup(app_info->id().c_str());
282 application->nodisplay = has_ime ? strdup("true") : strdup("false");
283 application->taskmanage = has_ime ? strdup("false") : strdup("true");
284 SetApplicationXDefaults(application);
285 if (has_watch_catergory)
286 application->ambient_support =
287 strdup(app_info->ambient_support() ? "true" : "false");
289 application->ambient_support = strdup("false");
290 application->package = strdup(app_info->package().c_str());
293 strdup((context_->root_application_path.get() / app_info->package()
294 / "bin" / application->appid).c_str());
295 application->type = strdup("webapp");
296 application->onboot = strdup("false");
297 application->autorestart = strdup("false");
299 application->launch_mode = strdup(app_info->launch_mode().c_str());
300 if (manifest->icon) {
301 icon_x* icon = reinterpret_cast<icon_x*>(manifest->icon->data);
302 icon_x* app_icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
303 app_icon->text = strdup(icon->text);
304 app_icon->lang = strdup(icon->lang);
305 application->icon = g_list_append(application->icon, app_icon);
307 // guarantees that the main app will be at the begining of the list
308 manifest->application = g_list_insert(manifest->application, application, 0);
310 manifest->package = strdup(app_info->package().c_str());
311 manifest->mainapp_id = strdup(app_info->id().c_str());
315 bool StepParse::FillServiceApplicationInfo(manifest_x* manifest) {
317 GetManifestDataForKey<const wgt::parse::ServiceList>(
318 app_keys::kTizenServiceKey);
321 for (auto& service_info : service_list->services) {
322 application_x* application = reinterpret_cast<application_x*>
323 (calloc(1, sizeof(application_x)));
324 application->component_type = strdup("svcapp");
325 application->mainapp = strdup("false");
326 application->appid = strdup(service_info.id().c_str());
328 strdup((context_->root_application_path.get() / manifest->package
329 / "bin" / application->appid).c_str());
330 application->type = strdup("webapp");
331 application->onboot =
332 service_info.on_boot() ? strdup("true") : strdup("false");
333 application->autorestart =
334 service_info.auto_restart() ? strdup("true") : strdup("false");
335 application->nodisplay = strdup("false");
336 application->taskmanage = strdup("true");
337 SetApplicationXDefaults(application);
338 application->ambient_support = strdup("false");
339 application->package = strdup(manifest->package);
341 for (auto& pair : service_info.names()) {
342 AppendLabel(application, pair.second, pair.first);
345 if (!service_info.icon().empty()) {
346 bf::path icon_path = context_->root_application_path.get()
347 / manifest->package / "res" / "wgt" / service_info.icon();
348 icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
349 icon->text = strdup(icon_path.c_str());
350 icon->lang = strdup(DEFAULT_LOCALE);
351 application->icon = g_list_append(application->icon, icon);
354 // TODO(t.iwanek): what about description, how is it different from name?
356 for (auto& category : service_info.categories()) {
357 application->category = g_list_append(application->category,
358 strdup(category.c_str()));
361 for (auto& pair : service_info.metadata_set()) {
362 metadata_x* item = reinterpret_cast<metadata_x*>(
363 calloc(1, sizeof(metadata_x)));
364 item->key = strdup(pair.first.c_str());
365 if (!pair.second.empty())
366 item->value = strdup(pair.second.c_str());
367 application->metadata = g_list_append(application->metadata, item);
370 manifest->application = g_list_append(manifest->application, application);
375 bool StepParse::FillWidgetApplicationInfo(manifest_x* manifest) {
376 auto appwidget_info =
377 GetManifestDataForKey<const wgt::parse::AppWidgetInfo>(
378 wgt::application_widget_keys::kTizenAppWidgetFullKey);
381 for (auto& app_widget : appwidget_info->app_widgets()) {
382 application_x* application = reinterpret_cast<application_x*>
383 (calloc(1, sizeof(application_x)));
384 application->component_type = strdup("widgetapp");
385 application->mainapp = strdup("false");
386 application->appid = strdup(app_widget.id.c_str());
388 strdup((context_->root_application_path.get() / manifest->package
389 / "bin" / application->appid).c_str());
390 application->type = strdup("webapp");
391 application->nodisplay = strdup("true");
392 application->taskmanage = strdup("false");
393 SetApplicationXDefaults(application);
394 application->ambient_support = strdup("false");
395 application->package = strdup(manifest->package);
397 if (!app_widget.label.default_value.empty()) {
398 AppendLabel(application, app_widget.label.default_value, std::string());
401 for (auto& pair : app_widget.label.lang_value_map) {
402 AppendLabel(application, pair.second, pair.first);
405 if (!app_widget.icon_src.empty()) {
406 icon_x* icon = reinterpret_cast<icon_x*>(calloc(1, sizeof(icon_x)));
407 icon->text = strdup(app_widget.icon_src.c_str());
408 icon->lang = strdup(DEFAULT_LOCALE);
409 application->icon = g_list_append(application->icon, icon);
412 manifest->application = g_list_append(manifest->application, application);
418 bool StepParse::FillBackgroundCategoryInfo(manifest_x* manifest) {
419 auto manifest_data = parser_->GetManifestData(
420 app_keys::kTizenBackgroundCategoryKey);
421 std::shared_ptr<const wgt::parse::BackgroundCategoryInfoList> bc_list =
422 std::static_pointer_cast<const wgt::parse::BackgroundCategoryInfoList>(
429 reinterpret_cast<application_x*>(manifest->application->data);
431 for (auto& background_category : bc_list->background_categories) {
432 app->background_category = g_list_append(
433 app->background_category, strdup(background_category.value().c_str()));
439 bool StepParse::FillAppControl(manifest_x* manifest) {
441 GetManifestDataForKey<const wgt::parse::AppControlInfoList>(
442 app_keys::kTizenApplicationAppControlsKey);
445 reinterpret_cast<application_x*>(manifest->application->data);
447 for (const auto& control : app_info_list->controls) {
448 appcontrol_x* app_control =
449 static_cast<appcontrol_x*>(calloc(1, sizeof(appcontrol_x)));
450 app_control->operation = strdup(control.operation().c_str());
451 app_control->mime = strdup(control.mime().c_str());
452 app_control->uri = strdup(control.uri().c_str());
453 app->appcontrol = g_list_append(app->appcontrol, app_control);
459 bool StepParse::FillPrivileges(manifest_x* manifest) {
461 GetManifestDataForKey<const wgt::parse::PermissionsInfo>(
462 app_keys::kTizenPermissionsKey);
463 std::set<std::string> privileges;
465 privileges = ExtractPrivileges(perm_info);
467 for (auto& priv : privileges) {
468 manifest->privileges =
469 g_list_append(manifest->privileges, strdup(priv.c_str()));
474 bool StepParse::FillCategories(manifest_x* manifest) {
476 GetManifestDataForKey<const wgt::parse::CategoryInfoList>(
477 app_keys::kTizenCategoryKey);
482 reinterpret_cast<application_x*>(manifest->application->data);
483 // there is one app atm
484 for (auto& category : category_info->categories) {
485 app->category = g_list_append(app->category, strdup(category.c_str()));
490 bool StepParse::FillMetadata(manifest_x* manifest) {
492 GetManifestDataForKey<const wgt::parse::MetaDataInfo>(
493 app_keys::kTizenMetaDataKey);
497 for (application_x* app : GListRange<application_x*>(manifest->application)) {
498 app->metadata = GenerateMetadataListX(*meta_info);
503 bool StepParse::FillAppWidget() {
504 // This is needed to store preview icons which are not saved into manifest_x
505 WgtBackendData* backend_data =
506 static_cast<WgtBackendData*>(context_->backend_data.get());
508 auto appwidget_info =
509 GetManifestDataForKey<const wgt::parse::AppWidgetInfo>(
510 wgt::application_widget_keys::kTizenAppWidgetFullKey);
512 backend_data->appwidgets.set(*appwidget_info);
516 bool StepParse::FillAccounts(manifest_x* manifest) {
518 GetManifestDataForKey<const wgt::parse::AccountInfo>(
519 app_keys::kAccountKey);
522 common_installer::AccountInfo info;
523 for (auto& account : account_info->accounts()) {
524 common_installer::SingleAccountInfo single_info;
525 single_info.capabilities = account.capabilities;
526 single_info.icon_paths = account.icon_paths;
527 single_info.multiple_account_support = account.multiple_account_support;
528 single_info.names = account.names;
529 // wgt can contain only one app so this assumes mainapp_id is valid here
530 single_info.appid = manifest->mainapp_id;
531 info.set_account(single_info);
533 context_->manifest_plugins_data.get().account_info.set(info);
537 bool StepParse::FillImeInfo() {
539 GetManifestDataForKey<const wgt::parse::ImeInfo>(
540 app_keys::kTizenImeKey);
544 common_installer::ImeInfo info;
545 info.setUuid(ime_info->uuid());
547 const auto &languages = ime_info->languages();
548 for (const auto &language : languages)
549 info.AddLanguage(language);
551 context_->manifest_plugins_data.get().ime_info.set(std::move(info));
555 bool StepParse::FillExtraManifestInfo(manifest_x* manifest) {
556 return FillAccounts(manifest) && FillImeInfo() && FillAppWidget();
559 bool StepParse::FillManifestX(manifest_x* manifest) {
560 if (!FillIconPaths(manifest))
562 if (!FillMainApplicationInfo(manifest))
564 if (!FillWidgetInfo(manifest))
566 if (!FillInstallationInfo(manifest))
568 if (!FillPrivileges(manifest))
570 if (!FillAppControl(manifest))
572 if (!FillCategories(manifest))
574 if (!FillMetadata(manifest))
576 // TODO(t.iwanek): fix adding ui application element
577 // for now adding application service is added here because rest of code
578 // assumes that there is one application at manifest->application
579 // so this must execute last. Don't move it above any item
580 if (!FillServiceApplicationInfo(manifest))
582 if (!FillWidgetApplicationInfo(manifest))
584 if (!FillBackgroundCategoryInfo(manifest))
586 if (!FillExtraManifestInfo(manifest))
591 bool StepParse::LocateConfigFile() {
592 switch (config_location_) {
593 case ConfigLocation::PACKAGE:
594 return StepParse::Check(context_->unpacked_dir_path.get());
595 case ConfigLocation::INSTALLED:
596 return StepParse::Check(context_->pkg_path.get() / kResWgt);
597 case ConfigLocation::RECOVERY:
598 if (StepParse::Check(common_installer::GetBackupPathForPackagePath(
599 context_->root_application_path.get()
600 / context_->pkgid.get()) / kResWgt))
602 if (StepParse::Check(
603 context_->root_application_path.get()
604 / context_->pkgid.get() / kResWgt))
607 case ConfigLocation::RESOURCE_WGT:
608 return StepParse::Check(context_->unpacked_dir_path.get() / kResWgt);
610 LOG(ERROR) << "Unknown config location";
615 common_installer::Step::Status StepParse::process() {
616 if (!LocateConfigFile()) {
617 LOG(ERROR) << "No config.xml";
618 return common_installer::Step::Status::MANIFEST_NOT_FOUND;
621 parser_.reset(new wgt::parse::WidgetConfigParser());
622 if (!parser_->ParseManifest(config_)) {
623 LOG(ERROR) << "[Parse] Parse failed. " << parser_->GetErrorMessage();
624 return common_installer::Step::Status::PARSE_ERROR;
626 if (check_start_file_) {
627 if (!parser_->HasValidStartFile()) {
628 LOG(ERROR) << parser_->GetErrorMessage();
629 return common_installer::Step::Status::PARSE_ERROR;
631 if (!parser_->HasValidServicesStartFiles()) {
632 LOG(ERROR) << parser_->GetErrorMessage();
633 return common_installer::Step::Status::PARSE_ERROR;
637 manifest_x* manifest =
638 static_cast<manifest_x*>(calloc(1, sizeof(manifest_x)));
639 if (!FillManifestX(manifest)) {
640 LOG(ERROR) << "[Parse] Storing manifest_x failed. "
641 << parser_->GetErrorMessage();
642 return common_installer::Step::Status::PARSE_ERROR;
645 // Copy data from ManifestData to InstallerContext
647 GetManifestDataForKey<const wgt::parse::TizenApplicationInfo>(
648 wgt::application_widget_keys::kTizenApplicationKey);
650 GetManifestDataForKey<const wgt::parse::WidgetInfo>(
651 wgt::application_widget_keys::kTizenWidgetKey);
654 const auto& name_set = wgt_info->name_set();
655 if (name_set.find("") != name_set.end())
656 name = name_set.find("")->second;
657 if (name_set.begin() != name_set.end())
658 name = name_set.begin()->second;
660 std::string short_name;
661 const auto& short_name_set = wgt_info->short_name_set();
662 if (short_name_set.find("") != short_name_set.end())
663 short_name = short_name_set.find("")->second;
664 if (short_name_set.begin() != short_name_set.end())
665 short_name = short_name_set.begin()->second;
667 const std::string& package_version = wgt_info->version();
668 const std::string& required_api_version = info->required_version();
670 manifest->api_version = strdup(required_api_version.c_str());
672 context_->pkgid.set(manifest->package);
674 // write pkgid for recovery file
675 if (context_->recovery_info.get().recovery_file) {
676 context_->recovery_info.get().recovery_file->set_pkgid(manifest->package);
677 context_->recovery_info.get().recovery_file->WriteAndCommitFileContent();
681 GetManifestDataForKey<const wgt::parse::PermissionsInfo>(
682 wgt::application_widget_keys::kTizenPermissionsKey);
683 parser::PermissionSet permissions;
685 permissions = perm_info->GetAPIPermissions();
687 WgtBackendData* backend_data =
688 static_cast<WgtBackendData*>(context_->backend_data.get());
691 GetManifestDataForKey<const wgt::parse::SettingInfo>(
692 wgt::application_widget_keys::kTizenSettingKey);
694 backend_data->settings.set(*settings_info);
696 LOG(DEBUG) << " Read data -[ ";
697 LOG(DEBUG) << "App id: " << info->id();
698 LOG(DEBUG) << " package = " << info->package();
699 LOG(DEBUG) << " id = " << info->id();
700 LOG(DEBUG) << " name = " << name;
701 LOG(DEBUG) << " short_name = " << short_name;
702 LOG(DEBUG) << " aplication version = " << package_version;
703 LOG(DEBUG) << " api_version = " << info->required_version();
704 LOG(DEBUG) << " launch_mode = " << info->launch_mode();
705 LOG(DEBUG) << " privileges -[";
706 for (const auto& p : permissions) {
707 LOG(DEBUG) << " " << p;
712 // TODO(t.iwanek): In delta mode this step is running two times
713 if (context_->manifest_data.get())
714 pkgmgr_parser_free_manifest_xml(context_->manifest_data.get());
716 context_->manifest_data.set(manifest);
717 return common_installer::Step::Status::OK;
720 bool StepParse::Check(const boost::filesystem::path& widget_path) {
721 boost::filesystem::path config = widget_path / "config.xml";
723 LOG(DEBUG) << "config.xml path: " << config;
725 if (!boost::filesystem::exists(config))
732 } // namespace configuration