Merge "Fixed Klocwork issues" 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_IAppManagerServiceEventListener.h"
33 #include "FApp_AppManagerProxy.h"
34 #include "FApp_AppManagerIpcMessage.h"
35
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Io;
39 using namespace Tizen::Base::Runtime;
40
41 static const int INVALID_CLIENT_ID = -1;
42
43 namespace Tizen { namespace App
44 {
45
46 const char IPC_SERVER_NAME[] = "osp.app.ipcserver.appmanager";
47
48 bool _AppManagerProxy::__isDeletable = true;
49 _IAppManager* _AppManagerProxy::__pSelf = null;
50 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
51
52 _AppManagerProxy::_AppManagerProxy(void)
53         : __pIpcClient(null)
54 {
55         SysLog(NID_APP, "");
56 }
57
58 _AppManagerProxy::~_AppManagerProxy(void)
59 {
60         delete __pIpcClient;
61 }
62
63
64 result
65 _AppManagerProxy::Construct(void)
66 {
67         __pIpcClient = new (std::nothrow) _IpcClient();
68         SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
69
70         result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
71         SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
72
73         return E_SUCCESS;
74 }
75
76 _IAppManager*
77 _AppManagerProxy::GetService(void)
78 {
79         _AppManagerProxy* pProxy = null;
80
81         if (__pSelf == null)
82         {
83                 SysLog(NID_APP, "Create new instance");
84
85                 pProxy = new (std::nothrow) _AppManagerProxy();
86                 SysTryReturn(NID_APP, pProxy != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create _AppManagerProxy");
87
88                 result r = pProxy->Construct();
89                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
90
91                 __pSelf = pProxy;
92         }
93
94         return __pSelf;
95
96 CATCH:
97         delete pProxy;
98         return null;
99 }
100
101
102 void
103 _AppManagerProxy::SetService(_IAppManager* pAppManager)
104 {
105         __pSelf = pAppManager;
106         __isDeletable = false;
107 }
108
109 void
110 _AppManagerProxy::DeleteService(void)
111 {
112         if (__isDeletable)
113         {
114                 delete __pSelf;
115                 __pSelf = null;
116         }
117 }
118
119
120 result
121 _AppManagerProxy::TerminateApplication(const AppId& appId)
122 {
123         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
124         SysLog(NID_APP, "");
125
126         result response = E_SUCCESS;
127         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_TerminateApplication(appId, &response));
128         result r = __pIpcClient->SendRequest(*pMsg.get());
129         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
130
131         return response;
132 }
133
134 result
135 _AppManagerProxy::RegisterApplication(const AppId& appId, _AppType appType, int pId)
136 {
137         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
138         SysLog(NID_APP, "");
139
140         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(appId, static_cast<int>(appType), pId));
141         result r = __pIpcClient->SendRequest(*pMsg.get());
142         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
143
144         return E_SUCCESS;
145 }
146
147 result
148 _AppManagerProxy::UnregisterApplication(int pId)
149 {
150         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
151
152         SysLog(NID_APP, "");
153
154         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
155         result r = __pIpcClient->SendRequest(*pMsg.get());
156         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
157
158         return E_SUCCESS;
159 }
160
161 void
162 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
163 {
164         IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
165                 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
166                 IPC_MESSAGE_HANDLER_EX(AppManager_OnAppLifecycleEventReceived, &client, OnAppLifecycleEventReceived )
167         IPC_END_MESSAGE_MAP()
168 }
169
170
171 void
172 _AppManagerProxy::OnTerminateApplicationRequested(void)
173 {
174         SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
175
176         __pServiceEventListener->OnTerminateApplicationRequested(-1);
177 }
178
179 result
180 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
181 {
182         __pServiceEventListener = pListener;
183         return E_SUCCESS;
184 }
185
186
187 bool 
188 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
189 {
190         SysLog(NID_APP, "begin.");
191         bool isUserPreferredApp = false;
192         result response = E_SUCCESS;
193         
194         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsUserPreferredAppForAppControlResolution(appId, &isUserPreferredApp, &response));
195         result r = __pIpcClient->SendRequest(*pMsg.get());
196         SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
197
198         SetLastResult(response);
199         SysLog(NID_APP, "end.");        
200         return isUserPreferredApp;
201 }
202
203 result 
204 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
205 {
206         SysLog(NID_APP, "begin.");
207         result response = E_SUCCESS;
208         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_ClearUserPreferenceForAppControlResolution(appId, &response));
209         result r = __pIpcClient->SendRequest(*pMsg.get());
210         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
211
212         SysLog(NID_APP, "end.");
213         return response;
214 }
215
216 result
217 _AppManagerProxy::RegisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
218 {
219         SysLog(NID_APP, "begin.");
220         result response = E_SUCCESS;
221         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterAppForAppLifecycleEvent(appId, &response));
222         result r = __pIpcClient->SendRequest(*pMsg.get());
223         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
224
225         SysLog(NID_APP, "end.");
226         return response;
227 }
228
229 result
230 _AppManagerProxy::UnregisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
231 {
232         SysLog(NID_APP, "begin.");
233         result response = E_SUCCESS;
234         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterAppForAppLifecycleEvent(appId, &response));
235         result r = __pIpcClient->SendRequest(*pMsg.get());
236         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
237
238         SysLog(NID_APP, "end.");
239         return response;
240 }
241
242 void
243 _AppManagerProxy::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
244 {
245         SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
246
247         _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
248
249         if (appLifecycleEventType == 0)
250         {
251                 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
252         }
253         else if (appLifecycleEventType == 1)
254         {
255                 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
256         }
257         else
258         {
259                 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
260         }
261
262         __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);
263 }
264
265 } } // Tizen::App