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>
25 #include <plugins-ipc-message/ipc_message_support.h>
27 #include "ApplicationInformation.h"
28 #include "ApplicationContext.h"
29 #include "ApplicationControlData.h"
30 #include "ApplicationControl.h"
31 #include "ApplicationCert.h"
32 #include "Application.h"
36 // to launch app by aul
39 // to get package name by appid
41 #include <app_manager.h>
43 // To get cert information from package
44 #include <package_manager.h>
45 #include <package_info.h>
47 // To get app size and installed time
48 #include <pkgmgr-info.h>
56 namespace Application {
59 using namespace WrtDeviceApis;
60 using namespace WrtDeviceApis::Commons;
64 typedef KeyMultiMap<ApplicationManager*, LaunchAppControlPendingEvent> LaunchAppControlPendingEventMap;
65 static LaunchAppControlPendingEventMap gLaunchAppControlPendingEventMap;
67 // Callback from 'app_manager_set_app_context_event_cb'
69 static void app_manager_app_context_event_callback(app_context_h app_context,
70 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 LoggerE("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 LoggerE("Fail to get appinfo");
104 ret = app_info_get_package(handle, &pkgName);
107 LoggerE("Fail to get pkgName");
111 ret = app_info_destroy(handle);
114 LoggerE("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 LoggerE("Fail to get app id from context (" << ret << ")");
145 ret = app_context_get_pid(app_context, &pid);
146 if(ret != APP_MANAGER_ERROR_NONE)
148 LoggerE("Fail to get pid from context (" << 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 LaunchAppControlPendingEventMap::DataKeyType key =
178 (LaunchAppControlPendingEventMap::DataKeyType)user_data;
180 LaunchAppControlPendingEvent *pendingEvent = gLaunchAppControlPendingEventMap.getData(key);
181 if(pendingEvent != NULL)
183 ApplicationManager *application = (ApplicationManager *)pendingEvent->getThisObject();
184 EventApplicationLaunchAppControlReplyPtr event = pendingEvent->getEvent();
185 application->invokeManualAnswerLaunchAppControl(request, reply, result, event);
191 gLaunchAppControlPendingEventMap.eraseData(key);
195 static bool package_cert_cb(package_info_h handle, package_cert_type_e cert_type, const char *cert_value, void *user_data)
197 ApplicationCertPtr cert(new ApplicationCert());
198 const char* certName = NULL;
201 case PACKAGE_INFO_AUTHOR_ROOT_CERT:
202 certName = "AUTHOR_ROOT";
204 case PACKAGE_INFO_AUTHOR_INTERMEDIATE_CERT:
205 certName = "AUTHOR_INTERMEDIATE";
207 case PACKAGE_INFO_AUTHOR_SIGNER_CERT:
208 certName = "AUTHOR_SIGNER";
210 case PACKAGE_INFO_DISTRIBUTOR_ROOT_CERT:
211 certName = "DISTRIBUTOR_ROOT";
213 case PACKAGE_INFO_DISTRIBUTOR_INTERMEDIATE_CERT:
214 certName = "DISTRIBUTOR_INTERMEDIATE";
216 case PACKAGE_INFO_DISTRIBUTOR_SIGNER_CERT:
217 certName = "DISTRIBUTOR_SIGNER";
219 case PACKAGE_INFO_DISTRIBUTOR2_ROOT_CERT:
220 certName = "DISTRIBUTOR2_ROOT";
222 case PACKAGE_INFO_DISTRIBUTOR2_INTERMEDIATE_CERT:
223 certName = "DISTRIBUTOR2_INTERMEDIATE";
225 case PACKAGE_INFO_DISTRIBUTOR2_SIGNER_CERT:
226 certName = "DISTRIBUTOR2_SIGNER";
229 LoggerE("Unknow Cert type!!!");
233 cert->setType(certName);
234 cert->setValue(cert_value);
236 ApplicationCertArray *certs = (ApplicationCertArray *)user_data;
237 certs->push_back(cert);
242 static std::string get_current_app_id()
244 std::string appId = AppManagerWrapperSingleton::Instance().getCurrentAppId();
248 static int category_cb(const char *category, void *user_data)
250 if (category == NULL)
253 ApplicationInformation* appInfo = (ApplicationInformation*)user_data;
254 appInfo->addCategories(category);
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"
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)
267 char *version = NULL;
269 int installed_time = 0;
270 pkgmgrinfo_pkginfo_h pkginfo_h;
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");
278 ret = pkgmgrinfo_pkginfo_get_version(pkginfo_h, &version);
279 if ((ret != PMINFO_R_OK) || (version == NULL)) {
280 LoggerD("Fail to get version");
282 appInfo->setVersion(version);
285 ret = pkgmgrinfo_pkginfo_get_total_size(pkginfo_h, &total_size);
286 if (ret != PMINFO_R_OK) {
287 LoggerD("Fail to get total size");
289 appInfo->setInstallSize(total_size);
292 ret = pkgmgrinfo_pkginfo_get_installed_time(pkginfo_h, &installed_time);
293 if (ret != PMINFO_R_OK) {
294 LoggerD("Fail to get installed time");
296 appInfo->setInstallDate(installed_time);
299 ret = pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
300 if (ret != PMINFO_R_OK) {
301 LoggerD("Fail to get destroy pkginfo");
306 static ApplicationInformationPtr create_app_info(pkgmgrinfo_appinfo_h handle)
310 char* iconPath = NULL;
311 bool noDisplay = false;
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");
320 appInfo->setAppId(appId);
323 ret = pkgmgrinfo_appinfo_get_label(handle, &name);
324 if ((ret != PMINFO_R_OK) || (name == NULL)) {
325 LoggerD("Fail to get name");
327 appInfo->setName(name);
330 ret = pkgmgrinfo_appinfo_get_icon(handle, &iconPath);
331 if ((ret != PMINFO_R_OK) || (iconPath == NULL)) {
332 LoggerD("Fail to get icon");
334 appInfo->setIconPath(iconPath);
337 ret = pkgmgrinfo_appinfo_is_nodisplay(handle, &noDisplay);
338 if (ret != PMINFO_R_OK) {
339 LoggerD("Fail to get nodisplay");
341 appInfo->setShow(!noDisplay);
344 ret = pkgmgrinfo_appinfo_foreach_category(handle, category_cb, (void*)appInfo.Get());
345 if (ret != PMINFO_R_OK) {
346 LoggerD("Fail to get categories");
349 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
350 if ((ret != PMINFO_R_OK) || (pkgId == NULL)) {
351 LoggerD("Fail to get pkg Id");
353 appInfo->setPackageId(pkgId);
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"
360 // So, temporally, we get version, total size, installed time on first access for that attribute.
361 // See JSApplicationInformation.cpp
363 ret = pkgmgrinfo_appinfo_get_pkgname(handle, &packageId);
364 if (ret != PMINFO_R_OK)
366 LoggerD("Fail to get package id");
370 set_additional_app_info(appInfo, packageId);
378 static int installed_app_info_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
380 ApplicationInformationPtr appInfo = create_app_info(handle);
381 ApplicationInformationArray *appInfoArray = (ApplicationInformationArray*)user_data;
382 appInfoArray->push_back(appInfo);
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)
392 LoggerD("appid is NULL");
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);
401 ApplicationInformationPtr appInfo = create_app_info(handle);
402 pkgmgrinfo_appinfo_destroy_appinfo(handle);
404 ApplicationInformationArray *appInfos = (ApplicationInformationArray *)user_data;
405 appInfos->push_back(appInfo);
412 ApplicationManager::ApplicationManager() :
413 m_initialized(false),
419 ApplicationManager::~ApplicationManager()
421 if(m_installedApplicationsEmitters.size() != 0)
423 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
424 WatchIdMap::iterator iter = m_watchIdMap.begin();
425 for(; iter != m_watchIdMap.end(); iter++)
427 m_installedApplicationsEmitters.detach(iter->second);
431 LaunchAppControlPendingEventMap::DataPtrListType dataPtrList =
432 gLaunchAppControlPendingEventMap.getDataPtrList(this);
434 LaunchAppControlPendingEventMap::DataPtrListType::iterator iter = dataPtrList.begin();
435 for(; iter != dataPtrList.end(); iter++)
440 gLaunchAppControlPendingEventMap.eraseKey(this);
443 void ApplicationManager::getCurrentApplication(const EventApplicationGetCurrAppPtr& event)
445 if (m_initialized == false) {
449 EventRequestReceiver<EventApplicationGetCurrApp>::PostRequest(event);
452 void ApplicationManager::launch(const EventApplicationLaunchPtr& event)
454 if (m_initialized == false) {
458 EventRequestReceiver<EventApplicationLaunch>::PostRequest(event);
461 void ApplicationManager::kill(const EventApplicationKillPtr& event)
463 if (m_initialized == false) {
467 EventRequestReceiver<EventApplicationKill>::PostRequest(event);
470 void ApplicationManager::launchAppControl(const EventApplicationLaunchAppControlPtr& event)
472 if (m_initialized == false) {
476 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
477 if(eventReply != NULL)
478 EventRequestReceiver<EventApplicationLaunchAppControlReply>::PostRequest(eventReply);
480 EventRequestReceiver<EventApplicationLaunchAppControl>::PostRequest(event);
483 void ApplicationManager::findAppControl(const EventApplicationFindAppControlPtr& event)
485 if (m_initialized == false) {
489 EventRequestReceiver<EventApplicationFindAppControl>::PostRequest(event);
492 void ApplicationManager::getAppsContext(const EventApplicationGetAppsContextPtr& event)
494 if (m_initialized == false) {
498 EventRequestReceiver<EventApplicationGetAppsContext>::PostRequest(event);
501 void ApplicationManager::getAppContext(const EventApplicationGetAppContextPtr& event)
503 if (m_initialized == false) {
507 EventRequestReceiver<EventApplicationGetAppContext>::PostRequest(event);
510 void ApplicationManager::getAppsInfo(const EventApplicationGetAppsInfoPtr& event)
512 if (m_initialized == false) {
516 EventRequestReceiver<EventApplicationGetAppsInfo>::PostRequest(event);
519 void ApplicationManager::getAppInfo(const EventApplicationGetAppInfoPtr& event)
521 if (m_initialized == false) {
525 EventRequestReceiver<EventApplicationGetAppInfo>::PostRequest(event);
528 void ApplicationManager::addAppInfoEventListener(const EventApplicationAddAppInfoEventListenerPtr& event)
530 if (m_initialized == false) {
534 EventRequestReceiver<EventApplicationAddAppInfoEventListener>::PostRequest(event);
537 void ApplicationManager::removeAppInfoEventListener(const EventApplicationRemoveAppInfoEventListenerPtr& event)
539 if (m_initialized == false) {
543 EventRequestReceiver<EventApplicationRemoveAppInfoEventListener>::PostRequest(event);
546 void ApplicationManager::getAppCerts(const EventApplicationGetAppCertsPtr& event)
548 if (m_initialized == false) {
552 EventRequestReceiver<EventApplicationGetAppCerts>::PostRequest(event);
556 void ApplicationManager::getAppSharedURI(const EventApplicationGetAppSharedURIPtr& event)
558 if (m_initialized == false) {
562 EventRequestReceiver<EventApplicationGetAppSharedURI>::PostRequest(event);
566 void ApplicationManager::invokeManualAnswerLaunchAppControl(service_h request, service_h reply,
567 service_result_e result,
568 EventApplicationLaunchAppControlReplyPtr &event)
574 if(result == SERVICE_RESULT_SUCCEEDED)
576 // create new service object to store result.
577 ApplicationControlDataArrayPtr appControlDataArray(new ApplicationControlDataArray());
579 int result = service_foreach_extra_data(reply, service_extra_data_callback, appControlDataArray.Get());
580 if( result == SERVICE_ERROR_NONE)
582 event->setAppControlDataArray(appControlDataArray);
586 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
589 else if(result == SERVICE_RESULT_FAILED || result == SERVICE_RESULT_CANCELED)
591 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
594 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(event);
597 void ApplicationManager::invokeManualAnswerKill(int pid)
599 DPL::Mutex::ScopedLock lock(&m_killMapLock);
601 std::map<int, EventApplicationKillPtr>::iterator it = m_killEventMap.find(pid);
602 if (it == m_killEventMap.end())
605 EventApplicationKillPtr event = it->second;
606 m_killEventMap.erase(it);
608 EventRequestReceiver<EventApplicationKill>::ManualAnswer(event);
612 bool ApplicationManager::service_extra_data_callback(service_h service, const char *key, void* user_data)
616 ApplicationControlDataArray* appControlDataArray = (ApplicationControlDataArray*)user_data;
618 bool isArray = false;
619 ret = service_is_extra_data_array(service, key, &isArray);
620 if (ret != SERVICE_ERROR_NONE)
622 LoggerE("service_is_extra_data_array passes error");
623 // fail to checking. go to next extra data.
627 std::string keyStr(key);
634 ret = service_get_extra_data_array(service, key, &value, &length);
637 case SERVICE_ERROR_NONE: {
638 std::vector<std::string> valArray;
639 LoggerI("value length : " << length);
640 for (int i = 0; i < length; i++)
644 valArray.push_back(value[i]);
648 ApplicationControlDataPtr appControlData(new ApplicationControlData());
649 appControlData->setKey(keyStr);
650 appControlData->setValue(valArray);
651 appControlDataArray->push_back(appControlData);
653 for (int i = 0; i < length; i++)
662 case SERVICE_ERROR_INVALID_PARAMETER:
663 LoggerE("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
665 case SERVICE_ERROR_KEY_NOT_FOUND:
666 LoggerE("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
668 case SERVICE_ERROR_OUT_OF_MEMORY:
669 LoggerE("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
672 LoggerE("service_get_extra_data retuns Error");
680 ret = service_get_extra_data(service, key, &value);
683 case SERVICE_ERROR_NONE:
687 LoggerE("service_get_extra_data returns NULL");
691 std::vector<std::string> valArray;
692 valArray.push_back(value);
694 ApplicationControlDataPtr appControlData(new ApplicationControlData());
695 appControlData->setKey(keyStr);
696 appControlData->setValue(valArray);
697 appControlDataArray->push_back(appControlData);
704 case SERVICE_ERROR_INVALID_PARAMETER:
705 LoggerE("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
707 case SERVICE_ERROR_KEY_NOT_FOUND:
708 LoggerE("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
710 case SERVICE_ERROR_OUT_OF_MEMORY:
711 LoggerE("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
714 LoggerE("service_get_extra_data retuns Error");
723 void ApplicationManager::OnRequestReceived(const EventApplicationGetCurrAppPtr& event)
726 LoggerD("CurrentApplicaton Object is already created.");
727 event->setApp(m_app);
733 std::string appId = get_current_app_id();
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");
742 ApplicationInformationPtr appInfo = create_app_info(handle);
743 pkgmgrinfo_appinfo_destroy_appinfo(handle);
746 ApplicationPtr app(new Application());
747 app->setAppInfo(appInfo);
749 LoggerD("set appinfo to application");
751 //int pid = getpid();
753 std::stringstream sstr;
755 app->setContextId(sstr.str());
761 Catch (WrtDeviceApis::Commons::Exception)
763 LoggerE("Error on getAppInfo : " << _rethrown_exception.GetMessage());
764 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
770 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchPtr& event)
777 std::string appId = event->getAppId();
780 LoggerE("App id is mandatory field.");
781 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
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.
789 ret = aul_open_app(appId.c_str());
793 // delay 300ms for each retry
796 LoggerD("retry launch request : " << retry);
804 LoggerE("returns Not Found error");
805 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
808 LoggerE("returns internal IPC error");
809 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
812 LoggerE("returns Unknown error");
813 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
817 LoggerD("Success to launch.");
820 Catch (WrtDeviceApis::Commons::Exception)
822 LoggerE("Error on launch : " << _rethrown_exception.GetMessage());
823 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
827 void ApplicationManager::OnRequestReceived(const EventApplicationKillPtr& event)
832 std::string contextId = event->getContextId();
834 if(contextId.empty())
836 LoggerE("Context id is mandatory");
837 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
842 std::stringstream(contextId) >> pid;
845 LoggerE("Given context id is wrong");
846 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
850 // if kill request is come for current context, throw InvalidValueException by spec
851 if (pid == getppid())
853 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
857 char *appIdCStr = NULL;
858 ret = app_manager_get_app_id(pid, &appIdCStr);
859 if (ret != APP_MANAGER_ERROR_NONE)
861 LoggerE("Error while getting app id (" << ret << ")");
862 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
866 std::string appId = appIdCStr;
869 app_context_h appContext;
870 ret = app_manager_get_app_context (appId.c_str(), &appContext);
871 if (ret != APP_MANAGER_ERROR_NONE)
873 LoggerE("Error while getting app context (" << ret << ")");
874 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
879 ret = app_manager_set_app_context_event_cb(app_manager_app_context_event_callback, this);
880 if (ret != APP_MANAGER_ERROR_NONE)
882 LoggerE("Error while registering app context event (" << ret << ")");
883 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
887 ret = app_manager_terminate_app(appContext);
888 if (ret != APP_MANAGER_ERROR_NONE)
890 LoggerE("Error while terminating app (" << ret << ")");
891 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
895 DPL::Mutex::ScopedLock lock(&m_killMapLock);
896 m_killEventMap[pid] = event;
897 event->switchToManualAnswer();
899 Catch (WrtDeviceApis::Commons::Exception)
901 LoggerE("Error on kill : " << _rethrown_exception.GetMessage());
902 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
906 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlPtr& event)
913 ApplicationControlPtr appControl = event->getAppControl();
914 if(appControl == NULL)
916 LoggerE("appControl is mandatory");
917 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
921 std::string operation = appControl->getOperation();
922 if(operation.empty())
924 LoggerE("operation is madatory");
925 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
929 std::string appId = event->getAppId();
932 service_create(&service);
936 service_set_app_id(service, appId.c_str());
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);
942 // Application exist checking. if specific application is not exist, return Not Found Exception.
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);
949 service_destroy(service);
953 app_info_destroy(info_h);
954 if (resolved_app_id) {
955 free(resolved_app_id);
959 const char* windowId = IPCMessageSupport::sendMessageToUiProcess("tizen://getWindowHandle", NULL);
960 if (windowId != NULL)
962 service_set_window(service, atoi(windowId));
965 service_set_operation(service, operation.c_str() );
967 std::string uri = appControl->getUri();
970 service_set_uri(service, uri.c_str() );
973 std::string mime = appControl->getMime();
976 service_set_mime(service, mime.c_str() );
979 std::string category = appControl->getCategory();
980 if (!category.empty())
982 service_set_category(service, category.c_str() );
985 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
987 if(!appControlDataArray.empty())
989 LoggerI(" data size : " << appControlDataArray.size());
991 ApplicationControlDataArray::iterator iter;
992 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
994 ApplicationControlDataPtr appControlData = *iter;
996 std::string key = appControlData->getKey();
1000 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1004 std::vector<std::string> valueArray = appControlData->getValue();
1005 size_t size = valueArray.size();
1007 const char **arr = (const char**)calloc(sizeof(char*), size);
1009 for (size_t j = 0; j < size; j++)
1011 arr[j] = valueArray.at(j).c_str();
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();
1017 service_add_extra_data(service, keyCStr, arr[0]);
1019 service_add_extra_data_array(service, keyCStr, arr, size);
1027 LaunchAppControlPendingEvent *pendingEvent = NULL;
1028 LaunchAppControlPendingEventMap::DataKeyType key = 0;
1030 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
1033 pendingEvent = new LaunchAppControlPendingEvent((void*)this, eventReply);
1034 key = gLaunchAppControlPendingEventMap.insert(this, pendingEvent);
1037 // give a second chance to launch application to avoid platform issue.
1038 // this retry code will be removed after platform code change.
1040 ret = service_send_launch_request(service, service_reply_callback, (void *)key);
1041 if (ret != SERVICE_ERROR_LAUNCH_REJECTED) {
1044 // delay 300ms for each retry
1047 LoggerD("retry launch request : " << retry);
1050 service_destroy(service);
1052 if(ret != SERVICE_ERROR_NONE)
1056 case SERVICE_ERROR_INVALID_PARAMETER:
1057 LoggerE("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
1058 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
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);
1064 case SERVICE_ERROR_LAUNCH_REJECTED:
1065 LoggerE("service_send_launch_request returns SERVICE_ERROR_LAUNCH_REJECTED!!!");
1066 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
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);
1073 LoggerE("service_send_launch_request returns UNKNOWN ERROR!!!");
1074 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
1080 gLaunchAppControlPendingEventMap.eraseData(key);
1082 delete pendingEvent;
1083 pendingEvent = NULL;
1085 eventReply->cancelRequest();
1086 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(eventReply);
1090 Catch (WrtDeviceApis::Commons::Exception)
1092 LoggerE("Error on launchAppControl : " << _rethrown_exception.GetMessage());
1093 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1097 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlReplyPtr& event)
1099 event->switchToManualAnswer();
1102 void ApplicationManager::OnRequestReceived(const EventApplicationFindAppControlPtr& event)
1106 ApplicationControlPtr appControl = event->getAppControl();
1107 if(appControl == NULL)
1109 LoggerE("appControl is NULL");
1110 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1114 std::string operation = appControl->getOperation();
1115 if(operation.empty())
1117 LoggerE("operation is madatory");
1118 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1123 service_create(&service);
1125 service_set_operation(service, operation.c_str() );
1127 std::string uri = appControl->getUri();
1130 service_set_uri(service, uri.c_str() );
1133 std::string mime = appControl->getMime();
1136 service_set_mime(service, mime.c_str() );
1139 std::string category = appControl->getCategory();
1140 if (!category.empty())
1142 service_set_category(service, category.c_str() );
1145 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1147 if(!appControlDataArray.empty())
1149 LoggerD(" data size : " << appControlDataArray.size());
1151 ApplicationControlDataArray::iterator iter;
1152 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1154 ApplicationControlDataPtr appControlData = *iter;
1156 std::string key = appControlData->getKey();
1160 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1164 std::vector<std::string> valueArray = appControlData->getValue();
1165 size_t size = valueArray.size();
1167 const char **arr = (const char**)calloc(sizeof(char*), size);
1169 for (size_t j = 0; j < size; j++)
1171 arr[j] = valueArray.at(j).c_str();
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();
1177 service_add_extra_data(service, keyCStr, arr[0]);
1179 service_add_extra_data_array(service, keyCStr, arr, size);
1187 ApplicationInformationArrayPtr appInfos(new ApplicationInformationArray());
1189 int result = service_foreach_app_matched(service, service_app_matched_callback, (void *)appInfos.Get());
1190 if (result != SERVICE_ERROR_NONE)
1192 LoggerE("service_foreach_app_matched error (" << result << ")");
1193 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1194 service_destroy(service);
1198 service_destroy(service);
1200 event->setAppInfos(appInfos);
1202 Catch (WrtDeviceApis::Commons::Exception)
1204 LoggerE("Error on findAppControl : " << _rethrown_exception.GetMessage());
1205 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1210 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsContextPtr& event)
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)
1220 LoggerE("app_manager_foreach_app_context error (" << ret << ")");
1221 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1224 Catch (WrtDeviceApis::Commons::Exception)
1226 LoggerE("Error on : " << _rethrown_exception.GetMessage());
1227 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1231 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppContextPtr& event)
1237 std::string contextId = event->getAppContextId();
1240 if (contextId.empty())
1245 std::stringstream sstr;
1247 contextId = sstr.str();
1251 std::stringstream(contextId) >> pid;
1254 LoggerE("Given contextId is wrong");
1255 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1260 char *app_id = NULL;
1262 ret = app_manager_get_app_id(pid, &app_id);
1263 if(ret != APP_MANAGER_ERROR_NONE)
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);
1273 LoggerE("app_manager_get_app_id error (" << ret << ")");
1274 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1284 ApplicationContextPtr appContext(new ApplicationContext());
1285 appContext->setAppId(app_id);
1286 appContext->setContextId(contextId);
1288 event->setAppContext(appContext);
1293 Catch (WrtDeviceApis::Commons::Exception)
1295 LoggerE("Error on getAppContext : " << _rethrown_exception.GetMessage());
1296 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1300 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsInfoPtr& event)
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);
1312 Catch (WrtDeviceApis::Commons::Exception)
1314 LoggerE("Error on getAppsInfo : " << _rethrown_exception.GetMessage());
1315 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1319 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppInfoPtr& event)
1323 std::string appId = event->getAppId();
1324 // in case of no argument, get application information of current.
1327 appId = get_current_app_id();
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);
1337 ApplicationInformationPtr appInfo = create_app_info(handle);
1338 event->setAppInfo(appInfo);
1339 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1341 Catch (WrtDeviceApis::Commons::Exception)
1343 LoggerE("Error on getAppInfo : " << _rethrown_exception.GetMessage());
1344 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1348 void ApplicationManager::OnRequestReceived(const EventApplicationAddAppInfoEventListenerPtr& event)
1352 EventApplicationAppInfoEventListenerEmitterPtr emitter = event->getEmitter();
1354 if(m_installedApplicationsEmitters.size() == 0)
1356 LoggerD("First time registering event listener to this application object.");
1358 // Below can throw Exception
1359 AppManagerWrapperSingleton::Instance().registerAppListChangedCallbacks(this);
1362 m_installedApplicationsEmitters.attach(emitter);
1364 long watchId = AppManagerWrapperSingleton::Instance().getWatchIdAndInc();
1366 m_watchIdMap[watchId] = emitter->getId();
1368 event->setWatchId(watchId);
1370 Catch (WrtDeviceApis::Commons::Exception)
1372 LoggerE("Error on addAppInfoEventListener : " << _rethrown_exception.GetMessage());
1373 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1377 void ApplicationManager::OnRequestReceived(const EventApplicationRemoveAppInfoEventListenerPtr& event)
1381 long watchId = event->getWatchId();
1383 if(m_watchIdMap.find(watchId) == m_watchIdMap.end()) {
1384 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1387 EventApplicationAppInfoEventListenerEmitter::IdType emitterId = m_watchIdMap[watchId];
1389 bool success = m_installedApplicationsEmitters.detach(emitterId);
1391 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1393 if(m_installedApplicationsEmitters.size() == 0)
1395 LoggerD("No more event listener on this application object.");
1397 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
1400 Catch (WrtDeviceApis::Commons::NotFoundException)
1402 LoggerE("Not found : " << _rethrown_exception.GetMessage());
1403 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1405 Catch (WrtDeviceApis::Commons::Exception)
1407 LoggerE("Error on removeAppInfoEventListener : " << _rethrown_exception.GetMessage());
1408 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1413 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppCertsPtr& event)
1417 std::string appId = event->getAppId();
1419 // in case of no argument, get application information of current.
1422 appId = get_current_app_id();
1425 char* package = getPackageByAppId(appId.c_str());
1426 if (package == NULL)
1428 LoggerE("Can not get package");
1429 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1433 package_info_h pkg_info;
1436 result = package_manager_get_package_info(package, &pkg_info);
1437 if (result != PACKAGE_MANAGER_ERROR_NONE)
1439 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1443 ApplicationCertArrayPtr certArray(new ApplicationCertArray());
1445 result = package_info_foreach_cert_info(pkg_info, package_cert_cb, (void*)certArray.Get());
1446 if (result != PACKAGE_MANAGER_ERROR_NONE)
1448 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1452 event->setAppCerts(certArray);
1454 Catch (WrtDeviceApis::Commons::Exception)
1456 LoggerE("Error on getAppInfo : " << _rethrown_exception.GetMessage());
1457 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1461 #define TIZENAPIS_APP_FILE_SCHEME "file://"
1462 #define TIZENAPIS_APP_SLASH "/"
1463 #define TIZENAPIS_APP_SHARED "shared"
1465 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppSharedURIPtr& event)
1467 std::string id = event->getAppId();
1471 appId = get_current_app_id();
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);
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);
1492 app_info_destroy(handle);
1494 pkgmgrinfo_pkginfo_h pkginfo_h;
1495 char* root_path = NULL;
1497 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &pkginfo_h);
1498 if (ret != PMINFO_R_OK) {
1500 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
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");
1508 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1512 std::string sharedURI = TIZENAPIS_APP_FILE_SCHEME + std::string(root_path) + TIZENAPIS_APP_SLASH + TIZENAPIS_APP_SHARED;
1515 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
1517 event->setSharedURI(sharedURI);
1522 void ApplicationManager::onAppManagerEventInstalled(const char *appId)
1524 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
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);
1531 ApplicationInformationPtr appInfo = create_app_info(handle);
1532 event->setAppInfo(appInfo);
1533 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1536 event->setType(EventApplicationAppInfoEventListener::OnInstalled);
1537 m_installedApplicationsEmitters.emit(event);
1540 void ApplicationManager::onAppManagerEventUninstalled(const char *appId)
1542 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1544 ApplicationInformationPtr appInfo(new ApplicationInformation(appId));
1546 event->setType(EventApplicationAppInfoEventListener::OnUninstalled);
1547 event->setAppInfo(appInfo);
1548 m_installedApplicationsEmitters.emit(event);
1551 void ApplicationManager::onAppManagerEventUpdated(const char *appId)
1553 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
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);
1560 ApplicationInformationPtr appInfo = create_app_info(handle);
1561 event->setAppInfo(appInfo);
1562 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1565 event->setType(EventApplicationAppInfoEventListener::OnUpdated);
1566 m_installedApplicationsEmitters.emit(event);
1569 void ApplicationManager::initialize()
1571 if (!m_initialized) {
1572 DPL::Mutex::ScopedLock lock(&m_initializationMutex);
1573 if (!m_initialized) {