Merge "[2.2.1] Modify String::Replace() which shared buffer even when the whole strin...
[platform/framework/native/appfw.git] / src / app / FApp_AppInfo.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file         FApp_AppInfo.cpp
19  * @brief       This is the implementation for the _AppInfo.cpp class.
20  */
21
22 #include <cstdio>
23 #include <cstring>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <new>
27 #include <fcntl.h>
28 #include <unique_ptr.h>
29
30 #include <appinfo.h>
31
32 #include <FBaseErrors.h>
33 #include <FAppPkgPackageInfo.h>
34 #include <FBaseSysLog.h>
35
36 #include <FBaseRt_Process.h>
37 #include <FBase_StringConverter.h>
38
39 #include "FAppPkg_PackageInfoImpl.h"
40 #include "FApp_AppInfo.h"
41
42 using namespace Tizen::App::Package;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Runtime;
45
46 extern "C"
47 {
48 void _OSP_EXPORT_
49 AppInfo_Update(void)
50 {
51         result r = Tizen::App::_AppInfo::GetInstance()->Construct();
52         if (IsFailed(r))
53         {
54                 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure.");
55                 fprintf(stderr, "Application initialization failure.\n");
56
57                 _Process::Exit(-1);
58         }
59 }
60
61 void _OSP_EXPORT_
62 InitAppInfo(const char* packageId, const char* svcId, int argc, char* pArgv[], int fd)
63 {
64         AppInfo_Update();
65 }
66
67 void _OSP_EXPORT_
68 InitWebAppInfo(const char* appId, const char* rootPath)
69 {
70         result r = Tizen::App::_AppInfo::GetInstance()->Construct(appId, rootPath, Tizen::App::_APP_TYPE_WEB_APP);
71         if (IsFailed(r))
72         {
73                 SysLogException(NID_APP, E_SYSTEM, "Application initialization failure for %s.", appId);
74                 fprintf(stderr, "Application initialization failure for %s.\n", appId);
75
76                 _Process::Exit(-1);
77         }
78 }
79
80 extern void FBase_Initialize(void);
81
82 }
83
84 namespace Tizen { namespace App
85 {
86
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";
96
97
98 _AppInfo::_AppInfo(void)
99         : __appState(TERMINATED)
100         , __appType(_APP_TYPE_NONE)
101         , __appRootDirFd(-1)
102         , __appHandlerType(_APP_HANDLER_NONE)
103         , __parentWindowHandle(-1)
104         , __pAppName(null)
105         , __pAppVersion(null)
106         , __isPackageInfoInitialized(false)
107         , __isSubMode(false)
108         , __isVirtualRoot(false)
109         , __isMultiWindow(false)
110 {
111         SysStaticAssert(sizeof(pid_t) == sizeof(int));
112
113         //FBase_Initialize();
114 }
115
116 _AppInfo::~_AppInfo(void)
117 {
118         delete __pAppName;
119         delete __pAppVersion;
120
121         // closing may or may not succeed
122         close(__appRootDirFd);
123 }
124
125 _AppInfo*
126 _AppInfo::GetInstance(void)
127 {
128         static _AppInfo info;
129
130         return &info;
131 }
132
133
134 result
135 _AppInfo::Construct(void)
136 {
137         SysAssertf(appinfo_is_initialized() == 1, "appinfo is not initialized.");
138
139         FBase_Initialize();
140
141         const char* pPackageId = appinfo_get_packageid();
142         const char* pExecName = appinfo_get_execname();
143
144
145         if (strncmp(pExecName, SUBMODE_NAME, strlen(SUBMODE_NAME)) == 0)
146         {
147                 SysLog(NID_APP, "Handling for submode.");
148                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(String(pPackageId));
149
150                 __isSubMode = true;
151
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());
155         }
156
157         result r = E_SUCCESS;
158         FILE* pFile = NULL;
159
160         {
161                 char appInfoPath[PATH_MAX] = {0, };
162
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);
167
168                 // app root directory file descriptor
169                 __appRootDirFd = open(appInfoPath, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
170                 __appRootPath = appInfoPath;
171                 
172                 SysLog(NID_APP, "App root directory (%s:%d) open.", appInfoPath, __appRootDirFd);
173
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));
176
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));
179
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));
183
184                 fclose(pFile);
185                 // fd is closed when the stream is closed by fclose();
186
187                 appinfo_set_api_version(appinfo_get_api_version_from_str(apiVersion));
188
189                 // to reduce package manager overhead, libc API is used
190                 if (faccessat(__appRootDirFd, COMPAT_FILE_PATH, F_OK, 0) == 0)
191                 {
192                         SysLog(NID_APP, "OSP compatibility mode on.");
193                         appinfo_set_compat(1);
194                 }
195
196                 if (faccessat(__appRootDirFd, VIRTUAL_ROOT_FILE_PATH, F_OK, 0) == 0)
197                 {
198                         SysLog(NID_APP, "virtual root mode on.");
199                         __isVirtualRoot = true;
200                 }
201
202                 // type file may does not exist
203                 fd = openat(__appRootDirFd, TYPE_FILE_PATH, O_RDONLY);
204                 if (fd > 0)
205                 {
206                         pFile = fdopen(fd, "r");
207                         if (pFile)
208                         {
209                                 int i;
210                                 int line = fscanf(pFile, "%d", &i);
211
212                                 fclose(pFile);
213                                 // fd is closed when the stream is closed by fclose();
214
215                                 __appType = _APP_TYPE_UI_APP | i;
216                                 SysLog(NID_APP, "Reading app type %d -> %d", i, __appType);
217                         }
218                 }
219
220                 SysLog(NID_APP, "AppInfo [%s][%s.%s][%d].", appinfo_get_appid(), appinfo_get_packageid(), appinfo_get_execname(), appinfo_get_api_version());
221         }
222
223         return E_SUCCESS;
224
225 CATCH:
226         if (pFile != NULL)
227         {
228                 fclose(pFile);
229         }
230
231         return r;
232 }
233
234
235 // initialize app context only
236 result
237 _AppInfo::Construct(const char* appId, const char* appRoot, _AppType type)
238 {
239         SysAssertf(appId != null, "Valid appId required to launch application.");
240
241         FBase_Initialize();
242
243         if (appinfo_is_initialized() == 0)
244         {
245                 appinfo_init(appId, 0);
246         }
247
248         __appRootDirFd = open(appRoot, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
249         appinfo_set_api_version(APP_INFO_VERSION_2_2);
250         __appType = type;
251
252         __appRootPath = appRoot;
253         if (__appRootPath[__appRootPath.GetLength()] != L'/')
254         {
255                 __appRootPath.Append(L'/');
256         }
257
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();
259
260         return E_SUCCESS;
261 }
262
263
264 result
265 _AppInfo::UpdateAppInfoFromPackageInfo(const PackageId& packageId)
266 {
267         _PackageManagerImpl* pPkg = _PackageManagerImpl::GetInstance();
268         SysTryReturnResult(NID_APP, pPkg != null, E_INVALID_STATE, "Invalid PackageManager instance.");
269
270         result r = E_SUCCESS;
271         std::unique_ptr<PackageInfo> pInfo(pPkg->GetPackageInfoN(packageId));
272         SysTryReturn(NID_APP, pInfo != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
273
274         const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo.get());
275         SysTryReturnResult(NID_APP, pPkgInfo != null, E_INVALID_STATE, "Invalid PackageInfo instance.");
276
277         delete __pAppName;
278         __pAppName = new (std::nothrow) String(pPkgInfo->GetName());
279         SysTryCatch(NID_APP, __pAppName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppName allocation failure.");
280
281         delete __pAppVersion;
282         __pAppVersion = new (std::nothrow) String(pPkgInfo->GetVersion());
283         SysTryCatch(NID_APP, __pAppVersion != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppVersion allocation failure.");
284
285         __isPackageInfoInitialized = true;
286
287         SysLog(NID_APP, "AppInfo updated [%ls][%ls].", __pAppName->GetPointer(), __pAppVersion->GetPointer());
288
289         return r;
290
291 CATCH:
292         delete __pAppName;
293         __pAppName = null;
294
295         delete __pAppVersion;
296         __pAppVersion = null;
297
298         return r;
299 }
300
301
302 _ApiVersion
303 _AppInfo::GetApiVersion(void)
304 {
305         return static_cast<_ApiVersion>(appinfo_get_api_version());
306 }
307
308
309 bool
310 _AppInfo::IsOspCompat(void)
311 {
312         return (appinfo_is_compat() == 1);
313 }
314
315 bool
316 _AppInfo::IsVirtualRoot(void)
317 {
318         return GetInstance()->__isVirtualRoot;
319 }
320
321 result
322 _AppInfo::SetApiVersion(_ApiVersion v)
323 {
324         appinfo_set_api_version(static_cast<app_info_version_e>(v));
325         return E_SUCCESS;
326 }
327
328
329 int
330 _AppInfo::GetProcessId(void)
331 {
332         static int processId = static_cast<int>(getpid());
333
334         return processId;
335 }
336
337 int
338 _AppInfo::GetAppRootDirFd(void)
339 {
340         return GetInstance()->__appRootDirFd;
341 }
342
343
344 AppId
345 _AppInfo::GetApplicationId(void)
346 {
347         return String(appinfo_get_appid());
348 }
349
350
351 PackageId
352 _AppInfo::GetPackageId(void)
353 {
354         return String(appinfo_get_packageid());
355 }
356
357
358 String
359 _AppInfo::GetAppExecutableName(void)
360 {
361         return String(appinfo_get_execname());
362 }
363
364
365 bool
366 _AppInfo::IsSubMode(void)
367 {
368         return GetInstance()->__isSubMode;
369 }
370
371
372 String
373 _AppInfo::GetAppName(void)
374 {
375         if (!GetInstance()->__isPackageInfoInitialized)
376         {
377                 const String& packageId = GetInstance()->GetPackageId();
378
379                 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
380
381                 result r = GetInstance()->UpdateAppInfoFromPackageInfo(packageId);
382                 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
383         }
384
385         return *(GetInstance()->__pAppName);
386 }
387
388
389 String
390 _AppInfo::GetAppRootPath(void)
391 {
392         return GetInstance()->__appRootPath;
393 }
394
395 String
396 _AppInfo::GetAppVersion(void)
397 {
398         if (!GetInstance()->__isPackageInfoInitialized)
399         {
400                 const String& packageId = GetInstance()->GetPackageId();
401
402                 SysAssertf(!packageId.IsEmpty(), "PackageId is not initialized properly.");
403
404                 result r = GetInstance()->UpdateAppInfoFromPackageInfo(packageId);
405                 SysAssertf(r == E_SUCCESS, "AppInfo update failure due to package problem.");
406         }
407
408         return *(GetInstance()->__pAppVersion);
409 }
410
411
412 AppState
413 _AppInfo::GetAppState(void)
414 {
415         return GetInstance()->__appState;
416 }
417
418
419 void
420 _AppInfo::SetAppState(AppState appState)
421 {
422         GetInstance()->__appState = appState;
423 }
424
425
426 int
427 _AppInfo::GetAppType(void)
428 {
429         return GetInstance()->__appType;
430 }
431
432
433 void
434 _AppInfo::SetAppType(_AppType appType)
435 {
436         GetInstance()->__appType |= appType;
437 }
438
439
440 int
441 _AppInfo::GetAppHandlerType(void)
442 {
443         return GetInstance()->__appHandlerType;
444 }
445
446
447 void
448 _AppInfo::SetAppHandlerType(int appHandlerType)
449 {
450         GetInstance()->__appHandlerType = appHandlerType;
451 }
452
453
454 unsigned int
455 _AppInfo::GetParentWindowHandle(void)
456 {
457         return GetInstance()->__parentWindowHandle;
458 }
459
460
461 void
462 _AppInfo::SetParentWindowHandle(unsigned int handle)
463 {
464         GetInstance()->__parentWindowHandle = handle;
465 }
466
467
468 void
469 _AppInfo::UpdatePackageInfo(bool update)
470 {
471         GetInstance()->__isPackageInfoInitialized = !update;
472 }
473
474 bool
475 _AppInfo::IsMultiWindow(void)
476 {
477         return GetInstance()->__isMultiWindow;
478 }
479
480 void
481 _AppInfo::SetMultiWindow(bool enable)
482 {
483         GetInstance()->__isMultiWindow = enable;
484 }
485
486 }} // Tizen::App