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);
199 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
200 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
201 SetLastResult(E_SUCCESS);
206 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
207 const String* pUriScheme)
209 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
210 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
213 const String* pMimeType = pDataType;
217 if ((*pDataType)[0] == L'.')
219 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
222 pDataType->SubString(1, ext);
224 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
226 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
228 pMimeType = &mimeType;
230 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
236 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
237 GetErrorMessage(E_INVALID_FORMAT));
240 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
241 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
243 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
247 SetLastResult(E_OBJ_NOT_FOUND);
251 SetLastResult(E_SUCCESS);
252 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
259 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
260 IAppControlListener* pListener)
262 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
267 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
268 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
270 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
271 "At least one of the specified argument must not be null.");
272 if (pDataList != null)
275 argSize = GetTotalSize(*pDataList);
277 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
279 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
280 "The size of pDataList exceeded the limit(%d).",
281 MAX_APPCONTROL_ARGUMENT);
284 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
286 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
287 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
289 _AppMessageImpl::SetOperation(pBundle.get(), operation);
293 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
298 String mimeType = *pDataType;
300 if ((*pDataType)[0] == L'.')
302 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
306 pDataType->SubString(1, ext);
308 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
310 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
312 pMimeType = &mimeType;
314 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
318 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
323 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
326 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
331 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
333 SqlDataControl* pDc = null;
335 String* pAppId = null;
336 String* pAccess = null;
339 // XXX: Need _NativeDataControlRegistry for SLP native app ?
340 // Try to searach SLP data control.
341 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
342 // pDc = pReg->GetNativeSqlDataControlN(providerId);
348 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
349 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
351 pAppId = dynamic_cast< String* >(pList->GetAt(0));
352 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
353 pAccess = dynamic_cast< String* >(pList->GetAt(1));
354 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
357 if (pAppId->StartsWith(L"org.tizen.", 0))
359 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
360 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
361 for (int i = 0; i < 3; ++i)
363 pStrTok->GetNextToken(appId);
366 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
370 appId.Append(*pAppId);
373 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
374 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
380 _AppManagerImpl::GetMapDataControlN(const String& providerId)
382 MapDataControl* pDc = null;
384 String* pAppId = null;
385 String* pAccess = null;
388 // XXX: Need _NativeDataControlRegistry for SLP native app ?
389 // Try to searach SLP data control.
390 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
391 // pDc = pReg->GetNativeMapDataControlN(providerId);
397 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
398 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
400 pAppId = dynamic_cast< String* >(pList->GetAt(0));
401 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
402 pAccess = dynamic_cast< String* >(pList->GetAt(1));
403 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
406 if (pAppId->StartsWith(L"org.tizen.", 0))
408 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
409 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
410 for (int i = 0; i < 3; ++i)
412 pStrTok->GetNextToken(appId);
415 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
419 appId.Append(*pAppId);
422 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
423 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
429 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
431 String pkgId = PackageManager::GetPackageIdByAppId(appId);
432 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
433 "The application is not installed. (app: %ls)", appId.GetPointer());
435 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
436 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
437 "Failed to get _PackageManagerImpl instance.");
439 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
440 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
441 "The application is not installed. (app: %ls)", appId.GetPointer());
443 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
444 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
445 "Failed to get _PackageInfoImpl instance.");
447 appRootPath = pPkgInfoImpl->GetAppRootPath();
448 appRootPath.Append(L"/");
454 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
456 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
457 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
458 "The target application(%ls) is not installed.", appId.GetPointer());
459 // SysTryReturnResult(NID_APP,
460 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
461 // "The length of appid exceeded the limit(%d).",
462 // WIDGET_APP_MAX_APPID_LENGTH);
464 std::unique_ptr<AppControl> pAc(FindAppControlN(appId, TIZEN_OPERATION_MAIN));
465 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
470 argSize = GetTotalSize(*pArguments);
472 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
474 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
475 "The size of pDataList exceeded the limit(%d).",
476 MAX_APPCONTROL_ARGUMENT);
479 result r = pAc->Start(pArguments, null);
481 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
487 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
489 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
491 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
493 int ret = aul_open_app(pName.get());
497 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
502 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
503 "The target application(%ls) is not installed.", appId.GetPointer());
509 r = E_APP_NOT_INSTALLED;
519 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
525 _AppManagerImpl::TerminateApplication(const AppId& appId)
527 _IAppManager* pMgr = _AppManagerProxy::GetService();
528 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
530 return pMgr->TerminateApplication(appId);
535 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
537 const char* pStr = static_cast<const char*>(pData);
539 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
541 aul_terminate_pid(pAppInfo->pid);
542 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
548 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
550 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
551 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
553 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
558 _AppManagerImpl::IsRunning(const AppId& appId) const
560 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
562 return aul_app_is_running(pAppId.get());
566 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
568 ArrayList* pList = static_cast<ArrayList*>(pData);
569 if (pList && pAppInfo && pAppInfo->appid)
571 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
578 _AppManagerImpl::GetRunningAppListN(void) const
580 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
581 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
582 GetErrorMessage(E_OUT_OF_MEMORY));
583 pRunningAppList->Construct();
585 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
586 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
588 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
589 return pRunningAppList;
594 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
595 AppManager::LaunchOption option)
597 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
598 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
600 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
601 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
604 result r = strTok.GetNextToken(key);
605 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
608 if (key == L"Serial")
610 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
612 else if (key == L"NFC")
614 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
616 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
618 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
622 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
624 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
625 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
629 SysTryReturnResult(NID_APP,
630 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
631 "No such a condition.");
634 return pProxy->UnregisterAppLaunch(appId, pCondition);
638 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
640 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
641 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
642 GetErrorMessage(E_INVALID_STATE));
645 return pProxy->IsAppLaunchRegistered(appId, pCondition);
649 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
651 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
656 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
664 ///////////////////////////////////////////////////////////////////////////////
665 // LifecycleManager begins.
666 ///////////////////////////////////////////////////////////////////////////////
669 _AppLifecycleManager::Init(void)
671 aul_listen_app_launch_signal(LaunchCallback, this);
672 aul_listen_app_dead_signal(TerminateCallback, this);
676 _AppLifecycleManager::Fini(void)
678 aul_listen_app_launch_signal(NULL, NULL);
679 aul_listen_app_dead_signal(NULL, NULL);
683 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
685 if (__pEventList == null)
687 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
688 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
692 __pEventList = pAppEventList.release();
693 SysLog(NID_APP, "Registered app event listener.");
696 return __pEventList->Add(&listener);
700 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
702 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
704 result r = __pEventList->Remove(&listener);
706 if (__pEventList->GetCount() == 0)
718 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
720 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
721 if (pImpl == null || pImpl->__pEventList == null)
723 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
728 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
731 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
735 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
738 const String tmp = appId;
739 pImpl->__map.Add(pid, tmp);
741 while (pEnum->MoveNext() == E_SUCCESS)
743 _IAppEventListener* pListener = null;
744 pEnum->GetCurrent(pListener);
746 pListener->OnApplicationLaunched(tmp, pid);
750 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
756 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
758 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
759 if (pImpl == null || pImpl->__pEventList == null)
761 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
765 // terminate callback cannot acquire appId from pid
767 result r = pImpl->__map.GetValue(pid, tmp);
770 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
774 pImpl->__map.Remove(pid);
776 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
779 while (pEnum->MoveNext() == E_SUCCESS)
781 _IAppEventListener* pListener = null;
782 pEnum->GetCurrent(pListener);
784 pListener->OnApplicationTerminated(tmp, pid);
788 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
793 ///////////////////////////////////////////////////////////////////////////////
794 // LifecycleManager ends.
795 ///////////////////////////////////////////////////////////////////////////////
799 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
801 return __lifeManager.AddListener(listener);
805 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
807 return __lifeManager.RemoveListener(listener);
812 _AppManagerImpl::GetUiLibraryImpl(void)
814 if (__pUiLibrary == null)
816 __pUiLibrary = new (std::nothrow) _LibraryImpl;
817 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
819 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
820 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
823 return *__pUiLibrary;
828 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
830 return __pActiveWindowManager->AddActiveAppEventListener(listener);
835 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
837 return __pActiveWindowManager->RemoveActiveAppEventListener(listener);
842 _AppManagerImpl::GetActiveApp(AppId& appId)
844 return __pActiveWindowManager->GetActiveApp(appId);
849 _AppManagerImpl::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
851 _IAppManager* pMgr = _AppManagerProxy::GetService();
852 SysTryReturn(NID_APP, pMgr, false, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
854 return pMgr->IsUserPreferredAppForAppControlResolution(appId);
858 _AppManagerImpl::ClearUserPreferenceForAppControlResolution(const AppId& appId)
860 _IAppManager* pMgr = _AppManagerProxy::GetService();
861 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
863 return pMgr->ClearUserPreferenceForAppControlResolution(appId);
867 _AppManagerImpl::AddAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
869 return __appLifecycleEvent.AddListener(listener, false);
873 _AppManagerImpl::RemoveAppLifecycleEventListener(_IAppLifecycleEventListener& listener)
875 return __appLifecycleEvent.RemoveListener(listener);
879 _AppManagerImpl::RegisterAppForAppLifecycleEvent(const AppId& appId)
881 SysLog(NID_APP, "Enter");
883 result res = __mutex.Acquire();
884 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
886 bool isContained = false;
887 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
889 int currentRefCnt = 0;
892 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
893 r = __appListForAppLifecycle.SetValue(appId, ++currentRefCnt);
897 r = __appListForAppLifecycle.Add(appId, currentRefCnt);
899 _IAppManager* pMgr = _AppManagerProxy::GetService();
900 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
902 r = pMgr->RegisterAppForAppLifecycleEvent(appId, -1);
903 SysLog(NID_APP, "The appId(%ls) is registered.", appId.GetPointer());
905 res = __mutex.Release();
906 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
908 SysLog(NID_APP, "Exit");
914 _AppManagerImpl::UnregisterAppForAppLifecycleEvent(const AppId& appId)
916 SysLog(NID_APP, "Enter");
918 result res = __mutex.Acquire();
919 SysTryLog(NID_APP, res == E_SUCCESS, "Acquiring mutex failed.");
921 bool isContained = false;
922 result r = __appListForAppLifecycle.ContainsKey(appId, isContained);
926 int currentRefCnt = 0;
927 r = __appListForAppLifecycle.GetValue(appId, currentRefCnt);
931 if (currentRefCnt < 0)
933 r = __appListForAppLifecycle.Remove(appId);
935 _IAppManager* pMgr = _AppManagerProxy::GetService();
936 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "_AppManagerProxy::GetService() is failed.");
938 r = pMgr->UnregisterAppForAppLifecycleEvent(appId, -1);
940 SysLog(NID_APP, "The appId(%ls) is unregistered.", appId.GetPointer());
944 r = __appListForAppLifecycle.SetValue(appId, currentRefCnt);
949 SysLog(NID_APP, "The appId(%ls) is not registered.", appId.GetPointer());
952 res = __mutex.Release();
953 SysTryLog(NID_APP, res == E_SUCCESS, "Releasing mutex failed.");
955 SysLog(NID_APP, "Exit");
962 _AppManagerImpl::OnAppLifecycleEventReceived(int clientId,const AppId& appId, _AppLifecycleEventType appLifecycleEventType)
964 SysLog(NID_APP, "Enter appId(%ls), appLifecycleEventType(%d)", appId.GetPointer(), appLifecycleEventType);
966 _AppLifecycleEventArg* pArg = new (std::nothrow)_AppLifecycleEventArg(appId, appLifecycleEventType);
967 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
969 __appLifecycleEvent.FireAsync(*pArg);