2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FApp_AppControlManager.cpp
20 * @brief This is the implementation for the _AppControlManager class.
25 #include <unique_ptr.h>
27 #include <appsvc/appsvc.h>
30 #include <content/mime_type.h>
32 #include <FBaseInteger.h>
33 #include <FBaseObject.h>
34 #include <FBaseString.h>
35 #include <FBaseColIListT.h>
36 #include <FBaseColArrayList.h>
37 #include <FBaseErrors.h>
38 #include <FAppAppControl.h>
39 #include <FAppAppManager.h>
40 #include <FAppIAppControlListener.h>
41 #include <FAppIAppControlResponseListener.h>
42 #include <FAppIAppFrame.h>
43 #include <FAppSqlDataControl.h>
44 #include <FAppMapDataControl.h>
45 #include <FAppPkgPackageInfo.h>
47 #include <FBaseSysLog.h>
48 #include <FBase_StringConverter.h>
49 #include <FBaseRt_LibraryImpl.h>
50 #include <FIo_DataControlResultSetImpl.h>
52 #include "FApp_AppArg.h"
53 #include "FApp_AppControlEventArg.h"
54 #include "FApp_AppControlRegistry.h"
55 #include "FApp_AppControlImpl.h"
56 #include "FApp_AppImpl.h"
57 #include "FApp_AppInfo.h"
58 #include "FApp_AppManagerEventArg.h"
60 #include "FApp_AppControlManager.h"
61 #include "FApp_AppManagerProxy.h"
62 #include "FApp_ConditionManagerProxy.h"
63 #include "FApp_IAppManagerEventListener.h"
64 #include "FApp_MapDataControlImpl.h"
65 #include "FApp_SqlDataControlImpl.h"
66 #include "FAppPkg_PackageManagerImpl.h"
67 #include "FAppPkg_PackageInfoImpl.h"
68 #include "FApp_AppMessageImpl.h"
69 #include "FApp_AppManagerImpl.h"
71 using namespace Tizen::App::Package;
72 using namespace Tizen::Base;
73 using namespace Tizen::Base::Collection;
74 using namespace Tizen::Base::Runtime;
75 using namespace Tizen::Base::Utility;
76 using namespace Tizen::Io;
78 //extern const char* _DATACONTROL_RESULTSET_DIR;
80 namespace Tizen { namespace App
83 const wchar_t TIZEN_OPERATION_PICK[] = L"http://tizen.org/appcontrol/operation/pick";
84 const wchar_t SELECTOR_NOTI_KEY[] = L"__APP_SVC_CALLER_NOTI__";
85 const int _MAX_PACKAGE_ID_LENGTH = 10;
87 _InProcessInfo::~_InProcessInfo(void)
92 _LaunchInfo::~_LaunchInfo(void)
98 _AppControlManager::_AppControlManager(void)
102 // AppControl event handling is expected to be performed in the main thread.
103 __appControlEvent.Construct();
104 __appControlEvent.AddListener(*dynamic_cast<_IAppControlSysEventListener*>(this));
105 __listenerList.Construct();
108 _AppControlManager::~_AppControlManager(void)
111 __appControlEvent.RemoveListener(*dynamic_cast<_IAppControlSysEventListener*>(this));
115 _AppControlManager::GetInstance(void)
117 static _AppControlManager inst;
123 _AppControlManager::GetMimeFromExt(const String& ext, String& out)
125 std::unique_ptr<char[]> pExtension(_StringConverter::CopyToCharArrayN(ext));
126 SysTryReturnResult(NID_APP, pExtension != null, E_OUT_OF_MEMORY, "String allocation failure.");
129 mime_type_get_mime_type(pExtension.get(), &mime);
131 SysTryReturnResult(NID_APP, mime != NULL, E_UNSUPPORTED_FORMAT, "MIME type conversion failure for %ls.", ext.GetPointer());
140 _AppControlManager::OnAppControlEventReceivedN(int reqId, _AppArg* pAppArg, int res)
142 SysLog(NID_APP, "Received request Id %d, arg 0x%x", reqId, pAppArg);
145 // get launch info from request Id
146 _LaunchInfo* pInfo = __launchManager.FindItem(reqId);
147 SysTryReturnVoidResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] request Id %d not found with response %d", reqId,
153 if (pInfo->magic == LAUNCH_INFO_MAGIC)
155 SysLog(NID_APP, "Invoking callback 0x%x", pInfo->launchCb);
158 if (pInfo->pUserData && (!__listenerList.Contains(pInfo->pUserData)))
160 (*pInfo->launchCb)(pInfo->pUserData, pInfo->pArg, pAppArg, static_cast<service_result_e>(res), pInfo->property);
165 SysLogException(NID_APP, E_SYSTEM, "Corrupted data structure.");
170 __launchManager.RemoveItem(reqId);
174 // callback for out-of-process AppControl start event
176 _AppControlManager::OnAppControlEventReceivedN(int reqId, const AppId& appId, const String& operationId)
178 SysLog(NID_APP, "Received request Id %d, appId %ls, operationId %ls", reqId, appId.GetPointer(), operationId.GetPointer());
180 // get launch info from request Id
181 _LaunchInfo* pInfo = __launchManager.FindItem(reqId);
182 SysTryReturnVoidResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] request Id %d not found.", reqId);
185 IAppControlResponseListener* pListener = static_cast<IAppControlResponseListener*>(pInfo->pUserData);
186 SysTryReturnVoidResult(NID_APP, typeid(pListener) == typeid(IAppControlResponseListener*), E_SYSTEM, "[E_SYSTEM] Invalid result callback.");
190 result r = E_SUCCESS;
191 AppId actualAppId = appId;
195 r = E_OPERATION_CANCELED;
197 SysLog(NID_APP, "Invoking callback 0x%x.", pListener);
198 pListener->OnAppControlStartResponseReceived(actualAppId, operationId, r);
202 SysLog(NID_APP, "No listener registered.");
207 // callback for in-process event handling
209 _AppControlManager::OnAppControlEventReceivedN(int reqId, int res, const IMap* pArgs)
211 SysLog(NID_APP, "Received request Id %d, args 0x%x", reqId, pArgs);
213 // process proper callback
214 _InProcessInfo* pInfo = __inAppManager.FindItem(reqId);
215 SysTryReturnVoidResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] request Id %d not found with args 0x%x", reqId,
218 if (pInfo->pListener)
220 String aId = pInfo->providerId;
221 String oId = pInfo->operationId;
223 SysLog(NID_APP, "Invoking callback 0x%x for (%ls, %ls).", pInfo->pListener, aId.GetPointer(), oId.GetPointer());
225 if (pInfo->property & _APPCONTROL_PROPERTY_ALIAS)
227 _AppControlRegistry::_AppControlAliasEntry* pEntry = null;
228 pEntry = _AppControlRegistry::GetInstance()->GetReverseAppControlAliasEntry(aId, oId);
231 aId = pEntry->provider;
232 oId = pEntry->operation;
234 SysLog(NID_APP, "Legacy AppControl name (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
240 IAppControlEventListener* pListener = dynamic_cast<IAppControlEventListener*>(pInfo->pListener);
243 ArrayList list(SingleObjectDeleter);
244 _AppArg::FillLegacyAppControlResult(list, res, pArgs, aId);
246 pListener->OnAppControlCompleted(aId, oId, &list);
250 SysLog(NID_APP, "Wrong AppControl listener type.");
255 IAppControlResponseListener* pListener = dynamic_cast<IAppControlResponseListener*>(pInfo->pListener);
256 if (pListener && (!__listenerList.Contains(pInfo->pListener)))
258 SysLog(NID_APP, "OSP_AC OnAppControlCompleteResponseReceived");
259 pListener->OnAppControlCompleteResponseReceived(aId, oId, static_cast<AppCtrlResult>(res), pArgs);
263 SysLog(NID_APP, "Wrong AppControl listener type.");
269 SysLogException(NID_APP, E_SYSTEM, "Invalid AppControl listener.");
272 // call TerminateAppControl
273 result (* pFunc)(int req) = null;
274 pFunc = reinterpret_cast<result (*)(int)>(pInfo->pLib->GetProcAddress(L"TerminateAppControl"));
277 (*pFunc)(pInfo->reqId);
281 SysLogException(NID_APP, E_SYSTEM, "No TerminateAppControl() function.");
284 // remove from list and unload dll
285 __inAppManager.RemoveItem(reqId);
290 _AppControlManager::SendAppControlEvent(IEventArg& arg)
292 return __appControlEvent.FireAsync(arg);
295 // generic launch callback
297 LaunchResultCb(bundle* b, int request_code, appsvc_result_val res, void* data)
299 SysLog(NID_APP, "SLP callee result: %d", res);
301 _AppControlManager* pImpl = static_cast<_AppControlManager*>(data);
307 _AppArg* pAppArg = new (std::nothrow) _AppArg;
308 SysTryReturnVoidResult(NID_APP, pAppArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppControl event argument creation failure.");
310 _AppControlEventArg* pArg = null;
311 result r = pAppArg->Construct(b);
312 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] AppControl event argument creation failure.", GetErrorMessage(r));
314 pArg = new (std::nothrow) _AppControlEventArg(request_code, pAppArg, res);
315 SysTryCatch(NID_APP, pArg != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] AppControl event argument creation failure.");
318 pImpl->SendAppControlEvent(*pArg);
328 _AppControlManager::SendAppControlStartResponse(int req, const char* pValue, const char* pOp)
330 _AppControlStartEventArg* pArg = new (std::nothrow) _AppControlStartEventArg(req, AppId(pValue), String(pOp));
331 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "AppControl start event creation failure.");
333 SendAppControlEvent(*pArg);
340 _AppControlManager::LaunchPkg(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
342 bundle* kb = msg.GetBundle();
343 SysTryReturnResult(NID_APP, kb != NULL, E_OUT_OF_MEMORY, "Bundle allocation failure.");
347 appsvc_set_pkgname(kb, pkg_name);
350 appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
354 appsvc_set_mime(kb, mime);
359 appsvc_set_uri(kb, uri);
362 if (_AppImpl::GetInstance() != null)
364 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
365 _AppArg::UpdateWindowHandle(kb, handle);
368 SysLog(NID_APP, "MIME(%s), URI(%s).", appsvc_get_mime(kb), appsvc_get_uri(kb));
369 int pid = appsvc_run_service(kb, 0, reinterpret_cast<appsvc_res_fn>(pCb), this);
371 result r = E_SUCCESS;
376 case APPSVC_RET_EILLACC:
377 r = E_ILLEGAL_ACCESS;
383 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
389 _AppControlManager::Launch(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
391 bundle* kb = msg.GetBundle();
392 SysTryReturn(NID_APP, kb != NULL, -1, E_OUT_OF_MEMORY, "Bundle allocation failure.");
396 appsvc_set_pkgname(kb, pkg_name);
399 appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
403 appsvc_set_mime(kb, mime);
408 appsvc_set_uri(kb, uri);
411 if (_AppImpl::GetInstance() != null)
413 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
414 _AppArg::UpdateWindowHandle(kb, handle);
417 SysLog(NID_APP, "MIME(%s), URI(%s).", appsvc_get_mime(kb), appsvc_get_uri(kb));
418 int pid = appsvc_run_service(kb, 0, reinterpret_cast<appsvc_res_fn>(pCb), this);
420 result r = E_SUCCESS;
425 case APPSVC_RET_EILLACC:
426 r = E_ILLEGAL_ACCESS;
432 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
441 _AppControlManager::LaunchPkg(const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
445 return LaunchPkg(msg, pkg_name, op, mime, uri, pCb, data);
449 _AppControlManager::LaunchAppWithCondition(const AppId& appId, const String& condition, IList* pArrayArgs)
451 result r = E_SUCCESS;
452 _AppArg * pArg = new (std::nothrow) _AppArg();
453 SysTryCatch(NID_APP, pArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
455 r = pArg->ConstructForAppLaunchCondition(condition, pArrayArgs);
456 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] ConstructForAppLaunchCondition(%ls, .. ) fails", GetErrorMessage(r), condition.GetPointer());
458 r = _AppControlManager::LaunchApp(appId, pArg);
465 _AppControlManager::LaunchApp(const AppId& appId, _AppArg* pArg, int req)
467 SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
468 SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
470 String actualAppId = appId;
471 if (appId.GetLength() == 10)
473 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
477 actualAppId.Append(L'.');
478 actualAppId.Append(name);
482 pArg->UpdateRequestId(req);
484 if (_AppImpl::GetInstance() != null)
486 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
487 pArg->UpdateWindowHandle(handle);
493 actualAppId.SubString(0, 10, tempId);
495 tempId += L"_AppControl";
497 // [INFO] Ugly solution for submode support
498 pArg->UpdateAppId(tempId);
499 kb = pArg->GetBundle();
501 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
504 SysLog(NID_APP, "Submode launch successful");
508 pArg->UpdateAppId(actualAppId);
510 // retry for possible failure
512 const int TRY_COUNT = 3;
513 const int TRY_SLEEP_TIME = 65;
516 kb = pArg->GetBundle();
517 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
520 SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
524 SysLog(NID_APP, "Waiting %dth time.", count);
525 Thread::Sleep(TRY_SLEEP_TIME);
527 while (count < TRY_COUNT);
529 result r = E_SUCCESS;
532 case APPSVC_RET_EILLACC:
533 r = E_ILLEGAL_ACCESS;
540 SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
546 _AppControlManager::Launch(const AppId& appId, _AppArg* pArg, int req)
548 SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid launch argument");
549 SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
551 String actualAppId = appId;
552 if (appId.GetLength() == 10)
554 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
558 actualAppId.Append(L'.');
559 actualAppId.Append(name);
563 pArg->UpdateRequestId(req);
565 if (_AppImpl::GetInstance() != null)
567 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
568 pArg->UpdateWindowHandle(handle);
574 actualAppId.SubString(0, 10, tempId);
576 tempId += L"_AppControl";
578 // [INFO] Ugly solution for submode support
579 pArg->UpdateAppId(tempId);
580 kb = pArg->GetBundle();
582 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
585 SysLog(NID_APP, "Submode launch successful");
589 pArg->UpdateAppId(actualAppId);
591 // retry for possible failure
593 const int TRY_COUNT = 3;
594 const int TRY_SLEEP_TIME = 65;
597 kb = pArg->GetBundle();
598 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
601 SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
605 SysLog(NID_APP, "Waiting %dth time.", count);
606 Thread::Sleep(TRY_SLEEP_TIME);
608 while (count < TRY_COUNT);
610 result r = E_SUCCESS;
613 case APPSVC_RET_EILLACC:
614 r = E_ILLEGAL_ACCESS;
621 SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
629 _AppControlManager::LaunchAppImplicit(_AppArg* pArg, int req)
631 SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
633 result r = E_SUCCESS;
634 bundle* kb = pArg->GetBundle();
638 pArg->UpdateRequestId(req);
639 _AppMessageImpl::AddData(kb, SELECTOR_NOTI_KEY, _AppInfo::GetApplicationId());
642 int pid = appsvc_run_service(kb, req, LaunchResultCb, this);
645 char pkgname[255] = {0, };
646 aul_app_get_pkgname_bypid(pid, pkgname, 255);
648 if (strncmp(pkgname, APP_SELECTOR, strlen(APP_SELECTOR)) != 0)
650 const char* pOperation = appsvc_get_operation(kb);
652 SysLog(NID_APP, "Starting application without selector : (%s, %s).", pkgname, pOperation);
654 SendAppControlStartResponse(req, pkgname, pOperation);
661 case APPSVC_RET_EINVAL:
664 case APPSVC_RET_ENOMATCH:
667 case APPSVC_RET_EILLACC:
668 r = E_ILLEGAL_ACCESS;
670 case APPSVC_RET_ERROR:
672 case APPSVC_RET_ELAUNCH:
680 SysLog(NID_APP, "[%s] Application(%d) launched with reqId(%d) and arg(0x%x).", GetErrorMessage(r), pid, req, pArg);
686 _AppControlManager::FinishAppControl(int reqId, int res, const IMap* pMap)
688 _NativeAppControlEventArg* pArg = new (std::nothrow) _NativeAppControlEventArg(reqId, res, pMap);
689 SysTryReturnVoidResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Return argument allocation failure.");
691 SendAppControlEvent(*pArg);
695 _AppControlManager::FindResultRequest(int reqId) const
697 const _ResultInfo* pInfo = __resultManager.FindItem(reqId);
698 return (pInfo) ? &(pInfo->arg) : null;
702 _AppControlManager::AddLaunchRequest(_AppArg* pArg, LaunchCbType pCb, void* pData, int prop)
704 SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
706 _LaunchInfo* pItem = new (std::nothrow) _LaunchInfo(pArg, pCb, pData, prop);
707 SysTryReturn(NID_APP, pItem != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
709 SysLog(NID_APP, "Registering callback 0x%x, 0x%x", pCb, pData);
711 return __launchManager.InsertItem(pItem);
715 _AppControlManager::RemoveLaunchRequest(int req)
717 __launchManager.RemoveItem(req);
720 _AppControlManager::GetLaunchRequestCount(void)
722 return __launchManager.GetCount();
726 _AppControlManager::RegisterRequest(service_s* service, int& req, _AppHandler& handler)
728 bundle* b = _AppArg::GetBundleFromSvc(service);
730 _AppArg* pArg = new (std::nothrow) _AppArg();
731 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "rrayList creation failure.");
734 result r = E_SUCCESS;
736 // ownership is transfered to RequestManager
737 _ResultInfo* pItem = new (std::nothrow) _ResultInfo(*pArg);
738 SysTryCatch(NID_APP, pItem != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
740 req = __resultManager.InsertItem(pItem);
741 SysTryCatch(NID_APP, req != -1, , r = E_INVALID_STATE, "[E_INVALID_STATE] Invalid argument handling state.");
743 handler = _AppArg::GetHandler(b);
755 _AppControlManager::IsAllowedAppControl(const wchar_t aTable[][2][64], int count, const String& aId, const String& oId)
757 for (int i = 0; i < count; i++)
759 if (aId == aTable[i][0] && oId == aTable[i][1])
761 SysLog(NID_APP, "Found entry (%ls, %ls)", aTable[i][0], aTable[i][1]);