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.
17 #include "common/current_application.h"
19 #include <app_common.h>
20 #include <app_manager.h>
21 #include <package_manager.h>
24 #include "common/extension.h"
25 #include "common/logger.h"
26 #include "common/scope_exit.h"
32 std::string GetRuntimeVariable(const char* variable_name) {
33 return common::Extension::GetRuntimeVariableString(variable_name);
38 CurrentApplication &CurrentApplication::GetInstance() {
40 static CurrentApplication current_application;
41 return current_application;
44 pid_t CurrentApplication::GetProcessId() const {
49 std::string CurrentApplication::GetApplicationId() const {
51 auto app_id = GetRuntimeVariable("application_id");
52 LoggerI("GetRuntimeVariable: cached application_id: %s, runtime application_id: %s", app_id_.c_str(), app_id.c_str());
53 return app_id.empty() ? app_id_ : app_id;
56 std::string CurrentApplication::GetPackageId() const {
58 auto pkg_id = GetRuntimeVariable("package_id");
59 LoggerI("GetRuntimeVariable: cached package_id: %s, runtime package_id: %s", package_id_.c_str(), pkg_id.c_str());
60 return pkg_id.empty() ? package_id_ : pkg_id;
63 std::string CurrentApplication::GetRoot() const {
65 auto app_root = GetRuntimeVariable("app_root");
66 LoggerI("GetRuntimeVariable: cached app_root: %s, runtime app_root: %s", root_.c_str(), app_root.c_str());
67 return app_root.empty() ? root_ : app_root;
70 CurrentApplication::CurrentApplication()
72 app_id_(FetchApplicationId()),
73 package_id_(FetchPackageId()),
78 std::string CurrentApplication::FetchApplicationId() const {
81 char* tmp_str = nullptr;
83 const int ret = app_manager_get_app_id(pid_, &tmp_str);
85 if ((APP_MANAGER_ERROR_NONE == ret) && (nullptr != tmp_str)) {
88 LoggerE("Failed to get application ID: %d (%s)", ret, get_error_message(ret));
96 std::string CurrentApplication::FetchPackageId() const {
98 std::string package_id;
100 int err = app_info_create(app_id_.c_str(), &app_info);
101 if (APP_MANAGER_ERROR_NONE != err) {
102 LoggerE("Can't create app info handle from appId %s: %d (%s)", app_id_.c_str(), err,
103 get_error_message(err));
104 return std::string();
107 app_info_destroy(app_info);
110 char* package = nullptr;
111 err = app_info_get_package(app_info, &package);
112 if (APP_MANAGER_ERROR_NONE != err) {
113 LoggerE("Can't get package name from app info: %d (%s)", err, get_error_message(err));
115 package_id = package;
123 std::string CurrentApplication::FetchRoot() const {
126 char* path = nullptr;
127 path = app_get_data_path();
128 if (nullptr == path) {
129 LoggerE("Can't get path from app_get_data_path");
130 return std::string();
133 std::string ret(path);
135 int index = ret.rfind("data");
136 ret = ret.substr(0, index - 1);
141 } // namespace common