2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FApp_AppManagerImpl.cpp
20 * @brief This is the implementation for the _AppManagerImpl class.
26 #include <unique_ptr.h>
30 #include <app_manager.h>
35 #include <FBaseInteger.h>
36 #include <FBaseObject.h>
37 #include <FBaseString.h>
38 #include <FBaseColArrayList.h>
39 #include <FBaseErrors.h>
40 #include <FAppAppControl.h>
41 #include <FAppAppManager.h>
42 #include <FAppSqlDataControl.h>
43 #include <FAppMapDataControl.h>
44 #include <FAppPkgPackageInfo.h>
45 #include <FAppIActiveAppEventListener.h>
46 #include <FBaseSysLog.h>
47 #include <FAppIAppControlListener.h>
49 #include <FBase_StringConverter.h>
50 #include <FBaseRt_LibraryImpl.h>
51 #include <FIo_DataControlResultSetImpl.h>
53 #include "FApp_AppControlRegistry.h"
54 #include "FApp_AppImpl.h"
55 #include "FApp_AppInfo.h"
56 #include "FApp_AppManagerEventArg.h"
57 #include "FApp_AppManagerImpl.h"
58 #include "FApp_AppManagerProxy.h"
59 #include "FApp_AppMessageImpl.h"
60 #include "FApp_AppControlImpl.h"
61 #include "FApp_ConditionManagerProxy.h"
62 #include "FApp_IAppManagerEventListener.h"
63 #include "FApp_IAppEventListener.h"
64 #include "FApp_MapDataControlImpl.h"
65 #include "FApp_SqlDataControlImpl.h"
66 #include "FAppPkg_PackageManagerImpl.h"
67 #include "FAppPkg_PackageInfoImpl.h"
68 #include "FApp_AppControlManager.h"
71 using namespace Tizen::App::Package;
72 using namespace Tizen::Base;
73 using namespace Tizen::Base::Collection;
74 using namespace Tizen::Base::Runtime;
75 using namespace Tizen::Base::Utility;
76 using namespace Tizen::Io;
78 //extern const char* _DATACONTROL_RESULTSET_DIR;
83 //const long MAX_APPCONTROL_ARGUMENT = 4096;
84 const long MAX_APPCONTROL_ARGUMENT = 1024;
85 const long MAX_CONDITION_LENGTH = 400;
87 // ActiveWindow related function and variable from shared library.
89 static Ecore_X_Atom(* p_ecore_x_atom_get)(const char* name) = null;
90 static int(* p_ecore_x_window_prop_window_get)(Ecore_X_Window win, Ecore_X_Atom atom, Ecore_X_Window* val, unsigned int len) = null;
91 static Eina_Bool(* p_ecore_x_netwm_pid_get)(Ecore_X_Window win, int* pid) = null;
92 static int(* p_ecore_x_netwm_name_get)(Ecore_X_Window win, char** name) = null;
93 static Ecore_X_Window* (* p_ecore_x_window_root_list)(int* num_ret) = null;
94 static int* p_ECORE_X_EVENT_WINDOW_PROPERTY = null;
96 static Ecore_Event_Handler* (* p_ecore_event_handler_add)(int type, Ecore_Event_Handler_Cb func, const void* data) = null;
97 static void* (* p_ecore_event_handler_del)(Ecore_Event_Handler* event_handler) = null;
99 static int (* p_XSelectInput)(Display* display, Window w, long event_mask) = null;
100 static Display* (* p_XOpenDisplay)(_Xconst char* display_name) = null;
101 static int (* p_XCloseDisplay)(Display* display) = null;
103 Ecore_Event_Handler* pWindowPropertyChanged = null;
105 struct _DisplayDeleter
107 void operator()(Display* pDisplay)
111 p_XCloseDisplay(pDisplay);
117 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
120 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
124 while (pEnum->MoveNext() == E_SUCCESS)
126 String* pStr = static_cast<String*>(pEnum->GetCurrent());
131 size += pStr->GetLength();
139 OnPropertyChanged(void* pData, int type, void* pEvent)
141 using namespace Tizen::App;
143 if (p_ecore_x_atom_get == null)
145 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
146 p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
147 SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, EINA_FALSE,
148 "A system error has been occurred. Failed to get ecore_x_atom_get.");
150 if (p_ecore_x_window_prop_window_get == null)
152 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
153 p_ecore_x_window_prop_window_get = reinterpret_cast<int(*)(Ecore_X_Window win, Ecore_X_Atom atom, Ecore_X_Window* val, unsigned int len)>(lib.GetProcAddress(L"ecore_x_window_prop_window_get"));
154 SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
155 "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
157 if (p_ecore_x_netwm_pid_get == null)
159 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
160 p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
161 SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
162 "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
164 if (p_ecore_x_netwm_name_get == null)
166 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
167 p_ecore_x_netwm_name_get = reinterpret_cast<int(*)(Ecore_X_Window win, char** name)>(lib.GetProcAddress(L"ecore_x_netwm_name_get"));
168 SysTryReturnResult(NID_APP, p_ecore_x_netwm_name_get != null, EINA_FALSE,
169 "A system error has been occurred. Failed to get ecore_x_netwm_name_get.");
174 SysLog(NID_UI, "The data is not valid.");
179 SysLog(NID_UI, "The event is not valid.");
183 Ecore_X_Event_Window_Property* pE = (Ecore_X_Event_Window_Property*) pEvent;
184 Ecore_X_Atom atom = pE->atom;
185 Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
187 if (atom != activeAtom)
189 return ECORE_CALLBACK_PASS_ON;
192 Ecore_X_Window win = pE->win;
193 Ecore_X_Window activeWin = 0;
194 p_ecore_x_window_prop_window_get(win, activeAtom, &activeWin, 1);
197 p_ecore_x_netwm_pid_get(activeWin, &pid);
199 char* pAppName = null;
200 p_ecore_x_netwm_name_get(activeWin, &pAppName);
202 Tizen::App::_AppManagerImpl* pAppManagerImpl = static_cast<Tizen::App::_AppManagerImpl*>(pData);
203 pAppManagerImpl->FireActiveAppEvent(activeWin, pid, pAppName);
210 return ECORE_CALLBACK_PASS_ON;
213 } // anonymous name-space
216 namespace Tizen { namespace App
219 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
220 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
221 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
222 const wchar_t OSP_ECORE_X_SONAME[] = L"libecore_x.so.1";
223 const wchar_t OSP_ECORE_SONAME[] = L"libecore.so.1";
224 const wchar_t OSP_X11_SONAME[] = L"libX11.so.6";
225 const int _MAX_PACKAGE_ID_LENGTH = 10;
227 _LibraryImpl* _AppManagerImpl::__pEcoreLibrary = null;
228 _LibraryImpl* _AppManagerImpl::__pEcoreXLibrary = null;
230 _AppManagerImpl::_AppManagerImpl(void)
231 : __pConditionManager(null)
232 , __eventListenerCount(0)
234 , __pX11Library(null)
235 , __pAppEventList(null)
240 _AppManagerImpl::~_AppManagerImpl(void)
244 if (__activeAppEventListenerList.GetCount() > 0)
246 if (p_ecore_event_handler_del == null)
248 _LibraryImpl& lib = GetEcoreLibraryImpl();
249 p_ecore_event_handler_del = reinterpret_cast<void*(*)(Ecore_Event_Handler* event_handler)>(lib.GetProcAddress(L"ecore_event_handler_del"));
251 if (p_ecore_event_handler_del)
253 if (pWindowPropertyChanged)
255 p_ecore_event_handler_del(pWindowPropertyChanged);
256 pWindowPropertyChanged = null;
261 delete __pConditionManager;
263 delete __pX11Library;
264 delete __pEcoreXLibrary;
265 __pEcoreXLibrary = null;
266 delete __pEcoreLibrary;
267 __pEcoreLibrary = null;
272 _AppManagerImpl::Construct(void)
274 __appManagerEvent.Construct();
276 _IAppManager* pMgr = _AppManagerProxy::GetService();
277 //todo : uncomment following _SysTryReturn or put assert.
278 //SysTryReturn(NID_APP, pMgr != null, GetLastResult(), GetLastResult(), "[%s]GetService failed. Please check 'ps -A | grep OspAppService'!", GetLastResult());
279 SysTryReturn(NID_APP, pMgr != null, E_SUCCESS, E_SUCCESS, "[E_SYSTEM] fatal error. Please check 'ps -A | grep osp-app-service'!", GetLastResult());
280 pMgr->InitEventListener(this);
286 _AppManagerImpl::GetInstance(void)
288 return AppManager::GetInstance()->__pAppManagerImpl;
291 _ConditionManagerProxy*
292 _AppManagerImpl::GetConditionManagerProxy(void)
294 if (__pConditionManager == null)
296 __pConditionManager = new (std::nothrow) _ConditionManagerProxy;
297 SysAssert(__pConditionManager != null);
299 result r = __pConditionManager->Construct();
300 SysAssertf(r == E_SUCCESS, "__pConditionManager->Construct() faliied [%s].", GetErrorMessage(r));
303 return __pConditionManager;
307 _AppManagerImpl::FindAppControlN(const AppId& aId, const String& oId)
309 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
311 AppControl* pAc = null;
313 pAc = pRegs->GetNativeAppControlN(aId, oId);
316 SetLastResult(E_SUCCESS);
320 pAc = pRegs->GetAliasAppControlN(aId, oId);
323 SetLastResult(E_SUCCESS);
327 pAc = pRegs->GetAppControlN(aId, oId);
329 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
330 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
331 SetLastResult(E_SUCCESS);
336 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
337 const String* pUriScheme)
339 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
340 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
343 const String* pMimeType = pDataType;
347 if ((*pDataType)[0] == L'.')
349 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
352 pDataType->SubString(1, ext);
354 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
356 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
358 pMimeType = &mimeType;
360 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
366 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
367 GetErrorMessage(E_INVALID_FORMAT));
370 ArrayList* pRetArg = null;
371 pRetArg = new (std::nothrow) ArrayList();
372 SysTryReturn(NID_APP, pRetArg != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
373 GetErrorMessage(E_OUT_OF_MEMORY));
375 pRetArg->Construct();
377 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
379 pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
381 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
385 SetLastResult(E_OBJ_NOT_FOUND);
389 SetLastResult(E_SUCCESS);
390 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
397 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
398 IAppControlListener* pListener)
400 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
405 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
406 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
408 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
409 "At least one of the specified argument must not be null.");
410 if (pDataList != null)
413 argSize = GetTotalSize(*pDataList);
415 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
417 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
418 "The size of pDataList exceeded the limit(%d).",
419 MAX_APPCONTROL_ARGUMENT);
422 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
424 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
425 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
427 _AppMessageImpl::SetOperation(pBundle.get(), operation);
431 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
436 String mimeType = *pDataType;
438 if ((*pDataType)[0] == L'.')
440 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
444 pDataType->SubString(1, ext);
446 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
448 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
450 pMimeType = &mimeType;
452 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
456 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
461 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
464 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
469 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
471 SqlDataControl* pDc = null;
473 String* pAppId = null;
474 String* pAccess = null;
477 // XXX: Need _NativeDataControlRegistry for SLP native app ?
478 // Try to searach SLP data control.
479 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
480 // pDc = pReg->GetNativeSqlDataControlN(providerId);
486 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
487 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
489 pAppId = dynamic_cast< String* >(pList->GetAt(0));
490 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
491 pAccess = dynamic_cast< String* >(pList->GetAt(1));
492 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
495 if (pAppId->StartsWith(L"org.tizen.", 0))
497 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
498 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
499 for (int i = 0; i < 3; ++i)
501 pStrTok->GetNextToken(appId);
504 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
508 appId.Append(*pAppId);
511 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
512 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
518 _AppManagerImpl::GetMapDataControlN(const String& providerId)
520 MapDataControl* pDc = null;
522 String* pAppId = null;
523 String* pAccess = null;
526 // XXX: Need _NativeDataControlRegistry for SLP native app ?
527 // Try to searach SLP data control.
528 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
529 // pDc = pReg->GetNativeMapDataControlN(providerId);
535 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
536 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
538 pAppId = dynamic_cast< String* >(pList->GetAt(0));
539 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
540 pAccess = dynamic_cast< String* >(pList->GetAt(1));
541 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
544 if (pAppId->StartsWith(L"org.tizen.", 0))
546 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
547 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
548 for (int i = 0; i < 3; ++i)
550 pStrTok->GetNextToken(appId);
553 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
557 appId.Append(*pAppId);
560 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
561 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
567 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
569 String pkgId = PackageManager::GetPackageIdByAppId(appId);
570 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
571 "The application is not installed. (appId: %ls)", appId.GetPointer());
573 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
574 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
575 "Failed to get _PackageManagerImpl instance.");
577 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
578 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
579 "The application is not installed. (appId: %ls)", appId.GetPointer());
581 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
582 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
583 "Failed to get _PackageInfoImpl instance.");
585 appRootPath = pPkgInfoImpl->GetAppRootPath();
586 appRootPath.Append(L"/");
592 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
594 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
595 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
596 "The target application(%ls) is not installed.", appId.GetPointer());
597 // SysTryReturnResult(NID_APP,
598 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
599 // "The length of appid exceeded the limit(%d).",
600 // WIDGET_APP_MAX_APPID_LENGTH);
602 AppControl* pAc = _AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN);
603 SysTryReturnResult(NID_APP, pAc != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
608 argSize = GetTotalSize(*pArguments);
610 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
612 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
613 "The size of pDataList exceeded the limit(%d).",
614 MAX_APPCONTROL_ARGUMENT);
617 result r = pAc->Start(pArguments, null);
619 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
625 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
627 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
629 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
631 int ret = aul_open_app(pName.get());
635 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
640 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
641 "The target application(%ls) is not installed.", appId.GetPointer());
647 r = E_APP_NOT_INSTALLED;
657 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
663 _AppManagerImpl::TerminateApplication(const AppId& appId)
665 _IAppManager* pMgr = _AppManagerProxy::GetService();
666 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
668 return pMgr->TerminateApplication(appId);
673 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
675 const char* pStr = static_cast<const char*>(pData);
677 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
679 aul_terminate_pid(pAppInfo->pid);
680 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
686 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
688 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
689 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
691 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
696 _AppManagerImpl::IsRunning(const AppId& appId) const
698 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
700 return aul_app_is_running(pAppId.get());
704 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
706 ArrayList* pList = static_cast<ArrayList*>(pData);
707 if (pList && pAppInfo && pAppInfo->appid)
709 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
716 _AppManagerImpl::GetRunningAppListN(void) const
718 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
719 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
720 GetErrorMessage(E_OUT_OF_MEMORY));
721 pRunningAppList->Construct();
723 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
724 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
726 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
727 return pRunningAppList;
732 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
733 AppManager::LaunchOption option)
735 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
736 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
738 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
742 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
744 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
745 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
749 SysTryReturnResult(NID_APP,
750 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
751 "No such a condition.");
754 return pProxy->UnregisterAppLaunch(appId, pCondition);
758 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
760 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
761 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
762 GetErrorMessage(E_INVALID_STATE));
765 return pProxy->IsAppLaunchRegistered(appId, pCondition);
769 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
771 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
775 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
777 SysLog(NID_APP, "appId:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
779 _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
780 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
781 __appManagerEvent.FireAsync(*pArg);
786 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
794 _AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
796 _IAppManager* pMgr = _AppManagerProxy::GetService();
797 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
799 result r = __appManagerEvent.AddListener(listener);
800 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
802 __eventListenerCount++;
803 SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
805 if( __eventListenerCount > 1)
810 return pMgr->AddEventListener(-1);
814 _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
816 _IAppManager* pMgr = _AppManagerProxy::GetService();
817 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
819 result r = __appManagerEvent.RemoveListener(listener);
820 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
822 __eventListenerCount--;
823 SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
825 if (__eventListenerCount == 0)
827 return pMgr->RemoveEventListener(-1);
835 _AppManagerImpl::AppEventCallback(app_context_h app_context, app_context_event_e event, void* pData)
837 _AppManagerImpl* pImpl = static_cast<_AppManagerImpl*>(pData);
838 if (pImpl == null || pImpl->__pAppEventList == null)
840 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppManagerImpl state.");
844 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pAppEventList->GetEnumeratorN());
849 app_context_get_app_id(app_context, &pAppId);
852 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire appId.");
856 const String appId = pAppId;
861 app_context_get_pid(app_context, &pid);
862 const int val = event;
867 case APP_CONTEXT_EVENT_LAUNCHED:
868 while (pEnum->MoveNext() == E_SUCCESS)
870 _IAppEventListener* pListener = null;
871 pEnum->GetCurrent(pListener);
873 pListener->OnApplicationLaunched(appId, pid);
876 case APP_CONTEXT_EVENT_TERMINATED:
877 while (pEnum->MoveNext() == E_SUCCESS)
879 _IAppEventListener* pListener = null;
880 pEnum->GetCurrent(pListener);
882 pListener->OnApplicationTerminated(appId, pid);
886 SysLog(NID_APP, "Invalid state.");
891 SysLog(NID_APP, "Finished invoking application event listener.");
895 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
897 if (__pAppEventList == null)
899 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
900 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
902 const int ret = app_manager_set_app_context_event_cb(_AppManagerImpl::AppEventCallback, this);
903 SysTryReturnResult(NID_APP, ret == APP_MANAGER_ERROR_NONE, E_SYSTEM, "Context registration failed.");
905 __pAppEventList = pAppEventList.release();
906 SysLog(NID_APP, "Registered app event listener.");
909 return __pAppEventList->Add(&listener);
913 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
915 result r = E_SUCCESS;
917 SysTryReturnResult(NID_APP,__pAppEventList != null, E_OBJ_NOT_FOUND, "_IAppEventListener list is empty.");
919 r = __pAppEventList->Remove(&listener);
921 if (__pAppEventList->GetCount() == 0)
923 app_manager_unset_app_context_event_cb();
925 delete __pAppEventList;
926 __pAppEventList = null;
934 _AppManagerImpl::GetUiLibraryImpl(void)
936 if (__pUiLibrary == null)
938 __pUiLibrary = new (std::nothrow) _LibraryImpl;
939 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
941 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
942 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
945 return *__pUiLibrary;
949 _AppManagerImpl::GetEcoreXLibraryImpl(void)
951 if (__pEcoreXLibrary == null)
953 __pEcoreXLibrary = new (std::nothrow) _LibraryImpl;
954 SysAssertf(__pEcoreXLibrary != null, "_LibraryImpl allocation failure.");
956 result r = __pEcoreXLibrary->Construct(OSP_ECORE_X_SONAME);
957 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
959 return *__pEcoreXLibrary;
963 _AppManagerImpl::GetEcoreLibraryImpl(void)
965 if (__pEcoreLibrary == null)
967 __pEcoreLibrary = new (std::nothrow) _LibraryImpl;
968 SysAssertf(__pEcoreLibrary != null, "_LibraryImpl allocation failure.");
970 result r = __pEcoreLibrary->Construct(OSP_ECORE_SONAME);
971 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
973 return *__pEcoreLibrary;
977 _AppManagerImpl::GetX11LibraryImpl(void)
979 if (__pX11Library == null)
981 __pX11Library = new (std::nothrow) _LibraryImpl;
982 SysAssertf(__pX11Library != null, "_LibraryImpl allocation failure.");
984 result r = __pX11Library->Construct(OSP_X11_SONAME);
985 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
987 return *__pX11Library;
991 _AppManagerImpl::FireActiveAppEvent(unsigned int xid, int pid, char* pAppName)
993 static int oldPid = 0;
997 std::unique_ptr<IEnumeratorT<Tizen::App::IActiveAppEventListener* > > pEnum(__activeAppEventListenerList.GetEnumeratorN());
1000 while (pEnum->MoveNext() == E_SUCCESS)
1002 Tizen::App::IActiveAppEventListener* pListener = null;
1003 pEnum->GetCurrent(pListener);
1006 char pkgname[255] = {0, };
1007 aul_app_get_pkgname_bypid(pid, pkgname, 255);
1008 // TODO: Translate it to package name --> AppId
1009 pListener->OnActiveAppChanged(AppId(pkgname));
1017 _AppManagerImpl::GetActiveWindow(void)
1019 if (p_ecore_x_window_root_list == null)
1021 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1022 p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
1023 SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, 0,
1024 "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
1026 if (p_ecore_x_atom_get == null)
1028 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1029 p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
1030 SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, 0,
1031 "A system error has been occurred. Failed to get ecore_x_atom_get.");
1033 if (p_ecore_x_window_prop_window_get == null)
1035 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1036 p_ecore_x_window_prop_window_get = reinterpret_cast<int(*)(Ecore_X_Window win, Ecore_X_Atom atom, Ecore_X_Window* val, unsigned int len)>(lib.GetProcAddress(L"ecore_x_window_prop_window_get"));
1037 SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
1038 "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
1042 Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
1044 Ecore_X_Window activeWin = 0;
1047 Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
1048 p_ecore_x_window_prop_window_get(pRoots[0], activeAtom, &activeWin, 1);
1056 _AppManagerImpl::GetProcessId(unsigned int window)
1058 if (p_ecore_x_netwm_pid_get == null)
1060 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1061 p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
1062 SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
1063 "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
1067 p_ecore_x_netwm_pid_get(window, &pid);
1073 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
1075 if (p_ecore_x_window_root_list == null)
1077 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1078 p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
1079 SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, E_SYSTEM,
1080 "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
1082 if (p_ECORE_X_EVENT_WINDOW_PROPERTY == null)
1084 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1085 p_ECORE_X_EVENT_WINDOW_PROPERTY = reinterpret_cast<int*>(lib.GetProcAddress(L"ECORE_X_EVENT_WINDOW_PROPERTY"));
1086 SysTryReturnResult(NID_APP, p_ECORE_X_EVENT_WINDOW_PROPERTY != null, E_SYSTEM,
1087 "A system error has been occurred. Failed to get p_ECORE_X_EVENT_WINDOW_PROPERTY.");
1089 if (p_XOpenDisplay == null)
1091 _LibraryImpl& lib = GetX11LibraryImpl();
1092 p_XOpenDisplay = reinterpret_cast<Display*(*)(_Xconst char* display_name)>(lib.GetProcAddress(L"XOpenDisplay"));
1093 SysTryReturnResult(NID_APP, p_XOpenDisplay != null, E_SYSTEM,
1094 "A system error has been occurred. Failed to get p_XOpenDisplay.");
1096 if (p_XCloseDisplay == null)
1098 _LibraryImpl& lib = GetX11LibraryImpl();
1099 p_XCloseDisplay = reinterpret_cast<int(*)(Display* display)>(lib.GetProcAddress(L"XCloseDisplay"));
1100 SysTryReturnResult(NID_APP, p_XCloseDisplay != null, E_SYSTEM,
1101 "A system error has been occurred. Failed to get p_XCloseDisplay.");
1103 if (p_XSelectInput == null)
1105 _LibraryImpl& lib = GetX11LibraryImpl();
1106 p_XSelectInput = reinterpret_cast<int(*)(Display* display, Window w, long event_mask)>(lib.GetProcAddress(L"XSelectInput"));
1107 SysTryReturnResult(NID_APP, p_XSelectInput != null, E_SYSTEM,
1108 "A system error has been occurred. Failed to get p_XSelectInput.");
1110 if (p_ecore_event_handler_add == null)
1112 _LibraryImpl& lib = GetEcoreLibraryImpl();
1113 p_ecore_event_handler_add = reinterpret_cast<Ecore_Event_Handler*(*)(int type, Ecore_Event_Handler_Cb func, const void* data)>(lib.GetProcAddress(L"ecore_event_handler_add"));
1114 SysTryReturnResult(NID_APP, p_ecore_event_handler_add != null, E_SYSTEM,
1115 "A system error has been occurred. Failed to get p_ecore_event_handler_add.");
1118 bool alreadyExist = __activeAppEventListenerList.Contains(&listener);
1119 SysTryReturnResult(NID_APP, !alreadyExist, E_OBJ_ALREADY_EXIST, "The event listener already exist.");
1120 result r = __activeAppEventListenerList.Add(&listener);
1121 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1123 if (!pWindowPropertyChanged)
1125 std::unique_ptr<Display, _DisplayDeleter> pDisplay(p_XOpenDisplay(NULL));
1126 SysTryReturnResult(NID_APP, pDisplay != null, E_SYSTEM, "A system error has been occurred. Failed to XOpenDisplay.");
1129 Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
1130 SysTryReturnResult(NID_APP, pRoots != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1132 for (int i = 0; i < num; i++)
1134 p_XSelectInput(pDisplay.get(), pRoots[i], PropertyChangeMask);
1137 pWindowPropertyChanged = p_ecore_event_handler_add(*p_ECORE_X_EVENT_WINDOW_PROPERTY, OnPropertyChanged, (void*) this);
1139 SysTryReturnResult(NID_APP, pWindowPropertyChanged, E_SYSTEM, "A system error has been occurred.");
1146 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
1148 if (p_ecore_event_handler_del == null)
1150 _LibraryImpl& lib = GetEcoreLibraryImpl();
1151 p_ecore_event_handler_del = reinterpret_cast<void*(*)(Ecore_Event_Handler* event_handler)>(lib.GetProcAddress(L"ecore_event_handler_del"));
1152 SysTryReturnResult(NID_APP, p_ecore_event_handler_del != null, E_SYSTEM,
1153 "A system error has been occurred. Failed to get p_ecore_event_handler_del.");
1156 result r = __activeAppEventListenerList.Remove(&listener);
1157 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1159 if (__activeAppEventListenerList.GetCount() == 0)
1161 p_ecore_event_handler_del(pWindowPropertyChanged);
1162 pWindowPropertyChanged = null;
1169 _AppManagerImpl::GetActiveApp(AppId& appId)
1171 unsigned int windowId = GetActiveWindow();
1172 int processId = GetProcessId(windowId);
1173 char pkgname[255] = {0, };
1174 aul_app_get_pkgname_bypid(processId, pkgname, 255);
1175 // TODO: Translate it to package name --> AppId