Implement SendDataAsync function
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 20 May 2020 06:57:55 +0000 (15:57 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 25 May 2020 04:28:15 +0000 (13:28 +0900)
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/theme/dbus/request_broker.cc
src/theme/dbus/request_broker.h

index 5be4040..a68bfe4 100644 (file)
@@ -36,9 +36,9 @@ void RequestBroker::OnReceiveDbusMethod(GDBusConnection *connection,
                                         GDBusMethodInvocation *invocation,
                                         gpointer user_data) {
   RequestBroker* broker = static_cast<RequestBroker*>(user_data);
-  GVariant *reply_body = NULL;
-  char *serialized = NULL;
+  GVariant *reply_body = nullptr;
   int command;
+  char *serialized = nullptr;
   g_variant_get(parameters, "(i&s)", &command, &serialized);
 
   Command cmd = static_cast<Command>(command);
@@ -58,9 +58,9 @@ void RequestBroker::OnReceiveDbusMethod(GDBusConnection *connection,
 
 bool RequestBroker::Init() {
   GError* error = nullptr;
-  connection_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
-  if (connection_ == NULL) {
-    if (error != NULL) {
+  connection_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
+  if (connection_ == nullptr) {
+    if (error != nullptr) {
       LOG(ERROR) << "Failed to get dbus [" << error->message << "]";
       g_error_free(error);
     }
@@ -102,12 +102,12 @@ bool RequestBroker::Listen() {
     "        </method>"
     "  </interface>"
     "  </node>";
-  GError *error = NULL;
+  GError *error = nullptr;
   GDBusNodeInfo *introspection_data =
       g_dbus_node_info_new_for_xml(introspection_xml, &error);
   if (!introspection_data) {
     LOG(ERROR) << "g_dbus_node_info_new_for_xml is failed";
-    if (error != NULL) {
+    if (error != nullptr) {
       LOG(ERROR) << "g_dbus_node_info_new_for_xml err ["
                  << error->message << "]";
       g_error_free(error);
@@ -118,16 +118,18 @@ bool RequestBroker::Listen() {
   registration_id_ = g_dbus_connection_register_object(
       connection_,
       THEME_MANAGER_OBJECT_PATH, introspection_data->interfaces[0],
-      &interface_vtable, this, NULL, NULL);
+      &interface_vtable, this, nullptr, nullptr);
   g_dbus_node_info_unref(introspection_data);
   if (registration_id_ == 0) {
     LOGE("register object fail");
     return false;
   }
 
-  int owner_id = g_bus_own_name_on_connection(connection_, THEME_MANAGER_DBUS_NAME,
-                                          G_BUS_NAME_OWNER_FLAGS_NONE,
-                                          NULL, NULL, NULL, NULL);
+  int owner_id = g_bus_own_name_on_connection(connection_,
+                                              THEME_MANAGER_DBUS_NAME,
+                                              G_BUS_NAME_OWNER_FLAGS_NONE,
+                                              nullptr, nullptr,
+                                              nullptr, nullptr);
   if (!owner_id) {
     g_object_unref(connection_);
     LOG(ERROR) << "g_bus_own_name_on_connection error";
@@ -145,7 +147,7 @@ void RequestBroker::OnReceiveDbusSignal(GDBusConnection* connection,
                                         GVariant* parameters,
                                         void* user_data) {
   RequestBroker* broker = static_cast<RequestBroker*>(user_data);
-  char *serialized = NULL;
+  char *serialized = nullptr;
   int command;
   g_variant_get(parameters, "(i&s)", &command, &serialized);
 
@@ -160,9 +162,9 @@ void RequestBroker::OnReceiveDbusSignal(GDBusConnection* connection,
 }
 
 bool RequestBroker::Subscribe() {
-  subscribe_id_ = g_dbus_connection_signal_subscribe(connection_, NULL,
-      THEME_MANAGER_INTERFACE_NAME, NULL, THEME_MANAGER_OBJECT_PATH, NULL,
-      G_DBUS_SIGNAL_FLAGS_NONE, OnReceiveDbusSignal, this, NULL);
+  subscribe_id_ = g_dbus_connection_signal_subscribe(connection_, nullptr,
+      THEME_MANAGER_INTERFACE_NAME, nullptr, THEME_MANAGER_OBJECT_PATH, nullptr,
+      G_DBUS_SIGNAL_FLAGS_NONE, OnReceiveDbusSignal, this, nullptr);
 
   return subscribe_id_ > 0;
 }
@@ -186,7 +188,8 @@ tizen_base::Bundle RequestBroker::SendData(Command cmd,
   );
 
   GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(
-      connection_, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
+      connection_, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+      -1, nullptr, nullptr, &err);
   GVariant *reply_body = g_dbus_message_get_body(reply);
   char *raw;
   g_variant_get(reply_body, "(s)", &raw);
@@ -195,8 +198,54 @@ tizen_base::Bundle RequestBroker::SendData(Command cmd,
   return result;
 }
 
+void RequestBroker::OnResultReceivedCb(GObject *source_object,
+                                       GAsyncResult *res,
+                                       gpointer user_data) {
+  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) {
+    LOG(ERROR) << "No reply . err[" << (err ? err->message : "Unknown") << "]";
+               return;
+       }
+
+  int command;
+  char *serialized = nullptr;
+
+  GVariant* reply_body = g_dbus_message_get_body(reply);
+  g_variant_get(reply_body, "(i&s)", &command, &serialized);
+  RequestBroker* broker = static_cast<RequestBroker*>(user_data);
+  Command cmd = static_cast<Command>(command);
+  tizen_base::Bundle b(serialized);
+  g_object_unref(reply);
+
+  auto it = broker->filters_.find(cmd);
+  if (it == broker->filters_.end())
+    return;
+
+  it->second.GetHandler()->OnRequest(cmd, b);
+}
+
 void RequestBroker::SendDataAsync(Command cmd, tizen_base::Bundle& data) {
+  GDBusMessage* msg = g_dbus_message_new_method_call(
+      THEME_MANAGER_DBUS_NAME, THEME_MANAGER_OBJECT_PATH,
+      THEME_MANAGER_INTERFACE_NAME, "SendData");
+  if (!msg) {
+    LOG(ERROR) << "Can't allocate new method call";
+    return;
+  }
+
+  g_dbus_message_set_body(msg,
+      g_variant_new("(is)",
+        static_cast<int>(cmd),
+        reinterpret_cast<char*>(data.ToRaw().first.get())
+      )
+  );
 
+  g_dbus_connection_send_message_with_reply(connection_, msg,
+      G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, nullptr,
+      nullptr, OnResultReceived, this);
 }
 
 }  // namespace dbus
index ecf7744..48178aa 100644 (file)
@@ -55,6 +55,11 @@ class RequestBroker {
                                   const gchar* signal_name,
                                   GVariant* parameters,
                                   void* user_data);
+
+  static void OnResultReceivedCb(GObject *source_object,
+                                 GAsyncResult *res,
+                                 gpointer user_data);
+
   RequestBroker() = default;
   ~RequestBroker() = default;
   bool Init();