[Common] Get package, application ID from GetRuntimeVariable
[platform/core/api/webapi-plugins.git] / src / common / current_application.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "common/current_application.h"
18
19 #include <app_common.h>
20 #include <app_manager.h>
21 #include <package_manager.h>
22 #include <unistd.h>
23
24 #include "common/extension.h"
25 #include "common/logger.h"
26 #include "common/scope_exit.h"
27
28 namespace common {
29
30 namespace {
31
32 std::string GetRuntimeVariable(const char* variable_name) {
33   return common::Extension::GetRuntimeVariableString(variable_name);
34 }
35
36 }  // namespace
37
38 CurrentApplication &CurrentApplication::GetInstance() {
39   ScopeLogger();
40   static CurrentApplication current_application;
41   return current_application;
42 }
43
44 pid_t CurrentApplication::GetProcessId() const {
45   ScopeLogger();
46   return pid_;
47 }
48
49 std::string CurrentApplication::GetApplicationId() const {
50   ScopeLogger();
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;
54 }
55
56 std::string CurrentApplication::GetPackageId() const {
57   ScopeLogger();
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;
61 }
62
63 std::string CurrentApplication::GetRoot() const {
64   ScopeLogger();
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;
68 }
69
70 CurrentApplication::CurrentApplication()
71     : pid_(getpid()),
72       app_id_(FetchApplicationId()),
73       package_id_(FetchPackageId()),
74       root_(FetchRoot()) {
75   ScopeLogger();
76 }
77
78 std::string CurrentApplication::FetchApplicationId() const {
79   ScopeLogger();
80   std::string app_id;
81   char* tmp_str = nullptr;
82
83   const int ret = app_manager_get_app_id(pid_, &tmp_str);
84
85   if ((APP_MANAGER_ERROR_NONE == ret) && (nullptr != tmp_str)) {
86     app_id = tmp_str;
87   } else {
88     LoggerE("Failed to get application ID: %d (%s)", ret, get_error_message(ret));
89   }
90
91   free(tmp_str);
92
93   return app_id;
94 }
95
96 std::string CurrentApplication::FetchPackageId() const {
97   ScopeLogger();
98   std::string package_id;
99   app_info_h app_info;
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();
105   }
106   SCOPE_EXIT {
107     app_info_destroy(app_info);
108   };
109
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));
114   } else {
115     package_id = package;
116   }
117
118   free(package);
119
120   return package_id;
121 }
122
123 std::string CurrentApplication::FetchRoot() const {
124   ScopeLogger();
125
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();
131   }
132
133   std::string ret(path);
134   free(path);
135   int index = ret.rfind("data");
136   ret = ret.substr(0, index - 1);
137   LoggerD("Exit");
138   return ret;
139 }
140
141 }  // namespace common