Merge "Update the size check in MessagePort" into tizen_2.1
[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         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.");
316
317         //_AppArg::Print(b);
318         pImpl->SendAppControlEvent(*pArg);
319
320         return;
321
322 CATCH:
323         delete pAppArg;
324 }
325
326
327 result
328 _AppControlManager::SendAppControlStartResponse(int req, const char* pValue, const char* pOp)
329 {
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.");
332
333         SendAppControlEvent(*pArg);
334
335         return E_SUCCESS;
336 }
337
338
339 result
340 _AppControlManager::LaunchPkg(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
341 {
342         bundle* kb = msg.GetBundle();
343         SysTryReturnResult(NID_APP, kb != NULL, E_OUT_OF_MEMORY, "Bundle allocation failure.");
344
345         if (pkg_name)
346         {
347                 appsvc_set_pkgname(kb, pkg_name);
348         }
349
350         appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
351
352         if (mime)
353         {
354                 appsvc_set_mime(kb, mime);
355         }
356
357         if (uri)
358         {
359                 appsvc_set_uri(kb, uri);
360         }
361
362         if (_AppImpl::GetInstance() != null)
363         {
364                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
365                 _AppArg::UpdateWindowHandle(kb, handle);
366         }
367
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);
370
371         result r = E_SUCCESS;
372         if (pid < 0)
373         {
374                 switch (pid)
375                 {
376                 case APPSVC_RET_EILLACC:
377                         r = E_ILLEGAL_ACCESS;
378                         break;
379                 default:
380                         r = E_SYSTEM;
381                         break;
382                 }
383                 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
384         }
385
386         return r;
387 }
388 int
389 _AppControlManager::Launch(_AppMessageImpl& msg, const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
390 {
391         bundle* kb = msg.GetBundle();
392         SysTryReturn(NID_APP, kb != NULL, -1, E_OUT_OF_MEMORY, "Bundle allocation failure.");
393
394         if (pkg_name)
395         {
396                 appsvc_set_pkgname(kb, pkg_name);
397         }
398
399         appsvc_set_operation(kb, (op) ? op : APPSVC_OPERATION_DEFAULT);
400
401         if (mime)
402         {
403                 appsvc_set_mime(kb, mime);
404         }
405
406         if (uri)
407         {
408                 appsvc_set_uri(kb, uri);
409         }
410
411         if (_AppImpl::GetInstance() != null)
412         {
413                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
414                 _AppArg::UpdateWindowHandle(kb, handle);
415         }
416
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);
419
420         result r = E_SUCCESS;
421         if (pid < 0)
422         {
423                 switch (pid)
424                 {
425                 case APPSVC_RET_EILLACC:
426                         r = E_ILLEGAL_ACCESS;
427                         break;
428                 default:
429                         r = E_SYSTEM;
430                         break;
431                 }
432                 SysLog(NID_APP, "[%s]Launching service %s failure", GetErrorMessage(r), pkg_name);
433         }
434         SetLastResult(r);
435
436         return pid;
437 }
438
439
440 result
441 _AppControlManager::LaunchPkg(const char* pkg_name, const char* op, const char* mime, const char* uri, AppSvcResFn pCb, void* data)
442 {
443         _AppMessageImpl msg;
444
445         return LaunchPkg(msg, pkg_name, op, mime, uri, pCb, data);
446 }
447
448 result
449 _AppControlManager::LaunchAppWithCondition(const AppId& appId, const String& condition, IList* pArrayArgs)
450 {
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]");
454
455         r = pArg->ConstructForAppLaunchCondition(condition, pArrayArgs);
456         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] ConstructForAppLaunchCondition(%ls, .. ) fails", GetErrorMessage(r), condition.GetPointer());
457
458         r = _AppControlManager::LaunchApp(appId, pArg);
459 CATCH:
460         delete pArg;
461         return r;
462 }
463
464 result
465 _AppControlManager::LaunchApp(const AppId& appId, _AppArg* pArg, int req)
466 {
467         SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
468         SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
469
470         String actualAppId = appId;
471         if (appId.GetLength() == 10)
472         {
473                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
474
475                 if (!name.IsEmpty())
476                 {
477                         actualAppId.Append(L'.');
478                         actualAppId.Append(name);
479                 }
480         }
481
482         pArg->UpdateRequestId(req);
483
484         if (_AppImpl::GetInstance() != null)
485         {
486                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
487                 pArg->UpdateWindowHandle(handle);
488         }
489
490         int pid = -1;
491         bundle* kb = NULL;
492         String tempId;
493         actualAppId.SubString(0, 10, tempId);
494         tempId += L'.';
495         tempId += L"_AppControl";
496
497         // [INFO] Ugly solution for submode support
498         pArg->UpdateAppId(tempId);
499         kb = pArg->GetBundle();
500         
501         pid = appsvc_run_service(kb, req, LaunchResultCb, this);
502         if (pid > 0)
503         {
504                 SysLog(NID_APP, "Submode launch successful");
505                 return E_SUCCESS;
506         }
507
508         pArg->UpdateAppId(actualAppId);
509
510         // retry for possible failure
511         int count = 0;
512         const int TRY_COUNT = 3;
513         const int TRY_SLEEP_TIME = 65;
514         do
515         {
516                 kb = pArg->GetBundle();
517                 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
518                 if (pid > 0)
519                 {
520                         SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
521                         return E_SUCCESS;
522                 }
523                 count++;
524                 SysLog(NID_APP, "Waiting %dth time.", count);
525                 Thread::Sleep(TRY_SLEEP_TIME);
526         }
527         while (count < TRY_COUNT);
528
529         result r = E_SUCCESS;
530         switch (pid)
531         {
532         case APPSVC_RET_EILLACC:
533                 r = E_ILLEGAL_ACCESS;
534                 break;
535         default:
536                 r = E_SYSTEM;
537                 break;
538         }
539
540         SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
541
542         return r;
543 }
544
545 int
546 _AppControlManager::Launch(const AppId& appId, _AppArg* pArg, int req)
547 {
548         SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid launch argument");
549         SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
550
551         String actualAppId = appId;
552         if (appId.GetLength() == 10)
553         {
554                 const String& name = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
555
556                 if (!name.IsEmpty())
557                 {
558                         actualAppId.Append(L'.');
559                         actualAppId.Append(name);
560                 }
561         }
562
563         pArg->UpdateRequestId(req);
564
565         if (_AppImpl::GetInstance() != null)
566         {
567                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
568                 pArg->UpdateWindowHandle(handle);
569         }
570
571         int pid = -1;
572         bundle* kb = NULL;
573         String tempId;
574         actualAppId.SubString(0, 10, tempId);
575         tempId += L'.';
576         tempId += L"_AppControl";
577
578         // [INFO] Ugly solution for submode support
579         pArg->UpdateAppId(tempId);
580         kb = pArg->GetBundle();
581         
582         pid = appsvc_run_service(kb, req, LaunchResultCb, this);
583         if (pid > 0)
584         {
585                 SysLog(NID_APP, "Submode launch successful");
586                 return pid;
587         }
588
589         pArg->UpdateAppId(actualAppId);
590
591         // retry for possible failure
592         int count = 0;
593         const int TRY_COUNT = 3;
594         const int TRY_SLEEP_TIME = 65;
595         do
596         {
597                 kb = pArg->GetBundle();
598                 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
599                 if (pid > 0)
600                 {
601                         SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
602                         return pid;
603                 }
604                 count++;
605                 SysLog(NID_APP, "Waiting %dth time.", count);
606                 Thread::Sleep(TRY_SLEEP_TIME);
607         }
608         while (count < TRY_COUNT);
609
610         result r = E_SUCCESS;
611         switch (pid)
612         {
613         case APPSVC_RET_EILLACC:
614                 r = E_ILLEGAL_ACCESS;
615                 break;
616         default:
617                 r = E_SYSTEM;
618                 break;
619         }
620
621         SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
622
623         SetLastResult(r);
624
625         return pid;
626 }
627
628 result
629 _AppControlManager::LaunchAppImplicit(_AppArg* pArg, int req)
630 {
631         SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
632
633         result r = E_SUCCESS;
634         bundle* kb = pArg->GetBundle();
635
636         if (req >= 0)
637         {
638                 pArg->UpdateRequestId(req);
639                 _AppMessageImpl::AddData(kb, SELECTOR_NOTI_KEY, _AppInfo::GetApplicationId());
640         }
641
642         int pid = appsvc_run_service(kb, req, LaunchResultCb, this);
643         if (pid > 0)
644         {
645                 char pkgname[255] = {0, };
646                 aul_app_get_pkgname_bypid(pid, pkgname, 255);
647
648                 if (strncmp(pkgname, APP_SELECTOR, strlen(APP_SELECTOR)) != 0)
649                 {
650                         const char* pOperation = appsvc_get_operation(kb);
651
652                         SysLog(NID_APP, "Starting application without selector : (%s, %s).", pkgname, pOperation);
653
654                         SendAppControlStartResponse(req, pkgname, pOperation);
655                 }
656         }
657         else
658         {
659                 switch (pid)
660                 {
661                         case APPSVC_RET_EINVAL:
662                                 r = E_OBJ_NOT_FOUND;
663                                 break;
664                         case APPSVC_RET_ENOMATCH:
665                                 r = E_OBJ_NOT_FOUND;
666                                 break;
667                         case APPSVC_RET_EILLACC:
668                                 r = E_ILLEGAL_ACCESS;
669                                 break;
670                         case APPSVC_RET_ERROR:
671                                 // fall through
672                         case APPSVC_RET_ELAUNCH:
673                                 // fall through
674                         default:
675                                 r = E_SYSTEM;
676                                 break;
677                 }
678         }
679
680         SysLog(NID_APP, "[%s] Application(%d) launched with reqId(%d) and arg(0x%x).", GetErrorMessage(r), pid, req, pArg);
681
682         return r;
683 }
684
685 void
686 _AppControlManager::FinishAppControl(int reqId, int res, const IMap* pMap)
687 {
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.");
690
691         SendAppControlEvent(*pArg);
692 }
693
694 const _AppArg*
695 _AppControlManager::FindResultRequest(int reqId) const
696 {
697         const _ResultInfo* pInfo = __resultManager.FindItem(reqId);
698         return (pInfo) ? &(pInfo->arg) : null;
699 }
700
701 int
702 _AppControlManager::AddLaunchRequest(_AppArg* pArg, LaunchCbType pCb, void* pData, int prop)
703 {
704         SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
705
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.");
708
709         SysLog(NID_APP, "Registering callback 0x%x, 0x%x", pCb, pData);
710
711         return __launchManager.InsertItem(pItem);
712 }
713
714 void
715 _AppControlManager::RemoveLaunchRequest(int req)
716 {
717         __launchManager.RemoveItem(req);
718 }
719 int
720 _AppControlManager::GetLaunchRequestCount(void)
721 {
722         return __launchManager.GetCount();
723 }
724
725 result
726 _AppControlManager::RegisterRequest(service_s* service, int& req, _AppHandler& handler)
727 {
728         bundle* b = _AppArg::GetBundleFromSvc(service);
729
730         _AppArg* pArg = new (std::nothrow) _AppArg();
731         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "rrayList creation failure.");
732         pArg->Construct(b);
733
734         result r = E_SUCCESS;
735
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.");
739
740         req = __resultManager.InsertItem(pItem);
741         SysTryCatch(NID_APP, req != -1, , r = E_INVALID_STATE, "[E_INVALID_STATE] Invalid argument handling state.");
742
743         handler = _AppArg::GetHandler(b);
744
745         return E_SUCCESS;
746
747 CATCH:
748         delete pArg;
749
750         return r;
751 }
752
753
754 bool
755 _AppControlManager::IsAllowedAppControl(const wchar_t aTable[][2][64], int count, const String& aId, const String& oId)
756 {
757         for (int i = 0; i < count; i++)
758         {
759                 if (aId == aTable[i][0] && oId == aTable[i][1])
760                 {
761                         SysLog(NID_APP, "Found entry (%ls, %ls)", aTable[i][0], aTable[i][1]);
762                         return true;
763                 }
764         }
765
766         return false;
767 }
768
769 }} // Tizen::App