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 bool _AppManagerProxy::__isDeletable = true;
51 _IAppManager* _AppManagerProxy::__pSelf = null;
52 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
54 _AppManagerProxy::_AppManagerProxy(void)
60 _AppManagerProxy::~_AppManagerProxy(void)
67 _AppManagerProxy::Construct(void)
69 __pIpcClient = new (std::nothrow) _IpcClient();
70 SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
72 result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
73 SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
79 _AppManagerProxy::GetService(void)
81 _AppManagerProxy* pProxy = null;
85 SysLog(NID_APP, "Create new instance");
87 pProxy = new (std::nothrow) _AppManagerProxy();
88 SysTryReturn(NID_APP, pProxy != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create _AppManagerProxy");
90 result r = pProxy->Construct();
91 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
105 _AppManagerProxy::SetService(_IAppManager* pAppManager)
107 __pSelf = pAppManager;
108 __isDeletable = false;
112 _AppManagerProxy::DeleteService(void)
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::RegisterApplication(const AppId& appId, _AppType appType, int pId)
139 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
142 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(appId, static_cast<int>(appType), pId));
143 result r = __pIpcClient->SendRequest(*pMsg.get());
144 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
150 _AppManagerProxy::UnregisterApplication(int pId)
152 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
156 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
157 result r = __pIpcClient->SendRequest(*pMsg.get());
158 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
164 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
166 IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
167 IPC_MESSAGE_HANDLER_EX(AppManager_OnEventReceived, &client, OnEventReceived )
168 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
169 IPC_MESSAGE_HANDLER_EX(AppManager_OnAppLifecycleEventReceived, &client, OnAppLifecycleEventReceived )
170 IPC_END_MESSAGE_MAP()
174 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
176 SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
178 __pServiceEventListener->OnServiceEventReceived(-1, arg);
184 _AppManagerProxy::OnTerminateApplicationRequested(void)
186 SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
188 __pServiceEventListener->OnTerminateApplicationRequested(-1);
192 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
194 __pServiceEventListener = pListener;
199 _AppManagerProxy::AddEventListener(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_AddEventListener(_AppInfo::GetProcessId()));
205 result r = __pIpcClient->SendRequest(*pMsg);
206 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
212 _AppManagerProxy::RemoveEventListener(int clientId)
214 SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
217 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RemoveEventListener(_AppInfo::GetProcessId()));
218 result r = __pIpcClient->SendRequest(*pMsg);
219 SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
225 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
227 SysLog(NID_APP, "begin.");
228 bool isUserPreferredApp = false;
229 result response = E_SUCCESS;
231 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsUserPreferredAppForAppControlResolution(appId, &isUserPreferredApp, &response));
232 result r = __pIpcClient->SendRequest(*pMsg.get());
233 SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
235 SetLastResult(response);
236 SysLog(NID_APP, "end.");
237 return isUserPreferredApp;
241 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
243 SysLog(NID_APP, "begin.");
244 result response = E_SUCCESS;
245 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_ClearUserPreferenceForAppControlResolution(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::RegisterAppForAppLifecycleEvent(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_RegisterAppForAppLifecycleEvent(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::UnregisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
269 SysLog(NID_APP, "begin.");
270 result response = E_SUCCESS;
271 std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterAppForAppLifecycleEvent(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::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
282 SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
284 _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
286 if (appLifecycleEventType == 0)
288 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
290 else if (appLifecycleEventType == 1)
292 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
296 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
299 __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);