Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FApp_AppManagerService.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_AppManagerService.cpp
20  * @brief       This is the implementation for the _AppManagerService class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FApp_AppInfo.h>
25 #include <FApp_Aul.h>
26 #include <FApp_AppManagerEventArg.h>
27 #include <FApp_IAppManagerServiceEventListener.h>
28
29 #include "FAppPkg_PackageManagerImpl.h"
30 #include "FApp_ContextManager.h"
31 #include "FApp_AppManagerService.h"
32
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Io;
36 using namespace Tizen::Text;
37
38
39 namespace Tizen { namespace App {
40
41
42 _AppManagerService::_AppManagerService(void)
43 : __pContextMgr(null)
44 , __pStub(null)
45 , __pProxy(null)
46 {
47         SysLog(NID_APP, "");
48 }
49
50 _AppManagerService::~_AppManagerService(void)
51 {
52         SysLog(NID_APP, "");
53 }
54
55 result
56 _AppManagerService::Construct(_ContextManager *pContextMgr, _IAppManagerServiceEventListener* pStub)
57 {
58         SysTryReturnResult(NID_APP, pContextMgr != null, E_INVALID_ARG, "pContextMgr should not be null!");
59
60         SysLog(NID_APP, "Enter\n");
61         __pContextMgr = pContextMgr;
62         __pContextMgr->SetEventListener(*this);
63         __pStub = pStub;
64         __eventListeners.Construct();
65         SysLog(NID_APP, "Exit\n");
66
67         return E_SUCCESS;
68 }
69
70
71 ///////////////////////////////////////////
72 // stub implementations
73 ///////////////////////////////////////////
74
75 result
76 _AppManagerService::LaunchApplication(const AppId& appId, const String& executableName, int req)
77 {
78         SysLog(NID_APP, "(appId:%ls, serviceId:%ls)", appId.GetPointer(), executableName.GetPointer());
79         return E_SUCCESS;
80 }
81
82 result
83 _AppManagerService::TerminateApplication(const AppId& appId, const String& executableName)
84 {
85         SysTryReturnResult(NID_APP, __pContextMgr != null, E_INVALID_STATE, "pContextMgr should not be null!");
86         SysTryReturnResult(NID_APP, __pStub != null, E_INVALID_STATE, "__pStub should not be null!");
87
88         SysTryReturnResult(NID_APP, appId.IsEmpty() == false, E_INVALID_ARG, "The appId is empty.")
89         SysTryReturnResult(NID_APP, appId.GetLength() < WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED, "The appId is too long (Maximum %d bytes).", WIDGET_APP_MAX_APPID_LENGTH)
90         SysTryReturnResult(NID_APP, appId != _AppInfo::GetAppId(), E_INVALID_ARG, "This service can't be terminated.")
91         //SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND, "The application(%ls) is not installed.", appId.GetPointer());
92
93         const _AppContext* pAppContext = __pContextMgr->Find(appId, executableName);
94         SysTryReturnResult(NID_APP, pAppContext != null, E_OBJ_NOT_FOUND, "can't find appId(%ls, %ls).", appId.GetPointer(), executableName.GetPointer());
95
96         SysLog(NID_APP, "trying aul_terminate_pid(appId:%ls, exec:%ls, pid:%d).", appId.GetPointer(), executableName.GetPointer(), pAppContext->pId );
97
98         return _Aul::TerminateApplicationByPid( pAppContext->pId );
99 //      return __pStub->OnTerminateApplicationRequested(pAppContext->ipcClientId);
100 }
101
102 bool
103 _AppManagerService::IsRunning(const AppId& appId, const String& executableName)
104 {
105         SysTryReturnResult(NID_APP, appId.IsEmpty()==false, E_INVALID_ARG, "The appId is empty.")
106         SysTryReturnResult(NID_APP, appId.GetLength() < WIDGET_APP_MAX_APPID_LENGTH, E_INVALID_ARG, "The appId is too long (Maximum %d bytes).", WIDGET_APP_MAX_APPID_LENGTH)
107
108         return _Aul::IsRunning(appId, executableName);
109 }
110
111 result
112 _AppManagerService::GetRunningAppList(Tizen::Base::Collection::ArrayList* pArray)
113 {
114         SysTryReturnResult(NID_APP, __pContextMgr != null, E_INVALID_STATE, "pContextMgr should not be null!");
115
116         return __pContextMgr->GetAppListN(pArray);
117 }
118
119 result
120 _AppManagerService::RegisterApplication(const AppId& appId, const String& executableName, _AppType appType, int pid)
121 {
122         return RegisterApplication(appId, executableName, appType, pid, -1);
123 }
124
125 result
126 _AppManagerService::RegisterApplication(const AppId& appId, const String& executableName, _AppType appType, int pid, int clientId)
127 {
128         if (_AppInfo::GetProcessId() == pid)
129         {
130                 SysLog(NID_APP, "Service itself is registered.");
131         }
132         else
133         {
134                 SysLog(NID_APP, "Sending event for %ls(%d).", appId.GetPointer(), pid);
135
136                 _AppManagerEventArg arg(appId, appType, _AppManagerEvent::_APP_MANAGER_EVENT_LAUNCHED);
137                 SendEventToAllListeners(arg);
138         }
139
140         SysTryReturnResult(NID_APP, __pContextMgr != null, E_INVALID_STATE, "pContextMgr should not be null!");
141         return __pContextMgr->Register(appId, executableName, appType, pid, clientId, true);
142 }
143
144 result
145 _AppManagerService::UnregisterApplication(int pid)
146 {
147         SysTryReturnResult(NID_APP, __pContextMgr != null, E_INVALID_STATE, "pContextMgr should not be null!");
148         return __pContextMgr->Unregister(pid);
149 }
150
151
152 //const _AppContext*
153 //_AppManagerService::GetAppInfoSimple(const AppId& appId, const String* pExecutableName) const
154 //{
155 //      SysTryReturn(NID_APP, __pContextMgr != null, null, E_INVALID_STATE, "pContextMgr should not be null!");
156 //
157 //      return __pContextMgr->Find(appId, pExecutableName);
158 //}
159
160 result
161 _AppManagerService::InitEventListener( _IAppManagerServiceEventListener* pListener)
162 {
163         __pProxy = pListener;
164         return E_SUCCESS;
165 }
166
167
168 result
169 _AppManagerService::AddEventListener(int clientId)
170 {
171         return __eventListeners.Add(clientId, null);
172 }
173
174 result
175 _AppManagerService::RemoveEventListener(int clientId)
176 {
177         return __eventListeners.Remove(clientId);
178 }
179
180 void
181 _AppManagerService::OnApplicationTerminated(const _AppContext& appInfo)
182 {
183         SysLog(NID_APP, "Enter");
184         if ( appInfo.pId == _AppInfo::GetProcessId() )
185         {
186                 SysLog(NID_APP, "Service itself is terminated.");
187                 return;
188         }
189
190         RemoveEventListener(appInfo.ipcClientId);
191
192         _AppManagerEventArg arg(appInfo.appId, appInfo.appType, _AppManagerEvent::_APP_MANAGER_EVENT_TERMINATED);
193         SendEventToAllListeners(arg);
194 }
195
196 result
197 _AppManagerService::SendEventToAllListeners(const _AppManagerEventArg& arg)
198 {
199         _AppManagerServiceEventListenerEnum* pEnum = __eventListeners.GetMapEnumeratorN();
200
201         result r = GetLastResult();
202         SysTryReturn(NID_APP, pEnum != null, r, r, "[%s] Propagated.", GetErrorMessage(r));
203
204         while (pEnum->MoveNext() == E_SUCCESS)
205         {
206                 int clientId = -1;
207                 r = pEnum->GetKey(clientId);
208                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
209
210                 _IAppManagerServiceEventListener* pListener = (clientId == -1) ? __pProxy : __pStub;
211
212                 if ( pListener != null )
213                 {
214                         SysLog(NID_APP, "Trying to SendResponse(%d)", clientId);
215                         r = pListener->OnServiceEventReceived(clientId, arg);
216                         SysTryLog(NID_APP, !IsFailed(r), "[%s] OnServiceEventReceived fails.", GetErrorMessage(r));
217 //                      SysTryCatch(NID_APP, !IsFailed(r),, r, "[%s] Propagated.", GetErrorMessage(r));
218                 }
219         }
220
221         delete pEnum;
222         return E_SUCCESS;
223
224 CATCH:
225         delete pEnum;
226         return r;
227 }
228
229 void
230 _AppManagerService::Dump(void)
231 {
232         __pContextMgr->Dump();
233 }
234
235
236 }}//namespace Tizen { namespace App {