Merge "Fix build warning based on GCC-9" into tizen
[platform/core/api/notification.git] / notification-ex / ex_util.cc
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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 <dlog.h>
18 #include <aul.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <package_info.h>
22 #include <package_manager.h>
23
24 #include <string>
25 #include <memory>
26 #include <map>
27
28 #include "notification-ex/ex_util.h"
29
30 #ifdef LOG_TAG
31 #undef LOG_TAG
32 #endif
33
34 #define LOG_TAG "NOTIFICATION_EX"
35 #define MAX_PACKAGE_STR_SIZE 512
36 #define MAX_CACHE_SIZE 100
37
38 using namespace std;
39 namespace notification {
40 namespace util {
41
42 int GetRequestId() {
43   static int id = 0;
44   g_atomic_int_inc(&id);
45   return id;
46 }
47
48 string GetAppId() {
49  return GetAppId(getpid());
50 }
51
52 string GetAppId(pid_t pid) {
53   static map<pid_t, string> appid_cache;
54
55   string appid;
56   char appid_buf[MAX_PACKAGE_STR_SIZE] = {0, };
57
58   if (appid_cache.find(pid) != appid_cache.end()) {
59     LOGI("appid(%s)", appid_cache[pid].c_str());
60     return appid_cache[pid];
61   }
62
63   if (appid_cache.size() > MAX_CACHE_SIZE)
64     appid_cache.clear();
65
66   int ret = aul_app_get_appid_bypid(pid, appid_buf, sizeof(appid_buf));
67   if (ret == AUL_R_OK) {
68     appid = string(appid_buf);
69   } else {
70     int fd, i;
71     int last_slash_index = 0;
72     char proc_buf[MAX_PACKAGE_STR_SIZE] = { 0, };
73
74     snprintf(proc_buf, sizeof(proc_buf), "/proc/%d/cmdline", pid);
75
76     fd = open(proc_buf, O_RDONLY);
77     if (fd < 0) {
78       LOGE("Fail to get appid (%d)", errno);
79       return "";
80     }
81
82     ret = read(fd, appid_buf, sizeof(appid_buf) - 1);
83     if (ret <= 0) {
84       LOGE("Fail to get appid (%d)", errno);
85       close(fd);
86       return "";
87     }
88     close(fd);
89
90     for (i = 0 ; i < ret ; i++) {
91       if (appid_buf[i] == '/')
92         last_slash_index = i;
93     }
94
95     if (last_slash_index == (ret - 1)) {
96       LOGE("Fail to get appid (%s)", appid_buf);
97       return "";
98     }
99
100     if (last_slash_index == 0)
101       appid = string(appid_buf);
102     else
103       appid = string(&appid_buf[last_slash_index + 1]);
104   }
105
106   LOGI("appid(%s)", appid.c_str());
107   appid_cache[pid] = appid;
108   return appid;
109 }
110
111 string GetPkgId() {
112   static string pkgid = "";
113   if (!pkgid.empty()) {
114     LOGI("pkgid(%s)", pkgid.c_str());
115     return pkgid;
116   }
117
118   char pkgid_buf[MAX_PACKAGE_STR_SIZE] = {0, };
119   int ret = aul_app_get_pkgid_bypid(getpid(), pkgid_buf, sizeof(pkgid_buf));
120   if (ret == AUL_R_OK)
121     pkgid = string(pkgid_buf);
122
123   return pkgid;
124 }
125
126 string GetDomainName() {
127   static string domain_str = "";
128   if (!domain_str.empty()) {
129     LOGI("domain(%s)", domain_str.c_str());
130     return domain_str;
131   }
132
133   string pkgid = GetPkgId();
134   if (pkgid.empty())
135     return "";
136
137   string::size_type pos = pkgid.rfind('.');
138   if (pos != string::npos)
139     ++pos;
140   else
141     return "";
142
143   domain_str = pkgid.substr(pos);
144   return domain_str;
145 }
146
147 string GetLocaleDirectory() {
148   string pkgid = GetPkgId();
149   if (pkgid.empty())
150     return "";
151
152   static string locale_directory = "";
153   if (!locale_directory.empty()) {
154     LOGI("locale_directory(%s)", locale_directory.c_str());
155     return locale_directory;
156   }
157
158   package_info_h package_info;
159   int err = package_info_create(pkgid.c_str(), &package_info);
160   if (err != PACKAGE_MANAGER_ERROR_NONE) {
161     LOGE("fail to get package info");
162     return "";
163   }
164   unique_ptr<package_info_s, decltype(package_info_destroy)*> pkg_ptr(
165       package_info, package_info_destroy);
166
167   char* app_root_path;
168   err = package_info_get_root_path(pkg_ptr.get(), &app_root_path);
169   if (err != PACKAGE_MANAGER_ERROR_NONE) {
170     LOGE("Failed to get root path err[%d] path[%p]",
171         err, app_root_path);
172     return "";
173   }
174
175   string app_root_str(app_root_path);
176   free(app_root_path);
177
178   locale_directory = app_root_str + string("/res/locale");
179   return locale_directory;
180 }
181
182 }  // namespace util
183 }  // namespace notification