fix code cleanup with file property change
[platform/framework/native/appfw.git] / src / app / FApp_AppManagerImpl.cpp
old mode 100755 (executable)
new mode 100644 (file)
index b13b58d..5aaf297
@@ -27,6 +27,7 @@
 
 #include <aul/aul.h>
 #include <bundle.h>
+#include <app_manager.h>
 #include <Ecore.h>
 #include <Ecore_X.h>
 #include <X11/Xlib.h>
@@ -59,6 +60,7 @@
 #include "FApp_AppControlImpl.h"
 #include "FApp_ConditionManagerProxy.h"
 #include "FApp_IAppManagerEventListener.h"
+#include "FApp_IAppEventListener.h"
 #include "FApp_MapDataControlImpl.h"
 #include "FApp_SqlDataControlImpl.h"
 #include "FAppPkg_PackageManagerImpl.h"
@@ -96,9 +98,20 @@ static void* (* p_ecore_event_handler_del)(Ecore_Event_Handler* event_handler) =
 // libX11.so
 static int (* p_XSelectInput)(Display* display, Window w, long event_mask) = null;
 static Display* (* p_XOpenDisplay)(_Xconst char* display_name) = null;
+static int (* p_XCloseDisplay)(Display* display) = null;
 
 Ecore_Event_Handler* pWindowPropertyChanged = null;
 
+struct _DisplayDeleter
+{
+       void operator()(Display* pDisplay)
+       {
+               if (p_XOpenDisplay)
+               {
+                       p_XCloseDisplay(pDisplay);
+               }
+       }
+};
 
 int
 GetTotalSize(const Tizen::Base::Collection::ICollection& col)
@@ -219,6 +232,7 @@ _AppManagerImpl::_AppManagerImpl(void)
        , __eventListenerCount(0)
        , __pUiLibrary(null)
        , __pX11Library(null)
+       , __pAppEventList(null)
 {
        SysLog(NID_APP, "");
 }
@@ -646,18 +660,12 @@ _AppManagerImpl::LaunchApplication(const String& appId, AppManager::LaunchOption
 
 
 result
-_AppManagerImpl::TerminateApplication(const AppId& appId, const String* pExecutableName)
+_AppManagerImpl::TerminateApplication(const AppId& appId)
 {
-       result r = E_SUCCESS;
-
        _IAppManager* pMgr = _AppManagerProxy::GetService();
-       if (pMgr)
-       {
-               const String& name = (pExecutableName) ? *pExecutableName : _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
-               r = pMgr->TerminateApplication(appId, name);
-       }
+       SysTryReturnResult(NID_APP, pMgr, E_SYSTEM, "Failed to _AppManagerProxy::GetService().");
 
-       return r;
+       return pMgr->TerminateApplication(appId);
 }
 
 
@@ -666,7 +674,7 @@ TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
 {
        const char* pStr = static_cast<const char*>(pData);
 
-       if (pStr && strncmp(pStr, pAppInfo->pkg_name, 10) == 0)
+       if (pStr && strncmp(pStr, pAppInfo->pkg_name, _MAX_PACKAGE_ID_LENGTH) == 0)
        {
                aul_terminate_pid(pAppInfo->pid);
                SysLog(NID_APP, "%s(%d) is terminated.", pAppInfo->pkg_name, pAppInfo->pid);
@@ -677,8 +685,6 @@ TerminateIterFnCb(const aul_app_info* pAppInfo, void* pData)
 result
 _AppManagerImpl::TerminateApplications(const PackageId& packageId)
 {
-       result r = E_SUCCESS;
-
        std::unique_ptr<char[]> pPackage(_StringConverter::CopyToCharArrayN(packageId));
        aul_app_get_running_app_info(TerminateIterFnCb, static_cast<void*>(pPackage.get()));
 
@@ -687,18 +693,11 @@ _AppManagerImpl::TerminateApplications(const PackageId& packageId)
 }
 
 bool
-_AppManagerImpl::IsRunning(const AppId& appId, const String* pExecutableName) const
+_AppManagerImpl::IsRunning(const AppId& appId) const
 {
-       bool b = false;
-
-       _IAppManager* pMgr = _AppManagerProxy::GetService();
-       if (pMgr)
-       {
-               const String& name = (pExecutableName) ? *pExecutableName : _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
-               b = pMgr->IsRunning(appId, name);
-       }
+       std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
 
-       return b;
+       return aul_app_is_running(pAppId.get());
 }
 
 static int
@@ -730,17 +729,17 @@ _AppManagerImpl::GetRunningAppListN(void) const
 
 
 result
-_AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& executableName, const String& condition, const IList* pArguments,
+_AppManagerImpl::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments,
                                                                   AppManager::LaunchOption option)
 {
        _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
        SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
 
-       return pProxy->RegisterAppLaunch(appId, executableName, condition, pArguments, option);
+       return pProxy->RegisterAppLaunch(appId, condition, pArguments, option);
 }
 
 result
-_AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String& executableName, const String* pCondition)
+_AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
 {
        _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
        SysTryReturnResult(NID_APP, null != pProxy, E_INVALID_STATE, "ConditionManager instance must not be null.");
@@ -752,18 +751,18 @@ _AppManagerImpl::UnregisterAppLaunch(const AppId& appId, const String& executabl
                                                  "No such a condition.");
        }
 
-       return pProxy->UnregisterAppLaunch(appId, executableName, pCondition);
+       return pProxy->UnregisterAppLaunch(appId, pCondition);
 }
 
 bool
-_AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String &executableName, const String* pCondition)
+_AppManagerImpl::IsAppLaunchRegistered(const AppId& appId, const String* pCondition)
 {
        _ConditionManagerProxy* pProxy = GetConditionManagerProxy();
        SysTryReturn(NID_APP, null != pProxy, false, E_INVALID_STATE, "[%s] ConditionManager instance must not be null.",
                                 GetErrorMessage(E_INVALID_STATE));
 
        ClearLastResult();
-       return pProxy->IsAppLaunchRegistered(appId, executableName, pCondition);
+       return pProxy->IsAppLaunchRegistered(appId, pCondition);
 }
 
 result
@@ -778,8 +777,7 @@ _AppManagerImpl::OnServiceEventReceived(int clientId, const _AppManagerEventArg&
        SysLog(NID_APP, "appId:%ls, appType:0x%x", arg.GetAppId().GetPointer(), arg.GetAppType());
 
        _AppManagerEventArg* pArg = new (std::nothrow)_AppManagerEventArg(arg);
-       SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
-                                          GetErrorMessage(E_OUT_OF_MEMORY));
+       SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
        __appManagerEvent.FireAsync(*pArg);
        return E_SUCCESS;
 }
@@ -832,26 +830,106 @@ _AppManagerImpl::RemoveEventListener(_IAppManagerEventListener& listener)
        return E_SUCCESS;
 }
 
-// input ("qik37po9ck.BasicApp") => output {"qik37po9ck", "BasicApp"}
-result
-_AppManagerImpl::ExtractValues(AppId inAppId, AppId& outAppId, String& outExecutableName)
+
+void
+_AppManagerImpl::AppEventCallback(app_context_h app_context, app_context_event_e event, void* pData)
 {
-       const int APP_ID_LEN = 10;
+       _AppManagerImpl* pImpl = static_cast<_AppManagerImpl*>(pData);
+       if (pImpl == null || pImpl->__pAppEventList == null)
+       {
+               SysLogException(NID_APP, E_SYSTEM, "Wrong _AppManagerImpl state.");
+               return;
+       }
+
+       std::unique_ptr< IEnumeratorT<_IAppEventListener*> > pEnum(pImpl->__pAppEventList->GetEnumeratorN());
 
-       if (inAppId.GetLength() > APP_ID_LEN)
+       if (pEnum.get())
        {
-               result r = inAppId.SubString(APP_ID_LEN + 1, inAppId.GetLength() - (APP_ID_LEN + 1), outExecutableName);
-               SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "invalid AppId(%ls)", inAppId.GetPointer());
+               char* pAppId = NULL;
+               app_context_get_app_id(app_context, &pAppId);
+               if (pAppId == NULL)
+               {
+                       SysLogException(NID_APP, E_SYSTEM, "Cannot acquire appId.");
+                       return;
+               }
 
-               r = inAppId.SubString(0, APP_ID_LEN, outAppId);
-               SysTryReturnResult(NID_APP, !IsFailed(r), E_INVALID_ARG, "invalid AppId(%ls)", inAppId.GetPointer());
+               const String appId = pAppId;
 
-               SysLog(NID_APP, "appId(%ls), executableName(%ls)", outAppId.GetPointer(), outExecutableName.GetPointer());
-               return E_SUCCESS;
+               free(pAppId);
+
+               pid_t pid = -1;
+               app_context_get_pid(app_context, &pid);
+               const int val = event;
+
+               // loop unfolding
+               switch (val)
+               {
+               case APP_CONTEXT_EVENT_LAUNCHED:
+                       while (pEnum->MoveNext() == E_SUCCESS)
+                       {
+                               _IAppEventListener* pListener = null;
+                               pEnum->GetCurrent(pListener);
+
+                               pListener->OnApplicationLaunched(appId, pid);
+                       }
+                       break;
+               case APP_CONTEXT_EVENT_TERMINATED:
+                       while (pEnum->MoveNext() == E_SUCCESS)
+                       {
+                               _IAppEventListener* pListener = null;
+                               pEnum->GetCurrent(pListener);
+
+                               pListener->OnApplicationTerminated(appId, pid);
+                       }
+                       break;
+               default:
+                       SysLog(NID_APP, "Invalid state.");
+                       break;
+               }
+       }
+
+       SysLog(NID_APP, "Finished invoking application event listener.");
+}
+
+result
+_AppManagerImpl::AddAppEventListener(_IAppEventListener& listener)
+{
+       if (__pAppEventList == null)
+       {
+               std::unique_ptr< LinkedListT<_IAppEventListener*> > pAppEventList(new LinkedListT<_IAppEventListener*>);
+               SysTryReturnResult(NID_APP, pAppEventList, E_SYSTEM, "Memory allocation failed.");
+
+               const int ret = app_manager_set_app_context_event_cb(_AppManagerImpl::AppEventCallback, this);
+               SysTryReturnResult(NID_APP, ret == APP_MANAGER_ERROR_NONE, E_SYSTEM, "Context registration failed.");
+
+               __pAppEventList = pAppEventList.release();
+               SysLog(NID_APP, "Registered app event listener.");
+       }
+
+       return __pAppEventList->Add(&listener);
+}
+
+result
+_AppManagerImpl::RemoveAppEventListener(_IAppEventListener& listener)
+{
+       result r = E_SUCCESS;
+
+       SysTryReturnResult(NID_APP,__pAppEventList != null, E_OBJ_NOT_FOUND, "_IAppEventListener list is empty.");
+
+       r = __pAppEventList->Remove(&listener);
+
+       if (__pAppEventList->GetCount() == 0)
+       {
+               app_manager_unset_app_context_event_cb();
+
+               delete __pAppEventList;
+               __pAppEventList = null;
        }
-       return E_SYSTEM;
+
+       return E_SUCCESS;
 }
 
+
 _LibraryImpl&
 _AppManagerImpl::GetUiLibraryImpl(void)
 {
@@ -960,14 +1038,16 @@ _AppManagerImpl::GetActiveWindow(void)
                                                   "A system error has been occurred. Failed to get ecore_x_window_prop_window_get.");
        }
 
-       Ecore_X_Window* pRoots = null;
        int num = 0;
-
-       pRoots = p_ecore_x_window_root_list(&num);
-       Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
+       Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
 
        Ecore_X_Window activeWin = 0;
-       p_ecore_x_window_prop_window_get(pRoots[0], activeAtom, &activeWin, 1);
+       if (pRoots)
+       {
+               Ecore_X_Atom activeAtom = p_ecore_x_atom_get("_NET_ACTIVE_WINDOW");
+               p_ecore_x_window_prop_window_get(pRoots[0], activeAtom, &activeWin, 1);
+               free(pRoots);
+       }
 
        return activeWin;
 }
@@ -996,35 +1076,42 @@ _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
        {
                _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
                p_ecore_x_window_root_list = reinterpret_cast<Ecore_X_Window*(*)(int* num_ret)>(lib.GetProcAddress(L"ecore_x_window_root_list"));
-               SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, EINA_FALSE,
+               SysTryReturnResult(NID_APP, p_ecore_x_window_root_list != null, E_SYSTEM,
                                                   "A system error has been occurred. Failed to get p_ecore_x_window_root_list.");
        }
        if (p_ECORE_X_EVENT_WINDOW_PROPERTY == null)
        {
                _LibraryImpl& lib = _AppManagerImpl::GetEcoreXLibraryImpl();
                p_ECORE_X_EVENT_WINDOW_PROPERTY = reinterpret_cast<int*>(lib.GetProcAddress(L"ECORE_X_EVENT_WINDOW_PROPERTY"));
-               SysTryReturnResult(NID_APP, p_ECORE_X_EVENT_WINDOW_PROPERTY != null, EINA_FALSE,
+               SysTryReturnResult(NID_APP, p_ECORE_X_EVENT_WINDOW_PROPERTY != null, E_SYSTEM,
                                                   "A system error has been occurred. Failed to get p_ECORE_X_EVENT_WINDOW_PROPERTY.");
        }
        if (p_XOpenDisplay == null)
        {
                _LibraryImpl& lib = GetX11LibraryImpl();
                p_XOpenDisplay = reinterpret_cast<Display*(*)(_Xconst char* display_name)>(lib.GetProcAddress(L"XOpenDisplay"));
-               SysTryReturnResult(NID_APP, p_XOpenDisplay != null, EINA_FALSE,
+               SysTryReturnResult(NID_APP, p_XOpenDisplay != null, E_SYSTEM,
                                                   "A system error has been occurred. Failed to get p_XOpenDisplay.");
        }
+       if (p_XCloseDisplay == null)
+       {
+               _LibraryImpl& lib = GetX11LibraryImpl();
+               p_XCloseDisplay = reinterpret_cast<int(*)(Display* display)>(lib.GetProcAddress(L"XCloseDisplay"));
+               SysTryReturnResult(NID_APP, p_XCloseDisplay != null, E_SYSTEM,
+                                                  "A system error has been occurred. Failed to get p_XCloseDisplay.");
+       }
        if (p_XSelectInput == null)
        {
                _LibraryImpl& lib = GetX11LibraryImpl();
                p_XSelectInput = reinterpret_cast<int(*)(Display* display, Window w, long event_mask)>(lib.GetProcAddress(L"XSelectInput"));
-               SysTryReturnResult(NID_APP, p_XSelectInput != null, EINA_FALSE,
+               SysTryReturnResult(NID_APP, p_XSelectInput != null, E_SYSTEM,
                                                   "A system error has been occurred. Failed to get p_XSelectInput.");
        }
        if (p_ecore_event_handler_add == null)
        {
                _LibraryImpl& lib = GetEcoreLibraryImpl();
                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"));
-               SysTryReturnResult(NID_APP, p_ecore_event_handler_add != null, EINA_FALSE,
+               SysTryReturnResult(NID_APP, p_ecore_event_handler_add != null, E_SYSTEM,
                                                   "A system error has been occurred. Failed to get p_ecore_event_handler_add.");
        }
 
@@ -1033,19 +1120,22 @@ _AppManagerImpl::AddActiveAppEventListener(IActiveAppEventListener& listener)
        result r = __activeAppEventListenerList.Add(&listener);
        SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
 
-       Ecore_X_Window* pRoots = null;
-       int num = 0;
-       pRoots = p_ecore_x_window_root_list(&num);
-
-       for (int i = 0; i < num; i++)
-       {
-               Display* pDisplay = p_XOpenDisplay(NULL);
-               p_XSelectInput(pDisplay, pRoots[i], PropertyChangeMask);
-       }
-
        if (!pWindowPropertyChanged)
        {
+               std::unique_ptr<Display, _DisplayDeleter> pDisplay(p_XOpenDisplay(NULL));
+               SysTryReturnResult(NID_APP, pDisplay != null, E_SYSTEM, "A system error has been occurred. Failed to XOpenDisplay.");
+
+               int num = 0;
+               Ecore_X_Window* pRoots = p_ecore_x_window_root_list(&num);
+               SysTryReturnResult(NID_APP, pRoots != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
+
+               for (int i = 0; i < num; i++)
+               {
+                       p_XSelectInput(pDisplay.get(), pRoots[i], PropertyChangeMask);
+               }
+
                pWindowPropertyChanged = p_ecore_event_handler_add(*p_ECORE_X_EVENT_WINDOW_PROPERTY, OnPropertyChanged, (void*) this);
+               free(pRoots);
                SysTryReturnResult(NID_APP, pWindowPropertyChanged, E_SYSTEM, "A system error has been occurred.");
        }