prevent issue fixes
[platform/framework/native/appfw.git] / src / app / FApp_AppManagerProxy.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FApp_AppManagerProxy.cpp
19  * @brief       This is the implementation for the _AppManagerProxy class.
20  */
21
22 #include <new>
23 #include <memory>
24
25 #include <FBaseErrors.h>
26
27 #include <FBaseSysLog.h>
28 #include <FIo_IpcClient.h>
29
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"
37 #include "FApp_IAppLifecycleEventListener.h"
38
39 namespace
40 {
41
42 const char IPC_SERVER_NAME[] = "osp.app.ipcserver.appmanager";
43 const int INVALID_CLIENT_ID = -1;
44 };
45
46
47 namespace Tizen { namespace App
48 {
49
50 using namespace Tizen::Base;
51 using namespace Tizen::Io;
52 using namespace Tizen::Base::Runtime;
53
54
55 _IAppManager* _AppManagerProxy::__pSelf = null;
56 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
57
58 _AppManagerProxy::_AppManagerProxy(void)
59         : __pIpcClient(null)
60 {
61         SysLog(NID_APP, "");
62 }
63
64 _AppManagerProxy::~_AppManagerProxy(void)
65 {
66         delete __pIpcClient;
67 }
68
69
70 result
71 _AppManagerProxy::Construct(void)
72 {
73         __pIpcClient = new (std::nothrow) _IpcClient();
74         SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
75
76         result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
77         SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
78
79         return E_SUCCESS;
80 }
81
82 _IAppManager*
83 _AppManagerProxy::GetService(void)
84 {
85         _AppManagerProxy* pProxy = null;
86
87         if (__pSelf == null)
88         {
89                 SysLog(NID_APP, "Create new instance");
90
91                 pProxy = new (std::nothrow) _AppManagerProxy();
92                 SysTryReturn(NID_APP, pProxy != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create _AppManagerProxy");
93
94                 result r = pProxy->Construct();
95                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
96
97                 __pSelf = pProxy;
98         }
99
100         return __pSelf;
101
102 CATCH:
103         delete pProxy;
104         return null;
105 }
106
107 void
108 _AppManagerProxy::SetService(_IAppManager* pAppManager)
109 {
110         __pSelf = pAppManager;
111 }
112
113
114 result
115 _AppManagerProxy::LaunchApplication(const AppId& appId, int req)
116 {
117         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
118         SysLog(NID_APP, "");
119
120         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_LaunchApplication(appId, req));
121         result r = __pIpcClient->SendRequest(*pMsg.get());
122         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
123
124         return E_SUCCESS;
125 }
126
127 result
128 _AppManagerProxy::TerminateApplication(const AppId& appId)
129 {
130         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
131         SysLog(NID_APP, "");
132
133         result response = E_SUCCESS;
134         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_TerminateApplication(appId, &response));
135         result r = __pIpcClient->SendRequest(*pMsg.get());
136         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
137
138         return response;
139 }
140
141 bool
142 _AppManagerProxy::IsRunning(const AppId& appId)
143 {
144         bool isRunning = false;
145
146         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsRunning(appId, &isRunning));
147         result r = __pIpcClient->SendRequest(*pMsg.get());
148         SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
149
150         return isRunning;
151 }
152 result
153 _AppManagerProxy::GetRunningAppList(Collection::ArrayList* pArray)
154 {
155         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
156
157         SysLog(NID_APP, "");
158
159         result response = E_SUCCESS;
160         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_GetRunningAppList(pArray, &response));
161         result r = __pIpcClient->SendRequest(*pMsg.get());
162         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
163
164         return response;
165 }
166
167 result
168 _AppManagerProxy::RegisterApplication(const String& packageId, const String& executableName, _AppType appType, int pId)
169 {
170         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
171         SysLog(NID_APP, "");
172
173         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(packageId, executableName, static_cast<int>(appType), pId));
174         result r = __pIpcClient->SendRequest(*pMsg.get());
175         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
176
177         return E_SUCCESS;
178 }
179
180 result
181 _AppManagerProxy::UnregisterApplication(int pId)
182 {
183         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
184
185         SysLog(NID_APP, "");
186
187         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
188         result r = __pIpcClient->SendRequest(*pMsg.get());
189         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
190
191         return E_SUCCESS;
192 }
193
194 void
195 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
196 {
197         IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
198                 IPC_MESSAGE_HANDLER_EX(AppManager_OnEventReceived, &client, OnEventReceived )
199                 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
200                 IPC_MESSAGE_HANDLER_EX(AppManager_OnAppLifecycleEventReceived, &client, OnAppLifecycleEventReceived )
201         IPC_END_MESSAGE_MAP()
202 }
203
204 bool
205 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
206 {
207         SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
208
209         __pServiceEventListener->OnServiceEventReceived(-1, arg);
210
211         return true;
212 }
213
214 void
215 _AppManagerProxy::OnTerminateApplicationRequested(void)
216 {
217         SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
218
219         __pServiceEventListener->OnTerminateApplicationRequested(-1);
220 }
221
222 result
223 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
224 {
225         __pServiceEventListener = pListener;
226         return E_SUCCESS;
227 }
228
229 result
230 _AppManagerProxy::AddEventListener(int clientId)
231 {
232         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
233         SysLog(NID_APP, "");
234
235         std::auto_ptr<IPC::Message> pMsg(new (std::nothrow) AppManager_AddEventListener(_AppInfo::GetProcessId()));
236         result r = __pIpcClient->SendRequest(*pMsg);
237         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
238
239         return E_SUCCESS;
240 }
241
242 result
243 _AppManagerProxy::RemoveEventListener(int clientId)
244 {
245         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
246         SysLog(NID_APP, "");
247
248         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RemoveEventListener(_AppInfo::GetProcessId()));
249         result r = __pIpcClient->SendRequest(*pMsg);
250         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
251
252         return E_SUCCESS;
253 }
254
255 bool 
256 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
257 {
258         SysLog(NID_APP, "begin.");
259         bool isUserPreferredApp = false;
260         result response = E_SUCCESS;
261         
262         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsUserPreferredAppForAppControlResolution(appId, &isUserPreferredApp, &response));
263         result r = __pIpcClient->SendRequest(*pMsg.get());
264         SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
265
266         SetLastResult(response);
267         SysLog(NID_APP, "end.");        
268         return isUserPreferredApp;
269 }
270
271 result 
272 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
273 {
274         SysLog(NID_APP, "begin.");
275         result response = E_SUCCESS;
276         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_ClearUserPreferenceForAppControlResolution(appId, &response));
277         result r = __pIpcClient->SendRequest(*pMsg.get());
278         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
279
280         SysLog(NID_APP, "end.");
281         return response;
282 }
283
284 result
285 _AppManagerProxy::RegisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
286 {
287         SysLog(NID_APP, "begin.");
288         result response = E_SUCCESS;
289         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterAppForAppLifecycleEvent(appId, &response));
290         result r = __pIpcClient->SendRequest(*pMsg.get());
291         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
292
293         SysLog(NID_APP, "end.");
294         return response;
295 }
296
297 result
298 _AppManagerProxy::UnregisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
299 {
300         SysLog(NID_APP, "begin.");
301         result response = E_SUCCESS;
302         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterAppForAppLifecycleEvent(appId, &response));
303         result r = __pIpcClient->SendRequest(*pMsg.get());
304         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
305
306         SysLog(NID_APP, "end.");
307         return response;
308 }
309
310 void
311 _AppManagerProxy::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
312 {
313         SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
314
315         _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
316
317         if (appLifecycleEventType == 0)
318         {
319                 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
320         }
321         else if (appLifecycleEventType == 1)
322         {
323                 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
324         }
325         else
326         {
327                 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
328         }
329
330         __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);
331 }
332
333 } } // Tizen::App