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"
70 #include "FSys_SystemInfoImpl.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;
79 //extern const char* _DATACONTROL_RESULTSET_DIR;
84 //const long MAX_APPCONTROL_ARGUMENT = 4096;
85 const long MAX_APPCONTROL_ARGUMENT = 1024;
86 const long MAX_CONDITION_LENGTH = 400;
88 // ActiveWindow related function and variable from shared library.
90 static Ecore_X_Atom(* p_ecore_x_atom_get)(const char* name) = null;
91 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;
92 static Eina_Bool(* p_ecore_x_netwm_pid_get)(Ecore_X_Window win, int* pid) = null;
93 static int(* p_ecore_x_netwm_name_get)(Ecore_X_Window win, char** name) = null;
94 static Ecore_X_Window* (* p_ecore_x_window_root_list)(int* num_ret) = null;
95 static int* p_ECORE_X_EVENT_WINDOW_PROPERTY = null;
97 static Ecore_Event_Handler* (* p_ecore_event_handler_add)(int type, Ecore_Event_Handler_Cb func, const void* data) = null;
98 static void* (* p_ecore_event_handler_del)(Ecore_Event_Handler* event_handler) = null;
100 static int (* p_XSelectInput)(Display* display, Window w, long event_mask) = null;
101 static Display* (* p_XOpenDisplay)(_Xconst char* display_name) = null;
102 static int (* p_XCloseDisplay)(Display* display) = null;
104 Ecore_Event_Handler* pWindowPropertyChanged = null;
106 struct _DisplayDeleter
108 void operator()(Display* pDisplay)
112 p_XCloseDisplay(pDisplay);
118 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
121 std::unique_ptr<IEnumerator> pEnum(col.GetEnumeratorN());
125 while (pEnum->MoveNext() == E_SUCCESS)
127 String* pStr = static_cast<String*>(pEnum->GetCurrent());
132 size += pStr->GetLength();
140 OnPropertyChanged(void* pData, int type, void* pEvent)
142 using namespace Tizen::App;
144 if (p_ecore_x_atom_get == null)
146 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
147 p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
148 SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, EINA_FALSE,
149 "A system error has been occurred. Failed to get ecore_x_atom_get.");
151 if (p_ecore_x_window_prop_window_get == null)
153 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
154 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"));
155 SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
156 "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
158 if (p_ecore_x_netwm_pid_get == null)
160 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
161 p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
162 SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
163 "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
165 if (p_ecore_x_netwm_name_get == null)
167 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
168 p_ecore_x_netwm_name_get = reinterpret_cast<int(*)(Ecore_X_Window win, char** name)>(lib.GetProcAddress(L"ecore_x_netwm_name_get"));
169 SysTryReturnResult(NID_APP, p_ecore_x_netwm_name_get != null, EINA_FALSE,
170 "A system error has been occurred. Failed to get ecore_x_netwm_name_get.");
175 SysLog(NID_UI, "The data is not valid.");
180 SysLog(NID_UI, "The event is not valid.");
184 Ecore_X_Event_Window_Property* pE = (Ecore_X_Event_Window_Property*) pEvent;
185 Ecore_X_Atom atom = pE->atom;
186 Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
188 if (atom != activeAtom)
190 return ECORE_CALLBACK_PASS_ON;
193 Ecore_X_Window win = pE->win;
194 Ecore_X_Window activeWin = 0;
195 p_ecore_x_window_prop_window_get(win, activeAtom, &activeWin, 1);
198 p_ecore_x_netwm_pid_get(activeWin, &pid);
200 char* pAppName = null;
201 p_ecore_x_netwm_name_get(activeWin, &pAppName);
203 Tizen::App::_AppManagerImpl* pAppManagerImpl = static_cast<Tizen::App::_AppManagerImpl*>(pData);
204 pAppManagerImpl->FireActiveAppEvent(activeWin, pid, pAppName);
211 return ECORE_CALLBACK_PASS_ON;
214 } // anonymous name-space
217 namespace Tizen { namespace App
220 const wchar_t LEGACY_LAUNCH_REASON_NORMAL[] = L"LAUNCH_NORMAL";
221 const wchar_t LEGACY_LAUNCH_REASON_CONDITIONAL[] = L"LAUNCH_CONDITIONAL";
222 const wchar_t OSP_UI_SONAME[] = L"libosp-uifw.so.1";
223 const wchar_t OSP_ECORE_X_SONAME[] = L"libecore_x.so.1";
224 const wchar_t OSP_ECORE_SONAME[] = L"libecore.so.1";
225 const wchar_t OSP_X11_SONAME[] = L"libX11.so.6";
226 const int _MAX_PACKAGE_ID_LENGTH = 10;
228 _LibraryImpl* _AppManagerImpl::__pEcoreLibrary = null;
229 _LibraryImpl* _AppManagerImpl::__pEcoreXLibrary = null;
231 _AppManagerImpl::_AppManagerImpl(void)
232 : __pConditionManager(null)
233 , __eventListenerCount(0)
235 , __pX11Library(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->GetTizenAppControlN(aId, oId);
316 SetLastResult(E_SUCCESS);
320 pAc = pRegs->GetAppControlN(aId, oId);
322 SysTryReturn(NID_APP, pAc != null, null, E_OBJ_NOT_FOUND, "[%s] No matching AppControl instance found (%ls, %ls).",
323 GetErrorMessage(E_OBJ_NOT_FOUND), aId.GetPointer(), oId.GetPointer());
324 SetLastResult(E_SUCCESS);
329 _AppManagerImpl::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType,
330 const String* pUriScheme)
332 SysTryReturn(NID_APP, !(pOperationId == null && pCategory == null && pUriScheme == null && pDataType == null), null,
333 E_INVALID_ARG, "[%s] At least one parameter should not be null.", GetErrorMessage(E_INVALID_ARG));
336 const String* pMimeType = pDataType;
340 if ((*pDataType)[0] == L'.')
342 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
345 pDataType->SubString(1, ext);
347 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
349 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
351 pMimeType = &mimeType;
353 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
359 SysTryReturn(NID_APP, !(pUriScheme->IsEmpty()), null, E_INVALID_FORMAT, "[%s] The specified URI scheme is invalid.",
360 GetErrorMessage(E_INVALID_FORMAT));
363 _AppControlRegistry* pRegs = _AppControlRegistry::GetInstance();
364 ArrayList* pRetArg = pRegs->FindAppControlListN(pOperationId, pUriScheme, pMimeType, pCategory);
366 if ((pRetArg == null) || (pRetArg->GetCount() == 0))
370 SetLastResult(E_OBJ_NOT_FOUND);
374 SetLastResult(E_SUCCESS);
375 SysLog(NID_APP, "Found %d matching AppControls.", pRetArg->GetCount());
382 _AppManagerImpl::StartAppControl(const String& uri, const String* pOperationId, const String* pDataType,
383 IAppControlListener* pListener)
385 return StartAppControl(pOperationId, null, pDataType, &uri, null, pListener);
390 _AppManagerImpl::StartAppControl(const String* pOperationId, const String* pCategory, const String* pDataType,
391 const String* pUri, const IList* pDataList, IAppControlListener* pListener)
393 SysTryReturnResult(NID_APP, !(pOperationId == null && pUri == null && pCategory == null && pDataType == null), E_INVALID_ARG,
394 "At least one of the specified argument must not be null.");
395 if (pDataList != null)
398 argSize = GetTotalSize(*pDataList);
400 SysLog(NID_APP, "Argument count = %d, size = %d", pDataList->GetCount(), argSize);
402 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
403 "The size of pDataList exceeded the limit(%d).",
404 MAX_APPCONTROL_ARGUMENT);
407 String operation = (pOperationId) ? *pOperationId : TIZEN_OPERATION_MAIN;
409 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
410 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
412 _AppMessageImpl::SetOperation(pBundle.get(), operation);
416 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
421 String mimeType = *pDataType;
423 if ((*pDataType)[0] == L'.')
425 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
429 pDataType->SubString(1, ext);
431 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
433 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
435 pMimeType = &mimeType;
437 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
441 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
446 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
449 return _AppControlImpl::StartImplicit(pBundle.get(), pDataList, pListener);
454 _AppManagerImpl::GetSqlDataControlN(const String& providerId)
456 SqlDataControl* pDc = null;
458 String* pAppId = null;
459 String* pAccess = null;
462 // XXX: Need _NativeDataControlRegistry for SLP native app ?
463 // Try to searach SLP data control.
464 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
465 // pDc = pReg->GetNativeSqlDataControlN(providerId);
471 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
472 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
474 pAppId = dynamic_cast< String* >(pList->GetAt(0));
475 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
476 pAccess = dynamic_cast< String* >(pList->GetAt(1));
477 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
480 if (pAppId->StartsWith(L"org.tizen.", 0))
482 std::unique_ptr<StringTokenizer> pStrTok(new (std::nothrow) StringTokenizer(*pAppId, L'.'));
483 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
484 for (int i = 0; i < 3; ++i)
486 pStrTok->GetNextToken(appId);
489 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
493 appId.Append(*pAppId);
496 pDc = _SqlDataControlImpl::CreateSqlDataControl(appId, providerId, *pAccess);
497 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
503 _AppManagerImpl::GetMapDataControlN(const String& providerId)
505 MapDataControl* pDc = null;
507 String* pAppId = null;
508 String* pAccess = null;
511 // XXX: Need _NativeDataControlRegistry for SLP native app ?
512 // Try to searach SLP data control.
513 // _NativeDataControlRegistry* pReg = _NativeDataControlRegistry::GetInstance(); // singleton
514 // pDc = pReg->GetNativeMapDataControlN(providerId);
520 std::unique_ptr <IList, AllElementsDeleter> pList(_PackageManagerImpl::GetInstance()->GetDataControlInfoN(providerId, type));
521 SysTryReturn(NID_APP, pList != null, null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The data control provider does not exist.");
523 pAppId = dynamic_cast< String* >(pList->GetAt(0));
524 SysTryReturn(NID_APP, pAppId != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
525 pAccess = dynamic_cast< String* >(pList->GetAt(1));
526 SysTryReturn(NID_APP, pAccess != null, null, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
529 if (pAppId->StartsWith(L"org.tizen.", 0))
531 std::unique_ptr<StringTokenizer> pStrTok( new (std::nothrow) StringTokenizer(*pAppId, L'.'));
532 SysTryReturn(NID_APP, pStrTok != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
533 for (int i = 0; i < 3; ++i)
535 pStrTok->GetNextToken(appId);
538 appId[_MAX_PACKAGE_ID_LENGTH] = L'.';
542 appId.Append(*pAppId);
545 pDc = _MapDataControlImpl::CreateMapDataControl(appId, providerId, *pAccess);
546 SysTryReturn(NID_APP, pDc != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
552 _AppManagerImpl::GetAppRootPath(const AppId& appId, String& appRootPath)
554 String pkgId = PackageManager::GetPackageIdByAppId(appId);
555 SysTryReturnResult(NID_APP, pkgId.IsEmpty() == false, E_APP_NOT_INSTALLED,
556 "The application is not installed. (app: %ls)", appId.GetPointer());
558 _PackageManagerImpl* pPkgMgr = _PackageManagerImpl::GetInstance();
559 SysTryReturnResult(NID_APP, pPkgMgr != null, E_SYSTEM,
560 "Failed to get _PackageManagerImpl instance.");
562 std::unique_ptr< PackageInfo >pPkgInfo(pPkgMgr->GetPackageInfoN(pkgId));
563 SysTryReturnResult(NID_APP, pPkgInfo != null, E_APP_NOT_INSTALLED,
564 "The application is not installed. (app: %ls)", appId.GetPointer());
566 _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
567 SysTryReturnResult(NID_APP, pPkgInfoImpl != null, E_SYSTEM,
568 "Failed to get _PackageInfoImpl instance.");
570 appRootPath = pPkgInfoImpl->GetAppRootPath();
571 appRootPath.Append(L"/");
577 _AppManagerImpl::LaunchApplication(const String& appId, const IList* pArguments, AppManager::LaunchOption option)
579 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_INVALID_ARG, "The appid is empty.");
580 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND,
581 "The target application(%ls) is not installed.", appId.GetPointer());
582 // SysTryReturnResult(NID_APP,
583 // appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
584 // "The length of appid exceeded the limit(%d).",
585 // WIDGET_APP_MAX_APPID_LENGTH);
587 std::unique_ptr<AppControl> pAc(_AppControlRegistry::GetInstance()->GetAppControlN(appId, TIZEN_OPERATION_MAIN));
588 SysTryReturnResult(NID_APP, pAc.get() != null, E_OBJ_NOT_FOUND, "The target application (%ls) is not found.", appId.GetPointer());
593 argSize = GetTotalSize(*pArguments);
595 SysLog(NID_APP, "Argument count = %d, size = %d", pArguments->GetCount(), argSize);
597 SysTryReturnResult(NID_APP, argSize <= MAX_APPCONTROL_ARGUMENT, E_MAX_EXCEEDED,
598 "The size of pDataList exceeded the limit(%d).",
599 MAX_APPCONTROL_ARGUMENT);
602 result r = pAc->Start(pArguments, null);
604 SysLog(NID_APP, "[%s] Launching %ls finished.", GetErrorMessage(r), appId.GetPointer());
610 _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption option)
612 SysTryReturnResult(NID_APP, !appId.IsEmpty(), E_APP_NOT_INSTALLED, "The appid is empty.");
614 std::unique_ptr<char[]> pName(_StringConverter::CopyToCharArrayN(appId));
616 int ret = aul_open_app(pName.get());
620 SysLog(NID_APP, "Launching %ls successful.", appId.GetPointer());
625 SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED,
626 "The target application(%ls) is not installed.", appId.GetPointer());
632 r = E_APP_NOT_INSTALLED;
642 SysLog(NID_APP, "[%s] Launching %ls failed.", GetErrorMessage(r), appId.GetPointer());
648 _AppManagerImpl::TerminateApplication(const AppId& appId)
650 _IAppManager* pMgr = _AppManagerProxy::GetService();
651 SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
653 return pMgr->TerminateApplication(appId);
658 TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
660 const char* pStr = static_cast<const char*>(pData);
662 if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
664 aul_terminate_pid(pAppInfo->pid);
665 SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
671 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
673 std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
674 aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
676 SysLog(NID_APP, "%ls terminated.", packageId.GetPointer());
681 _AppManagerImpl::IsRunning(const AppId& appId) const
683 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
685 return aul_app_is_running(pAppId.get());
689 AulAppInfoIterFnCb(const aul_app_info* pAppInfo, void* pData)
691 ArrayList* pList = static_cast<ArrayList*>(pData);
692 if (pList && pAppInfo && pAppInfo->appid)
694 pList->Add(*new (std::nothrow) String(pAppInfo->appid));
701 _AppManagerImpl::GetRunningAppListN(void) const
703 ArrayList* pRunningAppList = new (std::nothrow) ArrayList();
704 SysTryReturn(NID_APP, pRunningAppList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
705 GetErrorMessage(E_OUT_OF_MEMORY));
706 pRunningAppList->Construct();
708 int ret = aul_app_get_running_app_info(AulAppInfoIterFnCb, reinterpret_cast<void*>(pRunningAppList));
709 SysTryLog(NID_APP, ret == AUL_R_OK, "Getting running list failed.");
711 // according to the doxygen, GetRunningAppListN() does not return null pointer for no object
712 return pRunningAppList;
717 _AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
718 AppManager::LaunchOption option)
720 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
721 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
723 Tizen::Base::Utility::StringTokenizer strTok(condition, L"=");
724 SysTryReturnResult(NID_APP, strTok.GetTokenCount() > 0, E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
727 result r = strTok.GetNextToken(key);
728 SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "Condition string is invalid.(%ls)", condition.GetPointer());
731 if (key == L"Serial")
733 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/usb.accessory", ret);
735 else if (key == L"NFC")
737 r = Tizen::System::_SystemInfoImpl::GetSysInfo(L"http://tizen.org/feature/network.nfc", ret);
739 SysTryReturnResult(NID_APP, ret != false, E_UNSUPPORTED_OPERATION, "Condition(%ls)", condition.GetPointer());
741 return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
745 _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
747 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
748 SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
752 SysTryReturnResult(NID_APP,
753 !pCondition->IsEmpty() && pCondition->GetLength() < MAX_CONDITION_LENGTH, E_OBJ_NOT_FOUND,
754 "No such a condition.");
757 return pProxy->UnregisterAppLaunch(appId, pCondition);
761 _AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
763 _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
764 SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
765 GetErrorMessage(E_INVALID_STATE));
768 return pProxy->IsAppLaunchRegistered(appId, pCondition);
772 _AppManagerImpl::SetEventListener(_AppEvent appEvent, Tizen::Base::Runtime::IEventListener* pListener)
774 return _AppImpl::GetInstance()->SetListener(appEvent, pListener);
778 _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg& arg)
780 SysLog(NID_APP, "app:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
782 _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
783 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
784 __appManagerEvent.FireAsync(*pArg);
789 _AppManagerImpl::OnTerminateApplicationRequested(int clientId)
797 _AppManagerImpl::AddEventListener(_IAppManagerEventListener& listener)
799 _IAppManager* pMgr = _AppManagerProxy::GetService();
800 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
802 result r = __appManagerEvent.AddListener(listener);
803 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
805 __eventListenerCount++;
806 SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
808 if( __eventListenerCount > 1)
813 return pMgr->AddEventListener(-1);
817 _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
819 _IAppManager* pMgr = _AppManagerProxy::GetService();
820 SysTryReturnResult(NID_APP, pMgr != null, E_INVALID_STATE, "");
822 result r = __appManagerEvent.RemoveListener(listener);
823 SysTryReturn(NID_APP, IsFailed(r) == false, r, r, "[%s]", GetErrorMessage(r));
825 __eventListenerCount--;
826 SysLog(NID_APP, "registered event listener(s) # %d", __eventListenerCount);
828 if (__eventListenerCount == 0)
830 return pMgr->RemoveEventListener(-1);
837 ///////////////////////////////////////////////////////////////////////////////
838 // LifecycleManager begins.
839 ///////////////////////////////////////////////////////////////////////////////
842 _AppLifecycleManager::Init(void)
844 aul_listen_app_launch_signal(LaunchCallback, this);
845 aul_listen_app_dead_signal(TerminateCallback, this);
849 _AppLifecycleManager::Fini(void)
851 aul_listen_app_launch_signal(NULL, NULL);
852 aul_listen_app_dead_signal(NULL, NULL);
856 _AppLifecycleManager::AddListener(_IAppEventListener& listener)
858 if (__pEventList == null)
860 std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
861 SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
865 __pEventList = pAppEventList.release();
866 SysLog(NID_APP, "Registered app event listener.");
869 return __pEventList->Add(&listener);
873 _AppLifecycleManager::RemoveListener(_IAppEventListener& listener)
875 SysTryReturnResult(NID_APP,__pEventList != null, E_OBJ_NOT_FOUND, "_IEventListener list is empty.");
877 result r = __pEventList->Remove(&listener);
879 if (__pEventList->GetCount() == 0)
891 _AppLifecycleManager::LaunchCallback(int pid, void* pData)
893 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
894 if (pImpl == null || pImpl->__pEventList == null)
896 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
901 int ret = aul_app_get_appid_bypid(pid, appId, sizeof(appId));
904 SysLogException(NID_APP, E_SYSTEM, "Cannot acquire app for %d.", pid);
908 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
911 const String tmp = appId;
912 pImpl->__map.Add(pid, tmp);
914 while (pEnum->MoveNext() == E_SUCCESS)
916 _IAppEventListener* pListener = null;
917 pEnum->GetCurrent(pListener);
919 pListener->OnApplicationLaunched(tmp, pid);
923 SysLog(NID_APP, "Finished invoking application event listener for %s, %d.", appId, pid);
929 _AppLifecycleManager::TerminateCallback(int pid, void* pData)
931 _AppLifecycleManager* pImpl = static_cast<_AppLifecycleManager*>(pData);
932 if (pImpl == null || pImpl->__pEventList == null)
934 SysLogException(NID_APP, E_SYSTEM, "Wrong _AppLifecycleImpl state.");
938 // terminate callback cannot acquire appId from pid
940 result r = pImpl->__map.GetValue(pid, tmp);
943 SysLog(NID_APP, "Cannot acquire app from pid %d.", pid);
947 pImpl->__map.Remove(pid);
949 std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pEventList->GetEnumeratorN());
952 while (pEnum->MoveNext() == E_SUCCESS)
954 _IAppEventListener* pListener = null;
955 pEnum->GetCurrent(pListener);
957 pListener->OnApplicationTerminated(tmp, pid);
961 SysLog(NID_APP, "Finished invoking application event listener for %ls, %d.", tmp.GetPointer(), pid);
966 ///////////////////////////////////////////////////////////////////////////////
967 // LifecycleManager ends.
968 ///////////////////////////////////////////////////////////////////////////////
972 _AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
974 return __lifeManager.AddListener(listener);
978 _AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
980 return __lifeManager.RemoveListener(listener);
985 _AppManagerImpl::GetUiLibraryImpl(void)
987 if (__pUiLibrary == null)
989 __pUiLibrary = new (std::nothrow) _LibraryImpl;
990 SysAssertf(__pUiLibrary != null, "_LibraryImpl allocation failure.");
992 result r = __pUiLibrary->Construct(OSP_UI_SONAME);
993 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
996 return *__pUiLibrary;
1000 _AppManagerImpl::GetEcoreXLibraryImpl(void)
1002 if (__pEcoreXLibrary == null)
1004 __pEcoreXLibrary = new (std::nothrow) _LibraryImpl;
1005 SysAssertf(__pEcoreXLibrary != null, "_LibraryImpl allocation failure.");
1007 result r = __pEcoreXLibrary->Construct(OSP_ECORE_X_SONAME);
1008 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
1010 return *__pEcoreXLibrary;
1014 _AppManagerImpl::GetEcoreLibraryImpl(void)
1016 if (__pEcoreLibrary == null)
1018 __pEcoreLibrary = new (std::nothrow) _LibraryImpl;
1019 SysAssertf(__pEcoreLibrary != null, "_LibraryImpl allocation failure.");
1021 result r = __pEcoreLibrary->Construct(OSP_ECORE_SONAME);
1022 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
1024 return *__pEcoreLibrary;
1028 _AppManagerImpl::GetX11LibraryImpl(void)
1030 if (__pX11Library == null)
1032 __pX11Library = new (std::nothrow) _LibraryImpl;
1033 SysAssertf(__pX11Library != null, "_LibraryImpl allocation failure.");
1035 result r = __pX11Library->Construct(OSP_X11_SONAME);
1036 SysAssertf(r == E_SUCCESS, "Dynamic loading error : %s.", GetErrorMessage(r));
1038 return *__pX11Library;
1042 _AppManagerImpl::FireActiveAppEvent(unsigned int xid, int pid, char* pAppName)
1044 static int oldPid = 0;
1048 std::unique_ptr<IEnumeratorT<Tizen::App::IActiveAppEventListener* > > pEnum(__activeAppEventListenerList.GetEnumeratorN());
1051 while (pEnum->MoveNext() == E_SUCCESS)
1053 Tizen::App::IActiveAppEventListener* pListener = null;
1054 pEnum->GetCurrent(pListener);
1057 char pkgname[255] = {0, };
1058 aul_app_get_pkgname_bypid(pid, pkgname, 255);
1059 // TODO: Translate it to package name --> AppId
1060 pListener->OnActiveAppChanged(AppId(pkgname));
1068 _AppManagerImpl::GetActiveWindow(void)
1070 if (p_ecore_x_window_root_list == null)
1072 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1073 p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
1074 SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, 0,
1075 "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
1077 if (p_ecore_x_atom_get == null)
1079 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1080 p_ecore_x_atom_get = reinterpret_cast<Ecore_X_Atom(*)(const char* name)>(lib.GetProcAddress(L"ecore_x_atom_get"));
1081 SysTryReturnResult(NID_APP, p_ecore_x_atom_get != null, 0,
1082 "A system error has been occurred. Failed to get ecore_x_atom_get.");
1084 if (p_ecore_x_window_prop_window_get == null)
1086 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1087 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"));
1088 SysTryReturnResult(NID_APP, p_ecore_x_window_prop_window_get != null, EINA_FALSE,
1089 "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
1093 Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
1095 Ecore_X_Window activeWin = 0;
1098 Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
1099 p_ecore_x_window_prop_window_get(pRoots[0], activeAtom, &activeWin, 1);
1107 _AppManagerImpl::GetProcessId(unsigned int window)
1109 if (p_ecore_x_netwm_pid_get == null)
1111 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1112 p_ecore_x_netwm_pid_get = reinterpret_cast<Eina_Bool(*)(Ecore_X_Window win, int* pid)>(lib.GetProcAddress(L"ecore_x_netwm_pid_get"));
1113 SysTryReturnResult(NID_APP, p_ecore_x_netwm_pid_get != null, EINA_FALSE,
1114 "A system error has been occurred. Failed to get ecore_x_netwm_pid_get.");
1118 p_ecore_x_netwm_pid_get(window, &pid);
1124 _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
1126 if (p_ecore_x_window_root_list == null)
1128 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1129 p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
1130 SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, E_SYSTEM,
1131 "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
1133 if (p_ECORE_X_EVENT_WINDOW_PROPERTY == null)
1135 _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
1136 p_ECORE_X_EVENT_WINDOW_PROPERTY = reinterpret_cast<int*>(lib.GetProcAddress(L"ECORE_X_EVENT_WINDOW_PROPERTY"));
1137 SysTryReturnResult(NID_APP, p_ECORE_X_EVENT_WINDOW_PROPERTY != null, E_SYSTEM,
1138 "A system error has been occurred. Failed to get p_ECORE_X_EVENT_WINDOW_PROPERTY.");
1140 if (p_XOpenDisplay == null)
1142 _LibraryImpl& lib = GetX11LibraryImpl();
1143 p_XOpenDisplay = reinterpret_cast<Display*(*)(_Xconst char* display_name)>(lib.GetProcAddress(L"XOpenDisplay"));
1144 SysTryReturnResult(NID_APP, p_XOpenDisplay != null, E_SYSTEM,
1145 "A system error has been occurred. Failed to get p_XOpenDisplay.");
1147 if (p_XCloseDisplay == null)
1149 _LibraryImpl& lib = GetX11LibraryImpl();
1150 p_XCloseDisplay = reinterpret_cast<int(*)(Display* display)>(lib.GetProcAddress(L"XCloseDisplay"));
1151 SysTryReturnResult(NID_APP, p_XCloseDisplay != null, E_SYSTEM,
1152 "A system error has been occurred. Failed to get p_XCloseDisplay.");
1154 if (p_XSelectInput == null)
1156 _LibraryImpl& lib = GetX11LibraryImpl();
1157 p_XSelectInput = reinterpret_cast<int(*)(Display* display, Window w, long event_mask)>(lib.GetProcAddress(L"XSelectInput"));
1158 SysTryReturnResult(NID_APP, p_XSelectInput != null, E_SYSTEM,
1159 "A system error has been occurred. Failed to get p_XSelectInput.");
1161 if (p_ecore_event_handler_add == null)
1163 _LibraryImpl& lib = GetEcoreLibraryImpl();
1164 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"));
1165 SysTryReturnResult(NID_APP, p_ecore_event_handler_add != null, E_SYSTEM,
1166 "A system error has been occurred. Failed to get p_ecore_event_handler_add.");
1169 bool alreadyExist = __activeAppEventListenerList.Contains(&listener);
1170 SysTryReturnResult(NID_APP, !alreadyExist, E_OBJ_ALREADY_EXIST, "The event listener already exist.");
1171 result r = __activeAppEventListenerList.Add(&listener);
1172 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1174 if (!pWindowPropertyChanged)
1176 std::unique_ptr<Display, _DisplayDeleter> pDisplay(p_XOpenDisplay(NULL));
1177 SysTryReturnResult(NID_APP, pDisplay != null, E_SYSTEM, "A system error has been occurred. Failed to XOpenDisplay.");
1180 Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
1181 SysTryReturnResult(NID_APP, pRoots != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1183 for (int i = 0; i < num; i++)
1185 p_XSelectInput(pDisplay.get(), pRoots[i], PropertyChangeMask);
1188 pWindowPropertyChanged = p_ecore_event_handler_add(*p_ECORE_X_EVENT_WINDOW_PROPERTY, OnPropertyChanged, (void*) this);
1190 SysTryReturnResult(NID_APP, pWindowPropertyChanged, E_SYSTEM, "A system error has been occurred.");
1197 _AppManagerImpl::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
1199 if (p_ecore_event_handler_del == null)
1201 _LibraryImpl& lib = GetEcoreLibraryImpl();
1202 p_ecore_event_handler_del = reinterpret_cast<void*(*)(Ecore_Event_Handler* event_handler)>(lib.GetProcAddress(L"ecore_event_handler_del"));
1203 SysTryReturnResult(NID_APP, p_ecore_event_handler_del != null, E_SYSTEM,
1204 "A system error has been occurred. Failed to get p_ecore_event_handler_del.");
1207 result r = __activeAppEventListenerList.Remove(&listener);
1208 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1210 if (__activeAppEventListenerList.GetCount() == 0)
1212 p_ecore_event_handler_del(pWindowPropertyChanged);
1213 pWindowPropertyChanged = null;
1220 _AppManagerImpl::GetActiveApp(AppId& appId)
1222 unsigned int windowId = GetActiveWindow();
1223 int processId = GetProcessId(windowId);
1224 char pkgname[255] = {0, };
1225 aul_app_get_pkgname_bypid(processId, pkgname, 255);
1226 // TODO: Translate it to package name --> AppId