2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FApp_AppInfo.cpp
20 * @brief This is the implementation for the _AppInfo.cpp class.
30 #include <FBaseErrors.h>
31 #include <FAppPkgPackageInfo.h>
33 #include <FBaseSysLog.h>
34 #include <FBaseRt_Process.h>
36 #include "FAppPkg_PackageInfoImpl.h"
37 #include "FApp_AppInfo.h"
39 #include "FApp_AppArg.h"
41 using namespace Tizen::App::Package;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Runtime;
48 InitAppInfo(const char* appId, const char* svcId, int argc, char* pArgv[], int fd)
50 result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(appId, svcId, argc, pArgv);
53 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
54 fprintf(stderr, "Application initialization failure for %s.\n", appId);
61 InitWebAppInfo(const char* appId, const char* rootPath)
63 result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(appId, rootPath, Tizen::App::_APP_TYPE_WEB_APP);
66 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
67 fprintf(stderr, "Application initialization failure for %s.\n", appId);
73 extern void FBase_Initialize(void);
77 namespace Tizen { namespace App
80 const int MAX_APIVERSION = 8;
81 const int MAX_APPID = 10;
82 const char PACKAGE_PATH_FORMAT2[] = "/opt/apps/0000000000/";
83 const char PATH_ROOT[] = "/opt/apps/";
84 const char APPINFO_FILE_PATH[] = "info/version.info";
85 const char COMPAT_FILE_PATH[] = "info/compat.info";
86 const char TYPE_FILE_PATH[] = "info/type.info";
89 _AppInfo::_AppInfo(void)
90 : __appState(TERMINATED)
91 , __appType(_APP_TYPE_UI_APP)
93 , __appHandlerType(_APP_HANDLER_NONE)
94 , __parentWindowHandle(-1)
95 , __apiVersion(_API_VERSION_2_1)
100 , __isPackageInfoInitialized(false)
101 , __isOspCompat(false)
104 SysStaticAssert(sizeof(pid_t) == sizeof(int));
106 //FBase_Initialize();
109 _AppInfo::~_AppInfo(void)
112 delete __pAppVersion;
116 _AppInfo::GetAppInfo(void)
118 static _AppInfo info;
125 _AppInfo::Construct(const char* appId, const char* exeName, int argc, char* argv[])
127 SysAssertf(appId != null, "Valid appId required to launch application.");
131 __appExecutableName = exeName;
134 if (__appExecutableName == L"_AppControl")
136 SysLog(NID_APP, "Handling for submode.");
137 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
140 __appExecutableName = name;
141 SysLog(NID_APP, "Executable name is changed to %ls.", __appExecutableName.GetPointer());
144 __appId = __packageId + L'.' + __appExecutableName;
146 result r = E_SUCCESS;
150 const int len = strlen(PACKAGE_PATH_FORMAT2);
151 char appInfoPath[len + 1];
152 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
153 appInfoPath[len] = '\0';
155 // due to possible dependency problem, FIoFile is not used
156 // app root path first
158 strncpy(appInfoPath + strlen(PATH_ROOT), appId, MAX_APPID);
160 // app root directory file descriptor
161 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
163 __appRootPath = appInfoPath;
165 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
167 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
170 pFile = fdopen(fd, "r");
171 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
173 char apiVersion[MAX_APIVERSION] = {0, };
174 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
175 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
178 // fd is closed when the stream is closed by fclose();
180 __apiVersion = GetApiVersionFromStr(apiVersion);
185 // to reduce package manager overhead, libc API is used
186 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
188 SysLog(NID_APP, "OSP compatibility mode on.");
189 __isOspCompat = true;
192 // type file may does not exist
193 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
196 pFile = fdopen(fd, "r");
200 int line = fscanf(pFile, "%d", &i);
203 // fd is closed when the stream is closed by fclose();
205 __appType = _APP_TYPE_UI_APP | i;
206 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
210 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
211 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __apiVersion);
218 __appExecutableName.Clear();
223 delete __pAppVersion;
224 __pAppVersion = null;
235 // initialize app context only
237 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
239 SysAssertf(appId != null, "Valid appId required to launch application.");
246 if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
248 __appId.SubString(index + 1, __appExecutableName);
249 __appId.SubString(0, index, __packageId);
253 __appExecutableName = __appId;
254 __packageId = __appId;
257 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
258 __apiVersion = _API_VERSION_2_1;
261 __appRootPath = appRoot;
262 if (__appRootPath[__appRootPath.GetLength()] != L'/')
264 __appRootPath.Append(L'/');
267 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(), __apiVersion);
274 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
276 _PackageManagerImpl* pPkg = null;
277 pPkg = _PackageManagerImpl::GetInstance();
278 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
280 result r = E_SUCCESS;
281 PackageInfo* pInfo = null;
282 pInfo = pPkg->GetPackageInfoN(packageId);
283 SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
285 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
286 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
289 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
290 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
292 delete __pAppVersion;
293 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
294 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
296 __isPackageInfoInitialized = true;
300 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
310 delete __pAppVersion;
311 __pAppVersion = null;
317 // [FIXME] refactoring to use hash
319 _AppInfo::GetApiVersionFromStr(const char* pVer)
326 return _API_VERSION_2_1;
328 else if (ver == L"2.0")
330 return _API_VERSION_2_0;
332 else if (ver == L"1.2")
334 return _API_VERSION_1_2;
336 else if (ver == L"1.1")
338 return _API_VERSION_1_1;
340 else if (ver == L"1.0.2")
342 return _API_VERSION_1_0_2;
344 else if (ver == L"1.0")
346 return _API_VERSION_1_0;
350 return _API_VERSION_2_1;
356 _AppInfo::GetApiVersion(void)
358 return GetAppInfo()->__apiVersion;
363 _AppInfo::IsOspCompat(void)
365 return GetAppInfo()->__isOspCompat;
370 _AppInfo::SetApiVersion(_ApiVersion v)
372 GetAppInfo()->__apiVersion = v;
378 _AppInfo::GetProcessId(void)
380 static int processId = static_cast<int>(getpid());
386 _AppInfo::GetAppRootDirFd(void)
388 return GetAppInfo()->__appRootDirFd;
393 _AppInfo::GetApplicationId(void)
395 const String& appId = GetAppInfo()->__appId;
397 SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
404 _AppInfo::GetPackageId(void)
406 const String& packageId = GetAppInfo()->__packageId;
408 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
415 _AppInfo::GetAppExecutableName(void)
417 return GetAppInfo()->__appExecutableName;
422 _AppInfo::IsSubMode(void)
424 return GetAppInfo()->__isSubMode;
429 _AppInfo::GetAppName(void)
431 if (!GetAppInfo()->__isPackageInfoInitialized)
433 const String& packageId = GetAppInfo()->__packageId;
435 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
437 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
438 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
441 return *(GetAppInfo()->__pAppName);
446 _AppInfo::GetAppRootPath(void)
448 return GetAppInfo()->__appRootPath;
452 _AppInfo::GetAppVersion(void)
454 if (!GetAppInfo()->__isPackageInfoInitialized)
456 const String& packageId = GetAppInfo()->__packageId;
458 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
460 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
461 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
464 return *(GetAppInfo()->__pAppVersion);
469 _AppInfo::GetAppState(void)
471 return GetAppInfo()->__appState;
476 _AppInfo::SetAppState(AppState appState)
478 GetAppInfo()->__appState = appState;
483 _AppInfo::GetAppType(void)
485 return GetAppInfo()->__appType;
490 _AppInfo::SetAppType(_AppType appType)
492 GetAppInfo()->__appType |= appType;
497 _AppInfo::GetArgc(void)
499 return GetAppInfo()->__argc;
504 _AppInfo::GetArgv(void)
506 return GetAppInfo()->__pArgv;
511 _AppInfo::GetAppHandlerType(void)
513 return GetAppInfo()->__appHandlerType;
518 _AppInfo::SetAppHandlerType(int appHandlerType)
520 GetAppInfo()->__appHandlerType = appHandlerType;
525 _AppInfo::GetParentWindowHandle(void)
527 return GetAppInfo()->__parentWindowHandle;
532 _AppInfo::SetParentWindowHandle(unsigned int handle)
534 GetAppInfo()->__parentWindowHandle = handle;
539 _AppInfo::UpdatePackageInfo(bool update)
541 GetAppInfo()->__isPackageInfoInitialized = !update;