#include "common/logger.h"
#include "common/scope_exit.h"
+#include "common/current_application.h"
namespace common {
namespace tools {
static std::string cached_api_version;
static int cached_pid = -1;
- char* app_id = nullptr;
- char* pkgid = nullptr;
char* api_ver = nullptr;
- app_info_h app_handle = nullptr;
pkgmgrinfo_pkginfo_h pkginfo_handle = nullptr;
SCOPE_EXIT {
- if (app_id) {
- free(app_id);
- }
- if (pkgid) {
- free(pkgid);
- }
- if (app_handle) {
- app_info_destroy(app_handle);
- }
if (pkginfo_handle) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_handle);
}
if (cached_pid == pid) {
*api_version = cached_api_version; // Retrieve from local cache
} else {
- int ret = app_manager_get_app_id(pid, &app_id);
- if (ret != APP_MANAGER_ERROR_NONE) {
- return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get app id");
- }
-
- ret = app_info_create(app_id, &app_handle);
- if (ret != APP_MANAGER_ERROR_NONE) {
- return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get app info");
- }
-
- ret = app_info_get_package(app_handle, &pkgid);
- if ((ret != APP_MANAGER_ERROR_NONE) || (pkgid == nullptr)) {
+ const char *pkgid =common::CurrentApplication::GetInstance().GetPackageId().c_str();
+ if (pkgid == nullptr) {
return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get pkg id");
}
- ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, getuid(), &pkginfo_handle);
+ int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, getuid(), &pkginfo_handle);
if (ret != PMINFO_R_OK) {
return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Fail to get pkginfo_h");
}
#include "common/logger.h"
#include "common/scope_exit.h"
#include "common/tools.h"
+#include "common/current_application.h"
namespace extension {
namespace package {
void PackageInfoProvider::GetPackageInfo(picojson::object& out) {
ScopeLogger();
- char* package_id = NULL;
+ std::string package_id;
if (GetCurrentPackageId(&package_id)) {
- GetPackageInfo(package_id, out);
- free(package_id);
+ GetPackageInfo(package_id.c_str(), out);
} else {
LoggerE("Failed to get current package ID");
REPORT_ERROR(out, NotFoundException("The package with the specified ID is not found"));
GetSize(id, PM_GET_DATA_SIZE, out);
}
-bool PackageInfoProvider::GetCurrentPackageId(char** package_id) {
+bool PackageInfoProvider::GetCurrentPackageId(std::string* package_id) {
ScopeLogger();
- int ret = 0;
- char* app_id = NULL;
-
- int pid = getpid();
- ret = app_manager_get_app_id(pid, &app_id);
- if (ret != APP_MANAGER_ERROR_NONE) {
- LoggerE("Failed to get app id: %d (%s)", ret, get_error_message(ret));
- return false;
- }
-
- app_info_h handle;
- ret = app_info_create(app_id, &handle);
- free(app_id);
- if (ret != APP_MANAGER_ERROR_NONE) {
- LoggerE("Fail to get app info: %d (%s)", ret, get_error_message(ret));
- return false;
- }
-
- ret = app_info_get_package(handle, package_id);
- app_info_destroy(handle);
- if ((ret != APP_MANAGER_ERROR_NONE) || (*package_id == NULL)) {
- LoggerE("Fail to get pkg id: %d (%s)", ret, get_error_message(ret));
+ *package_id = common::CurrentApplication::GetInstance().GetPackageId();
+ if (package_id->empty()) {
+ LoggerE("Fail to get pkg id. Result is empty");
return false;
}
static void GetDataSize(const std::string& id, picojson::object* out);
private:
- static bool GetCurrentPackageId(char** package_id);
+ static bool GetCurrentPackageId(std::string* package_id);
};
} // namespace package