Specifiy that this supports M47
[platform/framework/web/crosswalk-tizen.git] / 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 <wgt_manifest_handlers/application_manifest_constants.h>
21 #include <wgt_manifest_handlers/widget_config_parser.h>
22 #include <app_manager.h>
23 #include <app_common.h>
24
25 #include <vector>
26
27 #include "common/file_utils.h"
28 #include "common/logger.h"
29 #include "common/profiler.h"
30
31 namespace common {
32
33 namespace {
34
35 const char* kPathSeparator = "/";
36 const char* kConfigXml = "config.xml";
37 const char* kWgtPath = "wgt";
38
39 #ifdef IME_FEATURE_SUPPORT
40 const char* kImeCategory = "http://tizen.org/category/ime";
41 #endif  // IME_FEATURE_SUPPORT
42 #ifdef WATCH_FACE_FEATURE_SUPPORT
43 const char* kIdleClockCategory = "com.samsung.wmanager.WATCH_CLOCK";
44 const char* kWearableClockCategory = "http://tizen.org/category/wearable_clock";
45 #endif  // WATCH_FACE_FEATURE_SUPPORT
46
47 }  // namespace
48
49 ApplicationData::ApplicationData(const std::string& appid)
50   : app_id_(appid),
51     loaded_(false) {
52   SCOPE_PROFILE();
53   char* res_path = app_get_resource_path();
54   if (res_path != NULL) {
55     application_path_ = std::string(res_path) + kWgtPath + kPathSeparator;
56     free(res_path);
57   }
58 }
59
60 ApplicationData::~ApplicationData() {}
61
62 std::shared_ptr<const wgt::parse::AppControlInfoList>
63     ApplicationData::app_control_info_list() const {
64   return app_control_info_list_;
65 }
66
67 std::shared_ptr<const wgt::parse::CategoryInfoList>
68     ApplicationData::category_info_list() const {
69   return category_info_list_;
70 }
71
72 std::shared_ptr<const wgt::parse::MetaDataInfo>
73     ApplicationData::meta_data_info() const {
74   return meta_data_info_;
75 }
76
77 std::shared_ptr<const wgt::parse::AllowedNavigationInfo>
78     ApplicationData::allowed_navigation_info() const {
79   return allowed_navigation_info_;
80 }
81
82 std::shared_ptr<const wgt::parse::PermissionsInfo>
83     ApplicationData::permissions_info() const {
84   return permissions_info_;
85 }
86
87 std::shared_ptr<const wgt::parse::SettingInfo>
88     ApplicationData::setting_info() const {
89   return setting_info_;
90 }
91
92 std::shared_ptr<const wgt::parse::LaunchScreenInfo>
93     ApplicationData::splash_screen_info() const {
94   return splash_screen_info_;
95 }
96
97 std::shared_ptr<const wgt::parse::TizenApplicationInfo>
98     ApplicationData::tizen_application_info() const {
99   return tizen_application_info_;
100 }
101
102 std::shared_ptr<const wgt::parse::WidgetInfo>
103     ApplicationData::widget_info() const {
104   return widget_info_;
105 }
106
107 std::shared_ptr<const wgt::parse::ContentInfo>
108     ApplicationData::content_info() const {
109   return content_info_;
110 }
111
112 std::shared_ptr<const wgt::parse::WarpInfo>
113     ApplicationData::warp_info() const {
114   return warp_info_;
115 }
116
117 std::shared_ptr<const wgt::parse::CSPInfo>
118     ApplicationData::csp_info() const {
119   return csp_info_;
120 }
121
122 std::shared_ptr<const wgt::parse::CSPInfo>
123     ApplicationData::csp_report_info() const {
124   return csp_report_info_;
125 }
126
127 const std::string ApplicationData::pkg_id() const {
128   if (pkg_id_.empty()) {
129     app_info_h app_info;
130     int ret = app_info_create(app_id_.c_str(), &app_info);
131     if (ret == APP_MANAGER_ERROR_NONE) {
132       char* pkg = NULL;
133       ret = app_info_get_package(app_info, &pkg);
134       if (ret == APP_MANAGER_ERROR_NONE && pkg != NULL) {
135         pkg_id_ = pkg;
136         free(pkg);
137       }
138       app_info_destroy(app_info);
139     }
140   }
141   return pkg_id_;
142 }
143
144 ApplicationData::AppType ApplicationData::GetAppType() {
145   if (category_info_list_) {
146     auto category_list = category_info_list_->categories;
147     auto it = category_list.begin();
148     auto end = category_list.end();
149     for (; it != end; ++it) {
150 #ifdef IME_FEATURE_SUPPORT
151       if (*it == kImeCategory) {
152         return IME;
153       }
154 #endif  // IME_FEATURE_SUPPORT
155 #ifdef WATCH_FACE_FEATURE_SUPPORT
156       if (*it == kIdleClockCategory || *it == kWearableClockCategory) {
157         return WATCH;
158       }
159 #endif  // WATCH_FACE_FEATURE_SUPPORT
160     }
161   }
162   return UI;
163 }
164
165 bool ApplicationData::LoadManifestData() {
166   if (loaded_) {
167     return true;
168   }
169
170   SCOPE_PROFILE();
171
172   std::string config_xml_path(application_path_ + kConfigXml);
173   if (!utils::Exists(config_xml_path)) {
174     LOGGER(ERROR) << "Failed to load manifest data : No such file '"
175                   << config_xml_path << "'.";
176     return false;
177   }
178
179   std::unique_ptr<wgt::parse::WidgetConfigParser> widget_config_parser;
180   widget_config_parser.reset(new wgt::parse::WidgetConfigParser());
181   if (!widget_config_parser->ParseManifest(config_xml_path)) {
182     LOGGER(ERROR) << "Failed to load widget config parser data: "
183                   << widget_config_parser->GetErrorMessage();
184     return false;
185   }
186
187   app_control_info_list_ =
188     std::static_pointer_cast<const wgt::parse::AppControlInfoList>(
189       widget_config_parser->GetManifestData(
190           wgt::parse::AppControlInfo::Key()));
191
192   category_info_list_ =
193     std::static_pointer_cast<const wgt::parse::CategoryInfoList>(
194       widget_config_parser->GetManifestData(
195         wgt::parse::CategoryInfoList::Key()));
196
197   meta_data_info_ =
198     std::static_pointer_cast<const wgt::parse::MetaDataInfo>(
199       widget_config_parser->GetManifestData(
200           wgt::parse::MetaDataInfo::Key()));
201
202   allowed_navigation_info_ =
203     std::static_pointer_cast<const wgt::parse::AllowedNavigationInfo>(
204       widget_config_parser->GetManifestData(
205           wgt::parse::AllowedNavigationInfo::Key()));
206
207   permissions_info_ =
208     std::static_pointer_cast<const wgt::parse::PermissionsInfo>(
209       widget_config_parser->GetManifestData(
210         wgt::parse::PermissionsInfo::Key()));
211
212   setting_info_ =
213     std::static_pointer_cast<const wgt::parse::SettingInfo>(
214       widget_config_parser->GetManifestData(
215         wgt::parse::SettingInfo::Key()));
216
217   splash_screen_info_ =
218     std::static_pointer_cast<const wgt::parse::LaunchScreenInfo>(
219       widget_config_parser->GetManifestData(
220         wgt::parse::LaunchScreenInfo::Key()));
221
222   tizen_application_info_ =
223     std::static_pointer_cast<const wgt::parse::TizenApplicationInfo>(
224       widget_config_parser->GetManifestData(
225           wgt::parse::TizenApplicationInfo::Key()));
226
227   widget_info_ =
228     std::static_pointer_cast<const wgt::parse::WidgetInfo>(
229       widget_config_parser->GetManifestData(
230           wgt::parse::WidgetInfo::Key()));
231
232   content_info_ =
233     std::static_pointer_cast<const wgt::parse::ContentInfo>(
234       widget_config_parser->GetManifestData(
235         wgt::parse::ContentInfo::Key()));
236
237   warp_info_ =
238     std::static_pointer_cast<const wgt::parse::WarpInfo>(
239       widget_config_parser->GetManifestData(
240           wgt::parse::WarpInfo::Key()));
241
242   csp_info_ =
243     std::static_pointer_cast<const wgt::parse::CSPInfo>(
244       widget_config_parser->GetManifestData(
245           wgt::parse::CSPInfo::Key()));
246
247   csp_report_info_ =
248     std::static_pointer_cast<const wgt::parse::CSPInfo>(
249       widget_config_parser->GetManifestData(
250           wgt::parse::CSPInfo::Report_only_key()));
251
252   // Set default empty object
253   if (widget_info_.get() == NULL) {
254     widget_info_.reset(new wgt::parse::WidgetInfo);
255   }
256   if (setting_info_.get() == NULL) {
257     setting_info_.reset(new wgt::parse::SettingInfo);
258   }
259
260   app_type_ = GetAppType();
261   loaded_ = true;
262
263   return true;
264 }
265
266 // static
267 ApplicationDataManager* ApplicationDataManager::GetInstance() {
268   static ApplicationDataManager self;
269   return &self;
270 }
271
272 ApplicationDataManager::ApplicationDataManager() {
273 }
274
275 ApplicationDataManager::~ApplicationDataManager() {
276 }
277
278 ApplicationData* ApplicationDataManager::GetApplicationData(
279     const std::string& appid) {
280   auto it = cache_.find(appid);
281   if (it == cache_.end()) {
282     cache_[appid].reset(new ApplicationData(appid));
283   }
284   return cache_[appid].get();
285 }
286
287 }  // namespace common