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;
118 _AppInfo::GetAppInfo(void)
120 static _AppInfo info;
127 _AppInfo::Construct(const char* appId, const char* exeName, int argc, char* argv[])
129 SysAssertf(appId != null, "Valid appId required to launch application.");
133 __appExecutableName = exeName;
136 if (__appExecutableName == L"_AppControl")
138 SysLog(NID_APP, "Handling for submode.");
139 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
142 __appExecutableName = name;
143 SysLog(NID_APP, "Executable name is changed to %ls.", __appExecutableName.GetPointer());
146 __appId = __packageId + L'.' + __appExecutableName;
148 result r = E_SUCCESS;
152 char appInfoPath[PATH_MAX] = {0, };
154 const int len = strlen(PACKAGE_PATH_FORMAT2);
155 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
156 appInfoPath[len] = '\0';
158 // due to possible dependency problem, FIoFile is not used
159 // app root path first
161 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
163 // [FIXME] temporary code for directory location migration
164 int len = strlen(PACKAGE_PATH_FORMAT2);
165 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
166 appInfoPath[len] = '\0';
167 strncpy(appInfoPath + strlen(PATH_ROOT2), appId, MAX_APPID);
169 if (access(appInfoPath, R_OK) != 0)
171 len = strlen(PACKAGE_PATH_FORMAT);
172 strncpy(appInfoPath, PACKAGE_PATH_FORMAT, len);
173 appInfoPath[len] = '\0';
175 strncpy(appInfoPath + strlen(PATH_ROOT), appId, MAX_APPID);
178 // app root directory file descriptor
179 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
181 __appRootPath = appInfoPath;
183 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
185 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
188 pFile = fdopen(fd, "r");
189 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
191 char apiVersion[MAX_APIVERSION] = {0, };
192 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
193 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
196 // fd is closed when the stream is closed by fclose();
198 __apiVersion = GetApiVersionFromStr(apiVersion);
203 // to reduce package manager overhead, libc API is used
204 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
206 SysLog(NID_APP, "OSP compatibility mode on.");
207 __isOspCompat = true;
210 // type file may does not exist
211 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
214 pFile = fdopen(fd, "r");
218 int line = fscanf(pFile, "%d", &i);
221 // fd is closed when the stream is closed by fclose();
223 __appType = _APP_TYPE_UI_APP | i;
224 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
228 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
229 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __apiVersion);
236 __appExecutableName.Clear();
241 delete __pAppVersion;
242 __pAppVersion = null;
253 // initialize app context only
255 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
257 SysAssertf(appId != null, "Valid appId required to launch application.");
264 if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
266 __appId.SubString(index + 1, __appExecutableName);
267 __appId.SubString(0, index, __packageId);
271 __appExecutableName = __appId;
272 __packageId = __appId;
275 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
276 __apiVersion = _API_VERSION_2_1;
279 __appRootPath = appRoot;
280 if (__appRootPath[__appRootPath.GetLength()] != L'/')
282 __appRootPath.Append(L'/');
285 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(), __apiVersion);
292 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
294 _PackageManagerImpl* pPkg = null;
295 pPkg = _PackageManagerImpl::GetInstance();
296 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
298 result r = E_SUCCESS;
299 PackageInfo* pInfo = null;
300 pInfo = pPkg->GetPackageInfoN(packageId);
301 SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
303 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
304 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
307 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
308 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
310 delete __pAppVersion;
311 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
312 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
314 __isPackageInfoInitialized = true;
318 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
328 delete __pAppVersion;
329 __pAppVersion = null;
335 // [FIXME] refactoring to use hash
337 _AppInfo::GetApiVersionFromStr(const char* pVer)
344 return _API_VERSION_2_1;
346 else if (ver == L"2.0")
348 return _API_VERSION_2_0;
350 else if (ver == L"1.2")
352 return _API_VERSION_1_2;
354 else if (ver == L"1.1")
356 return _API_VERSION_1_1;
358 else if (ver == L"1.0.2")
360 return _API_VERSION_1_0_2;
362 else if (ver == L"1.0")
364 return _API_VERSION_1_0;
368 return _API_VERSION_2_1;
374 _AppInfo::GetApiVersion(void)
376 return GetAppInfo()->__apiVersion;
381 _AppInfo::IsOspCompat(void)
383 return GetAppInfo()->__isOspCompat;
388 _AppInfo::SetApiVersion(_ApiVersion v)
390 GetAppInfo()->__apiVersion = v;
396 _AppInfo::GetProcessId(void)
398 static int processId = static_cast<int>(getpid());
404 _AppInfo::GetAppRootDirFd(void)
406 return GetAppInfo()->__appRootDirFd;
411 _AppInfo::GetApplicationId(void)
413 const String& appId = GetAppInfo()->__appId;
415 SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
422 _AppInfo::GetPackageId(void)
424 const String& packageId = GetAppInfo()->__packageId;
426 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
433 _AppInfo::GetAppExecutableName(void)
435 return GetAppInfo()->__appExecutableName;
440 _AppInfo::IsSubMode(void)
442 return GetAppInfo()->__isSubMode;
447 _AppInfo::GetAppName(void)
449 if (!GetAppInfo()->__isPackageInfoInitialized)
451 const String& packageId = GetAppInfo()->__packageId;
453 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
455 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
456 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
459 return *(GetAppInfo()->__pAppName);
464 _AppInfo::GetAppRootPath(void)
466 return GetAppInfo()->__appRootPath;
470 _AppInfo::GetAppVersion(void)
472 if (!GetAppInfo()->__isPackageInfoInitialized)
474 const String& packageId = GetAppInfo()->__packageId;
476 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
478 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
479 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
482 return *(GetAppInfo()->__pAppVersion);
487 _AppInfo::GetAppState(void)
489 return GetAppInfo()->__appState;
494 _AppInfo::SetAppState(AppState appState)
496 GetAppInfo()->__appState = appState;
501 _AppInfo::GetAppType(void)
503 return GetAppInfo()->__appType;
508 _AppInfo::SetAppType(_AppType appType)
510 GetAppInfo()->__appType |= appType;
515 _AppInfo::GetArgc(void)
517 return GetAppInfo()->__argc;
522 _AppInfo::GetArgv(void)
524 return GetAppInfo()->__pArgv;
529 _AppInfo::GetAppHandlerType(void)
531 return GetAppInfo()->__appHandlerType;
536 _AppInfo::SetAppHandlerType(int appHandlerType)
538 GetAppInfo()->__appHandlerType = appHandlerType;
543 _AppInfo::GetParentWindowHandle(void)
545 return GetAppInfo()->__parentWindowHandle;
550 _AppInfo::SetParentWindowHandle(unsigned int handle)
552 GetAppInfo()->__parentWindowHandle = handle;
557 _AppInfo::UpdatePackageInfo(bool update)
559 GetAppInfo()->__isPackageInfoInitialized = !update;