Merge "add cleanup for osp-appfw unload" 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 bool _AppManagerProxy::__isDeletable = true;
51 _IAppManager* _AppManagerProxy::__pSelf = null;
52 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
53
54 _AppManagerProxy::_AppManagerProxy(void)
55         : __pIpcClient(null)
56 {
57         SysLog(NID_APP, "");
58 }
59
60 _AppManagerProxy::~_AppManagerProxy(void)
61 {
62         delete __pIpcClient;
63 }
64
65
66 result
67 _AppManagerProxy::Construct(void)
68 {
69         __pIpcClient = new (std::nothrow) _IpcClient();
70         SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
71
72         result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
73         SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
74
75         return E_SUCCESS;
76 }
77
78 _IAppManager*
79 _AppManagerProxy::GetService(void)
80 {
81         _AppManagerProxy* pProxy = null;
82
83         if (__pSelf == null)
84         {
85                 SysLog(NID_APP, "Create new instance");
86
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");
89
90                 result r = pProxy->Construct();
91                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
92
93                 __pSelf = pProxy;
94         }
95
96         return __pSelf;
97
98 CATCH:
99         delete pProxy;
100         return null;
101 }
102
103
104 void
105 _AppManagerProxy::SetService(_IAppManager* pAppManager)
106 {
107         __pSelf = pAppManager;
108         __isDeletable = false;
109 }
110
111 void
112 _AppManagerProxy::DeleteService(void)
113 {
114         if (__isDeletable)
115         {
116                 delete __pSelf;
117                 __pSelf = null;
118         }
119 }
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 result
137 _AppManagerProxy::RegisterApplication(const AppId& appId, _AppType appType, int pId)
138 {
139         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
140         SysLog(NID_APP, "");
141
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.");
145
146         return E_SUCCESS;
147 }
148
149 result
150 _AppManagerProxy::UnregisterApplication(int pId)
151 {
152         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
153
154         SysLog(NID_APP, "");
155
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.");
159
160         return E_SUCCESS;
161 }
162
163 void
164 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
165 {
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()
171 }
172
173 bool
174 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
175 {
176         SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
177
178         __pServiceEventListener->OnServiceEventReceived(-1, arg);
179
180         return true;
181 }
182
183 void
184 _AppManagerProxy::OnTerminateApplicationRequested(void)
185 {
186         SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
187
188         __pServiceEventListener->OnTerminateApplicationRequested(-1);
189 }
190
191 result
192 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
193 {
194         __pServiceEventListener = pListener;
195         return E_SUCCESS;
196 }
197
198 result
199 _AppManagerProxy::AddEventListener(int clientId)
200 {
201         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
202         SysLog(NID_APP, "");
203
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.");
207
208         return E_SUCCESS;
209 }
210
211 result
212 _AppManagerProxy::RemoveEventListener(int clientId)
213 {
214         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
215         SysLog(NID_APP, "");
216
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.");
220
221         return E_SUCCESS;
222 }
223
224 bool 
225 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
226 {
227         SysLog(NID_APP, "begin.");
228         bool isUserPreferredApp = false;
229         result response = E_SUCCESS;
230         
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.");
234
235         SetLastResult(response);
236         SysLog(NID_APP, "end.");        
237         return isUserPreferredApp;
238 }
239
240 result 
241 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
242 {
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.");
248
249         SysLog(NID_APP, "end.");
250         return response;
251 }
252
253 result
254 _AppManagerProxy::RegisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
255 {
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.");
261
262         SysLog(NID_APP, "end.");
263         return response;
264 }
265
266 result
267 _AppManagerProxy::UnregisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
268 {
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.");
274
275         SysLog(NID_APP, "end.");
276         return response;
277 }
278
279 void
280 _AppManagerProxy::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
281 {
282         SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
283
284         _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
285
286         if (appLifecycleEventType == 0)
287         {
288                 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
289         }
290         else if (appLifecycleEventType == 1)
291         {
292                 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
293         }
294         else
295         {
296                 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
297         }
298
299         __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);
300 }
301
302 } } // Tizen::App