2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FApp_AppInfo.cpp
19 * @brief This is the implementation for the _AppInfo.cpp class.
29 #include <FBaseErrors.h>
30 #include <FAppPkgPackageInfo.h>
32 #include <FBaseSysLog.h>
33 #include <FBaseRt_Process.h>
35 #include "FAppPkg_PackageInfoImpl.h"
36 #include "FApp_AppInfo.h"
38 #include "FApp_AppArg.h"
40 using namespace Tizen::App::Package;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Runtime;
47 InitAppInfo(const char* packageId, const char* svcId, int argc, char* pArgv[], int fd)
49 result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(packageId, svcId, argc, pArgv);
52 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", packageId);
53 fprintf(stderr, "Application initialization failure for %s.\n", packageId);
60 InitWebAppInfo(const char* appId, const char* rootPath)
62 result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(appId, rootPath, Tizen::App::_APP_TYPE_WEB_APP);
65 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
66 fprintf(stderr, "Application initialization failure for %s.\n", appId);
72 extern void FBase_Initialize(void);
76 namespace Tizen { namespace App
79 const int MAX_APIVERSION = 8;
80 const int MAX_APPID = 10;
81 const char PACKAGE_PATH_FORMAT[] = "/opt/usr/apps/0000000000/";
82 const char PACKAGE_PATH_FORMAT2[] = "/opt/apps/0000000000/";
83 const char PATH_ROOT[] = "/opt/usr/apps/";
84 const char PATH_ROOT2[] = "/opt/apps/";
85 const char APPINFO_FILE_PATH[] = "info/version.info";
86 const char COMPAT_FILE_PATH[] = "info/compat.info";
87 const char VIRTUAL_ROOT_FILE_PATH[] = "info/virtual.info";
88 const char TYPE_FILE_PATH[] = "info/type.info";
91 _AppInfo::_AppInfo(void)
92 : __appState(TERMINATED)
93 , __appType(_APP_TYPE_NONE)
95 , __appHandlerType(_APP_HANDLER_NONE)
96 , __parentWindowHandle(-1)
97 , __apiVersion(_API_VERSION_2_2)
102 , __isPackageInfoInitialized(false)
103 , __isOspCompat(false)
105 , __isVirtualRoot(false)
107 SysStaticAssert(sizeof(pid_t) == sizeof(int));
109 //FBase_Initialize();
112 _AppInfo::~_AppInfo(void)
115 delete __pAppVersion;
117 // closing may or may not succeed
118 close(__appRootDirFd);
122 _AppInfo::GetAppInfo(void)
124 static _AppInfo info;
131 _AppInfo::Construct(const char* packageId, const char* exeName, int argc, char* argv[])
133 SysAssertf(packageId != null, "Valid appId required to launch application.");
137 __appExecutableName = exeName;
138 __packageId = packageId;
140 if (__appExecutableName == L"_AppControl")
142 SysLog(NID_APP, "Handling for submode.");
143 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
146 __appExecutableName = name;
147 SysLog(NID_APP, "Executable name is changed to %ls.", __appExecutableName.GetPointer());
150 __appId = __packageId + L'.' + __appExecutableName;
152 result r = E_SUCCESS;
156 char appInfoPath[PATH_MAX] = {0, };
158 const int len = strlen(PACKAGE_PATH_FORMAT2);
159 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
160 appInfoPath[len] = '\0';
162 // due to possible dependency problem, FIoFile is not used
163 // app root path first
165 strncpy(appInfoPath + strlen(PATH_ROOT2), packageId, MAX_APPID);
167 // [FIXME] temporary code for directory location migration
168 int len = strlen(PACKAGE_PATH_FORMAT2);
169 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
170 appInfoPath[len] = '\0';
171 strncpy(appInfoPath + strlen(PATH_ROOT2), packageId, MAX_APPID);
173 if (euidaccess(appInfoPath, R_OK) != 0)
175 len = strlen(PACKAGE_PATH_FORMAT);
176 strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
177 appInfoPath[len] = '\0';
179 strncpy(appInfoPath + strlen(PATH_ROOT), packageId, MAX_APPID);
182 // app root directory file descriptor
183 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
185 __appRootPath = appInfoPath;
187 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
189 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
190 SysAssertf(fd != -1, "Failed to open info file, errno: %d (%s)", errno, strerror(errno));
192 pFile = fdopen(fd, "r");
193 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
195 char apiVersion[MAX_APIVERSION] = {0, };
196 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
197 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
200 // fd is closed when the stream is closed by fclose();
202 __apiVersion = GetApiVersionFromStr(apiVersion);
207 // to reduce package manager overhead, libc API is used
208 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
210 SysLog(NID_APP, "OSP compatibility mode on.");
211 __isOspCompat = true;
214 if (faccessat(__appRootDirFd, VIRTUAL_ROOT_FILE_PATH, F_OK, 0) == 0)
216 SysLog(NID_APP, "virtual root mode on.");
217 __isVirtualRoot = true;
220 // type file may does not exist
221 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
224 pFile = fdopen(fd, "r");
228 int line = fscanf(pFile, "%d", &i);
231 // fd is closed when the stream is closed by fclose();
233 __appType = _APP_TYPE_UI_APP | i;
234 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
238 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
239 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __apiVersion);
246 __appExecutableName.Clear();
251 delete __pAppVersion;
252 __pAppVersion = null;
263 // initialize app context only
265 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
267 SysAssertf(appId != null, "Valid appId required to launch application.");
274 if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
276 __appId.SubString(index + 1, __appExecutableName);
277 __appId.SubString(0, index, __packageId);
281 __appExecutableName = __appId;
282 __packageId = __appId;
285 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
286 __apiVersion = _API_VERSION_2_1;
289 __appRootPath = appRoot;
290 if (__appRootPath[__appRootPath.GetLength()] != L'/')
292 __appRootPath.Append(L'/');
295 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(), __apiVersion);
302 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
304 _PackageManagerImpl* pPkg = null;
305 pPkg = _PackageManagerImpl::GetInstance();
306 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
308 result r = E_SUCCESS;
309 PackageInfo* pInfo = null;
310 pInfo = pPkg->GetPackageInfoN(packageId);
311 SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
313 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
314 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
317 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
318 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
320 delete __pAppVersion;
321 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
322 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
324 __isPackageInfoInitialized = true;
328 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
338 delete __pAppVersion;
339 __pAppVersion = null;
345 // [FIXME] refactoring to use hash
347 _AppInfo::GetApiVersionFromStr(const char* pVer)
354 return _API_VERSION_2_2;
356 else if (ver == L"2.1")
358 return _API_VERSION_2_1;
360 else if (ver == L"2.0")
362 return _API_VERSION_2_0;
364 else if (ver == L"1.2")
366 return _API_VERSION_1_2;
368 else if (ver == L"1.1")
370 return _API_VERSION_1_1;
372 else if (ver == L"1.0.2")
374 return _API_VERSION_1_0_2;
376 else if (ver == L"1.0")
378 return _API_VERSION_1_0;
382 return _API_VERSION_2_2;
388 _AppInfo::GetApiVersion(void)
390 return GetAppInfo()->__apiVersion;
395 _AppInfo::IsOspCompat(void)
397 return GetAppInfo()->__isOspCompat;
401 _AppInfo::IsVirtualRoot(void)
403 return GetAppInfo()->__isVirtualRoot;
407 _AppInfo::SetApiVersion(_ApiVersion v)
409 GetAppInfo()->__apiVersion = v;
415 _AppInfo::GetProcessId(void)
417 static int processId = static_cast<int>(getpid());
423 _AppInfo::GetAppRootDirFd(void)
425 return GetAppInfo()->__appRootDirFd;
430 _AppInfo::GetApplicationId(void)
432 const String& appId = GetAppInfo()->__appId;
434 SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
441 _AppInfo::GetPackageId(void)
443 const String& packageId = GetAppInfo()->__packageId;
445 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
452 _AppInfo::GetAppExecutableName(void)
454 return GetAppInfo()->__appExecutableName;
459 _AppInfo::IsSubMode(void)
461 return GetAppInfo()->__isSubMode;
466 _AppInfo::GetAppName(void)
468 if (!GetAppInfo()->__isPackageInfoInitialized)
470 const String& packageId = GetAppInfo()->__packageId;
472 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
474 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
475 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
478 return *(GetAppInfo()->__pAppName);
483 _AppInfo::GetAppRootPath(void)
485 return GetAppInfo()->__appRootPath;
489 _AppInfo::GetAppVersion(void)
491 if (!GetAppInfo()->__isPackageInfoInitialized)
493 const String& packageId = GetAppInfo()->__packageId;
495 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
497 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
498 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
501 return *(GetAppInfo()->__pAppVersion);
506 _AppInfo::GetAppState(void)
508 return GetAppInfo()->__appState;
513 _AppInfo::SetAppState(AppState appState)
515 GetAppInfo()->__appState = appState;
520 _AppInfo::GetAppType(void)
522 return GetAppInfo()->__appType;
527 _AppInfo::SetAppType(_AppType appType)
529 GetAppInfo()->__appType |= appType;
534 _AppInfo::GetArgc(void)
536 return GetAppInfo()->__argc;
541 _AppInfo::GetArgv(void)
543 return GetAppInfo()->__pArgv;
548 _AppInfo::GetAppHandlerType(void)
550 return GetAppInfo()->__appHandlerType;
555 _AppInfo::SetAppHandlerType(int appHandlerType)
557 GetAppInfo()->__appHandlerType = appHandlerType;
562 _AppInfo::GetParentWindowHandle(void)
564 return GetAppInfo()->__parentWindowHandle;
569 _AppInfo::SetParentWindowHandle(unsigned int handle)
571 GetAppInfo()->__parentWindowHandle = handle;
576 _AppInfo::UpdatePackageInfo(bool update)
578 GetAppInfo()->__isPackageInfoInitialized = !update;