Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FApp_AppManagerStub.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_AppManagerStub.cpp
20  * @brief       This is the implementation for the _AppManagerStub class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FIo_IpcServer.h>
25 #include <FSec_AccessController.h>
26
27 #include <FApp_AppManagerProxy.h>
28 #include <FApp_AppManagerIpcMessage.h>
29
30 #include "FApp_AppManagerService.h"
31 #include "FApp_AppManagerStub.h"
32 #include "FApp_ContextManager.h"
33
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Io;
37 using namespace Tizen::Text;
38 using namespace Tizen::Security;
39
40
41 namespace Tizen { namespace App {
42
43
44 _AppManagerStub::_AppManagerStub(void)
45 :__pIpcServer(null),
46  __pAppManagerService(null)
47 {
48         SysLog(NID_APP, "Enter\n");
49
50         SysLog(NID_APP, "Exit\n");
51 }
52
53 _AppManagerStub::~_AppManagerStub(void)
54 {
55         SysLog(NID_APP, "Enter\n");
56
57         if ( __pIpcServer != null)
58         {
59                 __pIpcServer->Stop();
60                 delete __pIpcServer;
61         }
62
63         delete __pAppManagerService;
64
65         SysLog(NID_APP, "Exit\n");
66 }
67
68 result
69 _AppManagerStub::Construct(_ContextManager *pContextMgr)
70 {
71         SysTryReturnResult(NID_APP, pContextMgr != null, E_INVALID_ARG, "pContextMgr is null.");
72
73         SysLog(NID_APP, "Enter\n");
74
75         result r = E_SUCCESS;
76
77         __pAppManagerService = new (std::nothrow) _AppManagerService();
78         SysTryReturnResult(NID_APP, __pAppManagerService != null, E_OUT_OF_MEMORY, "Not enough memory.");
79
80         r = __pAppManagerService->Construct(pContextMgr, this);
81         SysTryReturn(NID_APP, !IsFailed(r), r, r, "failed to __pAppManagerService->Construct. (%s)", GetErrorMessage(r) );
82
83         _AppManagerProxy::SetService(__pAppManagerService);
84
85         r = StartIpcServer();
86
87         SysLog(NID_APP, "Exit\n");
88
89         return r;
90 }
91
92 result
93 _AppManagerStub::StartIpcServer(void)
94 {
95         __pIpcServer = new (std::nothrow) _IpcServer();
96         SysTryReturn(NID_APP, __pIpcServer != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
97
98         result r = __pIpcServer->Construct( "osp.app.ipcserver.appmanager", *this);
99         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to create IPC server(%s)", GetErrorMessage(r), "osp.app.ipcserver.appmanager");
100
101         return E_SUCCESS;
102
103 CATCH:
104         delete __pIpcServer;
105         __pIpcServer = null;
106         return r;
107 }
108
109
110 // server to client message
111 result
112 _AppManagerStub::OnServiceEventReceived(const int clientId, const _AppManagerEventArg& arg)
113 {
114 //      SysLog(NID_APP, "appId:%ls, executableName:%ls, appType:%d", arg.GetAppId().GetPointer(), arg.GetName().GetPointer(), arg.GetAppType());
115         SysTryReturnResult(NID_APP, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
116
117         result r = __pIpcServer->SendResponse(clientId, new AppManager_OnEventReceived(arg));
118         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
119
120         return E_SUCCESS;
121 }
122
123 result
124 _AppManagerStub::OnTerminateApplicationRequested(int clientId)
125 {
126         SysTryReturnResult(NID_APP, __pIpcServer != null, E_INVALID_STATE, "__pIpcServer is null.");
127
128         result r = __pIpcServer->SendResponse(clientId, new AppManager_OnTerminateApplicationRequested());
129         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
130
131         return E_SUCCESS;
132 }
133
134
135 /////////////////////////////////////////////
136 // handlers
137 /////////////////////////////////////////////
138
139 bool
140 _AppManagerStub::OnLaunchApplication(const AppId& appId, const String& execName, int req)
141 {
142         SysTryCatch(NID_APP, __pAppManagerService != null, , E_INVALID_STATE, "__pAppManagerService is null!");
143
144         SysLog(NID_APP, "(appId:%ls)", appId.GetPointer());
145
146         __pAppManagerService->LaunchApplication(appId, execName, req);
147
148 CATCH:
149         return true;
150 }
151
152 bool
153 _AppManagerStub::OnTerminateApplication(const AppId& appId, const String& execName, result *pRes)
154 {
155         SysTryCatch(NID_APP, __pAppManagerService != null, *pRes = E_INVALID_STATE, E_INVALID_STATE, "__pAppManagerService is null!");
156
157         SysLog(NID_APP, "(appId:%ls, %ls)", appId.GetPointer(), execName.GetPointer());
158
159         // APPLICATION_MANAGER
160         *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_APPLICATION_KILL);
161
162         if (IsFailed(*pRes))
163         {
164                 SysLog(NID_APP, "[E_PRIVILEGE_DENIED]The application does not have the privilege to call this method.");
165                 *pRes = E_PRIVILEGE_DENIED;
166                 return true;    
167         }
168
169         *pRes = __pAppManagerService->TerminateApplication(appId, execName);
170
171 CATCH:
172         return true;
173 }
174
175 bool
176 _AppManagerStub::OnIsRunning(const AppId& appId, const String& execName, bool *pRes)
177 {
178         SysTryCatch(NID_APP, __pAppManagerService != null, *pRes = false, E_INVALID_STATE, "__pAppManagerService is null!");
179
180         SysLog(NID_APP, "_AppManagerStub::IsRunning(%ls, %ls)", appId.GetPointer(), execName.GetPointer());
181         *pRes = __pAppManagerService->IsRunning(appId, execName);
182
183 CATCH:
184         return true;
185 }
186
187 bool
188 _AppManagerStub::OnGetRunningAppList(Tizen::Base::Collection::ArrayList* pArray, result *pRes)
189 {
190         SysTryReturn(NID_APP, pArray != null, E_INVALID_ARG, E_INVALID_ARG, "pArray shoud not be null!");
191         SysTryCatch(NID_APP, __pAppManagerService != null, *pRes = E_INVALID_STATE, E_INVALID_STATE, "__pAppManagerService is null!");
192         SysLog(NID_APP, "");
193
194         *pRes = __pAppManagerService->GetRunningAppList(pArray);
195
196 CATCH:
197         return true;
198 }
199
200 bool
201 _AppManagerStub::OnRegisterApplication(const AppId& appId, const String& executableName, int appType, int pid)
202 {
203         SysTryCatch(NID_APP, __pAppManagerService != null, , E_INVALID_STATE, "__pAppManagerService is null!");
204         SysLog(NID_APP, "_AppManagerStub::RegisterApplication(appId:%ls, pid:%d)\n", appId.GetPointer(), pid);
205
206         __pAppManagerService->RegisterApplication(appId, executableName, static_cast<_AppType>(appType), pid, __pIpcServer->GetClientId());
207
208 CATCH:
209         return true;
210 }
211
212 bool
213 _AppManagerStub::OnUnregisterApplication(int pid)
214 {
215         SysTryCatch(NID_APP, __pAppManagerService != null, , E_INVALID_STATE, "__pAppManagerService is null!");
216         SysLog(NID_APP, "_AppManagerStub::UnregisterApplication(pid:%d)\n", pid);
217         __pAppManagerService->UnregisterApplication(pid);
218
219 CATCH:
220         return true;
221 }
222
223 bool
224 _AppManagerStub::OnAddEventListener(int pid)
225 {
226         SysTryReturn(NID_APP, __pIpcServer != null, true, E_INVALID_STATE, "__pIpcServer should not be null!");
227
228         __pAppManagerService->AddEventListener(__pIpcServer->GetClientId());//, this);
229         return true;
230 }
231
232 bool
233 _AppManagerStub::OnRemoveEventListener(int pid)
234 {
235         SysTryReturn(NID_APP, __pIpcServer != null, true, E_INVALID_STATE, "__pIpcServer should not be null!");
236
237         __pAppManagerService->RemoveEventListener(__pIpcServer->GetClientId());//, this);
238         return true;
239 }
240
241
242 void
243 _AppManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
244 {
245         SysLog(NID_APP, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), server.GetClientId());
246
247         IPC_BEGIN_MESSAGE_MAP(_AppManagerStub, message)
248                 IPC_MESSAGE_HANDLER_EX(AppManager_LaunchApplication, &server, OnLaunchApplication)
249                 IPC_MESSAGE_HANDLER_EX(AppManager_TerminateApplication, &server, OnTerminateApplication)
250                 IPC_MESSAGE_HANDLER_EX(AppManager_IsRunning, &server, OnIsRunning)
251                 IPC_MESSAGE_HANDLER_EX(AppManager_GetRunningAppList, &server, OnGetRunningAppList)
252                 IPC_MESSAGE_HANDLER_EX(AppManager_RegisterApplication, &server, OnRegisterApplication)
253                 IPC_MESSAGE_HANDLER_EX(AppManager_UnregisterApplication, &server, OnUnregisterApplication)
254                 IPC_MESSAGE_HANDLER_EX(AppManager_AddEventListener, &server, OnAddEventListener)
255                 IPC_MESSAGE_HANDLER_EX(AppManager_RemoveEventListener, &server, OnRemoveEventListener)
256         IPC_END_MESSAGE_MAP()
257 }
258
259 void
260 _AppManagerStub::OnIpcServerStarted(const _IpcServer& server)
261 {
262 //      SysLog(NID_APP, "\n");
263 }
264
265 void
266 _AppManagerStub::OnIpcServerStopped(const _IpcServer& server)
267 {
268 //      SysLog(NID_APP, "\n");
269 }
270
271 void
272 _AppManagerStub::OnIpcClientConnected(const _IpcServer& server, int clientId)
273 {
274 //      SysLog(NID_APP, "(clientId:%d)\n", clientId);
275 }
276
277 void
278 _AppManagerStub::OnIpcClientDisconnected(const _IpcServer& server, int clientId)
279 {
280 //      SysLog(NID_APP, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), clientId);
281 }
282
283
284 }}//namespace Tizen { namespace App {