Merge "Fix a bug on Systeminfo" into tizen_2.1
[platform/framework/native/appfw.git] / src / app / FApp_AppInfo.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file         FApp_AppInfo.cpp
20  * @brief       This is the implementation for the _AppInfo.cpp class.
21  */
22
23 #include <cstdio>
24 #include <cstring>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <new>
28 #include <fcntl.h>
29
30 #include <FBaseErrors.h>
31 #include <FAppPkgPackageInfo.h>
32
33 #include <FBaseSysLog.h>
34 #include <FBaseRt_Process.h>
35
36 #include "FAppPkg_PackageInfoImpl.h"
37 #include "FApp_AppInfo.h"
38 #include "FApp_Aul.h"
39 #include "FApp_AppArg.h"
40
41 using namespace Tizen::App::Package;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Runtime;
44
45 extern "C"
46 {
47 void _OSP_EXPORT_
48 InitAppInfo(const char* appId, const char* svcId, int argc, char* pArgv[], int fd)
49 {
50         result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(appId, svcId, argc, pArgv);
51         if (IsFailed(r))
52         {
53                 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
54                 fprintf(stderr, "Application initialization failure for %s.\n", appId);
55
56                 _Process::Exit(-1);
57         }
58 }
59
60 void _OSP_EXPORT_
61 InitWebAppInfo(const char* appId, const char* rootPath)
62 {
63         result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(appId, rootPath, Tizen::App::_APP_TYPE_WEB_APP);
64         if (IsFailed(r))
65         {
66                 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
67                 fprintf(stderr, "Application initialization failure for %s.\n", appId);
68
69                 _Process::Exit(-1);
70         }
71 }
72
73 extern void FBase_Initialize(void);
74
75 }
76
77 namespace Tizen { namespace App
78 {
79
80 const int MAX_APIVERSION = 8;
81 const int MAX_APPID = 10;
82 const char PACKAGE_PATH_FORMAT[] = "/opt/usr/apps/0000000000/";
83 const char PACKAGE_PATH_FORMAT2[] = "/opt/apps/0000000000/";
84 const char PATH_ROOT[] = "/opt/usr/apps/";
85 const char PATH_ROOT2[] = "/opt/apps/";
86 const char APPINFO_FILE_PATH[] = "info/version.info";
87 const char COMPAT_FILE_PATH[] = "info/compat.info";
88 const char TYPE_FILE_PATH[] = "info/type.info";
89
90
91 _AppInfo::_AppInfo(void)
92         : __appState(TERMINATED)
93         , __appType(_APP_TYPE_UI_APP)
94         , __appRootDirFd(-1)
95         , __appHandlerType(_APP_HANDLER_NONE)
96         , __parentWindowHandle(-1)
97         , __apiVersion(_API_VERSION_2_1)
98         , __pAppName(null)
99         , __pAppVersion(null)
100         , __argc(0)
101         , __pArgv(null)
102         , __isPackageInfoInitialized(false)
103         , __isOspCompat(false)
104         , __isSubMode(false)
105 {
106         SysStaticAssert(sizeof(pid_t) == sizeof(int));
107
108         //FBase_Initialize();
109 }
110
111 _AppInfo::~_AppInfo(void)
112 {
113         delete __pAppName;
114         delete __pAppVersion;
115 }
116
117 _AppInfo*
118 _AppInfo::GetAppInfo(void)
119 {
120         static _AppInfo info;
121
122         return &info;
123 }
124
125
126 result
127 _AppInfo::Construct(const char* appId, const char* exeName, int argc, char* argv[])
128 {
129         SysAssertf(appId != null, "Valid appId required to launch application.");
130
131      FBase_Initialize();
132
133         __appExecutableName = exeName;
134         __packageId = appId;
135
136         if (__appExecutableName == L"_AppControl")
137         {
138                 SysLog(NID_APP, "Handling for submode.");
139                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
140
141                 __isSubMode = true;
142                 __appExecutableName = name;
143                 SysLog(NID_APP, "Executable name is changed to %ls.", __appExecutableName.GetPointer());
144         }
145
146         __appId = __packageId + L'.' + __appExecutableName;
147
148         result r = E_SUCCESS;
149         FILE* pFile = NULL;
150
151         {
152                 char appInfoPath[PATH_MAX] = {0, };
153 #if 0
154                 const int len = strlen(PACKAGE_PATH_FORMAT2);
155                 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
156                 appInfoPath[len] = '\0';
157
158                 // due to possible dependency problem, FIoFile is not used
159                 // app root path first
160
161                 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
162 #else
163                 // [FIXME] temporary code for directory location migration
164                 int len = strlen(PACKAGE_PATH_FORMAT2);
165                 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
166                 appInfoPath[len] = '\0';
167                 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
168
169                 if (access(appInfoPath, R_OK) != 0)
170                 {
171                         len = strlen(PACKAGE_PATH_FORMAT);
172                         strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
173                         appInfoPath[len] = '\0';
174                         
175                         strncpy(appInfoPath + strlen(PATH_ROOT), appId, MAX_APPID);
176                 }
177 #endif
178                 // app root directory file descriptor
179                 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
180
181                 __appRootPath = appInfoPath;
182                 
183                 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
184
185                 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
186                 SysAssert(fd != -1);
187
188                 pFile = fdopen(fd, "r");
189                 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
190
191                 char apiVersion[MAX_APIVERSION] = {0, };
192                 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
193                 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
194
195                 fclose(pFile);
196                 // fd is closed when the stream is closed by fclose();
197
198                 __apiVersion = GetApiVersionFromStr(apiVersion);
199
200                 __argc = argc;
201                 __pArgv = argv;
202
203                 // to reduce package manager overhead, libc API is used
204                 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
205                 {
206                         SysLog(NID_APP, "OSP compatibility mode on.");
207                         __isOspCompat = true;
208                 }
209
210                 // type file may does not exist
211                 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
212                 if (fd > 0)
213                 {
214                         pFile = fdopen(fd, "r");
215                         if (pFile)
216                         {
217                                 int i;
218                                 int line = fscanf(pFile, "%d", &i);
219
220                                 fclose(pFile);
221                                 // fd is closed when the stream is closed by fclose();
222
223                                 __appType = _APP_TYPE_UI_APP | i;
224                                 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
225                         }
226                 }
227
228                 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
229                                 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __apiVersion);
230         }
231
232         return E_SUCCESS;
233
234 CATCH:
235         __appId.Clear();
236         __appExecutableName.Clear();
237
238         delete __pAppName;
239         __pAppName = null;
240
241         delete __pAppVersion;
242         __pAppVersion = null;
243
244         if (pFile != NULL)
245         {
246                 fclose(pFile);
247         }
248
249         return r;
250 }
251
252
253 // initialize app context only
254 result
255 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
256 {
257         SysAssertf(appId != null, "Valid appId required to launch application.");
258
259      FBase_Initialize();
260
261         __appId = appId;
262
263         int index = 0;
264         if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
265         {
266                 __appId.SubString(index + 1, __appExecutableName);
267                 __appId.SubString(0, index, __packageId);
268         }
269         else
270         {
271                 __appExecutableName = __appId;
272                 __packageId = __appId;
273         }
274
275         __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
276         __apiVersion = _API_VERSION_2_1;
277         __appType = type;
278
279         __appRootPath = appRoot;
280         if (__appRootPath[__appRootPath.GetLength()] != L'/')
281         {
282                 __appRootPath.Append(L'/');
283         }
284
285         SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(),  __apiVersion);
286
287         return E_SUCCESS;
288 }
289
290
291 result
292 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
293 {
294         _PackageManagerImpl* pPkg = null;
295         pPkg = _PackageManagerImpl::GetInstance();
296         SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
297
298         result r = E_SUCCESS;
299         PackageInfo* pInfo = null;
300         pInfo = pPkg->GetPackageInfoN(packageId);
301         SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
302
303         const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
304         SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
305
306         delete __pAppName;
307         __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
308         SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
309
310         delete __pAppVersion;
311         __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
312         SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
313
314         __isPackageInfoInitialized = true;
315
316         delete pInfo;
317
318         SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
319
320         return r;
321
322 CATCH:
323         delete pInfo;
324
325         delete __pAppName;
326         __pAppName = null;
327
328         delete __pAppVersion;
329         __pAppVersion = null;
330
331         return r;
332 }
333
334
335 // [FIXME] refactoring to use hash
336 _ApiVersion
337 _AppInfo::GetApiVersionFromStr(const char* pVer)
338 {
339         String ver(pVer);
340         ver.Trim();
341
342         if (ver == L"3.0")
343         {
344                 return _API_VERSION_2_1;
345         }
346         else if (ver == L"2.0")
347         {
348                 return _API_VERSION_2_0;
349         }
350         else if (ver == L"1.2")
351         {
352                 return _API_VERSION_1_2;
353         }
354         else if (ver == L"1.1")
355         {
356                 return _API_VERSION_1_1;
357         }
358         else if (ver == L"1.0.2")
359         {
360                 return _API_VERSION_1_0_2;
361         }
362         else if (ver == L"1.0")
363         {
364                 return _API_VERSION_1_0;
365         }
366         else
367         {
368                 return _API_VERSION_2_1;
369         }
370 }
371
372
373 _ApiVersion
374 _AppInfo::GetApiVersion(void)
375 {
376         return GetAppInfo()->__apiVersion;
377 }
378
379
380 bool
381 _AppInfo::IsOspCompat(void)
382 {
383         return GetAppInfo()->__isOspCompat;
384 }
385
386
387 result
388 _AppInfo::SetApiVersion(_ApiVersion v)
389 {
390         GetAppInfo()->__apiVersion = v;
391         return E_SUCCESS;
392 }
393
394
395 int
396 _AppInfo::GetProcessId(void)
397 {
398         static int processId = static_cast<int>(getpid());
399
400         return processId;
401 }
402
403 int
404 _AppInfo::GetAppRootDirFd(void)
405 {
406         return GetAppInfo()->__appRootDirFd;
407 }
408
409
410 AppId
411 _AppInfo::GetApplicationId(void)
412 {
413         const String& appId = GetAppInfo()->__appId;
414
415         SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
416
417         return appId;
418 }
419
420
421 PackageId
422 _AppInfo::GetPackageId(void)
423 {
424         const String& packageId = GetAppInfo()->__packageId;
425
426         SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
427
428         return packageId;
429 }
430
431
432 String
433 _AppInfo::GetAppExecutableName(void)
434 {
435         return GetAppInfo()->__appExecutableName;
436 }
437
438
439 bool
440 _AppInfo::IsSubMode(void)
441 {
442         return GetAppInfo()->__isSubMode;
443 }
444
445
446 String
447 _AppInfo::GetAppName(void)
448 {
449         if (!GetAppInfo()->__isPackageInfoInitialized)
450         {
451                 const String& packageId = GetAppInfo()->__packageId;
452
453                 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
454
455                 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
456                 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
457         }
458
459         return *(GetAppInfo()->__pAppName);
460 }
461
462
463 String
464 _AppInfo::GetAppRootPath(void)
465 {
466         return GetAppInfo()->__appRootPath;
467 }
468
469 String
470 _AppInfo::GetAppVersion(void)
471 {
472         if (!GetAppInfo()->__isPackageInfoInitialized)
473         {
474                 const String& packageId = GetAppInfo()->__packageId;
475
476                 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
477
478                 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
479                 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
480         }
481
482         return *(GetAppInfo()->__pAppVersion);
483 }
484
485
486 AppState
487 _AppInfo::GetAppState(void)
488 {
489         return GetAppInfo()->__appState;
490 }
491
492
493 void
494 _AppInfo::SetAppState(AppState appState)
495 {
496         GetAppInfo()->__appState = appState;
497 }
498
499
500 int
501 _AppInfo::GetAppType(void)
502 {
503         return GetAppInfo()->__appType;
504 }
505
506
507 void
508 _AppInfo::SetAppType(_AppType appType)
509 {
510         GetAppInfo()->__appType |= appType;
511 }
512
513
514 int
515 _AppInfo::GetArgc(void)
516 {
517         return GetAppInfo()->__argc;
518 }
519
520
521 char**
522 _AppInfo::GetArgv(void)
523 {
524         return GetAppInfo()->__pArgv;
525 }
526
527
528 int
529 _AppInfo::GetAppHandlerType(void)
530 {
531         return GetAppInfo()->__appHandlerType;
532 }
533
534
535 void
536 _AppInfo::SetAppHandlerType(int appHandlerType)
537 {
538         GetAppInfo()->__appHandlerType = appHandlerType;
539 }
540
541
542 unsigned int
543 _AppInfo::GetParentWindowHandle(void)
544 {
545         return GetAppInfo()->__parentWindowHandle;
546 }
547
548
549 void
550 _AppInfo::SetParentWindowHandle(unsigned int handle)
551 {
552         GetAppInfo()->__parentWindowHandle = handle;
553 }
554
555
556 void
557 _AppInfo::UpdatePackageInfo(bool update)
558 {
559         GetAppInfo()->__isPackageInfoInitialized = !update;
560 }
561
562 }} // Tizen::App