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>
53 #include <TimeTracer.h>
58 namespace Application {
61 using namespace WrtDeviceApis;
62 using namespace WrtDeviceApis::Commons;
66 typedef KeyMultiMap<ApplicationManager*, LaunchAppControlPendingEvent> LaunchAppControlPendingEventMap;
67 static LaunchAppControlPendingEventMap gLaunchAppControlPendingEventMap;
69 // Callback from 'app_manager_set_app_context_event_cb'
71 static void app_manager_app_context_event_callback(app_context_h app_context,
72 app_context_event_e event, void *user_data)
76 if(event != APP_CONTEXT_EVENT_TERMINATED)
81 ret = app_context_get_pid(app_context, &pid);
82 if(ret != APP_MANAGER_ERROR_NONE)
84 LoggerE("Fail to get pid of terminated app (" << ret << ")");
88 ApplicationManager* appManager = (ApplicationManager*)(user_data);
89 appManager->invokeManualAnswerKill(pid);
92 // get package name by id
93 static char* getPackageByAppId(const char* appId)
99 ret = app_manager_get_app_info(appId, &handle);
102 LoggerE("Fail to get appinfo");
106 ret = app_info_get_package(handle, &pkgName);
109 LoggerE("Fail to get pkgName");
113 ret = app_info_destroy(handle);
116 LoggerE("Fail to get destory appinfo");
124 // Callback of 'app_manager_foreach_app_context'
125 // Used by 'getAppsContext'
126 static bool app_manager_app_context_callback(app_context_h app_context, void *user_data)
133 std::string contextId;
135 if (user_data == NULL)
140 ret = app_context_get_app_id(app_context, &app_id);
141 if((ret != APP_MANAGER_ERROR_NONE) || (app_id == NULL))
143 LoggerE("Fail to get app id from context (" << ret << ")");
147 ret = app_context_get_pid(app_context, &pid);
148 if(ret != APP_MANAGER_ERROR_NONE)
150 LoggerE("Fail to get pid from context (" << ret << ")");
156 std::stringstream sstream;
158 contextId = sstream.str();
160 ApplicationContextPtr appContext(new ApplicationContext());
161 appContext->setAppId(app_id);
162 appContext->setContextId(contextId);
164 ApplicationContextArray* appContextArray = (ApplicationContextArray*)user_data;
166 appContextArray->push_back(appContext);
174 // Callback of 'service_send_launch_request'
175 // Used by 'launchAppControl'
176 static void service_reply_callback(service_h request, service_h reply,
177 service_result_e result, void *user_data)
179 LaunchAppControlPendingEventMap::DataKeyType key =
180 (LaunchAppControlPendingEventMap::DataKeyType)user_data;
182 LaunchAppControlPendingEvent *pendingEvent = gLaunchAppControlPendingEventMap.getData(key);
183 if(pendingEvent != NULL)
185 ApplicationManager *application = (ApplicationManager *)pendingEvent->getThisObject();
186 EventApplicationLaunchAppControlReplyPtr event = pendingEvent->getEvent();
187 application->invokeManualAnswerLaunchAppControl(request, reply, result, event);
193 gLaunchAppControlPendingEventMap.eraseData(key);
197 static bool package_cert_cb(package_info_h handle, package_cert_type_e cert_type, const char *cert_value, void *user_data)
199 ApplicationCertPtr cert(new ApplicationCert());
200 const char* certName = NULL;
203 case PACKAGE_INFO_AUTHOR_ROOT_CERT:
204 certName = "AUTHOR_ROOT";
206 case PACKAGE_INFO_AUTHOR_INTERMEDIATE_CERT:
207 certName = "AUTHOR_INTERMEDIATE";
209 case PACKAGE_INFO_AUTHOR_SIGNER_CERT:
210 certName = "AUTHOR_SIGNER";
212 case PACKAGE_INFO_DISTRIBUTOR_ROOT_CERT:
213 certName = "DISTRIBUTOR_ROOT";
215 case PACKAGE_INFO_DISTRIBUTOR_INTERMEDIATE_CERT:
216 certName = "DISTRIBUTOR_INTERMEDIATE";
218 case PACKAGE_INFO_DISTRIBUTOR_SIGNER_CERT:
219 certName = "DISTRIBUTOR_SIGNER";
221 case PACKAGE_INFO_DISTRIBUTOR2_ROOT_CERT:
222 certName = "DISTRIBUTOR2_ROOT";
224 case PACKAGE_INFO_DISTRIBUTOR2_INTERMEDIATE_CERT:
225 certName = "DISTRIBUTOR2_INTERMEDIATE";
227 case PACKAGE_INFO_DISTRIBUTOR2_SIGNER_CERT:
228 certName = "DISTRIBUTOR2_SIGNER";
231 LoggerE("Unknow Cert type!!!");
235 cert->setType(certName);
236 cert->setValue(cert_value);
238 ApplicationCertArray *certs = (ApplicationCertArray *)user_data;
239 certs->push_back(cert);
244 static std::string get_current_app_id()
246 std::string appId = AppManagerWrapperSingleton::Instance().getCurrentAppId();
250 static int category_cb(const char *category, void *user_data)
252 if (category == NULL)
255 ApplicationInformation* appInfo = (ApplicationInformation*)user_data;
256 appInfo->addCategories(category);
260 static ApplicationInformationPtr create_app_info(pkgmgrinfo_appinfo_h handle)
264 char* iconPath = NULL;
265 bool noDisplay = false;
269 ApplicationInformationPtr appInfo(new ApplicationInformation());
270 ret = pkgmgrinfo_appinfo_get_appid(handle, &appId);
271 if (ret != PMINFO_R_OK) {
272 LoggerD("Fail to get name");
274 appInfo->setAppId(appId);
277 ret = pkgmgrinfo_appinfo_get_label(handle, &name);
278 if ((ret != PMINFO_R_OK) || (name == NULL)) {
279 LoggerD("Fail to get name");
281 appInfo->setName(name);
284 ret = pkgmgrinfo_appinfo_get_icon(handle, &iconPath);
285 if ((ret != PMINFO_R_OK) || (iconPath == NULL)) {
286 LoggerD("Fail to get icon");
288 appInfo->setIconPath(iconPath);
291 ret = pkgmgrinfo_appinfo_is_nodisplay(handle, &noDisplay);
292 if (ret != PMINFO_R_OK) {
293 LoggerD("Fail to get nodisplay");
295 appInfo->setShow(!noDisplay);
298 ret = pkgmgrinfo_appinfo_foreach_category(handle, category_cb, (void*)appInfo.Get());
299 if (ret != PMINFO_R_OK) {
300 LoggerD("Fail to get categories");
303 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
304 if ((ret != PMINFO_R_OK) || (pkgId == NULL)) {
305 LoggerD("Fail to get pkg Id");
307 appInfo->setPackageId(pkgId);
310 char *version = NULL;
311 int installed_time = 0;
312 pkgmgrinfo_pkginfo_h pkginfo_h;
314 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId, &pkginfo_h);
315 if (ret != PMINFO_R_OK) {
316 LoggerE("Fail to get package info");
318 ret = pkgmgrinfo_pkginfo_get_version(pkginfo_h, &version);
319 if (ret != PMINFO_R_OK) {
320 LoggerE("Fail to get version");
322 appInfo->setVersion(version);
325 ret = pkgmgrinfo_pkginfo_get_installed_time(pkginfo_h, &installed_time);
326 if (ret != PMINFO_R_OK) {
327 LoggerE("Fail to get installed date");
329 appInfo->setInstallDate(installed_time);
332 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
335 // remark : attribute "total size" is set at first attribute access time for performance.
340 static int installed_app_info_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
342 ApplicationInformationPtr appInfo = create_app_info(handle);
343 ApplicationInformationArray *appInfoArray = (ApplicationInformationArray*)user_data;
344 appInfoArray->push_back(appInfo);
348 // Callback from 'service_foreach_app_matched'
349 // Used by 'findAppControl'
350 static bool service_app_matched_callback(service_h service, const char *appid, void *user_data)
354 LoggerD("appid is NULL");
357 //ApplicationInformationPtr appInfo(new ApplicationInformation(appid));
358 pkgmgrinfo_appinfo_h handle;
359 int ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
360 if (ret != PMINFO_R_OK) {
361 LoggerD("Fail to get appInfo from appId : " << appid);
363 ApplicationInformationPtr appInfo = create_app_info(handle);
364 pkgmgrinfo_appinfo_destroy_appinfo(handle);
366 ApplicationInformationArray *appInfos = (ApplicationInformationArray *)user_data;
367 appInfos->push_back(appInfo);
374 ApplicationManager::ApplicationManager() :
380 ApplicationManager::~ApplicationManager()
382 if(m_installedApplicationsEmitters.size() != 0)
384 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
385 WatchIdMap::iterator iter = m_watchIdMap.begin();
386 for(; iter != m_watchIdMap.end(); iter++)
388 m_installedApplicationsEmitters.detach(iter->second);
392 LaunchAppControlPendingEventMap::DataPtrListType dataPtrList =
393 gLaunchAppControlPendingEventMap.getDataPtrList(this);
395 LaunchAppControlPendingEventMap::DataPtrListType::iterator iter = dataPtrList.begin();
396 for(; iter != dataPtrList.end(); iter++)
401 gLaunchAppControlPendingEventMap.eraseKey(this);
403 // unset context event callback which is registered by kill().
404 app_manager_unset_app_context_event_cb();
407 void ApplicationManager::launch(const EventApplicationLaunchPtr& event)
409 if (m_initialized == false) {
413 EventRequestReceiver<EventApplicationLaunch>::PostRequest(event);
416 void ApplicationManager::kill(const EventApplicationKillPtr& event)
418 if (m_initialized == false) {
422 EventRequestReceiver<EventApplicationKill>::PostRequest(event);
425 void ApplicationManager::launchAppControl(const EventApplicationLaunchAppControlPtr& event)
427 if (m_initialized == false) {
431 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
432 if(eventReply != NULL)
433 EventRequestReceiver<EventApplicationLaunchAppControlReply>::PostRequest(eventReply);
435 EventRequestReceiver<EventApplicationLaunchAppControl>::PostRequest(event);
438 void ApplicationManager::findAppControl(const EventApplicationFindAppControlPtr& event)
440 if (m_initialized == false) {
444 EventRequestReceiver<EventApplicationFindAppControl>::PostRequest(event);
447 void ApplicationManager::getAppsContext(const EventApplicationGetAppsContextPtr& event)
449 if (m_initialized == false) {
453 EventRequestReceiver<EventApplicationGetAppsContext>::PostRequest(event);
457 void ApplicationManager::getAppsInfo(const EventApplicationGetAppsInfoPtr& event)
459 if (m_initialized == false) {
463 EventRequestReceiver<EventApplicationGetAppsInfo>::PostRequest(event);
467 void ApplicationManager::addAppInfoEventListener(const EventApplicationAddAppInfoEventListenerPtr& event)
469 if (m_initialized == false) {
473 EventRequestReceiver<EventApplicationAddAppInfoEventListener>::PostRequest(event);
476 void ApplicationManager::removeAppInfoEventListener(const EventApplicationRemoveAppInfoEventListenerPtr& event)
478 if (m_initialized == false) {
482 EventRequestReceiver<EventApplicationRemoveAppInfoEventListener>::PostRequest(event);
486 void ApplicationManager::invokeManualAnswerLaunchAppControl(service_h request, service_h reply,
487 service_result_e result,
488 EventApplicationLaunchAppControlReplyPtr &event)
494 if(result == SERVICE_RESULT_SUCCEEDED)
496 // create new service object to store result.
497 ApplicationControlDataArrayPtr appControlDataArray(new ApplicationControlDataArray());
499 int result = service_foreach_extra_data(reply, service_extra_data_callback, appControlDataArray.Get());
500 if( result == SERVICE_ERROR_NONE)
502 event->setAppControlDataArray(appControlDataArray);
506 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
509 else if(result == SERVICE_RESULT_FAILED || result == SERVICE_RESULT_CANCELED)
511 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
514 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(event);
517 void ApplicationManager::invokeManualAnswerKill(int pid)
519 DPL::Mutex::ScopedLock lock(&m_killMapLock);
521 std::map<int, EventApplicationKillPtr>::iterator it = m_killEventMap.find(pid);
522 if (it == m_killEventMap.end())
525 EventApplicationKillPtr event = it->second;
526 m_killEventMap.erase(it);
528 EventRequestReceiver<EventApplicationKill>::ManualAnswer(event);
532 bool ApplicationManager::service_extra_data_callback(service_h service, const char *key, void* user_data)
536 ApplicationControlDataArray* appControlDataArray = (ApplicationControlDataArray*)user_data;
538 bool isArray = false;
539 ret = service_is_extra_data_array(service, key, &isArray);
540 if (ret != SERVICE_ERROR_NONE)
542 LoggerE("service_is_extra_data_array passes error");
543 // fail to checking. go to next extra data.
547 std::string keyStr(key);
554 ret = service_get_extra_data_array(service, key, &value, &length);
557 case SERVICE_ERROR_NONE: {
558 std::vector<std::string> valArray;
559 LoggerI("value length : " << length);
560 for (int i = 0; i < length; i++)
564 valArray.push_back(value[i]);
568 ApplicationControlDataPtr appControlData(new ApplicationControlData());
569 appControlData->setKey(keyStr);
570 appControlData->setValue(valArray);
571 appControlDataArray->push_back(appControlData);
573 for (int i = 0; i < length; i++)
582 case SERVICE_ERROR_INVALID_PARAMETER:
583 LoggerE("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
585 case SERVICE_ERROR_KEY_NOT_FOUND:
586 LoggerE("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
588 case SERVICE_ERROR_OUT_OF_MEMORY:
589 LoggerE("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
592 LoggerE("service_get_extra_data retuns Error");
600 ret = service_get_extra_data(service, key, &value);
603 case SERVICE_ERROR_NONE:
607 LoggerE("service_get_extra_data returns NULL");
611 std::vector<std::string> valArray;
612 valArray.push_back(value);
614 ApplicationControlDataPtr appControlData(new ApplicationControlData());
615 appControlData->setKey(keyStr);
616 appControlData->setValue(valArray);
617 appControlDataArray->push_back(appControlData);
624 case SERVICE_ERROR_INVALID_PARAMETER:
625 LoggerE("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
627 case SERVICE_ERROR_KEY_NOT_FOUND:
628 LoggerE("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
630 case SERVICE_ERROR_OUT_OF_MEMORY:
631 LoggerE("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
634 LoggerE("service_get_extra_data retuns Error");
643 ApplicationPtr ApplicationManager::getCurrentApplication()
645 std::string appId = get_current_app_id();
647 //ApplicationInformationPtr appinfo(new ApplicationInformation(appId));
648 pkgmgrinfo_appinfo_h handle;
649 TIME_TRACER_ITEM_BEGIN("(getCurrentApplication)pkgmgrinfo_appinfo_get_appinfo", 0);
650 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
651 TIME_TRACER_ITEM_END("(getCurrentApplication)pkgmgrinfo_appinfo_get_appinfo", 0);
652 if (ret != PMINFO_R_OK) {
653 LoggerE("Fail to get appInfo");
654 ThrowMsg(UnknownException, "pkgmgrinfo_appinfo_get_appinfo error : unknown error");
656 ApplicationInformationPtr appInfo = create_app_info(handle);
657 pkgmgrinfo_appinfo_destroy_appinfo(handle);
660 ApplicationPtr app(new Application());
661 app->setAppInfo(appInfo);
663 LoggerD("set appinfo to application");
665 //int pid = getpid();
667 std::stringstream sstr;
669 app->setContextId(sstr.str());
676 ApplicationContextPtr ApplicationManager::getAppContext(const std::string id)
680 std::string contextId = id;
683 if (contextId.empty())
688 std::stringstream sstr;
690 contextId = sstr.str();
694 std::stringstream(contextId) >> pid;
697 LoggerE("Given contextId is wrong");
698 ThrowMsg(NotFoundException, "Given contextId is wrong");
704 TIME_TRACER_ITEM_BEGIN("(getAppContext)app_manager_get_app_id", 0);
705 ret = app_manager_get_app_id(pid, &app_id);
706 TIME_TRACER_ITEM_END("(getAppContext)app_manager_get_app_id", 0);
707 if(ret != APP_MANAGER_ERROR_NONE)
714 case APP_MANAGER_ERROR_NO_SUCH_APP:
715 case APP_MANAGER_ERROR_INVALID_PARAMETER:
716 LoggerE("app_manager_get_app_id error : no such app");
717 ThrowMsg(NotFoundException, "app_manager_get_app_id error : no such app");
719 LoggerE("app_manager_get_app_id error (" << ret << ")");
720 ThrowMsg(UnknownException, "app_manager_get_app_id error : unknown error");
724 ApplicationContextPtr appContext(new ApplicationContext());
725 appContext->setAppId(app_id);
726 appContext->setContextId(contextId);
735 ApplicationInformationPtr ApplicationManager::getAppInfo(const std::string id)
737 std::string appId = id;
738 // in case of no argument, get application information of current.
741 appId = get_current_app_id();
744 pkgmgrinfo_appinfo_h handle;
745 TIME_TRACER_ITEM_BEGIN("(getAppInfo)pkgmgrinfo_appinfo_get_appinfo", 0);
746 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
747 TIME_TRACER_ITEM_END("(getAppInfo)pkgmgrinfo_appinfo_get_appinfo", 0);
748 if (ret != PMINFO_R_OK) {
749 ThrowMsg(NotFoundException, "Can not get appinfo");
752 ApplicationInformationPtr appInfo = create_app_info(handle);
754 pkgmgrinfo_appinfo_destroy_appinfo(handle);
760 ApplicationCertArrayPtr ApplicationManager::getAppCerts(const std::string id)
762 std::string appId = id;
764 // in case of no argument, get application information of current.
767 appId = get_current_app_id();
770 TIME_TRACER_ITEM_BEGIN("(getAppCerts)getPackageByAppId", 0);
771 char* package = getPackageByAppId(appId.c_str());
772 TIME_TRACER_ITEM_END("(getAppCerts)getPackageByAppId", 0);
775 LoggerE("Can not get package");
776 ThrowMsg(NotFoundException, "Can not get package");
779 package_info_h pkg_info;
782 TIME_TRACER_ITEM_BEGIN("(getAppCerts)package_manager_get_package_info", 0);
783 result = package_manager_get_package_info(package, &pkg_info);
784 TIME_TRACER_ITEM_END("(getAppCerts)package_manager_get_package_info", 0);
785 if (result != PACKAGE_MANAGER_ERROR_NONE)
787 ThrowMsg(UnknownException, "Can not get package info");
790 ApplicationCertArrayPtr certArray(new ApplicationCertArray());
792 TIME_TRACER_ITEM_BEGIN("(getAppCerts)package_info_foreach_cert_info", 0);
793 result = package_info_foreach_cert_info(pkg_info, package_cert_cb, (void*)certArray.Get());
794 TIME_TRACER_ITEM_END("(getAppCerts)package_info_foreach_cert_info", 0);
795 if ((result != PACKAGE_MANAGER_ERROR_NONE) && (result != PACKAGE_MANAGER_ERROR_IO_ERROR))
797 ThrowMsg(UnknownException, "Can not get package cert info");
803 #define TIZENAPIS_APP_FILE_SCHEME "file://"
804 #define TIZENAPIS_APP_SLASH "/"
805 #define TIZENAPIS_APP_SHARED "shared"
808 std::string ApplicationManager::getAppSharedURI(const std::string id)
813 appId = get_current_app_id();
819 char* pkg_name = NULL;
820 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)app_manager_get_app_info", 0);
821 int ret = app_manager_get_app_info(appId.c_str(), &handle);
822 TIME_TRACER_ITEM_END("(getAppSharedURI)app_manager_get_app_info", 0);
823 if (ret != APP_ERROR_NONE) {
824 LoggerD("Fail to get appinfo");
825 //throw NotFoundException("Fail to get appinfo");
826 ThrowMsg(NotFoundException, "Fail to get appinfo");
829 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)app_info_get_package", 0);
830 ret = app_info_get_package(handle, &pkg_name);
831 TIME_TRACER_ITEM_END("(getAppSharedURI)app_info_get_package", 0);
832 if ((ret != APP_ERROR_NONE) || (pkg_name == NULL)) {
833 LoggerD("Fail to get pkg_name");
834 //throw NotFoundException("Fail to get pkg_name");
835 ThrowMsg(NotFoundException, "Fail to get pkg_name");
838 app_info_destroy(handle);
840 pkgmgrinfo_pkginfo_h pkginfo_h;
841 char* root_path = NULL;
843 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)pkgmgrinfo_pkginfo_get_pkginfo", 0);
844 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &pkginfo_h);
845 TIME_TRACER_ITEM_END("(getAppSharedURI)pkgmgrinfo_pkginfo_get_pkginfo", 0);
846 if (ret != PMINFO_R_OK) {
848 //throw UnknownException("Fail to get pkginfo");
849 ThrowMsg(UnknownException, "Fail to get pkginfo");
852 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)pkgmgrinfo_pkginfo_get_root_path", 0);
853 ret = pkgmgrinfo_pkginfo_get_root_path(pkginfo_h, &root_path);
854 TIME_TRACER_ITEM_END("(getAppSharedURI)pkgmgrinfo_pkginfo_get_root_path", 0);
855 if ((ret != PMINFO_R_OK) || (root_path == NULL)) {
856 LoggerE("Fail to get root path");
858 //throw UnknownException("Fail to get rotpath");
859 ThrowMsg(UnknownException, "Fail to get rotpath");
862 std::string sharedURI = TIZENAPIS_APP_FILE_SCHEME + std::string(root_path) + TIZENAPIS_APP_SLASH + TIZENAPIS_APP_SHARED + TIZENAPIS_APP_SLASH;
865 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
871 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchPtr& event)
878 std::string appId = event->getAppId();
881 LoggerE("App id is mandatory field.");
882 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
886 // if the application is running, send raise event to the app instead of reset the application.
887 // give a second chance to launch application to avoid platform issue.
888 // this retry code will be removed after platform code change.
890 ret = aul_open_app(appId.c_str());
894 // delay 300ms for each retry
897 LoggerD("retry launch request : " << retry);
905 LoggerE("returns Not Found error");
906 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
909 LoggerE("returns internal IPC error");
910 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
913 LoggerE("returns Unknown error");
914 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
918 LoggerD("Success to launch.");
921 Catch (WrtDeviceApis::Commons::Exception)
923 LoggerE("Error on launch : " << _rethrown_exception.GetMessage());
924 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
928 void ApplicationManager::OnRequestReceived(const EventApplicationKillPtr& event)
933 std::string contextId = event->getContextId();
935 if(contextId.empty())
937 LoggerE("Context id is mandatory");
938 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
943 std::stringstream(contextId) >> pid;
946 LoggerE("Given context id is wrong");
947 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
951 // if kill request is come for current context, throw InvalidValueException by spec
952 if (pid == getppid())
954 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
958 char *appIdCStr = NULL;
959 ret = app_manager_get_app_id(pid, &appIdCStr);
960 if (ret != APP_MANAGER_ERROR_NONE)
962 LoggerE("Error while getting app id (" << ret << ")");
963 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
967 std::string appId = appIdCStr;
970 app_context_h appContext;
971 ret = app_manager_get_app_context (appId.c_str(), &appContext);
972 if (ret != APP_MANAGER_ERROR_NONE)
974 LoggerE("Error while getting app context (" << ret << ")");
975 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
980 ret = app_manager_set_app_context_event_cb(app_manager_app_context_event_callback, this);
981 if (ret != APP_MANAGER_ERROR_NONE)
983 LoggerE("Error while registering app context event (" << ret << ")");
984 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
988 ret = app_manager_terminate_app(appContext);
989 if (ret != APP_MANAGER_ERROR_NONE)
991 LoggerE("Error while terminating app (" << ret << ")");
992 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
996 DPL::Mutex::ScopedLock lock(&m_killMapLock);
997 m_killEventMap[pid] = event;
998 event->switchToManualAnswer();
1000 Catch (WrtDeviceApis::Commons::Exception)
1002 LoggerE("Error on kill : " << _rethrown_exception.GetMessage());
1003 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1007 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlPtr& event)
1014 ApplicationControlPtr appControl = event->getAppControl();
1015 if(appControl == NULL)
1017 LoggerE("appControl is mandatory");
1018 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1022 std::string operation = appControl->getOperation();
1023 if(operation.empty())
1025 LoggerE("operation is madatory");
1026 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1030 std::string appId = event->getAppId();
1033 service_create(&service);
1037 service_set_app_id(service, appId.c_str());
1039 // get resolved app id for aliased app id cannot be used to app_manager_get_app_info()
1040 char* resolved_app_id = NULL;
1041 service_get_app_id(service, &resolved_app_id);
1043 // Application exist checking. if specific application is not exist, return Not Found Exception.
1045 if (app_manager_get_app_info(resolved_app_id, &info_h) != APP_MANAGER_ERROR_NONE) {
1046 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1047 if (resolved_app_id) {
1048 free(resolved_app_id);
1050 service_destroy(service);
1054 app_info_destroy(info_h);
1055 if (resolved_app_id) {
1056 free(resolved_app_id);
1060 const char* windowId = IPCMessageSupport::sendMessageToUiProcess("tizen://getWindowHandle", NULL);
1061 if (windowId != NULL)
1063 service_set_window(service, atoi(windowId));
1066 service_set_operation(service, operation.c_str() );
1068 std::string uri = appControl->getUri();
1071 service_set_uri(service, uri.c_str() );
1074 std::string mime = appControl->getMime();
1077 service_set_mime(service, mime.c_str() );
1080 std::string category = appControl->getCategory();
1081 if (!category.empty())
1083 service_set_category(service, category.c_str() );
1086 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1088 if(!appControlDataArray.empty())
1090 LoggerI(" data size : " << appControlDataArray.size());
1092 ApplicationControlDataArray::iterator iter;
1093 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1095 ApplicationControlDataPtr appControlData = *iter;
1097 std::string key = appControlData->getKey();
1101 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1105 std::vector<std::string> valueArray = appControlData->getValue();
1106 size_t size = valueArray.size();
1108 const char **arr = (const char**)calloc(sizeof(char*), size);
1110 for (size_t j = 0; j < size; j++)
1112 arr[j] = valueArray.at(j).c_str();
1115 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1116 const char *keyCStr = key.c_str();
1118 service_add_extra_data(service, keyCStr, arr[0]);
1120 service_add_extra_data_array(service, keyCStr, arr, size);
1128 LaunchAppControlPendingEvent *pendingEvent = NULL;
1129 LaunchAppControlPendingEventMap::DataKeyType key = 0;
1131 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
1134 pendingEvent = new LaunchAppControlPendingEvent((void*)this, eventReply);
1135 key = gLaunchAppControlPendingEventMap.insert(this, pendingEvent);
1138 // give a second chance to launch application to avoid platform issue.
1139 // this retry code will be removed after platform code change.
1141 ret = service_send_launch_request(service, service_reply_callback, (void *)key);
1142 if (ret != SERVICE_ERROR_LAUNCH_REJECTED) {
1145 // delay 300ms for each retry
1148 LoggerD("retry launch request : " << retry);
1151 service_destroy(service);
1153 if(ret != SERVICE_ERROR_NONE)
1157 case SERVICE_ERROR_INVALID_PARAMETER:
1158 LoggerE("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
1159 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1161 case SERVICE_ERROR_OUT_OF_MEMORY:
1162 LoggerE("service_send_launch_request returns SERVICE_ERROR_OUT_OF_MEMORY");
1163 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1165 case SERVICE_ERROR_LAUNCH_REJECTED:
1166 LoggerE("service_send_launch_request returns SERVICE_ERROR_LAUNCH_REJECTED!!!");
1167 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1169 case SERVICE_ERROR_APP_NOT_FOUND:
1170 LoggerE("service_send_launch_request returns SERVICE_ERROR_APP_NOT_FOUND");
1171 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1174 LoggerE("service_send_launch_request returns UNKNOWN ERROR!!!");
1175 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
1181 gLaunchAppControlPendingEventMap.eraseData(key);
1183 delete pendingEvent;
1184 pendingEvent = NULL;
1186 eventReply->cancelRequest();
1187 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(eventReply);
1191 Catch (WrtDeviceApis::Commons::Exception)
1193 LoggerE("Error on launchAppControl : " << _rethrown_exception.GetMessage());
1194 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1198 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlReplyPtr& event)
1200 event->switchToManualAnswer();
1203 void ApplicationManager::OnRequestReceived(const EventApplicationFindAppControlPtr& event)
1207 ApplicationControlPtr appControl = event->getAppControl();
1208 if(appControl == NULL)
1210 LoggerE("appControl is NULL");
1211 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1215 std::string operation = appControl->getOperation();
1216 if(operation.empty())
1218 LoggerE("operation is madatory");
1219 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1224 service_create(&service);
1226 service_set_operation(service, operation.c_str() );
1228 std::string uri = appControl->getUri();
1231 service_set_uri(service, uri.c_str() );
1234 std::string mime = appControl->getMime();
1237 service_set_mime(service, mime.c_str() );
1240 std::string category = appControl->getCategory();
1241 if (!category.empty())
1243 service_set_category(service, category.c_str() );
1246 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1248 if(!appControlDataArray.empty())
1250 LoggerD(" data size : " << appControlDataArray.size());
1252 ApplicationControlDataArray::iterator iter;
1253 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1255 ApplicationControlDataPtr appControlData = *iter;
1257 std::string key = appControlData->getKey();
1261 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1265 std::vector<std::string> valueArray = appControlData->getValue();
1266 size_t size = valueArray.size();
1268 const char **arr = (const char**)calloc(sizeof(char*), size);
1270 for (size_t j = 0; j < size; j++)
1272 arr[j] = valueArray.at(j).c_str();
1275 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1276 const char *keyCStr = key.c_str();
1278 service_add_extra_data(service, keyCStr, arr[0]);
1280 service_add_extra_data_array(service, keyCStr, arr, size);
1288 ApplicationInformationArrayPtr appInfos(new ApplicationInformationArray());
1290 int result = service_foreach_app_matched(service, service_app_matched_callback, (void *)appInfos.Get());
1291 if (result != SERVICE_ERROR_NONE)
1293 LoggerE("service_foreach_app_matched error (" << result << ")");
1294 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1295 service_destroy(service);
1299 service_destroy(service);
1301 event->setAppInfos(appInfos);
1303 Catch (WrtDeviceApis::Commons::Exception)
1305 LoggerE("Error on findAppControl : " << _rethrown_exception.GetMessage());
1306 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1311 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsContextPtr& event)
1317 ApplicationContextArrayPtr appContextArray = event->getAppContextArray();
1318 ret = app_manager_foreach_app_context(app_manager_app_context_callback, appContextArray.Get());
1319 if(ret != APP_MANAGER_ERROR_NONE)
1321 LoggerE("app_manager_foreach_app_context error (" << ret << ")");
1322 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1325 Catch (WrtDeviceApis::Commons::Exception)
1327 LoggerE("Error on : " << _rethrown_exception.GetMessage());
1328 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1334 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsInfoPtr& event)
1339 ApplicationInformationArrayPtr appInfoArray = event->getAppInfoArray();
1340 ret = pkgmgrinfo_appinfo_get_installed_list(installed_app_info_cb, (void*)appInfoArray.Get());
1341 if (ret != PMINFO_R_OK) {
1342 LoggerE("Error on getAppsInfo : ");
1343 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1346 Catch (WrtDeviceApis::Commons::Exception)
1348 LoggerE("Error on getAppsInfo : " << _rethrown_exception.GetMessage());
1349 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1354 void ApplicationManager::OnRequestReceived(const EventApplicationAddAppInfoEventListenerPtr& event)
1358 EventApplicationAppInfoEventListenerEmitterPtr emitter = event->getEmitter();
1360 if(m_installedApplicationsEmitters.size() == 0)
1362 LoggerD("First time registering event listener to this application object.");
1364 // Below can throw Exception
1365 AppManagerWrapperSingleton::Instance().registerAppListChangedCallbacks(this);
1368 m_installedApplicationsEmitters.attach(emitter);
1370 long watchId = AppManagerWrapperSingleton::Instance().getWatchIdAndInc();
1372 m_watchIdMap[watchId] = emitter->getId();
1374 event->setWatchId(watchId);
1376 Catch (WrtDeviceApis::Commons::Exception)
1378 LoggerE("Error on addAppInfoEventListener : " << _rethrown_exception.GetMessage());
1379 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1383 void ApplicationManager::OnRequestReceived(const EventApplicationRemoveAppInfoEventListenerPtr& event)
1387 long watchId = event->getWatchId();
1389 if(m_watchIdMap.find(watchId) == m_watchIdMap.end()) {
1390 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1393 EventApplicationAppInfoEventListenerEmitter::IdType emitterId = m_watchIdMap[watchId];
1395 bool success = m_installedApplicationsEmitters.detach(emitterId);
1397 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1399 if(m_installedApplicationsEmitters.size() == 0)
1401 LoggerD("No more event listener on this application object.");
1403 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
1406 Catch (WrtDeviceApis::Commons::NotFoundException)
1408 LoggerE("Not found : " << _rethrown_exception.GetMessage());
1409 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1411 Catch (WrtDeviceApis::Commons::Exception)
1413 LoggerE("Error on removeAppInfoEventListener : " << _rethrown_exception.GetMessage());
1414 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1420 void ApplicationManager::onAppManagerEventInstalled(const char *appId)
1422 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1424 pkgmgrinfo_appinfo_h handle;
1425 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1426 if (ret != PMINFO_R_OK) {
1427 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1429 ApplicationInformationPtr appInfo = create_app_info(handle);
1430 event->setAppInfo(appInfo);
1431 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1434 event->setType(EventApplicationAppInfoEventListener::OnInstalled);
1435 m_installedApplicationsEmitters.emit(event);
1438 void ApplicationManager::onAppManagerEventUninstalled(const char *appId)
1440 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1442 ApplicationInformationPtr appInfo(new ApplicationInformation(appId));
1444 event->setType(EventApplicationAppInfoEventListener::OnUninstalled);
1445 event->setAppInfo(appInfo);
1446 m_installedApplicationsEmitters.emit(event);
1449 void ApplicationManager::onAppManagerEventUpdated(const char *appId)
1451 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1453 pkgmgrinfo_appinfo_h handle;
1454 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1455 if (ret != PMINFO_R_OK) {
1456 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1458 ApplicationInformationPtr appInfo = create_app_info(handle);
1459 event->setAppInfo(appInfo);
1460 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1463 event->setType(EventApplicationAppInfoEventListener::OnUpdated);
1464 m_installedApplicationsEmitters.emit(event);
1467 void ApplicationManager::initialize()
1469 if (!m_initialized) {
1470 DPL::Mutex::ScopedLock lock(&m_initializationMutex);
1471 if (!m_initialized) {