[Application] Killing and launching moved to main thread 11/162811/3
authorRafal Walczyna <r.walczyna@partner.samsung.com>
Tue, 5 Dec 2017 12:39:17 +0000 (13:39 +0100)
committerRafal Walczyna <r.walczyna@partner.samsung.com>
Tue, 5 Dec 2017 12:48:30 +0000 (13:48 +0100)
Because of the fact that appfw APIs are not thread safe
some functions need to be called from main thread

[Verification] 100% passrate on TM1:
tct-appcontrol-tizen-tests
tct-application-tizen-tests

Change-Id: I092cc84e2ac244cfd6439566c3c65a5e2652813c
Signed-off-by: Rafal Walczyna <r.walczyna@partner.samsung.com>
src/application/application_manager.cc

index 6557a50..8744c0c 100644 (file)
@@ -169,42 +169,7 @@ class TerminateHandler {
         response);
   }
 
-  void LaunchCheckTerminate() {
-    ScopeLogger();
-    timeout_id_ = g_timeout_add(3000, CheckTerminate, this);
-    LoggerD("END");
-  }
-
  private:
-  static gboolean CheckTerminate(gpointer user_data) {
-    ScopeLogger();
-    TerminateHandler* that = static_cast<TerminateHandler*>(user_data);
-    LoggerD("PID: %d", that->pid_);
-
-    // we're canceling the callback by returning false, no need for Invoke() to do that again
-    that->timeout_id_ = 0;
-
-    char* app_id = nullptr;
-    std::shared_ptr<picojson::value> response{new picojson::value(picojson::object())};
-
-    LoggerD("checking if application is still alive");
-    int ret = app_manager_get_app_id(that->pid_, &app_id);
-    if (APP_MANAGER_ERROR_NONE == ret) {
-      free(app_id);
-      // context is still alive, report error
-      LogAndReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to kill application."),
-                        &response->get<picojson::object>(), ("application is alive - failure"));
-    } else {
-      LoggerD("application is dead - success: %d (%s)", ret, get_error_message(ret));
-      ReportSuccess(response->get<picojson::object>());
-    }
-
-    that->Invoke(response);
-    delete that;
-
-    return false;
-  }
-
   int callback_handle_;
   pid_t pid_;
   guint timeout_id_;
@@ -264,7 +229,7 @@ void ApplicationManager::Kill(const picojson::value& args) {
   }
 
   auto kill = [this, callback_id, context_id]() -> void {
-    ScopeLogger("Kill async");
+    ScopeLogger("Kill");
 
     std::shared_ptr<picojson::value> response =
         std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
@@ -292,7 +257,7 @@ void ApplicationManager::Kill(const picojson::value& args) {
       CHECK_RESULT(result, response, handler)
     }
 
-    LoggerD("Kill async, pid: %d", pid);
+    LoggerD("Kill, pid: %d", pid);
 
     char* app_id = nullptr;
     int ret = app_manager_get_app_id(pid, &app_id);
@@ -306,7 +271,7 @@ void ApplicationManager::Kill(const picojson::value& args) {
       CHECK_RESULT(result, response, handler)
     }
 
-    LoggerD("Kill async, app ID: %s", app_id);
+    LoggerD("Kill, app ID: %s", app_id);
 
     // acquire application context
     app_context_h app_context = nullptr;
@@ -352,7 +317,7 @@ void ApplicationManager::Kill(const picojson::value& args) {
       }
     };
 
-    LoggerD("Kill async, setting callback");
+    LoggerD("Kill, setting callback");
     handler->set_pid(pid);
     ret = app_manager_set_app_context_event_cb(terminate_callback, handler);
 
@@ -363,12 +328,7 @@ void ApplicationManager::Kill(const picojson::value& args) {
       CHECK_RESULT(result, response, handler)
     }
 
-    // due to platform issue, sometimes termination event is not reported to callback
-    // registered with app_manager_set_app_context_event_cb()
-    // this is a workaround, it should be removed when issue is solved
-    // handler->LaunchCheckTerminate();
-
-    LoggerD("Kill async, KILL!!!!!!!!!");
+    LoggerD("Kill, calling native function");
 
     // terminate application
     ret = app_manager_terminate_app(app_context);
@@ -380,10 +340,10 @@ void ApplicationManager::Kill(const picojson::value& args) {
       CHECK_RESULT(result, response, handler)
     }
 
-    LoggerD("Kill async, end, waiting for notification");
+    LoggerD("Kill, end, waiting for notification");
   };
 
-  TaskQueue::GetInstance().Queue(kill);
+  kill();
 }
 
 void ApplicationManager::Launch(const picojson::value& args) {
@@ -409,7 +369,7 @@ void ApplicationManager::Launch(const picojson::value& args) {
   const std::string& id = app_id.get<std::string>();
 
   auto launch = [id](const std::shared_ptr<picojson::value>& response) -> void {
-    ScopeLogger("Entered into asynchronous function, launch");
+    ScopeLogger("launch");
     PlatformResult result = PlatformResult(ErrorCode::NO_ERROR);
     const char* app_id = id.c_str();
     const int retry_count = 3;
@@ -458,11 +418,8 @@ void ApplicationManager::Launch(const picojson::value& args) {
     }
   };
 
-  auto launch_response = [this](const std::shared_ptr<picojson::value>& response) -> void {
-    Instance::PostMessage(&this->instance_, response->serialize().c_str());
-  };
-
-  TaskQueue::GetInstance().Queue<picojson::value>(launch, launch_response, response);
+  launch(response);
+  Instance::PostMessage(&this->instance_, response->serialize().c_str());
 }
 
 void ApplicationManager::LaunchAppControl(const picojson::value& args) {
@@ -651,13 +608,8 @@ void ApplicationManager::LaunchAppControl(const picojson::value& args) {
     ReportSuccess(response->get<picojson::object>());
   };
 
-  auto launch_response = [this](const std::shared_ptr<picojson::value>& response) -> void {
-    ScopeLogger("launch_response");
-
-    Instance::PostMessage(&this->instance_, response->serialize().c_str());
-  };
-
-  TaskQueue::GetInstance().Queue<picojson::value>(launch, launch_response, response);
+  launch(response);
+  Instance::PostMessage(&this->instance_, response->serialize().c_str());
 }
 
 // internal impl of app_control_foreach_app_matched() for handling APP_CONTROL_ERROR_APP_NOT_FOUND