Upstream version 10.38.217.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 ail_cb_ret_e callback_x_slp_exe_path(const ail_appinfo_h appinfo,
19     void* user_data, uid_t /*uid*/) {
20   char* package_exec;
21   ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_EXE_PATH, &package_exec);
22   if (!package_exec)
23     return AIL_CB_RET_CONTINUE;
24
25   std::string* x_slp_exe_path = static_cast<std::string*>(user_data);
26   *x_slp_exe_path = package_exec;
27   return AIL_CB_RET_CANCEL;
28 }
29
30 ail_cb_ret_e callback_installed_time(const ail_appinfo_h appinfo,
31     void* user_data, uid_t /*uid*/) {
32   int* installed_time = static_cast<int*>(user_data);
33   ail_appinfo_get_int(appinfo, AIL_PROP_X_SLP_INSTALLEDTIME_INT,
34       installed_time);
35   return AIL_CB_RET_CANCEL;
36 }
37
38 void GetProperty(const std::string& id,
39     const char* type,
40     PropertyCallback callback,
41     void* user_data) {
42   ail_filter_h filter;
43   ail_error_e ret = ail_filter_new(&filter);
44   if (ret != AIL_ERROR_OK) {
45     LOG(ERROR) << "Failed to create AIL filter.";
46     return;
47   }
48
49   ret = ail_filter_add_str(filter, type, id.c_str());
50   if (ret != AIL_ERROR_OK) {
51     LOG(ERROR) << "Failed to init AIL filter.";
52     ail_filter_destroy(filter);
53     return;
54   }
55
56   int count;
57   uid_t uid = getuid();
58   if (uid != GLOBAL_USER)
59     ret = ail_filter_count_usr_appinfo(filter, &count, uid);
60   else
61     ret = ail_filter_count_appinfo(filter, &count);
62   if (ret != AIL_ERROR_OK) {
63     LOG(ERROR) << "Failed to count AIL app info.";
64     ail_filter_destroy(filter);
65     return;
66   }
67
68   if (count != 1) {
69     LOG(ERROR) << "Invalid count (" << count
70                << ") of the AIL DB records for the app id " << id;
71     ail_filter_destroy(filter);
72     return;
73   }
74
75
76   if (uid != GLOBAL_USER)
77     ail_filter_list_usr_appinfo_foreach(filter, callback,
78                                         user_data, uid);
79   else
80     ail_filter_list_appinfo_foreach(filter,
81                                     callback, user_data);
82   ail_filter_destroy(filter);
83 }
84
85 base::FilePath GetPath(const std::string& app_id, const char* type) {
86   std::string x_slp_exe_path;
87   GetProperty(app_id,
88       type,
89       callback_x_slp_exe_path,
90       static_cast<void*>(&x_slp_exe_path));
91
92   if (x_slp_exe_path.empty()) {
93     return base::FilePath();
94   }
95
96   // x_slp_exe_path is <app_path>/bin/<app_id>, we need to
97   // return just <app_path>.
98   return base::FilePath(x_slp_exe_path).DirName().DirName();
99 }
100
101 }  // namespace
102
103 namespace xwalk {
104 namespace application {
105
106 base::FilePath GetApplicationPath(const std::string& app_id) {
107   return GetPath(app_id, AIL_PROP_X_SLP_APPID_STR);
108 }
109
110 base::FilePath GetPackagePath(const std::string& pkg_id) {
111   return GetPath(pkg_id, AIL_PROP_X_SLP_PKGID_STR);
112 }
113
114 base::Time GetApplicationInstallationTime(const std::string& app_id) {
115   int installed_time = 0;  // seconds since epoch
116   GetProperty(app_id,
117       AIL_PROP_X_SLP_APPID_STR,
118       callback_installed_time,
119       static_cast<void*>(&installed_time));
120   return base::Time::FromTimeT(installed_time);
121 }
122
123 }  // namespace application
124 }  // namespace xwalk