Merge "Update deprecated libprivilege-control API functions." into tizen
[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         if (IsFailed(r))
80         {
81                 SysPropagate(NID_APP, r);
82                 goto CATCH;
83         }
84
85         return r;
86
87 CATCH:
88         delete __pAppManagerImpl;
89         __pAppManagerImpl = null;
90
91         return r;
92 }
93
94
95 AppManager::~AppManager(void)
96 {
97         delete __pAppManagerImpl;
98 }
99
100
101 AppControl*
102 AppManager::FindAppControlN(const AppId& appId, const String& operationId)
103 {
104         return _AppManagerImpl::FindAppControlN(appId, operationId);
105 }
106
107
108 IList*
109 AppManager::FindAppControlsN(const String* pOperationId, const String* pCategory, const String* pDataType, const String* pUriScheme)
110 {
111         return _AppManagerImpl::FindAppControlsN(pOperationId, pCategory, pDataType, pUriScheme);
112 }
113
114
115 result
116 AppManager::StartAppControl(const String& uri, const String* pOperationId,
117                                                         const String* pDataType,
118                                                         IAppControlListener* pListener)
119 {
120         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
121         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
122
123         return _AppManagerImpl::StartAppControl(uri, pOperationId, pDataType, pListener);
124 }
125
126
127 result
128 AppManager::StartAppControl(const String* pOperationId, const String* pCategory,
129                                                         const String* pDataType, const String* pUriScheme,
130                                                         const IList* pDataList,
131                                                         IAppControlListener* pListener)
132 {
133         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
134         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
135
136         return _AppManagerImpl::StartAppControl(pOperationId, pCategory, pDataType, pUriScheme, pDataList, pListener);
137 }
138
139 SqlDataControl*
140 AppManager::GetSqlDataControlN(const String& providerId)
141 {
142         return _AppManagerImpl::GetSqlDataControlN(providerId);
143 }
144
145 MapDataControl*
146 AppManager::GetMapDataControlN(const String& providerId)
147 {
148         return _AppManagerImpl::GetMapDataControlN(providerId);
149 }
150
151 String
152 AppManager::GetAppSharedPath(const AppId& appId)
153 {
154         String sharedPath;
155
156         result r = _AppManagerImpl::GetAppRootPath(appId, sharedPath);
157         SysTryReturn(NID_APP, !IsFailed(r), sharedPath, r, "[%s] Propagating to caller...", GetErrorMessage(r));
158
159         sharedPath.Append(L"shared/");
160
161         SetLastResult(E_SUCCESS);
162         return sharedPath;
163 }
164
165 AppManager*
166 AppManager::GetInstance(void)
167 {
168         result r = E_SUCCESS;
169         static AppManager* pAppMgr = null;
170
171         if (pAppMgr == null)
172         {
173                 pAppMgr = new (std::nothrow) AppManager();
174                 SysTryReturn(NID_APP, pAppMgr != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppManager allocation failed.");
175
176                 r = pAppMgr->Construct();
177                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] AppManager construction failed.", GetErrorMessage(r));
178
179 #ifdef _SINGLETON_CLEANUP
180                 // [FIXME] temporary disable to resolve N_SE-23319, ImeApp cleanup issue
181                 //_LongevityManager::GetInstance().RegisterOwnership(*pAppMgr);
182 #endif
183         }
184
185         return pAppMgr;
186
187 CATCH:
188         delete pAppMgr;
189         pAppMgr = null;
190
191         return null;
192 }
193
194 result
195 AppManager::LaunchApplication(const AppId& appId, const IList* pArguments, LaunchOption option)
196 {
197         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
198         SysTryReturnResult(NID_APP, appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
199                                           "The length of appid exceeded the limit(%d).", WIDGET_APP_MAX_APPID_LENGTH);
200
201         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
202         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
203
204         return __pAppManagerImpl->LaunchApplication(appId, pArguments, LAUNCH_OPTION_DEFAULT);
205 }
206
207 result
208 AppManager::LaunchApplication(const AppId& appId, LaunchOption option)
209 {
210         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
211         SysTryReturnResult(NID_APP, appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
212                                           "The length of appid exceeded the limit(%d).", WIDGET_APP_MAX_APPID_LENGTH);
213
214         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
215         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
216
217         return __pAppManagerImpl->LaunchApplication(appId, LAUNCH_OPTION_DEFAULT);
218 }
219
220 result
221 AppManager::TerminateApplication(const AppId& appId)
222 {
223         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
224         SysTryReturnResult(NID_APP, appId.GetLength() <= WIDGET_APP_MAX_APPID_LENGTH, E_MAX_EXCEEDED,
225                                                   "The length of appid exceeded the limit(%d).", WIDGET_APP_MAX_APPID_LENGTH);
226
227         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_KILL, _PRV_APPMANAGER_KILL);
228         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
229
230         return __pAppManagerImpl->TerminateApplication(appId);
231 }
232
233 bool
234 AppManager::IsRunning(const AppId& appId) const
235 {
236         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
237
238         return __pAppManagerImpl->IsRunning(appId);
239 }
240
241 IList*
242 AppManager::GetRunningAppListN(void) const
243 {
244         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
245
246         return __pAppManagerImpl->GetRunningAppListN();
247 }
248
249 result
250 AppManager::RegisterAppLaunch(const String& condition, const IList* pArguments, LaunchOption option)
251 {
252         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
253
254         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
255         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
256
257         return __pAppManagerImpl->RegisterAppLaunch(L"", condition, pArguments, option);
258 }
259
260 result
261 AppManager::UnregisterAppLaunch(void)
262 {
263         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
264
265         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
266         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
267
268         return __pAppManagerImpl->UnregisterAppLaunch(L"", null);
269 }
270
271 result
272 AppManager::UnregisterAppLaunch(const String& condition)
273 {
274         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
275
276         result r = _AccessController::CheckUserPrivilege(_PRV_APPLICATION_LAUNCH);
277         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
278
279         return __pAppManagerImpl->UnregisterAppLaunch(L"", &condition);
280 }
281
282 bool
283 AppManager::IsAppLaunchRegistered(void) const
284 {
285         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
286
287         return __pAppManagerImpl->IsAppLaunchRegistered(L"", null);
288 }
289
290 result
291 AppManager::RegisterAppLaunch(const AppId& appId, const String& condition, const IList* pArguments, LaunchOption option)
292 {
293         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
294         SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
295
296         return __pAppManagerImpl->RegisterAppLaunch(appId, condition, pArguments, option);
297 }
298
299
300 result
301 AppManager::UnregisterAppLaunch(const AppId& appId, const String* pCondition)
302 {
303         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
304         SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
305
306         return __pAppManagerImpl->UnregisterAppLaunch(appId, pCondition);
307 }
308
309
310 bool
311 AppManager::IsAppLaunchRegistered(const AppId& appId, const String* pCondition) const
312 {
313         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
314
315         return __pAppManagerImpl->IsAppLaunchRegistered(appId, pCondition);
316 }
317
318 result
319 AppManager::SetCheckpointEventListener(IAppCheckpointEventListener& listener)
320 {
321         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
322
323         Tizen::Base::Runtime::IEventListener* pListener = &listener;
324         return __pAppManagerImpl->SetEventListener(_APP_EVENT_CHECKPOINT, pListener);
325 }
326
327 void
328 AppManager::SetAppLaunchConditionEventListener(IAppLaunchConditionEventListener* pListener)
329 {
330         SysLog(NID_APP, "");
331         _AppImpl::GetInstance()->SetAppLaunchConditionEventListener(pListener);
332 }
333
334
335 result
336 AppManager::SendAppControlResult(const String& appControlRequestId, const IList* pResultList)
337 {
338         int req = _AppArg::GetRequestId(appControlRequestId);
339         AppControlProviderManager* pInstance = AppControlProviderManager::GetInstance();
340         return _AppControlProviderManagerImpl::GetInstance(*pInstance)->SendAppControlResult(req, pResultList);
341 }
342
343 result
344 AppManager::AddActiveAppEventListener(IActiveAppEventListener& listener)
345 {
346         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
347
348         result r = _AccessController::CheckUserPrivilege(_PRV_APPUSAGE);
349         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
350
351         return __pAppManagerImpl->AddActiveAppEventListener(listener);
352 }
353
354 result
355 AppManager::RemoveActiveAppEventListener(IActiveAppEventListener& listener)
356 {
357         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
358
359         result r = _AccessController::CheckUserPrivilege(_PRV_APPUSAGE);
360         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
361
362         return __pAppManagerImpl->RemoveActiveAppEventListener(listener);
363 }
364
365 result
366 AppManager::GetActiveApp(AppId& appId)
367 {
368         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
369
370         result r = _AccessController::CheckUserPrivilege(_PRV_APPUSAGE);
371         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
372
373         return __pAppManagerImpl->GetActiveApp(appId);
374 }
375
376 bool 
377 AppManager::IsUserPreferredAppForAppControlResolution(const AppId& appId) const
378 {
379         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
380         SysTryReturn(NID_APP, _Aul::IsInstalled(appId) == true, false, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
381
382         return __pAppManagerImpl->IsUserPreferredAppForAppControlResolution(appId);
383 }
384
385 result 
386 AppManager::ClearUserPreferenceForAppControlResolution(const AppId& appId)
387 {
388         SysAssertf(__pAppManagerImpl != null, "Not constructed properly by platform.");
389         SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_APP_NOT_INSTALLED, "The application(%ls) is not installed.", appId.GetPointer());
390
391         return __pAppManagerImpl->ClearUserPreferenceForAppControlResolution(appId);
392 }
393
394 }} // Tizen::App