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.
28 #include <unique_ptr.h>
32 #include <FBaseErrors.h>
33 #include <FAppPkgPackageInfo.h>
34 #include <FBaseSysLog.h>
36 #include <FBaseRt_Process.h>
37 #include <FBase_StringConverter.h>
39 #include "FAppPkg_PackageInfoImpl.h"
40 #include "FApp_AppInfo.h"
42 using namespace Tizen::App::Package;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Runtime;
51 result r = Tizen::App::_AppInfo::GetInstance()->Construct();
54 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure.");
55 fprintf(stderr, "Application initialization failure.\n");
62 InitAppInfo(const char* packageId, const char* svcId, int argc, char* pArgv[], int fd)
68 InitWebAppInfo(const char* appId, const char* rootPath)
70 result r = Tizen::App::_AppInfo::GetInstance()->Construct(appId, rootPath, Tizen::App::_APP_TYPE_WEB_APP);
73 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
74 fprintf(stderr, "Application initialization failure for %s.\n", appId);
80 extern void FBase_Initialize(void);
84 namespace Tizen { namespace App
87 const int MAX_APIVERSION = 8;
88 const int MAX_APPID = 10;
89 const char PACKAGE_PATH_FORMAT[] = "/opt/usr/apps/0000000000/";
90 const char PATH_ROOT[] = "/opt/usr/apps/";
91 const char APPINFO_FILE_PATH[] = "info/version.info";
92 const char COMPAT_FILE_PATH[] = "info/compat.info";
93 const char VIRTUAL_ROOT_FILE_PATH[] = "info/virtual.info";
94 const char TYPE_FILE_PATH[] = "info/type.info";
95 const char SUBMODE_NAME[] = "_AppControl";
98 _AppInfo::_AppInfo(void)
99 : __appState(TERMINATED)
100 , __appType(_APP_TYPE_NONE)
102 , __appHandlerType(_APP_HANDLER_NONE)
103 , __parentWindowHandle(-1)
105 , __pAppVersion(null)
106 , __isPackageInfoInitialized(false)
108 , __isVirtualRoot(false)
109 , __isMultiWindow(false)
111 SysStaticAssert(sizeof(pid_t) == sizeof(int));
113 //FBase_Initialize();
116 _AppInfo::~_AppInfo(void)
119 delete __pAppVersion;
121 // closing may or may not succeed
122 close(__appRootDirFd);
126 _AppInfo::GetInstance(void)
128 static _AppInfo info;
135 _AppInfo::Construct(void)
137 SysAssertf(appinfo_is_initialized() == 1, "appinfo is not initialized.");
141 const char* pPackageId = appinfo_get_packageid();
142 const char* pExecName = appinfo_get_execname();
145 if (strncmp(pExecName, SUBMODE_NAME, strlen(SUBMODE_NAME)) == 0)
147 SysLog(NID_APP, "Handling for submode.");
148 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(String(pPackageId));
152 std::unique_ptr<char[]> pActualExec(_StringConverter::CopyToCharArrayN(name));
153 appinfo_update_submode_execname_and_appid(pActualExec.get());
154 SysLog(NID_APP, "Executable name is changed to %s.", pActualExec.get());
157 result r = E_SUCCESS;
161 char appInfoPath[PATH_MAX] = {0, };
163 int len = strlen(PACKAGE_PATH_FORMAT);
164 strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
165 appInfoPath[len] = '\0';
166 strncpy(appInfoPath + strlen(PATH_ROOT), pPackageId, MAX_APPID);
168 // app root directory file descriptor
169 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
170 __appRootPath = appInfoPath;
172 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
174 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
175 SysAssertf(fd != -1, "Failed to open info file, errno: %d (%s)", errno, strerror(errno));
177 pFile = fdopen(fd, "r");
178 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
180 char apiVersion[MAX_APIVERSION] = {0, };
181 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
182 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
185 // fd is closed when the stream is closed by fclose();
187 appinfo_set_api_version(appinfo_get_api_version_from_str(apiVersion));
189 // to reduce package manager overhead, libc API is used
190 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
192 SysLog(NID_APP, "OSP compatibility mode on.");
193 appinfo_set_compat(1);
196 if (faccessat(__appRootDirFd, VIRTUAL_ROOT_FILE_PATH, F_OK, 0) == 0)
198 SysLog(NID_APP, "virtual root mode on.");
199 __isVirtualRoot = true;
202 // type file may does not exist
203 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
206 pFile = fdopen(fd, "r");
210 int line = fscanf(pFile, "%d", &i);
213 // fd is closed when the stream is closed by fclose();
215 __appType = _APP_TYPE_UI_APP | i;
216 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
220 SysLog(NID_APP, "AppInfo [%s][%s.%s][%d].", appinfo_get_appid(), appinfo_get_packageid(), appinfo_get_execname(), appinfo_get_api_version());
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.");
243 if (appinfo_is_initialized() == 0)
245 appinfo_init(appId, 0);
248 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
249 appinfo_set_api_version(APP_INFO_VERSION_2_2);
252 __appRootPath = appRoot;
253 if (__appRootPath[__appRootPath.GetLength()] != L'/')
255 __appRootPath.Append(L'/');
258 SysLog(NID_APP, "AppInfo [%s][%s.%s][%ls][%d].", appinfo_get_appid(), appinfo_get_packageid(), appinfo_get_execname()), __appRootPath.GetPointer(), appinfo_get_api_version();
265 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
267 _PackageManagerImpl* pPkg = _PackageManagerImpl::GetInstance();
268 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
270 result r = E_SUCCESS;
271 std::unique_ptr<PackageInfo> pInfo(pPkg->GetPackageInfoN(packageId));
272 if (pInfo.get() == null)
275 SysPropagate(NID_APP, r);
279 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo.get());
280 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
283 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
284 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
286 delete __pAppVersion;
287 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
288 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
290 __isPackageInfoInitialized = true;
292 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
300 delete __pAppVersion;
301 __pAppVersion = null;
308 _AppInfo::GetApiVersion(void)
310 return static_cast<_ApiVersion>(appinfo_get_api_version());
315 _AppInfo::IsOspCompat(void)
317 return (appinfo_is_compat() == 1);
321 _AppInfo::IsVirtualRoot(void)
323 return GetInstance()->__isVirtualRoot;
327 _AppInfo::SetApiVersion(_ApiVersion v)
329 appinfo_set_api_version(static_cast<app_info_version_e>(v));
335 _AppInfo::GetProcessId(void)
337 static int processId = static_cast<int>(getpid());
343 _AppInfo::GetAppRootDirFd(void)
345 return GetInstance()->__appRootDirFd;
350 _AppInfo::GetApplicationId(void)
352 return String(appinfo_get_appid());
357 _AppInfo::GetPackageId(void)
359 return String(appinfo_get_packageid());
364 _AppInfo::GetAppExecutableName(void)
366 return String(appinfo_get_execname());
371 _AppInfo::IsSubMode(void)
373 return GetInstance()->__isSubMode;
378 _AppInfo::GetAppName(void)
380 if (!GetInstance()->__isPackageInfoInitialized)
382 const String& packageId = GetInstance()->GetPackageId();
384 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
386 result r = GetInstance()->UpdateAppInfoFromPackageInfo(packageId);
387 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
390 return *(GetInstance()->__pAppName);
395 _AppInfo::GetAppRootPath(void)
397 return GetInstance()->__appRootPath;
401 _AppInfo::GetAppVersion(void)
403 if (!GetInstance()->__isPackageInfoInitialized)
405 const String& packageId = GetInstance()->GetPackageId();
407 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
409 result r = GetInstance()->UpdateAppInfoFromPackageInfo(packageId);
410 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
413 return *(GetInstance()->__pAppVersion);
418 _AppInfo::GetAppState(void)
420 return GetInstance()->__appState;
425 _AppInfo::SetAppState(AppState appState)
427 GetInstance()->__appState = appState;
432 _AppInfo::GetAppType(void)
434 return GetInstance()->__appType;
439 _AppInfo::SetAppType(_AppType appType)
441 GetInstance()->__appType |= appType;
446 _AppInfo::GetAppHandlerType(void)
448 return GetInstance()->__appHandlerType;
453 _AppInfo::SetAppHandlerType(int appHandlerType)
455 GetInstance()->__appHandlerType = appHandlerType;
460 _AppInfo::GetParentWindowHandle(void)
462 return GetInstance()->__parentWindowHandle;
467 _AppInfo::SetParentWindowHandle(unsigned int handle)
469 GetInstance()->__parentWindowHandle = handle;
474 _AppInfo::UpdatePackageInfo(bool update)
476 GetInstance()->__isPackageInfoInitialized = !update;
480 _AppInfo::IsMultiWindow(void)
482 return GetInstance()->__isMultiWindow;
486 _AppInfo::SetMultiWindow(bool enable)
488 GetInstance()->__isMultiWindow = enable;