21f75bd5b8bd15fbe656310136d89a60623b31c5
[platform/framework/web/crosswalk-tizen.git] / src / common / application_data.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "common/application_data.h"
6
7 #include <package_manager.h>
8 #include <manifest_parser/manifest_parser.h>
9 #include <manifest_parser/manifest_handler.h>
10
11 #include <vector>
12
13 #include "common/logger.h"
14 #include "common/file_utils.h"
15
16 namespace wrt {
17
18 namespace {
19
20 const char* kPathSeparator = "/";
21 const char* kConfigXml = "config.xml";
22
23 static std::string GetPackageIdByAppId(const std::string& appid) {
24   char* pkgid = NULL;
25   package_manager_get_package_id_by_app_id(appid.c_str(), &pkgid);
26
27   std::unique_ptr<char, decltype(std::free)*>
28     pkgid_ptr {pkgid, std::free};
29
30   if (pkgid != NULL) {
31     return std::string(pkgid_ptr.get());
32   } else {
33     return std::string();
34   }
35 }
36
37 static std::string GetPackageRootPath(const std::string& pkgid) {
38   package_info_h pkg_info = NULL;
39   if (package_manager_get_package_info(
40         pkgid.c_str(), &pkg_info) != PACKAGE_MANAGER_ERROR_NONE) {
41     return std::string();
42   }
43
44   char* pkg_root_path = NULL;
45   package_info_get_root_path(pkg_info, &pkg_root_path);
46
47   std::unique_ptr<char, decltype(std::free)*>
48     path_ptr {pkg_root_path, std::free};
49
50   package_info_destroy(pkg_info);
51
52   if (pkg_root_path != NULL) {
53     return std::string(path_ptr.get());
54   } else {
55     return std::string();
56   }
57 }
58
59 }  // namespace
60
61 ApplicationData::ApplicationData(const std::string& appid) : app_id_(appid) {
62   pkg_id_ = GetPackageIdByAppId(appid);
63
64   if (!pkg_id_.empty()) {
65     application_path_ = GetPackageRootPath(pkg_id_) + kPathSeparator +
66                         appid + kPathSeparator;
67   }
68 }
69
70 ApplicationData::~ApplicationData() {}
71
72 std::shared_ptr<const wgt::parse::AppControlInfoList>
73     ApplicationData::app_control_info_list() const {
74   return app_control_info_list_;
75 }
76
77 std::shared_ptr<const wgt::parse::CategoryInfoList>
78     ApplicationData::category_info_list() const {
79   return category_info_list_;
80 }
81
82 std::shared_ptr<const wgt::parse::MetaDataInfo>
83     ApplicationData::meta_data_info() const {
84   return meta_data_info_;
85 }
86
87 std::shared_ptr<const wgt::parse::AllowedNavigationInfo>
88     ApplicationData::allowed_navigation_info() const {
89   return allowed_navigation_info_;
90 }
91
92 std::shared_ptr<const wgt::parse::PermissionsInfo>
93     ApplicationData::permissions_info() const {
94   return permissions_info_;
95 }
96
97 std::shared_ptr<const wgt::parse::SettingInfo>
98     ApplicationData::setting_info() const {
99   return setting_info_;
100 }
101
102 std::shared_ptr<const wgt::parse::SplashScreenInfo>
103     ApplicationData::splash_screen_info() const {
104   return splash_screen_info_;
105 }
106
107 std::shared_ptr<const wgt::parse::TizenApplicationInfo>
108     ApplicationData::tizen_application_info() const {
109   return tizen_application_info_;
110 }
111
112 std::shared_ptr<const wgt::parse::WidgetInfo>
113     ApplicationData::widget_info() const {
114   return widget_info_;
115 }
116
117 std::shared_ptr<const wgt::parse::ContentInfo>
118     ApplicationData::content_info() const {
119   return content_info_;
120 }
121
122
123 bool ApplicationData::LoadManifestData() {
124   std::string config_xml_path(application_path_ + kConfigXml);
125   if (!utils::Exists(config_xml_path)) {
126     LOGGER(ERROR) << "Failed to load manifest data. : No such file '"
127                   << config_xml_path << "'.";
128     return false;
129   }
130
131   enum ManifestHandlerType {
132     APP_CONTROL_HANDLER = 0,
133     CATEGORY_HANDLER,
134     META_DATA_HANDLER,
135     NAVIGATION_HANDLER,
136     PERMISSIONS_HANDLER,
137     SETTING_HANDLER,
138     SPLASH_SCREEN_HANDLER,
139     TIZEN_APPLICATION_HANDLER,
140     WIDGET_HANDLER,
141     CONTENT_HANDLER
142   };
143
144   std::vector<parser::ManifestHandler*> handlers = {
145     new wgt::parse::AppControlHandler,        // APP_CONTROL_HANDLER
146     new wgt::parse::CategoryHandler,          // CATEGORY_HANDLER
147     new wgt::parse::MetaDataHandler,          // META_DATA_HANDLER
148     new wgt::parse::NavigationHandler,        // NAVIGATION_HANDLER
149     new wgt::parse::PermissionsHandler,       // PERMISSIONS_HANDLER
150     new wgt::parse::SettingHandler,           // SETTING_HANDLER
151     new wgt::parse::SplashScreenHandler,      // SPLASH_SCREEN_HANDLER
152     new wgt::parse::TizenApplicationHandler,  // TIZEN_APPLICATION_HANDLER
153     new wgt::parse::WidgetHandler,            // WIDGET_HANDLER
154     new wgt::parse::ContentHandler            // CONTENT_HANDLER
155   };
156
157   std::unique_ptr<parser::ManifestHandlerRegistry> registry;
158   registry.reset(new parser::ManifestHandlerRegistry(handlers));
159
160   parser::ManifestParser manifest_parser(std::move(registry));
161   if (!manifest_parser.ParseManifest(config_xml_path)) {
162     for (auto iter = handlers.begin(); iter != handlers.end(); ++iter) {
163       delete *iter;
164     }
165     LOGGER(ERROR) << "Failed to load manifest data : "
166                   << manifest_parser.GetErrorMessage();
167     return false;
168   }
169
170   app_control_info_list_ =
171     std::static_pointer_cast<const wgt::parse::AppControlInfoList>(
172       manifest_parser.GetManifestData(
173         handlers[ManifestHandlerType::APP_CONTROL_HANDLER]->Key()));
174
175   category_info_list_ =
176     std::static_pointer_cast<const wgt::parse::CategoryInfoList>(
177       manifest_parser.GetManifestData(
178         handlers[ManifestHandlerType::CATEGORY_HANDLER]->Key()));
179
180   meta_data_info_ =
181     std::static_pointer_cast<const wgt::parse::MetaDataInfo>(
182       manifest_parser.GetManifestData(
183         handlers[ManifestHandlerType::META_DATA_HANDLER]->Key()));
184
185   allowed_navigation_info_ =
186     std::static_pointer_cast<const wgt::parse::AllowedNavigationInfo>(
187       manifest_parser.GetManifestData(
188         handlers[ManifestHandlerType::NAVIGATION_HANDLER]->Key()));
189
190   permissions_info_ =
191     std::static_pointer_cast<const wgt::parse::PermissionsInfo>(
192       manifest_parser.GetManifestData(
193         handlers[ManifestHandlerType::PERMISSIONS_HANDLER]->Key()));
194
195   setting_info_ =
196     std::static_pointer_cast<const wgt::parse::SettingInfo>(
197       manifest_parser.GetManifestData(
198         handlers[ManifestHandlerType::SETTING_HANDLER]->Key()));
199
200   splash_screen_info_ =
201     std::static_pointer_cast<const wgt::parse::SplashScreenInfo>(
202       manifest_parser.GetManifestData(
203         handlers[ManifestHandlerType::SPLASH_SCREEN_HANDLER]->Key()));
204
205   tizen_application_info_ =
206     std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
207       manifest_parser.GetManifestData(
208         handlers[ManifestHandlerType::TIZEN_APPLICATION_HANDLER]->Key()));
209
210   widget_info_ =
211     std::static_pointer_cast<const wgt::parse::WidgetInfo>(
212       manifest_parser.GetManifestData(
213         handlers[ManifestHandlerType::WIDGET_HANDLER]->Key()));
214
215   content_info_ =
216     std::static_pointer_cast<const wgt::parse::ContentInfo>(
217       manifest_parser.GetManifestData(
218         handlers[ManifestHandlerType::CONTENT_HANDLER]->Key()));
219
220   for (auto iter = handlers.begin(); iter != handlers.end(); ++iter) {
221     delete *iter;
222   }
223
224   // Set default empty object
225   if (widget_info_.get() == NULL) {
226     widget_info_.reset(new wgt::parse::WidgetInfo);
227   }
228   if (setting_info_.get() == NULL) {
229     setting_info_.reset(new wgt::parse::SettingInfo);
230   }
231
232   return true;
233 }
234
235 }  // namespace wrt