Merge "Fix default memory leak for AppControl request" into tizen_2.2
[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
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Io;
41 using namespace Tizen::Base::Runtime;
42
43 static const int INVALID_CLIENT_ID = -1;
44
45 namespace Tizen { namespace App
46 {
47
48 const char IPC_SERVER_NAME[] = "osp.app.ipcserver.appmanager";
49
50 _IAppManager* _AppManagerProxy::__pSelf = null;
51 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
52
53 _AppManagerProxy::_AppManagerProxy(void)
54         : __pIpcClient(null)
55 {
56         SysLog(NID_APP, "");
57 }
58
59 _AppManagerProxy::~_AppManagerProxy(void)
60 {
61         delete __pIpcClient;
62 }
63
64
65 result
66 _AppManagerProxy::Construct(void)
67 {
68         __pIpcClient = new (std::nothrow) _IpcClient();
69         SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
70
71         result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
72         SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
73
74         return E_SUCCESS;
75 }
76
77 _IAppManager*
78 _AppManagerProxy::GetService(void)
79 {
80         _AppManagerProxy* pProxy = null;
81
82         if (__pSelf == null)
83         {
84                 SysLog(NID_APP, "Create new instance");
85
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");
88
89                 result r = pProxy->Construct();
90                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
91
92                 __pSelf = pProxy;
93         }
94
95         return __pSelf;
96
97 CATCH:
98         delete pProxy;
99         return null;
100 }
101
102 void
103 _AppManagerProxy::SetService(_IAppManager* pAppManager)
104 {
105         __pSelf = pAppManager;
106 }
107
108
109 result
110 _AppManagerProxy::LaunchApplication(const AppId& appId, int req)
111 {
112         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
113         SysLog(NID_APP, "");
114
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.");
118
119         return E_SUCCESS;
120 }
121
122 result
123 _AppManagerProxy::TerminateApplication(const AppId& appId)
124 {
125         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
126         SysLog(NID_APP, "");
127
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.");
132
133         return response;
134 }
135
136 bool
137 _AppManagerProxy::IsRunning(const AppId& appId)
138 {
139         bool isRunning = false;
140
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.");
144
145         return isRunning;
146 }
147 result
148 _AppManagerProxy::GetRunningAppList(Collection::ArrayList* pArray)
149 {
150         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
151
152         SysLog(NID_APP, "");
153
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.");
158
159         return response;
160 }
161
162 result
163 _AppManagerProxy::RegisterApplication(const String& packageId, const String& executableName, _AppType appType, int pId)
164 {
165         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
166         SysLog(NID_APP, "");
167
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.");
171
172         return E_SUCCESS;
173 }
174
175 result
176 _AppManagerProxy::UnregisterApplication(int pId)
177 {
178         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
179
180         SysLog(NID_APP, "");
181
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.");
185
186         return E_SUCCESS;
187 }
188
189 void
190 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
191 {
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()
197 }
198
199 bool
200 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
201 {
202         SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
203
204         __pServiceEventListener->OnServiceEventReceived(-1, arg);
205
206         return true;
207 }
208
209 void
210 _AppManagerProxy::OnTerminateApplicationRequested(void)
211 {
212         SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
213
214         __pServiceEventListener->OnTerminateApplicationRequested(-1);
215 }
216
217 result
218 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
219 {
220         __pServiceEventListener = pListener;
221         return E_SUCCESS;
222 }
223
224 result
225 _AppManagerProxy::AddEventListener(int clientId)
226 {
227         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
228         SysLog(NID_APP, "");
229
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.");
233
234         return E_SUCCESS;
235 }
236
237 result
238 _AppManagerProxy::RemoveEventListener(int clientId)
239 {
240         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
241         SysLog(NID_APP, "");
242
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.");
246
247         return E_SUCCESS;
248 }
249
250 bool 
251 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
252 {
253         SysLog(NID_APP, "begin.");
254         bool isUserPreferredApp = false;
255         result response = E_SUCCESS;
256         
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.");
260
261         SetLastResult(response);
262         SysLog(NID_APP, "end.");        
263         return isUserPreferredApp;
264 }
265
266 result 
267 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
268 {
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.");
274
275         SysLog(NID_APP, "end.");
276         return response;
277 }
278
279 result
280 _AppManagerProxy::RegisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
281 {
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.");
287
288         SysLog(NID_APP, "end.");
289         return response;
290 }
291
292 result
293 _AppManagerProxy::UnregisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
294 {
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.");
300
301         SysLog(NID_APP, "end.");
302         return response;
303 }
304
305 void
306 _AppManagerProxy::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
307 {
308         SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
309
310         _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
311
312         if (appLifecycleEventType == 0)
313         {
314                 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
315         }
316         else if (appLifecycleEventType == 1)
317         {
318                 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
319         }
320         else
321         {
322                 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
323         }
324
325         __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);
326 }
327
328 } } // Tizen::App