2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FApp_AppControlImpl.cpp
19 * @brief This is the implementation for the Application Control class.
24 #include <unique_ptr.h>
26 #include <appsvc/appsvc.h>
28 #include <FBaseColHashMap.h>
29 #include <FBaseSysLog.h>
30 #include <FAppAppControl.h>
31 #include <FAppAppManager.h>
32 #include <FAppPkgPackageAppInfo.h>
33 #include <FAppIAppControlEventListener.h>
34 #include <FAppIAppControlResponseListener.h>
36 #include "FApp_AppControlImpl.h"
37 #include "FApp_AppControlManager.h"
38 #include "FApp_IAppControlPluginProvider.h"
39 #include "FApp_AppArg.h"
40 #include "FApp_AppControlRegistry.h"
41 #include "FApp_AppMessageImpl.h"
42 #include "FApp_AppInfo.h"
43 #include "FAppPkg_PackageManagerImpl.h"
45 #include "FApp_AppControlEventArg.h"
46 #include "FApp_AppControlResponseEvent.h"
47 #include "FBaseRt_ThreadImpl.h"
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::Base::Runtime;
52 using namespace Tizen::App;
53 using namespace Tizen::App::Package;
54 using namespace Tizen::Io;
59 static const int _REQ_ID_INVALID = -1;
60 const wchar_t ACTL_IMPLICIT_PLUGIN[] = L"libosp-ac-implicit.so";
64 namespace Tizen { namespace App
67 _AppControlImpl::_AppControlImpl(const AppControl& value)
69 , _reqId(_REQ_ID_INVALID)
70 , _property(_APPCONTROL_PROPERTY_NONE)
71 , _processId(_REQ_ID_INVALID)
73 __appControlResponseEventList.Construct();
76 _AppControlImpl::~_AppControlImpl(void)
78 IEnumeratorT<int>* pEnum = __appControlResponseEventList.GetEnumeratorN();
79 IMapT<int, _AppControlResponseEvent*>* pResponseEventContainer = null;
82 pResponseEventContainer = _AppControlManager::GetInstance()->GetAppControlResponseEventContainer();
85 while(pEnum->MoveNext() == E_SUCCESS)
88 pEnum->GetCurrent(reqId);
89 if (pResponseEventContainer != null)
91 _AppControlResponseEvent* pResponseEvent = null;
92 pResponseEventContainer->GetValue(reqId, pResponseEvent);
93 delete pResponseEvent;
95 pResponseEventContainer->Remove(reqId);
96 SysLog(NID_APP, "pResponseEvent gets deleted. reqId(%d)", reqId);
103 _AppControlImpl::CreateN(const String& path, const String& aId, const String& oId, int prop)
105 SysTryReturn(NID_APP, !path.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] Path is empty.");
106 SysTryReturn(NID_APP, !aId.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] Provider Id is empty.");
107 SysTryReturn(NID_APP, !oId.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] Operation Id is empty.");
109 AppControl* pAc = new (std::nothrow) AppControl;
110 SysTryReturn(NID_APP, pAc != null, null, E_OUT_OF_MEMORY, "AppControl allocation failure.");
112 _AppControlImpl* pImpl = pAc->__pAppControlImpl;
113 SysTryReturn(NID_APP, pImpl != null, null, E_OUT_OF_MEMORY, "AppControlImpl instance must not be null.");
118 pImpl->_property = prop;
124 const _AppControlImpl*
125 _AppControlImpl::GetInstance(const AppControl& ac)
127 return ac.__pAppControlImpl;
131 _AppControlImpl::GetInstance(AppControl& ac)
133 return ac.__pAppControlImpl;
136 _IAppControlPluginProvider*
137 _AppControlImpl::GetAppControlPluginProvider(const String& path)
140 lib.Construct(path, _LIBRARY_OPTION);
142 APP_CONTROL_PROVIDER_GET_FN pProvider = reinterpret_cast<APP_CONTROL_PROVIDER_GET_FN>(lib.GetProcAddress(L"GetAppControlProviderPlugin"));
146 SysLogException(NID_APP, E_SYSTEM, "Cannot load plugin properly for %ls.", path.GetPointer());
150 return (*pProvider)();
155 _AppControlImpl::FindAndStart(const String& operationId, const String* pUriPattern, const String* pDataType, const String* pCategory, const IMap* pExtraData, IAppControlResponseListener* pListener)
157 // [FIXME] valid argument size checking required
158 SysLog(NID_APP, "Enter");
160 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
161 SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
163 _AppMessageImpl::SetOperation(pBundle.get(), operationId);
167 _AppMessageImpl::SetUri(pBundle.get(), *pUriPattern);
172 String mimeType = *pDataType;
174 if ((*pDataType)[0] == L'.')
176 SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
180 pDataType->SubString(1, ext);
182 result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
184 SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
186 pMimeType = &mimeType;
188 SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
192 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
197 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
200 return StartImplicit(pBundle.get(), pExtraData, pListener);
205 _AppControlImpl::StartImplicit(const _AppMessageImpl& msg, IEventListener* pListener, bool isLegacy)
207 SysLog(NID_APP, "Enter");
208 int req = _REQ_ID_INVALID;
210 _IAppControlPluginProvider* pProvider = GetAppControlPluginProvider(ACTL_IMPLICIT_PLUGIN);
211 SysTryReturnResult(NID_APP, pProvider != null, E_OBJ_NOT_FOUND, "Propagating.");
215 _InProcessInfo* pItem = new (std::nothrow) _InProcessInfo(isLegacy, pProvider, pListener);
218 req = _AppControlManager::GetInstance()->__inAppManager.InsertItem(pItem);
222 result r = InvokeStartAppControl(pProvider, req, L"", msg);
224 if (pListener == null)
226 pProvider->Release();
229 // after acquring request number, pLib should be managed from the list, not CATCH
232 _AppControlManager::GetInstance()->__inAppManager.RemoveItem(req);
233 SysLog(NID_APP, "[%s] A system error has occurred.", GetErrorMessage(r));
238 SysLog(NID_APP, "Exit %d", req);
244 _AppControlImpl::StartImplicit(bundle* pBundle, const IList* pDataList, IAppControlEventListener* pListener)
246 _AppMessageImpl msg(pBundle);
247 msg.AddData(pDataList);
249 return StartImplicit(msg, pListener, true);
254 _AppControlImpl::StartImplicit(bundle* pBundle, const IMap* pData, IAppControlResponseListener* pListener)
256 _AppMessageImpl msg(pBundle);
257 _AppArg::AddStrMap(msg.GetBundle(), pData);
259 return StartImplicit(msg, pListener, false);
264 _AppControlImpl::Start(const IList* pDataList, IAppControlEventListener* pListener)
266 SysLog(NID_APP, "Enter");
267 _InProcessInfo* pInfo = _AppControlManager::GetInstance()->__inAppManager.FindItem(_reqId);
268 SysTryReturnResult(NID_APP, pInfo == null, E_IN_PROGRESS, "Request ID %d is already in progress.", _reqId);
270 int req = _REQ_ID_INVALID;
272 _IAppControlPluginProvider* pProvider = GetAppControlPluginProvider(_path);
273 SysTryReturnResult(NID_APP, pProvider != null, E_OBJ_NOT_FOUND, "Propagating.");
277 _InProcessInfo* pItem = new (std::nothrow) _InProcessInfo(_appId, _opId, _property, true, pProvider, pListener);
280 req = _AppControlManager::GetInstance()->__inAppManager.InsertItem(pItem);
284 result r = InvokeStartAppControl(pProvider, req, _appId, _opId, pDataList);
286 if (pListener == null)
288 pProvider->Release();
291 // after acquring request number, pLib should be managed from the list, not CATCH
294 _AppControlManager::GetInstance()->__inAppManager.RemoveItem(req);
295 SysLog(NID_APP, "[%s] A system error has occurred.", GetErrorMessage(r));
302 SysLog(NID_APP, "Exit %d", req);
307 _AppControlImpl::Start(const String* pUriData, const String* pMimeType, const IMap* pDataList, IAppControlResponseListener* pListener)
309 SysLog(NID_APP, "Enter");
310 _InProcessInfo* pInfo = _AppControlManager::GetInstance()->__inAppManager.FindItem(_reqId);
311 SysTryReturnResult(NID_APP, pInfo == null, E_IN_PROGRESS, "Request ID %d is already in progress.", _reqId);
313 int req = _REQ_ID_INVALID;
314 result r = E_SUCCESS;
316 _IAppControlPluginProvider* pProvider = GetAppControlPluginProvider(_path);
317 SysTryReturnResult(NID_APP, pProvider != null, E_OBJ_NOT_FOUND, "Propagating.");
321 _InProcessInfo* pItem = new (std::nothrow) _InProcessInfo(_appId, _opId, _property, false, pProvider, pListener);
324 req = _AppControlManager::GetInstance()->__inAppManager.InsertItem(pItem);
327 if (_ThreadImpl::GetCurrentThreadImpl()->GetThreadType() == THREAD_TYPE_EVENT_DRIVEN)
329 _AppControlResponseEvent* pAppControlResponseEvent = new (std::nothrow) _AppControlResponseEvent();
331 if (pAppControlResponseEvent != null)
333 r = pAppControlResponseEvent->Construct();
334 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::Construct() is failed", GetErrorMessage(r));
336 r = pAppControlResponseEvent->AddListener(*this, true);
337 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::AddListener() is failed", GetErrorMessage(r));
339 IMapT<int, _AppControlResponseEvent*>* pResponseEventContainer = _AppControlManager::GetInstance()->GetAppControlResponseEventContainer();
340 if (pResponseEventContainer != null)
342 int responseEventRequestId = RESPONSE_EVENT_REQID_MAGIC + req;
343 pResponseEventContainer->Add(responseEventRequestId, pAppControlResponseEvent);
344 __appControlResponseEventList.Add(responseEventRequestId);
345 SysLog(NID_APP, "pResponseEvent gets added. reqId(%d)", responseEventRequestId);
350 r = InvokeStartAppControl(pProvider, req, _appId, _opId, pUriData, pMimeType, pDataList);
352 if (pListener == null)
354 pProvider->Release();
357 // after acquring request number, pLib should be managed from the list, not CATCH
360 _AppControlManager::GetInstance()->__inAppManager.RemoveItem(req);
361 SysLog(NID_APP, "[%s] A system error has occurred.", GetErrorMessage(r));
367 SysLog(NID_APP, "Exit %d", req);
373 _AppControlImpl::InvokeStartAppControl(_IAppControlPluginProvider* pProvider, int req, const String& appId, const String& oId, const IList* pList)
375 SysLog(NID_APP, "Legacy stuff for converting argument");
377 HashMap map(SingleObjectDeleter);
378 HashMap* pMap = null;
383 _AppArg::FillMapFromList(&map, pList);
388 return InvokeStartAppControl(pProvider, req, appId, oId, null, null, pMap);
393 _AppControlImpl::InvokeStartAppControl(_IAppControlPluginProvider* pProvider, int req, const String& appId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
395 _AppMessageImpl msg(appId, oId, pUri, pMime, pMap);
397 return InvokeStartAppControl(pProvider, req, appId, msg);
402 _AppControlImpl::InvokeStartAppControl(_IAppControlPluginProvider* pProvider, int req, const String& appId, const _AppMessageImpl& message)
404 SysTryReturnResult(NID_APP, pProvider != null, E_SYSTEM, "Wrong AppControl provider plugin for %ls(%d).", appId.GetPointer(), req);
406 return pProvider->StartAppControlPlugin(req, appId, message, null);
411 IsValidAppControl(const String& appcontrolID)
413 return ((appcontrolID == L"osp.appcontrol.provider.audio")
414 || (appcontrolID == L"osp.appcontrol.provider.bluetooth")
415 || (appcontrolID == L"osp.appcontrol.provider.calendar")
416 || (appcontrolID == L"osp.appcontrol.provider.camera")
417 || (appcontrolID == L"osp.appcontrol.provider.contact")
418 || (appcontrolID == L"osp.appcontrol.provider.certificatemanager")
419 || (appcontrolID == L"osp.appcontrol.provider.email")
420 || (appcontrolID == L"osp.appcontrol.provider.image")
421 || (appcontrolID == L"osp.appcontrol.provider.media")
422 || (appcontrolID == L"osp.appcontrol.provider.message")
423 || (appcontrolID == L"osp.appcontrol.provider.video")
424 || (appcontrolID == L"osp.appcontrol.provider.imageeditor")
425 || (appcontrolID == L"osp.appcontrol.provider.allshare")
426 || (appcontrolID == L"tizen.filemanager")
427 || (appcontrolID == L"tizen.camera")
428 || (appcontrolID == L"tizen.gallery")
429 || (appcontrolID == L"tizen.imageviewer")
430 || (appcontrolID == L"tizen.videoplayer")
431 || (appcontrolID == L"tizen.memo")
432 || (appcontrolID == L"tizen.contacts")
433 || (appcontrolID == L"tizen.calendar")
434 || (appcontrolID == L"tizen.todo")
435 || (appcontrolID == L"tizen.email")
436 || (appcontrolID == L"tizen.settings")
437 || (appcontrolID == L"tizen.messages")
438 || (appcontrolID == L"tizen.musicplayer")
439 || (appcontrolID == L"tizen.bluetooth")
440 || (appcontrolID == L"samsung.snote")
441 || (appcontrolID == L"0pnxz8hbsr.MyFiles")
442 || (appcontrolID == L"hdufar9ycj.Camera")
443 || (appcontrolID == L"ijudt7w61q.Gallery")
444 || (appcontrolID == L"jysyv9o1dc.ImageViewer")
445 || (appcontrolID == L"npwf0scb88.VideoPlayer")
446 || (appcontrolID == L"zunqjlsnce.Memo")
447 || (appcontrolID == L"f9uev8hsyo.Contacts")
448 || (appcontrolID == L"ph1vq2phrp.Calendar")
449 || (appcontrolID == L"vxqbrefica.Email")
450 || (appcontrolID == L"kto5jikgul.Settings")
451 || (appcontrolID == L"8r4r5ddzzn.Messages")
452 || (appcontrolID == L"dhrul6qzj3.MusicPlayer")
453 || (appcontrolID == L"smemo-efl"));
457 _AppControlImpl::Stop(void)
459 const String appcontrolID(GetAppControlProviderId());
460 SysTryReturnResult(NID_APP, IsValidAppControl(appcontrolID), E_INVALID_OPERATION, "Invalid appcontrolID(%ls)", appcontrolID.GetPointer());
462 result (*pStop)(int req) = null;
464 if (_reqId != _REQ_ID_INVALID)
466 _InProcessInfo* pInfo = _AppControlManager::GetInstance()->__inAppManager.FindItem(_reqId);
467 SysTryReturnResult(NID_APP, pInfo != null, E_INVALID_OPERATION, "Request ID %d is not found.", _reqId);
469 if (pInfo->pProvider)
471 pInfo->pProvider->StopAppControlPlugin(_reqId);
474 _AppControlManager::GetInstance()->__inAppManager.RemoveItem(_reqId);
476 _reqId = _REQ_ID_INVALID;
480 _IAppControlPluginProvider* pProvider = GetAppControlPluginProvider(_path);
483 pProvider->StopAppControlPlugin(-1);
484 SysLog(NID_APP, "Request is stopped.");
486 pProvider->Release();
494 _AppControlImpl::GetAppName(void)
496 if (_appName.IsEmpty())
498 AppId appId = GetAppId();
499 AppId aliasAppId = _AppControlRegistry::GetInstance()->GetAliasAppId(appId);
500 if (!aliasAppId.IsEmpty())
505 std::unique_ptr<PackageAppInfo> pInfo(_PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId));
508 SysLog(NID_APP, "PackageInfo of appId(%ls) exists", appId.GetPointer());
509 const String& name = pInfo->GetAppName();
510 if (name == L"_AppControl")
512 // workaround for special case: requery with actual appId
513 const PackageId& packageId = _PackageManagerImpl::GetPackageIdByAppId(appId);
514 const String& defaultName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(packageId);
516 const String& convertedAppId = packageId + L'.' + defaultName;
518 std::unique_ptr<PackageAppInfo> pNewInfo(_PackageManagerImpl::GetInstance()->GetPackageAppInfoN(convertedAppId));
522 _appName = pNewInfo->GetAppDisplayName();
526 SysLog(NID_APP, "No default applicaiton information, possible database error.");
531 _appName = pInfo->GetAppDisplayName();
536 SysLog(NID_APP, "PackageInfo of appId(%ls) does not exist", appId.GetPointer());
544 _AppControlImpl::GetAppId(void) const
550 _AppControlImpl::GetAppControlProviderId(void) const
556 _AppControlImpl::GetOperationId(void) const
562 _AppControlImpl::GetCategoryListN(void) const
564 AppId appId = GetAppId();
565 SysTryReturn(NID_APP, !appId.IsEmpty(), null, E_SYSTEM, "[E_SYSTEM] Empty appId.");
567 AppId aliasAppId = _AppControlRegistry::GetInstance()->GetAliasAppId(appId);
568 if (!aliasAppId.IsEmpty())
573 SysLog(NID_APP, "Acquiring category for app %ls.", appId.GetPointer());
575 std::unique_ptr<PackageAppInfo> pAppInfo(_PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId));
576 SysTryReturn(NID_APP, pAppInfo.get() != null, null, E_SYSTEM, "[E_SYSTEM] Getting PackageAppInfo failed.");
578 return pAppInfo->GetAppCategoryListN();
582 _AppControlImpl::StopAppControlResponseListener(IAppControlResponseListener* pListener)
584 _AppControlManager::GetInstance()->StopAppControlResponseListener(pListener);
588 _AppControlImpl::OnAppControlResponseEventReceivedN(const Tizen::Base::Runtime::IEventArg* arg)
590 const _AppControlResponseEventArg* pEventArg = dynamic_cast<const _AppControlResponseEventArg*>(arg);
592 if (pEventArg != null)
594 IAppControlResponseListener* pResponseListener = pEventArg->GetListener();
596 if(pResponseListener != null)
598 if(pEventArg->GetType() == _APPCONTROL_RESPONSETYPE_COMPLETE)
600 _AppControlManager::InvokeAppControlCompleteListener(*pResponseListener, pEventArg->GetAppId(), pEventArg->GetOperationId(), pEventArg->GetAppControlResult(), pEventArg->GetExtraData(), pEventArg->IsSubMode());
602 _AppControlResponseEvent* pResponseEvent = null;
603 _AppControlManager::GetInstance()->GetAppControlResponseEventContainer()->GetValue(pEventArg->GetRequestId(), pResponseEvent);
604 _AppControlManager::GetInstance()->GetAppControlResponseEventContainer()->Remove(pEventArg->GetRequestId());
605 delete pResponseEvent;
606 SysLog(NID_APP, "pResponseEvent gets deleted, reqId(%d)", pEventArg->GetRequestId());
610 SysLog(NID_APP, "Unexpected AppControlResponseType(%d)", pEventArg->GetType());
615 SysLog(NID_APP, "Invalid ResponseListener");
620 SysLog(NID_APP, "Invalid AppControl arguments : arg(0x%x)", &arg);