Refactor logic to check sending buffer size.
[platform/framework/native/appfw.git] / src / app / FApp_AppControlManager.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        FApp_AppControlManager.cpp
20  * @brief       This is the implementation for the _AppControlManager class.
21  */
22
23 #include <stdint.h>
24 #include <cstdio>
25 #include <unique_ptr.h>
26
27 #include <appsvc/appsvc.h>
28 #include <aul/aul.h>
29 #include <bundle.h>
30 #include <content/mime_type.h>
31
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>
46
47 #include <FBaseSysLog.h>
48 #include <FBase_StringConverter.h>
49 #include <FBaseRt_LibraryImpl.h>
50 #include <FIo_DataControlResultSetImpl.h>
51
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"
59 #include "FApp_Aul.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"
70
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;
77
78 //extern const char* _DATACONTROL_RESULTSET_DIR;
79
80 namespace Tizen { namespace App
81 {
82
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;
86
87 _InProcessInfo::~_InProcessInfo(void)
88 {
89         delete pLib;
90 }
91
92 _LaunchInfo::~_LaunchInfo(void)
93 {
94         delete pArg;
95 }
96
97
98 _AppControlManager::_AppControlManager(void)
99 {
100         SysLog(NID_APP, "");
101
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();
106 }
107
108 _AppControlManager::~_AppControlManager(void)
109 {
110         SysLog(NID_APP, "");
111         __appControlEvent.RemoveListener(*dynamic_cast<_IAppControlSysEventListener*>(this));
112 }
113
114 _AppControlManager*
115 _AppControlManager::GetInstance(void)
116 {
117         static _AppControlManager inst;
118
119         return &inst;
120 }
121
122 result
123 _AppControlManager::GetMimeFromExt(const String& ext, String& out)
124 {
125         std::unique_ptr<char[]> pExtension(_StringConverter::CopyToCharArrayN(ext));
126         SysTryReturnResult(NID_APP, pExtension != null, E_OUT_OF_MEMORY, "String allocation failure.");
127
128         char* mime = NULL;
129         mime_type_get_mime_type(pExtension.get(), &mime);
130
131         SysTryReturnResult(NID_APP, mime != NULL, E_UNSUPPORTED_FORMAT, "MIME type conversion failure for %ls.", ext.GetPointer());
132
133         out = mime;
134         free(mime);
135
136         return E_SUCCESS;
137 }
138
139 void
140 _AppControlManager::OnAppControlEventReceivedN(int reqId, _AppArg* pAppArg, int res)
141 {
142         SysLog(NID_APP, "Received request Id %d, arg 0x%x", reqId, pAppArg);
143
144         //_AppArg::Print(b);
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,
148                                         res);
149
150         // invoke callback
151         if (pInfo->launchCb)
152         {
153                 if (pInfo->magic == LAUNCH_INFO_MAGIC)
154                 {
155                         SysLog(NID_APP, "Invoking callback 0x%x", pInfo->launchCb);
156                         //pAppArg->Print();
157
158                         if (pInfo->pUserData && (!__listenerList.Contains(pInfo->pUserData)))
159                         {
160                                 (*pInfo->launchCb)(pInfo->pUserData, pInfo->pArg, pAppArg, static_cast<service_result_e>(res), pInfo->property);
161                         }
162                 }
163                 else
164                 {
165                         SysLogException(NID_APP, E_SYSTEM, "Corrupted data structure.");
166                 }
167         }
168
169         // clean up argument
170         __launchManager.RemoveItem(reqId);
171 }
172
173
174 // callback for out-of-process AppControl start event
175 void
176 _AppControlManager::OnAppControlEventReceivedN(int reqId, const AppId& appId, const String& operationId)
177 {
178         SysLog(NID_APP, "Received request Id %d, appId %ls, operationId %ls", reqId, appId.GetPointer(), operationId.GetPointer());
179
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);
183
184         // at least listener
185         IAppControlResponseListener* pListener = static_cast<IAppControlResponseListener*>(pInfo->pUserData);
186         SysTryReturnVoidResult(NID_APP, typeid(pListener) == typeid(IAppControlResponseListener*), E_SYSTEM, "[E_SYSTEM] Invalid result callback.");
187
188         if (pListener)
189         {
190                 result r = E_SUCCESS;
191                 AppId actualAppId = appId;
192                 if (appId == L'c')
193                 {
194                         actualAppId.Clear();
195                         r = E_OPERATION_CANCELED;
196                 }
197                 SysLog(NID_APP, "Invoking callback 0x%x.", pListener);
198                 pListener->OnAppControlStartResponseReceived(actualAppId, operationId, r);
199         }
200         else
201         {
202                 SysLog(NID_APP, "No listener registered.");
203         }
204 }
205
206
207 // callback for in-process event handling
208 void
209 _AppControlManager::OnAppControlEventReceivedN(int reqId, int res, const IMap* pArgs)
210 {
211         SysLog(NID_APP, "Received request Id %d, args 0x%x", reqId, pArgs);
212
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,
216                                         pArgs);
217
218         if (pInfo->pListener)
219         {
220                 String aId = pInfo->providerId;
221                 String oId = pInfo->operationId;
222
223                 SysLog(NID_APP, "Invoking callback 0x%x for (%ls, %ls).", pInfo->pListener, aId.GetPointer(), oId.GetPointer());
224
225                 if (pInfo->property & _APPCONTROL_PROPERTY_ALIAS)
226                 {
227                         _AppControlRegistry::_AppControlAliasEntry* pEntry = null;
228                         pEntry = _AppControlRegistry::GetInstance()->GetReverseAppControlAliasEntry(aId, oId);
229                         if (pEntry)
230                         {
231                                 aId = pEntry->provider;
232                                 oId = pEntry->operation;
233
234                                 SysLog(NID_APP, "Legacy AppControl name (%ls, %ls).", aId.GetPointer(), oId.GetPointer());
235                         }
236                 }
237
238                 if (pInfo->isLegacy)
239                 {
240                         IAppControlEventListener* pListener = dynamic_cast<IAppControlEventListener*>(pInfo->pListener);
241                         if (pListener)
242                         {
243                                 ArrayList list(SingleObjectDeleter);
244                                 _AppArg::FillLegacyAppControlResult(list, res, pArgs, aId);
245
246                                 pListener->OnAppControlCompleted(aId, oId, &list);
247                         }
248                         else
249                         {
250                                 SysLog(NID_APP, "Wrong AppControl listener type.");
251                         }
252                 }
253                 else
254                 {
255                         IAppControlResponseListener* pListener = dynamic_cast<IAppControlResponseListener*>(pInfo->pListener);
256                         if (pListener && (!__listenerList.Contains(pInfo->pListener)))
257                         {
258                                 SysLog(NID_APP, "OSP_AC OnAppControlCompleteResponseReceived");
259                                 pListener->OnAppControlCompleteResponseReceived(aId, oId, static_cast<AppCtrlResult>(res), pArgs);
260                         }
261                         else
262                         {
263                                 SysLog(NID_APP, "Wrong AppControl listener type.");
264                         }
265                 }
266         }
267         else
268         {
269                 SysLogException(NID_APP, E_SYSTEM, "Invalid AppControl listener.");
270         }
271
272         // call TerminateAppControl
273         result (* pFunc)(int req) = null;
274         pFunc = reinterpret_cast<result (*)(int)>(pInfo->pLib->GetProcAddress(L"TerminateAppControl"));
275         if (pFunc)
276         {
277                 (*pFunc)(pInfo->reqId);
278         }
279         else
280         {
281                 SysLogException(NID_APP, E_SYSTEM, "No TerminateAppControl() function.");
282         }
283
284         // remove from list and unload dll
285         __inAppManager.RemoveItem(reqId);
286 }
287
288
289 result
290 _AppControlManager::SendAppControlEvent(IEventArg& arg)
291 {
292         return __appControlEvent.FireAsync(arg);
293 }
294
295 // generic launch callback
296 static void
297 LaunchResultCb(bundle* b, int request_code, appsvc_result_val res, void* data)
298 {
299         SysLog(NID_APP, "SLP callee result: %d", res);
300
301         _AppControlManager* pImpl = static_cast<_AppControlManager*>(data);
302         if (pImpl == null)
303         {
304                 return;
305         }
306
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.");
309
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));
313
314         // save callee appId ASAP
315         pAppArg->SaveCalleeAppId();
316
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.");
319
320         //_AppArg::Print(b);
321         pImpl->SendAppControlEvent(*pArg);
322
323         return;
324
325 CATCH:
326         delete pAppArg;
327 }
328
329
330 result
331 _AppControlManager::SendAppControlStartResponse(int req, const char* pValue, const char* pOp)
332 {
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.");
335
336         SendAppControlEvent(*pArg);
337
338         return E_SUCCESS;
339 }
340
341
342 result
343 _AppControlManager::LaunchPkg(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
344 {
345         bundle* kb = msg.GetBundle();
346         SysTryReturnResult(NID_APP, kb != NULL, E_OUT_OF_MEMORY, "Bundle allocation failure.");
347
348         if (pkg_name)
349         {
350                 appsvc_set_pkgname(kb, pkg_name);
351         }
352
353         appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
354
355         if (mime)
356         {
357                 appsvc_set_mime(kb, mime);
358         }
359
360         if (uri)
361         {
362                 appsvc_set_uri(kb, uri);
363         }
364
365         if (_AppImpl::GetInstance() != null)
366         {
367                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
368                 _AppArg::UpdateWindowHandle(kb, handle);
369         }
370
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);
373
374         result r = E_SUCCESS;
375         if (pid < 0)
376         {
377                 switch (pid)
378                 {
379                 case APPSVC_RET_EILLACC:
380                         r = E_ILLEGAL_ACCESS;
381                         break;
382                 default:
383                         r = E_SYSTEM;
384                         break;
385                 }
386                 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
387         }
388
389         return r;
390 }
391 int
392 _AppControlManager::Launch(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
393 {
394         bundle* kb = msg.GetBundle();
395         SysTryReturn(NID_APP, kb != NULL, -1, E_OUT_OF_MEMORY, "Bundle allocation failure.");
396
397         if (pkg_name)
398         {
399                 appsvc_set_pkgname(kb, pkg_name);
400         }
401
402         appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
403
404         if (mime)
405         {
406                 appsvc_set_mime(kb, mime);
407         }
408
409         if (uri)
410         {
411                 appsvc_set_uri(kb, uri);
412         }
413
414         if (_AppImpl::GetInstance() != null)
415         {
416                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
417                 _AppArg::UpdateWindowHandle(kb, handle);
418         }
419
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);
422
423         result r = E_SUCCESS;
424         if (pid < 0)
425         {
426                 switch (pid)
427                 {
428                 case APPSVC_RET_EILLACC:
429                         r = E_ILLEGAL_ACCESS;
430                         break;
431                 default:
432                         r = E_SYSTEM;
433                         break;
434                 }
435                 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
436         }
437         SetLastResult(r);
438
439         return pid;
440 }
441
442
443 result
444 _AppControlManager::LaunchPkg(const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
445 {
446         _AppMessageImpl msg;
447
448         return LaunchPkg(msg, pkg_name, op, mime, uri, pCb, data);
449 }
450
451 result
452 _AppControlManager::LaunchAppWithCondition(const AppId& appId, const String& condition, IList* pArrayArgs)
453 {
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]");
457
458         r = pArg->ConstructForAppLaunchCondition(condition, pArrayArgs);
459         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] ConstructForAppLaunchCondition(%ls, .. ) fails", GetErrorMessage(r), condition.GetPointer());
460
461         r = _AppControlManager::LaunchApp(appId, pArg);
462 CATCH:
463         delete pArg;
464         return r;
465 }
466
467 result
468 _AppControlManager::LaunchApp(const AppId& appId, _AppArg* pArg, int req)
469 {
470         SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
471         SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
472
473         String actualAppId = appId;
474         if (appId.GetLength() == 10)
475         {
476                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
477
478                 if (!name.IsEmpty())
479                 {
480                         actualAppId.Append(L'.');
481                         actualAppId.Append(name);
482                 }
483         }
484
485         pArg->UpdateRequestId(req);
486
487         if (_AppImpl::GetInstance() != null)
488         {
489                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
490                 pArg->UpdateWindowHandle(handle);
491         }
492
493         int pid = -1;
494         bundle* kb = NULL;
495         String tempId;
496         actualAppId.SubString(0, 10, tempId);
497         tempId += L'.';
498         tempId += L"_AppControl";
499
500         // [INFO] Ugly solution for submode support
501         pArg->UpdateAppId(tempId);
502         kb = pArg->GetBundle();
503         
504         pid = appsvc_run_service(kb, req, LaunchResultCb, this);
505         if (pid > 0)
506         {
507                 SysLog(NID_APP, "Submode launch successful");
508                 return E_SUCCESS;
509         }
510
511         pArg->UpdateAppId(actualAppId);
512
513         // retry for possible failure
514         int count = 0;
515         const int TRY_COUNT = 3;
516         const int TRY_SLEEP_TIME = 65;
517         do
518         {
519                 kb = pArg->GetBundle();
520                 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
521                 if (pid > 0)
522                 {
523                         SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
524                         return E_SUCCESS;
525                 }
526                 count++;
527                 SysLog(NID_APP, "Waiting %dth time.", count);
528                 Thread::Sleep(TRY_SLEEP_TIME);
529         }
530         while (count < TRY_COUNT);
531
532         result r = E_SUCCESS;
533         switch (pid)
534         {
535         case APPSVC_RET_EILLACC:
536                 r = E_ILLEGAL_ACCESS;
537                 break;
538         default:
539                 r = E_SYSTEM;
540                 break;
541         }
542
543         SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
544
545         return r;
546 }
547
548 int
549 _AppControlManager::Launch(const AppId& appId, _AppArg* pArg, int req)
550 {
551         SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid launch argument");
552         SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
553
554         String actualAppId = appId;
555         if (appId.GetLength() == 10)
556         {
557                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
558
559                 if (!name.IsEmpty())
560                 {
561                         actualAppId.Append(L'.');
562                         actualAppId.Append(name);
563                 }
564         }
565
566         pArg->UpdateRequestId(req);
567
568         if (_AppImpl::GetInstance() != null)
569         {
570                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
571                 pArg->UpdateWindowHandle(handle);
572         }
573
574         int pid = -1;
575         bundle* kb = NULL;
576         String tempId;
577         actualAppId.SubString(0, 10, tempId);
578         tempId += L'.';
579         tempId += L"_AppControl";
580
581         // [INFO] Ugly solution for submode support
582         pArg->UpdateAppId(tempId);
583         kb = pArg->GetBundle();
584         
585         pid = appsvc_run_service(kb, req, LaunchResultCb, this);
586         if (pid > 0)
587         {
588                 SysLog(NID_APP, "Submode launch successful");
589                 return pid;
590         }
591
592         pArg->UpdateAppId(actualAppId);
593
594         // retry for possible failure
595         int count = 0;
596         const int TRY_COUNT = 3;
597         const int TRY_SLEEP_TIME = 65;
598         do
599         {
600                 kb = pArg->GetBundle();
601                 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
602                 if (pid > 0)
603                 {
604                         SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
605                         return pid;
606                 }
607                 count++;
608                 SysLog(NID_APP, "Waiting %dth time.", count);
609                 Thread::Sleep(TRY_SLEEP_TIME);
610         }
611         while (count < TRY_COUNT);
612
613         result r = E_SUCCESS;
614         switch (pid)
615         {
616         case APPSVC_RET_EILLACC:
617                 r = E_ILLEGAL_ACCESS;
618                 break;
619         default:
620                 r = E_SYSTEM;
621                 break;
622         }
623
624         SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
625
626         SetLastResult(r);
627
628         return pid;
629 }
630
631 result
632 _AppControlManager::LaunchAppImplicit(_AppArg* pArg, int req)
633 {
634         SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
635
636         result r = E_SUCCESS;
637         bundle* kb = pArg->GetBundle();
638
639         if (req >= 0)
640         {
641                 pArg->UpdateRequestId(req);
642                 _AppMessageImpl::AddData(kb, SELECTOR_NOTI_KEY, _AppInfo::GetApplicationId());
643         }
644
645         int pid = appsvc_run_service(kb, req, LaunchResultCb, this);
646         if (pid > 0)
647         {
648                 char pkgname[255] = {0, };
649                 aul_app_get_pkgname_bypid(pid, pkgname, 255);
650
651                 if (strncmp(pkgname, APP_SELECTOR, strlen(APP_SELECTOR)) != 0)
652                 {
653                         const char* pOperation = appsvc_get_operation(kb);
654
655                         SysLog(NID_APP, "Starting application without selector : (%s, %s).", pkgname, pOperation);
656
657                         SendAppControlStartResponse(req, pkgname, pOperation);
658                 }
659         }
660         else
661         {
662                 switch (pid)
663                 {
664                         case APPSVC_RET_EINVAL:
665                                 r = E_OBJ_NOT_FOUND;
666                                 break;
667                         case APPSVC_RET_ENOMATCH:
668                                 r = E_OBJ_NOT_FOUND;
669                                 break;
670                         case APPSVC_RET_EILLACC:
671                                 r = E_ILLEGAL_ACCESS;
672                                 break;
673                         case APPSVC_RET_ERROR:
674                                 // fall through
675                         case APPSVC_RET_ELAUNCH:
676                                 // fall through
677                         default:
678                                 r = E_SYSTEM;
679                                 break;
680                 }
681         }
682
683         SysLog(NID_APP, "[%s] Application(%d) launched with reqId(%d) and arg(0x%x).", GetErrorMessage(r), pid, req, pArg);
684
685         return r;
686 }
687
688 void
689 _AppControlManager::FinishAppControl(int reqId, int res, const IMap* pMap)
690 {
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.");
693
694         SendAppControlEvent(*pArg);
695 }
696
697 const _AppArg*
698 _AppControlManager::FindResultRequest(int reqId) const
699 {
700         const _ResultInfo* pInfo = __resultManager.FindItem(reqId);
701         return (pInfo) ? &(pInfo->arg) : null;
702 }
703
704 int
705 _AppControlManager::AddLaunchRequest(_AppArg* pArg, LaunchCbType pCb, void* pData, int prop)
706 {
707         SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
708
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.");
711
712         SysLog(NID_APP, "Registering callback 0x%x, 0x%x", pCb, pData);
713
714         return __launchManager.InsertItem(pItem);
715 }
716
717 void
718 _AppControlManager::RemoveLaunchRequest(int req)
719 {
720         __launchManager.RemoveItem(req);
721 }
722 int
723 _AppControlManager::GetLaunchRequestCount(void)
724 {
725         return __launchManager.GetCount();
726 }
727
728 result
729 _AppControlManager::RegisterRequest(service_s* service, int& req, _AppHandler& handler)
730 {
731         bundle* b = _AppArg::GetBundleFromSvc(service);
732
733         _AppArg* pArg = new (std::nothrow) _AppArg();
734         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "rrayList creation failure.");
735         pArg->Construct(b);
736
737         result r = E_SUCCESS;
738
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.");
742
743         req = __resultManager.InsertItem(pItem);
744         SysTryCatch(NID_APP, req != -1, , r = E_INVALID_STATE, "[E_INVALID_STATE] Invalid argument handling state.");
745
746         handler = _AppArg::GetHandler(b);
747
748         return E_SUCCESS;
749
750 CATCH:
751         delete pArg;
752
753         return r;
754 }
755
756
757 bool
758 _AppControlManager::IsAllowedAppControl(const wchar_t aTable[][2][64], int count, const String& aId, const String& oId)
759 {
760         for (int i = 0; i < count; i++)
761         {
762                 if (aId == aTable[i][0] && oId == aTable[i][1])
763                 {
764                         SysLog(NID_APP, "Found entry (%ls, %ls)", aTable[i][0], aTable[i][1]);
765                         return true;
766                 }
767         }
768
769         return false;
770 }
771
772 }} // Tizen::App