sync with master
[platform/framework/native/appfw.git] / src / app / FApp_AppManagerProxy.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FApp_AppManagerProxy.cpp
20  * @brief       This is the implementation for the _AppManagerProxy class.
21  */
22
23 #include <new>
24 #include <memory>
25
26 #include <FBaseErrors.h>
27
28 #include <FBaseSysLog.h>
29 #include <FIo_IpcClient.h>
30
31 #include "FApp_Types.h"
32 #include "FApp_AppInfo.h"
33 #include "FApp_IAppManagerEventListener.h"
34 #include "FApp_IAppManagerServiceEventListener.h"
35 #include "FApp_AppManagerEventArg.h"
36 #include "FApp_AppManagerProxy.h"
37 #include "FApp_AppManagerIpcMessage.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_END_MESSAGE_MAP()
201 }
202
203 bool
204 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
205 {
206         SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
207
208         __pServiceEventListener->OnServiceEventReceived(-1, arg);
209
210         return true;
211 }
212
213 void
214 _AppManagerProxy::OnTerminateApplicationRequested(void)
215 {
216         SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
217
218         __pServiceEventListener->OnTerminateApplicationRequested(-1);
219 }
220
221 result
222 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
223 {
224         __pServiceEventListener = pListener;
225         return E_SUCCESS;
226 }
227
228 result
229 _AppManagerProxy::AddEventListener(int clientId)
230 {
231         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
232         SysLog(NID_APP, "");
233
234         std::auto_ptr<IPC::Message> pMsg(new (std::nothrow) AppManager_AddEventListener(_AppInfo::GetProcessId()));
235         result r = __pIpcClient->SendRequest(*pMsg);
236         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
237
238         return E_SUCCESS;
239 }
240
241 result
242 _AppManagerProxy::RemoveEventListener(int clientId)
243 {
244         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
245         SysLog(NID_APP, "");
246
247         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RemoveEventListener(_AppInfo::GetProcessId()));
248         result r = __pIpcClient->SendRequest(*pMsg);
249         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
250
251         return E_SUCCESS;
252 }
253
254
255 } } // Tizen::App