Bug 1282433 - Use dbus PropertiesChanged signal for notification of battery status changes, r=karlt
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 02 Nov 2016 10:37:11 +0100
changeset 347320 03ccd2a9b30cddc081644cdaa9204a64a575115e
parent 347319 90f8b4647d6e6f6913525e529c37b5617b8eba24
child 347321 214a61dbd0397b702fc1d0243ae6a4cca7635a97
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt
bugs1282433
milestone52.0a1
Bug 1282433 - Use dbus PropertiesChanged signal for notification of battery status changes, r=karlt From UPower 0.99, DeviceChanged has been removed from the UPower interface and PropertiesChanged support added.
hal/linux/UPowerClient.cpp
--- a/hal/linux/UPowerClient.cpp
+++ b/hal/linux/UPowerClient.cpp
@@ -87,16 +87,25 @@ private:
 
   /**
    * Callback used by 'DeviceChanged' signal.
    */
   static void DeviceChanged(DBusGProxy* aProxy, const gchar* aObjectPath,
                             UPowerClient* aListener);
 
   /**
+   * Callback used by 'PropertiesChanged' signal.
+   * This method is called when the the battery level changes.
+   * (Only with upower >= 0.99)
+   */
+  static void PropertiesChanged(DBusGProxy* aProxy, const gchar*,
+                                GHashTable*, char**,
+                                UPowerClient* aListener);
+
+  /**
    * Callback called when mDBusConnection gets a signal.
    */
   static DBusHandlerResult ConnectionSignalFilter(DBusConnection* aConnection,
                                                   DBusMessage* aMessage,
                                                   void* aData);
 
   // The DBus connection object.
   DBusGConnection* mDBusConnection;
@@ -238,16 +247,19 @@ UPowerClient::StopListening()
 
   dbus_g_proxy_disconnect_signal(mUPowerProxy, "DeviceChanged",
                                  G_CALLBACK (DeviceChanged), this);
 
   g_free(mTrackedDevice);
   mTrackedDevice = nullptr;
 
   if (mTrackedDeviceProxy) {
+    dbus_g_proxy_disconnect_signal(mTrackedDeviceProxy, "PropertiesChanged",
+                                   G_CALLBACK (PropertiesChanged), this);
+
     g_object_unref(mTrackedDeviceProxy);
     mTrackedDeviceProxy = nullptr;
   }
 
   g_object_unref(mUPowerProxy);
   mUPowerProxy = nullptr;
 
   dbus_g_connection_unref(mDBusConnection);
@@ -268,16 +280,19 @@ UPowerClient::UpdateTrackedDeviceSync()
   GError* error = nullptr;
 
   // Reset the current tracked device:
   g_free(mTrackedDevice);
   mTrackedDevice = nullptr;
 
   // Reset the current tracked device proxy:
   if (mTrackedDeviceProxy) {
+    dbus_g_proxy_disconnect_signal(mTrackedDeviceProxy, "PropertiesChanged",
+                                   G_CALLBACK (PropertiesChanged), this);
+
     g_object_unref(mTrackedDeviceProxy);
     mTrackedDeviceProxy = nullptr;
   }
 
   // If that fails, that likely means upower isn't installed.
   if (!dbus_g_proxy_call(mUPowerProxy, "EnumerateDevices", &error, G_TYPE_INVALID,
                          typeGPtrArray, &devices, G_TYPE_INVALID)) {
     HAL_LOG("Error: %s\n", error->message);
@@ -304,37 +319,55 @@ UPowerClient::UpdateTrackedDeviceSync()
       mTrackedDeviceProxy = proxy;
       break;
     }
 
     g_object_unref(proxy);
     g_free(devicePath);
   }
 
+  if (mTrackedDeviceProxy) {
+    dbus_g_proxy_add_signal(mTrackedDeviceProxy, "PropertiesChanged",
+                            G_TYPE_STRING,
+                            dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
+                                                G_TYPE_VALUE),
+                            G_TYPE_STRV, G_TYPE_INVALID);
+    dbus_g_proxy_connect_signal(mTrackedDeviceProxy, "PropertiesChanged",
+                                G_CALLBACK (PropertiesChanged), this, nullptr);
+  }
+
   g_ptr_array_free(devices, true);
 }
 
 /* static */ void
-UPowerClient::DeviceChanged(DBusGProxy* aProxy, const gchar* aObjectPath, UPowerClient* aListener)
+UPowerClient::DeviceChanged(DBusGProxy* aProxy, const gchar* aObjectPath,
+                            UPowerClient* aListener)
 {
   if (!aListener->mTrackedDevice) {
     return;
   }
 
 #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 16
   if (g_strcmp0(aObjectPath, aListener->mTrackedDevice)) {
 #else
   if (g_ascii_strcasecmp(aObjectPath, aListener->mTrackedDevice)) {
 #endif
     return;
   }
 
   aListener->GetDevicePropertiesAsync(aListener->mTrackedDeviceProxy);
 }
 
+/* static */ void
+UPowerClient::PropertiesChanged(DBusGProxy* aProxy, const gchar*, GHashTable*,
+                                char**, UPowerClient* aListener)
+{
+  aListener->GetDevicePropertiesAsync(aListener->mTrackedDeviceProxy);
+}
+
 /* static */ DBusHandlerResult
 UPowerClient::ConnectionSignalFilter(DBusConnection* aConnection,
                                      DBusMessage* aMessage, void* aData)
 {
   if (dbus_message_is_signal(aMessage, DBUS_INTERFACE_LOCAL, "Disconnected")) {
     static_cast<UPowerClient*>(aData)->StopListening();
     // We do not return DBUS_HANDLER_RESULT_HANDLED here because the connection
     // might be shared and some other filters might want to do something.