Merge "Apply model-config.xml" into tizen_2.2
[platform/framework/native/appfw.git] / src / app / FApp_Aul.cpp
1 //
2 // Copyright (c) 2012 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 /**
18  * @file         FApp_Aul.cpp
19  * @brief       This is the implementation for the _Aul.cpp class.
20  */
21 #include <cstdio>
22 #include <cstdlib>
23 #include <new>
24 #include <unistd.h>
25 #include <sys/prctl.h>
26 #include <signal.h>
27 #include <unique_ptr.h>
28
29 #include <aul.h>
30 #include <bundle.h>
31 #include <appsvc/appsvc.h>
32 #include <app_manager.h>
33 #include <heynoti.h>
34
35 #include <FBaseObject.h>
36 #include <FBaseString.h>
37 #include <FBaseUtil.h>
38 #include <FBaseSysLog.h>
39 #include <FBaseColHashMapT.h>
40 #include <FAppPkgPackageInfo.h>
41
42 #include <FBaseRt_Process.h>
43 #include <FBase_StringConverter.h>
44 #include "FAppPkg_PackageManagerImpl.h"
45 #include "FApp_Types.h"
46 #include "FApp_Aul.h"
47 #include "FApp_TemplateUtil.h"
48
49
50 using namespace Tizen::App::Package;
51 using namespace Tizen::Base;
52 using namespace Tizen::Base::Collection;
53 using namespace Tizen::Base::Runtime;
54 using namespace Tizen::Base::Utility;
55
56 namespace
57 {
58 const int _MAX_CATEGORY = 34;
59
60 // borrowed from app-svc/include/pri_key.h
61 #define APP_SVC_K_RES_VAL       "__APP_SVC_K_RES_VAL__"
62
63 }
64
65 namespace Tizen { namespace App
66 {
67
68 struct _CategoryList
69 {
70         const char category[_MAX_CATEGORY];
71         const _AppType type;
72 };
73
74 static const _CategoryList _CATEGORY_LIST[] =
75 {
76         {"home-screen", _APP_TYPE_HOME_APP},
77         {"lock-screen", _APP_TYPE_LOCK_APP},
78         {"ime", _APP_TYPE_IME_APP},
79         {"http://tizen.org/category/homeapp", _APP_TYPE_HOME_APP},
80         {"http://tizen.org/category/lockapp", _APP_TYPE_LOCK_APP},
81         {"http://tizen.org/category/ime", _APP_TYPE_IME_APP},
82 };
83
84 static const int _NUM_CATEGORY = sizeof(_CATEGORY_LIST) / sizeof(_CategoryList);
85
86 result
87 _Aul::GetConvertedResult(const int aul_ret, const char* pFunctionName)
88 {
89         result r = E_SUCCESS;
90
91         switch (aul_ret)
92         {
93         case AUL_R_EINVAL:
94                 r = E_INVALID_ARG;
95                 SysLogException(NID_APP, r, "%s : Invalid argument.", pFunctionName);
96                 break;
97
98         case AUL_R_ECOMM:
99                 r = E_SYSTEM;
100                 SysLogException(NID_APP, r, "%s : Internal IPC error.", pFunctionName);
101                 break;
102
103         case AUL_R_ERROR:
104                 r = E_SYSTEM;
105                 SysLogException(NID_APP, r, "%s : General error.", pFunctionName);
106                 break;
107
108         default:
109                 SysLog(NID_APP, "%s : successed.", pFunctionName);
110                 break;
111         }
112
113         return r;
114 }
115
116 result
117 _Aul::SendResult(bundle* b, appsvc_result_val res)
118 {
119         // to skip error handling, of appsvc_send_result, use aul_send_service_result() directly.
120         //int ret = appsvc_send_result(b, res);
121
122         char tmp[32] = {0, };
123         snprintf(tmp, 32, "%d", static_cast<int>(res));
124         appsvc_add_data(b, APP_SVC_K_RES_VAL, tmp);
125
126         const int aul_ret = aul_send_service_result(b);
127
128         result r = GetConvertedResult(aul_ret, "SendResult");
129         if (r == E_INVALID_ARG)
130         {
131                 SysLog(NID_APP, "Converting internal exception to E_MAX_EXCEEDED.");
132                 r = E_MAX_EXCEEDED;
133         }
134         return r;
135 }
136
137
138 bool
139 _Aul::IsRunning(const String& appId)
140 {
141         bool isRunning = false;
142         std::unique_ptr<char[]> pSlpPackageName(_StringConverter::CopyToCharArrayN(appId));
143
144         app_manager_is_running(pSlpPackageName.get(), &isRunning);
145
146         SysLog(NID_APP, "'%ls' %s running now.", appId.GetPointer(), (isRunning) ? "is" : "is NOT");
147         return isRunning;
148 }
149
150
151 result
152 _Aul::TerminateApplicationByPid(int pid)
153 {
154         int ret_aul = aul_terminate_pid(pid);
155
156         return GetConvertedResult(ret_aul, "TerminateApplicationByPid");
157 }
158
159 static int
160 TerminateApplicationIterFnCb(const aul_app_info* pAppInfo, void* pData)
161 {
162         const char* pStr = static_cast<const char*>(pData);
163
164         if (pStr && strncmp(pStr, pAppInfo->pkg_name, NATIVE_APP_MAX_APPID_LENGTH) == 0)
165         {
166                 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
167                 int ret_aul = aul_terminate_pid( pAppInfo->pid );
168                 SetLastResult(_Aul::GetConvertedResult(ret_aul, "TerminateApplication"));
169         }
170         return 0;
171 }
172
173 result
174 _Aul::TerminateApplication(const AppId& appId)
175 {
176         SetLastResult(E_OBJ_NOT_FOUND);
177         std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
178         aul_app_get_running_app_info(TerminateApplicationIterFnCb, static_cast<void*>(pAppId.get()));
179
180         SysLog(NID_APP, "%ls terminated.", appId.GetPointer());
181         return GetLastResult();
182 }
183
184 result
185 _Aul::SetOomAdj(int pid, int adj)
186 {
187         // set oom_adj to -17 for system service
188         result r = E_SUCCESS;
189         char buf[FILENAME_MAX];
190         FILE *fP = NULL;
191
192         snprintf(buf, FILENAME_MAX, "/proc/%d/oom_adj", pid);
193         fP = fopen(buf, "w");
194         SysTryReturnResult(NID_APP, fP != NULL, E_SYSTEM, "oom_adj change failed with %s.", strerror(errno));
195
196         fprintf(fP, "%d", adj);
197         fclose(fP);
198
199         return r;
200 }
201
202 result
203 _Aul::SetPowerOffNotiListener( void (*powerOffCb)(void *pData), void *pData)
204 {
205         int heyFd = heynoti_init();
206         SysTryReturnResult(NID_APP, heyFd >= 0, E_SYSTEM, "heynoti_init failed.");
207
208         int ret = heynoti_subscribe(heyFd, "power_off_start", powerOffCb, pData);
209         SysTryReturnResult(NID_APP, ret >= 0, E_SYSTEM, "heynoti_subscribe failed.");
210
211         ret = heynoti_attach_handler(heyFd);
212         SysTryReturnResult(NID_APP, ret >= 0, E_SYSTEM, "heynoti_attach_handler failed.");
213
214         return E_SUCCESS;
215 }
216
217
218 // _Aul::GetAppType is provided for installer usage
219 int
220 _Aul::GetAppType(const String& category)
221 {
222         int ret = 0;
223
224         HashMapT<String, int> map;
225         map.Construct();
226
227         StringTokenizer strTok(category, L';');
228
229         String token;
230         while (strTok.HasMoreTokens())
231         {
232                 result r = strTok.GetNextToken(token);
233                 if (r == E_SUCCESS)
234                 {
235                         map.Add(token, 0);
236                 }
237         }
238
239         SysLog(NID_APP, "%d category items .", map.GetCount());
240
241         String key;
242
243         for (int i = 0; i < _NUM_CATEGORY; i++)
244         {
245                 bool b = false;
246                 key = _CATEGORY_LIST[i].category;
247                 result r = map.ContainsKey(key, b);
248                 if (r == E_SUCCESS && b)
249                 {
250                         ret |= _CATEGORY_LIST[i].type;
251                 }
252         }
253
254         return ret;
255 }
256
257
258 bool
259 _Aul::IsInstalled(const AppId& appId)
260 {
261         String packageId;
262         packageId = _PackageManagerImpl::GetPackageIdByAppId(appId);
263
264         return _PackageManagerImpl::GetInstance()->IsPackageInstalled(packageId);
265 }
266
267
268 AppId
269 _Aul::GetRealAppId(const AppId& appId)
270 {
271         String temp;
272         // [INFO] ugly code for submode callee
273         appId.SubString(11, temp);
274         if (temp == L"_AppControl")
275         {
276                 String id;
277                 appId.SubString(0, 10, id);
278                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(id);
279
280                 const String retVal = id + L'.' + name;
281                 SysLog(NID_APP, "Converted caller Id is %ls", retVal.GetPointer());
282
283                 return retVal;
284         }
285         else
286         {
287                 return appId;
288         }
289 }
290
291 } } // Tizen::App