Cancel sending dbus message
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 17 Jul 2019 04:36:04 +0000 (13:36 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 17 Jul 2019 22:22:03 +0000 (07:22 +0900)
To prevent calling the callback function after the proxy object is destoyed,
the GCancellable object is used. If Send() method is called twice, the
previous request will be cancelled.

Change-Id: Ibe3d692306864749ee37a0e78cbf7c82de651346
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/fdbroker-internal.cc
src/fdbroker-internal.h

index 0265566..d145655 100644 (file)
@@ -195,6 +195,12 @@ GUnixFDList* FdBroker::FdList::GetRaw() {
 }
 
 FdBroker::~FdBroker() {
+  if (cancellable_) {
+    LOGW("Cancel the send request");
+    g_cancellable_cancel(cancellable_);
+    g_object_unref(cancellable_);
+  }
+
   if (watcher_id_ > 0)
     g_bus_unwatch_name(watcher_id_);
 
@@ -270,11 +276,23 @@ int FdBroker::Send(const std::string& target_appid,
     return -1;
   }
 
+  if (cancellable_) {
+    g_cancellable_cancel(cancellable_);
+    g_object_unref(cancellable_);
+  }
+
+  cancellable_ = g_cancellable_new();
+  if (!cancellable_) {
+    LOGE("Failed to create GCancellable");
+    g_object_unref(msg);
+    return -1;
+  }
+
   g_dbus_message_set_unix_fd_list(msg, fd_list.GetRaw());
   g_dbus_connection_send_message_with_reply(
       DBusConnectionManager::GetInst().GetConnection(),
       msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-      5000, NULL, NULL, OnResultReceived, this);
+      5000, NULL, cancellable_, OnResultReceived, this);
   g_object_unref(msg);
 
   (*fds)[0] = main_sock_pair.Detach(SocketPair::SENDER);
@@ -590,29 +608,23 @@ int FdBroker::Watch(IEventWatcher* ev, const std::string& target_appid,
 void FdBroker::OnResultReceived(GObject* source_object,
                                 GAsyncResult* res,
                                 gpointer user_data) {
-  FdBroker* broker = static_cast<FdBroker*>(user_data);
-  IEventWatcher* watcher = broker->watcher_;
   GDBusConnection* conn = reinterpret_cast<GDBusConnection*>(source_object);
-  GError *err = nullptr;
+  GErrorerr = nullptr;
   GDBusMessage* reply = g_dbus_connection_send_message_with_reply_finish(conn,
       res, &err);
   if (reply == nullptr) {
     LOGE("No reply. err(%s)", err ? err->message : "Unknown");
-    if (watcher) {
-      watcher->OnPortDisconnected(broker->watch_appid_,
-          broker->watch_port_name_);
-    }
     g_error_free(err);
     return;
   }
 
+  FdBroker* broker = static_cast<FdBroker*>(user_data);
+  IEventWatcher* watcher = broker->watcher_;
+
   GVariant* reply_body = g_dbus_message_get_body(reply);
   if (reply_body == nullptr) {
     LOGE("g_dbus_message_get_body() is failed");
-    if (watcher) {
-      watcher->OnPortDisconnected(broker->watch_appid_,
-          broker->watch_port_name_);
-    }
+    watcher->OnPortDisconnected(broker->watch_appid_, broker->watch_port_name_);
     g_object_unref(reply);
     return;
   }
@@ -622,13 +634,11 @@ void FdBroker::OnResultReceived(GObject* source_object,
   g_object_unref(reply);
   if (ret != 0) {
     LOGE("Access Denied[sender_appid : %s]", broker->watch_appid_.c_str());
-    if (watcher)
-      watcher->OnPortRejected(broker->watch_appid_);
+    watcher->OnPortRejected(broker->watch_appid_);
     return;
   }
 
-  if (watcher)
-    watcher->OnPortConnected(broker->watch_appid_, broker->watch_port_name_);
+  watcher->OnPortConnected(broker->watch_appid_, broker->watch_port_name_);
   LOGD("[Reply : %d]", ret);
 }
 
index e9603f4..e7cca55 100644 (file)
@@ -176,6 +176,7 @@ class FdBroker {
   guint watcher_id_ = 0;
   std::string watch_appid_;
   std::string watch_port_name_;
+  GCancellable* cancellable_ = nullptr;
 };
 
 }  // namespace internal