Fix Send Method of FDBroker class 35/207735/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 11 Jun 2019 22:59:23 +0000 (07:59 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 12 Jun 2019 00:02:41 +0000 (09:02 +0900)
- Uses g_dbus_connection_send_message_with_reply() instead of
g_dbus_connection_send_message_with_reply_sync()

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

index ad14b96..46eb58a 100644 (file)
@@ -230,14 +230,10 @@ int FdBroker::Send(const std::string& target_appid,
                    int (*fds)[2]) {
   std::string interface_name = GetInterfaceName(target_appid, port_name);
   GDBusMessage *msg;
-  GDBusMessage *reply;
-  GError *err = nullptr;
-  GVariant *reply_body;
   SocketPair main_sock_pair(mock_);
   SocketPair delegate_sock_pair(mock_);
   FdList fd_list;
   char sender_appid[255];
-  int ret;
 
   if (!mock_ && aul_app_get_appid_bypid(getpid(),
        sender_appid, sizeof(sender_appid)) < 0) {
@@ -277,37 +273,14 @@ int FdBroker::Send(const std::string& target_appid,
   }
 
   g_dbus_message_set_unix_fd_list(msg, fd_list.GetRaw());
-  reply = g_dbus_connection_send_message_with_reply_sync(
+  g_dbus_connection_send_message_with_reply(
       DBusConnectionManager::GetInst().GetConnection(),
-      msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 5000, nullptr, nullptr, &err);
-  if (reply == nullptr) {
-    LOGE("No reply. error = %s", err->message);
-    g_error_free(err);
-    g_object_unref(msg);
-    return -1;
-  }
-
-  reply_body = g_dbus_message_get_body(reply);
-  if (reply_body == nullptr) {
-    LOGE("g_dbus_message_get_body() is failed");
-    g_object_unref(msg);
-    return -1;
-  }
-
-  g_variant_get(reply_body, "(i)", &ret);
-
-  if (ret != 0) {
-    LOGE("Access Denied[sender_appid : %s]", sender_appid);
-    g_object_unref(msg);
-    g_object_unref(reply);
-    return -EILLEGALACCESS;
-  }
-
-  LOGD("[Reply : %d]", ret);
+      msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+      5000, NULL, NULL, OnResultReceived, this);
+  g_object_unref(msg);
 
   (*fds)[0] = main_sock_pair.Detach(SocketPair::SENDER);
   (*fds)[1] = delegate_sock_pair.Detach(SocketPair::SENDER);
-  g_object_unref(msg);
 
   return (*fds)[0];
 }
@@ -616,5 +589,42 @@ int FdBroker::Watch(IEventWatcher* ev, const std::string& target_appid,
   return 0;
 }
 
+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;
+  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");
+    watcher->OnPortRejected(broker->watch_appid_);
+    g_error_free(err);
+    return;
+  }
+
+  GVariant* reply_body = g_dbus_message_get_body(reply);
+  if (reply_body == nullptr) {
+    LOGE("g_dbus_message_get_body() is failed");
+    watcher->OnPortRejected(broker->watch_appid_);
+    g_object_unref(reply);
+    return;
+  }
+
+  int ret;
+  g_variant_get(reply_body, "(i)", &ret);
+  g_object_unref(reply);
+  if (ret != 0) {
+    LOGE("Access Denied[sender_appid : %s]", broker->watch_appid_.c_str());
+    watcher->OnPortRejected(broker->watch_appid_);
+    return;
+  }
+
+  watcher->OnPortConnected(broker->watch_appid_, broker->watch_port_name_);
+  LOGD("[Reply : %d]", ret);
+}
+
 }  // namespace internal
 }  // namespace rpc_port
index 510c992..86944d9 100644 (file)
@@ -46,6 +46,8 @@ class FdBroker {
     virtual void OnPortVanished(const std::string& appid,
                                 const std::string& port_name) = 0;
     virtual void OnPortRejected(const std::string& appid) = 0;
+    virtual void OnPortConnected(const std::string& appid,
+                                 const std::string& port_name) = 0;
   };
 
   explicit FdBroker(bool mock = false) : mock_(mock) {}
@@ -159,6 +161,9 @@ class FdBroker {
   static void OnNameVanished(GDBusConnection *connection,
                              const gchar *name,
                              gpointer user_data);
+  static void OnResultReceived(GObject* source_object,
+                               GAsyncResult* res,
+                               gpointer user_data);
 
  private:
   IEventListener* listener_ = nullptr;
index 262634b..86da798 100644 (file)
@@ -115,7 +115,6 @@ void Proxy::OnPortAppeared(const std::string& appid,
   LOGW("[__OnPortAppeared__] fds[0]: %d, fds[1]: %d", fds[0], fds[1]);
   main_port_.reset(new ProxyPort(this, fds[0], appid, false));
   delegate_port_.reset(new ProxyPort(this, fds[1], appid));
-  listener_->OnConnected(appid, main_port_.get());
 }
 
 void Proxy::OnPortVanished(const std::string& appid,
@@ -124,6 +123,14 @@ void Proxy::OnPortVanished(const std::string& appid,
       appid.c_str(), port_name.c_str());
 }
 
+void Proxy::OnPortConnected(const std::string& appid,
+                            const std::string& port_name) {
+  LOGW("[__OnPortConnected__] endpoint(%s), port_name(%s)",
+      appid.c_str(), port_name.c_str());
+
+  listener_->OnConnected(appid, main_port_.get());
+}
+
 int Proxy::Connect(std::string appid, std::string port_name,
                    IEventListener* ev) {
   if (ev == nullptr)
index 8584ca2..76fc462 100644 (file)
@@ -85,6 +85,8 @@ class Proxy : public FdBroker::IEventWatcher {
   void OnPortVanished(const std::string& appid,
                       const std::string& port_name) override;
   void OnPortRejected(const std::string& appid) override;
+  void OnPortConnected(const std::string& appid,
+                       const std::string& port_name) override;
 
  private:
   std::string port_name_;