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