2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ApplicationManager.h"
22 #include <Commons/Exception.h>
23 #include <Commons/EventReceiver.h>
24 #include <Commons/Regex.h>
26 #include "ApplicationInformation.h"
27 #include "ApplicationContext.h"
28 #include "ApplicationControlData.h"
29 #include "ApplicationControl.h"
30 #include "ApplicationCert.h"
31 #include "Application.h"
35 #include <dpl/log/log.h>
37 // to launch app by aul
40 // to get package name by appid
42 #include <app_manager.h>
44 // To get cert information from package
45 #include <package_manager.h>
46 #include <package_info.h>
48 // To get app size and installed time
49 #include <pkgmgr-info.h>
55 namespace Application {
58 using namespace WrtDeviceApis;
59 using namespace WrtDeviceApis::Commons;
63 typedef KeyMultiMap<ApplicationManager*, LaunchAppControlPendingEvent> LaunchAppControlPendingEventMap;
64 static LaunchAppControlPendingEventMap gLaunchAppControlPendingEventMap;
66 // Callback from 'app_manager_set_app_context_event_cb'
68 static void app_manager_app_context_event_callback(app_context_h app_context,
69 app_context_event_e event, void *user_data)
74 if(event != APP_CONTEXT_EVENT_TERMINATED)
79 ret = app_context_get_pid(app_context, &pid);
80 if(ret != APP_MANAGER_ERROR_NONE)
82 LogDebug("Fail to get pid of terminated app (" << ret << ")");
86 ApplicationManager* appManager = (ApplicationManager*)(user_data);
87 appManager->invokeManualAnswerKill(pid);
90 // get package name by id
91 static char* getPackageByAppId(const char* appId)
97 ret = app_manager_get_app_info(appId, &handle);
100 LogDebug("Fail to get appinfo by " << appId);
104 ret = app_info_get_package(handle, &pkgName);
107 LogDebug("Fail to get pkgName by " << appId);
111 ret = app_info_destroy(handle);
114 LogDebug("Fail to get destory appinfo");
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)
131 std::string contextId;
133 if (user_data == NULL)
138 ret = app_context_get_app_id(app_context, &app_id);
139 if((ret != APP_MANAGER_ERROR_NONE) || (app_id == NULL))
141 LogDebug("app_context_get_app_id error (" << ret << ")");
145 ret = app_context_get_pid(app_context, &pid);
146 if(ret != APP_MANAGER_ERROR_NONE)
148 LogDebug("app_context_get_app_id error (" << ret << ")");
154 std::stringstream sstream;
156 contextId = sstream.str();
158 ApplicationContextPtr appContext(new ApplicationContext());
159 appContext->setAppId(app_id);
160 appContext->setContextId(contextId);
162 ApplicationContextArray* appContextArray = (ApplicationContextArray*)user_data;
164 appContextArray->push_back(appContext);
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)
177 LogDebug("appControlResultCallback called");
178 LaunchAppControlPendingEventMap::DataKeyType key =
179 (LaunchAppControlPendingEventMap::DataKeyType)user_data;
181 LaunchAppControlPendingEvent *pendingEvent = gLaunchAppControlPendingEventMap.getData(key);
182 if(pendingEvent != NULL)
184 ApplicationManager *application = (ApplicationManager *)pendingEvent->getThisObject();
185 EventApplicationLaunchAppControlReplyPtr event = pendingEvent->getEvent();
186 application->invokeManualAnswerLaunchAppControl(request, reply, result, event);
192 gLaunchAppControlPendingEventMap.eraseData(key);
196 static bool package_cert_cb(package_info_h handle, package_cert_type_e cert_type, const char *cert_value, void *user_data)
198 ApplicationCertPtr cert(new ApplicationCert());
199 const char* certName = NULL;
202 case PACKAGE_INFO_AUTHOR_ROOT_CERT:
203 certName = "AUTHOR_ROOT";
205 case PACKAGE_INFO_AUTHOR_INTERMEDIATE_CERT:
206 certName = "AUTHOR_INTERMEDIATE";
208 case PACKAGE_INFO_AUTHOR_SIGNER_CERT:
209 certName = "AUTHOR_SIGNER";
211 case PACKAGE_INFO_DISTRIBUTOR_ROOT_CERT:
212 certName = "DISTRIBUTOR_ROOT";
214 case PACKAGE_INFO_DISTRIBUTOR_INTERMEDIATE_CERT:
215 certName = "DISTRIBUTOR_INTERMEDIATE";
217 case PACKAGE_INFO_DISTRIBUTOR_SIGNER_CERT:
218 certName = "DISTRIBUTOR_SIGNER";
220 case PACKAGE_INFO_DISTRIBUTOR2_ROOT_CERT:
221 certName = "DISTRIBUTOR2_ROOT";
223 case PACKAGE_INFO_DISTRIBUTOR2_INTERMEDIATE_CERT:
224 certName = "DISTRIBUTOR2_INTERMEDIATE";
226 case PACKAGE_INFO_DISTRIBUTOR2_SIGNER_CERT:
227 certName = "DISTRIBUTOR2_SIGNER";
230 LogDebug("Unknow Cert type!!!");
234 cert->setType(certName);
235 cert->setValue(cert_value);
237 ApplicationCertArray *certs = (ApplicationCertArray *)user_data;
238 certs->push_back(cert);
244 static int get_current_app_id(char** app_id)
246 LogDebug("get_current_app_id enter");
247 int parent_pid = getppid();
248 LogDebug("parent pid : " << parent_pid);
250 int ret = app_manager_get_app_id(parent_pid, app_id);
252 if ((ret != APP_MANAGER_ERROR_NONE) || (app_id == NULL))
254 LogDebug("Cannot get app id from pid : " << parent_pid);
258 LogDebug("appid : " << *app_id);
264 static int category_cb(const char *category, void *user_data)
267 if (category == NULL)
270 ApplicationInformation* appInfo = (ApplicationInformation*)user_data;
271 appInfo->addCategories(category);
275 // @20130125-wscho: current pkgmanager has a problem while db operation.
276 // So, if enable below code, foreach callback of "pkgmgrinfo_appinfo_get_installed_list"
278 // So, temporally, we get version, total size, installed time on first access for that attribute.
279 // See JSApplicationInformation.cpp
280 static void set_additional_app_info(ApplicationInformationPtr &appInfo, const char* packageId)
283 char *version = NULL;
285 int installed_time = 0;
286 pkgmgrinfo_pkginfo_h pkginfo_h;
288 ret = pkgmgrinfo_pkginfo_get_pkginfo(packageId, &pkginfo_h);
289 if (ret != PMINFO_R_OK) {
290 LogDebug("Fail to get package info");
293 ret = pkgmgrinfo_pkginfo_get_version(pkginfo_h, &version);
294 if (ret != PMINFO_R_OK) {
295 LogDebug("Fail to get version");
297 appInfo->setVersion(version);
300 ret = pkgmgrinfo_pkginfo_get_total_size(pkginfo_h, &total_size);
301 if (ret != PMINFO_R_OK) {
302 LogDebug("Fail to get total size");
304 appInfo->setInstallSize(total_size);
307 ret = pkgmgrinfo_pkginfo_get_installed_time(pkginfo_h, &installed_time);
308 if (ret != PMINFO_R_OK) {
309 LogDebug("Fail to get installed time");
311 appInfo->setInstallDate(installed_time);
314 ret = pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
315 if (ret != PMINFO_R_OK) {
316 LogDebug("Fail to get destroy pkginfo");
321 static ApplicationInformationPtr create_app_info(pkgmgrinfo_appinfo_h handle)
325 //char* packageId = NULL;
327 char* iconPath = NULL;
328 bool noDisplay = false;
332 ApplicationInformationPtr appInfo(new ApplicationInformation());
333 ret = pkgmgrinfo_appinfo_get_appid(handle, &appId);
334 if (ret != PMINFO_R_OK) {
335 LogDebug("Fail to get name by " << appId);
337 appInfo->setAppId(appId);
340 ret = pkgmgrinfo_appinfo_get_label(handle, &name);
341 if (ret != PMINFO_R_OK) {
342 LogDebug("Fail to get name by " << appId);
344 appInfo->setName(name);
347 ret = pkgmgrinfo_appinfo_get_icon(handle, &iconPath);
348 if (ret != PMINFO_R_OK) {
349 LogDebug("Fail to get icon by " << appId);
351 appInfo->setIconPath(iconPath);
354 ret = pkgmgrinfo_appinfo_is_nodisplay(handle, &noDisplay);
355 if (ret != PMINFO_R_OK) {
356 LogDebug("Fail to get nodisplay by " << appId);
358 appInfo->setShow(!noDisplay);
361 ret = pkgmgrinfo_appinfo_foreach_category(handle, category_cb, (void*)appInfo.Get());
362 if (ret != PMINFO_R_OK) {
363 LogDebug("Fail to get categories " << appId);
366 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
367 if (ret != PMINFO_R_OK) {
368 LogDebug("Fail to get pkg Id by " << appId);
370 appInfo->setPackageId(pkgId);
374 // @20130125-wscho: current pkgmanager has a problem while db operation.
375 // So, if enable below code, foreach callback of "pkgmgrinfo_appinfo_get_installed_list"
377 // So, temporally, we get version, total size, installed time on first access for that attribute.
378 // See JSApplicationInformation.cpp
380 ret = pkgmgrinfo_appinfo_get_pkgname(handle, &packageId);
381 if (ret != PMINFO_R_OK)
383 LogDebug("Fail to get package id with appId : " << appId);
387 LogDebug("package id : " << packageId);
389 set_additional_app_info(appInfo, packageId);
397 static int installed_app_info_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
400 ApplicationInformationPtr appInfo = create_app_info(handle);
401 ApplicationInformationArray *appInfoArray = (ApplicationInformationArray*)user_data;
402 appInfoArray->push_back(appInfo);
406 // Callback from 'service_foreach_app_matched'
407 // Used by 'findAppControl'
408 static bool service_app_matched_callback(service_h service, const char *appid, void *user_data)
412 LogWarning("appid is NULL");
415 //ApplicationInformationPtr appInfo(new ApplicationInformation(appid));
416 pkgmgrinfo_appinfo_h handle;
417 int ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
418 if (ret != PMINFO_R_OK) {
419 LogWarning("Fail to get appInfo from appId : " << appid);
421 ApplicationInformationPtr appInfo = create_app_info(handle);
422 pkgmgrinfo_appinfo_destroy_appinfo(handle);
424 ApplicationInformationArray *appInfos = (ApplicationInformationArray *)user_data;
425 appInfos->push_back(appInfo);
432 ApplicationManager::ApplicationManager() :
433 m_initialized(false),
439 ApplicationManager::~ApplicationManager()
441 if(m_installedApplicationsEmitters.size() != 0)
443 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
444 WatchIdMap::iterator iter = m_watchIdMap.begin();
445 for(; iter != m_watchIdMap.end(); iter++)
447 m_installedApplicationsEmitters.detach(iter->second);
451 LaunchAppControlPendingEventMap::DataPtrListType dataPtrList =
452 gLaunchAppControlPendingEventMap.getDataPtrList(this);
454 LaunchAppControlPendingEventMap::DataPtrListType::iterator iter = dataPtrList.begin();
455 for(; iter != dataPtrList.end(); iter++)
460 gLaunchAppControlPendingEventMap.eraseKey(this);
463 void ApplicationManager::getCurrentApplication(const EventApplicationGetCurrAppPtr& event)
465 if (m_initialized == false) {
469 EventRequestReceiver<EventApplicationGetCurrApp>::PostRequest(event);
472 void ApplicationManager::launch(const EventApplicationLaunchPtr& event)
474 if (m_initialized == false) {
478 EventRequestReceiver<EventApplicationLaunch>::PostRequest(event);
481 void ApplicationManager::kill(const EventApplicationKillPtr& event)
483 if (m_initialized == false) {
487 EventRequestReceiver<EventApplicationKill>::PostRequest(event);
490 void ApplicationManager::launchAppControl(const EventApplicationLaunchAppControlPtr& event)
492 if (m_initialized == false) {
496 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
497 if(eventReply != NULL)
498 EventRequestReceiver<EventApplicationLaunchAppControlReply>::PostRequest(eventReply);
500 EventRequestReceiver<EventApplicationLaunchAppControl>::PostRequest(event);
503 void ApplicationManager::findAppControl(const EventApplicationFindAppControlPtr& event)
505 if (m_initialized == false) {
509 EventRequestReceiver<EventApplicationFindAppControl>::PostRequest(event);
512 void ApplicationManager::getAppsContext(const EventApplicationGetAppsContextPtr& event)
514 if (m_initialized == false) {
518 EventRequestReceiver<EventApplicationGetAppsContext>::PostRequest(event);
521 void ApplicationManager::getAppContext(const EventApplicationGetAppContextPtr& event)
523 if (m_initialized == false) {
527 EventRequestReceiver<EventApplicationGetAppContext>::PostRequest(event);
530 void ApplicationManager::getAppsInfo(const EventApplicationGetAppsInfoPtr& event)
532 if (m_initialized == false) {
536 EventRequestReceiver<EventApplicationGetAppsInfo>::PostRequest(event);
539 void ApplicationManager::getAppInfo(const EventApplicationGetAppInfoPtr& event)
541 if (m_initialized == false) {
545 EventRequestReceiver<EventApplicationGetAppInfo>::PostRequest(event);
548 void ApplicationManager::addAppInfoEventListener(const EventApplicationAddAppInfoEventListenerPtr& event)
550 if (m_initialized == false) {
554 EventRequestReceiver<EventApplicationAddAppInfoEventListener>::PostRequest(event);
557 void ApplicationManager::removeAppInfoEventListener(const EventApplicationRemoveAppInfoEventListenerPtr& event)
559 if (m_initialized == false) {
563 EventRequestReceiver<EventApplicationRemoveAppInfoEventListener>::PostRequest(event);
566 void ApplicationManager::getAppCerts(const EventApplicationGetAppCertsPtr& event)
568 LogError("ApplicationManager::getAppCerts");
569 if (m_initialized == false) {
573 EventRequestReceiver<EventApplicationGetAppCerts>::PostRequest(event);
577 void ApplicationManager::invokeManualAnswerLaunchAppControl(service_h request, service_h reply,
578 service_result_e result,
579 EventApplicationLaunchAppControlReplyPtr &event)
585 if(result == SERVICE_RESULT_SUCCEEDED)
587 LogDebug("SERVICE_RESULT_SUCCEEDED in serviceResultCallback");
589 // create new service object to store result.
590 ApplicationControlDataArrayPtr appControlDataArray(new ApplicationControlDataArray());
592 int result = service_foreach_extra_data(reply, service_extra_data_callback, appControlDataArray.Get());
593 if( result == SERVICE_ERROR_NONE)
595 event->setAppControlDataArray(appControlDataArray);
596 LogDebug("service_foreach_extra_data() success");
600 LogDebug("service_foreach_extra_data() failed");
601 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
604 else if(result == SERVICE_RESULT_FAILED || result == SERVICE_RESULT_CANCELED)
606 LogDebug("SERVICE_RESULT_FAILED in serviceResultCallback : " <<result);
607 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
610 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(event);
613 void ApplicationManager::invokeManualAnswerKill(int pid)
615 DPL::Mutex::ScopedLock lock(&m_killMapLock);
617 std::map<int, EventApplicationKillPtr>::iterator it = m_killEventMap.find(pid);
618 if (it == m_killEventMap.end())
621 EventApplicationKillPtr event = it->second;
622 m_killEventMap.erase(it);
624 LogDebug("Calling kill success callback for pid : " << pid);
626 EventRequestReceiver<EventApplicationKill>::ManualAnswer(event);
630 bool ApplicationManager::service_extra_data_callback(service_h service, const char *key, void* user_data)
634 ApplicationControlDataArray* appControlDataArray = (ApplicationControlDataArray*)user_data;
635 LogDebug(" key : " << key);
637 bool isArray = false;
638 ret = service_is_extra_data_array(service, key, &isArray);
639 if (ret != SERVICE_ERROR_NONE)
641 LogDebug("service_is_extra_data_array passes error");
642 // fail to checking. go to next extra data.
646 std::string keyStr(key);
653 ret = service_get_extra_data_array(service, key, &value, &length);
656 case SERVICE_ERROR_NONE: {
657 LogDebug("service_get_extra_data success");
658 std::vector<std::string> valArray;
659 LogDebug("value length : " << length);
660 for (int i = 0; i < length; i++)
662 LogDebug("value : " << value[i]);
665 valArray.push_back(value[i]);
669 ApplicationControlDataPtr appControlData(new ApplicationControlData());
670 appControlData->setKey(keyStr);
671 appControlData->setValue(valArray);
672 appControlDataArray->push_back(appControlData);
674 for (int i = 0; i < length; i++)
683 case SERVICE_ERROR_INVALID_PARAMETER:
684 LogDebug("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
686 case SERVICE_ERROR_KEY_NOT_FOUND:
687 LogDebug("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
689 case SERVICE_ERROR_OUT_OF_MEMORY:
690 LogDebug("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
693 LogDebug("service_get_extra_data retuns Error");
701 ret = service_get_extra_data(service, key, &value);
704 case SERVICE_ERROR_NONE:
708 LogDebug("service_get_extra_data returns NULL");
711 LogDebug("service_get_extra_data success");
712 LogDebug("key = "<<key<<" value = "<<value);
713 std::vector<std::string> valArray;
714 valArray.push_back(value);
716 ApplicationControlDataPtr appControlData(new ApplicationControlData());
717 appControlData->setKey(keyStr);
718 appControlData->setValue(valArray);
719 appControlDataArray->push_back(appControlData);
726 case SERVICE_ERROR_INVALID_PARAMETER:
727 LogDebug("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
729 case SERVICE_ERROR_KEY_NOT_FOUND:
730 LogDebug("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
732 case SERVICE_ERROR_OUT_OF_MEMORY:
733 LogDebug("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
736 LogDebug("service_get_extra_data retuns Error");
745 void ApplicationManager::OnRequestReceived(const EventApplicationGetCurrAppPtr& event)
750 LogDebug("CurrentApplicaton Object is already created.");
751 event->setApp(m_app);
759 // get package info of current app
763 //int ret = app_get_id(&app_id);
764 int ret = get_current_app_id(&app_id);
765 if((ret != APP_ERROR_NONE) || (app_id == NULL))
767 LogError("Can not get app id from current pid (" << ret << ")");
768 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
776 //ApplicationInformationPtr appinfo(new ApplicationInformation(appId));
777 pkgmgrinfo_appinfo_h handle;
778 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
779 if (ret != PMINFO_R_OK) {
780 LogWarning("Fail to get appInfo from appId : " << appId);
783 ApplicationInformationPtr appInfo = create_app_info(handle);
784 pkgmgrinfo_appinfo_destroy_appinfo(handle);
787 ApplicationPtr app(new Application());
788 app->setAppInfo(appInfo);
790 LogDebug("set appinfo to application");
792 //int pid = getpid();
794 std::stringstream sstr;
796 app->setContextId(sstr.str());
798 LogError("set pid : " << pid);
802 LogDebug("set application to event ");
806 Catch (WrtDeviceApis::Commons::Exception)
808 LogError("Error on getAppInfo : " << _rethrown_exception.GetMessage());
809 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
815 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchPtr& event)
824 std::string appId = event->getAppId();
827 LogError("App id is empty");
828 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
832 LogDebug("Launching app id : " << appId);
834 // if the application is running, send raise event to the app instead of reset the application.
836 // give a second chance to launch application to avoid platform issue.
837 // this retry code will be removed after platform code change.
839 ret = aul_open_app(appId.c_str());
843 // delay 300ms for each retry
846 LogDebug("retry launch request : " << retry);
854 LogDebug("aul_open_app returns Not Found error");
855 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
858 LogDebug("aul_open_app returns internal IPC error");
859 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
862 LogDebug("aul_open_app returns Unknown error");
863 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
867 LogDebug("callee's pid " << ret);
871 service_create(&service);
873 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
875 service_set_app_id(service, appId.c_str());
877 // give a second chance to launch application to avoid platform issue.
878 // this retry code will be removed after platform code change.
880 ret = service_send_launch_request(service, NULL, this);
881 if (ret != SERVICE_ERROR_LAUNCH_REJECTED) {
884 // delay 300ms for each retry
887 LogDebug("retry launch request : " << retry);
890 service_destroy(service);
892 if(ret != SERVICE_ERROR_NONE)
896 case SERVICE_ERROR_INVALID_PARAMETER:
897 LogDebug("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
898 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
900 case SERVICE_ERROR_OUT_OF_MEMORY:
901 LogDebug("service_send_launch_request returns SERVICE_ERROR_OUT_OF_MEMORY");
902 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
904 case SERVICE_ERROR_LAUNCH_REJECTED:
905 case SERVICE_ERROR_APP_NOT_FOUND:
906 LogDebug("service_send_launch_request returns SERVICE_ERROR_APP_NOT_FOUND");
907 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
910 LogDebug("service_send_launch_request returns UNKNOWN ERROR!!! (" << ret << ")");
911 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
919 Catch (WrtDeviceApis::Commons::Exception)
921 LogError("Error on launch : " << _rethrown_exception.GetMessage());
922 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
926 void ApplicationManager::OnRequestReceived(const EventApplicationKillPtr& event)
933 std::string contextId = event->getContextId();
935 if(contextId.empty())
937 LogError("Context id is empty");
938 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
942 LogDebug("Killing context id : " << contextId);
945 std::stringstream(contextId) >> pid;
948 LogError("Context id is wrong : " << pid);
949 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
953 char *appIdCStr = NULL;
954 ret = app_manager_get_app_id(pid, &appIdCStr);
955 if (ret != APP_MANAGER_ERROR_NONE)
957 LogError("Error while getting app id (" << ret << ")");
958 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
962 std::string appId = appIdCStr;
965 app_context_h appContext;
966 ret = app_manager_get_app_context (appId.c_str(), &appContext);
967 if (ret != APP_MANAGER_ERROR_NONE)
969 LogError("Error while getting app context (" << ret << ")");
970 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
975 ret = app_manager_set_app_context_event_cb(app_manager_app_context_event_callback, this);
976 if (ret != APP_MANAGER_ERROR_NONE)
978 LogError("Error while registering app context event (" << ret << ")");
979 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
983 ret = app_manager_terminate_app(appContext);
984 if (ret != APP_MANAGER_ERROR_NONE)
986 LogError("Error while terminating app (" << ret << ")");
987 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
991 DPL::Mutex::ScopedLock lock(&m_killMapLock);
992 m_killEventMap[pid] = event;
993 event->switchToManualAnswer();
995 Catch (WrtDeviceApis::Commons::Exception)
997 LogError("Error on kill : " << _rethrown_exception.GetMessage());
998 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1002 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlPtr& event)
1004 LogDebug("entered");
1011 ApplicationControlPtr appControl = event->getAppControl();
1012 if(appControl == NULL)
1014 LogError("appControl is NULL");
1015 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1019 std::string operation = appControl->getOperation();
1020 if(operation.empty())
1022 LogDebug("operation being madatory is empty");
1023 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1027 std::string appId = event->getAppId();
1030 service_create(&service);
1034 LogDebug(" app id : " << appId);
1036 service_set_app_id(service, appId.c_str());
1039 service_set_operation(service, operation.c_str() );
1040 LogDebug(" operation : " << operation);
1042 std::string uri = appControl->getUri();
1045 LogDebug(" uri : " << uri);
1046 service_set_uri(service, uri.c_str() );
1049 std::string mime = appControl->getMime();
1052 LogDebug(" mime : " << mime);
1053 service_set_mime(service, mime.c_str() );
1056 std::string category = appControl->getCategory();
1057 if (!category.empty())
1059 LogDebug(" category : " << category);
1060 service_set_category(service, category.c_str() );
1063 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1065 if(!appControlDataArray.empty())
1067 LogDebug(" datas : " << appControlDataArray.size());
1069 ApplicationControlDataArray::iterator iter;
1070 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1072 ApplicationControlDataPtr appControlData = *iter;
1074 std::string key = appControlData->getKey();
1075 LogDebug(" key : " << key);
1079 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1083 std::vector<std::string> valueArray = appControlData->getValue();
1084 size_t size = valueArray.size();
1086 const char **arr = (const char**)calloc(sizeof(char*), size);
1088 for (size_t j = 0; j < size; j++)
1090 arr[j] = valueArray.at(j).c_str();
1091 LogDebug(" value : " << arr[j]);
1094 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1095 const char *keyCStr = key.c_str();
1097 service_add_extra_data(service, keyCStr, arr[0]);
1099 service_add_extra_data_array(service, keyCStr, arr, size);
1107 LaunchAppControlPendingEvent *pendingEvent = NULL;
1108 LaunchAppControlPendingEventMap::DataKeyType key = 0;
1110 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
1113 pendingEvent = new LaunchAppControlPendingEvent((void*)this, eventReply);
1114 key = gLaunchAppControlPendingEventMap.insert(this, pendingEvent);
1117 // give a second chance to launch application to avoid platform issue.
1118 // this retry code will be removed after platform code change.
1120 ret = service_send_launch_request(service, service_reply_callback, (void *)key);
1121 if (ret != SERVICE_ERROR_LAUNCH_REJECTED) {
1124 // delay 300ms for each retry
1127 LogDebug("retry launch request : " << retry);
1130 service_destroy(service);
1132 if(ret != SERVICE_ERROR_NONE)
1136 case SERVICE_ERROR_INVALID_PARAMETER:
1137 LogDebug("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
1138 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1140 case SERVICE_ERROR_OUT_OF_MEMORY:
1141 LogDebug("service_send_launch_request returns SERVICE_ERROR_OUT_OF_MEMORY");
1142 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1144 case SERVICE_ERROR_LAUNCH_REJECTED:
1145 case SERVICE_ERROR_APP_NOT_FOUND:
1146 LogDebug("service_send_launch_request returns SERVICE_ERROR_APP_NOT_FOUND");
1147 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1150 LogDebug("service_send_launch_request returns UNKNOWN ERROR!!!");
1151 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
1157 gLaunchAppControlPendingEventMap.eraseData(key);
1159 delete pendingEvent;
1160 pendingEvent = NULL;
1162 eventReply->cancelRequest();
1163 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(eventReply);
1167 Catch (WrtDeviceApis::Commons::Exception)
1169 LogError("Error on launchAppControl : " << _rethrown_exception.GetMessage());
1170 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1174 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlReplyPtr& event)
1176 LogDebug("entered");
1177 event->switchToManualAnswer();
1180 void ApplicationManager::OnRequestReceived(const EventApplicationFindAppControlPtr& event)
1182 LogDebug("entered");
1186 ApplicationControlPtr appControl = event->getAppControl();
1187 if(appControl == NULL)
1189 LogError("appControl is NULL");
1190 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1194 std::string operation = appControl->getOperation();
1195 if(operation.empty())
1197 LogDebug("operation being madatory is empty");
1198 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1203 service_create(&service);
1205 service_set_operation(service, operation.c_str() );
1206 LogDebug(" operation : " << operation);
1208 std::string uri = appControl->getUri();
1211 LogDebug(" uri : " << uri);
1212 service_set_uri(service, uri.c_str() );
1215 std::string mime = appControl->getMime();
1218 LogDebug(" mime : " << mime);
1219 service_set_mime(service, mime.c_str() );
1222 std::string category = appControl->getCategory();
1223 if (!category.empty())
1225 LogDebug(" category : " << category);
1226 service_set_category(service, category.c_str() );
1229 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1231 if(!appControlDataArray.empty())
1233 LogDebug(" datas : " << appControlDataArray.size());
1235 ApplicationControlDataArray::iterator iter;
1236 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1238 ApplicationControlDataPtr appControlData = *iter;
1240 std::string key = appControlData->getKey();
1241 LogDebug(" key : " << key);
1245 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1249 std::vector<std::string> valueArray = appControlData->getValue();
1250 size_t size = valueArray.size();
1252 const char **arr = (const char**)calloc(sizeof(char*), size);
1254 for (size_t j = 0; j < size; j++)
1256 arr[j] = valueArray.at(j).c_str();
1257 LogDebug(" value : " << arr[j]);
1260 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1261 const char *keyCStr = key.c_str();
1263 service_add_extra_data(service, keyCStr, arr[0]);
1265 service_add_extra_data_array(service, keyCStr, arr, size);
1273 ApplicationInformationArrayPtr appInfos(new ApplicationInformationArray());
1275 int result = service_foreach_app_matched(service, service_app_matched_callback, (void *)appInfos.Get());
1276 if (result != SERVICE_ERROR_NONE)
1278 LogError("service_foreach_app_matched error (" << result << ")");
1279 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1280 service_destroy(service);
1284 service_destroy(service);
1286 event->setAppInfos(appInfos);
1288 Catch (WrtDeviceApis::Commons::Exception)
1290 LogError("Error on findAppControl : " << _rethrown_exception.GetMessage());
1291 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1296 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsContextPtr& event)
1298 LogDebug("entered");
1304 ApplicationContextArrayPtr appContextArray = event->getAppContextArray();
1305 ret = app_manager_foreach_app_context(app_manager_app_context_callback, appContextArray.Get());
1306 if(ret != APP_MANAGER_ERROR_NONE)
1308 LogError("app_manager_foreach_app_context error (" << ret << ")");
1309 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1312 Catch (WrtDeviceApis::Commons::Exception)
1314 LogError("Error on : " << _rethrown_exception.GetMessage());
1315 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1319 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppContextPtr& event)
1321 LogDebug("entered");
1327 std::string contextId = event->getAppContextId();
1330 if (contextId.empty())
1335 std::stringstream sstr;
1337 contextId = sstr.str();
1341 std::stringstream(contextId) >> pid;
1344 LogError("contextId is wrong : " << contextId);
1345 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1350 char *app_id = NULL;
1352 ret = app_manager_get_app_id(pid, &app_id);
1353 if(ret != APP_MANAGER_ERROR_NONE)
1357 case APP_MANAGER_ERROR_NO_SUCH_APP:
1358 LogError("app_manager_get_app_id error : no such app");
1359 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1361 case APP_MANAGER_ERROR_INVALID_PARAMETER:
1362 LogError("app_manager_get_app_id error : invalid parameter");
1363 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1366 LogError("app_manager_get_app_id error (" << ret << ")");
1367 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1377 ApplicationContextPtr appContext(new ApplicationContext());
1378 appContext->setAppId(app_id);
1379 appContext->setContextId(contextId);
1381 event->setAppContext(appContext);
1386 Catch (WrtDeviceApis::Commons::Exception)
1388 LogError("Error on getAppContext : " << _rethrown_exception.GetMessage());
1389 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1393 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsInfoPtr& event)
1395 LogDebug("entered");
1400 ApplicationInformationArrayPtr appInfoArray = event->getAppInfoArray();
1401 ret = pkgmgrinfo_appinfo_get_installed_list(installed_app_info_cb, (void*)appInfoArray.Get());
1402 if (ret != PMINFO_R_OK) {
1403 LogError("Error on getAppsInfo : ");
1404 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1407 Catch (WrtDeviceApis::Commons::Exception)
1409 LogError("Error on getAppsInfo : " << _rethrown_exception.GetMessage());
1410 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1414 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppInfoPtr& event)
1416 LogDebug("entered");
1420 std::string appId = event->getAppId();
1421 // in case of no argument, get application information of current.
1424 char *app_id = NULL;
1426 //int ret = app_get_id(&app_id); // webkit2 patch.
1427 int ret = get_current_app_id(&app_id);
1428 if((ret != APP_ERROR_NONE) || (app_id == NULL))
1430 LogError("Can not get app id from current pid (" << ret << ")");
1431 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1438 pkgmgrinfo_appinfo_h handle;
1439 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
1440 if (ret != PMINFO_R_OK) {
1441 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1444 ApplicationInformationPtr appInfo = create_app_info(handle);
1445 event->setAppInfo(appInfo);
1446 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1448 Catch (WrtDeviceApis::Commons::Exception)
1450 LogError("Error on getAppInfo : " << _rethrown_exception.GetMessage());
1451 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1455 void ApplicationManager::OnRequestReceived(const EventApplicationAddAppInfoEventListenerPtr& event)
1457 LogDebug("entered");
1461 EventApplicationAppInfoEventListenerEmitterPtr emitter = event->getEmitter();
1463 if(m_installedApplicationsEmitters.size() == 0)
1465 LogDebug("First time registering event listener to this application object.");
1467 // Below can throw Exception
1468 AppManagerWrapperSingleton::Instance().registerAppListChangedCallbacks(this);
1471 m_installedApplicationsEmitters.attach(emitter);
1473 long watchId = AppManagerWrapperSingleton::Instance().getWatchIdAndInc();
1475 m_watchIdMap[watchId] = emitter->getId();
1477 event->setWatchId(watchId);
1479 Catch (WrtDeviceApis::Commons::Exception)
1481 LogError("Error on addAppInfoEventListener : " << _rethrown_exception.GetMessage());
1482 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1486 void ApplicationManager::OnRequestReceived(const EventApplicationRemoveAppInfoEventListenerPtr& event)
1488 LogDebug("entered");
1492 long watchId = event->getWatchId();
1494 if(m_watchIdMap.find(watchId) == m_watchIdMap.end())
1495 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1497 EventApplicationAppInfoEventListenerEmitter::IdType emitterId = m_watchIdMap[watchId];
1499 bool success = m_installedApplicationsEmitters.detach(emitterId);
1501 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1503 if(m_installedApplicationsEmitters.size() == 0)
1505 LogDebug("No more event listener on this application object.");
1507 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
1510 Catch (WrtDeviceApis::Commons::NotFoundException)
1512 LogError("Not found : " << _rethrown_exception.GetMessage());
1513 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1515 Catch (WrtDeviceApis::Commons::Exception)
1517 LogError("Error on removeAppInfoEventListener : " << _rethrown_exception.GetMessage());
1518 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1523 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppCertsPtr& event)
1525 LogDebug("entered");
1529 std::string appId = event->getAppId();
1531 // in case of no argument, get application information of current.
1536 //int ret = app_get_id(&tmp);
1537 int ret = get_current_app_id(&tmp);
1538 if((ret != APP_ERROR_NONE) || (tmp == NULL))
1540 LogError("Can not get app id from current pid (" << ret << ")");
1541 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1550 char* package = getPackageByAppId(appId.c_str());
1551 if (package == NULL)
1553 LogError("Can not get package from appId (" << appId << ")");
1554 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1558 LogDebug("package : " << package);
1559 package_info_h pkg_info;
1562 result = package_manager_get_package_info(package, &pkg_info);
1563 if (result != PACKAGE_MANAGER_ERROR_NONE)
1565 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1569 ApplicationCertArrayPtr certArray(new ApplicationCertArray());
1571 result = package_info_foreach_cert_info(pkg_info, package_cert_cb, (void*)certArray.Get());
1572 if (result != PACKAGE_MANAGER_ERROR_NONE)
1574 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1578 event->setAppCerts(certArray);
1580 LogDebug("Finish to getting package cert info");
1582 Catch (WrtDeviceApis::Commons::Exception)
1584 LogError("Error on getAppInfo : " << _rethrown_exception.GetMessage());
1585 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1590 void ApplicationManager::onAppManagerEventInstalled(const char *appId)
1592 LogDebug("APP_MANAGER_EVENT_INSTALLED, appId : "<< appId);
1594 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1596 pkgmgrinfo_appinfo_h handle;
1597 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1598 if (ret != PMINFO_R_OK) {
1599 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1602 ApplicationInformationPtr appInfo = create_app_info(handle);
1603 event->setAppInfo(appInfo);
1604 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1606 event->setType(EventApplicationAppInfoEventListener::OnInstalled);
1607 m_installedApplicationsEmitters.emit(event);
1610 void ApplicationManager::onAppManagerEventUninstalled(const char *appId)
1612 LogDebug("APP_MANAGER_EVENT_UNINSTALLED, appId : "<< appId);
1614 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1616 ApplicationInformationPtr appInfo(new ApplicationInformation(appId));
1618 event->setType(EventApplicationAppInfoEventListener::OnUninstalled);
1619 event->setAppInfo(appInfo);
1620 m_installedApplicationsEmitters.emit(event);
1623 void ApplicationManager::onAppManagerEventUpdated(const char *appId)
1625 LogDebug("APP_MANAGER_EVENT_UPDATED, appId : "<< appId);
1627 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1629 pkgmgrinfo_appinfo_h handle;
1630 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1631 if (ret != PMINFO_R_OK) {
1632 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1635 ApplicationInformationPtr appInfo = create_app_info(handle);
1636 event->setAppInfo(appInfo);
1637 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1639 event->setType(EventApplicationAppInfoEventListener::OnUpdated);
1640 m_installedApplicationsEmitters.emit(event);
1643 void ApplicationManager::initialize()
1645 if (!m_initialized) {
1646 DPL::Mutex::ScopedLock lock(&m_initializationMutex);
1647 if (!m_initialized) {