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_AppManagerProxy.cpp
19 * @brief This is the implementation for the _AppManagerProxy class.
25 #include <FBaseErrors.h>
27 #include <FBaseSysLog.h>
28 #include <FIo_IpcClient.h>
30 #include "FApp_Types.h"
31 #include "FApp_AppInfo.h"
32 #include "FApp_IAppManagerEventListener.h"
33 #include "FApp_IAppManagerServiceEventListener.h"
34 #include "FApp_AppManagerEventArg.h"
35 #include "FApp_AppManagerProxy.h"
36 #include "FApp_AppManagerIpcMessage.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Io;
41 using namespace Tizen::Base::Runtime;
43 static const int INVALID_CLIENT_ID = -1;
45 namespace Tizen { namespace App
48 const char IPC_SERVER_NAME[] = "osp.app.ipcserver.appmanager";
50 _IAppManager* _AppManagerProxy::__pSelf = null;
51 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
53 _AppManagerProxy::_AppManagerProxy(void)
59 _AppManagerProxy::~_AppManagerProxy(void)
66 _AppManagerProxy::Construct(void)
68 __pIpcClient = new (std::nothrow) _IpcClient();
69 SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
71 result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
72 SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
78 _AppManagerProxy::GetService(void)
80 _AppManagerProxy* pProxy = null;
84 SysLog(NID_APP, "Create new instance");
86 pProxy = new (std::nothrow) _AppManagerProxy();
87 SysTryReturn(NID_APP, pProxy != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create _AppManagerProxy");
89 result r = pProxy->Construct();
90 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
103 _AppManagerProxy::SetService(_IAppManager* pAppManager)
105 __pSelf = pAppManager;
110 _AppManagerProxy::TerminateApplication(const AppId& appId)
112 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
115 result response = E_SUCCESS;
116 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_TerminateApplication(appId, &response));
117 result r = __pIpcClient->SendRequest(*pMsg.get());
118 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
124 _AppManagerProxy::RegisterApplication(const AppId& appId, _AppType appType, int pId)
126 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
129 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(appId, static_cast<int>(appType), pId));
130 result r = __pIpcClient->SendRequest(*pMsg.get());
131 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
137 _AppManagerProxy::UnregisterApplication(int pId)
139 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
143 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
144 result r = __pIpcClient->SendRequest(*pMsg.get());
145 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
151 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
153 IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
154 IPC_MESSAGE_HANDLER_EX(AppManager_OnEventReceived, &client, OnEventReceived )
155 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
156 IPC_MESSAGE_HANDLER_EX(AppManager_OnAppLifecycleEventReceived, &client, OnAppLifecycleEventReceived )
157 IPC_END_MESSAGE_MAP()
161 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
163 SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
165 __pServiceEventListener->OnServiceEventReceived(-1, arg);
171 _AppManagerProxy::OnTerminateApplicationRequested(void)
173 SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
175 __pServiceEventListener->OnTerminateApplicationRequested(-1);
179 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
181 __pServiceEventListener = pListener;
186 _AppManagerProxy::AddEventListener(int clientId)
188 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
191 std::auto_ptr<IPC::Message> pMsg(new (std::nothrow) AppManager_AddEventListener(_AppInfo::GetProcessId()));
192 result r = __pIpcClient->SendRequest(*pMsg);
193 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
199 _AppManagerProxy::RemoveEventListener(int clientId)
201 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
204 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RemoveEventListener(_AppInfo::GetProcessId()));
205 result r = __pIpcClient->SendRequest(*pMsg);
206 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
212 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
214 SysLog(NID_APP, "begin.");
215 bool isUserPreferredApp = false;
216 result response = E_SUCCESS;
218 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsUserPreferredAppForAppControlResolution(appId, &isUserPreferredApp, &response));
219 result r = __pIpcClient->SendRequest(*pMsg.get());
220 SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
222 SetLastResult(response);
223 SysLog(NID_APP, "end.");
224 return isUserPreferredApp;
228 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
230 SysLog(NID_APP, "begin.");
231 result response = E_SUCCESS;
232 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_ClearUserPreferenceForAppControlResolution(appId, &response));
233 result r = __pIpcClient->SendRequest(*pMsg.get());
234 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
236 SysLog(NID_APP, "end.");
241 _AppManagerProxy::RegisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
243 SysLog(NID_APP, "begin.");
244 result response = E_SUCCESS;
245 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterAppForAppLifecycleEvent(appId, &response));
246 result r = __pIpcClient->SendRequest(*pMsg.get());
247 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
249 SysLog(NID_APP, "end.");
254 _AppManagerProxy::UnregisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
256 SysLog(NID_APP, "begin.");
257 result response = E_SUCCESS;
258 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterAppForAppLifecycleEvent(appId, &response));
259 result r = __pIpcClient->SendRequest(*pMsg.get());
260 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
262 SysLog(NID_APP, "end.");
267 _AppManagerProxy::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
269 SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
271 _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
273 if (appLifecycleEventType == 0)
275 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
277 else if (appLifecycleEventType == 1)
279 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
283 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
286 __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);