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