Merge "Revert "Revert "[N_SE-32429,32965] Forcedly set the system locale to avoid...
[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 static bool
546 _IsDefaultApplication(const AppId& packageId, const String& appId)
547 {
548         const String& execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(packageId);
549
550         int index = -1;
551         result r = appId.IndexOf(L'.', 0, index);
552         if (r != E_SUCCESS)
553         {
554                 return false;
555         }
556
557         String tmp;
558         appId.SubString(index + 1, tmp);
559         if (tmp == execName)
560         {
561                 SysLog(NID_APP, "Default application %ls", tmp.GetPointer());
562                 return true;
563         }
564
565         return false;
566 }
567
568 int
569 _AppControlManager::Launch(const AppId& appId, _AppArg* pArg, int req)
570 {
571         SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid launch argument");
572         SysLog(NID_APP, "AppId: %ls.", appId.GetPointer());
573
574         String actualAppId = appId;
575         if (appId.GetLength() == 10)
576         {
577                 const String& execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(appId);
578
579                 if (!execName.IsEmpty())
580                 {
581                         actualAppId.Append(L'.');
582                         actualAppId.Append(execName);
583                 }
584         }
585
586         pArg->UpdateRequestId(req);
587
588         if (_AppImpl::GetInstance() != null)
589         {
590                 const long handle = _AppImpl::GetInstance()->GetWindowHandle();
591                 pArg->UpdateWindowHandle(handle);
592         }
593
594         int pid = -1;
595         bundle* kb = NULL;
596         String tempId;
597
598         actualAppId.SubString(0, 10, tempId);
599
600         if (_IsDefaultApplication(tempId, appId))
601         {
602                 tempId += L'.';
603                 tempId += L"_AppControl";
604
605                 // [INFO] Ugly solution for submode support
606                 pArg->UpdateAppId(tempId);
607                 kb = pArg->GetBundle();
608
609                 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
610                 if (pid > 0)
611                 {
612                         SysLog(NID_APP, "Submode launch successful");
613                         return pid;
614                 }
615         }
616
617         pArg->UpdateAppId(actualAppId);
618
619         // retry for possible failure
620         int count = 0;
621         const int TRY_COUNT = 3;
622         const int TRY_SLEEP_TIME = 65;
623         do
624         {
625                 kb = pArg->GetBundle();
626                 pid = appsvc_run_service(kb, req, LaunchResultCb, this);
627                 if (pid > 0)
628                 {
629                         SysLog(NID_APP, "Application(%d) launched with reqId(%d) and arg(0x%x).", pid, req, pArg);
630                         return pid;
631                 }
632                 count++;
633                 SysLog(NID_APP, "Waiting %dth time.", count);
634                 Thread::Sleep(TRY_SLEEP_TIME);
635         }
636         while (count < TRY_COUNT);
637
638         result r = E_SUCCESS;
639         switch (pid)
640         {
641         case APPSVC_RET_EILLACC:
642                 r = E_ILLEGAL_ACCESS;
643                 break;
644         default:
645                 r = E_SYSTEM;
646                 break;
647         }
648
649         SysLogException(NID_APP, r, "[%s] Launching service failure for %ls", GetErrorMessage(r), appId.GetPointer());
650
651         SetLastResult(r);
652
653         return pid;
654 }
655
656 result
657 _AppControlManager::LaunchAppImplicit(_AppArg* pArg, int req)
658 {
659         SysTryReturnResult(NID_APP, pArg != null, E_INVALID_ARG, "Invalid launch argument");
660
661         result r = E_SUCCESS;
662         bundle* kb = pArg->GetBundle();
663
664         if (req >= 0)
665         {
666                 pArg->UpdateRequestId(req);
667                 _AppMessageImpl::AddData(kb, SELECTOR_NOTI_KEY, _AppInfo::GetApplicationId());
668         }
669
670         int pid = appsvc_run_service(kb, req, LaunchResultCb, this);
671         if (pid > 0)
672         {
673                 char pkgname[255] = {0, };
674                 aul_app_get_pkgname_bypid(pid, pkgname, 255);
675
676                 if (strncmp(pkgname, APP_SELECTOR, strlen(APP_SELECTOR)) != 0)
677                 {
678                         const char* pOperation = appsvc_get_operation(kb);
679
680                         SysLog(NID_APP, "Starting application without selector : (%s, %s).", pkgname, pOperation);
681
682                         SendAppControlStartResponse(req, pkgname, pOperation);
683                 }
684         }
685         else
686         {
687                 switch (pid)
688                 {
689                         case APPSVC_RET_EINVAL:
690                                 r = E_OBJ_NOT_FOUND;
691                                 break;
692                         case APPSVC_RET_ENOMATCH:
693                                 r = E_OBJ_NOT_FOUND;
694                                 break;
695                         case APPSVC_RET_EILLACC:
696                                 r = E_ILLEGAL_ACCESS;
697                                 break;
698                         case APPSVC_RET_ERROR:
699                                 // fall through
700                         case APPSVC_RET_ELAUNCH:
701                                 // fall through
702                         default:
703                                 r = E_SYSTEM;
704                                 break;
705                 }
706         }
707
708         SysLog(NID_APP, "[%s] Application(%d) launched with reqId(%d) and arg(0x%x).", GetErrorMessage(r), pid, req, pArg);
709
710         return r;
711 }
712
713 void
714 _AppControlManager::FinishAppControl(int reqId, int res, const IMap* pMap)
715 {
716         _NativeAppControlEventArg* pArg = new (std::nothrow) _NativeAppControlEventArg(reqId, res, pMap);
717         SysTryReturnVoidResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Return argument allocation failure.");
718
719         SendAppControlEvent(*pArg);
720 }
721
722 const _AppArg*
723 _AppControlManager::FindResultRequest(int reqId) const
724 {
725         const _ResultInfo* pInfo = __resultManager.FindItem(reqId);
726         return (pInfo) ? &(pInfo->arg) : null;
727 }
728
729 int
730 _AppControlManager::AddLaunchRequest(_AppArg* pArg, LaunchCbType pCb, void* pData, int prop)
731 {
732         SysTryReturn(NID_APP, pArg != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
733
734         _LaunchInfo* pItem = new (std::nothrow) _LaunchInfo(pArg, pCb, pData, prop);
735         SysTryReturn(NID_APP, pItem != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
736
737         SysLog(NID_APP, "Registering callback 0x%x, 0x%x", pCb, pData);
738
739         return __launchManager.InsertItem(pItem);
740 }
741
742 void
743 _AppControlManager::RemoveLaunchRequest(int req)
744 {
745         __launchManager.RemoveItem(req);
746 }
747 int
748 _AppControlManager::GetLaunchRequestCount(void)
749 {
750         return __launchManager.GetCount();
751 }
752
753 result
754 _AppControlManager::RegisterRequest(service_s* service, int& req, _AppHandler& handler)
755 {
756         bundle* b = _AppArg::GetBundleFromSvc(service);
757
758         _AppArg* pArg = new (std::nothrow) _AppArg();
759         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "rrayList creation failure.");
760         pArg->Construct(b);
761
762         result r = E_SUCCESS;
763
764         // ownership is transfered to RequestManager
765         _ResultInfo* pItem = new (std::nothrow) _ResultInfo(*pArg);
766         SysTryCatch(NID_APP, pItem != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Empty argument.");
767
768         req = __resultManager.InsertItem(pItem);
769         SysTryCatch(NID_APP, req != -1, , r = E_INVALID_STATE, "[E_INVALID_STATE] Invalid argument handling state.");
770
771         handler = _AppArg::GetHandler(b);
772
773         return E_SUCCESS;
774
775 CATCH:
776         delete pArg;
777
778         return r;
779 }
780
781
782 bool
783 _AppControlManager::IsAllowedAppControl(const wchar_t aTable[][2][64], int count, const String& aId, const String& oId)
784 {
785         for (int i = 0; i < count; i++)
786         {
787                 if (aId == aTable[i][0] && oId == aTable[i][1])
788                 {
789                         SysLog(NID_APP, "Found entry (%ls, %ls)", aTable[i][0], aTable[i][1]);
790                         return true;
791                 }
792         }
793
794         return false;
795 }
796
797 }} // Tizen::App