Merge "Add a _LocalizedNumParser class and 4 static functions" into tizen_2.1
[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 namespace
39 {
40
41 const char IPC_SERVER_NAME[] = "osp.app.ipcserver.appmanager";
42 const int INVALID_CLIENT_ID = -1;
43 };
44
45
46 namespace Tizen { namespace App
47 {
48
49 using namespace Tizen::Base;
50 using namespace Tizen::Io;
51 using namespace Tizen::Base::Runtime;
52
53
54 _IAppManager* _AppManagerProxy::__pSelf = null;
55 _IAppManagerServiceEventListener* _AppManagerProxy::__pServiceEventListener = null;
56
57 _AppManagerProxy::_AppManagerProxy(void)
58         : __pIpcClient(null)
59 {
60         SysLog(NID_APP, "");
61 }
62
63 _AppManagerProxy::~_AppManagerProxy(void)
64 {
65         delete __pIpcClient;
66 }
67
68
69 result
70 _AppManagerProxy::Construct(void)
71 {
72         __pIpcClient = new (std::nothrow) _IpcClient();
73         SysTryReturnResult(NID_APP, __pIpcClient != null, E_OUT_OF_MEMORY, "_IpcClient creation failed.");
74
75         result r = __pIpcClient->Construct(IPC_SERVER_NAME, this);
76         SysTryReturn(NID_APP, !IsFailed(r), r, r, "_IpcClient constructing faliied [%s].", GetErrorMessage(r));
77
78         return E_SUCCESS;
79 }
80
81 _IAppManager*
82 _AppManagerProxy::GetService(void)
83 {
84         _AppManagerProxy* pProxy = null;
85
86         if (__pSelf == null)
87         {
88                 SysLog(NID_APP, "Create new instance");
89
90                 pProxy = new (std::nothrow) _AppManagerProxy();
91                 SysTryReturn(NID_APP, pProxy != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] failed to create _AppManagerProxy");
92
93                 result r = pProxy->Construct();
94                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] pProxy->Construct() failure.", GetErrorMessage(r));
95
96                 __pSelf = pProxy;
97         }
98
99         return __pSelf;
100
101 CATCH:
102         delete pProxy;
103         return null;
104 }
105
106 void
107 _AppManagerProxy::SetService(_IAppManager* pAppManager)
108 {
109         __pSelf = pAppManager;
110 }
111
112
113 result
114 _AppManagerProxy::LaunchApplication(const AppId& appId, int req)
115 {
116         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
117         SysLog(NID_APP, "");
118
119         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_LaunchApplication(appId, req));
120         result r = __pIpcClient->SendRequest(*pMsg.get());
121         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
122
123         return E_SUCCESS;
124 }
125
126 result
127 _AppManagerProxy::TerminateApplication(const AppId& appId)
128 {
129         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
130         SysLog(NID_APP, "");
131
132         result response = E_SUCCESS;
133         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_TerminateApplication(appId, &response));
134         result r = __pIpcClient->SendRequest(*pMsg.get());
135         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
136
137         return response;
138 }
139
140 bool
141 _AppManagerProxy::IsRunning(const AppId& appId)
142 {
143         bool isRunning = false;
144
145         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_IsRunning(appId, &isRunning));
146         result r = __pIpcClient->SendRequest(*pMsg.get());
147         SysTryReturn(NID_APP, !IsFailed(r), false, r, "SendRequest is failed.");
148
149         return isRunning;
150 }
151 result
152 _AppManagerProxy::GetRunningAppList(Collection::ArrayList* pArray)
153 {
154         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
155
156         SysLog(NID_APP, "");
157
158         result response = E_SUCCESS;
159         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_GetRunningAppList(pArray, &response));
160         result r = __pIpcClient->SendRequest(*pMsg.get());
161         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
162
163         return response;
164 }
165
166 result
167 _AppManagerProxy::RegisterApplication(const String& packageId, const String& executableName, _AppType appType, int pId)
168 {
169         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
170         SysLog(NID_APP, "");
171
172         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RegisterApplication(packageId, executableName, static_cast<int>(appType), pId));
173         result r = __pIpcClient->SendRequest(*pMsg.get());
174         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
175
176         return E_SUCCESS;
177 }
178
179 result
180 _AppManagerProxy::UnregisterApplication(int pId)
181 {
182         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
183
184         SysLog(NID_APP, "");
185
186         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_UnregisterApplication(pId));
187         result r = __pIpcClient->SendRequest(*pMsg.get());
188         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
189
190         return E_SUCCESS;
191 }
192
193 void
194 _AppManagerProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
195 {
196         IPC_BEGIN_MESSAGE_MAP(_AppManagerProxy, message)
197                 IPC_MESSAGE_HANDLER_EX(AppManager_OnEventReceived, &client, OnEventReceived )
198                 IPC_MESSAGE_HANDLER_EX(AppManager_OnTerminateApplicationRequested, &client, OnTerminateApplicationRequested )
199         IPC_END_MESSAGE_MAP()
200 }
201
202 bool
203 _AppManagerProxy::OnEventReceived(const _AppManagerEventArg& arg)
204 {
205         SysTryReturnResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "__pServiceEventListener instance must not be null.");
206
207         __pServiceEventListener->OnServiceEventReceived(-1, arg);
208
209         return true;
210 }
211
212 void
213 _AppManagerProxy::OnTerminateApplicationRequested(void)
214 {
215         SysTryReturnVoidResult(NID_APP, __pServiceEventListener != null, E_INVALID_STATE, "[E_INVALID_STATE] __pServiceEventListener instance must not be null.");
216
217         __pServiceEventListener->OnTerminateApplicationRequested(-1);
218 }
219
220 result
221 _AppManagerProxy::InitEventListener(_IAppManagerServiceEventListener* pListener)
222 {
223         __pServiceEventListener = pListener;
224         return E_SUCCESS;
225 }
226
227 result
228 _AppManagerProxy::AddEventListener(int clientId)
229 {
230         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
231         SysLog(NID_APP, "");
232
233         std::auto_ptr<IPC::Message> pMsg(new (std::nothrow) AppManager_AddEventListener(_AppInfo::GetProcessId()));
234         result r = __pIpcClient->SendRequest(*pMsg);
235         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
236
237         return E_SUCCESS;
238 }
239
240 result
241 _AppManagerProxy::RemoveEventListener(int clientId)
242 {
243         SysTryReturnResult(NID_APP, __pIpcClient != null, E_INVALID_STATE, "__pIpcClient instance must not be null.");
244         SysLog(NID_APP, "");
245
246         std::auto_ptr<IPC::Message> pMsg (new (std::nothrow) AppManager_RemoveEventListener(_AppInfo::GetProcessId()));
247         result r = __pIpcClient->SendRequest(*pMsg);
248         SysTryReturn(NID_APP, !IsFailed(r), r, r, "SendRequest is failed.");
249
250         return E_SUCCESS;
251 }
252
253
254 } } // Tizen::App