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* appId, const char* svcId, int argc, char* pArgv[], int fd)
49 result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(appId, svcId, argc, pArgv);
52 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
53 fprintf(stderr, "Application initialization failure for %s.\n", appId);
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 TYPE_FILE_PATH[] = "info/type.info";
90 _AppInfo::_AppInfo(void)
91 : __appState(TERMINATED)
92 , __appType(_APP_TYPE_UI_APP)
94 , __appHandlerType(_APP_HANDLER_NONE)
95 , __parentWindowHandle(-1)
96 , __apiVersion(_API_VERSION_2_1)
101 , __isPackageInfoInitialized(false)
102 , __isOspCompat(false)
105 SysStaticAssert(sizeof(pid_t) == sizeof(int));
107 //FBase_Initialize();
110 _AppInfo::~_AppInfo(void)
113 delete __pAppVersion;
115 // closing may or may not succeed
116 close(__appRootDirFd);
120 _AppInfo::GetAppInfo(void)
122 static _AppInfo info;
129 _AppInfo::Construct(const char* appId, const char* exeName, int argc, char* argv[])
131 SysAssertf(appId != null, "Valid appId required to launch application.");
135 __appExecutableName = exeName;
138 if (__appExecutableName == L"_AppControl")
140 SysLog(NID_APP, "Handling for submode.");
141 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
144 __appExecutableName = name;
145 SysLog(NID_APP, "Executable name is changed to %ls.", __appExecutableName.GetPointer());
148 __appId = __packageId + L'.' + __appExecutableName;
150 result r = E_SUCCESS;
154 char appInfoPath[PATH_MAX] = {0, };
156 const int len = strlen(PACKAGE_PATH_FORMAT2);
157 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
158 appInfoPath[len] = '\0';
160 // due to possible dependency problem, FIoFile is not used
161 // app root path first
163 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
165 // [FIXME] temporary code for directory location migration
166 int len = strlen(PACKAGE_PATH_FORMAT2);
167 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
168 appInfoPath[len] = '\0';
169 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
171 if (euidaccess(appInfoPath, R_OK) != 0)
173 len = strlen(PACKAGE_PATH_FORMAT);
174 strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
175 appInfoPath[len] = '\0';
177 strncpy(appInfoPath + strlen(PATH_ROOT), appId, MAX_APPID);
180 // app root directory file descriptor
181 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
183 __appRootPath = appInfoPath;
185 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
187 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
190 pFile = fdopen(fd, "r");
191 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
193 char apiVersion[MAX_APIVERSION] = {0, };
194 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
195 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
198 // fd is closed when the stream is closed by fclose();
200 __apiVersion = GetApiVersionFromStr(apiVersion);
205 // to reduce package manager overhead, libc API is used
206 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
208 SysLog(NID_APP, "OSP compatibility mode on.");
209 __isOspCompat = true;
212 // type file may does not exist
213 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
216 pFile = fdopen(fd, "r");
220 int line = fscanf(pFile, "%d", &i);
223 // fd is closed when the stream is closed by fclose();
225 __appType = _APP_TYPE_UI_APP | i;
226 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
230 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
231 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __apiVersion);
238 __appExecutableName.Clear();
243 delete __pAppVersion;
244 __pAppVersion = null;
255 // initialize app context only
257 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
259 SysAssertf(appId != null, "Valid appId required to launch application.");
266 if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
268 __appId.SubString(index + 1, __appExecutableName);
269 __appId.SubString(0, index, __packageId);
273 __appExecutableName = __appId;
274 __packageId = __appId;
277 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
278 __apiVersion = _API_VERSION_2_1;
281 __appRootPath = appRoot;
282 if (__appRootPath[__appRootPath.GetLength()] != L'/')
284 __appRootPath.Append(L'/');
287 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(), __apiVersion);
294 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
296 _PackageManagerImpl* pPkg = null;
297 pPkg = _PackageManagerImpl::GetInstance();
298 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
300 result r = E_SUCCESS;
301 PackageInfo* pInfo = null;
302 pInfo = pPkg->GetPackageInfoN(packageId);
303 SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
305 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
306 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
309 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
310 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
312 delete __pAppVersion;
313 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
314 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
316 __isPackageInfoInitialized = true;
320 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
330 delete __pAppVersion;
331 __pAppVersion = null;
337 // [FIXME] refactoring to use hash
339 _AppInfo::GetApiVersionFromStr(const char* pVer)
346 return _API_VERSION_2_1;
348 else if (ver == L"2.0")
350 return _API_VERSION_2_0;
352 else if (ver == L"1.2")
354 return _API_VERSION_1_2;
356 else if (ver == L"1.1")
358 return _API_VERSION_1_1;
360 else if (ver == L"1.0.2")
362 return _API_VERSION_1_0_2;
364 else if (ver == L"1.0")
366 return _API_VERSION_1_0;
370 return _API_VERSION_2_1;
376 _AppInfo::GetApiVersion(void)
378 return GetAppInfo()->__apiVersion;
383 _AppInfo::IsOspCompat(void)
385 return GetAppInfo()->__isOspCompat;
390 _AppInfo::SetApiVersion(_ApiVersion v)
392 GetAppInfo()->__apiVersion = v;
398 _AppInfo::GetProcessId(void)
400 static int processId = static_cast<int>(getpid());
406 _AppInfo::GetAppRootDirFd(void)
408 return GetAppInfo()->__appRootDirFd;
413 _AppInfo::GetApplicationId(void)
415 const String& appId = GetAppInfo()->__appId;
417 SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
424 _AppInfo::GetPackageId(void)
426 const String& packageId = GetAppInfo()->__packageId;
428 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
435 _AppInfo::GetAppExecutableName(void)
437 return GetAppInfo()->__appExecutableName;
442 _AppInfo::IsSubMode(void)
444 return GetAppInfo()->__isSubMode;
449 _AppInfo::GetAppName(void)
451 if (!GetAppInfo()->__isPackageInfoInitialized)
453 const String& packageId = GetAppInfo()->__packageId;
455 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
457 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
458 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
461 return *(GetAppInfo()->__pAppName);
466 _AppInfo::GetAppRootPath(void)
468 return GetAppInfo()->__appRootPath;
472 _AppInfo::GetAppVersion(void)
474 if (!GetAppInfo()->__isPackageInfoInitialized)
476 const String& packageId = GetAppInfo()->__packageId;
478 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
480 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
481 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
484 return *(GetAppInfo()->__pAppVersion);
489 _AppInfo::GetAppState(void)
491 return GetAppInfo()->__appState;
496 _AppInfo::SetAppState(AppState appState)
498 GetAppInfo()->__appState = appState;
503 _AppInfo::GetAppType(void)
505 return GetAppInfo()->__appType;
510 _AppInfo::SetAppType(_AppType appType)
512 GetAppInfo()->__appType |= appType;
517 _AppInfo::GetArgc(void)
519 return GetAppInfo()->__argc;
524 _AppInfo::GetArgv(void)
526 return GetAppInfo()->__pArgv;
531 _AppInfo::GetAppHandlerType(void)
533 return GetAppInfo()->__appHandlerType;
538 _AppInfo::SetAppHandlerType(int appHandlerType)
540 GetAppInfo()->__appHandlerType = appHandlerType;
545 _AppInfo::GetParentWindowHandle(void)
547 return GetAppInfo()->__parentWindowHandle;
552 _AppInfo::SetParentWindowHandle(unsigned int handle)
554 GetAppInfo()->__parentWindowHandle = handle;
559 _AppInfo::UpdatePackageInfo(bool update)
561 GetAppInfo()->__isPackageInfoInitialized = !update;