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));
107 _AppControlManager::~_AppControlManager(void)
110 __appControlEvent.RemoveListener(*dynamic_cast<_IAppControlSysEventListener*>(this));
114 _AppControlManager::GetInstance(void)
116 static _AppControlManager inst;
122 _AppControlManager::GetMimeFromExt(const String& ext, String& out)
124 std::unique_ptr<char[]> pExtension(_StringConverter::CopyToCharArrayN(ext));
125 SysTryReturnResult(NID_APP, pExtension != null, E_OUT_OF_MEMORY, "String allocation failure.");
128 mime_type_get_mime_type(pExtension.get(), &mime);
130 SysTryReturnResult(NID_APP, mime != NULL, E_UNSUPPORTED_FORMAT, "MIME type conversion failure for %ls.", ext.GetPointer());
139 _AppControlManager::OnAppControlEventReceivedN(int reqId, _AppArg* pAppArg, int res)
141 SysLog(NID_APP, "Received request Id %d, arg 0x%x", reqId, pAppArg);
144 // get launch info from request Id
145 _LaunchInfo* pInfo = __launchManager.FindItem(reqId);
146 SysTryReturnVoidResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] request Id %d not found with response %d", reqId,
152 if (pInfo->magic == LAUNCH_INFO_MAGIC)
154 SysLog(NID_APP, "Invoking callback 0x%x", pInfo->launchCb);
156 (*pInfo->launchCb)(pInfo->pUserData, pInfo->pArg, pAppArg, static_cast<service_result_e>(res), pInfo->property);
160 SysLogException(NID_APP, E_SYSTEM, "Corrupted data structure.");
165 __launchManager.RemoveItem(reqId);
169 // callback for out-of-process AppControl start event
171 _AppControlManager::OnAppControlEventReceivedN(int reqId, const AppId& appId, const String& operationId)
173 SysLog(NID_APP, "Received request Id %d, appId %ls, operationId %ls", reqId, appId.GetPointer(), operationId.GetPointer());
175 // get launch info from request Id
176 _LaunchInfo* pInfo = __launchManager.FindItem(reqId);
177 SysTryReturnVoidResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] request Id %d not found.", reqId);
180 IAppControlResponseListener* pListener = static_cast<IAppControlResponseListener*>(pInfo->pUserData);
181 SysTryReturnVoidResult(NID_APP, typeid(pListener) == typeid(IAppControlResponseListener*), E_SYSTEM, "[E_SYSTEM] Invalid result callback.");
185 result r = E_SUCCESS;
186 AppId actualAppId = appId;
190 r = E_OPERATION_CANCELED;
192 SysLog(NID_APP, "Invoking callback 0x%x.", pListener);
193 pListener->OnAppControlStartResponseReceived(actualAppId, operationId, r);
197 SysLog(NID_APP, "No listener registered.");
202 // callback for in-process event handling
204 _AppControlManager::OnAppControlEventReceivedN(int reqId, int res, const IMap* pArgs)
206 SysLog(NID_APP, "Received request Id %d, args 0x%x", reqId, pArgs);
208 // process proper callback
209 _InProcessInfo* pInfo = __inAppManager.FindItem(reqId);
210 SysTryReturnVoidResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] request Id %d not found with args 0x%x", reqId,
213 if (pInfo->pListener)
215 String aId = pInfo->providerId;
216 String oId = pInfo->operationId;
218 SysLog(NID_APP, "Invoking callback 0x%x for (%ls, %ls).", pInfo->pListener, aId.GetPointer(), oId.GetPointer());
220 if (pInfo->property & _APPCONTROL_PROPERTY_ALIAS)
222 _AppControlRegistry::_AppControlAliasEntry* pEntry = null;
223 pEntry = _AppControlRegistry::GetInstance()->GetReverseAppControlAliasEntry(aId, oId);
226 aId = pEntry->provider;
227 oId = pEntry->operation;
229 SysLog(NID_APP, "Legacy AppControl name (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
235 IAppControlEventListener* pListener = dynamic_cast<IAppControlEventListener*>(pInfo->pListener);
238 ArrayList list(SingleObjectDeleter);
239 _AppArg::FillLegacyAppControlResult(list, res, pArgs, aId);
241 pListener->OnAppControlCompleted(aId, oId, &list);
245 SysLog(NID_APP, "Wrong AppControl listener type.");
250 IAppControlResponseListener* pListener = dynamic_cast<IAppControlResponseListener*>(pInfo->pListener);
253 pListener->OnAppControlCompleteResponseReceived(aId, oId, static_cast<AppCtrlResult>(res), pArgs);
257 SysLog(NID_APP, "Wrong AppControl listener type.");
263 SysLogException(NID_APP, E_SYSTEM, "Invalid AppControl listener.");
266 // call TerminateAppControl
267 result (* pFunc)(int req) = null;
268 pFunc = reinterpret_cast<result (*)(int)>(pInfo->pLib->GetProcAddress(L"TerminateAppControl"));
271 (*pFunc)(pInfo->reqId);
275 SysLogException(NID_APP, E_SYSTEM, "No TerminateAppControl() function.");
278 // remove from list and unload dll
279 __inAppManager.RemoveItem(reqId);
284 _AppControlManager::SendAppControlEvent(IEventArg& arg)
286 return __appControlEvent.FireAsync(arg);
289 // generic launch callback
291 LaunchResultCb(bundle* b, int request_code, appsvc_result_val res, void* data)
293 SysLog(NID_APP, "SLP callee result: %d", res);
295 _AppControlManager* pImpl = static_cast<_AppControlManager*>(data);
301 _AppArg* pAppArg = new (std::nothrow) _AppArg;
302 SysTryReturnVoidResult(NID_APP, pAppArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] AppControl event argument creation failure.");
304 _AppControlEventArg* pArg = null;
305 result r = pAppArg->Construct(b);
306 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] AppControl event argument creation failure.", GetErrorMessage(r));
308 pArg = new (std::nothrow) _AppControlEventArg(request_code, pAppArg, res);
309 SysTryCatch(NID_APP, pArg != null, r = E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] AppControl event argument creation failure.");
312 pImpl->SendAppControlEvent(*pArg);
322 _AppControlManager::SendAppControlStartResponse(int req, const char* pValue, const char* pOp)
324 _AppControlStartEventArg* pArg = new (std::nothrow) _AppControlStartEventArg(req, AppId(pValue), String(pOp));
325 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "AppControl start event creation failure.");
327 SendAppControlEvent(*pArg);
334 _AppControlManager::LaunchPkg(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
336 bundle* kb = msg.GetBundle();
337 SysTryReturnResult(NID_APP, kb != NULL, E_OUT_OF_MEMORY, "Bundle allocation failure.");
341 appsvc_set_pkgname(kb, pkg_name);
344 appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
348 appsvc_set_mime(kb, mime);
353 appsvc_set_uri(kb, uri);
356 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
357 _AppArg::UpdateWindowHandle(kb, handle);
359 SysLog(NID_APP, "MIME(%s), URI(%s).", appsvc_get_mime(kb), appsvc_get_uri(kb));
360 int pid = appsvc_run_service(kb, 0, reinterpret_cast<appsvc_res_fn>(pCb), this);
362 SysTryReturnResult(NID_APP, pid >= 0, E_SYSTEM, "Launching service %s failure with %d", pkg_name, pid);
367 _AppControlManager::Launch(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
369 bundle* kb = msg.GetBundle();
370 SysTryReturn(NID_APP, kb != NULL, -1, E_OUT_OF_MEMORY, "Bundle allocation failure.");
374 appsvc_set_pkgname(kb, pkg_name);
377 appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
381 appsvc_set_mime(kb, mime);
386 appsvc_set_uri(kb, uri);
389 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
390 _AppArg::UpdateWindowHandle(kb, handle);
392 SysLog(NID_APP, "MIME(%s), URI(%s).", appsvc_get_mime(kb), appsvc_get_uri(kb));
393 int pid = appsvc_run_service(kb, 0, reinterpret_cast<appsvc_res_fn>(pCb), this);
395 SysTryReturn(NID_APP, pid >= 0, -1, E_SYSTEM, "Launching service %s failure with %d", pkg_name, pid);
402 _AppControlManager::LaunchPkg(const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
406 return LaunchPkg(msg, pkg_name, op, mime, uri, pCb, data);
410 _AppControlManager::LaunchAppWithCondition(const AppId& appId, const String& executableName, const String& condition, IList* pArrayArgs)
412 result r = E_SUCCESS;
413 const String& actualAppId = appId + L'.' + executableName;
414 _AppArg * pArg = new (std::nothrow) _AppArg();
415 SysTryCatch(NID_APP, pArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
417 r = pArg->ConstructForAppLaunchCondition(condition, pArrayArgs);
418 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] ConstructForAppLaunchCondition(%ls, .. ) fails", GetErrorMessage(r), condition.GetPointer());
420 r = _AppControlManager::LaunchApp(actualAppId, pArg);
428 _AppControlManager::LaunchApp(const AppId& appId, _AppArg* pArg, int req)
430 SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
431 SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
433 String actualAppId = appId;
434 if (appId.GetLength() == 10)
436 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
440 actualAppId.Append(L'.');
441 actualAppId.Append(name);
445 pArg->UpdateRequestId(req);
447 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
448 pArg->UpdateWindowHandle(handle);
453 actualAppId.SubString(0, 10, tempId);
455 tempId += L"_AppControl";
457 // [INFO] Ugly solution for submode support
458 pArg->UpdateAppId(tempId);
459 kb = pArg->GetBundle();
461 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
464 SysLog(NID_APP, "Submode launch successful");
468 pArg->UpdateAppId(actualAppId);
470 // retry for possible failure
472 const int TRY_COUNT = 3;
473 const int TRY_SLEEP_TIME = 65;
476 kb = pArg->GetBundle();
477 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
480 SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
484 SysLog(NID_APP, "Waiting %dth time.", count);
485 Thread::Sleep(TRY_SLEEP_TIME);
487 while (count < TRY_COUNT);
489 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] Launching service failure for %ls with %d", appId.GetPointer(), pid);
495 _AppControlManager::Launch(const AppId& appId, _AppArg* pArg, int req)
497 SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid launch argument");
498 SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
500 String actualAppId = appId;
501 if (appId.GetLength() == 10)
503 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
507 actualAppId.Append(L'.');
508 actualAppId.Append(name);
512 pArg->UpdateRequestId(req);
514 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
515 pArg->UpdateWindowHandle(handle);
520 actualAppId.SubString(0, 10, tempId);
522 tempId += L"_AppControl";
524 // [INFO] Ugly solution for submode support
525 pArg->UpdateAppId(tempId);
526 kb = pArg->GetBundle();
528 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
531 SysLog(NID_APP, "Submode launch successful");
535 pArg->UpdateAppId(actualAppId);
537 // retry for possible failure
539 const int TRY_COUNT = 3;
540 const int TRY_SLEEP_TIME = 65;
543 kb = pArg->GetBundle();
544 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
547 SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
551 SysLog(NID_APP, "Waiting %dth time.", count);
552 Thread::Sleep(TRY_SLEEP_TIME);
554 while (count < TRY_COUNT);
556 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] Launching service failure for %ls with %d", appId.GetPointer(), pid);
562 _AppControlManager::LaunchAppImplicit(_AppArg* pArg, int req)
564 SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
566 result r = E_SUCCESS;
567 bundle* kb = pArg->GetBundle();
571 pArg->UpdateRequestId(req);
572 _AppMessageImpl::AddData(kb, SELECTOR_NOTI_KEY, _AppInfo::GetApplicationId());
575 int pid = appsvc_run_service(kb, req, LaunchResultCb, this);
578 case APPSVC_RET_EINVAL:
581 case APPSVC_RET_ENOMATCH:
584 case APPSVC_RET_ERROR:
586 case APPSVC_RET_ELAUNCH:
593 SysLog(NID_APP, "[%s] Application(%d) launched with reqId(%d) and arg(0x%x).", GetErrorMessage(r), pid, req, pArg);
599 _AppControlManager::FinishAppControl(int reqId, int res, const IMap* pMap)
601 _NativeAppControlEventArg* pArg = new (std::nothrow) _NativeAppControlEventArg(reqId, res, pMap);
602 SysTryReturnVoidResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Return argument allocation failure.");
604 SendAppControlEvent(*pArg);
608 _AppControlManager::FindResultRequest(int reqId) const
610 const _ResultInfo* pInfo = __resultManager.FindItem(reqId);
611 return (pInfo) ? &(pInfo->arg) : null;
615 _AppControlManager::AddLaunchRequest(_AppArg* pArg, LaunchCbType pCb, void* pData, int prop)
617 SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
619 _LaunchInfo* pItem = new (std::nothrow) _LaunchInfo(pArg, pCb, pData, prop);
620 SysTryReturn(NID_APP, pItem != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
622 SysLog(NID_APP, "Registering callback 0x%x, 0x%x", pCb, pData);
624 return __launchManager.InsertItem(pItem);
628 _AppControlManager::RemoveLaunchRequest(int req)
630 __launchManager.RemoveItem(req);
634 _AppControlManager::RegisterRequest(service_s* service, int& req, _AppHandler& handler)
636 bundle* b = _AppArg::GetBundleFromSvc(service);
638 _AppArg* pArg = new (std::nothrow) _AppArg();
639 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "rrayList creation failure.");
642 result r = E_SUCCESS;
644 // ownership is transfered to RequestManager
645 _ResultInfo* pItem = new (std::nothrow) _ResultInfo(*pArg);
646 SysTryCatch(NID_APP, pItem != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
648 req = __resultManager.InsertItem(pItem);
649 SysTryCatch(NID_APP, req != -1, , r = E_INVALID_STATE, "[E_INVALID_STATE] Invalid argument handling state.");
651 handler = _AppArg::GetHandler(b);
663 _AppControlManager::IsAllowedAppControl(const wchar_t aTable[][2][64], int count, const String& aId, const String& oId)
665 for (int i = 0; i < count; i++)
667 if (aId == aTable[i][0] && oId == aTable[i][1])
669 SysLog(NID_APP, "Found entry (%ls, %ls)", aTable[i][0], aTable[i][1]);