Update change log and spec for wrt-plugins-tizen_0.4.9
[platform/framework/web/wrt-plugins-tizen.git] / src / Application / ApplicationManager.cpp
1 //
2 // Tizen Web Device API
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 #include "ApplicationManager.h"
19
20 #include <cassert>
21 #include <sstream>
22 #include <Commons/Exception.h>
23 #include <Commons/EventReceiver.h>
24 #include <Commons/Regex.h>
25
26 #include "ApplicationInformation.h"
27 #include "ApplicationContext.h"
28 #include "ApplicationControlData.h"
29 #include "ApplicationControl.h"
30 #include "ApplicationCert.h"
31 #include "Application.h"
32
33 #include <app.h>
34
35 #include <dpl/log/log.h>
36
37 // to launch app by aul
38 #include <aul.h>
39
40 // to get package name by appid
41 #include <app_info.h>
42 #include <app_manager.h>
43
44 // To get cert information from package
45 #include <package_manager.h>
46 #include <package_info.h>
47
48 // To get app size and installed time
49 #include <pkgmgr-info.h>
50
51 // To get ppid
52 #include <unistd.h>
53
54 namespace DeviceAPI {
55 namespace Application {
56
57 using namespace DPL;
58 using namespace WrtDeviceApis;
59 using namespace WrtDeviceApis::Commons;
60
61 namespace {
62
63         typedef KeyMultiMap<ApplicationManager*, LaunchAppControlPendingEvent> LaunchAppControlPendingEventMap;
64         static LaunchAppControlPendingEventMap gLaunchAppControlPendingEventMap;
65
66         // Callback from 'app_manager_set_app_context_event_cb'
67         // Used by 'kill'
68         static void app_manager_app_context_event_callback(app_context_h app_context,
69                         app_context_event_e event, void *user_data)
70         {
71                 LogDebug("called");
72                 int ret = 0;
73
74                 if(event != APP_CONTEXT_EVENT_TERMINATED)
75                         return;
76
77                 int pid = 0;
78
79                 ret = app_context_get_pid(app_context, &pid);
80                 if(ret != APP_MANAGER_ERROR_NONE)
81                 {
82                         LogDebug("Fail to get pid of terminated app (" << ret << ")");
83                         return;
84                 }
85
86                 ApplicationManager* appManager = (ApplicationManager*)(user_data);
87                 appManager->invokeManualAnswerKill(pid);
88         }
89
90         // get package name by id
91         static char* getPackageByAppId(const char* appId)
92         {
93                 app_info_h handle;
94                 char* pkgName;
95                 int ret = 0;
96                 
97                 ret = app_manager_get_app_info(appId, &handle);
98                 if (ret < 0)
99                 {
100                         LogDebug("Fail to get appinfo by " << appId);
101                         return NULL;
102                 }
103
104                 ret = app_info_get_package(handle, &pkgName);
105                 if (ret < 0)
106                 {
107                         LogDebug("Fail to get pkgName by " << appId);
108                         pkgName = NULL;
109                 }
110
111                 ret = app_info_destroy(handle);
112                 if (ret < 0)
113                 {
114                         LogDebug("Fail to get destory appinfo");
115                         return NULL;
116                 }
117
118                 return pkgName;
119         }
120
121
122         // Callback of 'app_manager_foreach_app_context'
123         // Used by 'getAppsContext'
124         static bool app_manager_app_context_callback(app_context_h app_context, void *user_data)
125         {
126                 int ret = 0;
127
128                 char *app_id = NULL;
129                 int pid;
130
131                 std::string contextId;
132
133                 if (user_data == NULL)
134                 {
135                         return false;
136                 }
137
138                 ret = app_context_get_app_id(app_context, &app_id);
139                 if((ret != APP_MANAGER_ERROR_NONE) || (app_id == NULL))
140                 {
141                         LogDebug("app_context_get_app_id error (" << ret << ")");
142                         return false;
143                 }
144
145                 ret = app_context_get_pid(app_context, &pid);
146                 if(ret != APP_MANAGER_ERROR_NONE)
147                 {
148                         LogDebug("app_context_get_app_id error (" << ret << ")");
149                         if (app_id)
150                                 free(app_id);
151                         return false;
152                 }
153
154                 std::stringstream sstream;
155                 sstream << pid;
156                 contextId = sstream.str();
157
158                 ApplicationContextPtr appContext(new ApplicationContext());
159                 appContext->setAppId(app_id);
160                 appContext->setContextId(contextId);
161
162                 ApplicationContextArray* appContextArray = (ApplicationContextArray*)user_data;
163
164                 appContextArray->push_back(appContext);
165
166                 if (app_id)
167                         free(app_id);
168
169                 return true;
170         }
171
172         // Callback of 'service_send_launch_request'
173         // Used by 'launchAppControl'
174         static void service_reply_callback(service_h request, service_h reply,
175                         service_result_e result, void *user_data)
176         {
177                 LogDebug("appControlResultCallback called");
178                 LaunchAppControlPendingEventMap::DataKeyType key =
179                                 (LaunchAppControlPendingEventMap::DataKeyType)user_data;
180
181                 LaunchAppControlPendingEvent *pendingEvent = gLaunchAppControlPendingEventMap.getData(key);
182                 if(pendingEvent != NULL)
183                 {
184                         ApplicationManager *application = (ApplicationManager *)pendingEvent->getThisObject();
185                         EventApplicationLaunchAppControlReplyPtr event = pendingEvent->getEvent();
186                         application->invokeManualAnswerLaunchAppControl(request, reply, result, event);
187
188                         delete pendingEvent;
189                         pendingEvent = NULL;
190                         user_data = NULL;
191
192                         gLaunchAppControlPendingEventMap.eraseData(key);
193                 }
194         }
195
196         static bool package_cert_cb(package_info_h handle, package_cert_type_e cert_type, const char *cert_value, void *user_data)
197         {
198                 ApplicationCertPtr cert(new ApplicationCert());
199                 const char* certName = NULL;
200                 
201                 switch(cert_type) {
202                 case PACKAGE_INFO_AUTHOR_ROOT_CERT:
203                         certName = "AUTHOR_ROOT";
204                         break;
205                 case PACKAGE_INFO_AUTHOR_INTERMEDIATE_CERT:
206                         certName = "AUTHOR_INTERMEDIATE";
207                         break;
208                 case PACKAGE_INFO_AUTHOR_SIGNER_CERT:
209                         certName = "AUTHOR_SIGNER";
210                         break;
211                 case PACKAGE_INFO_DISTRIBUTOR_ROOT_CERT:
212                         certName = "DISTRIBUTOR_ROOT";
213                         break;
214                 case PACKAGE_INFO_DISTRIBUTOR_INTERMEDIATE_CERT:
215                         certName = "DISTRIBUTOR_INTERMEDIATE";
216                         break;
217                 case PACKAGE_INFO_DISTRIBUTOR_SIGNER_CERT:
218                         certName = "DISTRIBUTOR_SIGNER";
219                         break;
220                 case PACKAGE_INFO_DISTRIBUTOR2_ROOT_CERT:
221                         certName = "DISTRIBUTOR2_ROOT";
222                         break;
223                 case PACKAGE_INFO_DISTRIBUTOR2_INTERMEDIATE_CERT:
224                         certName = "DISTRIBUTOR2_INTERMEDIATE";
225                         break;
226                 case PACKAGE_INFO_DISTRIBUTOR2_SIGNER_CERT:
227                         certName = "DISTRIBUTOR2_SIGNER";
228                         break;
229                 default:
230                         LogDebug("Unknow Cert type!!!");
231                         break;
232                 }
233                 
234                 cert->setType(certName);
235                 cert->setValue(cert_value);
236
237                 ApplicationCertArray *certs = (ApplicationCertArray *)user_data;
238                 certs->push_back(cert);
239
240                 return true;
241         }
242
243         
244         static int get_current_app_id(char** app_id)
245         {
246                 LogDebug("get_current_app_id enter");
247                 int parent_pid = getppid();
248                 LogDebug("parent pid : " << parent_pid);
249         
250                 int ret = app_manager_get_app_id(parent_pid, app_id);
251         
252                 if ((ret != APP_MANAGER_ERROR_NONE) || (app_id == NULL))
253                 {
254                         LogDebug("Cannot get app id from pid : " << parent_pid);
255                 }
256                 else
257                 {
258                         LogDebug("appid : " << *app_id);
259                 }
260         
261                 return ret; 
262         }
263         
264         static int category_cb(const char *category, void *user_data)
265         {
266                 //LogDebug("enter");
267                 if (category == NULL)
268                         return true;
269
270                 ApplicationInformation* appInfo = (ApplicationInformation*)user_data;
271                 appInfo->addCategories(category);
272                 return true;
273         }
274
275         // @20130125-wscho: current pkgmanager has a problem while db operation.
276         // So, if enable below code, foreach callback of "pkgmgrinfo_appinfo_get_installed_list"
277         // is not running.
278         // So, temporally, we get version, total size, installed time on first access for that attribute.
279         // See JSApplicationInformation.cpp
280         static void set_additional_app_info(ApplicationInformationPtr &appInfo, const char* packageId)
281         {
282                 int ret = 0;
283                 char *version = NULL;
284                 int total_size = 0;
285                 int installed_time = 0;
286                 pkgmgrinfo_pkginfo_h pkginfo_h;
287
288                 ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &pkginfo_h);
289                 if (ret != PMINFO_R_OK) {
290                         LogDebug("Fail to get package info");
291                 }
292
293                 ret = pkgmgrinfo_pkginfo_get_version(pkginfo_h, &version);
294                 if (ret != PMINFO_R_OK) {
295                         LogDebug("Fail to get version");
296                 } else {
297                         appInfo->setVersion(version);
298                 }
299
300                 ret = pkgmgrinfo_pkginfo_get_total_size(pkginfo_h, &total_size);
301                 if (ret != PMINFO_R_OK) {
302                         LogDebug("Fail to get total size");
303                 } else {
304                         appInfo->setInstallSize(total_size);
305                 }
306
307                 ret = pkgmgrinfo_pkginfo_get_installed_time(pkginfo_h, &installed_time);
308                 if (ret != PMINFO_R_OK) {
309                         LogDebug("Fail to get installed time");
310                 } else {
311                         appInfo->setInstallDate(installed_time);
312                 }
313
314                 ret = pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
315                 if (ret != PMINFO_R_OK) {
316                         LogDebug("Fail to get destroy pkginfo");
317                 }
318                 LogDebug("6");
319         }
320
321         static ApplicationInformationPtr create_app_info(pkgmgrinfo_appinfo_h handle)
322         {
323                 //LogDebug("enter");
324                 char* appId = NULL;
325                 //char* packageId = NULL;
326                 char* name = NULL;
327                 char* iconPath = NULL;  
328                 bool noDisplay = false;
329                 char* pkgId = NULL;
330                 int ret = 0;
331         
332                 ApplicationInformationPtr appInfo(new ApplicationInformation());
333                 ret = pkgmgrinfo_appinfo_get_appid(handle, &appId);
334                 if (ret != PMINFO_R_OK) {
335                         LogDebug("Fail to get name by " << appId);
336                 } else {
337                         appInfo->setAppId(appId);
338                 }
339
340                 ret = pkgmgrinfo_appinfo_get_label(handle, &name);
341                 if (ret != PMINFO_R_OK) {
342                         LogDebug("Fail to get name by " << appId);
343                 } else {
344                         appInfo->setName(name);
345                 }
346
347                 ret = pkgmgrinfo_appinfo_get_icon(handle, &iconPath);
348                 if (ret != PMINFO_R_OK) {
349                         LogDebug("Fail to get icon by " << appId);
350                 } else {
351                         appInfo->setIconPath(iconPath);
352                 }
353
354                 ret = pkgmgrinfo_appinfo_is_nodisplay(handle, &noDisplay);
355                 if (ret != PMINFO_R_OK) {
356                         LogDebug("Fail to get nodisplay by " << appId);
357                 } else {
358                         appInfo->setShow(!noDisplay);
359                 }
360
361                 ret = pkgmgrinfo_appinfo_foreach_category(handle, category_cb, (void*)appInfo.Get());
362                 if (ret != PMINFO_R_OK) {
363                         LogDebug("Fail to get categories " << appId);
364                 }
365
366                 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
367                 if (ret != PMINFO_R_OK) {
368                         LogDebug("Fail to get pkg Id by " << appId);
369                 } else {
370                         appInfo->setPackageId(pkgId);
371                 }
372
373
374 // @20130125-wscho: current pkgmanager has a problem while db operation.
375 // So, if enable below code, foreach callback of "pkgmgrinfo_appinfo_get_installed_list"
376 // is not running.
377 // So, temporally, we get version, total size, installed time on first access for that attribute.
378 // See JSApplicationInformation.cpp
379 #if 0
380                 ret = pkgmgrinfo_appinfo_get_pkgname(handle, &packageId);
381                 if (ret != PMINFO_R_OK)
382                 {
383                         LogDebug("Fail to get package id with appId : " << appId);
384                         return appInfo;
385                 }
386
387                 LogDebug("package id : " << packageId);
388
389                 set_additional_app_info(appInfo, packageId);
390 #endif
391
392                 return appInfo;
393                 
394         }
395         
396         
397         static int installed_app_info_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
398         {
399                 //LogDebug("enter");
400                 ApplicationInformationPtr appInfo = create_app_info(handle);
401                 ApplicationInformationArray *appInfoArray = (ApplicationInformationArray*)user_data;
402                 appInfoArray->push_back(appInfo);
403                 return 0;
404         }
405
406         // Callback from 'service_foreach_app_matched'
407         // Used by 'findAppControl'
408         static bool service_app_matched_callback(service_h service, const char *appid, void *user_data)
409         {
410                 if(appid == NULL)
411                 {
412                         LogWarning("appid is NULL");
413                         return false;
414                 }
415                 //ApplicationInformationPtr appInfo(new ApplicationInformation(appid));
416                 pkgmgrinfo_appinfo_h handle;
417                 int ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
418                 if (ret != PMINFO_R_OK) {
419                         LogWarning("Fail to get appInfo from appId : " << appid);
420                 }
421                 ApplicationInformationPtr appInfo = create_app_info(handle);
422                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
423
424                 ApplicationInformationArray *appInfos = (ApplicationInformationArray *)user_data;
425                 appInfos->push_back(appInfo);
426
427                 return true;
428         }
429
430 }
431
432 ApplicationManager::ApplicationManager() :
433         m_initialized(false),
434         m_app(NULL)
435 {
436         
437 }
438
439 ApplicationManager::~ApplicationManager() 
440 {
441         if(m_installedApplicationsEmitters.size() != 0)
442         {
443                 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
444                 WatchIdMap::iterator iter = m_watchIdMap.begin();
445                 for(; iter != m_watchIdMap.end(); iter++)
446                 {
447                         m_installedApplicationsEmitters.detach(iter->second);
448                 }
449         }
450
451         LaunchAppControlPendingEventMap::DataPtrListType dataPtrList =
452                         gLaunchAppControlPendingEventMap.getDataPtrList(this);
453
454         LaunchAppControlPendingEventMap::DataPtrListType::iterator iter = dataPtrList.begin();
455         for(; iter != dataPtrList.end(); iter++)
456         {
457                 delete *iter;
458         }
459
460         gLaunchAppControlPendingEventMap.eraseKey(this);
461 }
462
463 void ApplicationManager::getCurrentApplication(const EventApplicationGetCurrAppPtr& event)
464 {
465         if (m_initialized == false) {
466                 initialize();
467         }
468
469         EventRequestReceiver<EventApplicationGetCurrApp>::PostRequest(event);
470 }
471
472 void ApplicationManager::launch(const EventApplicationLaunchPtr& event)
473 {
474         if (m_initialized == false) {
475                 initialize();
476         }
477
478         EventRequestReceiver<EventApplicationLaunch>::PostRequest(event);
479 }
480
481 void ApplicationManager::kill(const EventApplicationKillPtr& event)
482 {
483         if (m_initialized == false) {
484                 initialize();
485         }
486
487         EventRequestReceiver<EventApplicationKill>::PostRequest(event);
488 }
489
490 void ApplicationManager::launchAppControl(const EventApplicationLaunchAppControlPtr& event)
491 {
492         if (m_initialized == false) {
493                 initialize();
494         }
495
496         EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
497         if(eventReply != NULL)
498                 EventRequestReceiver<EventApplicationLaunchAppControlReply>::PostRequest(eventReply);
499
500         EventRequestReceiver<EventApplicationLaunchAppControl>::PostRequest(event);
501 }
502
503 void ApplicationManager::findAppControl(const EventApplicationFindAppControlPtr& event)
504 {
505         if (m_initialized == false) {
506                 initialize();
507         }
508
509         EventRequestReceiver<EventApplicationFindAppControl>::PostRequest(event);
510 }
511
512 void ApplicationManager::getAppsContext(const EventApplicationGetAppsContextPtr& event)
513 {
514         if (m_initialized == false) {
515                 initialize();
516         }
517
518         EventRequestReceiver<EventApplicationGetAppsContext>::PostRequest(event);
519 }
520
521 void ApplicationManager::getAppContext(const EventApplicationGetAppContextPtr& event)
522 {
523         if (m_initialized == false) {
524                 initialize();
525         }
526
527         EventRequestReceiver<EventApplicationGetAppContext>::PostRequest(event);
528 }
529
530 void ApplicationManager::getAppsInfo(const EventApplicationGetAppsInfoPtr& event)
531 {
532         if (m_initialized == false) {
533                 initialize();
534         }
535
536         EventRequestReceiver<EventApplicationGetAppsInfo>::PostRequest(event);
537 }
538
539 void ApplicationManager::getAppInfo(const EventApplicationGetAppInfoPtr& event)
540 {
541         if (m_initialized == false) {
542                 initialize();
543         }
544
545         EventRequestReceiver<EventApplicationGetAppInfo>::PostRequest(event);
546 }
547
548 void ApplicationManager::addAppInfoEventListener(const EventApplicationAddAppInfoEventListenerPtr& event)
549 {
550         if (m_initialized == false) {
551                 initialize();
552         }
553
554         EventRequestReceiver<EventApplicationAddAppInfoEventListener>::PostRequest(event);
555 }
556
557 void ApplicationManager::removeAppInfoEventListener(const EventApplicationRemoveAppInfoEventListenerPtr& event)
558 {
559         if (m_initialized == false) {
560                 initialize();
561         }
562
563         EventRequestReceiver<EventApplicationRemoveAppInfoEventListener>::PostRequest(event);
564 }
565
566 void ApplicationManager::getAppCerts(const EventApplicationGetAppCertsPtr& event)
567 {
568         LogError("ApplicationManager::getAppCerts");
569         if (m_initialized == false) {
570                 initialize();
571         }
572
573         EventRequestReceiver<EventApplicationGetAppCerts>::PostRequest(event);
574 }
575
576
577 void ApplicationManager::invokeManualAnswerLaunchAppControl(service_h request, service_h reply,
578                 service_result_e result,
579                 EventApplicationLaunchAppControlReplyPtr &event)
580 {
581         if (event == NULL) {
582                 return;
583         }
584
585         if(result == SERVICE_RESULT_SUCCEEDED)
586         {
587                 LogDebug("SERVICE_RESULT_SUCCEEDED in serviceResultCallback");
588
589                 // create new service object to store result.
590                 ApplicationControlDataArrayPtr appControlDataArray(new ApplicationControlDataArray());
591
592                 int result = service_foreach_extra_data(reply, service_extra_data_callback, appControlDataArray.Get());
593                 if( result == SERVICE_ERROR_NONE)
594                 {
595                         event->setAppControlDataArray(appControlDataArray);
596                         LogDebug("service_foreach_extra_data() success");
597                 }
598                 else
599                 {
600                         LogDebug("service_foreach_extra_data() failed");
601                         event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
602                 }
603         }
604         else if(result == SERVICE_RESULT_FAILED || result == SERVICE_RESULT_CANCELED)
605         {
606                 LogDebug("SERVICE_RESULT_FAILED in serviceResultCallback : " <<result);
607                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
608         }
609
610         EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(event);
611 }
612
613 void ApplicationManager::invokeManualAnswerKill(int pid)
614 {
615         DPL::Mutex::ScopedLock lock(&m_killMapLock);
616
617         std::map<int, EventApplicationKillPtr>::iterator it = m_killEventMap.find(pid);
618         if (it == m_killEventMap.end())
619                 return;
620
621         EventApplicationKillPtr event = it->second;
622         m_killEventMap.erase(it);
623
624         LogDebug("Calling kill success callback for pid : " << pid);
625
626         EventRequestReceiver<EventApplicationKill>::ManualAnswer(event);
627 }
628
629
630 bool ApplicationManager::service_extra_data_callback(service_h service, const char *key, void* user_data)
631 {
632         int ret = 0;
633
634         ApplicationControlDataArray* appControlDataArray = (ApplicationControlDataArray*)user_data;
635         LogDebug("      key      : " << key);
636
637         bool isArray = false;
638         ret = service_is_extra_data_array(service, key, &isArray);
639         if (ret != SERVICE_ERROR_NONE)
640         {
641                 LogDebug("service_is_extra_data_array passes error");
642                 // fail to checking. go to next extra data.
643                 return true;
644         }
645
646         std::string keyStr(key);
647
648         if(isArray)
649         {
650                 int length = 0;
651                 char **value = NULL;
652
653                 ret = service_get_extra_data_array(service, key, &value, &length);
654                 switch(ret)
655                 {
656                 case SERVICE_ERROR_NONE: {
657                         LogDebug("service_get_extra_data success");
658                         std::vector<std::string> valArray;
659                         LogDebug("value length : " << length);
660                         for (int i = 0; i < length; i++)
661                         {
662                                 LogDebug("value            : " << value[i]);
663                                 if(value[i])
664                                 {
665                                         valArray.push_back(value[i]);
666                                 }
667                         }
668
669                         ApplicationControlDataPtr appControlData(new ApplicationControlData());
670                         appControlData->setKey(keyStr);
671                         appControlData->setValue(valArray);
672                         appControlDataArray->push_back(appControlData);
673
674                         for (int i = 0; i < length; i++)
675                         {
676                                 if (value[i])
677                                         free(value[i]);
678                         }
679                         if (value)
680                                 free(value);
681                         break;
682                 }
683                 case SERVICE_ERROR_INVALID_PARAMETER:
684                         LogDebug("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
685                         break;
686                 case SERVICE_ERROR_KEY_NOT_FOUND:
687                         LogDebug("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
688                         break;
689                 case SERVICE_ERROR_OUT_OF_MEMORY:
690                         LogDebug("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
691                         break;
692                 default:
693                         LogDebug("service_get_extra_data retuns Error");
694                         break;
695                 }                       
696         }
697         else // (!isArray)
698         {
699                 char *value = NULL;
700
701                 ret = service_get_extra_data(service, key, &value);
702                 switch (ret)
703                 {
704                 case SERVICE_ERROR_NONE:
705                 {
706                         if(value == NULL)
707                         {
708                                 LogDebug("service_get_extra_data returns NULL");
709                                 break;
710                         }
711                         LogDebug("service_get_extra_data success");
712                         LogDebug("key = "<<key<<" value = "<<value);
713                         std::vector<std::string> valArray;
714                         valArray.push_back(value);
715
716                         ApplicationControlDataPtr appControlData(new ApplicationControlData());
717                         appControlData->setKey(keyStr);
718                         appControlData->setValue(valArray);
719                         appControlDataArray->push_back(appControlData);
720
721                         if (value)
722                                 free(value);
723
724                         break;
725                 }
726                 case SERVICE_ERROR_INVALID_PARAMETER:
727                         LogDebug("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
728                         break;
729                 case SERVICE_ERROR_KEY_NOT_FOUND:
730                         LogDebug("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
731                         break;
732                 case SERVICE_ERROR_OUT_OF_MEMORY:
733                         LogDebug("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
734                         break;
735                 default:
736                         LogDebug("service_get_extra_data retuns Error");
737                         break;
738                 }
739         }
740         
741         return true;
742 }
743
744
745 void ApplicationManager::OnRequestReceived(const EventApplicationGetCurrAppPtr& event)
746 {
747         LogDebug("entered");
748
749         if (m_app != NULL) {
750                 LogDebug("CurrentApplicaton Object is already created.");
751                 event->setApp(m_app);
752                 return;
753         }
754
755         Try
756         {
757                 std::string appId;
758
759                 // get package info of current app
760                 {
761                         char *app_id = NULL;
762
763                         //int ret = app_get_id(&app_id);
764                         int ret = get_current_app_id(&app_id);
765                         if((ret != APP_ERROR_NONE) || (app_id == NULL))
766                         {
767                                 LogError("Can not get app id from current pid (" << ret << ")");
768                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
769                                 return;
770                         }
771                         appId = app_id;
772
773                         free(app_id);
774                 }
775
776                 //ApplicationInformationPtr appinfo(new ApplicationInformation(appId));
777                 pkgmgrinfo_appinfo_h handle;
778                 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
779                 if (ret != PMINFO_R_OK) {
780                         LogWarning("Fail to get appInfo from appId : " << appId);
781                         
782                 }
783                 ApplicationInformationPtr appInfo = create_app_info(handle);
784                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
785
786
787                 ApplicationPtr app(new Application());
788                 app->setAppInfo(appInfo);
789
790                 LogDebug("set appinfo to application");
791                 {
792                         //int pid = getpid();
793                         int pid = getppid();
794                         std::stringstream sstr;
795                         sstr << pid;
796                         app->setContextId(sstr.str());
797
798                         LogError("set pid : " << pid);
799                 }
800
801                 event->setApp(app);
802                 LogDebug("set application to event ");
803                 m_app = app;
804                 
805         }
806         Catch (WrtDeviceApis::Commons::Exception)
807         {
808                 LogError("Error on getAppInfo : " << _rethrown_exception.GetMessage());
809                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
810         }
811
812 }
813
814
815 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchPtr& event)
816 {
817         LogDebug("entered");
818
819         Try
820         {
821                 int ret;
822                 int retry = 0;
823
824                 std::string appId = event->getAppId();
825                 if(appId.empty())
826                 {
827                         LogError("App id is empty");
828                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
829                         return;
830                 }
831
832                 LogDebug("Launching app id : " << appId);
833
834 // if the application is running, send raise event to the app instead of reset the application.
835 #if 1
836                 // give a second chance to launch application to avoid platform issue.
837                 // this retry code will be removed after platform code change.
838                 while (retry < 3) {
839                         ret = aul_open_app(appId.c_str());
840                         if (ret >= 0) {
841                                 break;
842                         }
843                         // delay 300ms for each retry
844                         usleep(300 * 1000);
845                         retry++;
846                         LogDebug("retry launch request : " << retry);
847                 }
848
849                 if (ret < 0) {
850                         switch (ret)
851                         {
852                         case AUL_R_EINVAL:
853                         case AUL_R_ERROR:       
854                                 LogDebug("aul_open_app returns Not Found error");
855                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);                            
856                                 break;
857                         case AUL_R_ECOMM:
858                                 LogDebug("aul_open_app returns internal IPC error");
859                                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);                            
860                                 break;
861                         default:
862                                 LogDebug("aul_open_app returns Unknown error");
863                                 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);                             
864                                 break;
865                         }
866                 } else {
867                         LogDebug("callee's pid " << ret);
868                 }
869 #else
870                 service_h service;
871                 service_create(&service);
872
873                 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
874
875                 service_set_app_id(service, appId.c_str());
876
877                 // give a second chance to launch application to avoid platform issue.
878                 // this retry code will be removed after platform code change.
879                 while (retry < 3) {
880                         ret = service_send_launch_request(service, NULL, this);
881                         if (ret != SERVICE_ERROR_LAUNCH_REJECTED) {
882                                 break;
883                         }
884                         // delay 300ms for each retry
885                         usleep(300 * 1000);
886                         retry++;
887                         LogDebug("retry launch request : " << retry);
888                 }
889
890                 service_destroy(service);
891                 
892                 if(ret != SERVICE_ERROR_NONE)
893                 {
894                         switch (ret)
895                         {
896                         case SERVICE_ERROR_INVALID_PARAMETER:
897                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
898                                 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
899                                 break;
900                         case SERVICE_ERROR_OUT_OF_MEMORY:
901                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_OUT_OF_MEMORY");
902                                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
903                                 break;
904                         case SERVICE_ERROR_LAUNCH_REJECTED:
905                         case SERVICE_ERROR_APP_NOT_FOUND:
906                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_APP_NOT_FOUND");
907                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
908                                 break;
909                         default:
910                                 LogDebug("service_send_launch_request returns UNKNOWN ERROR!!! (" << ret << ")");
911                                 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
912                                 break;
913                         }
914                 }
915 #endif
916
917
918         }
919         Catch (WrtDeviceApis::Commons::Exception)
920         {
921                 LogError("Error on launch : " << _rethrown_exception.GetMessage());
922                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
923         }
924 }
925
926 void ApplicationManager::OnRequestReceived(const EventApplicationKillPtr& event)
927 {
928         LogDebug("entered");
929
930         Try
931         {
932                 int ret;
933                 std::string contextId = event->getContextId();
934
935                 if(contextId.empty())
936                 {
937                         LogError("Context id is empty");
938                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
939                         return;
940                 }
941
942                 LogDebug("Killing context id : " << contextId);
943
944                 int pid;
945                 std::stringstream(contextId) >> pid;
946                 if(pid <= 0)
947                 {
948                         LogError("Context id is wrong : " << pid);
949                         event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
950                         return;
951                 }
952
953                 char *appIdCStr = NULL;
954                 ret = app_manager_get_app_id(pid, &appIdCStr);
955                 if (ret != APP_MANAGER_ERROR_NONE)
956                 {
957                         LogError("Error while getting app id (" << ret << ")");
958                         event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
959                         return;
960                 }
961
962                 std::string appId = appIdCStr;
963                 free(appIdCStr);
964
965                 app_context_h appContext;
966                 ret = app_manager_get_app_context (appId.c_str(), &appContext);
967                 if (ret != APP_MANAGER_ERROR_NONE)
968                 {
969                         LogError("Error while getting app context (" << ret << ")");
970                         event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
971                         return;
972                 }
973
974                 // TODO thread
975                 ret = app_manager_set_app_context_event_cb(app_manager_app_context_event_callback, this);
976                 if (ret != APP_MANAGER_ERROR_NONE)
977                 {
978                         LogError("Error while registering app context event (" << ret << ")");
979                         event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
980                         return;
981                 }
982
983                 ret = app_manager_terminate_app(appContext);
984                 if (ret != APP_MANAGER_ERROR_NONE)
985                 {
986                         LogError("Error while terminating app (" << ret << ")");
987                         event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
988                         return;
989                 }
990
991                 DPL::Mutex::ScopedLock lock(&m_killMapLock);
992                 m_killEventMap[pid] = event;
993                 event->switchToManualAnswer();
994         }
995         Catch (WrtDeviceApis::Commons::Exception)
996         {
997                 LogError("Error on kill : " << _rethrown_exception.GetMessage());
998                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
999         }
1000 }
1001
1002 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlPtr& event)
1003 {
1004         LogDebug("entered");
1005
1006         Try
1007         {
1008                 int ret = 0;
1009                 int retry = 0;
1010
1011                 ApplicationControlPtr appControl = event->getAppControl();
1012                 if(appControl == NULL)
1013                 {
1014                         LogError("appControl is NULL");
1015                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1016                         return;
1017                 }
1018
1019                 std::string operation = appControl->getOperation();
1020                 if(operation.empty())
1021                 {
1022                         LogDebug("operation being madatory is empty");
1023                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1024                         return;
1025                 }
1026
1027                 std::string appId = event->getAppId();
1028
1029                 service_h service;
1030                 service_create(&service);
1031
1032                 if (!appId.empty())
1033                 {
1034                         LogDebug(" app id    : " << appId);
1035
1036                         service_set_app_id(service, appId.c_str());
1037                 }
1038
1039                 service_set_operation(service, operation.c_str() );
1040                 LogDebug(" operation : " << operation);
1041
1042                 std::string uri = appControl->getUri();
1043                 if (!uri.empty())
1044                 {
1045                         LogDebug(" uri       : " << uri);
1046                         service_set_uri(service, uri.c_str() );
1047                 }
1048
1049                 std::string mime = appControl->getMime();
1050                 if (!mime.empty())
1051                 {
1052                         LogDebug(" mime      : " << mime);
1053                         service_set_mime(service, mime.c_str() );
1054                 }
1055
1056                 std::string category = appControl->getCategory();
1057                 if (!category.empty())
1058                 {
1059                         LogDebug(" category  : " << category);
1060                         service_set_category(service, category.c_str() );
1061                 }
1062
1063                 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1064
1065                 if(!appControlDataArray.empty())
1066                 {
1067                         LogDebug(" datas     : " << appControlDataArray.size());
1068
1069                         ApplicationControlDataArray::iterator iter;
1070                         for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1071                         {
1072                                 ApplicationControlDataPtr appControlData = *iter;
1073
1074                                 std::string key = appControlData->getKey();
1075                                 LogDebug("  key      : " << key);
1076
1077                                 if(key.empty())
1078                                 {
1079                                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1080                                         continue;
1081                                 }
1082
1083                                 std::vector<std::string> valueArray = appControlData->getValue();
1084                                 size_t size = valueArray.size();
1085
1086                                 const char **arr = (const char**)calloc(sizeof(char*), size);
1087
1088                                 for (size_t j = 0; j < size; j++)
1089                                 {
1090                                         arr[j] = valueArray.at(j).c_str();
1091                                         LogDebug("  value    : " << arr[j]);
1092                                 }
1093
1094                                 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1095                                 const char *keyCStr = key.c_str();
1096                                 if (size == 1) {
1097                                         service_add_extra_data(service, keyCStr, arr[0]);
1098                                 } else {
1099                                         service_add_extra_data_array(service, keyCStr, arr, size);
1100                                 }
1101
1102                                 if (arr)
1103                                         free(arr);
1104                         }
1105                 }
1106
1107                 LaunchAppControlPendingEvent *pendingEvent = NULL;
1108                 LaunchAppControlPendingEventMap::DataKeyType key = 0;
1109
1110                 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
1111                 if(eventReply)
1112                 {
1113                         pendingEvent = new LaunchAppControlPendingEvent((void*)this, eventReply);
1114                         key = gLaunchAppControlPendingEventMap.insert(this, pendingEvent);
1115                 }
1116
1117                 // give a second chance to launch application to avoid platform issue.
1118                 // this retry code will be removed after platform code change.
1119                 while (retry < 3) {
1120                         ret = service_send_launch_request(service, service_reply_callback, (void *)key);
1121                         if (ret != SERVICE_ERROR_LAUNCH_REJECTED) {
1122                                 break;
1123                         }
1124                         // delay 300ms for each retry
1125                         usleep(300 * 1000);
1126                         retry++;
1127                         LogDebug("retry launch request : " << retry);
1128                 }
1129
1130                 service_destroy(service);
1131
1132                 if(ret != SERVICE_ERROR_NONE)
1133                 {
1134                         switch (ret)
1135                         {
1136                         case SERVICE_ERROR_INVALID_PARAMETER:
1137                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
1138                                 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1139                                 break;
1140                         case SERVICE_ERROR_OUT_OF_MEMORY:
1141                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_OUT_OF_MEMORY");
1142                                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1143                                 break;
1144                         case SERVICE_ERROR_LAUNCH_REJECTED:
1145                         case SERVICE_ERROR_APP_NOT_FOUND:
1146                                 LogDebug("service_send_launch_request returns SERVICE_ERROR_APP_NOT_FOUND");
1147                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1148                                 break;
1149                         default:
1150                                 LogDebug("service_send_launch_request returns UNKNOWN ERROR!!!");
1151                                 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
1152                                 break;
1153                         }
1154
1155                         if(pendingEvent)
1156                         {
1157                                 gLaunchAppControlPendingEventMap.eraseData(key);
1158
1159                                 delete pendingEvent;
1160                                 pendingEvent = NULL;
1161
1162                                 eventReply->cancelRequest();
1163                                 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(eventReply);
1164                         }
1165                 }
1166         }
1167         Catch (WrtDeviceApis::Commons::Exception)
1168         {
1169                 LogError("Error on launchAppControl : " << _rethrown_exception.GetMessage());
1170                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1171         }
1172 }
1173
1174 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlReplyPtr& event)
1175 {
1176         LogDebug("entered");
1177         event->switchToManualAnswer();
1178 }
1179
1180 void ApplicationManager::OnRequestReceived(const EventApplicationFindAppControlPtr& event)
1181 {
1182         LogDebug("entered");
1183
1184         Try
1185         {
1186                 ApplicationControlPtr appControl = event->getAppControl();
1187                 if(appControl == NULL)
1188                 {
1189                         LogError("appControl is NULL");
1190                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1191                         return;
1192                 }
1193
1194                 std::string operation = appControl->getOperation();
1195                 if(operation.empty())
1196                 {
1197                         LogDebug("operation being madatory is empty");
1198                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1199                         return;
1200                 }
1201
1202                 service_h service;
1203                 service_create(&service);
1204
1205                 service_set_operation(service, operation.c_str() );
1206                 LogDebug(" operation : " << operation);
1207
1208                 std::string uri = appControl->getUri();
1209                 if (!uri.empty())
1210                 {
1211                         LogDebug(" uri       : " << uri);
1212                         service_set_uri(service, uri.c_str() );
1213                 }
1214
1215                 std::string mime = appControl->getMime();
1216                 if (!mime.empty())
1217                 {
1218                         LogDebug(" mime      : " << mime);
1219                         service_set_mime(service, mime.c_str() );
1220                 }
1221
1222                 std::string category = appControl->getCategory();
1223                 if (!category.empty())
1224                 {
1225                         LogDebug(" category  : " << category);
1226                         service_set_category(service, category.c_str() );
1227                 }
1228
1229                 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1230
1231                 if(!appControlDataArray.empty())
1232                 {
1233                         LogDebug(" datas     : " << appControlDataArray.size());
1234
1235                         ApplicationControlDataArray::iterator iter;
1236                         for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1237                         {
1238                                 ApplicationControlDataPtr appControlData = *iter;
1239
1240                                 std::string key = appControlData->getKey();
1241                                 LogDebug("  key      : " << key);
1242
1243                                 if(key.empty())
1244                                 {
1245                                         event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1246                                         continue;
1247                                 }
1248
1249                                 std::vector<std::string> valueArray = appControlData->getValue();
1250                                 size_t size = valueArray.size();
1251
1252                                 const char **arr = (const char**)calloc(sizeof(char*), size);
1253
1254                                 for (size_t j = 0; j < size; j++)
1255                                 {
1256                                         arr[j] = valueArray.at(j).c_str();
1257                                         LogDebug("  value    : " << arr[j]);
1258                                 }
1259
1260                                 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1261                                 const char *keyCStr = key.c_str();
1262                                 if (size == 1) {
1263                                         service_add_extra_data(service, keyCStr, arr[0]);
1264                                 } else {
1265                                         service_add_extra_data_array(service, keyCStr, arr, size);
1266                                 }
1267
1268                                 if (arr)
1269                                         free(arr);
1270                         }
1271                 }
1272
1273                 ApplicationInformationArrayPtr appInfos(new ApplicationInformationArray());
1274
1275                 int result = service_foreach_app_matched(service, service_app_matched_callback, (void *)appInfos.Get());
1276                 if (result != SERVICE_ERROR_NONE)
1277                 {
1278                         LogError("service_foreach_app_matched error (" << result << ")");
1279                         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1280                         service_destroy(service);
1281                         return;
1282                 }
1283
1284                 service_destroy(service);
1285
1286                 event->setAppInfos(appInfos);
1287         }
1288         Catch (WrtDeviceApis::Commons::Exception)
1289         {
1290                 LogError("Error on findAppControl : " << _rethrown_exception.GetMessage());
1291                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1292         }
1293 }
1294
1295
1296 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsContextPtr& event)
1297 {
1298         LogDebug("entered");
1299
1300         Try
1301         {
1302                 int ret = 0;
1303
1304                 ApplicationContextArrayPtr appContextArray = event->getAppContextArray();
1305                 ret = app_manager_foreach_app_context(app_manager_app_context_callback, appContextArray.Get());
1306                 if(ret != APP_MANAGER_ERROR_NONE)
1307                 {
1308                         LogError("app_manager_foreach_app_context error (" << ret << ")");
1309                         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1310                 }
1311         }
1312         Catch (WrtDeviceApis::Commons::Exception)
1313         {
1314                 LogError("Error on  : " << _rethrown_exception.GetMessage());
1315                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1316         }
1317 }
1318
1319 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppContextPtr& event)
1320 {
1321         LogDebug("entered");
1322
1323         Try
1324         {
1325                 int ret = 0;
1326
1327                 std::string contextId = event->getAppContextId();
1328                 int pid;
1329
1330                 if (contextId.empty())
1331                 {
1332                         //pid = getpid();
1333                         pid = getppid();
1334
1335                         std::stringstream sstr;
1336                         sstr << pid;
1337                         contextId = sstr.str();
1338                 }
1339                 else
1340                 {
1341                         std::stringstream(contextId) >> pid;
1342                         if (pid <= 0)
1343                         {
1344                                 LogError("contextId is wrong : " << contextId);
1345                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1346                                 return;
1347                         }
1348                 }
1349
1350                 char *app_id = NULL;
1351
1352                 ret = app_manager_get_app_id(pid, &app_id);
1353                 if(ret != APP_MANAGER_ERROR_NONE)
1354                 {
1355                         switch(ret)
1356                         {
1357                         case APP_MANAGER_ERROR_NO_SUCH_APP:
1358                                 LogError("app_manager_get_app_id error : no such app");
1359                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1360                                 break;
1361                         case APP_MANAGER_ERROR_INVALID_PARAMETER:
1362                                 LogError("app_manager_get_app_id error : invalid parameter");
1363                                 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1364                                 break;
1365                         default:
1366                                 LogError("app_manager_get_app_id error (" << ret << ")");
1367                                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1368                                 break;
1369                         }
1370
1371                         if(app_id)
1372                                 free(app_id);
1373
1374                         return;
1375                 }
1376
1377                 ApplicationContextPtr appContext(new ApplicationContext());
1378                 appContext->setAppId(app_id);
1379                 appContext->setContextId(contextId);
1380
1381                 event->setAppContext(appContext);
1382
1383                 if(app_id)
1384                         free(app_id);
1385         }
1386         Catch (WrtDeviceApis::Commons::Exception)
1387         {
1388                 LogError("Error on getAppContext : " << _rethrown_exception.GetMessage());
1389                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1390         }
1391 }
1392
1393 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsInfoPtr& event)
1394 {
1395         LogDebug("entered");
1396
1397         Try
1398         {
1399                 int ret = 0;
1400                 ApplicationInformationArrayPtr appInfoArray = event->getAppInfoArray();
1401                 ret = pkgmgrinfo_appinfo_get_installed_list(installed_app_info_cb, (void*)appInfoArray.Get());
1402                 if (ret != PMINFO_R_OK) {
1403                         LogError("Error on getAppsInfo : ");
1404                         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1405                 }
1406         }
1407         Catch (WrtDeviceApis::Commons::Exception)
1408         {
1409                 LogError("Error on getAppsInfo : " << _rethrown_exception.GetMessage());
1410                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1411         }
1412 }
1413
1414 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppInfoPtr& event)
1415 {
1416         LogDebug("entered");
1417
1418         Try
1419         {
1420                 std::string appId = event->getAppId();
1421                 // in case of no argument, get application information of current.
1422                 if (appId.empty())
1423                 {
1424                         char *app_id = NULL;
1425
1426                         //int ret = app_get_id(&app_id);        // webkit2 patch.
1427                         int ret = get_current_app_id(&app_id);
1428                         if((ret != APP_ERROR_NONE) || (app_id == NULL))
1429                         {
1430                                 LogError("Can not get app id from current pid (" << ret << ")");
1431                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1432                                 return;
1433                         }
1434                         appId = app_id;
1435                         free(app_id);
1436                 }
1437
1438                 pkgmgrinfo_appinfo_h handle;
1439                 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
1440                 if (ret != PMINFO_R_OK) {
1441                         event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1442                 }
1443
1444                 ApplicationInformationPtr appInfo = create_app_info(handle);
1445                 event->setAppInfo(appInfo);
1446                 pkgmgrinfo_appinfo_destroy_appinfo(handle);             
1447         }
1448         Catch (WrtDeviceApis::Commons::Exception)
1449         {
1450                 LogError("Error on getAppInfo : " << _rethrown_exception.GetMessage());
1451                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1452         }
1453 }
1454
1455 void ApplicationManager::OnRequestReceived(const EventApplicationAddAppInfoEventListenerPtr& event)
1456 {
1457         LogDebug("entered");
1458
1459         Try
1460         {
1461                 EventApplicationAppInfoEventListenerEmitterPtr emitter = event->getEmitter();
1462
1463                 if(m_installedApplicationsEmitters.size() == 0)
1464                 {
1465                         LogDebug("First time registering event listener to this application object.");
1466
1467                         // Below can throw Exception
1468                         AppManagerWrapperSingleton::Instance().registerAppListChangedCallbacks(this);
1469                 }
1470
1471                 m_installedApplicationsEmitters.attach(emitter);
1472
1473                 long watchId = AppManagerWrapperSingleton::Instance().getWatchIdAndInc();
1474
1475                 m_watchIdMap[watchId] = emitter->getId();
1476
1477                 event->setWatchId(watchId);
1478         }
1479         Catch (WrtDeviceApis::Commons::Exception)
1480         {
1481                 LogError("Error on addAppInfoEventListener : " << _rethrown_exception.GetMessage());
1482                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1483         }
1484 }
1485
1486 void ApplicationManager::OnRequestReceived(const EventApplicationRemoveAppInfoEventListenerPtr& event)
1487 {
1488         LogDebug("entered");
1489
1490         Try
1491         {
1492                 long watchId = event->getWatchId();
1493
1494                 if(m_watchIdMap.find(watchId) == m_watchIdMap.end())
1495                         ThrowMsg(NotFoundException, "No watchId : " << watchId);
1496
1497                 EventApplicationAppInfoEventListenerEmitter::IdType emitterId = m_watchIdMap[watchId];
1498
1499                 bool success = m_installedApplicationsEmitters.detach(emitterId);
1500                 if(!success)
1501                         ThrowMsg(NotFoundException, "No watchId : " << watchId);
1502
1503                 if(m_installedApplicationsEmitters.size() == 0)
1504                 {
1505                         LogDebug("No more event listener on this application object.");
1506
1507                         AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
1508                 }
1509         }
1510         Catch (WrtDeviceApis::Commons::NotFoundException)
1511         {
1512                 LogError("Not found : " << _rethrown_exception.GetMessage());
1513                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1514         }
1515         Catch (WrtDeviceApis::Commons::Exception)
1516         {
1517                 LogError("Error on removeAppInfoEventListener : " << _rethrown_exception.GetMessage());
1518                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1519         }
1520 }
1521
1522
1523 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppCertsPtr& event)
1524 {
1525         LogDebug("entered");
1526
1527         Try
1528         {
1529                 std::string appId = event->getAppId();
1530
1531                 // in case of no argument, get application information of current.
1532                 if (appId.empty())
1533                 {
1534                         char *tmp = NULL;
1535
1536                         //int ret = app_get_id(&tmp);
1537                         int ret = get_current_app_id(&tmp);
1538                         if((ret != APP_ERROR_NONE) || (tmp == NULL))
1539                         {
1540                                 LogError("Can not get app id from current pid (" << ret << ")");
1541                                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1542                                 return;
1543                         }
1544
1545                         appId = tmp;
1546                         if (tmp)
1547                                 free(tmp);
1548                 }
1549
1550                 char* package = getPackageByAppId(appId.c_str());
1551                 if (package == NULL)
1552                 {
1553                         LogError("Can not get package from appId (" << appId << ")");
1554                         event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1555                         return;
1556                 }
1557                 
1558                 LogDebug("package : " << package);
1559                 package_info_h pkg_info;
1560                 int result = 0;
1561                 
1562                 result = package_manager_get_package_info(package, &pkg_info);
1563                 if (result != PACKAGE_MANAGER_ERROR_NONE)
1564                 {
1565                         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1566                         return;
1567                 }       
1568
1569                 ApplicationCertArrayPtr certArray(new ApplicationCertArray());
1570
1571                 result = package_info_foreach_cert_info(pkg_info, package_cert_cb, (void*)certArray.Get());
1572                 if (result != PACKAGE_MANAGER_ERROR_NONE)
1573                 {
1574                         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1575                         return;
1576                 }
1577
1578                 event->setAppCerts(certArray);
1579
1580                 LogDebug("Finish to getting package cert info");
1581         }
1582         Catch (WrtDeviceApis::Commons::Exception)
1583         {
1584                 LogError("Error on getAppInfo : " << _rethrown_exception.GetMessage());
1585                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1586         }
1587 }
1588
1589
1590 void ApplicationManager::onAppManagerEventInstalled(const char *appId)
1591 {
1592         LogDebug("APP_MANAGER_EVENT_INSTALLED, appId : "<< appId);
1593
1594         EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1595
1596         pkgmgrinfo_appinfo_h handle;
1597         int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1598         if (ret != PMINFO_R_OK) {
1599                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1600         }
1601         
1602         ApplicationInformationPtr appInfo = create_app_info(handle);
1603         event->setAppInfo(appInfo);
1604         pkgmgrinfo_appinfo_destroy_appinfo(handle); 
1605
1606         event->setType(EventApplicationAppInfoEventListener::OnInstalled);      
1607         m_installedApplicationsEmitters.emit(event);
1608 }
1609
1610 void ApplicationManager::onAppManagerEventUninstalled(const char *appId)
1611 {
1612         LogDebug("APP_MANAGER_EVENT_UNINSTALLED, appId : "<< appId);
1613
1614         EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1615
1616         ApplicationInformationPtr appInfo(new ApplicationInformation(appId));
1617
1618         event->setType(EventApplicationAppInfoEventListener::OnUninstalled);
1619         event->setAppInfo(appInfo);
1620         m_installedApplicationsEmitters.emit(event);
1621 }
1622
1623 void ApplicationManager::onAppManagerEventUpdated(const char *appId)
1624 {
1625         LogDebug("APP_MANAGER_EVENT_UPDATED, appId : "<< appId);
1626
1627         EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1628
1629         pkgmgrinfo_appinfo_h handle;
1630         int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1631         if (ret != PMINFO_R_OK) {
1632                 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1633         }
1634         
1635         ApplicationInformationPtr appInfo = create_app_info(handle);
1636         event->setAppInfo(appInfo);
1637         pkgmgrinfo_appinfo_destroy_appinfo(handle); 
1638
1639         event->setType(EventApplicationAppInfoEventListener::OnUpdated);        
1640         m_installedApplicationsEmitters.emit(event);
1641 }
1642
1643 void ApplicationManager::initialize() 
1644 {
1645         if (!m_initialized) {
1646                 DPL::Mutex::ScopedLock lock(&m_initializationMutex);
1647                 if (!m_initialized) {
1648                         
1649                 }
1650         }
1651 }
1652
1653 }
1654 }