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_AppManagerImpl.cpp
19 * @brief This is the implementation for the _AppManagerImpl class.
25 #include <unique_ptr.h>
30 #include <FBaseObject.h>
31 #include <FBaseString.h>
32 #include <FBaseColArrayList.h>
33 #include <FBaseErrors.h>
34 #include <FAppAppControl.h>
35 #include <FAppAppManager.h>
36 #include <FAppSqlDataControl.h>
37 #include <FAppMapDataControl.h>
38 #include <FAppPkgPackageInfo.h>
39 #include <FAppIActiveAppEventListener.h>
40 #include <FBaseSysLog.h>
41 #include <FAppIAppControlListener.h>
43 #include <FBase_StringConverter.h>
44 #include <FBaseRt_LibraryImpl.h>
45 #include <FSys_SystemInfoImpl.h>
47 #include "FApp_AppControlRegistry.h"
48 #include "FApp_AppImpl.h"
49 #include "FApp_AppInfo.h"
50 #include "FApp_AppManagerImpl.h"
51 #include "FApp_AppManagerProxy.h"
52 #include "FApp_AppMessageImpl.h"
53 #include "FApp_AppControlImpl.h"
54 #include "FApp_ConditionManagerProxy.h"
55 #include "FApp_IAppEventListener.h"
56 #include "FApp_MapDataControlImpl.h"
57 #include "FApp_SqlDataControlImpl.h"
58 #include "FAppPkg_PackageManagerImpl.h"
59 #include "FAppPkg_PackageInfoImpl.h"
60 #include "FApp_AppControlManager.h"
61 #include "FApp_TemplateUtil.h"
63 #include "FApp_AppLifecycleEvent.h"
64 #include "FApp_AppLifecycleEventArg.h"
65 #include "FApp_IAppLifecycleEventListener.h"
66 #include "FApp_ActiveWindowManager.h"
68 using namespace Tizen::App::Package;
69 using namespace Tizen::Base;
70 using namespace Tizen::Base::Collection;
71 using namespace Tizen::Base::Runtime;
72 using namespace Tizen::Base::Utility;
73 using namespace Tizen::Io;
78 //const long MAX_APPCONTROL_ARGUMENT = 4096;
79 const long MAX_APPCONTROL_ARGUMENT = 1024;
80 const long MAX_CONDITION_LENGTH = 400;
83 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
86 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
90 while (pEnum->MoveNext() == E_SUCCESS)
92 String* pStr = static_cast<String*>(pEnum->GetCurrent());
97 size += pStr->GetLength();
104 } // anonymous name-space
107 namespace Tizen { namespace App
110 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
111 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
112 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
113 const int _MAX_PACKAGE_ID_LENGTH = 10;
116 _AppManagerImpl::_AppManagerImpl(void)
117 : __pConditionManager(null)
118 , __pActiveWindowManager(new (std::nothrow) _ActiveWindowManager)
124 _AppManagerImpl::~_AppManagerImpl(void)
128 delete __pConditionManager;
134 _AppManagerImpl::Construct(void)
136 __appLifecycleEvent.Construct();
137 __appListForAppLifecycle.Construct();
138 result r = __mutex.Create();
139 SysTryLog(NID_APP, r == E_SUCCESS, "Creating mutex failed.");
141 _IAppManager* pMgr = _AppManagerProxy::GetService();
142 //todo : uncomment following _SysTryReturn or put assert.
143 //SysTryReturn(NID_APP, pMgr != null, GetLastResult(), GetLastResult(), "[%s]GetService failed. Please check 'ps -A | grep OspAppService'!", GetLastResult());
144 SysTryReturn(NID_APP, pMgr != null, E_SUCCESS, E_SUCCESS, "[E_SYSTEM] fatal error. Please check 'ps -A | grep osp-app-service'!", GetLastResult());
145 pMgr->InitEventListener(this);
151 _AppManagerImpl::GetInstance(void)
153 return AppManager::GetInstance()->__pAppManagerImpl;
156 _ConditionManagerProxy*
157 _AppManagerImpl::GetConditionManagerProxy(void)
159 if (__pConditionManager == null)
161 const int MAX_TRY_COUNT = 5;
162 const int TRY_SLEEP_TIME = 250;
164 __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
165 SysAssert(__pConditionManager != null);
170 result r = __pConditionManager->Construct();
173 SysLog(NID_APP, "Succeeded in connecting condition manager.");
177 if (count >= MAX_TRY_COUNT)
179 SysLog(NID_APP, "Failed to connecting condition manager.");
184 Thread::Sleep(TRY_SLEEP_TIME);
188 return __pConditionManager;
192 _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
194 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
196 AppControl* pAc = null;
198 pAc = pRegs->GetTizenAppControlN(aId, oId);
201 SetLastResult(E_SUCCESS);
205 pAc = pRegs->GetAppControlN(aId, oId);
207 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
208 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
209 SetLastResult(E_SUCCESS);
214 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
215 const String* pUriScheme)
217 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
218 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
221 const String* pMimeType = pDataType;
225 if ((*pDataType)[0] == L'.')
227 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
230 pDataType->SubString(1, ext);
232 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
234 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
236 pMimeType = &mimeType;
238 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
244 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
245 GetErrorMessage(E_INVALID_FORMAT));
248 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
249 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
251 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
255 SetLastResult(E_OBJ_NOT_FOUND);
259 SetLastResult(E_SUCCESS);
260 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
267 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
268 IAppControlListener* pListener)
270 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
275 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
276 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
278 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
279 "At least one of the specified argument must not be null.");
280 if (pDataList != null)
283 argSize = GetTotalSize(*pDataList);
285 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
287 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
288 "The size of pDataList exceeded the limit(%d).",
289 MAX_APPCONTROL_ARGUMENT);
292 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
294 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
295 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
297 _AppMessageImpl::SetOperation(pBundle.get(), operation);
301 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
306 String mimeType = *pDataType;
308 if ((*pDataType)[0] == L'.')
310 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
314 pDataType->SubString(1, ext);
316 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
318 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
320 pMimeType = &mimeType;
322 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
326 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
331 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
334 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
339 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
341 SqlDataControl* pDc = null;
343 String* pAppId = null;
344 String* pAccess = null;
347 // XXX: Need _NativeDataControlRegistry for SLP native app ?
348 // Try to searach SLP data control.
349 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
350 // pDc = pReg->GetNativeSqlDataControlN(providerId);
356 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
357 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
359 pAppId = dynamic_cast< String* >(pList->GetAt(0));
360 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
361 pAccess = dynamic_cast< String* >(pList->GetAt(1));
362 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
365 if (pAppId->StartsWith(L"org.tizen.", 0))
367 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
368 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
369 for (int i = 0; i < 3; ++i)
371 pStrTok->GetNextToken(appId);
374 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
378 appId.Append(*pAppId);
381 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
382 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
388 _AppManagerImpl::GetMapDataControlN(const String& providerId)
390 MapDataControl* pDc = null;
392 String* pAppId = null;
393 String* pAccess = null;
396 // XXX: Need _NativeDataControlRegistry for SLP native app ?
397 // Try to searach SLP data control.
398 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
399 // pDc = pReg->GetNativeMapDataControlN(providerId);
405 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
406 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
408 pAppId = dynamic_cast< String* >(pList->GetAt(0));
409 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
410 pAccess = dynamic_cast< String* >(pList->GetAt(1));
411 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
414 if (pAppId->StartsWith(L"org.tizen.", 0))
416 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
417 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
418 for (int i = 0; i < 3; ++i)
420 pStrTok->GetNextToken(appId);
423 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
427 appId.Append(*pAppId);
430 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
431 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
437 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
439 String pkgId = PackageManager::GetPackageIdByAppId(appId);
440 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
441 "The application is not installed. (app: %ls)", appId.GetPointer());
443 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
444 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
445 "Failed to get _PackageManagerImpl instance.");
447 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
448 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
449 "The application is not installed. (app: %ls)", appId.GetPointer());
451 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
452 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
453 "Failed to get _PackageInfoImpl instance.");
455 appRootPath = pPkgInfoImpl->GetAppRootPath();
456 appRootPath.Append(L"/");
462 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
464 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
465 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
466 "The target application(%ls) is not installed.", appId.GetPointer());
467 // SysTryReturnResult(NID_APP,
468 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
469 // "The length of appid exceeded the limit(%d).",
470 // WIDGET_APP_MAX_APPID_LENGTH);
472 std::unique_ptr<AppControl> pAc(_AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN));
473 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
478 argSize = GetTotalSize(*pArguments);
480 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
482 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
483 "The size of pDataList exceeded the limit(%d).",
484 MAX_APPCONTROL_ARGUMENT);
487 result r = pAc->Start(pArguments, null);
489 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
495 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
497 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
499 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
501 int ret = aul_open_app(pName.get());
505 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
510 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
511 "The target application(%ls) is not installed.", appId.GetPointer());
517 r = E_APP_NOT_INSTALLED;
527 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
533 _AppManagerImpl::TerminateApplication(const AppId& appId)
535 _IAppManager* pMgr = _AppManagerProxy::GetService();
536 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
538 return pMgr->TerminateApplication(appId);
543 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
545 const char* pStr = static_cast<const char*>(pData);
547 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
549 aul_terminate_pid(pAppInfo->pid);
550 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
556 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
558 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
559 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
561 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
566 _AppManagerImpl::IsRunning(const AppId& appId) const
568 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
570 return aul_app_is_running(pAppId.get());
574 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
576 ArrayList* pList = static_cast<ArrayList*>(pData);
577 if (pList && pAppInfo && pAppInfo->appid)
579 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
586 _AppManagerImpl::GetRunningAppListN(void) const
588 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
589 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
590 GetErrorMessage(E_OUT_OF_MEMORY));
591 pRunningAppList->Construct();
593 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
594 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
596 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
597 return pRunningAppList;
602 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
603 AppManager::LaunchOption option)
605 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
606 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
608 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
609 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
612 result r = strTok.GetNextToken(key);
613 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
616 if (key == L"Serial")
618 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
620 else if (key == L"NFC")
622 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
624 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
626 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
630 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
632 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
633 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
637 SysTryReturnResult(NID_APP,
638 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
639 "No such a condition.");
642 return pProxy->UnregisterAppLaunch(appId, pCondition);
646 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
648 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
649 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
650 GetErrorMessage(E_INVALID_STATE));
653 return pProxy->IsAppLaunchRegistered(appId, pCondition);
657 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
659 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
664 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
672 ///////////////////////////////////////////////////////////////////////////////
673 // LifecycleManager begins.
674 ///////////////////////////////////////////////////////////////////////////////
677 _AppLifecycleManager::Init(void)
679 aul_listen_app_launch_signal(LaunchCallback, this);
680 aul_listen_app_dead_signal(TerminateCallback, this);
684 _AppLifecycleManager::Fini(void)
686 aul_listen_app_launch_signal(NULL, NULL);
687 aul_listen_app_dead_signal(NULL, NULL);
691 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
693 if (__pEventList == null)
695 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
696 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
700 __pEventList = pAppEventList.release();
701 SysLog(NID_APP, "Registered app event listener.");
704 return __pEventList->Add(&listener);
708 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
710 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
712 result r = __pEventList->Remove(&listener);
714 if (__pEventList->GetCount() == 0)
726 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
728 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
729 if (pImpl == null || pImpl->__pEventList == null)
731 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
736 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
739 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
743 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
746 const String tmp = appId;
747 pImpl->__map.Add(pid, tmp);
749 while (pEnum->MoveNext() == E_SUCCESS)
751 _IAppEventListener* pListener = null;
752 pEnum->GetCurrent(pListener);
754 pListener->OnApplicationLaunched(tmp, pid);
758 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
764 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
766 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
767 if (pImpl == null || pImpl->__pEventList == null)
769 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
773 // terminate callback cannot acquire appId from pid
775 result r = pImpl->__map.GetValue(pid, tmp);
778 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
782 pImpl->__map.Remove(pid);
784 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
787 while (pEnum->MoveNext() == E_SUCCESS)
789 _IAppEventListener* pListener = null;
790 pEnum->GetCurrent(pListener);
792 pListener->OnApplicationTerminated(tmp, pid);
796 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
801 ///////////////////////////////////////////////////////////////////////////////
802 // LifecycleManager ends.
803 ///////////////////////////////////////////////////////////////////////////////
807 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
809 return __lifeManager.AddListener(listener);
813 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
815 return __lifeManager.RemoveListener(listener);
820 _AppManagerImpl::GetUiLibraryImpl(void)
822 if (__pUiLibrary == null)
824 __pUiLibrary = new (std::nothrow) _LibraryImpl;
825 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
827 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
828 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
831 return *__pUiLibrary;
836 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
838 return __pActiveWindowManager->AddActiveAppEventListener(listener);
843 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
845 return __pActiveWindowManager->RemoveActiveAppEventListener(listener);
850 _AppManagerImpl::GetActiveApp(AppId& appId)
852 return __pActiveWindowManager->GetActiveApp(appId);
857 _AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
859 _IAppManager* pMgr = _AppManagerProxy::GetService();
860 SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
862 return pMgr->IsUserPreferredAppForAppControlResolution(appId);
866 _AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
868 _IAppManager* pMgr = _AppManagerProxy::GetService();
869 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
871 return pMgr->ClearUserPreferenceForAppControlResolution(appId);
875 _AppManagerImpl::AddAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
877 return __appLifecycleEvent.AddListener(listener, false);
881 _AppManagerImpl::RemoveAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
883 return __appLifecycleEvent.RemoveListener(listener);
887 _AppManagerImpl::RegisterAppForAppLifecycleEvent(const AppId& appId)
889 SysLog(NID_APP, "Enter");
891 result res = __mutex.Acquire();
892 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
894 bool isContained = false;
895 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
897 int currentRefCnt = 0;
900 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
901 r = __appListForAppLifecycle.SetValue(appId, ++currentRefCnt);
905 r = __appListForAppLifecycle.Add(appId, currentRefCnt);
907 _IAppManager* pMgr = _AppManagerProxy::GetService();
908 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
910 r = pMgr->RegisterAppForAppLifecycleEvent(appId, -1);
911 SysLog(NID_APP, "The appId(%ls) is registered.", appId.GetPointer());
913 res = __mutex.Release();
914 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
916 SysLog(NID_APP, "Exit");
922 _AppManagerImpl::UnregisterAppForAppLifecycleEvent(const AppId& appId)
924 SysLog(NID_APP, "Enter");
926 result res = __mutex.Acquire();
927 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
929 bool isContained = false;
930 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
934 int currentRefCnt = 0;
935 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
939 if (currentRefCnt < 0)
941 r = __appListForAppLifecycle.Remove(appId);
943 _IAppManager* pMgr = _AppManagerProxy::GetService();
944 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
946 r = pMgr->UnregisterAppForAppLifecycleEvent(appId, -1);
948 SysLog(NID_APP, "The appId(%ls) is unregistered.", appId.GetPointer());
952 r = __appListForAppLifecycle.SetValue(appId, currentRefCnt);
957 SysLog(NID_APP, "The appId(%ls) is not registered.", appId.GetPointer());
960 res = __mutex.Release();
961 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
963 SysLog(NID_APP, "Exit");
970 _AppManagerImpl::OnAppLifecycleEventReceived(int clientId,const AppId& appId, _AppLifecycleEventType appLifecycleEventType)
972 SysLog(NID_APP, "Enter appId(%ls), appLifecycleEventType(%d)", appId.GetPointer(), appLifecycleEventType);
974 _AppLifecycleEventArg* pArg = new (std::nothrow)_AppLifecycleEventArg(appId, appLifecycleEventType);
975 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
977 __appLifecycleEvent.FireAsync(*pArg);