2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 #include <klay/exception.h>
17 #include <klay/audit/logger.h>
23 int PackageEventCallback(uid_t uid, int id, const char* type, const char* name,
24 const char* key, const char* val, const void* msg, void* data)
29 int AppListCallback(pkgmgrinfo_appinfo_h handle, void *data)
31 std::vector<ApplicationInfo>* appList = static_cast<std::vector<ApplicationInfo>*>(data);
32 appList->push_back(ApplicationInfo(handle));
37 int PackageListCallback(pkgmgrinfo_pkginfo_h handle, void *data)
39 char* pkgid = nullptr;
40 ::pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
42 std::vector<std::string>* packageList = static_cast<std::vector<std::string>*>(data);
43 packageList->push_back(pkgid);
50 PackageInfo::PackageInfo(const std::string& pkgid, uid_t uid) :
51 user(uid), handle(nullptr)
57 if (::pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid.c_str(), user, &handle) != PMINFO_R_OK) {
58 throw runtime::Exception("Invalid Package Id");
62 PackageInfo::~PackageInfo()
64 ::pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
67 std::vector<ApplicationInfo> PackageInfo::getAppList() const
69 std::vector<ApplicationInfo> appList;
71 if (::pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, AppListCallback, &appList, user) != PMINFO_R_OK) {
72 ERROR("Error in pkgmgrinfo_appinfo_get_usr_list");
78 std::string PackageInfo::getType() const
81 if (::pkgmgrinfo_pkginfo_get_type(handle, &pkgtype) != PMINFO_R_OK) {
82 throw runtime::Exception("Invalid operation");
88 std::string PackageInfo::getLabel() const
91 if (::pkgmgrinfo_pkginfo_get_label(handle, &label) != PMINFO_R_OK) {
92 throw runtime::Exception("Invalid operation");
98 std::string PackageInfo::getIcon() const
101 if (::pkgmgrinfo_pkginfo_get_icon(handle, &icon) != PMINFO_R_OK) {
102 throw runtime::Exception("Invalid operation");
108 std::string PackageInfo::getDescription() const
111 if (::pkgmgrinfo_pkginfo_get_description(handle, &desc) != PMINFO_R_OK) {
112 throw runtime::Exception("Invalid operation");
121 std::string PackageInfo::getAuthorName() const
124 if (::pkgmgrinfo_pkginfo_get_author_name(handle, &name) != PMINFO_R_OK) {
125 throw runtime::Exception("Invalid operation");
131 std::string PackageInfo::getAuthorEmail() const
134 if (::pkgmgrinfo_pkginfo_get_author_email(handle, &email) != PMINFO_R_OK) {
135 throw runtime::Exception("Invalid operation");
141 std::string PackageInfo::getAuthorHref() const
144 if (::pkgmgrinfo_pkginfo_get_author_name(handle, &href) != PMINFO_R_OK) {
145 throw runtime::Exception("Invalid operation");
151 std::string PackageInfo::getVersion() const
154 if (::pkgmgrinfo_pkginfo_get_version(handle, &version) != PMINFO_R_OK) {
155 throw runtime::Exception("Invalid operation");
161 std::string PackageInfo::getApiVersion() const
164 if (::pkgmgrinfo_pkginfo_get_api_version(handle, &api) != PMINFO_R_OK) {
165 throw runtime::Exception("Invalid operation");
171 std::string PackageInfo::getMainAppId() const
174 if (::pkgmgrinfo_pkginfo_get_mainappid(handle, &mainappid) != PMINFO_R_OK) {
175 throw runtime::Exception("Invalid operation");
181 bool PackageInfo::isSystem() const
184 if (::pkgmgrinfo_pkginfo_is_system(handle, &ret) != PMINFO_R_OK) {
185 throw runtime::Exception("Invalid operation");
191 bool PackageInfo::isRemovable() const
194 if (::pkgmgrinfo_pkginfo_is_removable(handle, &ret) != PMINFO_R_OK) {
195 throw runtime::Exception("Invalid operation");
201 bool PackageInfo::isPreload() const
204 if (::pkgmgrinfo_pkginfo_is_preload(handle, &ret) != PMINFO_R_OK) {
205 throw runtime::Exception("Invalid operation");
211 ApplicationInfo::ApplicationInfo(const std::string& aid, uid_t uid)
213 pkgmgrinfo_appinfo_h handle;
215 if (::pkgmgrinfo_appinfo_get_usr_appinfo(aid.c_str(), uid, &handle) != PMINFO_R_OK) {
216 throw runtime::Exception("Invalid App Id");
221 ::pkgmgrinfo_appinfo_destroy_appinfo(handle);
224 ApplicationInfo::ApplicationInfo(pkgmgrinfo_appinfo_h handle)
230 ApplicationInfo::~ApplicationInfo()
234 void ApplicationInfo::load(pkgmgrinfo_appinfo_h handle) {
235 pkgmgrinfo_app_component comp;
239 ret = ::pkgmgrinfo_appinfo_get_appid(handle, &str);
240 if (ret == PMINFO_R_OK) {
244 ret = ::pkgmgrinfo_appinfo_get_pkgid(handle, &str);
245 if (ret == PMINFO_R_OK) {
249 ret = ::pkgmgrinfo_appinfo_get_apptype(handle, &str);
250 if (ret == PMINFO_R_OK) {
254 ret = ::pkgmgrinfo_appinfo_get_icon(handle, &str);
255 if (ret == PMINFO_R_OK) {
259 ret = ::pkgmgrinfo_appinfo_get_label(handle, &str);
260 if (ret == PMINFO_R_OK) {
264 ret = ::pkgmgrinfo_appinfo_get_component(handle, &comp);
265 if (ret == PMINFO_R_OK) {
266 componentType = static_cast<int>(comp);
269 ::pkgmgrinfo_appinfo_is_nodisplay(handle, &noDisplayed);
270 ::pkgmgrinfo_appinfo_is_taskmanage(handle, &taskManaged);
273 const std::string& ApplicationInfo::getId() const
278 const std::string& ApplicationInfo::getPackage() const
283 const std::string& ApplicationInfo::getType() const
288 const std::string& ApplicationInfo::getIcon() const
293 const std::string& ApplicationInfo::getLabel() const
298 int ApplicationInfo::getComponentType() const
300 return componentType;
303 bool ApplicationInfo::isNoDisplayed() const
308 bool ApplicationInfo::isTaskManaged() const
313 PackageManager::PackageManager() :
314 nativeRequestHandle(nullptr), nativeListenHandle(nullptr)
316 nativeRequestHandle = ::pkgmgr_client_new(PC_REQUEST);
317 if (nativeRequestHandle == nullptr) {
318 throw runtime::Exception("No package manager request instance");
321 nativeListenHandle = ::pkgmgr_client_new(PC_LISTENING);
322 if (nativeListenHandle == nullptr) {
323 ::pkgmgr_client_free(nativeRequestHandle);
324 throw runtime::Exception("No package manager listening instance");
328 PackageManager::~PackageManager()
330 ::pkgmgr_client_free(nativeRequestHandle);
331 ::pkgmgr_client_free(nativeListenHandle);
334 PackageManager& PackageManager::instance()
336 static PackageManager __instance__;
340 void PackageManager::setEventCallback(pkgmgrinfo_handler callback, void* user_data)
343 ret = ::pkgmgr_client_listen_status(nativeListenHandle, callback, user_data);
345 throw runtime::Exception("Failed to set package event callback");
349 void PackageManager::unsetEventCallback()
352 ret = ::pkgmgr_client_remove_listen_status(nativeListenHandle);
354 throw runtime::Exception("Failed to unset package event callback");
358 void PackageManager::installPackage(const std::string& pkgpath, const uid_t user)
360 int ret = ::pkgmgr_client_usr_install(nativeRequestHandle, NULL, NULL, pkgpath.c_str(), NULL, PM_QUIET, PackageEventCallback, nullptr, user);
361 if (ret < PKGMGR_R_OK) {
362 throw runtime::Exception("Package installation failed");
366 void PackageManager::uninstallPackage(const std::string& pkgid, const uid_t user)
370 PackageInfo pkgInfo(pkgid, user);
371 pkgtype = pkgInfo.getType();
373 int ret = ::pkgmgr_client_usr_uninstall(nativeRequestHandle, pkgtype.c_str(), pkgid.c_str(), PM_QUIET, PackageEventCallback, nullptr, user);
374 if (ret < PKGMGR_R_OK) {
375 ERROR("Error in pkgmgr_client_uninstall");
376 throw runtime::Exception("Package manager exception");
380 std::vector<std::string> PackageManager::getPackageList(const uid_t user)
382 std::vector<std::string> packageList;
384 if (::pkgmgrinfo_pkginfo_get_usr_list(PackageListCallback, &packageList, user) != PMINFO_R_OK) {
385 ERROR("Error in pkgmgrinfo_pkginfo_get_list");
391 std::vector<ApplicationInfo> PackageManager::getAppList(const uid_t user)
393 std::vector<ApplicationInfo> appList;
395 if (::pkgmgrinfo_appinfo_get_usr_installed_list(AppListCallback, user, &appList) != PMINFO_R_OK) {
396 ERROR("Error in pkgmgrinfo_appinfo_get_installed_list");
402 void PackageManager::setModeRestriction(int mode, uid_t user)
404 if (::pkgmgr_client_usr_set_restriction_mode(nativeRequestHandle,
406 user) != PKGMGR_R_OK) {
407 throw runtime::Exception("Failed to set package restriction mode");
411 void PackageManager::unsetModeRestriction(int mode, uid_t user)
413 if (::pkgmgr_client_usr_unset_restriction_mode(nativeRequestHandle,
415 user) != PKGMGR_R_OK) {
416 throw runtime::Exception("Failed to unset package mode restriction");
420 int PackageManager::getModeRestriction(uid_t user)
423 if (::pkgmgr_client_usr_get_restriction_mode(nativeRequestHandle,
425 user) != PKGMGR_R_OK) {
426 throw runtime::Exception("Failed to get package restriction mode");