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 <FIo_DataControlResultSetImpl.h>
46 #include <FSys_SystemInfoImpl.h>
48 #include "FApp_AppControlRegistry.h"
49 #include "FApp_AppImpl.h"
50 #include "FApp_AppInfo.h"
51 #include "FApp_AppManagerEventArg.h"
52 #include "FApp_AppManagerImpl.h"
53 #include "FApp_AppManagerProxy.h"
54 #include "FApp_AppMessageImpl.h"
55 #include "FApp_AppControlImpl.h"
56 #include "FApp_ConditionManagerProxy.h"
57 #include "FApp_IAppManagerEventListener.h"
58 #include "FApp_IAppEventListener.h"
59 #include "FApp_MapDataControlImpl.h"
60 #include "FApp_SqlDataControlImpl.h"
61 #include "FAppPkg_PackageManagerImpl.h"
62 #include "FAppPkg_PackageInfoImpl.h"
63 #include "FApp_AppControlManager.h"
65 #include "FApp_AppLifecycleEvent.h"
66 #include "FApp_AppLifecycleEventArg.h"
67 #include "FApp_IAppLifecycleEventListener.h"
68 #include "FApp_ActiveWindowManager.h"
70 using namespace Tizen::App::Package;
71 using namespace Tizen::Base;
72 using namespace Tizen::Base::Collection;
73 using namespace Tizen::Base::Runtime;
74 using namespace Tizen::Base::Utility;
75 using namespace Tizen::Io;
80 //const long MAX_APPCONTROL_ARGUMENT = 4096;
81 const long MAX_APPCONTROL_ARGUMENT = 1024;
82 const long MAX_CONDITION_LENGTH = 400;
85 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
88 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
92 while (pEnum->MoveNext() == E_SUCCESS)
94 String* pStr = static_cast<String*>(pEnum->GetCurrent());
99 size += pStr->GetLength();
106 } // anonymous name-space
109 namespace Tizen { namespace App
112 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
113 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
114 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
115 const int _MAX_PACKAGE_ID_LENGTH = 10;
118 _AppManagerImpl::_AppManagerImpl(void)
119 : __pConditionManager(null)
120 , __pActiveWindowManager(new (std::nothrow) _ActiveWindowManager)
126 _AppManagerImpl::~_AppManagerImpl(void)
130 delete __pConditionManager;
136 _AppManagerImpl::Construct(void)
138 __appManagerEvent.Construct();
139 __appLifecycleEvent.Construct();
140 __appListForAppLifecycle.Construct(0, 0, __strHashCodeProvider, __comparer);
141 result r = __mutex.Create();
142 SysTryLog(NID_APP, r == E_SUCCESS, "Creating mutex failed.");
144 _IAppManager* pMgr = _AppManagerProxy::GetService();
145 //todo : uncomment following _SysTryReturn or put assert.
146 //SysTryReturn(NID_APP, pMgr != null, GetLastResult(), GetLastResult(), "[%s]GetService failed. Please check 'ps -A | grep OspAppService'!", GetLastResult());
147 SysTryReturn(NID_APP, pMgr != null, E_SUCCESS, E_SUCCESS, "[E_SYSTEM] fatal error. Please check 'ps -A | grep osp-app-service'!", GetLastResult());
148 pMgr->InitEventListener(this);
154 _AppManagerImpl::GetInstance(void)
156 return AppManager::GetInstance()->__pAppManagerImpl;
159 _ConditionManagerProxy*
160 _AppManagerImpl::GetConditionManagerProxy(void)
162 if (__pConditionManager == null)
164 const int MAX_TRY_COUNT = 5;
165 const int TRY_SLEEP_TIME = 250;
167 __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
168 SysAssert(__pConditionManager != null);
173 result r = __pConditionManager->Construct();
176 SysLog(NID_APP, "Succeeded in connecting condition manager.");
180 if (count >= MAX_TRY_COUNT)
182 SysLog(NID_APP, "Failed to connecting condition manager.");
187 Thread::Sleep(TRY_SLEEP_TIME);
191 return __pConditionManager;
195 _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
197 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
199 AppControl* pAc = null;
201 pAc = pRegs->GetTizenAppControlN(aId, oId);
204 SetLastResult(E_SUCCESS);
208 pAc = pRegs->GetAppControlN(aId, oId);
210 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
211 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
212 SetLastResult(E_SUCCESS);
217 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
218 const String* pUriScheme)
220 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
221 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
224 const String* pMimeType = pDataType;
228 if ((*pDataType)[0] == L'.')
230 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
233 pDataType->SubString(1, ext);
235 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
237 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
239 pMimeType = &mimeType;
241 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
247 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
248 GetErrorMessage(E_INVALID_FORMAT));
251 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
252 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
254 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
258 SetLastResult(E_OBJ_NOT_FOUND);
262 SetLastResult(E_SUCCESS);
263 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
270 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
271 IAppControlListener* pListener)
273 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
278 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
279 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
281 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
282 "At least one of the specified argument must not be null.");
283 if (pDataList != null)
286 argSize = GetTotalSize(*pDataList);
288 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
290 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
291 "The size of pDataList exceeded the limit(%d).",
292 MAX_APPCONTROL_ARGUMENT);
295 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
297 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
298 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
300 _AppMessageImpl::SetOperation(pBundle.get(), operation);
304 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
309 String mimeType = *pDataType;
311 if ((*pDataType)[0] == L'.')
313 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
317 pDataType->SubString(1, ext);
319 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
321 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
323 pMimeType = &mimeType;
325 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
329 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
334 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
337 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
342 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
344 SqlDataControl* pDc = null;
346 String* pAppId = null;
347 String* pAccess = null;
350 // XXX: Need _NativeDataControlRegistry for SLP native app ?
351 // Try to searach SLP data control.
352 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
353 // pDc = pReg->GetNativeSqlDataControlN(providerId);
359 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
360 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
362 pAppId = dynamic_cast< String* >(pList->GetAt(0));
363 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
364 pAccess = dynamic_cast< String* >(pList->GetAt(1));
365 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
368 if (pAppId->StartsWith(L"org.tizen.", 0))
370 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
371 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
372 for (int i = 0; i < 3; ++i)
374 pStrTok->GetNextToken(appId);
377 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
381 appId.Append(*pAppId);
384 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
385 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
391 _AppManagerImpl::GetMapDataControlN(const String& providerId)
393 MapDataControl* pDc = null;
395 String* pAppId = null;
396 String* pAccess = null;
399 // XXX: Need _NativeDataControlRegistry for SLP native app ?
400 // Try to searach SLP data control.
401 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
402 // pDc = pReg->GetNativeMapDataControlN(providerId);
408 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
409 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
411 pAppId = dynamic_cast< String* >(pList->GetAt(0));
412 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
413 pAccess = dynamic_cast< String* >(pList->GetAt(1));
414 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
417 if (pAppId->StartsWith(L"org.tizen.", 0))
419 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
420 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
421 for (int i = 0; i < 3; ++i)
423 pStrTok->GetNextToken(appId);
426 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
430 appId.Append(*pAppId);
433 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
434 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
440 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
442 String pkgId = PackageManager::GetPackageIdByAppId(appId);
443 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
444 "The application is not installed. (app: %ls)", appId.GetPointer());
446 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
447 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
448 "Failed to get _PackageManagerImpl instance.");
450 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
451 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
452 "The application is not installed. (app: %ls)", appId.GetPointer());
454 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
455 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
456 "Failed to get _PackageInfoImpl instance.");
458 appRootPath = pPkgInfoImpl->GetAppRootPath();
459 appRootPath.Append(L"/");
465 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
467 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
468 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
469 "The target application(%ls) is not installed.", appId.GetPointer());
470 // SysTryReturnResult(NID_APP,
471 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
472 // "The length of appid exceeded the limit(%d).",
473 // WIDGET_APP_MAX_APPID_LENGTH);
475 std::unique_ptr<AppControl> pAc(_AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN));
476 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
481 argSize = GetTotalSize(*pArguments);
483 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
485 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
486 "The size of pDataList exceeded the limit(%d).",
487 MAX_APPCONTROL_ARGUMENT);
490 result r = pAc->Start(pArguments, null);
492 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
498 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
500 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
502 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
504 int ret = aul_open_app(pName.get());
508 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
513 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
514 "The target application(%ls) is not installed.", appId.GetPointer());
520 r = E_APP_NOT_INSTALLED;
530 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
536 _AppManagerImpl::TerminateApplication(const AppId& appId)
538 _IAppManager* pMgr = _AppManagerProxy::GetService();
539 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
541 return pMgr->TerminateApplication(appId);
546 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
548 const char* pStr = static_cast<const char*>(pData);
550 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
552 aul_terminate_pid(pAppInfo->pid);
553 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
559 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
561 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
562 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
564 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
569 _AppManagerImpl::IsRunning(const AppId& appId) const
571 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
573 return aul_app_is_running(pAppId.get());
577 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
579 ArrayList* pList = static_cast<ArrayList*>(pData);
580 if (pList && pAppInfo && pAppInfo->appid)
582 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
589 _AppManagerImpl::GetRunningAppListN(void) const
591 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
592 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
593 GetErrorMessage(E_OUT_OF_MEMORY));
594 pRunningAppList->Construct();
596 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
597 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
599 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
600 return pRunningAppList;
605 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
606 AppManager::LaunchOption option)
608 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
609 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
611 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
612 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
615 result r = strTok.GetNextToken(key);
616 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
619 if (key == L"Serial")
621 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
623 else if (key == L"NFC")
625 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
627 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
629 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
633 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
635 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
636 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
640 SysTryReturnResult(NID_APP,
641 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
642 "No such a condition.");
645 return pProxy->UnregisterAppLaunch(appId, pCondition);
649 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
651 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
652 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
653 GetErrorMessage(E_INVALID_STATE));
656 return pProxy->IsAppLaunchRegistered(appId, pCondition);
660 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
662 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
666 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
668 SysLog(NID_APP, "app:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
670 _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
671 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
672 __appManagerEvent.FireAsync(*pArg);
677 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
685 _AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
687 _IAppManager* pMgr = _AppManagerProxy::GetService();
688 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
690 result r = __appManagerEvent.AddListener(listener);
691 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
693 const int count = __appManagerEvent.GetListenerCount();
694 SysLog(NID_APP, "registered event listener(s) # %d", count);
701 return pMgr->AddEventListener(-1);
705 _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
707 _IAppManager* pMgr = _AppManagerProxy::GetService();
708 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
710 result r = __appManagerEvent.RemoveListener(listener);
711 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
713 const int count = __appManagerEvent.GetListenerCount();
714 SysLog(NID_APP, "registered event listener(s) # %d", count);
718 return pMgr->RemoveEventListener(-1);
725 ///////////////////////////////////////////////////////////////////////////////
726 // LifecycleManager begins.
727 ///////////////////////////////////////////////////////////////////////////////
730 _AppLifecycleManager::Init(void)
732 aul_listen_app_launch_signal(LaunchCallback, this);
733 aul_listen_app_dead_signal(TerminateCallback, this);
737 _AppLifecycleManager::Fini(void)
739 aul_listen_app_launch_signal(NULL, NULL);
740 aul_listen_app_dead_signal(NULL, NULL);
744 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
746 if (__pEventList == null)
748 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
749 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
753 __pEventList = pAppEventList.release();
754 SysLog(NID_APP, "Registered app event listener.");
757 return __pEventList->Add(&listener);
761 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
763 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
765 result r = __pEventList->Remove(&listener);
767 if (__pEventList->GetCount() == 0)
779 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
781 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
782 if (pImpl == null || pImpl->__pEventList == null)
784 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
789 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
792 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
796 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
799 const String tmp = appId;
800 pImpl->__map.Add(pid, tmp);
802 while (pEnum->MoveNext() == E_SUCCESS)
804 _IAppEventListener* pListener = null;
805 pEnum->GetCurrent(pListener);
807 pListener->OnApplicationLaunched(tmp, pid);
811 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
817 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
819 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
820 if (pImpl == null || pImpl->__pEventList == null)
822 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
826 // terminate callback cannot acquire appId from pid
828 result r = pImpl->__map.GetValue(pid, tmp);
831 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
835 pImpl->__map.Remove(pid);
837 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
840 while (pEnum->MoveNext() == E_SUCCESS)
842 _IAppEventListener* pListener = null;
843 pEnum->GetCurrent(pListener);
845 pListener->OnApplicationTerminated(tmp, pid);
849 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
854 ///////////////////////////////////////////////////////////////////////////////
855 // LifecycleManager ends.
856 ///////////////////////////////////////////////////////////////////////////////
860 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
862 return __lifeManager.AddListener(listener);
866 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
868 return __lifeManager.RemoveListener(listener);
873 _AppManagerImpl::GetUiLibraryImpl(void)
875 if (__pUiLibrary == null)
877 __pUiLibrary = new (std::nothrow) _LibraryImpl;
878 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
880 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
881 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
884 return *__pUiLibrary;
889 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
891 return __pActiveWindowManager->AddActiveAppEventListener(listener);
896 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
898 return __pActiveWindowManager->RemoveActiveAppEventListener(listener);
903 _AppManagerImpl::GetActiveApp(AppId& appId)
905 return __pActiveWindowManager->GetActiveApp(appId);
910 _AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
912 _IAppManager* pMgr = _AppManagerProxy::GetService();
913 SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
915 return pMgr->IsUserPreferredAppForAppControlResolution(appId);
919 _AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
921 _IAppManager* pMgr = _AppManagerProxy::GetService();
922 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
924 return pMgr->ClearUserPreferenceForAppControlResolution(appId);
928 _AppManagerImpl::AddAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
930 return __appLifecycleEvent.AddListener(listener, false);
934 _AppManagerImpl::RemoveAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
936 return __appLifecycleEvent.RemoveListener(listener);
940 _AppManagerImpl::RegisterAppForAppLifecycleEvent(const AppId& appId)
942 SysLog(NID_APP, "Enter");
944 result res = __mutex.Acquire();
945 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
947 bool isContained = false;
948 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
950 int currentRefCnt = 0;
953 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
954 r = __appListForAppLifecycle.SetValue(appId, ++currentRefCnt);
958 r = __appListForAppLifecycle.Add(appId, currentRefCnt);
960 _IAppManager* pMgr = _AppManagerProxy::GetService();
961 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
963 r = pMgr->RegisterAppForAppLifecycleEvent(appId, -1);
964 SysLog(NID_APP, "The appId(%ls) is registered.", appId.GetPointer());
966 res = __mutex.Release();
967 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
969 SysLog(NID_APP, "Exit");
975 _AppManagerImpl::UnregisterAppForAppLifecycleEvent(const AppId& appId)
977 SysLog(NID_APP, "Enter");
979 result res = __mutex.Acquire();
980 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
982 bool isContained = false;
983 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
987 int currentRefCnt = 0;
988 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
992 if (currentRefCnt < 0)
994 r = __appListForAppLifecycle.Remove(appId);
996 _IAppManager* pMgr = _AppManagerProxy::GetService();
997 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
999 r = pMgr->UnregisterAppForAppLifecycleEvent(appId, -1);
1001 SysLog(NID_APP, "The appId(%ls) is unregistered.", appId.GetPointer());
1005 r = __appListForAppLifecycle.SetValue(appId, currentRefCnt);
1010 SysLog(NID_APP, "The appId(%ls) is not registered.", appId.GetPointer());
1013 res = __mutex.Release();
1014 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
1016 SysLog(NID_APP, "Exit");
1023 _AppManagerImpl::OnAppLifecycleEventReceived(int clientId,const AppId& appId, _AppLifecycleEventType appLifecycleEventType)
1025 SysLog(NID_APP, "Enter appId(%ls), appLifecycleEventType(%d)", appId.GetPointer(), appLifecycleEventType);
1027 _AppLifecycleEventArg* pArg = new (std::nothrow)_AppLifecycleEventArg(appId, appLifecycleEventType);
1028 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1030 __appLifecycleEvent.FireAsync(*pArg);