Merge "Add readlink failure case of timezone" 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::TerminateApplication(const AppId& appId)
111 {
112         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
113         SysLog(NID_APP, "");
114
115         result response = E_SUCCESS;
116         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_TerminateApplication(appId, &response));
117         result r = __pIpcClient->SendRequest(*pMsg.get());
118         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
119
120         return response;
121 }
122
123 result
124 _AppManagerProxy::RegisterApplication(const AppId& appId, _AppType appType, int pId)
125 {
126         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
127         SysLog(NID_APP, "");
128
129         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(appId, static_cast<int>(appType), pId));
130         result r = __pIpcClient->SendRequest(*pMsg.get());
131         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
132
133         return E_SUCCESS;
134 }
135
136 result
137 _AppManagerProxy::UnregisterApplication(int pId)
138 {
139         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
140
141         SysLog(NID_APP, "");
142
143         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
144         result r = __pIpcClient->SendRequest(*pMsg.get());
145         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
146
147         return E_SUCCESS;
148 }
149
150 void
151 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
152 {
153         IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
154                 IPC_MESSAGE_HANDLER_EX(AppManager_OnEventReceived, &client, OnEventReceived )
155                 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
156                 IPC_MESSAGE_HANDLER_EX(AppManager_OnAppLifecycleEventReceived, &client, OnAppLifecycleEventReceived )
157         IPC_END_MESSAGE_MAP()
158 }
159
160 bool
161 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
162 {
163         SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
164
165         __pServiceEventListener->OnServiceEventReceived(-1, arg);
166
167         return true;
168 }
169
170 void
171 _AppManagerProxy::OnTerminateApplicationRequested(void)
172 {
173         SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
174
175         __pServiceEventListener->OnTerminateApplicationRequested(-1);
176 }
177
178 result
179 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
180 {
181         __pServiceEventListener = pListener;
182         return E_SUCCESS;
183 }
184
185 result
186 _AppManagerProxy::AddEventListener(int clientId)
187 {
188         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
189         SysLog(NID_APP, "");
190
191         std::auto_ptr<IPC::Message> pMsg(new (std::nothrow) AppManager_AddEventListener(_AppInfo::GetProcessId()));
192         result r = __pIpcClient->SendRequest(*pMsg);
193         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
194
195         return E_SUCCESS;
196 }
197
198 result
199 _AppManagerProxy::RemoveEventListener(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_RemoveEventListener(_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 bool 
212 _AppManagerProxy::IsUserPreferredAppForAppControlResolution(const AppId& appId)
213 {
214         SysLog(NID_APP, "begin.");
215         bool isUserPreferredApp = false;
216         result response = E_SUCCESS;
217         
218         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsUserPreferredAppForAppControlResolution(appId, &isUserPreferredApp, &response));
219         result r = __pIpcClient->SendRequest(*pMsg.get());
220         SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
221
222         SetLastResult(response);
223         SysLog(NID_APP, "end.");        
224         return isUserPreferredApp;
225 }
226
227 result 
228 _AppManagerProxy::ClearUserPreferenceForAppControlResolution(const AppId& appId)
229 {
230         SysLog(NID_APP, "begin.");
231         result response = E_SUCCESS;
232         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_ClearUserPreferenceForAppControlResolution(appId, &response));
233         result r = __pIpcClient->SendRequest(*pMsg.get());
234         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
235
236         SysLog(NID_APP, "end.");
237         return response;
238 }
239
240 result
241 _AppManagerProxy::RegisterAppForAppLifecycleEvent(const AppId& appId, int clientId)
242 {
243         SysLog(NID_APP, "begin.");
244         result response = E_SUCCESS;
245         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterAppForAppLifecycleEvent(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::UnregisterAppForAppLifecycleEvent(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_UnregisterAppForAppLifecycleEvent(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 void
267 _AppManagerProxy::OnAppLifecycleEventReceived(const AppId& appId, int appLifecycleEventType)
268 {
269         SysTryLog(NID_APP, __pServiceEventListener != null, "__pServiceEventListener instance must not be null.");
270
271         _AppLifecycleEventType eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
272
273         if (appLifecycleEventType == 0)
274         {
275                 eventType = _APP_LIFECYCLE_EVENT_LAUNCH;
276         }
277         else if (appLifecycleEventType == 1)
278         {
279                 eventType = _APP_LIFECYCLE_EVENT_TERMINATE;
280         }
281         else
282         {
283                 SysLog(NID_APP, "Not expected appLifecycleEventType(%d)", appLifecycleEventType);
284         }
285
286         __pServiceEventListener->OnAppLifecycleEventReceived(-1, appId, eventType);
287 }
288
289 } } // Tizen::App