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 "Application.h"
35 // to launch app by aul
38 // to get package name by appid
40 #include <app_manager.h>
42 // To get cert information from package
43 #include <package_manager.h>
44 #include <package_info.h>
46 // To get app size and installed time
47 #include <pkgmgr-info.h>
52 #include <TimeTracer.h>
57 namespace Application {
60 using namespace WrtDeviceApis;
61 using namespace WrtDeviceApis::Commons;
65 typedef KeyMultiMap<ApplicationManager*, LaunchAppControlPendingEvent> LaunchAppControlPendingEventMap;
66 static LaunchAppControlPendingEventMap gLaunchAppControlPendingEventMap;
68 // Callback from 'app_manager_set_app_context_event_cb'
70 static void app_manager_app_context_event_callback(app_context_h app_context,
71 app_context_event_e event, void *user_data)
75 if(event != APP_CONTEXT_EVENT_TERMINATED)
80 ret = app_context_get_pid(app_context, &pid);
81 if(ret != APP_MANAGER_ERROR_NONE)
83 LoggerE("Fail to get pid of terminated app (" << ret << ")");
87 ApplicationManager* appManager = (ApplicationManager*)(user_data);
88 appManager->invokeManualAnswerKill(pid);
91 // get package name by id
92 static char* getPackageByAppId(const char* appId)
98 ret = app_manager_get_app_info(appId, &handle);
101 LoggerE("Fail to get appinfo");
105 ret = app_info_get_package(handle, &pkgName);
108 LoggerE("Fail to get pkgName");
112 ret = app_info_destroy(handle);
115 LoggerE("Fail to get destory appinfo");
123 // Callback of 'app_manager_foreach_app_context'
124 // Used by 'getAppsContext'
125 static bool app_manager_app_context_callback(app_context_h app_context, void *user_data)
132 std::string contextId;
134 if (user_data == NULL)
139 ret = app_context_get_app_id(app_context, &app_id);
140 if((ret != APP_MANAGER_ERROR_NONE) || (app_id == NULL))
142 LoggerE("Fail to get app id from context (" << ret << ")");
146 ret = app_context_get_pid(app_context, &pid);
147 if(ret != APP_MANAGER_ERROR_NONE)
149 LoggerE("Fail to get pid from context (" << ret << ")");
155 std::stringstream sstream;
157 contextId = sstream.str();
159 ApplicationContextPtr appContext(new ApplicationContext());
160 appContext->setAppId(app_id);
161 appContext->setContextId(contextId);
163 ApplicationContextArray* appContextArray = (ApplicationContextArray*)user_data;
165 appContextArray->push_back(appContext);
173 // Callback of 'service_send_launch_request'
174 // Used by 'launchAppControl'
175 static void service_reply_callback(service_h request, service_h reply,
176 service_result_e result, void *user_data)
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 LoggerE("Unknow Cert type!!!");
234 cert->setType(certName);
235 cert->setValue(cert_value);
237 ApplicationCertArray *certs = (ApplicationCertArray *)user_data;
238 certs->push_back(cert);
243 static std::string get_current_app_id()
245 std::string appId = AppManagerWrapperSingleton::Instance().getCurrentAppId();
249 static int category_cb(const char *category, void *user_data)
251 if (category == NULL)
254 ApplicationInformation* appInfo = (ApplicationInformation*)user_data;
255 appInfo->addCategories(category);
259 static ApplicationInformationPtr create_app_info(pkgmgrinfo_appinfo_h handle)
263 char* iconPath = NULL;
264 bool noDisplay = false;
268 ApplicationInformationPtr appInfo(new ApplicationInformation());
269 ret = pkgmgrinfo_appinfo_get_appid(handle, &appId);
270 if (ret != PMINFO_R_OK) {
271 LoggerD("Fail to get name");
273 appInfo->setAppId(appId);
276 ret = pkgmgrinfo_appinfo_get_label(handle, &name);
277 if ((ret != PMINFO_R_OK) || (name == NULL)) {
278 LoggerD("Fail to get name");
280 appInfo->setName(name);
283 ret = pkgmgrinfo_appinfo_get_icon(handle, &iconPath);
284 if ((ret != PMINFO_R_OK) || (iconPath == NULL)) {
285 LoggerD("Fail to get icon");
287 appInfo->setIconPath(iconPath);
290 ret = pkgmgrinfo_appinfo_is_nodisplay(handle, &noDisplay);
291 if (ret != PMINFO_R_OK) {
292 LoggerD("Fail to get nodisplay");
294 appInfo->setShow(!noDisplay);
297 ret = pkgmgrinfo_appinfo_foreach_category(handle, category_cb, (void*)appInfo.Get());
298 if (ret != PMINFO_R_OK) {
299 LoggerD("Fail to get categories");
302 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
303 if ((ret != PMINFO_R_OK) || (pkgId == NULL)) {
304 LoggerD("Fail to get pkg Id");
306 appInfo->setPackageId(pkgId);
309 char *version = NULL;
310 int installed_time = 0;
311 pkgmgrinfo_pkginfo_h pkginfo_h;
313 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId, &pkginfo_h);
314 if (ret != PMINFO_R_OK) {
315 LoggerE("Fail to get package info");
317 ret = pkgmgrinfo_pkginfo_get_version(pkginfo_h, &version);
318 if (ret != PMINFO_R_OK) {
319 LoggerE("Fail to get version");
321 appInfo->setVersion(version);
324 ret = pkgmgrinfo_pkginfo_get_installed_time(pkginfo_h, &installed_time);
325 if (ret != PMINFO_R_OK) {
326 LoggerE("Fail to get installed date");
328 appInfo->setInstallDate(installed_time);
331 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
334 // remark : attribute "total size" is set at first attribute access time for performance.
339 static int installed_app_info_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
341 ApplicationInformationPtr appInfo = create_app_info(handle);
342 ApplicationInformationArray *appInfoArray = (ApplicationInformationArray*)user_data;
343 appInfoArray->push_back(appInfo);
347 // Callback from 'service_foreach_app_matched'
348 // Used by 'findAppControl'
349 static bool service_app_matched_callback(service_h service, const char *appid, void *user_data)
353 LoggerD("appid is NULL");
356 //ApplicationInformationPtr appInfo(new ApplicationInformation(appid));
357 pkgmgrinfo_appinfo_h handle;
358 int ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
359 if (ret != PMINFO_R_OK) {
360 LoggerD("Fail to get appInfo from appId : " << appid);
362 ApplicationInformationPtr appInfo = create_app_info(handle);
363 pkgmgrinfo_appinfo_destroy_appinfo(handle);
365 ApplicationInformationArray *appInfos = (ApplicationInformationArray *)user_data;
366 appInfos->push_back(appInfo);
372 static int app_meta_data_cb(const char *meta_key, const char *meta_value, void *user_data)
374 if ((meta_key == NULL) || (meta_value == NULL)) {
375 LoggerE("meta_key or meta_value is null");
379 ApplicationMetaDataPtr metaData(new ApplicationMetaData());
381 metaData->setKey(meta_key);
382 metaData->setValue(meta_value);
384 ApplicationMetaDataArray *metaDataArray = (ApplicationMetaDataArray *)user_data;
385 metaDataArray->push_back(metaData);
391 ApplicationManager::ApplicationManager() :
397 ApplicationManager::~ApplicationManager()
399 if(m_installedApplicationsEmitters.size() != 0)
401 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
402 WatchIdMap::iterator iter = m_watchIdMap.begin();
403 for(; iter != m_watchIdMap.end(); iter++)
405 m_installedApplicationsEmitters.detach(iter->second);
409 LaunchAppControlPendingEventMap::DataPtrListType dataPtrList =
410 gLaunchAppControlPendingEventMap.getDataPtrList(this);
412 LaunchAppControlPendingEventMap::DataPtrListType::iterator iter = dataPtrList.begin();
413 for(; iter != dataPtrList.end(); iter++)
418 gLaunchAppControlPendingEventMap.eraseKey(this);
420 // unset context event callback which is registered by kill().
421 app_manager_unset_app_context_event_cb();
424 void ApplicationManager::launch(const EventApplicationLaunchPtr& event)
426 if (m_initialized == false) {
430 EventRequestReceiver<EventApplicationLaunch>::PostRequest(event);
433 void ApplicationManager::kill(const EventApplicationKillPtr& event)
435 if (m_initialized == false) {
439 EventRequestReceiver<EventApplicationKill>::PostRequest(event);
442 void ApplicationManager::launchAppControl(const EventApplicationLaunchAppControlPtr& event)
444 if (m_initialized == false) {
448 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
449 if(eventReply != NULL)
450 EventRequestReceiver<EventApplicationLaunchAppControlReply>::PostRequest(eventReply);
452 EventRequestReceiver<EventApplicationLaunchAppControl>::PostRequest(event);
455 void ApplicationManager::findAppControl(const EventApplicationFindAppControlPtr& event)
457 if (m_initialized == false) {
461 EventRequestReceiver<EventApplicationFindAppControl>::PostRequest(event);
464 void ApplicationManager::getAppsContext(const EventApplicationGetAppsContextPtr& event)
466 if (m_initialized == false) {
470 EventRequestReceiver<EventApplicationGetAppsContext>::PostRequest(event);
474 void ApplicationManager::getAppsInfo(const EventApplicationGetAppsInfoPtr& event)
476 if (m_initialized == false) {
480 EventRequestReceiver<EventApplicationGetAppsInfo>::PostRequest(event);
484 void ApplicationManager::addAppInfoEventListener(const EventApplicationAddAppInfoEventListenerPtr& event)
486 if (m_initialized == false) {
490 EventRequestReceiver<EventApplicationAddAppInfoEventListener>::PostRequest(event);
493 void ApplicationManager::removeAppInfoEventListener(const EventApplicationRemoveAppInfoEventListenerPtr& event)
495 if (m_initialized == false) {
499 EventRequestReceiver<EventApplicationRemoveAppInfoEventListener>::PostRequest(event);
503 void ApplicationManager::invokeManualAnswerLaunchAppControl(service_h request, service_h reply,
504 service_result_e result,
505 EventApplicationLaunchAppControlReplyPtr &event)
511 if(result == SERVICE_RESULT_SUCCEEDED)
513 // create new service object to store result.
514 ApplicationControlDataArrayPtr appControlDataArray(new ApplicationControlDataArray());
516 int result = service_foreach_extra_data(reply, service_extra_data_callback, appControlDataArray.Get());
517 if( result == SERVICE_ERROR_NONE)
519 event->setAppControlDataArray(appControlDataArray);
523 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
526 else if(result == SERVICE_RESULT_FAILED || result == SERVICE_RESULT_CANCELED)
528 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
531 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(event);
534 void ApplicationManager::invokeManualAnswerKill(int pid)
536 DPL::Mutex::ScopedLock lock(&m_killMapLock);
538 std::map<int, EventApplicationKillPtr>::iterator it = m_killEventMap.find(pid);
539 if (it == m_killEventMap.end()) {
543 EventApplicationKillPtr event = it->second;
544 m_killEventMap.erase(it);
546 EventRequestReceiver<EventApplicationKill>::ManualAnswer(event);
550 bool ApplicationManager::service_extra_data_callback(service_h service, const char *key, void* user_data)
554 ApplicationControlDataArray* appControlDataArray = (ApplicationControlDataArray*)user_data;
556 bool isArray = false;
557 ret = service_is_extra_data_array(service, key, &isArray);
558 if (ret != SERVICE_ERROR_NONE)
560 LoggerE("service_is_extra_data_array passes error");
561 // fail to checking. go to next extra data.
565 std::string keyStr(key);
572 ret = service_get_extra_data_array(service, key, &value, &length);
575 case SERVICE_ERROR_NONE: {
576 std::vector<std::string> valArray;
577 LoggerI("value length : " << length);
578 for (int i = 0; i < length; i++)
582 valArray.push_back(value[i]);
586 ApplicationControlDataPtr appControlData(new ApplicationControlData());
587 appControlData->setKey(keyStr);
588 appControlData->setValue(valArray);
589 appControlDataArray->push_back(appControlData);
591 for (int i = 0; i < length; i++)
600 case SERVICE_ERROR_INVALID_PARAMETER:
601 LoggerE("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
603 case SERVICE_ERROR_KEY_NOT_FOUND:
604 LoggerE("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
606 case SERVICE_ERROR_OUT_OF_MEMORY:
607 LoggerE("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
610 LoggerE("service_get_extra_data retuns Error");
618 ret = service_get_extra_data(service, key, &value);
621 case SERVICE_ERROR_NONE:
625 LoggerE("service_get_extra_data returns NULL");
629 std::vector<std::string> valArray;
630 valArray.push_back(value);
632 ApplicationControlDataPtr appControlData(new ApplicationControlData());
633 appControlData->setKey(keyStr);
634 appControlData->setValue(valArray);
635 appControlDataArray->push_back(appControlData);
642 case SERVICE_ERROR_INVALID_PARAMETER:
643 LoggerE("service_get_extra_data retuns SERVICE_ERROR_INVALID_PARAMETER");
645 case SERVICE_ERROR_KEY_NOT_FOUND:
646 LoggerE("service_get_extra_data retuns SERVICE_ERROR_KEY_NOT_FOUND");
648 case SERVICE_ERROR_OUT_OF_MEMORY:
649 LoggerE("service_get_extra_data retuns SERVICE_ERROR_OUT_OF_MEMORY");
652 LoggerE("service_get_extra_data retuns Error");
661 ApplicationPtr ApplicationManager::getCurrentApplication()
663 std::string appId = get_current_app_id();
665 //ApplicationInformationPtr appinfo(new ApplicationInformation(appId));
666 pkgmgrinfo_appinfo_h handle;
667 TIME_TRACER_ITEM_BEGIN("(getCurrentApplication)pkgmgrinfo_appinfo_get_appinfo", 0);
668 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
669 TIME_TRACER_ITEM_END("(getCurrentApplication)pkgmgrinfo_appinfo_get_appinfo", 0);
670 if (ret != PMINFO_R_OK) {
671 LoggerE("Fail to get appInfo");
672 ThrowMsg(UnknownException, "pkgmgrinfo_appinfo_get_appinfo error : unknown error");
674 ApplicationInformationPtr appInfo = create_app_info(handle);
675 pkgmgrinfo_appinfo_destroy_appinfo(handle);
678 ApplicationPtr app(new Application());
679 app->setAppInfo(appInfo);
681 LoggerD("set appinfo to application");
683 //int pid = getpid();
685 std::stringstream sstr;
687 app->setContextId(sstr.str());
694 ApplicationContextPtr ApplicationManager::getAppContext(const std::string id)
698 std::string contextId = id;
701 if (contextId.empty())
706 std::stringstream sstr;
708 contextId = sstr.str();
712 std::stringstream(contextId) >> pid;
715 LoggerE("Given contextId is wrong");
716 ThrowMsg(NotFoundException, "Given contextId is wrong");
722 TIME_TRACER_ITEM_BEGIN("(getAppContext)app_manager_get_app_id", 0);
723 ret = app_manager_get_app_id(pid, &app_id);
724 TIME_TRACER_ITEM_END("(getAppContext)app_manager_get_app_id", 0);
725 if(ret != APP_MANAGER_ERROR_NONE)
732 case APP_MANAGER_ERROR_NO_SUCH_APP:
733 case APP_MANAGER_ERROR_INVALID_PARAMETER:
734 LoggerE("app_manager_get_app_id error : no such app");
735 ThrowMsg(NotFoundException, "app_manager_get_app_id error : no such app");
737 LoggerE("app_manager_get_app_id error (" << ret << ")");
738 ThrowMsg(UnknownException, "app_manager_get_app_id error : unknown error");
742 ApplicationContextPtr appContext(new ApplicationContext());
743 appContext->setAppId(app_id);
744 appContext->setContextId(contextId);
753 ApplicationInformationPtr ApplicationManager::getAppInfo(const std::string id)
755 std::string appId = id;
756 // in case of no argument, get application information of current.
759 appId = get_current_app_id();
762 pkgmgrinfo_appinfo_h handle;
763 TIME_TRACER_ITEM_BEGIN("(getAppInfo)pkgmgrinfo_appinfo_get_appinfo", 0);
764 int ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
765 TIME_TRACER_ITEM_END("(getAppInfo)pkgmgrinfo_appinfo_get_appinfo", 0);
766 if (ret != PMINFO_R_OK) {
767 ThrowMsg(NotFoundException, "Can not get appinfo");
770 ApplicationInformationPtr appInfo = create_app_info(handle);
772 pkgmgrinfo_appinfo_destroy_appinfo(handle);
778 ApplicationCertArrayPtr ApplicationManager::getAppCerts(const std::string id)
780 std::string appId = id;
782 // in case of no argument, get application information of current.
785 appId = get_current_app_id();
788 TIME_TRACER_ITEM_BEGIN("(getAppCerts)getPackageByAppId", 0);
789 char* package = getPackageByAppId(appId.c_str());
790 TIME_TRACER_ITEM_END("(getAppCerts)getPackageByAppId", 0);
793 LoggerE("Can not get package");
794 ThrowMsg(NotFoundException, "Can not get package");
797 package_info_h pkg_info;
800 TIME_TRACER_ITEM_BEGIN("(getAppCerts)package_manager_get_package_info", 0);
801 result = package_manager_get_package_info(package, &pkg_info);
802 TIME_TRACER_ITEM_END("(getAppCerts)package_manager_get_package_info", 0);
803 if (result != PACKAGE_MANAGER_ERROR_NONE)
805 ThrowMsg(UnknownException, "Can not get package info");
808 ApplicationCertArrayPtr certArray(new ApplicationCertArray());
810 TIME_TRACER_ITEM_BEGIN("(getAppCerts)package_info_foreach_cert_info", 0);
811 result = package_info_foreach_cert_info(pkg_info, package_cert_cb, (void*)certArray.Get());
812 TIME_TRACER_ITEM_END("(getAppCerts)package_info_foreach_cert_info", 0);
813 if ((result != PACKAGE_MANAGER_ERROR_NONE) && (result != PACKAGE_MANAGER_ERROR_IO_ERROR))
815 ThrowMsg(UnknownException, "Can not get package cert info");
821 std::string ApplicationManager::getAppSharedURI(const std::string id)
823 #define TIZENAPIS_APP_FILE_SCHEME "file://"
824 #define TIZENAPIS_APP_SLASH "/"
825 #define TIZENAPIS_APP_SHARED "shared"
830 appId = get_current_app_id();
836 char* pkg_name = NULL;
837 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)app_manager_get_app_info", 0);
838 int ret = app_manager_get_app_info(appId.c_str(), &handle);
839 TIME_TRACER_ITEM_END("(getAppSharedURI)app_manager_get_app_info", 0);
840 if (ret != APP_ERROR_NONE) {
841 LoggerD("Fail to get appinfo");
842 //throw NotFoundException("Fail to get appinfo");
843 ThrowMsg(NotFoundException, "Fail to get appinfo");
846 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)app_info_get_package", 0);
847 ret = app_info_get_package(handle, &pkg_name);
848 TIME_TRACER_ITEM_END("(getAppSharedURI)app_info_get_package", 0);
849 if ((ret != APP_ERROR_NONE) || (pkg_name == NULL)) {
850 LoggerD("Fail to get pkg_name");
851 //throw NotFoundException("Fail to get pkg_name");
852 ThrowMsg(NotFoundException, "Fail to get pkg_name");
855 app_info_destroy(handle);
857 pkgmgrinfo_pkginfo_h pkginfo_h;
858 char* root_path = NULL;
860 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)pkgmgrinfo_pkginfo_get_pkginfo", 0);
861 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &pkginfo_h);
862 TIME_TRACER_ITEM_END("(getAppSharedURI)pkgmgrinfo_pkginfo_get_pkginfo", 0);
863 if (ret != PMINFO_R_OK) {
865 //throw UnknownException("Fail to get pkginfo");
866 ThrowMsg(UnknownException, "Fail to get pkginfo");
869 TIME_TRACER_ITEM_BEGIN("(getAppSharedURI)pkgmgrinfo_pkginfo_get_root_path", 0);
870 ret = pkgmgrinfo_pkginfo_get_root_path(pkginfo_h, &root_path);
871 TIME_TRACER_ITEM_END("(getAppSharedURI)pkgmgrinfo_pkginfo_get_root_path", 0);
872 if ((ret != PMINFO_R_OK) || (root_path == NULL)) {
873 LoggerE("Fail to get root path");
875 //throw UnknownException("Fail to get rotpath");
876 ThrowMsg(UnknownException, "Fail to get rotpath");
879 std::string sharedURI = TIZENAPIS_APP_FILE_SCHEME + std::string(root_path) + TIZENAPIS_APP_SLASH + TIZENAPIS_APP_SHARED + TIZENAPIS_APP_SLASH;
882 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo_h);
887 ApplicationMetaDataArrayPtr ApplicationManager::getAppMetaData(const std::string id)
889 std::string appId = id;
891 // in case of no argument, get application information of current.
894 appId = get_current_app_id();
898 pkgmgrinfo_appinfo_h handle;
900 TIME_TRACER_ITEM_BEGIN("(getAppMetaData)pkgmgrinfo_appinfo_get_appinfo", 0);
901 ret = pkgmgrinfo_appinfo_get_appinfo(appId.c_str(), &handle);
902 TIME_TRACER_ITEM_END("(getAppMetaData)pkgmgrinfo_appinfo_get_appinfo", 0);
904 if (ret != PMINFO_R_OK) {
905 ThrowMsg(NotFoundException, "Cannot found application with given appId");
908 ApplicationMetaDataArrayPtr metaDataArray(new ApplicationMetaDataArray());
910 TIME_TRACER_ITEM_BEGIN("(getAppMetaData)pkgmgrinfo_appinfo_foreach_metadata", 0);
911 ret = pkgmgrinfo_appinfo_foreach_metadata(handle, app_meta_data_cb, (void*)metaDataArray.Get());
912 TIME_TRACER_ITEM_END("(getAppMetaData)pkgmgrinfo_appinfo_foreach_metadata", 0);
914 if (ret != PMINFO_R_OK) {
915 LoggerE("pkgmgrinfo_appinfo_metadata_filter_foreach() failed");
916 pkgmgrinfo_appinfo_destroy_appinfo(handle);
917 ThrowMsg(UnknownException, "fail to get custom tag");
920 pkgmgrinfo_appinfo_destroy_appinfo(handle);
922 return metaDataArray;
925 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchPtr& event)
932 std::string appId = event->getAppId();
935 LoggerE("App id is mandatory field.");
936 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
940 // if the application is running, send raise event to the app instead of reset the application.
941 // give a second chance to launch application to avoid platform issue.
942 // this retry code will be removed after platform code change.
944 ret = aul_open_app(appId.c_str());
948 // delay 300ms for each retry
951 LoggerD("retry launch request : " << retry);
959 LoggerE("returns Not Found error");
960 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
963 LoggerE("returns internal IPC error");
964 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
967 LoggerE("returns Unknown error");
968 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
972 LoggerD("Success to launch.");
975 Catch (WrtDeviceApis::Commons::Exception)
977 LoggerE("Error on launch : " << _rethrown_exception.GetMessage());
978 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
982 // Because of platform issue, some termination event is not come to app_manager_set_app_context_event_cb().
983 // To prevent blocking issue by wating callback function, add termination checking code.
984 // this function check whether callback is called or not after 3 sec. if callback is not called,
985 // calls callback function forcibily.
986 // After fixing platform issue, below code should be removed
989 ApplicationManager* appManager;
991 EventApplicationKillPtr event;
994 gboolean check_terminate_callback (gpointer user_data)
996 //EventApplicationKillPtr event = (EventApplicationKillPtr)data;
997 KILL_DATA_T* data = (KILL_DATA_T*)user_data;
1000 if (app_manager_get_app_id(data->pid, &appId) == APP_MANAGER_ERROR_NONE) {
1001 // if context is still alive, error callback should be called.
1002 data->event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1005 data->appManager->invokeManualAnswerKill(data->pid);
1011 void ApplicationManager::OnRequestReceived(const EventApplicationKillPtr& event)
1016 std::string contextId = event->getContextId();
1018 if(contextId.empty())
1020 LoggerE("Context id is mandatory");
1021 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1026 std::stringstream(contextId) >> pid;
1029 LoggerE("Given context id is wrong");
1030 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1034 // if kill request is come for current context, throw InvalidValueException by spec
1035 if (pid == getppid())
1037 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1041 char *appIdCStr = NULL;
1042 ret = app_manager_get_app_id(pid, &appIdCStr);
1043 if (ret != APP_MANAGER_ERROR_NONE)
1045 LoggerE("Error while getting app id (" << ret << ")");
1046 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1050 std::string appId = appIdCStr;
1053 app_context_h appContext;
1054 ret = app_manager_get_app_context (appId.c_str(), &appContext);
1055 if (ret != APP_MANAGER_ERROR_NONE)
1057 LoggerE("Error while getting app context (" << ret << ")");
1058 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1063 ret = app_manager_set_app_context_event_cb(app_manager_app_context_event_callback, this);
1064 if (ret != APP_MANAGER_ERROR_NONE)
1066 LoggerE("Error while registering app context event (" << ret << ")");
1067 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
1071 ret = app_manager_terminate_app(appContext);
1072 if (ret != APP_MANAGER_ERROR_NONE)
1074 LoggerE("Error while terminating app (" << ret << ")");
1075 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
1079 // Because of platform issue, some termination event is not come to app_manager_set_app_context_event_cb().
1080 // To prevent blocking issue by wating callback function, add termination checking code.
1081 // this function check whether callback is called or not after 3 sec. if callback is not called,
1082 // calls callback function forcibily.
1083 // After fixing platform issue, below code should be removed
1085 KILL_DATA_T *data = new KILL_DATA_T;
1087 data->appManager = this;
1088 data->event = event;
1089 g_timeout_add(3000, check_terminate_callback, (void*)data);
1092 DPL::Mutex::ScopedLock lock(&m_killMapLock);
1093 m_killEventMap[pid] = event;
1094 event->switchToManualAnswer();
1096 Catch (WrtDeviceApis::Commons::Exception)
1098 LoggerE("Error on kill : " << _rethrown_exception.GetMessage());
1099 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1103 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlPtr& event)
1110 ApplicationControlPtr appControl = event->getAppControl();
1111 if(appControl == NULL)
1113 LoggerE("appControl is mandatory");
1114 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1118 std::string operation = appControl->getOperation();
1119 if(operation.empty())
1121 LoggerE("operation is madatory");
1122 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1126 std::string appId = event->getAppId();
1129 service_create(&service);
1133 service_set_app_id(service, appId.c_str());
1135 // get resolved app id for aliased app id cannot be used to app_manager_get_app_info()
1136 char* resolved_app_id = NULL;
1137 service_get_app_id(service, &resolved_app_id);
1139 // Application exist checking. if specific application is not exist, return Not Found Exception.
1141 if (app_manager_get_app_info(resolved_app_id, &info_h) != APP_MANAGER_ERROR_NONE) {
1142 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1143 if (resolved_app_id) {
1144 free(resolved_app_id);
1146 service_destroy(service);
1150 app_info_destroy(info_h);
1151 if (resolved_app_id) {
1152 free(resolved_app_id);
1156 const char* windowId = IPCMessageSupport::sendMessageToUiProcess("tizen://getWindowHandle", NULL);
1157 if (windowId != NULL)
1159 service_set_window(service, atoi(windowId));
1162 service_set_operation(service, operation.c_str() );
1164 std::string uri = appControl->getUri();
1167 service_set_uri(service, uri.c_str() );
1170 std::string mime = appControl->getMime();
1173 service_set_mime(service, mime.c_str() );
1176 std::string category = appControl->getCategory();
1177 if (!category.empty())
1179 service_set_category(service, category.c_str() );
1182 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1184 if(!appControlDataArray.empty())
1186 LoggerI(" data size : " << appControlDataArray.size());
1188 ApplicationControlDataArray::iterator iter;
1189 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1191 ApplicationControlDataPtr appControlData = *iter;
1193 std::string key = appControlData->getKey();
1197 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1201 std::vector<std::string> valueArray = appControlData->getValue();
1202 size_t size = valueArray.size();
1204 const char **arr = (const char**)calloc(sizeof(char*), size);
1206 for (size_t j = 0; j < size; j++)
1208 arr[j] = valueArray.at(j).c_str();
1211 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1212 const char *keyCStr = key.c_str();
1214 service_add_extra_data(service, keyCStr, arr[0]);
1216 service_add_extra_data_array(service, keyCStr, arr, size);
1224 LaunchAppControlPendingEvent *pendingEvent = NULL;
1225 LaunchAppControlPendingEventMap::DataKeyType key = 0;
1227 EventApplicationLaunchAppControlReplyPtr eventReply = event->getEventReply();
1230 pendingEvent = new LaunchAppControlPendingEvent((void*)this, eventReply);
1231 key = gLaunchAppControlPendingEventMap.insert(this, pendingEvent);
1234 // give a second chance to launch application to avoid platform issue.
1235 // this retry code will be removed after platform code change.
1237 ret = service_send_launch_request(service, service_reply_callback, (void *)key);
1238 if (ret != SERVICE_ERROR_LAUNCH_REJECTED) {
1241 // delay 300ms for each retry
1244 LoggerD("retry launch request : " << retry);
1247 service_destroy(service);
1249 if(ret != SERVICE_ERROR_NONE)
1253 case SERVICE_ERROR_INVALID_PARAMETER:
1254 LoggerE("service_send_launch_request returns SERVICE_ERROR_INVALID_PARAMETER");
1255 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1257 case SERVICE_ERROR_OUT_OF_MEMORY:
1258 LoggerE("service_send_launch_request returns SERVICE_ERROR_OUT_OF_MEMORY");
1259 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1261 case SERVICE_ERROR_LAUNCH_REJECTED:
1262 LoggerE("service_send_launch_request returns SERVICE_ERROR_LAUNCH_REJECTED!!!");
1263 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1265 case SERVICE_ERROR_APP_NOT_FOUND:
1266 LoggerE("service_send_launch_request returns SERVICE_ERROR_APP_NOT_FOUND");
1267 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1270 LoggerE("service_send_launch_request returns UNKNOWN ERROR!!!");
1271 event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
1277 gLaunchAppControlPendingEventMap.eraseData(key);
1279 delete pendingEvent;
1280 pendingEvent = NULL;
1282 eventReply->cancelRequest();
1283 EventRequestReceiver<EventApplicationLaunchAppControlReply>::ManualAnswer(eventReply);
1287 Catch (WrtDeviceApis::Commons::Exception)
1289 LoggerE("Error on launchAppControl : " << _rethrown_exception.GetMessage());
1290 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1294 void ApplicationManager::OnRequestReceived(const EventApplicationLaunchAppControlReplyPtr& event)
1296 event->switchToManualAnswer();
1299 void ApplicationManager::OnRequestReceived(const EventApplicationFindAppControlPtr& event)
1303 ApplicationControlPtr appControl = event->getAppControl();
1304 if(appControl == NULL)
1306 LoggerE("appControl is NULL");
1307 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1311 std::string operation = appControl->getOperation();
1312 if(operation.empty())
1314 LoggerE("operation is madatory");
1315 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1320 service_create(&service);
1322 service_set_operation(service, operation.c_str() );
1324 std::string uri = appControl->getUri();
1327 service_set_uri(service, uri.c_str() );
1330 std::string mime = appControl->getMime();
1333 service_set_mime(service, mime.c_str() );
1336 std::string category = appControl->getCategory();
1337 if (!category.empty())
1339 service_set_category(service, category.c_str() );
1342 std::vector<ApplicationControlDataPtr> appControlDataArray = appControl->getAppControlDataArray();
1344 if(!appControlDataArray.empty())
1346 LoggerD(" data size : " << appControlDataArray.size());
1348 ApplicationControlDataArray::iterator iter;
1349 for(iter = appControlDataArray.begin(); iter != appControlDataArray.end(); iter++)
1351 ApplicationControlDataPtr appControlData = *iter;
1353 std::string key = appControlData->getKey();
1357 event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
1361 std::vector<std::string> valueArray = appControlData->getValue();
1362 size_t size = valueArray.size();
1364 const char **arr = (const char**)calloc(sizeof(char*), size);
1366 for (size_t j = 0; j < size; j++)
1368 arr[j] = valueArray.at(j).c_str();
1371 // @20121207-wscho: roll-back to return extra-data instead of extra-data array when the value size is one.
1372 const char *keyCStr = key.c_str();
1374 service_add_extra_data(service, keyCStr, arr[0]);
1376 service_add_extra_data_array(service, keyCStr, arr, size);
1384 ApplicationInformationArrayPtr appInfos(new ApplicationInformationArray());
1386 int result = service_foreach_app_matched(service, service_app_matched_callback, (void *)appInfos.Get());
1387 if (result != SERVICE_ERROR_NONE)
1389 LoggerE("service_foreach_app_matched error (" << result << ")");
1390 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1391 service_destroy(service);
1395 service_destroy(service);
1397 event->setAppInfos(appInfos);
1399 Catch (WrtDeviceApis::Commons::Exception)
1401 LoggerE("Error on findAppControl : " << _rethrown_exception.GetMessage());
1402 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1407 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsContextPtr& event)
1413 ApplicationContextArrayPtr appContextArray = event->getAppContextArray();
1414 ret = app_manager_foreach_app_context(app_manager_app_context_callback, appContextArray.Get());
1415 if(ret != APP_MANAGER_ERROR_NONE)
1417 LoggerE("app_manager_foreach_app_context error (" << ret << ")");
1418 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1421 Catch (WrtDeviceApis::Commons::Exception)
1423 LoggerE("Error on : " << _rethrown_exception.GetMessage());
1424 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1430 void ApplicationManager::OnRequestReceived(const EventApplicationGetAppsInfoPtr& event)
1435 ApplicationInformationArrayPtr appInfoArray = event->getAppInfoArray();
1436 ret = pkgmgrinfo_appinfo_get_installed_list(installed_app_info_cb, (void*)appInfoArray.Get());
1437 if (ret != PMINFO_R_OK) {
1438 LoggerE("Error on getAppsInfo : ");
1439 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1442 Catch (WrtDeviceApis::Commons::Exception)
1444 LoggerE("Error on getAppsInfo : " << _rethrown_exception.GetMessage());
1445 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1450 void ApplicationManager::OnRequestReceived(const EventApplicationAddAppInfoEventListenerPtr& event)
1454 EventApplicationAppInfoEventListenerEmitterPtr emitter = event->getEmitter();
1456 if(m_installedApplicationsEmitters.size() == 0)
1458 LoggerD("First time registering event listener to this application object.");
1460 // Below can throw Exception
1461 AppManagerWrapperSingleton::Instance().registerAppListChangedCallbacks(this);
1464 m_installedApplicationsEmitters.attach(emitter);
1466 long watchId = AppManagerWrapperSingleton::Instance().getWatchIdAndInc();
1468 m_watchIdMap[watchId] = emitter->getId();
1470 event->setWatchId(watchId);
1472 Catch (WrtDeviceApis::Commons::Exception)
1474 LoggerE("Error on addAppInfoEventListener : " << _rethrown_exception.GetMessage());
1475 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1479 void ApplicationManager::OnRequestReceived(const EventApplicationRemoveAppInfoEventListenerPtr& event)
1483 long watchId = event->getWatchId();
1485 if(m_watchIdMap.find(watchId) == m_watchIdMap.end()) {
1486 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1489 EventApplicationAppInfoEventListenerEmitter::IdType emitterId = m_watchIdMap[watchId];
1491 bool success = m_installedApplicationsEmitters.detach(emitterId);
1493 ThrowMsg(NotFoundException, "No watchId : " << watchId);
1495 if(m_installedApplicationsEmitters.size() == 0)
1497 LoggerD("No more event listener on this application object.");
1499 AppManagerWrapperSingleton::Instance().unregisterAppListChangedCallbacks(this);
1502 Catch (WrtDeviceApis::Commons::NotFoundException)
1504 LoggerE("Not found : " << _rethrown_exception.GetMessage());
1505 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1507 Catch (WrtDeviceApis::Commons::Exception)
1509 LoggerE("Error on removeAppInfoEventListener : " << _rethrown_exception.GetMessage());
1510 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
1516 void ApplicationManager::onAppManagerEventInstalled(const char *appId)
1518 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1520 pkgmgrinfo_appinfo_h handle;
1521 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1522 if (ret != PMINFO_R_OK) {
1523 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1525 ApplicationInformationPtr appInfo = create_app_info(handle);
1526 event->setAppInfo(appInfo);
1527 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1530 event->setType(EventApplicationAppInfoEventListener::OnInstalled);
1531 m_installedApplicationsEmitters.emit(event);
1534 void ApplicationManager::onAppManagerEventUninstalled(const char *appId)
1536 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1538 ApplicationInformationPtr appInfo(new ApplicationInformation(appId));
1540 event->setType(EventApplicationAppInfoEventListener::OnUninstalled);
1541 event->setAppInfo(appInfo);
1542 m_installedApplicationsEmitters.emit(event);
1545 void ApplicationManager::onAppManagerEventUpdated(const char *appId)
1547 EventApplicationAppInfoEventListenerPtr event(new EventApplicationAppInfoEventListener());
1549 pkgmgrinfo_appinfo_h handle;
1550 int ret = pkgmgrinfo_appinfo_get_appinfo(appId, &handle);
1551 if (ret != PMINFO_R_OK) {
1552 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1554 ApplicationInformationPtr appInfo = create_app_info(handle);
1555 event->setAppInfo(appInfo);
1556 pkgmgrinfo_appinfo_destroy_appinfo(handle);
1559 event->setType(EventApplicationAppInfoEventListener::OnUpdated);
1560 m_installedApplicationsEmitters.emit(event);
1563 void ApplicationManager::initialize()
1565 if (!m_initialized) {
1566 DPL::Mutex::ScopedLock lock(&m_initializationMutex);
1567 if (!m_initialized) {