Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. 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 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DBUS_INTERFACE_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DBUS_INTERFACE_H__ |
| 7 | |
| 8 | // This class interfaces with DBus. The interface allows it to be mocked. |
| 9 | |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 10 | #include <base/logging.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 11 | #include <dbus/dbus-glib.h> |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 12 | #include <dbus/dbus-glib-lowlevel.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 13 | |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 14 | #ifndef DBUS_TYPE_G_OBJECT_PATH_ARRAY |
| 15 | #define DBUS_TYPE_G_OBJECT_PATH_ARRAY \ |
| 16 | (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH)) |
| 17 | #endif |
| 18 | |
| 19 | #ifndef DBUS_TYPE_G_STRING_ARRAY |
| 20 | #define DBUS_TYPE_G_STRING_ARRAY \ |
| 21 | (dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING)) |
| 22 | #endif |
| 23 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 24 | namespace chromeos_update_engine { |
| 25 | |
| 26 | class DbusGlibInterface { |
| 27 | public: |
| 28 | // wraps dbus_g_proxy_new_for_name_owner |
| 29 | virtual DBusGProxy* ProxyNewForNameOwner(DBusGConnection* connection, |
| 30 | const char* name, |
| 31 | const char* path, |
| 32 | const char* interface, |
| 33 | GError** error) = 0; |
| 34 | |
| 35 | // wraps g_object_unref |
| 36 | virtual void ProxyUnref(DBusGProxy* proxy) = 0; |
| 37 | |
| 38 | // wraps dbus_g_bus_get |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 39 | virtual DBusGConnection* BusGet(DBusBusType type, GError** error) = 0; |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 40 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 41 | // wraps dbus_g_proxy_call |
| 42 | virtual gboolean ProxyCall(DBusGProxy* proxy, |
| 43 | const char* method, |
| 44 | GError** error, |
| 45 | GType first_arg_type, |
| 46 | GType var_arg1, |
| 47 | GHashTable** var_arg2, |
| 48 | GType var_arg3) = 0; |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 49 | |
| 50 | virtual gboolean ProxyCall(DBusGProxy* proxy, |
| 51 | const char* method, |
| 52 | GError** error, |
| 53 | GType var_arg1, const char* var_arg2, |
| 54 | GType var_arg3, |
| 55 | GType var_arg4, gchar** var_arg5, |
| 56 | GType var_arg6, GArray** var_arg7, |
| 57 | GType var_arg8) = 0; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 58 | |
| 59 | virtual gboolean ProxyCall(DBusGProxy* proxy, |
| 60 | const char* method, |
| 61 | GError** error, |
| 62 | GType var_arg1, const char* var_arg2, |
| 63 | GType var_arg3, const char* var_arg4, |
| 64 | GType var_arg5, const char* var_arg6, |
| 65 | GType var_arg7, GType var_arg8) = 0; |
| 66 | |
| 67 | virtual DBusConnection* ConnectionGetConnection(DBusGConnection* gbus) = 0; |
| 68 | |
| 69 | virtual void DbusBusAddMatch(DBusConnection* connection, |
| 70 | const char* rule, |
| 71 | DBusError* error) = 0; |
| 72 | |
| 73 | virtual dbus_bool_t DbusConnectionAddFilter( |
| 74 | DBusConnection* connection, |
| 75 | DBusHandleMessageFunction function, |
| 76 | void* user_data, |
| 77 | DBusFreeFunction free_data_function) = 0; |
| 78 | virtual void DbusConnectionRemoveFilter(DBusConnection* connection, |
| 79 | DBusHandleMessageFunction function, |
| 80 | void* user_data) = 0; |
| 81 | virtual dbus_bool_t DbusMessageIsSignal(DBusMessage* message, |
| 82 | const char* interface, |
| 83 | const char* signal_name) = 0; |
| 84 | virtual dbus_bool_t DbusMessageGetArgs(DBusMessage* message, |
| 85 | DBusError* error, |
| 86 | GType var_arg1, char** var_arg2, |
| 87 | GType var_arg3, char** var_arg4, |
| 88 | GType var_arg5, char** var_arg6, |
| 89 | GType var_arg7) = 0; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class ConcreteDbusGlib : public DbusGlibInterface { |
| 93 | virtual DBusGProxy* ProxyNewForNameOwner(DBusGConnection* connection, |
| 94 | const char* name, |
| 95 | const char* path, |
| 96 | const char* interface, |
| 97 | GError** error) { |
| 98 | return dbus_g_proxy_new_for_name_owner(connection, |
| 99 | name, |
| 100 | path, |
| 101 | interface, |
| 102 | error); |
| 103 | } |
| 104 | |
| 105 | virtual void ProxyUnref(DBusGProxy* proxy) { |
| 106 | g_object_unref(proxy); |
| 107 | } |
| 108 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 109 | virtual DBusGConnection* BusGet(DBusBusType type, GError** error) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 110 | return dbus_g_bus_get(type, error); |
| 111 | } |
| 112 | |
| 113 | virtual gboolean ProxyCall(DBusGProxy* proxy, |
| 114 | const char* method, |
| 115 | GError** error, |
| 116 | GType first_arg_type, |
| 117 | GType var_arg1, |
| 118 | GHashTable** var_arg2, |
| 119 | GType var_arg3) { |
| 120 | return dbus_g_proxy_call( |
| 121 | proxy, method, error, first_arg_type, var_arg1, var_arg2, var_arg3); |
| 122 | } |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 123 | |
| 124 | virtual gboolean ProxyCall(DBusGProxy* proxy, |
| 125 | const char* method, |
| 126 | GError** error, |
| 127 | GType var_arg1, const char* var_arg2, |
| 128 | GType var_arg3, |
| 129 | GType var_arg4, gchar** var_arg5, |
| 130 | GType var_arg6, GArray** var_arg7, |
| 131 | GType var_arg8) { |
| 132 | return dbus_g_proxy_call( |
| 133 | proxy, method, error, var_arg1, var_arg2, var_arg3, |
| 134 | var_arg4, var_arg5, var_arg6, var_arg7, var_arg8); |
| 135 | } |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 136 | |
| 137 | virtual gboolean ProxyCall(DBusGProxy* proxy, |
| 138 | const char* method, |
| 139 | GError** error, |
| 140 | GType var_arg1, const char* var_arg2, |
| 141 | GType var_arg3, const char* var_arg4, |
| 142 | GType var_arg5, const char* var_arg6, |
| 143 | GType var_arg7, GType var_arg8) { |
| 144 | return dbus_g_proxy_call( |
| 145 | proxy, method, error, var_arg1, var_arg2, var_arg3, |
| 146 | var_arg4, var_arg5, var_arg6, var_arg7, var_arg8); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | virtual DBusConnection* ConnectionGetConnection(DBusGConnection* gbus) { |
| 151 | return dbus_g_connection_get_connection(gbus); |
| 152 | } |
| 153 | |
| 154 | virtual void DbusBusAddMatch(DBusConnection* connection, |
| 155 | const char* rule, |
| 156 | DBusError* error) { |
| 157 | dbus_bus_add_match(connection, rule, error); |
| 158 | } |
| 159 | |
| 160 | virtual dbus_bool_t DbusConnectionAddFilter( |
| 161 | DBusConnection* connection, |
| 162 | DBusHandleMessageFunction function, |
| 163 | void* user_data, |
| 164 | DBusFreeFunction free_data_function) { |
| 165 | return dbus_connection_add_filter(connection, |
| 166 | function, |
| 167 | user_data, |
| 168 | free_data_function); |
| 169 | } |
| 170 | virtual void DbusConnectionRemoveFilter(DBusConnection* connection, |
| 171 | DBusHandleMessageFunction function, |
| 172 | void* user_data) { |
| 173 | dbus_connection_remove_filter(connection, function, user_data); |
| 174 | } |
| 175 | dbus_bool_t DbusMessageIsSignal(DBusMessage* message, |
| 176 | const char* interface, |
| 177 | const char* signal_name) { |
| 178 | return dbus_message_is_signal(message, interface, signal_name); |
| 179 | } |
| 180 | virtual dbus_bool_t DbusMessageGetArgs(DBusMessage* message, |
| 181 | DBusError* error, |
| 182 | GType var_arg1, char** var_arg2, |
| 183 | GType var_arg3, char** var_arg4, |
| 184 | GType var_arg5, char** var_arg6, |
| 185 | GType var_arg7) { |
| 186 | return dbus_message_get_args(message, error, |
| 187 | var_arg1, var_arg2, |
| 188 | var_arg3, var_arg4, |
| 189 | var_arg5, var_arg6, |
| 190 | var_arg7); |
| 191 | } |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | } // namespace chromeos_update_engine |
| 195 | |
| 196 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DBUS_INTERFACE_H__ |