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 APPINFO_FILE_PATH[] = "info/version.info";
83 const char PACKAGE_PATH_FORMAT2[] = "/opt/apps/0000000000/";
84 const char PATH_ROOT[] = "/opt/apps/";
85 const char COMPAT_FILE_PATH[] = "info/compat.info";
88 _AppInfo::_AppInfo(void)
89 : __appState(TERMINATED)
90 , __appType(_APP_TYPE_UI_APP)
92 , __appHandlerType(_APP_HANDLER_NONE)
93 , __parentWindowHandle(-1)
94 , __apiVersion(_API_VERSION_2_1)
99 , __isPackageInfoInitialized(false)
100 , __isOspCompat(false)
103 SysStaticAssert(sizeof(pid_t) == sizeof(int));
105 //FBase_Initialize();
108 _AppInfo::~_AppInfo(void)
111 delete __pAppVersion;
115 _AppInfo::GetAppInfo(void)
117 static _AppInfo info;
124 _AppInfo::Construct(const char* appId, const char* exeName, int argc, char* argv[])
126 SysAssertf(appId != null, "Valid appId required to launch application.");
130 __appExecutableName = exeName;
133 if (__appExecutableName == L"_AppControl")
135 SysLog(NID_APP, "Handling for submode.");
136 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(__packageId);
139 __appExecutableName = name;
140 SysLog(NID_APP, "Executable name is changed to %ls.", __appExecutableName.GetPointer());
143 __appId = __packageId + L'.' + __appExecutableName;
145 result r = E_SUCCESS;
149 const int len = strlen(PACKAGE_PATH_FORMAT2);
150 char appInfoPath[len + 1];
151 strncpy(appInfoPath, PACKAGE_PATH_FORMAT2, len);
152 appInfoPath[len] = '\0';
154 // due to possible dependency problem, FIoFile is not used
155 // app root path first
157 strncpy(appInfoPath + strlen(PATH_ROOT), appId, MAX_APPID);
159 // app root directory file descriptor
160 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
162 __appRootPath = appInfoPath;
164 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
166 int fd = openat(__appRootDirFd, APPINFO_FILE_PATH, O_RDONLY);
169 pFile = fdopen(fd, "r");
170 SysTryCatch(NID_APP, pFile != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Opening appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
172 char apiVersion[MAX_APIVERSION] = {0, };
173 char* pRet = fgets(apiVersion, MAX_APIVERSION - 1, pFile);
174 SysTryCatch(NID_APP, pRet != NULL, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Reading appinfo file (%s) failed : %s.", appInfoPath, strerror(errno));
177 // fd is closed when the stream is closed by fclose();
179 __apiVersion = GetApiVersionFromStr(apiVersion);
184 // to reduce package manager overhead, libc API is used
185 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
187 SysLog(NID_APP, "OSP compatibility mode on.");
188 __isOspCompat = true;
191 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%d].",
192 __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __apiVersion);
199 __appExecutableName.Clear();
204 delete __pAppVersion;
205 __pAppVersion = null;
216 // initialize app context only
218 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
220 SysAssertf(appId != null, "Valid appId required to launch application.");
227 if (__appId.LastIndexOf(L'.', __appId.GetLength() - 1, index) == E_SUCCESS)
229 __appId.SubString(index + 1, __appExecutableName);
230 __appId.SubString(0, index, __packageId);
234 __appExecutableName = __appId;
235 __packageId = __appId;
238 __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
239 __apiVersion = _API_VERSION_2_1;
242 __appRootPath = appRoot;
243 if (__appRootPath[__appRootPath.GetLength()] != L'/')
245 __appRootPath.Append(L'/');
248 SysLog(NID_APP, "AppInfo initialization finished [%ls][%ls.%ls][%ls][%d].", __appId.GetPointer(), __packageId.GetPointer(), __appExecutableName.GetPointer(), __appRootPath.GetPointer(), __apiVersion);
255 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
257 _PackageManagerImpl* pPkg = null;
258 pPkg = _PackageManagerImpl::GetInstance();
259 SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
261 result r = E_SUCCESS;
262 PackageInfo* pInfo = null;
263 pInfo = pPkg->GetPackageInfoN(packageId);
264 SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
266 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
267 SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
270 __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
271 SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
273 delete __pAppVersion;
274 __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
275 SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
277 __isPackageInfoInitialized = true;
281 SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
291 delete __pAppVersion;
292 __pAppVersion = null;
298 // [FIXME] refactoring to use hash
300 _AppInfo::GetApiVersionFromStr(const char* pVer)
307 return _API_VERSION_2_1;
309 else if (ver == L"2.0")
311 return _API_VERSION_2_0;
313 else if (ver == L"1.2")
315 return _API_VERSION_1_2;
317 else if (ver == L"1.1")
319 return _API_VERSION_1_1;
321 else if (ver == L"1.0.2")
323 return _API_VERSION_1_0_2;
325 else if (ver == L"1.0")
327 return _API_VERSION_1_0;
331 return _API_VERSION_2_1;
337 _AppInfo::GetApiVersion(void)
339 return GetAppInfo()->__apiVersion;
344 _AppInfo::IsOspCompat(void)
346 return GetAppInfo()->__isOspCompat;
351 _AppInfo::SetApiVersion(_ApiVersion v)
353 GetAppInfo()->__apiVersion = v;
359 _AppInfo::GetProcessId(void)
361 static int processId = static_cast<int>(getpid());
367 _AppInfo::GetAppRootDirFd(void)
369 return GetAppInfo()->__appRootDirFd;
374 _AppInfo::GetAppId(void)
376 const String& packageId = GetAppInfo()->__packageId;
378 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
385 _AppInfo::GetApplicationId(void)
387 const String& appId = GetAppInfo()->__appId;
389 SysAssertf(!appId.IsEmpty(), "AppId is not initialized properly.");
396 _AppInfo::GetPackageId(void)
398 const String& packageId = GetAppInfo()->__packageId;
400 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
407 _AppInfo::GetAppExecutableName(void)
409 return GetAppInfo()->__appExecutableName;
414 _AppInfo::IsSubMode(void)
416 return GetAppInfo()->__isSubMode;
421 _AppInfo::GetAppName(void)
423 if (!GetAppInfo()->__isPackageInfoInitialized)
425 const String& packageId = GetAppInfo()->__packageId;
427 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
429 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
430 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
433 return *(GetAppInfo()->__pAppName);
438 _AppInfo::GetAppRootPath(void)
440 return GetAppInfo()->__appRootPath;
444 _AppInfo::GetAppVersion(void)
446 if (!GetAppInfo()->__isPackageInfoInitialized)
448 const String& packageId = GetAppInfo()->__packageId;
450 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
452 result r = GetAppInfo()->UpdateAppInfoFromPackageInfo(packageId);
453 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
456 return *(GetAppInfo()->__pAppVersion);
461 _AppInfo::GetAppState(void)
463 return GetAppInfo()->__appState;
468 _AppInfo::SetAppState(AppState appState)
470 GetAppInfo()->__appState = appState;
475 _AppInfo::GetAppType(void)
477 return GetAppInfo()->__appType;
482 _AppInfo::SetAppType(_AppType appType)
484 GetAppInfo()->__appType = appType;
489 _AppInfo::GetArgc(void)
491 return GetAppInfo()->__argc;
496 _AppInfo::GetArgv(void)
498 return GetAppInfo()->__pArgv;
503 _AppInfo::GetAppHandlerType(void)
505 return GetAppInfo()->__appHandlerType;
510 _AppInfo::SetAppHandlerType(int appHandlerType)
512 GetAppInfo()->__appHandlerType = appHandlerType;
517 _AppInfo::GetParentWindowHandle(void)
519 return GetAppInfo()->__parentWindowHandle;
524 _AppInfo::SetParentWindowHandle(unsigned int handle)
526 GetAppInfo()->__parentWindowHandle = handle;
531 _AppInfo::UpdatePackageInfo(bool update)
533 GetAppInfo()->__isPackageInfoInitialized = !update;