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.
31 #include <FBaseErrors.h>
32 #include <FAppPkgPackageInfo.h>
33 #include <FBaseSysLog.h>
35 #include <FBaseRt_Process.h>
36 #include <FBase_StringConverter.h>
38 #include "FAppPkg_PackageInfoImpl.h"
39 #include "FApp_AppInfo.h"
41 using namespace Tizen::App::Package;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Runtime;
48 InitAppInfo(const char* packageId, const char* svcId, int argc, char* pArgv[], int fd)
50 result r = Tizen::App::_AppInfo::GetAppInfo()->Construct(packageId, svcId, argc, pArgv);
53 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", packageId);
54 fprintf(stderr, "Application initialization failure for %s.\n", packageId);
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 PATH_ROOT[] = "/opt/usr/apps/";
84 const char APPINFO_FILE_PATH[] = "info/version.info";
85 const char COMPAT_FILE_PATH[] = "info/compat.info";
86 const char VIRTUAL_ROOT_FILE_PATH[] = "info/virtual.info";
87 const char TYPE_FILE_PATH[] = "info/type.info";
90 _AppInfo::_AppInfo(void)
91 : __appState(TERMINATED)
92 , __appType(_APP_TYPE_NONE)
94 , __appHandlerType(_APP_HANDLER_NONE)
95 , __parentWindowHandle(-1)
98 , __isPackageInfoInitialized(false)
100 , __isVirtualRoot(false)
102 SysStaticAssert(sizeof(pid_t) == sizeof(int));
104 //FBase_Initialize();
107 _AppInfo::~_AppInfo(void)
110 delete __pAppVersion;
112 // closing may or may not succeed
113 close(__appRootDirFd);
117 _AppInfo::GetAppInfo(void)
119 static _AppInfo info;
126 _AppInfo::Construct(void)
128 SysAssertf(appinfo_is_initialized() == 1, "appinfo is not initialized.");
137 _AppInfo::Construct(const char* packageId, const char* exeName, int argc, char* argv[])
139 SysAssertf(packageId != null, "Valid appId required to launch application.");
140 SysAssertf(appinfo_is_initialized() == 1, "appinfo is not initialized.");
144 __appExecutableName = exeName;
145 __packageId = packageId;
147 if (__appExecutableName == L"_AppControl")
149 SysLog(NID_APP, "Handling for submode.");
150 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
153 __appExecutableName = name;
155 std::unique_ptr<char[]> pActualExec(_StringConverter::CopyToCharArrayN(name));
156 appinfo_update_submode_execname_and_appid(pActualExec.get());
157 SysLog(NID_APP, "Executable name is changed to %s.", pActualExec.get());
160 __appId = __packageId + L'.' + __appExecutableName;
162 result r = E_SUCCESS;
166 char appInfoPath[PATH_MAX] = {0, };
168 int len = strlen(PACKAGE_PATH_FORMAT);
169 strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
170 appInfoPath[len] = '\0';
171 strncpy(appInfoPath + strlen(PATH_ROOT), packageId, MAX_APPID);
173 // app root directory file descriptor
174 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
175 __appRootPath = appInfoPath;
177 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
179 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
180 SysAssertf(fd != -1, "Failed to open info file, errno: %d (%s)", errno, strerror(errno));
182 pFile = fdopen(fd, "r");
183 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
185 char apiVersion[MAX_APIVERSION] = {0, };
186 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
187 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
190 // fd is closed when the stream is closed by fclose();
192 appinfo_set_api_version(appinfo_get_api_version_from_str(apiVersion));
194 // to reduce package manager overhead, libc API is used
195 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
197 SysLog(NID_APP, "OSP compatibility mode on.");
198 appinfo_set_compat(1);
201 if (faccessat(__appRootDirFd, VIRTUAL_ROOT_FILE_PATH, F_OK, 0) == 0)
203 SysLog(NID_APP, "virtual root mode on.");
204 __isVirtualRoot = true;
207 // type file may does not exist
208 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
211 pFile = fdopen(fd, "r");
215 int line = fscanf(pFile, "%d", &i);
218 // fd is closed when the stream is closed by fclose();
220 __appType = _APP_TYPE_UI_APP | i;
221 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
225 SysLog(NID_APP, "New appinfo [%s][%s.%s].", appinfo_get_appid(), appinfo_get_packageid(), appinfo_get_execname());
226 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
227 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), appinfo_get_api_version());
234 __appExecutableName.Clear();
239 delete __pAppVersion;
240 __pAppVersion = null;
251 // initialize app context only
253 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
255 SysAssertf(appId != null, "Valid appId required to launch application.");
262 if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
264 __appId.SubString(index + 1, __appExecutableName);
265 __appId.SubString(0, index, __packageId);
269 __appExecutableName = __appId;
270 __packageId = __appId;
273 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
274 appinfo_set_api_version(APP_INFO_VERSION_2_2);
277 __appRootPath = appRoot;
278 if (__appRootPath[__appRootPath.GetLength()] != L'/')
280 __appRootPath.Append(L'/');
283 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(), appinfo_get_api_version());
290 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
292 _PackageManagerImpl* pPkg = null;
293 pPkg = _PackageManagerImpl::GetInstance();
294 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
296 result r = E_SUCCESS;
297 PackageInfo* pInfo = null;
298 pInfo = pPkg->GetPackageInfoN(packageId);
299 SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
301 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
302 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
305 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
306 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
308 delete __pAppVersion;
309 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
310 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
312 __isPackageInfoInitialized = true;
316 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
326 delete __pAppVersion;
327 __pAppVersion = null;
334 _AppInfo::GetApiVersion(void)
336 return static_cast<_ApiVersion>(appinfo_get_api_version());
341 _AppInfo::IsOspCompat(void)
343 return (appinfo_is_compat() == 1);
347 _AppInfo::IsVirtualRoot(void)
349 return GetAppInfo()->__isVirtualRoot;
353 _AppInfo::SetApiVersion(_ApiVersion v)
355 appinfo_set_api_version(static_cast<app_info_version_e>(v));
361 _AppInfo::GetProcessId(void)
363 static int processId = static_cast<int>(getpid());
369 _AppInfo::GetAppRootDirFd(void)
371 return GetAppInfo()->__appRootDirFd;
376 _AppInfo::GetApplicationId(void)
378 const String& appId = GetAppInfo()->__appId;
380 SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
387 _AppInfo::GetPackageId(void)
389 const String& packageId = GetAppInfo()->__packageId;
391 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
398 _AppInfo::GetAppExecutableName(void)
400 return GetAppInfo()->__appExecutableName;
405 _AppInfo::IsSubMode(void)
407 return GetAppInfo()->__isSubMode;
412 _AppInfo::GetAppName(void)
414 if (!GetAppInfo()->__isPackageInfoInitialized)
416 const String& packageId = GetAppInfo()->__packageId;
418 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
420 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
421 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
424 return *(GetAppInfo()->__pAppName);
429 _AppInfo::GetAppRootPath(void)
431 return GetAppInfo()->__appRootPath;
435 _AppInfo::GetAppVersion(void)
437 if (!GetAppInfo()->__isPackageInfoInitialized)
439 const String& packageId = GetAppInfo()->__packageId;
441 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
443 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
444 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
447 return *(GetAppInfo()->__pAppVersion);
452 _AppInfo::GetAppState(void)
454 return GetAppInfo()->__appState;
459 _AppInfo::SetAppState(AppState appState)
461 GetAppInfo()->__appState = appState;
466 _AppInfo::GetAppType(void)
468 return GetAppInfo()->__appType;
473 _AppInfo::SetAppType(_AppType appType)
475 GetAppInfo()->__appType |= appType;
480 _AppInfo::GetAppHandlerType(void)
482 return GetAppInfo()->__appHandlerType;
487 _AppInfo::SetAppHandlerType(int appHandlerType)
489 GetAppInfo()->__appHandlerType = appHandlerType;
494 _AppInfo::GetParentWindowHandle(void)
496 return GetAppInfo()->__parentWindowHandle;
501 _AppInfo::SetParentWindowHandle(unsigned int handle)
503 GetAppInfo()->__parentWindowHandle = handle;
508 _AppInfo::UpdatePackageInfo(bool update)
510 GetAppInfo()->__isPackageInfoInitialized = !update;