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