Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / tizen / package_query.cc
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include <ail.h>
7 #include <pkgmgr-info.h>
8
9 #include <cstdlib>
10
11 #include "base/logging.h"
12 #include "xwalk/application/common/tizen/package_query.h"
13
14 namespace {
15
16 typedef ail_cb_ret_e (*PropertyCallback)(const ail_appinfo_h, void*, uid_t);
17
18 char kFieldInstalledTime[] = AIL_PROP_X_SLP_INSTALLEDTIME_INT;
19 char kFieldPackageType[] = AIL_PROP_X_SLP_PACKAGETYPE_STR;
20 char kFieldExePath[] = AIL_PROP_X_SLP_EXE_PATH;
21
22 template<const char* field> struct CallbackForStr {
23   static ail_cb_ret_e callback(const ail_appinfo_h appinfo,
24       void* user_data, uid_t /*uid*/) {
25     char* str_name;
26     ail_appinfo_get_str(appinfo, field, &str_name);
27     if (!str_name)
28       return AIL_CB_RET_CONTINUE;
29
30     std::string* data = static_cast<std::string*>(user_data);
31     *data = str_name;
32     return AIL_CB_RET_CANCEL;
33   }
34 };
35
36 template<const char* field> struct CallbackForInt {
37   static ail_cb_ret_e callback(const ail_appinfo_h appinfo,
38       void* user_data, uid_t /*uid*/) {
39     int* data = static_cast<int*>(user_data);
40     ail_appinfo_get_int(appinfo, field, data);
41     return AIL_CB_RET_CANCEL;
42   }
43 };
44
45 void GetProperty(const std::string& id,
46     const char* type,
47     PropertyCallback callback,
48     void* user_data) {
49   ail_filter_h filter;
50   ail_error_e ret = ail_filter_new(&filter);
51   if (ret != AIL_ERROR_OK) {
52     LOG(ERROR) << "Failed to create AIL filter.";
53     return;
54   }
55
56   ret = ail_filter_add_str(filter, type, id.c_str());
57   if (ret != AIL_ERROR_OK) {
58     LOG(ERROR) << "Failed to init AIL filter.";
59     ail_filter_destroy(filter);
60     return;
61   }
62
63   int count;
64   uid_t uid = getuid();
65   if (uid != GLOBAL_USER)
66     ret = ail_filter_count_usr_appinfo(filter, &count, uid);
67   else
68     ret = ail_filter_count_appinfo(filter, &count);
69   if (ret != AIL_ERROR_OK) {
70     LOG(ERROR) << "Failed to count AIL app info.";
71     ail_filter_destroy(filter);
72     return;
73   }
74
75   if (count != 1) {
76     LOG(ERROR) << "Invalid count (" << count
77                << ") of the AIL DB records for the app id " << id;
78     ail_filter_destroy(filter);
79     return;
80   }
81
82   if (uid != GLOBAL_USER)
83     ail_filter_list_usr_appinfo_foreach(filter, callback,
84                                         user_data, uid);
85   else
86     ail_filter_list_appinfo_foreach(filter,
87                                     callback, user_data);
88   ail_filter_destroy(filter);
89 }
90
91 base::FilePath GetPath(const std::string& app_id, const char* type) {
92   std::string x_slp_exe_path;
93   GetProperty(app_id,
94       type,
95       CallbackForStr<kFieldExePath>::callback,
96       &x_slp_exe_path);
97
98   if (x_slp_exe_path.empty()) {
99     return base::FilePath();
100   }
101
102   // x_slp_exe_path is <app_path>/bin/<app_id>, we need to
103   // return just <app_path>.
104   return base::FilePath(x_slp_exe_path).DirName().DirName();
105 }
106
107 }  // namespace
108
109 namespace xwalk {
110 namespace application {
111
112 base::FilePath GetApplicationPath(const std::string& app_id) {
113   return GetPath(app_id, AIL_PROP_X_SLP_APPID_STR);
114 }
115
116 base::FilePath GetPackagePath(const std::string& pkg_id) {
117   return GetPath(pkg_id, AIL_PROP_X_SLP_PKGID_STR);
118 }
119
120 std::string GetPackageType(const std::string& pkg_id) {
121   std::string type;
122   GetProperty(pkg_id,
123       AIL_PROP_X_SLP_PKGID_STR,
124       CallbackForStr<kFieldPackageType>::callback,
125       &type);
126   return type;
127 }
128
129 base::Time GetApplicationInstallationTime(const std::string& app_id) {
130   int installed_time = 0;  // seconds since epoch
131   GetProperty(app_id,
132       AIL_PROP_X_SLP_APPID_STR,
133       CallbackForInt<kFieldInstalledTime>::callback,
134       &installed_time);
135   return base::Time::FromTimeT(installed_time);
136 }
137
138 }  // namespace application
139 }  // namespace xwalk