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