upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Application / Application.cpp
index ae13b88..97d0b70 100755 (executable)
@@ -40,10 +40,18 @@ using namespace DPL;
 using namespace WrtDeviceApis;
 using namespace WrtDeviceApis::Commons;
 using namespace Platform;
-
 namespace {
 
        extern "C" int service_create_event(bundle *data, struct service_s **service);
+       extern "C" int aul_listen_app_dead_signal(int (*func) (int, void *), void *data);
+
+       static int app_dead_handler(int pid, void* user_data)
+       {
+               LogDebug("kill Callback called");
+               Application* appManager = (Application*)(user_data);
+               appManager->callKillEventCallback(pid);
+               return 0;
+       }
 
        // callback function to get package name. this callback is called from getPackageById()
        static ail_cb_ret_e appinfoCallback(const ail_appinfo_h appinfo, void *user_data)
@@ -192,9 +200,10 @@ namespace {
                                free(iconPath);
                        if (version)
                                free(version);
-                       if (appid)
-                               free(appid);
                }
+               if (appid)
+                       free(appid);
+               
                return true;
        }
 
@@ -267,7 +276,6 @@ namespace {
                                        for (int i = 0; i < length; i++) {
                                                valArray.push_back(value[i]);
                                        }
-
                                        event->addServiceData(keyStr, valArray);
 
                                        for (int i = 0; i < length; i++) {
@@ -276,7 +284,6 @@ namespace {
                                        }
                                        if (value)
                                                free(value);
-                                       
                                        break;
                                }
                                case SERVICE_ERROR_INVALID_PARAMETER:
@@ -369,17 +376,6 @@ void Application::kill(const EventManageApplicationPtr& event)
        EventRequestReceiver<EventManageApplication>::PostRequest(event);
 }
 
-void Application::exit()
-{
-       // should be changed to event raise
-       app_efl_exit();
-}
-
-void Application::hide()
-{
-       // hide application call
-}
-
 void Application::listApplications(const EventListInstalledApplicationsPtr& event)
 {
        if (m_initialized == false) {
@@ -564,7 +560,19 @@ void Application::launchServiceManualAnswer(service_h request, service_h reply,
        EventRequestReceiver<EventLaunchService>::ManualAnswer(m_eventLaunchServicePtr);
 }
 
-
+void Application::callKillEventCallback(int pid)
+{
+       DPL::Mutex::ScopedLock lock(&m_killMapLock);
+       std::map<int, EventManageApplicationPtr>::iterator it = m_killEventMap.find(pid);
+       if (it == m_killEventMap.end()) {
+               return;
+       } else {
+               EventManageApplicationPtr event = it->second;
+               m_killEventMap.erase(it);
+               LogDebug("calling kill success callback for pid::" << pid);
+               EventRequestReceiver<EventManageApplication>::ManualAnswer(event);
+       }
+}
 void Application::OnRequestReceived(const EventManageApplicationPtr& event)
 {
        switch (event->getEventType()) {
@@ -608,20 +616,47 @@ void Application::OnRequestReceived(const EventManageApplicationPtr& event)
                        break;
                }
                case EventManageApplication::APP_MANAGER_KILL_APPLICATION: {
-                       const char *contextId = event->getApplicationContext()->getContextId().c_str();
+                       const char *contextId = event->getContextId().c_str();
                        if (contextId == NULL) {
                                LogError("[ERROR] Can not get context id from context");
                                event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
                        }
-                       
+
+                       LogError("[ERROR] contextId = "<<contextId);
                        int pid = atoi(contextId);
-                       int result = aul_terminate_pid(pid);
-                       if (result < 0) {
-                               LogError("[ERROR] Fail to terminate application with context");
-                               if (result == AUL_R_EINVAL) {
-                                       event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
+                       LogError("[ERROR] pid = "<<pid);
+                       if (pid <= 0) {
+                               event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
+                               break;
+                       }
+                       char buf[256];
+                       if (aul_app_get_pkgname_bypid(pid, buf, 256) != AUL_R_OK) {
+                               event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
+                               break;
+                       }
+                       LogError("[ERROR] pkg = "<<buf);
+
+                       if (!aul_app_is_running(buf)) {
+                               event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
+                               break;
+                       }
+                       
+                       int result = aul_listen_app_dead_signal(app_dead_handler, this);
+                       if (result != 0) {
+                               event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
+                       } else {
+                               result = aul_terminate_pid(pid);
+                               if (result < 0) {
+                                       LogError("[ERROR] Fail to terminate application with context");
+                                       if (result == AUL_R_EINVAL) {
+                                               event->setExceptionCode(Commons::ExceptionCodes::InvalidArgumentException);
+                                       } else {
+                                               event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
+                                       }
                                } else {
-                                       event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
+                                       DPL::Mutex::ScopedLock lock(&m_killMapLock);
+                                       m_killEventMap[pid] = event;
+                                       event->switchToManualAnswer();
                                }
                        }
                        break;
@@ -689,32 +724,49 @@ void Application::OnRequestReceived(const EventGetApplicationPtr& event)
                        break;
                }
                case EventGetApplication::APP_MANAGER_GET_CONTEXT: {
-                       char *package;
-                       char contextId[PATH_MAX];
-                       snprintf(contextId, PATH_MAX, "%d", getpid());
-
-                       if (app_get_package(&package) != 0) {
-                               LogError("[ERROR] Can not get package name from current pid");
-                               event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
-                       } else {
-                               char *appId = getIdFromPackage(package);
-                               if (appId == NULL) {
+                       charpackage;
+                       char *appId;
+                       std::string contextId = event->getContextId();
+                       
+                       if (contextId.empty()) {
+                               char currPID[PATH_MAX];
+                               snprintf(currPID, PATH_MAX, "%d", getpid());
+                               contextId = std::string(currPID);
+                               
+                               if (app_get_package(&package) != APP_ERROR_NONE) {
                                        LogError("[ERROR] Can not get package name from current pid");
                                        event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
                                        break;
                                }
-                               
-                               ApplicationContextPtr appContext(new ApplicationContext());
-                               appContext->setAppId(appId);
-                               appContext->setContextId(contextId);
-
-                               event->setApplicationContext(appContext);
-                               
-                               if (package)
-                                       free(package);
-                               if (appId)
-                                       free(appId);
+                       } else {
+                               int pid = atoi(contextId.c_str());
+                               if (pid <= 0) {
+                                       event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
+                                       break;
+                               }
+                               package = (char*)calloc(sizeof(char), PATH_MAX);
+                               if ((package != NULL) && (aul_app_get_pkgname_bypid(pid, package, PATH_MAX) != AUL_R_OK)) {
+                                       event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
+                                       break;
+                               }
                        }
+                       appId = getIdFromPackage(package);
+                       if (appId == NULL) {
+                               LogError("[ERROR] Can not get package name from current pid");
+                               event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
+                               break;
+                       }
+                       
+                       ApplicationContextPtr appContext(new ApplicationContext());
+                       appContext->setAppId(appId);
+                       appContext->setContextId(contextId);
+
+                       event->setApplicationContext(appContext);
+                       
+                       if (package)
+                               free(package);
+                       if (appId)
+                               free(appId);
                        break;
                }
                default:
@@ -754,8 +806,9 @@ void Application::OnRequestReceived(const EventLaunchServicePtr& event)
 {
        switch(event->getEventType()) {
                case EventLaunchService::APPLICATION_SERVICE_GET_REQUEST: {
-                       // TODO: get bundle_str from wrt engine to get real request from caller. below bundle str is test code. 
-                       std::string bundle_str = "NQAAAAEEAAAUAAAAX19BUFBfU1ZDX09QX1RZUEVfXwARAAAAVEhJU19JU19PRVJBVElPTgAsAAAAAQQAABAAAABfX0FQUF9TVkNfVVJJX18ADAAAAFRISVNfSVNfVVJJADMAAAABBAAAFgAAAF9fQVBQX1NWQ19NSU1FX1RZUEVfXwANAAAAVEhJU19JU19NSU1FADUAAAABBAAAFQAAAF9fQVBQX1NWQ19QS0dfTkFNRV9fABAAAABUSElTX0lTX1BBQ0tBR0UAMgAAAAEEAAAQAAAARVhUUkFfREFUQV9LRVkxABIAAABFWFRSQV9EQVRBX1ZBTFVFMQAyAAAAAQQAABAAAABFWFRSQV9EQVRBX0tFWTIAEgAAAEVYVFJBX0RBVEFfVkFMVUUyAHoAAAABBQAAFgAAAEVYVFJBX0RBVEFfQVJSQVlfS0VZMQADAAAAGAAAABgAAAAYAAAARVhUUkFfREFUQV9BUlJBWV9WQUxVRTEARVhUUkFfREFUQV9BUlJBWV9WQUxVRTIARVhUUkFfREFUQV9BUlJBWV9WQUxVRTMA";
+                       std::string bundle_str = event->getEncodedBundle();
+                       //LogError("bundle_str : "<<bundle_str);
+
                        service_h service;
                        char* tmpStr = NULL;
 
@@ -765,7 +818,6 @@ void Application::OnRequestReceived(const EventLaunchServicePtr& event)
                                // TODO: find proper exception.
                                event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
                        }
-
                        ApplicationServicePtr appService(new ApplicationService());
                        appService->setService_h(service);
 
@@ -793,20 +845,9 @@ void Application::OnRequestReceived(const EventLaunchServicePtr& event)
                                        tmpStr = NULL;
                                }                       
                        }
-                       if (service_get_package(service, &tmpStr) == SERVICE_ERROR_NONE) {
-                               LogInfo("package : "<<tmpStr);
-                               if (tmpStr != NULL) {
-                                       char* appId = getIdFromPackage(tmpStr);
-                                       LogInfo("appId : "<<appId);                                     
-                                       if (appId != NULL) {
-                                               appService->setAppId(appId);
-                                               free(appId);
-                                       }
-                                       free(tmpStr);
-                               }                       
-                       }
 
                        event->setService(appService);
+
                        if (service_foreach_extra_data(service, serviceExtraDataCallback, event.Get()) != SERVICE_ERROR_NONE) {
                                LogError("service_foreach_extra_data fail");
                                event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
@@ -834,7 +875,7 @@ void Application::OnRequestReceived(const EventLaunchServicePtr& event)
                        }
 
                        if (appservice->getUri().compare("") != 0) {
-                               LogDebug("uri is not null");
+                               LogDebug("uri is "<<appservice->getUri());
                                service_set_uri(service, appservice->getUri().c_str() );
                        }
 
@@ -843,9 +884,9 @@ void Application::OnRequestReceived(const EventLaunchServicePtr& event)
                                service_set_mime(service, appservice->getMime().c_str() );
                        }
 
-                       if (appservice->getAppId().compare("") != 0) {
-                               LogDebug("appId is "<<appservice->getAppId());
-                               char* pkg = getPackageById(appservice->getAppId().c_str());
+                       if (event->getAppId().compare("") != 0) {
+                               LogDebug("appId is "<<event->getAppId());
+                               char* pkg = getPackageById(event->getAppId().c_str());
                                if (pkg != NULL) {
                                        service_set_package(service, pkg);
                                        free(pkg);