You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_DBUS_H_
  28. #define TALK_BASE_DBUS_H_
  29. #ifdef HAVE_DBUS_GLIB
  30. #include <dbus/dbus.h>
  31. #include <string>
  32. #include <vector>
  33. #include "talk/base/libdbusglibsymboltable.h"
  34. #include "talk/base/messagehandler.h"
  35. #include "talk/base/thread.h"
  36. namespace talk_base {
  37. #define DBUS_TYPE "type"
  38. #define DBUS_SIGNAL "signal"
  39. #define DBUS_PATH "path"
  40. #define DBUS_INTERFACE "interface"
  41. #define DBUS_MEMBER "member"
  42. #ifdef CHROMEOS
  43. #define CROS_PM_PATH "/"
  44. #define CROS_PM_INTERFACE "org.chromium.PowerManager"
  45. #define CROS_SIG_POWERCHANGED "PowerStateChanged"
  46. #define CROS_VALUE_SLEEP "mem"
  47. #define CROS_VALUE_RESUME "on"
  48. #else
  49. #define UP_PATH "/org/freedesktop/UPower"
  50. #define UP_INTERFACE "org.freedesktop.UPower"
  51. #define UP_SIG_SLEEPING "Sleeping"
  52. #define UP_SIG_RESUMING "Resuming"
  53. #endif // CHROMEOS
  54. // Wraps a DBus messages.
  55. class DBusSigMessageData : public TypedMessageData<DBusMessage *> {
  56. public:
  57. explicit DBusSigMessageData(DBusMessage *message);
  58. ~DBusSigMessageData();
  59. };
  60. // DBusSigFilter is an abstract class that defines the interface of DBus
  61. // signal handling.
  62. // The subclasses implement ProcessSignal() for various purposes.
  63. // When a DBus signal comes, a DSM_SIGNAL message is posted to the caller thread
  64. // which will then invokes ProcessSignal().
  65. class DBusSigFilter : protected MessageHandler {
  66. public:
  67. enum DBusSigMessage { DSM_SIGNAL };
  68. // This filter string should ususally come from BuildFilterString()
  69. explicit DBusSigFilter(const std::string &filter)
  70. : caller_thread_(Thread::Current()), filter_(filter) {
  71. }
  72. // Builds a DBus monitor filter string from given DBus path, interface, and
  73. // member.
  74. // See http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
  75. static std::string BuildFilterString(const std::string &path,
  76. const std::string &interface,
  77. const std::string &member);
  78. // Handles callback on DBus messages by DBus system.
  79. static DBusHandlerResult DBusCallback(DBusConnection *dbus_conn,
  80. DBusMessage *message,
  81. void *instance);
  82. // Handles callback on DBus messages to each DBusSigFilter instance.
  83. DBusHandlerResult Callback(DBusMessage *message);
  84. // From MessageHandler.
  85. virtual void OnMessage(Message *message);
  86. // Returns the DBus monitor filter string.
  87. const std::string &filter() const { return filter_; }
  88. private:
  89. // On caller thread.
  90. virtual void ProcessSignal(DBusMessage *message) = 0;
  91. Thread *caller_thread_;
  92. const std::string filter_;
  93. };
  94. // DBusMonitor is a class for DBus signal monitoring.
  95. //
  96. // The caller-thread calls AddFilter() first to add the signals that it wants to
  97. // monitor and then calls StartMonitoring() to start the monitoring.
  98. // This will create a worker-thread which listens on DBus connection and sends
  99. // DBus signals back through the callback.
  100. // The worker-thread will be running forever until either StopMonitoring() is
  101. // called from the caller-thread or the worker-thread hit some error.
  102. //
  103. // Programming model:
  104. // 1. Caller-thread: Creates an object of DBusMonitor.
  105. // 2. Caller-thread: Calls DBusMonitor::AddFilter() one or several times.
  106. // 3. Caller-thread: StartMonitoring().
  107. // ...
  108. // 4. Worker-thread: DBus signal recieved. Post a message to caller-thread.
  109. // 5. Caller-thread: DBusFilterBase::ProcessSignal() is invoked.
  110. // ...
  111. // 6. Caller-thread: StopMonitoring().
  112. //
  113. // Assumption:
  114. // AddFilter(), StartMonitoring(), and StopMonitoring() methods are called by
  115. // a single thread. Hence, there is no need to make them thread safe.
  116. class DBusMonitor {
  117. public:
  118. // Status of DBus monitoring.
  119. enum DBusMonitorStatus {
  120. DMS_NOT_INITIALIZED, // Not initialized.
  121. DMS_INITIALIZING, // Initializing the monitoring thread.
  122. DMS_RUNNING, // Monitoring.
  123. DMS_STOPPED, // Not monitoring. Stopped normally.
  124. DMS_FAILED, // Not monitoring. Failed.
  125. };
  126. // Returns the DBus-Glib symbol table.
  127. // We should only use this function to access DBus-Glib symbols.
  128. static LibDBusGlibSymbolTable *GetDBusGlibSymbolTable();
  129. // Creates an instance of DBusMonitor.
  130. static DBusMonitor *Create(DBusBusType type);
  131. ~DBusMonitor();
  132. // Adds a filter to DBusMonitor.
  133. bool AddFilter(DBusSigFilter *filter);
  134. // Starts DBus message monitoring.
  135. bool StartMonitoring();
  136. // Stops DBus message monitoring.
  137. bool StopMonitoring();
  138. // Gets the status of DBus monitoring.
  139. DBusMonitorStatus GetStatus();
  140. private:
  141. // Forward declaration. Defined in the .cc file.
  142. class DBusMonitoringThread;
  143. explicit DBusMonitor(DBusBusType type);
  144. // Updates status_ when monitoring status has changed.
  145. void OnMonitoringStatusChanged(DBusMonitorStatus status);
  146. DBusBusType type_;
  147. DBusMonitorStatus status_;
  148. DBusMonitoringThread *monitoring_thread_;
  149. std::vector<DBusSigFilter *> filter_list_;
  150. };
  151. } // namespace talk_base
  152. #endif // HAVE_DBUS_GLIB
  153. #endif // TALK_BASE_DBUS_H_