Merge "Revert "Delete checking if strlen() is 0 in ConvertWcsToMbsN()"" into tizen_2.1
[platform/framework/native/appfw.git] / src / app / FApp_AppControlImpl.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_AppControlImpl.cpp
20  * @brief               This is the implementation for the Application Control class.
21  */
22
23 #include <new>
24 #include <typeinfo>
25 #include <unique_ptr.h>
26
27 #include <appsvc/appsvc.h>
28
29 #include <FAppAppControl.h>
30 #include <FAppAppManager.h>
31 #include <FAppPkgPackageAppInfo.h>
32 #include <FAppIAppControlEventListener.h>
33 #include <FAppIAppControlResponseListener.h>
34 #include <FBaseColHashMap.h>
35 #include <FBaseSysLog.h>
36
37 #include <FBaseRt_LibraryImpl.h>
38 #include <FIo_DirectoryImpl.h>
39
40 #include "FApp_AppControlImpl.h"
41 #include "FApp_AppControlManager.h"
42 #include "FApp_AppArg.h"
43 #include "FApp_AppImpl.h"
44 #include "FApp_AppControlRegistry.h"
45 #include "FApp_AppMessageImpl.h"
46 #include "FApp_AppInfo.h"
47 #include "FAppPkg_PackageManagerImpl.h"
48 #include "FApp_Aul.h"
49 #include "FApp_AppControlEventArg.h"
50 #include "FApp_AppControlResponseEvent.h"
51 #include "FBaseRt_ThreadImpl.h"
52
53 using namespace Tizen::Base;
54 using namespace Tizen::Base::Collection;
55 using namespace Tizen::Base::Runtime;
56 using namespace Tizen::App;
57 using namespace Tizen::App::Package;
58 using namespace Tizen::Io;
59
60 namespace
61 {
62
63 static const int _REQ_ID_INVALID = -1;
64
65 }
66
67 namespace Tizen { namespace App
68 {
69
70 const wchar_t TIZEN_OPERATION_MAIN[] = L"http://tizen.org/appcontrol/operation/main";
71 const char TIZEN_APPCONTROL_DATA_LEGACY[] = "http://tizen.org/appcontrol/data/legacyresult";
72
73 _AppControlImpl::_AppControlImpl(const AppControl& value)
74         : _appControl(value)
75         , _reqId(_REQ_ID_INVALID)
76         , _property(_APPCONTROL_PROPERTY_NONE)
77         , _processId(_REQ_ID_INVALID)
78 {
79         __appControlResponseEventList.Construct();
80 }
81
82 _AppControlImpl::~_AppControlImpl(void)
83 {
84         IEnumeratorT<int>* pEnum = __appControlResponseEventList.GetEnumeratorN();
85         IMapT<int, _AppControlResponseEvent*>* pResponseEventContainer = null;
86         if(pEnum != null)
87         {
88                 pResponseEventContainer = _AppControlManager::GetInstance()->GetAppControlResponseEventContainer();
89         }
90
91         while(pEnum->MoveNext() == E_SUCCESS)
92         {
93                 int reqId;
94                 pEnum->GetCurrent(reqId);
95                 if (pResponseEventContainer != null)
96                 {
97                         _AppControlResponseEvent* pResponseEvent = null;
98                         pResponseEventContainer->GetValue(reqId, pResponseEvent);
99                         delete pResponseEvent;
100
101                         pResponseEventContainer->Remove(reqId);
102                         SysLog(NID_APP, "pResponseEvent gets deleted. reqId(%d)", reqId);
103                 }
104         }
105         delete pEnum;
106 }
107
108 AppControl*
109 _AppControlImpl::CreateN(const String& path, const String& aId, const String& oId, const String& name, int prop)
110 {
111         SysTryReturn(NID_APP, !path.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] Path is empty.");
112         SysTryReturn(NID_APP, !aId.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] Provider Id is empty.");
113         SysTryReturn(NID_APP, !oId.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] Operation Id is empty.");
114
115         AppControl* pAc = new (std::nothrow) AppControl;
116         SysTryReturn(NID_APP, pAc != null, null, E_OUT_OF_MEMORY, "AppControl allocation failure.");
117
118         _AppControlImpl* pImpl = pAc->__pAppControlImpl;
119         SysTryReturn(NID_APP, pImpl != null, null, E_OUT_OF_MEMORY, "AppControlImpl instance must not be null.");
120
121         pImpl->_path = path;
122         pImpl->_provider = aId;
123         pImpl->_opId = oId;
124         // [FIXME] Proper App name setting
125         pImpl->_appName = name;
126         pImpl->_property = prop;
127
128         return pAc;
129 }
130
131 AppControl*
132 _AppControlImpl::CreateN(const AppId& appId, const String& operationId, bool changeAppId)
133 {
134         SysTryReturn(NID_APP, !appId.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] appId is empty.");
135
136         AppControl* pAc = new (std::nothrow) AppControl;
137         SysTryReturn(NID_APP, pAc != null, null, E_OUT_OF_MEMORY, "AppControl allocation failure.");
138
139         _AppControlImpl* pImpl = pAc->__pAppControlImpl;
140         SysTryReturn(NID_APP, pImpl != null, null, E_OUT_OF_MEMORY, "AppControlImpl instance must not be null.");
141
142         pImpl->_path = appId;
143         pImpl->_provider = appId;
144         pImpl->_opId = operationId;
145         //pImpl->_appName = appId;
146         pImpl->_property |= (_APPCONTROL_PROPERTY_OSP | _APPCONTROL_PROPERTY_PUBLIC);
147
148         if (changeAppId)
149         {
150                 pImpl->_property |= _APPCONTROL_PROPERTY_APPID_CHANGE;
151         }
152
153         return pAc;
154 }
155
156 AppControl*
157 _AppControlImpl::CreateN(const AppControl& ac)
158 {
159         const _AppControlImpl* pImpl = GetInstance(ac);
160         SysTryReturn(NID_APP, pImpl != null, null, E_INVALID_STATE, "AppControlImpl instance must not be null.");
161
162         return CreateN(pImpl->_path, pImpl->_provider, pImpl->_opId, pImpl->_appName, pImpl->_property);
163 }
164
165 const _AppControlImpl*
166 _AppControlImpl::GetInstance(const AppControl& ac)
167 {
168         return ac.__pAppControlImpl;
169 }
170
171 _AppControlImpl*
172 _AppControlImpl::GetInstance(AppControl& ac)
173 {
174         return ac.__pAppControlImpl;
175 }
176
177 result
178 _AppControlImpl::Start(const IList* pDataList, IAppControlEventListener* pListener)
179 {
180         SysLog(NID_APP, "Enter");
181         result r = E_SYSTEM;
182
183         if (_property & _APPCONTROL_PROPERTY_SLP)
184         {
185                 r = StartNative(pDataList, pListener);
186         }
187         else if (_property & _APPCONTROL_PROPERTY_OSP)
188         {
189                 r = StartOsp(pDataList, pListener);
190         }
191         else
192         {
193                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] Invalid AppControl type(property %d)", _property);
194         }
195         SysLog(NID_APP, "Exit");
196
197         return r;
198 }
199
200 result
201 _AppControlImpl::Start(const String* pUriData, const String* pDataType, const IMap* pExtraData, IAppControlResponseListener* pListener)
202 {
203         SysLog(NID_APP, "Enter");
204         result r = E_SYSTEM;
205
206         if (_property & _APPCONTROL_PROPERTY_SLP)
207         {
208                 r = StartNative(pUriData, pDataType, pExtraData, pListener);
209         }
210         else if (_property & _APPCONTROL_PROPERTY_OSP)
211         {
212                 r = StartOsp(pUriData, pDataType, pExtraData, pListener);
213         }
214         else
215         {
216                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] Invalid AppControl type(property %d)", _property);
217         }
218         SysLog(NID_APP, "Exit");
219
220         return r;
221 }
222
223 result
224 _AppControlImpl::FindAndStart(const String& operationId, const String* pUriPattern, const String* pDataType, const String* pCategory, const IMap* pExtraData, IAppControlResponseListener* pListener)
225 {
226         // [FIXME] valid argument size checking required
227         SysLog(NID_APP, "Enter");
228
229         std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
230         SysTryReturnResult(NID_APP, pBundle.get(), E_OUT_OF_MEMORY, "Bundle creation failure.");
231
232         _AppMessageImpl::SetOperation(pBundle.get(), operationId);
233
234         if (pUriPattern)
235         {
236                 _AppMessageImpl::SetUri(pBundle.get(), *pUriPattern);
237         }
238
239         if (pDataType)
240         {
241                 String mimeType = *pDataType;
242
243                 if ((*pDataType)[0] == L'.')
244                 {
245                         SysLog(NID_APP, "Extension to MIME conversion for %ls", pDataType->GetPointer());
246
247 #if 0
248                         String ext;
249                         pDataType->SubString(1, ext);
250
251                         result r = _AppControlManager::GetMimeFromExt(ext, mimeType);
252
253                         SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] MIME type conversion failure for %ls.", GetErrorMessage(r), ext.GetPointer());
254
255                         pMimeType = &mimeType;
256
257                         SysLog(NID_APP, "Conversion : %ls -> %ls.", pDataType->GetPointer(), pMimeType->GetPointer());
258 #endif
259                 }
260
261                 _AppMessageImpl::SetMime(pBundle.get(), mimeType);
262         }
263
264         if (pCategory)
265         {
266                 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
267         }
268
269         return StartImplicit(pBundle.get(), pExtraData, pListener);
270 }
271
272 result
273 _AppControlImpl::StartOsp(const IList* pDataList, IAppControlEventListener* pListener)
274 {
275         SysLog(NID_APP, "Enter");
276         result r = E_SUCCESS;
277
278         _AppArg* pArg = new (std::nothrow) _AppArg;
279         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Argument allocation failure.");
280
281         pArg->Construct(*this, pDataList);
282
283         _AppControlManager* pImpl = _AppControlManager::GetInstance();
284         int req = _REQ_ID_INVALID;
285
286         if (pListener)
287         {
288                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pImpl, pArg, AppControlCbLegacy, pListener, _property);
289                 req = reqObj.GetRequestNumber();
290
291                 _processId = pImpl->Launch(_path, pArg, req);
292
293                 if (_processId < 0)
294                 {
295                         reqObj.Invalidate();
296                         r = GetLastResult();
297                         SysLog(NID_APP, "[%s]Launching(%ls) is failed" , GetErrorMessage(r), _path.GetPointer());
298                         return r;
299                 }
300         }
301         else
302         {
303                 _processId = pImpl->Launch(_path, pArg);
304                 delete pArg;
305                 SysTryReturnResult(NID_APP, _processId >= 0, GetLastResult(), "Launching(%ls) is failed", _path.GetPointer());
306         }
307         SysLog(NID_APP, "Exit");
308
309         return r;
310 }
311
312 result
313 _AppControlImpl::StartOsp(const String* pUriData, const String* pMimeType, const IMap* pDataList, IAppControlResponseListener* pListener)
314 {
315         SysLog(NID_APP, "Enter");
316         result r = E_SUCCESS;
317
318         _AppArg* pArg = new (std::nothrow) _AppArg;
319         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Argument allocation failure.");
320
321         pArg->Construct(*this, pUriData, pMimeType, pDataList);
322
323         _AppControlManager* pImpl = _AppControlManager::GetInstance();
324         int req = _REQ_ID_INVALID;
325
326         if (pListener)
327         {
328                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pImpl, pArg, AppControlCb, pListener, _property);
329                 req = reqObj.GetRequestNumber();
330
331                 if (_ThreadImpl::GetCurrentThreadImpl()->GetThreadType() == THREAD_TYPE_EVENT_DRIVEN)
332                 {
333                         _AppControlResponseEvent* pAppControlResponseEvent = new (std::nothrow) _AppControlResponseEvent();
334                         if (pAppControlResponseEvent != null)
335                         {
336                                 r = pAppControlResponseEvent->Construct();
337                                 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::Construct() is failed", GetErrorMessage(r));
338
339                                 r = pAppControlResponseEvent->AddListener(*this, true);
340                                 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::AddListener() is failed", GetErrorMessage(r));
341
342                                 IMapT<int, _AppControlResponseEvent*>* pResponseEventContainer = pImpl->GetAppControlResponseEventContainer();
343                                 if (pResponseEventContainer != null)
344                                 {
345                                         pResponseEventContainer->Add(req, pAppControlResponseEvent);
346                                         __appControlResponseEventList.Add(req);
347                                         SysLog(NID_APP, "pResponseEvent gets added. reqId(%d)", req);
348                                 }
349                         }
350                 }
351
352                 _processId = pImpl->Launch(_path, pArg, req);           
353                 if (_processId < 0)
354                 {
355                         reqObj.Invalidate();
356                         r = GetLastResult();
357                         SysLog(NID_APP, "[%s]Launching(%ls) is failed" , GetErrorMessage(r), _path.GetPointer());
358                         return r;
359                 }
360         }
361         else
362         {
363                 _processId = pImpl->Launch(_path, pArg);
364                 delete pArg;
365                 SysTryReturnResult(NID_APP, _processId >= 0, GetLastResult(), "Launching(%ls) is failed", _path.GetPointer());
366         }
367         SysLog(NID_APP, "Exit");
368
369         return r;
370 }
371
372 result
373 _AppControlImpl::StartImplicit(bundle* pBundle, const IList* pDataList, IAppControlEventListener* pListener)
374 {
375         SysLog(NID_APP, "Enter");
376         result r = E_SUCCESS;
377
378         _AppArg* pArg = new (std::nothrow) _AppArg;
379         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Argument allocation failure.");
380
381         pArg->Construct(pBundle);
382
383         _AppMessageImpl::AddData(pArg->GetBundle(), pDataList);
384
385         _AppControlManager* pImpl = _AppControlManager::GetInstance();
386         int req = _REQ_ID_INVALID;
387
388         if (pListener)
389         {
390                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pImpl, pArg, AppControlCbLegacy, pListener, _APPCONTROL_PROPERTY_NONE);
391                 req = reqObj.GetRequestNumber();
392
393                 r = pImpl->LaunchAppImplicit(pArg, req);
394                 SysTryCatch(NID_APP, r == E_SUCCESS, reqObj.Invalidate(), r, "[%s] Propagating." , GetErrorMessage(r));
395         }
396         else
397         {
398                 r = pImpl->LaunchAppImplicit(pArg, -1);
399                 delete pArg;
400         }
401
402         // [FIXME] launch registration is not working correctly
403         SysLog(NID_APP, "Exit");
404
405 CATCH:
406         return r;
407 }
408
409 result
410 _AppControlImpl::StartImplicit(bundle* pBundle, const IMap* pData, IAppControlResponseListener* pListener)
411 {
412         SysLog(NID_APP, "Enter");
413         result r = E_SUCCESS;
414
415         _AppArg* pArg = new (std::nothrow) _AppArg;
416         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "Argument allocation failure.");
417
418         pArg->Construct(pBundle);
419
420         _AppArg::AddStrMap(pArg->GetBundle(), pData);
421
422         _AppControlManager* pImpl = _AppControlManager::GetInstance();
423         int req = _REQ_ID_INVALID;
424
425         if (pListener)
426         {
427                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pImpl, pArg, AppControlCb, pListener, _APPCONTROL_PROPERTY_NONE);
428                 req = reqObj.GetRequestNumber();
429
430                 if (_ThreadImpl::GetCurrentThreadImpl()->GetThreadType() == THREAD_TYPE_EVENT_DRIVEN)
431                 {
432                         _AppControlResponseEvent* pAppControlResponseEvent = new (std::nothrow) _AppControlResponseEvent();
433
434                         if (pAppControlResponseEvent != null)
435                         {
436                                 r = pAppControlResponseEvent->Construct();
437                                 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::Construct() is failed", GetErrorMessage(r));
438
439                                 r = pAppControlResponseEvent->AddListener(*pImpl, true);
440                                 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::AddListener() is failed", GetErrorMessage(r));
441
442                                 IMapT<int, _AppControlResponseEvent*>* pResponseEventContainer = pImpl->GetAppControlResponseEventContainer();
443                                 if (pResponseEventContainer != null)
444                                 {
445                                         pResponseEventContainer->Add(req, pAppControlResponseEvent);
446                                         SysLog(NID_APP, "pResponseEvent gets added. reqId(%d)", req);
447                                 }
448                         }
449                 }
450                 r = pImpl->LaunchAppImplicit(pArg, req);
451                 SysTryCatch(NID_APP, r == E_SUCCESS, reqObj.Invalidate(), r, "[%s] Propagating." , GetErrorMessage(r));
452         }
453         else
454         {
455                 r = pImpl->LaunchAppImplicit(pArg, -1);
456                 delete pArg;
457         }
458         SysLog(NID_APP, "Exit");
459
460         // [FIXME] launch registration is not working correctly
461
462 CATCH:
463
464         return r;
465 }
466
467 result
468 _AppControlImpl::AppControlCbLegacy(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int reqId)
469 {
470         SysLog(NID_APP, "Result value %d", res);
471
472         SysTryReturnResult(NID_APP, data && pResArg && pArg, E_SYSTEM, "Invalid result (callback, result, arg) = (0x%x, 0x%x, 0x%x).",
473                                           data, pResArg,
474                                           pArg);
475
476         bundle* b = pResArg->GetBundle();
477         SysTryReturnResult(NID_APP, b != NULL, E_SYSTEM, "Invalid result bundle.");
478
479         IAppControlEventListener* pListener = static_cast<IAppControlEventListener*>(data);
480         SysTryReturnResult(NID_APP, typeid(pListener) == typeid(IAppControlEventListener*), E_SYSTEM, "Invalid result callback");
481
482         bundle* inb = pArg->GetBundle();
483         SysTryReturnResult(NID_APP, inb != NULL, E_SYSTEM, "Empty caller bundle.");
484
485         String oId;
486
487         AppId provider = pResArg->GetCalleeAppId();
488
489         const char* p = appsvc_get_operation(inb);
490         if (p)
491         {
492                 oId = p;
493         }
494
495         SysLog(NID_APP, "Invoking callback with (%ls, %ls)", provider.GetPointer(), oId.GetPointer());
496
497         if (prop & _APPCONTROL_PROPERTY_ALIAS)
498         {
499                 const _AppControlRegistry::_AppControlAliasEntry* pEntry = null;
500                 pEntry = _AppControlRegistry::GetInstance()->GetReverseAppControlAliasEntry(provider, oId);
501                 if (pEntry)
502                 {
503                         provider = pEntry->provider;
504                         oId = pEntry->operation;
505
506                         SysLog(NID_APP, "Legacy AppControl name (%ls, %ls).", provider.GetPointer(), oId.GetPointer());
507                 }
508         }
509
510         if (prop & _APPCONTROL_PROPERTY_APPID_CHANGE)
511         {
512                 String tmp = _AppControlRegistry::GetInstance()->GetReverseAliasAppId(provider);
513                 if (!tmp.IsEmpty())
514                 {
515                         SysLog(NID_APP, "AppId change (%ls -> %ls).", provider.GetPointer(), tmp.GetPointer());
516
517                         provider = tmp;
518                 }
519         }
520
521         std::unique_ptr<HashMap> pMap(pResArg->GetArgMapN());
522
523         ArrayList list(SingleObjectDeleter);
524         _AppArg::FillLegacyAppControlResult(list, res, pMap.get(), provider);
525
526         // proper callback invokation
527         pListener->OnAppControlCompleted(provider, oId, &list);
528
529         return E_SUCCESS;
530 }
531
532 result
533 _AppControlImpl::AppControlCb(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int reqId)
534 {
535         SysLog(NID_APP, "Result value : %d, property : 0x%x", res, prop);
536
537         SysTryReturnResult(NID_APP, data && pResArg && pArg, E_SYSTEM, "Invalid result (callback, result, arg) = (0x%x, 0x%x, 0x%x).",
538                                           data, pResArg,
539                                           pArg);
540
541         bundle* b = pResArg->GetBundle();
542         SysTryReturnResult(NID_APP, b != NULL, E_SYSTEM, "Invalid result bundle.");
543
544         IAppControlResponseListener* pListener = static_cast<IAppControlResponseListener*>(data);
545         SysTryReturnResult(NID_APP, typeid(pListener) == typeid(IAppControlResponseListener*), E_SYSTEM, "Invalid result callback");
546
547         bundle* inb = pArg->GetBundle();
548         SysTryReturnResult(NID_APP, inb != NULL, E_SYSTEM, "Empty caller bundle.");
549
550         String oId;
551
552         AppId provider = pResArg->GetCalleeAppId();
553
554         const char* p = appsvc_get_operation(inb);
555         if (p)
556         {
557                 oId = p;
558         }
559
560         SysLog(NID_APP, "Invoking callback with (%ls, %ls)", provider.GetPointer(), oId.GetPointer());
561
562         if (prop & _APPCONTROL_PROPERTY_ALIAS)
563         {
564                 const _AppControlRegistry::_AppControlAliasEntry* pEntry = null;
565                 pEntry = _AppControlRegistry::GetInstance()->GetReverseRuntimeAliasEntry(provider, oId);
566                 if (pEntry)
567                 {
568                         provider = pEntry->provider;
569                         oId = pEntry->operation;
570
571                         SysLog(NID_APP, "Original AppControl name (%ls, %ls).", provider.GetPointer(), oId.GetPointer());
572                 }
573         }
574
575         if (prop & _APPCONTROL_PROPERTY_APPID_CHANGE)
576         {
577                 String tmp = _AppControlRegistry::GetInstance()->GetReverseAliasAppId(provider);
578                 if (!tmp.IsEmpty())
579                 {
580                         SysLog(NID_APP, "AppId change (%ls -> %ls).", provider.GetPointer(), tmp.GetPointer());
581
582                         provider = tmp;
583                 }
584         }
585
586         std::unique_ptr<HashMap> pMap(pResArg->GetArgMapN());
587
588         AppCtrlResult ret = _AppControlManager::ConvertAppControlResultCode(res);
589
590         SysLog(NID_APP, "Result code : 0x%x.", ret);
591
592         // proper callback invokation
593         _AppControlResponseEvent* pResponseEvent = null;
594         _AppControlManager::GetInstance()->GetAppControlResponseEventContainer()->GetValue(reqId, pResponseEvent);
595
596         if (pResponseEvent != null)
597         {
598                 _AppControlResponseEventArg* pResponseEventArg = new (std::nothrow) _AppControlResponseEventArg(pListener, _APPCONTROL_RESPONSETYPE_COMPLETE, provider, oId, E_SUCCESS, ret, pMap.get(), reqId);
599                 pResponseEvent->Fire(*pResponseEventArg);
600                 SysLog(NID_APP, "OnAppControlCompleteResponseReceived, pResponseEvent is Fired");
601         }
602         else
603         {
604                 pListener->OnAppControlCompleteResponseReceived(provider, oId, ret, pMap.get());
605                 SysLog(NID_APP, "OnAppControlCompleteResponseReceived, Called");
606         }
607
608         return E_SUCCESS;
609 }
610
611 result
612 _AppControlImpl::StartNative(const IList* pDataList, IAppControlEventListener* pListener)
613 {
614         SysLog(NID_APP, "Enter");
615         _InProcessInfo* pInfo = _AppControlManager::GetInstance()->__inAppManager.FindItem(_reqId);
616         SysTryReturnResult(NID_APP, pInfo == null, E_IN_PROGRESS, "Request ID %d is already in progress.", _reqId);
617
618         int req = _REQ_ID_INVALID;
619         _LibraryImpl* pLib = null;
620
621         pLib = new (std::nothrow) _LibraryImpl;
622         SysTryReturnResult(NID_APP, pLib != null, E_OUT_OF_MEMORY, "Failed to allocate libraryimpl.");
623
624         unsigned long option = _LIBRARY_LOAD_OPTION_LAZY;
625         option |= _LIBRARY_LOAD_OPTION_NODELETE;
626
627         result r = pLib->Construct(_path, option);
628         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
629
630         if (pListener)
631         {
632                 _InProcessInfo* pItem = new (std::nothrow) _InProcessInfo(_provider, _opId, _property, true, *pLib, pListener);
633                 if (pItem)
634                 {
635                         req = _AppControlManager::GetInstance()->__inAppManager.InsertItem(pItem);
636                 }
637         }
638         r = InvokeStartAppControl(*pLib, req, _provider, _opId, pDataList);
639
640         if (pListener == null)
641         {
642                 delete pLib;
643         }
644
645         // after acquring request number, pLib should be managed from the list, not CATCH
646         if (IsFailed(r))
647         {
648                 SetLastResult(E_SYSTEM);
649                 _AppControlManager::GetInstance()->__inAppManager.RemoveItem(req);
650                 SysLog(NID_APP, "[E_SYSTEM] A system error has occurred with %s.", GetErrorMessage(r));
651
652                 return E_SYSTEM;
653         }
654
655         _reqId = req;
656
657         SysLog(NID_APP, "Exit");
658         return E_SUCCESS;
659
660 CATCH:
661         delete pLib;
662
663         switch (r)
664         {
665         case E_LIBRARY_NOT_FOUND:
666                 r = E_OBJ_NOT_FOUND;
667                 break;
668         default:
669                 r = E_SYSTEM;
670                 break;
671         }
672
673         return r;
674 }
675
676 result
677 _AppControlImpl::StartNative(const String* pUriData, const String* pMimeType, const IMap* pDataList, IAppControlResponseListener* pListener)
678 {
679         SysLog(NID_APP, "Enter");
680         _InProcessInfo* pInfo = _AppControlManager::GetInstance()->__inAppManager.FindItem(_reqId);
681         SysTryReturnResult(NID_APP, pInfo == null, E_IN_PROGRESS, "Request ID %d is already in progress.", _reqId);
682
683         int req = _REQ_ID_INVALID;
684         _LibraryImpl* pLib = null;
685
686         pLib = new (std::nothrow) _LibraryImpl;
687         SysTryReturnResult(NID_APP, pLib != null, E_OUT_OF_MEMORY, "Failed to allocate libraryimpl.");
688
689         unsigned long option = _LIBRARY_LOAD_OPTION_LAZY;
690         option |= _LIBRARY_LOAD_OPTION_NODELETE;
691
692         result r = pLib->Construct(_path, option);
693
694         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
695
696         if (pListener)
697         {
698                 _InProcessInfo* pItem = new (std::nothrow) _InProcessInfo(_provider, _opId, _property, false, *pLib, pListener);
699                 if (pItem)
700                 {
701                         req = _AppControlManager::GetInstance()->__inAppManager.InsertItem(pItem);
702                 }
703
704                 if (_ThreadImpl::GetCurrentThreadImpl()->GetThreadType() == THREAD_TYPE_EVENT_DRIVEN)
705                 {
706                         _AppControlResponseEvent* pAppControlResponseEvent = new (std::nothrow) _AppControlResponseEvent();
707
708                         if (pAppControlResponseEvent != null)
709                         {
710                                 r = pAppControlResponseEvent->Construct();
711                                 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::Construct() is failed", GetErrorMessage(r));
712
713                                 r = pAppControlResponseEvent->AddListener(*this, true);
714                                 SysTryLog(NID_APP, r == E_SUCCESS, "[%s]_AppControlResponseEvent::AddListener() is failed", GetErrorMessage(r));
715
716                                 IMapT<int, _AppControlResponseEvent*>* pResponseEventContainer = _AppControlManager::GetInstance()->GetAppControlResponseEventContainer();
717                                 if (pResponseEventContainer != null)
718                                 {
719                                         int responseEventRequestId = RESPONSE_EVENT_REQID_MAGIC + req;
720                                         pResponseEventContainer->Add(responseEventRequestId, pAppControlResponseEvent);
721                                         __appControlResponseEventList.Add(responseEventRequestId);
722                                         SysLog(NID_APP, "pResponseEvent gets added. reqId(%d)", responseEventRequestId);
723                                 }
724                         }
725                 }
726         }
727         r = InvokeStartAppControl(*pLib, req, _provider, _opId, pUriData, pMimeType, pDataList);
728
729         if (pListener == null)
730         {
731                 delete pLib;
732         }
733
734         // after acquring request number, pLib should be managed from the list, not CATCH
735         if (IsFailed(r))
736         {
737                 SetLastResult(E_SYSTEM);
738                 _AppControlManager::GetInstance()->__inAppManager.RemoveItem(req);
739                 SysLog(NID_APP, "[E_SYSTEM] A system error has occurred with %s.", GetErrorMessage(r));
740
741                 return E_SYSTEM;
742         }
743
744         _reqId = req;
745         SysLog(NID_APP, "Exit");
746
747         return E_SUCCESS;
748
749 CATCH:
750         delete pLib;
751
752         switch (r)
753         {
754         case E_LIBRARY_NOT_FOUND:
755                 r = E_OBJ_NOT_FOUND;
756                 break;
757         default:
758                 r = E_SYSTEM;
759                 break;
760         }
761
762         return r;
763 }
764
765 result
766 _AppControlImpl::InvokeStartAppControl(_LibraryImpl& lib, int req, const String& appId, const String& oId, const IList* pList)
767 {
768         SysLog(NID_APP, "Legacy stuff for converting argument");
769
770         HashMap map(SingleObjectDeleter);
771         HashMap* pMap = null;
772         if (pList)
773         {
774                 map.Construct();
775
776                 _AppArg::FillMapFromList(&map, pList);
777
778                 pMap = &map;
779         }
780
781         return InvokeStartAppControl(lib, req, appId, oId, null, null, pMap);
782 }
783
784
785 result
786 _AppControlImpl::InvokeStartAppControl(_LibraryImpl& lib, int req, const String& appId, const String& oId, const String* pUri, const String* pMime, const IMap* pMap)
787 {
788         result (*pFunc)(int req, const String&, const String&, const String*, const String*, const IMap*) = null;
789
790         pFunc =
791                 reinterpret_cast<result (*)(int, const String&, const String&, const String*, const String*, const IMap*)>(lib.GetProcAddress(L"StartAppControl"));
792         SysTryReturnResult(NID_APP, pFunc != null, E_OBJ_NOT_FOUND, "Entry \"StartAppControl\" not found for %ls", appId.GetPointer());
793
794         const String* pActualUri = (pUri && !(pUri->IsEmpty())) ? pUri : null;
795         const String* pActualMime = (pMime && !(pMime->IsEmpty())) ? pMime : null;
796         return (*pFunc)(req, appId, oId, pActualUri, pActualMime, pMap);
797 }
798
799 static bool
800 IsValidAppControl(const String& appcontrolID)
801 {
802         return ((appcontrolID == L"osp.appcontrol.provider.audio")
803                 || (appcontrolID == L"osp.appcontrol.provider.bluetooth")
804                 || (appcontrolID == L"osp.appcontrol.provider.calendar")
805                 || (appcontrolID == L"osp.appcontrol.provider.camera")
806                 || (appcontrolID == L"osp.appcontrol.provider.contact")
807                 || (appcontrolID == L"osp.appcontrol.provider.certificatemanager")
808                 || (appcontrolID == L"osp.appcontrol.provider.email")
809                 || (appcontrolID == L"osp.appcontrol.provider.image")
810                 || (appcontrolID == L"osp.appcontrol.provider.media")
811                 || (appcontrolID == L"osp.appcontrol.provider.message")
812                 || (appcontrolID == L"osp.appcontrol.provider.video")
813                 || (appcontrolID == L"osp.appcontrol.provider.imageeditor")
814                 || (appcontrolID == L"osp.appcontrol.provider.allshare")
815                 || (appcontrolID == L"tizen.filemanager")
816                 || (appcontrolID == L"tizen.camera")
817                 || (appcontrolID == L"tizen.gallery")
818                 || (appcontrolID == L"tizen.imageviewer")
819                 || (appcontrolID == L"tizen.videoplayer")
820                 || (appcontrolID == L"tizen.memo")
821                 || (appcontrolID == L"tizen.contacts")
822                 || (appcontrolID == L"tizen.calendar")
823                 || (appcontrolID == L"tizen.events")
824                 || (appcontrolID == L"tizen.email")
825                 || (appcontrolID == L"tizen.settings")
826                 || (appcontrolID == L"tizen.messages")
827                 || (appcontrolID == L"tizen.musicplayer")
828                 || (appcontrolID == L"tizen.bluetooth")
829                 || (appcontrolID == L"samsung.snote")
830                 || (appcontrolID == L"0pnxz8hbsr.MyFiles")
831                 || (appcontrolID == L"hdufar9ycj.Camera")
832                 || (appcontrolID == L"ijudt7w61q.Gallery")
833                 || (appcontrolID == L"jysyv9o1dc.ImageViewer")
834                 || (appcontrolID == L"npwf0scb88.VideoPlayer")
835                 || (appcontrolID == L"zunqjlsnce.Memo")
836                 || (appcontrolID == L"f9uev8hsyo.Contacts")
837                 || (appcontrolID == L"ph1vq2phrp.Calendar")
838                 || (appcontrolID == L"vxqbrefica.Email")
839                 || (appcontrolID == L"kto5jikgul.Settings")
840                 || (appcontrolID == L"8r4r5ddzzn.Messages")
841                 || (appcontrolID == L"dhrul6qzj3.MusicPlayer")
842                 || (appcontrolID == L"smemo-efl"));
843 }
844
845 result
846 _AppControlImpl::Stop(void)
847 {
848         const String appcontrolID(GetAppControlProviderId());
849         SysTryReturnResult(NID_APP, IsValidAppControl(appcontrolID), E_INVALID_OPERATION, "Invalid appcontrolID(%ls)", appcontrolID.GetPointer());
850
851         if (_property & _APPCONTROL_PROPERTY_SLP)
852         {       
853                 SysTryReturnResult(NID_APP, _reqId != _REQ_ID_INVALID, E_INVALID_OPERATION, "Invalid request ID .");
854
855                 _InProcessInfo* pInfo = _AppControlManager::GetInstance()->__inAppManager.FindItem(_reqId);
856                 SysTryReturnResult(NID_APP, pInfo != null, E_INVALID_OPERATION, "Request ID %d is not found.", _reqId);
857                 
858                 result (*pStop)(int req) = null;
859                 pStop = reinterpret_cast<result (*)(int)>(pInfo->pLib->GetProcAddress(L"TerminateAppControl"));
860                 SysTryReturnResult(NID_APP, pStop != null, E_SYSTEM, "No TerminateAppControl() function.");
861                 
862                 (*pStop)(_reqId);
863                 
864                 _AppControlManager::GetInstance()->__inAppManager.RemoveItem(_reqId);
865                 
866                 _reqId = _REQ_ID_INVALID;
867         }
868         else if (_property & _APPCONTROL_PROPERTY_OSP)
869         {
870                 _Aul::TerminateApplicationByPid(_processId);
871         }
872
873         return E_SUCCESS;
874 }
875
876 String
877 _AppControlImpl::GetAppName(void)
878 {
879         if ((_property & _APPCONTROL_PROPERTY_OSP) && _appName.IsEmpty())
880         {
881                 const AppId& appId = _path;
882
883                 std::unique_ptr<PackageAppInfo> pInfo(_PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId));
884
885                 if (pInfo.get())
886                 {
887                         const String& name = pInfo->GetAppName();
888                         if (name == L"_AppControl")
889                         {
890                                 // workaround for special case: requery with actual appId
891                                 const PackageId& packageId = _PackageManagerImpl::GetPackageIdByAppId(appId);
892                                 const String& defaultName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(packageId);
893
894                                 const String& convertedAppId = packageId + L'.' + defaultName;
895
896                                 std::unique_ptr<PackageAppInfo> pNewInfo(_PackageManagerImpl::GetInstance()->GetPackageAppInfoN(convertedAppId));
897
898                                 if (pNewInfo.get())
899                                 {
900                                         _appName = pNewInfo->GetAppDisplayName();
901                                 }
902                                 else
903                                 {
904                                         SysLog(NID_APP, "No default applicaiton information, possible database error.");
905                                 }
906                         }
907                         else
908                         {
909                                 _appName = pInfo->GetAppDisplayName();
910                         }
911                         SysLog(NID_APP, "Initializing AppName(%ls) for %ls.", _appName.GetPointer(), appId.GetPointer());
912                 }
913         }
914
915         return _appName;
916 }
917
918 String
919 _AppControlImpl::GetAppId(void) const
920 {
921         return (_property & _APPCONTROL_PROPERTY_OSP) ? _path : _provider;
922 }
923
924 const String&
925 _AppControlImpl::GetAppControlProviderId(void) const
926 {
927         return _provider;
928 }
929
930 const String&
931 _AppControlImpl::GetOperationId(void) const
932 {
933         return _opId;
934 }
935
936 IList*
937 _AppControlImpl::GetCategoryListN(void) const
938 {
939         const AppId& appId = GetAppId();
940         SysTryReturn(NID_APP, !appId.IsEmpty(), null, E_SYSTEM, "[E_SYSTEM] Empty appId.");
941
942         SysLog(NID_APP, "Acquiring category for appId %ls.", appId.GetPointer());
943
944         std::unique_ptr<PackageAppInfo> pAppInfo(_PackageManagerImpl::GetInstance()->GetPackageAppInfoN(appId));
945         SysTryReturn(NID_APP, pAppInfo.get() != null, null, E_SYSTEM, "[E_SYSTEM] Getting PackageAppInfo failed.");
946
947         return pAppInfo->GetAppCategoryListN();
948 }
949
950 void
951 _AppControlImpl::StopAppControlResponseListener(IAppControlResponseListener* pListener)
952 {
953         _AppControlManager::GetInstance()->__listenerList.Add(pListener);
954 }
955
956 void
957 _AppControlImpl::OnAppControlResponseEventReceivedN(const Tizen::Base::Runtime::IEventArg* arg)
958 {
959         const _AppControlResponseEventArg* pEventArg = dynamic_cast<const _AppControlResponseEventArg*>(arg);
960
961         if (pEventArg != null)
962         {
963                 IAppControlResponseListener* pResponseListener = pEventArg->GetListener();
964
965                 if(pResponseListener != null)
966                 {
967                         if(pEventArg->GetType() == _APPCONTROL_RESPONSETYPE_COMPLETE)
968                         {
969                                 pResponseListener->OnAppControlCompleteResponseReceived(pEventArg->GetAppId(), pEventArg->GetOperationId(), pEventArg->GetAppControlResult(), pEventArg->GetExtraData());
970                                 SysLog(NID_APP, "OnAppControlCompleteResponseReceived called");
971
972                                 _AppControlResponseEvent* pResponseEvent = null;
973                                 _AppControlManager::GetInstance()->GetAppControlResponseEventContainer()->GetValue(pEventArg->GetRequestId(), pResponseEvent);
974                                 _AppControlManager::GetInstance()->GetAppControlResponseEventContainer()->Remove(pEventArg->GetRequestId());
975                                 delete pResponseEvent;
976                                 SysLog(NID_APP, "pResponseEvent gets deleted, reqId(%d)", pEventArg->GetRequestId());
977                         }
978                         else
979                         {
980                                 SysLog(NID_APP, "Unexpected AppControlResponseType(%d)", pEventArg->GetType());
981                         }
982                 }
983                 else
984                 {
985                         SysLog(NID_APP, "Invalid ResponseListener");
986                 }
987         }
988         else
989         {
990                 SysLog(NID_APP, "Invalid AppControl arguments : arg(0x%x)", &arg);
991         }
992
993 }
994 }}    //Tizen::App