Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium 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 DBUS_BUS_H_ | |
| 6 #define DBUS_BUS_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <dbus/dbus.h> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/tracked_objects.h" | |
| 15 | |
| 16 namespace base { | |
| 17 class Thread; | |
| 18 } | |
| 19 | |
| 20 namespace dbus { | |
| 21 | |
| 22 class ObjectProxy; | |
| 23 | |
| 24 // Bus is used to establish a connection with D-Bus, and create object | |
| 25 // proxies. | |
| 26 // | |
| 27 // | |
| 28 // For asynchronous method and signal calls, the bus object will use a | |
| 29 // separate thread if io_thread option is specified. This is useful if you | |
| 30 // want to issue D-Bus calls from UI thread. | |
| 31 class Bus { | |
| 32 public: | |
| 33 // Specifies the bus type. SESSION is used to communicate with per-user | |
| 34 // services like GNOME applications. SYSTEM is used to communicate with | |
| 35 // system-wide services like NetworkManager. | |
| 36 enum BusType { | |
| 37 SESSION = DBUS_BUS_SESSION, | |
| 38 SYSTEM = DBUS_BUS_SYSTEM, | |
| 39 }; | |
| 40 | |
| 41 // Specifies the connection type. PRIVATE should usually be used unless | |
| 42 // you are sure that SHARED is safe for you. PRIVATE gives you a private | |
| 43 // connection, that won't be shared with other Bus objects. SHARED gives | |
| 44 // you a connection shared among other Bus objects, which is unsafe if | |
| 45 // the connection is shared with multiple threads. | |
| 46 enum ConnectionType { | |
| 47 PRIVATE, | |
| 48 SHARED, | |
| 49 }; | |
| 50 | |
| 51 // Options used to create a Bus object. | |
| 52 struct Options { | |
| 53 Options(); | |
|
stevenjb
2011/07/29 21:54:17
For a struct should probably just inline the const
satorux1
2011/08/01 19:56:41
That will break the Clang bot. I learned it hard w
satorux1
2011/08/01 20:01:49
I take it back. This struct is probably ok, as mem
| |
| 54 BusType bus_type; | |
| 55 ConnectionType connection_type; | |
| 56 // If the thread is set, the bus object will use this thread to | |
| 57 // process asynchronous method and signal calls. The thread has to be | |
| 58 // 1) already running. 2) has a MessageLoopForIO. | |
| 59 base::Thread* io_thread; | |
| 60 }; | |
| 61 | |
| 62 // Creates a Bus object. The actual connection will be established when | |
| 63 // Init() is called. | |
| 64 Bus(const Options& options); | |
|
stevenjb
2011/07/29 21:54:17
explicit
satorux1
2011/08/01 19:56:41
Done.
| |
| 65 virtual ~Bus(); | |
| 66 | |
| 67 // Gets the object proxy for the given service name and the object path. | |
| 68 // |service_name| looks like "org.freedesktop.NetworkManager", and | |
| 69 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0". | |
| 70 // | |
| 71 // This method never returns NULL. The caller should delete the object. | |
| 72 virtual ObjectProxy* GetObjectProxy(const std::string& service_name, | |
| 73 const std::string& object_path); | |
| 74 | |
| 75 // Returns true if the bus is configured to use the IO thread. | |
| 76 bool use_io_thread() { | |
| 77 return io_thread_ != NULL; | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 friend class ObjectProxy; | |
|
stevenjb
2011/07/29 21:54:17
We should avoid necessitating this friendship if p
satorux1
2011/08/01 19:56:41
Removed them for now.
| |
| 82 DBusConnection* connection() { return connection_; } | |
| 83 base::Thread* io_thread() { return io_thread_; } | |
| 84 | |
| 85 // Initializes the bus by establishing a connection with the D-Bus. | |
| 86 // Returns true on success. | |
| 87 // | |
| 88 // This is a bit tricky but we put off calling this function until when | |
| 89 // we first send a message to the D-Bus, as connecting to D-Bus is a | |
| 90 // blocking call, and we don't want to do this when the Bus object is | |
| 91 // created (that can be in the UI thread). | |
| 92 bool Init(); | |
|
stevenjb
2011/07/29 21:54:17
Should probably rename this since Init() is typica
satorux1
2011/08/01 19:56:41
Good point. Renamed to Connect().
| |
| 93 | |
| 94 // Sets up things needed to start async operations. | |
| 95 void SetUpAsyncOperations(); | |
| 96 | |
| 97 // Processes the all incoming data to the connection, if any. | |
| 98 void ProcessAllIncomingDataIfAny(DBusConnection* connection); | |
| 99 | |
| 100 // Posts the task to the message loop of the IO thread. | |
| 101 virtual void PostTaskToIoThread( | |
| 102 const tracked_objects::Location& from_here, | |
| 103 const base::Closure& task); | |
| 104 | |
| 105 // Called when a watch object is added. | |
| 106 dbus_bool_t OnAddWatch(DBusWatch* raw_watch); | |
| 107 // Called when a watch object is removed. | |
| 108 void OnRemoveWatch(DBusWatch* raw_watch); | |
| 109 // Called when the "enabled" status of |raw_watch| is toggled. | |
| 110 void OnToggleWatch(DBusWatch* raw_watch); | |
| 111 // Called when the dispatch status (i.e. if any incoming data is | |
| 112 // available) is changed. | |
| 113 void OnDispatchStatusChanged(DBusConnection* connection, | |
| 114 DBusDispatchStatus status); | |
| 115 | |
| 116 // Redirects the function call to OnAddWatch(). | |
| 117 static dbus_bool_t OnAddWatchStub(DBusWatch* raw_watch, void* data); | |
| 118 // Redirects the function call to OnRemoveWatch(). | |
| 119 static void OnRemoveWatchStub(DBusWatch* raw_watch, void* data); | |
| 120 // Redirects the function call to OnToggleWatch(). | |
| 121 static void OnToggleWatchStub(DBusWatch* raw_watch, void* data); | |
| 122 // Redirects the function call to OnDispatchStatusChanged(). | |
| 123 static void OnDispatchStatusChangedStub(DBusConnection* connection, | |
| 124 DBusDispatchStatus status, | |
| 125 void* data); | |
| 126 const BusType bus_type_; | |
| 127 const ConnectionType connection_type_; | |
| 128 base::Thread* io_thread_; | |
| 129 DBusConnection* connection_; | |
| 130 | |
| 131 DISALLOW_COPY_AND_ASSIGN(Bus); | |
| 132 }; | |
| 133 | |
| 134 } // namespace dbus | |
| 135 | |
| 136 #endif // DBUS_BUS_H_ | |
| OLD | NEW |