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 // save callee appId ASAP
315 pAppArg->SaveCalleeAppId();
317 pArg = new (std::nothrow) _AppControlEventArg(request_code, pAppArg, res);
318 SysTryCatch(NID_APP, pArg != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] AppControl event argument creation failure.");
321 pImpl->SendAppControlEvent(*pArg);
331 _AppControlManager::SendAppControlStartResponse(int req, const char* pValue, const char* pOp)
333 _AppControlStartEventArg* pArg = new (std::nothrow) _AppControlStartEventArg(req, AppId(pValue), String(pOp));
334 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "AppControl start event creation failure.");
336 SendAppControlEvent(*pArg);
343 _AppControlManager::LaunchPkg(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
345 bundle* kb = msg.GetBundle();
346 SysTryReturnResult(NID_APP, kb != NULL, E_OUT_OF_MEMORY, "Bundle allocation failure.");
350 appsvc_set_pkgname(kb, pkg_name);
353 appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
357 appsvc_set_mime(kb, mime);
362 appsvc_set_uri(kb, uri);
365 if (_AppImpl::GetInstance() != null)
367 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
368 _AppArg::UpdateWindowHandle(kb, handle);
371 SysLog(NID_APP, "MIME(%s), URI(%s).", appsvc_get_mime(kb), appsvc_get_uri(kb));
372 int pid = appsvc_run_service(kb, 0, reinterpret_cast<appsvc_res_fn>(pCb), this);
374 result r = E_SUCCESS;
379 case APPSVC_RET_EILLACC:
380 r = E_ILLEGAL_ACCESS;
386 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
392 _AppControlManager::Launch(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
394 bundle* kb = msg.GetBundle();
395 SysTryReturn(NID_APP, kb != NULL, -1, E_OUT_OF_MEMORY, "Bundle allocation failure.");
399 appsvc_set_pkgname(kb, pkg_name);
402 appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
406 appsvc_set_mime(kb, mime);
411 appsvc_set_uri(kb, uri);
414 if (_AppImpl::GetInstance() != null)
416 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
417 _AppArg::UpdateWindowHandle(kb, handle);
420 SysLog(NID_APP, "MIME(%s), URI(%s).", appsvc_get_mime(kb), appsvc_get_uri(kb));
421 int pid = appsvc_run_service(kb, 0, reinterpret_cast<appsvc_res_fn>(pCb), this);
423 result r = E_SUCCESS;
428 case APPSVC_RET_EILLACC:
429 r = E_ILLEGAL_ACCESS;
435 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
444 _AppControlManager::LaunchPkg(const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
448 return LaunchPkg(msg, pkg_name, op, mime, uri, pCb, data);
452 _AppControlManager::LaunchAppWithCondition(const AppId& appId, const String& condition, IList* pArrayArgs)
454 result r = E_SUCCESS;
455 _AppArg * pArg = new (std::nothrow) _AppArg();
456 SysTryCatch(NID_APP, pArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
458 r = pArg->ConstructForAppLaunchCondition(condition, pArrayArgs);
459 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] ConstructForAppLaunchCondition(%ls, .. ) fails", GetErrorMessage(r), condition.GetPointer());
461 r = _AppControlManager::LaunchApp(appId, pArg);
468 _AppControlManager::LaunchApp(const AppId& appId, _AppArg* pArg, int req)
470 SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
471 SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
473 String actualAppId = appId;
474 if (appId.GetLength() == 10)
476 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
480 actualAppId.Append(L'.');
481 actualAppId.Append(name);
485 pArg->UpdateRequestId(req);
487 if (_AppImpl::GetInstance() != null)
489 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
490 pArg->UpdateWindowHandle(handle);
496 actualAppId.SubString(0, 10, tempId);
498 tempId += L"_AppControl";
500 // [INFO] Ugly solution for submode support
501 pArg->UpdateAppId(tempId);
502 kb = pArg->GetBundle();
504 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
507 SysLog(NID_APP, "Submode launch successful");
511 pArg->UpdateAppId(actualAppId);
513 // retry for possible failure
515 const int TRY_COUNT = 3;
516 const int TRY_SLEEP_TIME = 65;
519 kb = pArg->GetBundle();
520 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
523 SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
527 SysLog(NID_APP, "Waiting %dth time.", count);
528 Thread::Sleep(TRY_SLEEP_TIME);
530 while (count < TRY_COUNT);
532 result r = E_SUCCESS;
535 case APPSVC_RET_EILLACC:
536 r = E_ILLEGAL_ACCESS;
543 SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
549 _AppControlManager::Launch(const AppId& appId, _AppArg* pArg, int req)
551 SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid launch argument");
552 SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
554 String actualAppId = appId;
555 if (appId.GetLength() == 10)
557 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
561 actualAppId.Append(L'.');
562 actualAppId.Append(name);
566 pArg->UpdateRequestId(req);
568 if (_AppImpl::GetInstance() != null)
570 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
571 pArg->UpdateWindowHandle(handle);
577 actualAppId.SubString(0, 10, tempId);
579 tempId += L"_AppControl";
581 // [INFO] Ugly solution for submode support
582 pArg->UpdateAppId(tempId);
583 kb = pArg->GetBundle();
585 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
588 SysLog(NID_APP, "Submode launch successful");
592 pArg->UpdateAppId(actualAppId);
594 // retry for possible failure
596 const int TRY_COUNT = 3;
597 const int TRY_SLEEP_TIME = 65;
600 kb = pArg->GetBundle();
601 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
604 SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
608 SysLog(NID_APP, "Waiting %dth time.", count);
609 Thread::Sleep(TRY_SLEEP_TIME);
611 while (count < TRY_COUNT);
613 result r = E_SUCCESS;
616 case APPSVC_RET_EILLACC:
617 r = E_ILLEGAL_ACCESS;
624 SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
632 _AppControlManager::LaunchAppImplicit(_AppArg* pArg, int req)
634 SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
636 result r = E_SUCCESS;
637 bundle* kb = pArg->GetBundle();
641 pArg->UpdateRequestId(req);
642 _AppMessageImpl::AddData(kb, SELECTOR_NOTI_KEY, _AppInfo::GetApplicationId());
645 int pid = appsvc_run_service(kb, req, LaunchResultCb, this);
648 char pkgname[255] = {0, };
649 aul_app_get_pkgname_bypid(pid, pkgname, 255);
651 if (strncmp(pkgname, APP_SELECTOR, strlen(APP_SELECTOR)) != 0)
653 const char* pOperation = appsvc_get_operation(kb);
655 SysLog(NID_APP, "Starting application without selector : (%s, %s).", pkgname, pOperation);
657 SendAppControlStartResponse(req, pkgname, pOperation);
664 case APPSVC_RET_EINVAL:
667 case APPSVC_RET_ENOMATCH:
670 case APPSVC_RET_EILLACC:
671 r = E_ILLEGAL_ACCESS;
673 case APPSVC_RET_ERROR:
675 case APPSVC_RET_ELAUNCH:
683 SysLog(NID_APP, "[%s] Application(%d) launched with reqId(%d) and arg(0x%x).", GetErrorMessage(r), pid, req, pArg);
689 _AppControlManager::FinishAppControl(int reqId, int res, const IMap* pMap)
691 _NativeAppControlEventArg* pArg = new (std::nothrow) _NativeAppControlEventArg(reqId, res, pMap);
692 SysTryReturnVoidResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Return argument allocation failure.");
694 SendAppControlEvent(*pArg);
698 _AppControlManager::FindResultRequest(int reqId) const
700 const _ResultInfo* pInfo = __resultManager.FindItem(reqId);
701 return (pInfo) ? &(pInfo->arg) : null;
705 _AppControlManager::AddLaunchRequest(_AppArg* pArg, LaunchCbType pCb, void* pData, int prop)
707 SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
709 _LaunchInfo* pItem = new (std::nothrow) _LaunchInfo(pArg, pCb, pData, prop);
710 SysTryReturn(NID_APP, pItem != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
712 SysLog(NID_APP, "Registering callback 0x%x, 0x%x", pCb, pData);
714 return __launchManager.InsertItem(pItem);
718 _AppControlManager::RemoveLaunchRequest(int req)
720 __launchManager.RemoveItem(req);
723 _AppControlManager::GetLaunchRequestCount(void)
725 return __launchManager.GetCount();
729 _AppControlManager::RegisterRequest(service_s* service, int& req, _AppHandler& handler)
731 bundle* b = _AppArg::GetBundleFromSvc(service);
733 _AppArg* pArg = new (std::nothrow) _AppArg();
734 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "rrayList creation failure.");
737 result r = E_SUCCESS;
739 // ownership is transfered to RequestManager
740 _ResultInfo* pItem = new (std::nothrow) _ResultInfo(*pArg);
741 SysTryCatch(NID_APP, pItem != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
743 req = __resultManager.InsertItem(pItem);
744 SysTryCatch(NID_APP, req != -1, , r = E_INVALID_STATE, "[E_INVALID_STATE] Invalid argument handling state.");
746 handler = _AppArg::GetHandler(b);
758 _AppControlManager::IsAllowedAppControl(const wchar_t aTable[][2][64], int count, const String& aId, const String& oId)
760 for (int i = 0; i < count; i++)
762 if (aId == aTable[i][0] && oId == aTable[i][1])
764 SysLog(NID_APP, "Found entry (%ls, %ls)", aTable[i][0], aTable[i][1]);