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::LaunchApplication(const AppId& appId, int req)
112 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
115 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_LaunchApplication(appId, req));
116 result r = __pIpcClient->SendRequest(*pMsg.get());
117 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
123 _AppManagerProxy::TerminateApplication(const AppId& appId)
125 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
128 result response = E_SUCCESS;
129 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_TerminateApplication(appId, &response));
130 result r = __pIpcClient->SendRequest(*pMsg.get());
131 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
137 _AppManagerProxy::IsRunning(const AppId& appId)
139 bool isRunning = false;
141 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsRunning(appId, &isRunning));
142 result r = __pIpcClient->SendRequest(*pMsg.get());
143 SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
148 _AppManagerProxy::GetRunningAppList(Collection::ArrayList* pArray)
150 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
154 result response = E_SUCCESS;
155 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_GetRunningAppList(pArray, &response));
156 result r = __pIpcClient->SendRequest(*pMsg.get());
157 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
163 _AppManagerProxy::RegisterApplication(const String& packageId, const String& executableName, _AppType appType, int pId)
165 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
168 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(packageId, executableName, static_cast<int>(appType), pId));
169 result r = __pIpcClient->SendRequest(*pMsg.get());
170 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
176 _AppManagerProxy::UnregisterApplication(int pId)
178 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
182 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
183 result r = __pIpcClient->SendRequest(*pMsg.get());
184 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
190 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
192 IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
193 IPC_MESSAGE_HANDLER_EX(AppManager_OnEventReceived, &client, OnEventReceived )
194 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
195 IPC_MESSAGE_HANDLER_EX(AppManager_OnAppLifecycleEventReceived, &client, OnAppLifecycleEventReceived )
196 IPC_END_MESSAGE_MAP()
200 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
202 SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
204 __pServiceEventListener->OnServiceEventReceived(-1, arg);
210 _AppManagerProxy::OnTerminateApplicationRequested(void)
212 SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
214 __pServiceEventListener->OnTerminateApplicationRequested(-1);
218 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
220 __pServiceEventListener = pListener;
225 _AppManagerProxy::AddEventListener(int clientId)
227 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
230 std::auto_ptr<IPC::Message> pMsg(new (std::nothrow) AppManager_AddEventListener(_AppInfo::GetProcessId()));
231 result r = __pIpcClient->SendRequest(*pMsg);
232 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
238 _AppManagerProxy::RemoveEventListener(int clientId)
240 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
243 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RemoveEventListener(_AppInfo::GetProcessId()));
244 result r = __pIpcClient->SendRequest(*pMsg);
245 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
251 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
253 SysLog(NID_APP, "begin.");
254 bool isUserPreferredApp = false;
255 result response = E_SUCCESS;
257 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsUserPreferredAppForAppControlResolution(appId, &isUserPreferredApp, &response));
258 result r = __pIpcClient->SendRequest(*pMsg.get());
259 SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
261 SetLastResult(response);
262 SysLog(NID_APP, "end.");
263 return isUserPreferredApp;
267 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
269 SysLog(NID_APP, "begin.");
270 result response = E_SUCCESS;
271 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_ClearUserPreferenceForAppControlResolution(appId, &response));
272 result r = __pIpcClient->SendRequest(*pMsg.get());
273 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
275 SysLog(NID_APP, "end.");
280 _AppManagerProxy::RegisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
282 SysLog(NID_APP, "begin.");
283 result response = E_SUCCESS;
284 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterAppForAppLifecycleEvent(appId, &response));
285 result r = __pIpcClient->SendRequest(*pMsg.get());
286 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
288 SysLog(NID_APP, "end.");
293 _AppManagerProxy::UnregisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
295 SysLog(NID_APP, "begin.");
296 result response = E_SUCCESS;
297 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterAppForAppLifecycleEvent(appId, &response));
298 result r = __pIpcClient->SendRequest(*pMsg.get());
299 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
301 SysLog(NID_APP, "end.");
306 _AppManagerProxy::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
308 SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
310 _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
312 if (appLifecycleEventType == 0)
314 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
316 else if (appLifecycleEventType == 1)
318 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
322 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
325 __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);