d336af2cbe86437fe797a0d66001460687615cc1
[platform/framework/web/crosswalk-tizen.git] / src / common / dbus_client.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "common/dbus_client.h"
6
7 #include "common/logger.h"
8
9 namespace wrt {
10
11 namespace {
12
13 void OnSignalReceived(GDBusConnection* /*connection*/,
14                       const gchar* /*sender_name*/,
15                       const gchar* /*object_path*/,
16                       const gchar* interface_name,
17                       const gchar* signal_name,
18                       GVariant* parameters,
19                       gpointer user_data) {
20   DBusClient* self = reinterpret_cast<DBusClient*>(user_data);
21   auto callback = self->GetSignalCallback(interface_name);
22   if (callback) {
23     callback(signal_name, parameters);
24   }
25 }
26
27 }  // namespace
28
29 DBusClient::DBusClient()
30     : connection_(NULL),
31       signal_subscription_id_(0) {
32 }
33
34 DBusClient::~DBusClient() {
35   if (connection_) {
36     g_dbus_connection_signal_unsubscribe(connection_, signal_subscription_id_);
37     g_dbus_connection_close_sync(connection_, NULL, NULL);
38   }
39 }
40
41 bool DBusClient::ConnectByName(const std::string& name) {
42   std::string address("unix:path=");
43   address.append(g_get_user_runtime_dir());
44   address.append("/.");
45   address.append(name);
46   return Connect(address);
47 }
48
49 bool DBusClient::Connect(const std::string& address) {
50   GError *err = NULL;
51   connection_ = g_dbus_connection_new_for_address_sync(
52       address.c_str(),
53       G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
54       NULL, NULL, &err);
55   if (!connection_) {
56     LOGGER(ERROR) << "Failed to connect to bus address " << address
57                   << " : " << err->message;
58     g_error_free(err);
59     return false;
60   }
61
62   signal_subscription_id_ = g_dbus_connection_signal_subscribe(
63       connection_, NULL, NULL, NULL, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
64       OnSignalReceived, this, NULL);
65
66   return true;
67 }
68
69 GVariant* DBusClient::Call(const std::string& iface,
70                            const std::string& method,
71                            GVariant* parameters,
72                            const GVariantType* reply_type) {
73   if (!connection_) {
74     return NULL;
75   }
76
77   GError *err = NULL;
78   GVariant* reply = NULL;
79
80   if (reply_type) {
81     reply = g_dbus_connection_call_sync(
82         connection_, NULL, "/", iface.c_str(), method.c_str(), parameters,
83         reply_type, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
84     if (!reply) {
85       LOGGER(ERROR) << "Failed to CallSync : " << err->message;
86       g_error_free(err);
87     }
88   } else {
89     g_dbus_connection_call(
90         connection_, NULL, "/", iface.c_str(), method.c_str(), parameters,
91         NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
92   }
93
94   return reply;
95 }
96
97 void DBusClient::SetSignalCallback(const std::string& iface,
98                                    SignalCallback func) {
99   signal_callbacks_[iface] = func;
100 }
101
102 DBusClient::SignalCallback
103 DBusClient::GetSignalCallback(const std::string& iface) {
104   return signal_callbacks_[iface];
105 }
106
107 }  // namespace wrt