Merge "Add readlink failure case of timezone" into tizen_2.2
[platform/framework/native/appfw.git] / src / app / FApp_AppInfo.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_AppInfo.cpp
19  * @brief       This is the implementation for the _AppInfo.cpp class.
20  */
21
22 #include <cstdio>
23 #include <cstring>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <new>
27 #include <fcntl.h>
28 #include <unique_ptr.h>
29
30 #include <appinfo.h>
31
32 #include <FBaseErrors.h>
33 #include <FAppPkgPackageInfo.h>
34 #include <FBaseSysLog.h>
35
36 #include <FBaseRt_Process.h>
37 #include <FBase_StringConverter.h>
38
39 #include "FAppPkg_PackageInfoImpl.h"
40 #include "FApp_AppInfo.h"
41
42 using namespace Tizen::App::Package;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Runtime;
45
46 extern "C"
47 {
48 void _OSP_EXPORT_
49 AppInfo_Update(void)
50 {
51         result r = Tizen::App::_AppInfo::GetInstance()->Construct();
52         if (IsFailed(r))
53         {
54                 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure.");
55                 fprintf(stderr, "Application initialization failure.\n");
56
57                 _Process::Exit(-1);
58         }
59 }
60
61 void _OSP_EXPORT_
62 InitAppInfo(const char* packageId, const char* svcId, int argc, char* pArgv[], int fd)
63 {
64         AppInfo_Update();
65 }
66
67 void _OSP_EXPORT_
68 InitWebAppInfo(const char* appId, const char* rootPath)
69 {
70         result r = Tizen::App::_AppInfo::GetInstance()->Construct(appId, rootPath, Tizen::App::_APP_TYPE_WEB_APP);
71         if (IsFailed(r))
72         {
73                 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
74                 fprintf(stderr, "Application initialization failure for %s.\n", appId);
75
76                 _Process::Exit(-1);
77         }
78 }
79
80 extern void FBase_Initialize(void);
81
82 }
83
84 namespace Tizen { namespace App
85 {
86
87 const int MAX_APIVERSION = 8;
88 const int MAX_APPID = 10;
89 const char PACKAGE_PATH_FORMAT[] = "/opt/usr/apps/0000000000/";
90 const char PATH_ROOT[] = "/opt/usr/apps/";
91 const char APPINFO_FILE_PATH[] = "info/version.info";
92 const char COMPAT_FILE_PATH[] = "info/compat.info";
93 const char VIRTUAL_ROOT_FILE_PATH[] = "info/virtual.info";
94 const char TYPE_FILE_PATH[] = "info/type.info";
95 const char SUBMODE_NAME[] = "_AppControl";
96
97
98 _AppInfo::_AppInfo(void)
99         : __appState(TERMINATED)
100         , __appType(_APP_TYPE_NONE)
101         , __appRootDirFd(-1)
102         , __appHandlerType(_APP_HANDLER_NONE)
103         , __parentWindowHandle(-1)
104         , __pAppName(null)
105         , __pAppVersion(null)
106         , __isPackageInfoInitialized(false)
107         , __isSubMode(false)
108         , __isVirtualRoot(false)
109 {
110         SysStaticAssert(sizeof(pid_t) == sizeof(int));
111
112         //FBase_Initialize();
113 }
114
115 _AppInfo::~_AppInfo(void)
116 {
117         delete __pAppName;
118         delete __pAppVersion;
119
120         // closing may or may not succeed
121         close(__appRootDirFd);
122 }
123
124 _AppInfo*
125 _AppInfo::GetInstance(void)
126 {
127         static _AppInfo info;
128
129         return &info;
130 }
131
132
133 result
134 _AppInfo::Construct(void)
135 {
136         SysAssertf(appinfo_is_initialized() == 1, "appinfo is not initialized.");
137
138         FBase_Initialize();
139
140         const char* pPackageId = appinfo_get_packageid();
141         const char* pExecName = appinfo_get_execname();
142
143
144         if (strncmp(pExecName, SUBMODE_NAME, strlen(SUBMODE_NAME)) == 0)
145         {
146                 SysLog(NID_APP, "Handling for submode.");
147                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(String(pPackageId));
148
149                 __isSubMode = true;
150
151                 std::unique_ptr<char[]> pActualExec(_StringConverter::CopyToCharArrayN(name));
152                 appinfo_update_submode_execname_and_appid(pActualExec.get());
153                 SysLog(NID_APP, "Executable name is changed to %s.", pActualExec.get());
154         }
155
156         result r = E_SUCCESS;
157         FILE* pFile = NULL;
158
159         {
160                 char appInfoPath[PATH_MAX] = {0, };
161
162                 int len = strlen(PACKAGE_PATH_FORMAT);
163                 strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
164                 appInfoPath[len] = '\0';
165                 strncpy(appInfoPath + strlen(PATH_ROOT), pPackageId, MAX_APPID);
166
167                 // app root directory file descriptor
168                 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
169                 __appRootPath = appInfoPath;
170                 
171                 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
172
173                 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
174                 SysAssertf(fd != -1, "Failed to open info file, errno: %d (%s)", errno, strerror(errno));
175
176                 pFile = fdopen(fd, "r");
177                 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
178
179                 char apiVersion[MAX_APIVERSION] = {0, };
180                 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
181                 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
182
183                 fclose(pFile);
184                 // fd is closed when the stream is closed by fclose();
185
186                 appinfo_set_api_version(appinfo_get_api_version_from_str(apiVersion));
187
188                 // to reduce package manager overhead, libc API is used
189                 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
190                 {
191                         SysLog(NID_APP, "OSP compatibility mode on.");
192                         appinfo_set_compat(1);
193                 }
194
195                 if (faccessat(__appRootDirFd, VIRTUAL_ROOT_FILE_PATH, F_OK, 0) == 0)
196                 {
197                         SysLog(NID_APP, "virtual root mode on.");
198                         __isVirtualRoot = true;
199                 }
200
201                 // type file may does not exist
202                 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
203                 if (fd > 0)
204                 {
205                         pFile = fdopen(fd, "r");
206                         if (pFile)
207                         {
208                                 int i;
209                                 int line = fscanf(pFile, "%d", &i);
210
211                                 fclose(pFile);
212                                 // fd is closed when the stream is closed by fclose();
213
214                                 __appType = _APP_TYPE_UI_APP | i;
215                                 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
216                         }
217                 }
218
219                 SysLog(NID_APP, "AppInfo [%s][%s.%s][%d].", appinfo_get_appid(), appinfo_get_packageid(), appinfo_get_execname(), appinfo_get_api_version());
220         }
221
222         return E_SUCCESS;
223
224 CATCH:
225         if (pFile != NULL)
226         {
227                 fclose(pFile);
228         }
229
230         return r;
231 }
232
233
234 // initialize app context only
235 result
236 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
237 {
238         SysAssertf(appId != null, "Valid appId required to launch application.");
239
240         FBase_Initialize();
241
242         if (appinfo_is_initialized() == 0)
243         {
244                 appinfo_init(appId, 0);
245         }
246
247         __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
248         appinfo_set_api_version(APP_INFO_VERSION_2_2);
249         __appType = type;
250
251         __appRootPath = appRoot;
252         if (__appRootPath[__appRootPath.GetLength()] != L'/')
253         {
254                 __appRootPath.Append(L'/');
255         }
256
257         SysLog(NID_APP, "AppInfo [%s][%s.%s][%ls][%d].", appinfo_get_appid(), appinfo_get_packageid(), appinfo_get_execname()), __appRootPath.GetPointer(), appinfo_get_api_version();
258
259         return E_SUCCESS;
260 }
261
262
263 result
264 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
265 {
266         _PackageManagerImpl* pPkg = _PackageManagerImpl::GetInstance();
267         SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
268
269         result r = E_SUCCESS;
270         std::unique_ptr<PackageInfo> pInfo(pPkg->GetPackageInfoN(packageId));
271         SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
272
273         const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo.get());
274         SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
275
276         delete __pAppName;
277         __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
278         SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
279
280         delete __pAppVersion;
281         __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
282         SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
283
284         __isPackageInfoInitialized = true;
285
286         SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
287
288         return r;
289
290 CATCH:
291         delete __pAppName;
292         __pAppName = null;
293
294         delete __pAppVersion;
295         __pAppVersion = null;
296
297         return r;
298 }
299
300
301 _ApiVersion
302 _AppInfo::GetApiVersion(void)
303 {
304         return static_cast<_ApiVersion>(appinfo_get_api_version());
305 }
306
307
308 bool
309 _AppInfo::IsOspCompat(void)
310 {
311         return (appinfo_is_compat() == 1);
312 }
313
314 bool
315 _AppInfo::IsVirtualRoot(void)
316 {
317         return GetInstance()->__isVirtualRoot;
318 }
319
320 result
321 _AppInfo::SetApiVersion(_ApiVersion v)
322 {
323         appinfo_set_api_version(static_cast<app_info_version_e>(v));
324         return E_SUCCESS;
325 }
326
327
328 int
329 _AppInfo::GetProcessId(void)
330 {
331         static int processId = static_cast<int>(getpid());
332
333         return processId;
334 }
335
336 int
337 _AppInfo::GetAppRootDirFd(void)
338 {
339         return GetInstance()->__appRootDirFd;
340 }
341
342
343 AppId
344 _AppInfo::GetApplicationId(void)
345 {
346         return String(appinfo_get_appid());
347 }
348
349
350 PackageId
351 _AppInfo::GetPackageId(void)
352 {
353         return String(appinfo_get_packageid());
354 }
355
356
357 String
358 _AppInfo::GetAppExecutableName(void)
359 {
360         return String(appinfo_get_execname());
361 }
362
363
364 bool
365 _AppInfo::IsSubMode(void)
366 {
367         return GetInstance()->__isSubMode;
368 }
369
370
371 String
372 _AppInfo::GetAppName(void)
373 {
374         if (!GetInstance()->__isPackageInfoInitialized)
375         {
376                 const String& packageId = GetInstance()->GetPackageId();
377
378                 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
379
380                 result r = GetInstance()->UpdateAppInfoFromPackageInfo(packageId);
381                 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
382         }
383
384         return *(GetInstance()->__pAppName);
385 }
386
387
388 String
389 _AppInfo::GetAppRootPath(void)
390 {
391         return GetInstance()->__appRootPath;
392 }
393
394 String
395 _AppInfo::GetAppVersion(void)
396 {
397         if (!GetInstance()->__isPackageInfoInitialized)
398         {
399                 const String& packageId = GetInstance()->GetPackageId();
400
401                 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
402
403                 result r = GetInstance()->UpdateAppInfoFromPackageInfo(packageId);
404                 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
405         }
406
407         return *(GetInstance()->__pAppVersion);
408 }
409
410
411 AppState
412 _AppInfo::GetAppState(void)
413 {
414         return GetInstance()->__appState;
415 }
416
417
418 void
419 _AppInfo::SetAppState(AppState appState)
420 {
421         GetInstance()->__appState = appState;
422 }
423
424
425 int
426 _AppInfo::GetAppType(void)
427 {
428         return GetInstance()->__appType;
429 }
430
431
432 void
433 _AppInfo::SetAppType(_AppType appType)
434 {
435         GetInstance()->__appType |= appType;
436 }
437
438
439 int
440 _AppInfo::GetAppHandlerType(void)
441 {
442         return GetInstance()->__appHandlerType;
443 }
444
445
446 void
447 _AppInfo::SetAppHandlerType(int appHandlerType)
448 {
449         GetInstance()->__appHandlerType = appHandlerType;
450 }
451
452
453 unsigned int
454 _AppInfo::GetParentWindowHandle(void)
455 {
456         return GetInstance()->__parentWindowHandle;
457 }
458
459
460 void
461 _AppInfo::SetParentWindowHandle(unsigned int handle)
462 {
463         GetInstance()->__parentWindowHandle = handle;
464 }
465
466
467 void
468 _AppInfo::UpdatePackageInfo(bool update)
469 {
470         GetInstance()->__isPackageInfoInitialized = !update;
471 }
472
473 }} // Tizen::App