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_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";
91 _AppInfo::_AppInfo(void)
92 : __appState(TERMINATED)
93 , __appType(_APP_TYPE_UI_APP)
95 , __appHandlerType(_APP_HANDLER_NONE)
96 , __parentWindowHandle(-1)
97 , __apiVersion(_API_VERSION_2_1)
102 , __isPackageInfoInitialized(false)
103 , __isOspCompat(false)
106 SysStaticAssert(sizeof(pid_t) == sizeof(int));
108 //FBase_Initialize();
111 _AppInfo::~_AppInfo(void)
114 delete __pAppVersion;
116 // closing may or may not succeed
117 close(__appRootDirFd);
121 _AppInfo::GetAppInfo(void)
123 static _AppInfo info;
130 _AppInfo::Construct(const char* appId, const char* exeName, int argc, char* argv[])
132 SysAssertf(appId != null, "Valid appId required to launch application.");
136 __appExecutableName = exeName;
139 if (__appExecutableName == L"_AppControl")
141 SysLog(NID_APP, "Handling for submode.");
142 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
145 __appExecutableName = name;
146 SysLog(NID_APP, "Executable name is changed to %ls.", __appExecutableName.GetPointer());
149 __appId = __packageId + L'.' + __appExecutableName;
151 result r = E_SUCCESS;
155 char appInfoPath[PATH_MAX] = {0, };
157 const int len = strlen(PACKAGE_PATH_FORMAT2);
158 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
159 appInfoPath[len] = '\0';
161 // due to possible dependency problem, FIoFile is not used
162 // app root path first
164 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
166 // [FIXME] temporary code for directory location migration
167 int len = strlen(PACKAGE_PATH_FORMAT2);
168 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
169 appInfoPath[len] = '\0';
170 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
172 if (access(appInfoPath, R_OK) != 0)
174 len = strlen(PACKAGE_PATH_FORMAT);
175 strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
176 appInfoPath[len] = '\0';
178 strncpy(appInfoPath + strlen(PATH_ROOT), appId, MAX_APPID);
181 // app root directory file descriptor
182 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
184 __appRootPath = appInfoPath;
186 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
188 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
191 pFile = fdopen(fd, "r");
192 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
194 char apiVersion[MAX_APIVERSION] = {0, };
195 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
196 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
199 // fd is closed when the stream is closed by fclose();
201 __apiVersion = GetApiVersionFromStr(apiVersion);
206 // to reduce package manager overhead, libc API is used
207 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
209 SysLog(NID_APP, "OSP compatibility mode on.");
210 __isOspCompat = true;
213 // type file may does not exist
214 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
217 pFile = fdopen(fd, "r");
221 int line = fscanf(pFile, "%d", &i);
224 // fd is closed when the stream is closed by fclose();
226 __appType = _APP_TYPE_UI_APP | i;
227 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
231 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
232 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __apiVersion);
239 __appExecutableName.Clear();
244 delete __pAppVersion;
245 __pAppVersion = null;
256 // initialize app context only
258 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
260 SysAssertf(appId != null, "Valid appId required to launch application.");
267 if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
269 __appId.SubString(index + 1, __appExecutableName);
270 __appId.SubString(0, index, __packageId);
274 __appExecutableName = __appId;
275 __packageId = __appId;
278 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
279 __apiVersion = _API_VERSION_2_1;
282 __appRootPath = appRoot;
283 if (__appRootPath[__appRootPath.GetLength()] != L'/')
285 __appRootPath.Append(L'/');
288 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(), __apiVersion);
295 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
297 _PackageManagerImpl* pPkg = null;
298 pPkg = _PackageManagerImpl::GetInstance();
299 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
301 result r = E_SUCCESS;
302 PackageInfo* pInfo = null;
303 pInfo = pPkg->GetPackageInfoN(packageId);
304 SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
306 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
307 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
310 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
311 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
313 delete __pAppVersion;
314 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
315 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
317 __isPackageInfoInitialized = true;
321 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
331 delete __pAppVersion;
332 __pAppVersion = null;
338 // [FIXME] refactoring to use hash
340 _AppInfo::GetApiVersionFromStr(const char* pVer)
347 return _API_VERSION_2_1;
349 else if (ver == L"2.0")
351 return _API_VERSION_2_0;
353 else if (ver == L"1.2")
355 return _API_VERSION_1_2;
357 else if (ver == L"1.1")
359 return _API_VERSION_1_1;
361 else if (ver == L"1.0.2")
363 return _API_VERSION_1_0_2;
365 else if (ver == L"1.0")
367 return _API_VERSION_1_0;
371 return _API_VERSION_2_1;
377 _AppInfo::GetApiVersion(void)
379 return GetAppInfo()->__apiVersion;
384 _AppInfo::IsOspCompat(void)
386 return GetAppInfo()->__isOspCompat;
391 _AppInfo::SetApiVersion(_ApiVersion v)
393 GetAppInfo()->__apiVersion = v;
399 _AppInfo::GetProcessId(void)
401 static int processId = static_cast<int>(getpid());
407 _AppInfo::GetAppRootDirFd(void)
409 return GetAppInfo()->__appRootDirFd;
414 _AppInfo::GetApplicationId(void)
416 const String& appId = GetAppInfo()->__appId;
418 SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
425 _AppInfo::GetPackageId(void)
427 const String& packageId = GetAppInfo()->__packageId;
429 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
436 _AppInfo::GetAppExecutableName(void)
438 return GetAppInfo()->__appExecutableName;
443 _AppInfo::IsSubMode(void)
445 return GetAppInfo()->__isSubMode;
450 _AppInfo::GetAppName(void)
452 if (!GetAppInfo()->__isPackageInfoInitialized)
454 const String& packageId = GetAppInfo()->__packageId;
456 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
458 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
459 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
462 return *(GetAppInfo()->__pAppName);
467 _AppInfo::GetAppRootPath(void)
469 return GetAppInfo()->__appRootPath;
473 _AppInfo::GetAppVersion(void)
475 if (!GetAppInfo()->__isPackageInfoInitialized)
477 const String& packageId = GetAppInfo()->__packageId;
479 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
481 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
482 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
485 return *(GetAppInfo()->__pAppVersion);
490 _AppInfo::GetAppState(void)
492 return GetAppInfo()->__appState;
497 _AppInfo::SetAppState(AppState appState)
499 GetAppInfo()->__appState = appState;
504 _AppInfo::GetAppType(void)
506 return GetAppInfo()->__appType;
511 _AppInfo::SetAppType(_AppType appType)
513 GetAppInfo()->__appType |= appType;
518 _AppInfo::GetArgc(void)
520 return GetAppInfo()->__argc;
525 _AppInfo::GetArgv(void)
527 return GetAppInfo()->__pArgv;
532 _AppInfo::GetAppHandlerType(void)
534 return GetAppInfo()->__appHandlerType;
539 _AppInfo::SetAppHandlerType(int appHandlerType)
541 GetAppInfo()->__appHandlerType = appHandlerType;
546 _AppInfo::GetParentWindowHandle(void)
548 return GetAppInfo()->__parentWindowHandle;
553 _AppInfo::SetParentWindowHandle(unsigned int handle)
555 GetAppInfo()->__parentWindowHandle = handle;
560 _AppInfo::UpdatePackageInfo(bool update)
562 GetAppInfo()->__isPackageInfoInitialized = !update;