Add event for update application
[platform/framework/native/appfw.git] / src / app / FAppAppManager.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        FAppAppManager.cpp
19  * @brief       This is the implementation for the AppManager class.
20  */
21
22 #include <new>
23
24 #include <FBaseString.h>
25 #include <FBaseColIListT.h>
26 #include <FAppAppManager.h>
27 #include <FAppIAppControlListener.h>
28 #include <FAppSqlDataControl.h>
29 #include <FAppMapDataControl.h>
30 #include <FAppIAppCheckPointEventListener.h>
31 #include <FAppAppControlProviderManager.h>
32 #include <FAppIActiveAppEventListener.h>
33 #include <FBaseSysLog.h>
34
35 #include <FSec_AccessController.h>
36 #include "FApp_Types.h"
37 #include "FAppPkg_PackageManagerImpl.h"
38 #include "FApp_AppManagerImpl.h"
39 #include "FApp_AppImpl.h"
40 #include "FApp_AppInfo.h"
41 #include "FApp_AppArg.h"
42 #include "FApp_Aul.h"
43 #include "FApp_AppControlProviderManagerImpl.h"
44 #ifdef _SINGLETON_CLEANUP
45 #include "FApp_LongevityManager.h"
46 #endif
47
48 using namespace Tizen::App::Package;
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::Base::Runtime;
52 using namespace Tizen::Io;
53 using namespace Tizen::Security;
54
55 namespace Tizen { namespace App
56 {
57
58 const long MAX_ARGUMENTS_SIZE = 1024;
59
60
61 AppManager::AppManager(void)
62 : __pAppManagerImpl(null)
63 {
64 }
65
66
67 result
68 AppManager::Construct(void)
69 {
70         result r = E_SUCCESS;
71
72         SysAssertf(__pAppManagerImpl == null,
73                         "Already constructed. Calling Construct() twice or more on a same in stance is not allowed for this class.");
74
75         __pAppManagerImpl = new (std::nothrow) _AppManagerImpl();
76         SysTryReturnResult(NID_APP, __pAppManagerImpl != null, E_OUT_OF_MEMORY, "");
77
78         r = __pAppManagerImpl->Construct();
79         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
80
81         return r;
82
83 CATCH:
84         delete __pAppManagerImpl;
85         __pAppManagerImpl = null;
86
87         return r;
88 }
89
90
91 AppManager::~AppManager(void)
92 {
93         delete __pAppManagerImpl;
94 }
95
96
97 AppControl*
98 AppManager::FindAppControlN(const AppId& appId, const String& operationId)
99 {
100         return _AppManagerImpl::FindAppControlN(appId, operationId);
101 }
102
103
104 IList*
105 AppManager::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType, const String* pUriScheme)
106 {
107         return _AppManagerImpl::FindAppControlsN(pOperationId, pCategory, pDataType, pUriScheme);
108 }
109
110
111 result
112 AppManager::StartAppControl(const String& uri, const String* pOperationId,
113                                                         const String* pDataType,
114                                                         IAppControlListener* pListener)
115 {
116         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
117         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
118
119         return _AppManagerImpl::StartAppControl(uri, pOperationId, pDataType, pListener);
120 }
121
122
123 result
124 AppManager::StartAppControl(const String* pOperationId, const String* pCategory,
125                                                         const String* pDataType, const String* pUriScheme,
126                                                         const IList* pDataList,
127                                                         IAppControlListener* pListener)
128 {
129         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
130         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
131
132         return _AppManagerImpl::StartAppControl(pOperationId, pCategory, pDataType, pUriScheme, pDataList, pListener);
133 }
134
135 SqlDataControl*
136 AppManager::GetSqlDataControlN(const String& providerId)
137 {
138         return _AppManagerImpl::GetSqlDataControlN(providerId);
139 }
140
141 MapDataControl*
142 AppManager::GetMapDataControlN(const String& providerId)
143 {
144         return _AppManagerImpl::GetMapDataControlN(providerId);
145 }
146
147 String
148 AppManager::GetAppSharedPath(const AppId& appId)
149 {
150         String sharedPath;
151
152         result r = _AppManagerImpl::GetAppRootPath(appId, sharedPath);
153         SysTryReturn(NID_APP, !IsFailed(r), sharedPath, r, "[%s] Propagating to caller...",
154                         GetErrorMessage(r));
155
156         sharedPath.Append(L"shared/");
157
158         SetLastResult(E_SUCCESS);
159         return sharedPath;
160 }
161
162 AppManager*
163 AppManager::GetInstance(void)
164 {
165         result r = E_SUCCESS;
166         static AppManager* pAppMgr = null;
167
168         if (pAppMgr == null)
169         {
170                 pAppMgr = new (std::nothrow) AppManager();
171                 SysTryReturn(NID_APP, pAppMgr != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppManager allocation failed.");
172
173                 r = pAppMgr->Construct();
174                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] AppManager construction failed.", GetErrorMessage(r));
175
176 #ifdef _SINGLETON_CLEANUP
177                 // [FIXME] temporary disable to resolve N_SE-23319, ImeApp cleanup issue
178                 //_LongevityManager::GetInstance().RegisterOwnership(*pAppMgr);
179 #endif
180         }
181
182         return pAppMgr;
183
184 CATCH:
185         delete pAppMgr;
186         pAppMgr = null;
187
188         return null;
189 }
190
191 result
192 AppManager::LaunchApplication(const AppId& appId, const IList* pArguments, LaunchOption option)
193 {
194         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
195         SysTryReturnResult(NID_APP, appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
196                                           "The length of appid exceeded the limit(%d).", WIDGET_APP_MAX_APPID_LENGTH);
197
198         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
199         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
200
201         return __pAppManagerImpl->LaunchApplication(appId, pArguments, LAUNCH_OPTION_DEFAULT);
202 }
203
204 result
205 AppManager::LaunchApplication(const AppId& appId, LaunchOption option)
206 {
207         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
208         SysTryReturnResult(NID_APP, appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
209                                           "The length of appid exceeded the limit(%d).", WIDGET_APP_MAX_APPID_LENGTH);
210
211         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
212         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
213
214         return __pAppManagerImpl->LaunchApplication(appId, LAUNCH_OPTION_DEFAULT);
215 }
216
217 result
218 AppManager::TerminateApplication(const AppId& appId)
219 {
220         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
221         SysTryReturnResult(NID_APP, appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
222                                                   "The length of appid exceeded the limit(%d).", WIDGET_APP_MAX_APPID_LENGTH);
223
224         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_KILL, _PRV_APPMANAGER_KILL);
225         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
226
227         return __pAppManagerImpl->TerminateApplication(appId);
228 }
229
230 bool
231 AppManager::IsRunning(const AppId& appId) const
232 {
233         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
234
235         return __pAppManagerImpl->IsRunning(appId);
236 }
237
238 IList*
239 AppManager::GetRunningAppListN(void) const
240 {
241         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
242
243         return __pAppManagerImpl->GetRunningAppListN();
244 }
245
246 result
247 AppManager::RegisterAppLaunch(const String& condition, const IList* pArguments, LaunchOption option)
248 {
249         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
250
251         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
252         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
253
254         return __pAppManagerImpl->RegisterAppLaunch(L"", condition, pArguments, option);
255 }
256
257 result
258 AppManager::UnregisterAppLaunch(void)
259 {
260         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
261
262         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
263         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
264
265         return __pAppManagerImpl->UnregisterAppLaunch(L"", null);
266 }
267
268 result
269 AppManager::UnregisterAppLaunch(const String& condition)
270 {
271         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
272
273         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
274         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
275
276         return __pAppManagerImpl->UnregisterAppLaunch(L"", &condition);
277 }
278
279 bool
280 AppManager::IsAppLaunchRegistered(void) const
281 {
282         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
283
284         return __pAppManagerImpl->IsAppLaunchRegistered(L"", null);
285 }
286
287 result
288 AppManager::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments, LaunchOption option)
289 {
290         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
291         SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
292
293         return __pAppManagerImpl->RegisterAppLaunch(appId, condition, pArguments, option);
294 }
295
296
297 result
298 AppManager::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
299 {
300         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
301         SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
302
303         return __pAppManagerImpl->UnregisterAppLaunch(appId, pCondition);
304 }
305
306
307 bool
308 AppManager::IsAppLaunchRegistered(const AppId& appId, const String* pCondition) const
309 {
310         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
311
312         return __pAppManagerImpl->IsAppLaunchRegistered(appId, pCondition);
313 }
314
315 result
316 AppManager::SetCheckpointEventListener(IAppCheckpointEventListener& listener)
317 {
318         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
319
320         Tizen::Base::Runtime::IEventListener* pListener = &listener;
321         return __pAppManagerImpl->SetEventListener(_APP_EVENT_CHECKPOINT, pListener);
322 }
323
324 void
325 AppManager::SetAppLaunchConditionEventListener(IAppLaunchConditionEventListener* pListener)
326 {
327         SysLog(NID_APP, "");
328         _AppImpl::GetInstance()->SetAppLaunchConditionEventListener(pListener);
329 }
330
331
332 result
333 AppManager::SendAppControlResult(const String& appControlRequestId, const IList* pResultList)
334 {
335         int req = _AppArg::GetRequestId(appControlRequestId);
336         AppControlProviderManager* pInstance = AppControlProviderManager::GetInstance();
337         return _AppControlProviderManagerImpl::GetInstance(*pInstance)->SendAppControlResult(req, pResultList);
338 }
339
340 result
341 AppManager::AddActiveAppEventListener(IActiveAppEventListener& listener)
342 {
343         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
344
345         result r = _AccessController::CheckUserPrivilege(_PRV_APPUSAGE);
346         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
347
348         return __pAppManagerImpl->AddActiveAppEventListener(listener);
349 }
350
351 result
352 AppManager::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
353 {
354         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
355
356         result r = _AccessController::CheckUserPrivilege(_PRV_APPUSAGE);
357         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
358
359         return __pAppManagerImpl->RemoveActiveAppEventListener(listener);
360 }
361
362 result
363 AppManager::GetActiveApp(AppId& appId)
364 {
365         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
366
367         result r = _AccessController::CheckUserPrivilege(_PRV_APPUSAGE);
368         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
369
370         return __pAppManagerImpl->GetActiveApp(appId);
371 }
372
373 bool 
374 AppManager::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
375 {
376         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
377         SysTryReturn(NID_APP, _Aul::IsInstalled(appId) == true, false, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
378
379         return __pAppManagerImpl->IsUserPreferredAppForAppControlResolution(appId);
380 }
381
382 result 
383 AppManager::ClearUserPreferenceForAppControlResolution(const AppId& appId)
384 {
385         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
386         SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
387
388         return __pAppManagerImpl->ClearUserPreferenceForAppControlResolution(appId);
389 }
390
391 }} // Tizen::App