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 <FBaseInteger.h>
31 #include <FBaseObject.h>
32 #include <FBaseString.h>
33 #include <FBaseColArrayList.h>
34 #include <FBaseErrors.h>
35 #include <FAppAppControl.h>
36 #include <FAppAppManager.h>
37 #include <FAppSqlDataControl.h>
38 #include <FAppMapDataControl.h>
39 #include <FAppPkgPackageInfo.h>
40 #include <FAppIActiveAppEventListener.h>
41 #include <FBaseSysLog.h>
42 #include <FAppIAppControlListener.h>
44 #include <FBase_StringConverter.h>
45 #include <FBaseRt_LibraryImpl.h>
46 #include <FIo_DataControlResultSetImpl.h>
47 #include <FSys_SystemInfoImpl.h>
49 #include "FApp_AppControlRegistry.h"
50 #include "FApp_AppImpl.h"
51 #include "FApp_AppInfo.h"
52 #include "FApp_AppManagerEventArg.h"
53 #include "FApp_AppManagerImpl.h"
54 #include "FApp_AppManagerProxy.h"
55 #include "FApp_AppMessageImpl.h"
56 #include "FApp_AppControlImpl.h"
57 #include "FApp_ConditionManagerProxy.h"
58 #include "FApp_IAppManagerEventListener.h"
59 #include "FApp_IAppEventListener.h"
60 #include "FApp_MapDataControlImpl.h"
61 #include "FApp_SqlDataControlImpl.h"
62 #include "FAppPkg_PackageManagerImpl.h"
63 #include "FAppPkg_PackageInfoImpl.h"
64 #include "FApp_AppControlManager.h"
65 #include "FApp_TemplateUtil.h"
67 #include "FApp_AppLifecycleEvent.h"
68 #include "FApp_AppLifecycleEventArg.h"
69 #include "FApp_IAppLifecycleEventListener.h"
70 #include "FApp_ActiveWindowManager.h"
72 using namespace Tizen::App::Package;
73 using namespace Tizen::Base;
74 using namespace Tizen::Base::Collection;
75 using namespace Tizen::Base::Runtime;
76 using namespace Tizen::Base::Utility;
77 using namespace Tizen::Io;
82 //const long MAX_APPCONTROL_ARGUMENT = 4096;
83 const long MAX_APPCONTROL_ARGUMENT = 1024;
84 const long MAX_CONDITION_LENGTH = 400;
87 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
90 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
94 while (pEnum->MoveNext() == E_SUCCESS)
96 String* pStr = static_cast<String*>(pEnum->GetCurrent());
101 size += pStr->GetLength();
108 } // anonymous name-space
111 namespace Tizen { namespace App
114 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
115 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
116 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
117 const int _MAX_PACKAGE_ID_LENGTH = 10;
120 _AppManagerImpl::_AppManagerImpl(void)
121 : __pConditionManager(null)
122 , __pActiveWindowManager(new (std::nothrow) _ActiveWindowManager)
128 _AppManagerImpl::~_AppManagerImpl(void)
132 delete __pConditionManager;
138 _AppManagerImpl::Construct(void)
140 __appManagerEvent.Construct();
141 __appLifecycleEvent.Construct();
142 __appListForAppLifecycle.Construct();
143 result r = __mutex.Create();
144 SysTryLog(NID_APP, r == E_SUCCESS, "Creating mutex failed.");
146 _IAppManager* pMgr = _AppManagerProxy::GetService();
147 //todo : uncomment following _SysTryReturn or put assert.
148 //SysTryReturn(NID_APP, pMgr != null, GetLastResult(), GetLastResult(), "[%s]GetService failed. Please check 'ps -A | grep OspAppService'!", GetLastResult());
149 SysTryReturn(NID_APP, pMgr != null, E_SUCCESS, E_SUCCESS, "[E_SYSTEM] fatal error. Please check 'ps -A | grep osp-app-service'!", GetLastResult());
150 pMgr->InitEventListener(this);
156 _AppManagerImpl::GetInstance(void)
158 return AppManager::GetInstance()->__pAppManagerImpl;
161 _ConditionManagerProxy*
162 _AppManagerImpl::GetConditionManagerProxy(void)
164 if (__pConditionManager == null)
166 const int MAX_TRY_COUNT = 5;
167 const int TRY_SLEEP_TIME = 250;
169 __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
170 SysAssert(__pConditionManager != null);
175 result r = __pConditionManager->Construct();
178 SysLog(NID_APP, "Succeeded in connecting condition manager.");
182 if (count >= MAX_TRY_COUNT)
184 SysLog(NID_APP, "Failed to connecting condition manager.");
189 Thread::Sleep(TRY_SLEEP_TIME);
193 return __pConditionManager;
197 _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
199 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
201 AppControl* pAc = null;
203 pAc = pRegs->GetTizenAppControlN(aId, oId);
206 SetLastResult(E_SUCCESS);
210 pAc = pRegs->GetAppControlN(aId, oId);
212 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
213 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
214 SetLastResult(E_SUCCESS);
219 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
220 const String* pUriScheme)
222 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
223 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
226 const String* pMimeType = pDataType;
230 if ((*pDataType)[0] == L'.')
232 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
235 pDataType->SubString(1, ext);
237 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
239 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
241 pMimeType = &mimeType;
243 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
249 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
250 GetErrorMessage(E_INVALID_FORMAT));
253 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
254 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
256 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
260 SetLastResult(E_OBJ_NOT_FOUND);
264 SetLastResult(E_SUCCESS);
265 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
272 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
273 IAppControlListener* pListener)
275 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
280 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
281 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
283 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
284 "At least one of the specified argument must not be null.");
285 if (pDataList != null)
288 argSize = GetTotalSize(*pDataList);
290 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
292 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
293 "The size of pDataList exceeded the limit(%d).",
294 MAX_APPCONTROL_ARGUMENT);
297 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
299 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
300 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
302 _AppMessageImpl::SetOperation(pBundle.get(), operation);
306 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
311 String mimeType = *pDataType;
313 if ((*pDataType)[0] == L'.')
315 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
319 pDataType->SubString(1, ext);
321 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
323 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
325 pMimeType = &mimeType;
327 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
331 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
336 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
339 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
344 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
346 SqlDataControl* pDc = null;
348 String* pAppId = null;
349 String* pAccess = null;
352 // XXX: Need _NativeDataControlRegistry for SLP native app ?
353 // Try to searach SLP data control.
354 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
355 // pDc = pReg->GetNativeSqlDataControlN(providerId);
361 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
362 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
364 pAppId = dynamic_cast< String* >(pList->GetAt(0));
365 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
366 pAccess = dynamic_cast< String* >(pList->GetAt(1));
367 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
370 if (pAppId->StartsWith(L"org.tizen.", 0))
372 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
373 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
374 for (int i = 0; i < 3; ++i)
376 pStrTok->GetNextToken(appId);
379 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
383 appId.Append(*pAppId);
386 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
387 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
393 _AppManagerImpl::GetMapDataControlN(const String& providerId)
395 MapDataControl* pDc = null;
397 String* pAppId = null;
398 String* pAccess = null;
401 // XXX: Need _NativeDataControlRegistry for SLP native app ?
402 // Try to searach SLP data control.
403 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
404 // pDc = pReg->GetNativeMapDataControlN(providerId);
410 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
411 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
413 pAppId = dynamic_cast< String* >(pList->GetAt(0));
414 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
415 pAccess = dynamic_cast< String* >(pList->GetAt(1));
416 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
419 if (pAppId->StartsWith(L"org.tizen.", 0))
421 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
422 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
423 for (int i = 0; i < 3; ++i)
425 pStrTok->GetNextToken(appId);
428 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
432 appId.Append(*pAppId);
435 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
436 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
442 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
444 String pkgId = PackageManager::GetPackageIdByAppId(appId);
445 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
446 "The application is not installed. (app: %ls)", appId.GetPointer());
448 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
449 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
450 "Failed to get _PackageManagerImpl instance.");
452 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
453 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
454 "The application is not installed. (app: %ls)", appId.GetPointer());
456 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
457 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
458 "Failed to get _PackageInfoImpl instance.");
460 appRootPath = pPkgInfoImpl->GetAppRootPath();
461 appRootPath.Append(L"/");
467 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
469 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
470 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
471 "The target application(%ls) is not installed.", appId.GetPointer());
472 // SysTryReturnResult(NID_APP,
473 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
474 // "The length of appid exceeded the limit(%d).",
475 // WIDGET_APP_MAX_APPID_LENGTH);
477 std::unique_ptr<AppControl> pAc(_AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN));
478 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
483 argSize = GetTotalSize(*pArguments);
485 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
487 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
488 "The size of pDataList exceeded the limit(%d).",
489 MAX_APPCONTROL_ARGUMENT);
492 result r = pAc->Start(pArguments, null);
494 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
500 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
502 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
504 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
506 int ret = aul_open_app(pName.get());
510 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
515 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
516 "The target application(%ls) is not installed.", appId.GetPointer());
522 r = E_APP_NOT_INSTALLED;
532 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
538 _AppManagerImpl::TerminateApplication(const AppId& appId)
540 _IAppManager* pMgr = _AppManagerProxy::GetService();
541 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
543 return pMgr->TerminateApplication(appId);
548 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
550 const char* pStr = static_cast<const char*>(pData);
552 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
554 aul_terminate_pid(pAppInfo->pid);
555 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
561 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
563 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
564 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
566 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
571 _AppManagerImpl::IsRunning(const AppId& appId) const
573 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
575 return aul_app_is_running(pAppId.get());
579 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
581 ArrayList* pList = static_cast<ArrayList*>(pData);
582 if (pList && pAppInfo && pAppInfo->appid)
584 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
591 _AppManagerImpl::GetRunningAppListN(void) const
593 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
594 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
595 GetErrorMessage(E_OUT_OF_MEMORY));
596 pRunningAppList->Construct();
598 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
599 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
601 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
602 return pRunningAppList;
607 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
608 AppManager::LaunchOption option)
610 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
611 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
613 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
614 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
617 result r = strTok.GetNextToken(key);
618 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
621 if (key == L"Serial")
623 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
625 else if (key == L"NFC")
627 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
629 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
631 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
635 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
637 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
638 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
642 SysTryReturnResult(NID_APP,
643 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
644 "No such a condition.");
647 return pProxy->UnregisterAppLaunch(appId, pCondition);
651 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
653 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
654 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
655 GetErrorMessage(E_INVALID_STATE));
658 return pProxy->IsAppLaunchRegistered(appId, pCondition);
662 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
664 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
668 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
670 SysLog(NID_APP, "app:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
672 _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
673 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
674 __appManagerEvent.FireAsync(*pArg);
679 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
687 _AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
689 _IAppManager* pMgr = _AppManagerProxy::GetService();
690 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
692 result r = __appManagerEvent.AddListener(listener);
693 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
695 const int count = __appManagerEvent.GetListenerCount();
696 SysLog(NID_APP, "registered event listener(s) # %d", count);
703 return pMgr->AddEventListener(-1);
707 _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
709 _IAppManager* pMgr = _AppManagerProxy::GetService();
710 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
712 result r = __appManagerEvent.RemoveListener(listener);
713 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
715 const int count = __appManagerEvent.GetListenerCount();
716 SysLog(NID_APP, "registered event listener(s) # %d", count);
720 return pMgr->RemoveEventListener(-1);
727 ///////////////////////////////////////////////////////////////////////////////
728 // LifecycleManager begins.
729 ///////////////////////////////////////////////////////////////////////////////
732 _AppLifecycleManager::Init(void)
734 aul_listen_app_launch_signal(LaunchCallback, this);
735 aul_listen_app_dead_signal(TerminateCallback, this);
739 _AppLifecycleManager::Fini(void)
741 aul_listen_app_launch_signal(NULL, NULL);
742 aul_listen_app_dead_signal(NULL, NULL);
746 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
748 if (__pEventList == null)
750 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
751 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
755 __pEventList = pAppEventList.release();
756 SysLog(NID_APP, "Registered app event listener.");
759 return __pEventList->Add(&listener);
763 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
765 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
767 result r = __pEventList->Remove(&listener);
769 if (__pEventList->GetCount() == 0)
781 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
783 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
784 if (pImpl == null || pImpl->__pEventList == null)
786 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
791 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
794 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
798 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
801 const String tmp = appId;
802 pImpl->__map.Add(pid, tmp);
804 while (pEnum->MoveNext() == E_SUCCESS)
806 _IAppEventListener* pListener = null;
807 pEnum->GetCurrent(pListener);
809 pListener->OnApplicationLaunched(tmp, pid);
813 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
819 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
821 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
822 if (pImpl == null || pImpl->__pEventList == null)
824 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
828 // terminate callback cannot acquire appId from pid
830 result r = pImpl->__map.GetValue(pid, tmp);
833 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
837 pImpl->__map.Remove(pid);
839 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
842 while (pEnum->MoveNext() == E_SUCCESS)
844 _IAppEventListener* pListener = null;
845 pEnum->GetCurrent(pListener);
847 pListener->OnApplicationTerminated(tmp, pid);
851 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
856 ///////////////////////////////////////////////////////////////////////////////
857 // LifecycleManager ends.
858 ///////////////////////////////////////////////////////////////////////////////
862 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
864 return __lifeManager.AddListener(listener);
868 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
870 return __lifeManager.RemoveListener(listener);
875 _AppManagerImpl::GetUiLibraryImpl(void)
877 if (__pUiLibrary == null)
879 __pUiLibrary = new (std::nothrow) _LibraryImpl;
880 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
882 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
883 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
886 return *__pUiLibrary;
891 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
893 return __pActiveWindowManager->AddActiveAppEventListener(listener);
898 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
900 return __pActiveWindowManager->RemoveActiveAppEventListener(listener);
905 _AppManagerImpl::GetActiveApp(AppId& appId)
907 return __pActiveWindowManager->GetActiveApp(appId);
912 _AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
914 _IAppManager* pMgr = _AppManagerProxy::GetService();
915 SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
917 return pMgr->IsUserPreferredAppForAppControlResolution(appId);
921 _AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
923 _IAppManager* pMgr = _AppManagerProxy::GetService();
924 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
926 return pMgr->ClearUserPreferenceForAppControlResolution(appId);
930 _AppManagerImpl::AddAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
932 return __appLifecycleEvent.AddListener(listener, false);
936 _AppManagerImpl::RemoveAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
938 return __appLifecycleEvent.RemoveListener(listener);
942 _AppManagerImpl::RegisterAppForAppLifecycleEvent(const AppId& appId)
944 SysLog(NID_APP, "Enter");
946 result r = __mutex.Acquire();
947 SysTryLog(NID_APP, r == E_SUCCESS, "Acquiring mutex failed.");
949 bool isContained = false;
950 r = __appListForAppLifecycle.ContainsKey(appId, isContained);
952 int currentRefCnt = 0;
955 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
956 r = __appListForAppLifecycle.SetValue(appId, ++currentRefCnt);
960 r = __appListForAppLifecycle.Add(appId, currentRefCnt);
962 _IAppManager* pMgr = _AppManagerProxy::GetService();
963 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
965 r = pMgr->RegisterAppForAppLifecycleEvent(appId, -1);
966 SysLog(NID_APP, "The appId(%ls) is registered.", appId.GetPointer());
968 r = __mutex.Release();
969 SysTryLog(NID_APP, r == E_SUCCESS, "Releasing mutex failed.");
971 SysLog(NID_APP, "Exit");
977 _AppManagerImpl::UnregisterAppForAppLifecycleEvent(const AppId& appId)
979 SysLog(NID_APP, "Enter");
981 result r = __mutex.Acquire();
982 SysTryLog(NID_APP, r == E_SUCCESS, "Acquiring mutex failed.");
984 bool isContained = false;
985 r = __appListForAppLifecycle.ContainsKey(appId, isContained);
989 int currentRefCnt = 0;
990 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
994 if (currentRefCnt < 0)
996 r = __appListForAppLifecycle.Remove(appId);
998 _IAppManager* pMgr = _AppManagerProxy::GetService();
999 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
1001 r = pMgr->UnregisterAppForAppLifecycleEvent(appId, -1);
1003 SysLog(NID_APP, "The appId(%ls) is unregistered.", appId.GetPointer());
1007 r = __appListForAppLifecycle.SetValue(appId, currentRefCnt);
1012 SysLog(NID_APP, "The appId(%ls) is not registered.", appId.GetPointer());
1015 r = __mutex.Release();
1016 SysTryLog(NID_APP, r == E_SUCCESS, "Releasing mutex failed.");
1018 SysLog(NID_APP, "Exit");
1025 _AppManagerImpl::OnAppLifecycleEventReceived(int clientId,const AppId& appId, _AppLifecycleEventType appLifecycleEventType)
1027 SysLog(NID_APP, "Enter appId(%ls), appLifecycleEventType(%d)", appId.GetPointer(), appLifecycleEventType);
1029 _AppLifecycleEventArg* pArg = new (std::nothrow)_AppLifecycleEventArg(appId, appLifecycleEventType);
1030 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1032 __appLifecycleEvent.FireAsync(*pArg);