Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Unified Diff: dbus/bus.h

Issue 7491029: Implement Bus and ObjectProxy classes for our D-Bus library. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: work-in-progress Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | dbus/bus.cc » ('j') | dbus/bus.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dbus/bus.h
diff --git a/dbus/bus.h b/dbus/bus.h
new file mode 100644
index 0000000000000000000000000000000000000000..529d5be7c1a82ef6f58ed81c363d416257d2d4d2
--- /dev/null
+++ b/dbus/bus.h
@@ -0,0 +1,136 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef DBUS_BUS_H_
+#define DBUS_BUS_H_
+#pragma once
+
+#include <string>
+#include <dbus/dbus.h>
+
+#include "base/callback.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/tracked_objects.h"
+
+namespace base {
+class Thread;
+}
+
+namespace dbus {
+
+class ObjectProxy;
+
+// Bus is used to establish a connection with D-Bus, and create object
+// proxies.
+//
+//
+// For asynchronous method and signal calls, the bus object will use a
+// separate thread if io_thread option is specified. This is useful if you
+// want to issue D-Bus calls from UI thread.
+class Bus {
+ public:
+ // Specifies the bus type. SESSION is used to communicate with per-user
+ // services like GNOME applications. SYSTEM is used to communicate with
+ // system-wide services like NetworkManager.
+ enum BusType {
+ SESSION = DBUS_BUS_SESSION,
+ SYSTEM = DBUS_BUS_SYSTEM,
+ };
+
+ // Specifies the connection type. PRIVATE should usually be used unless
+ // you are sure that SHARED is safe for you. PRIVATE gives you a private
+ // connection, that won't be shared with other Bus objects. SHARED gives
+ // you a connection shared among other Bus objects, which is unsafe if
+ // the connection is shared with multiple threads.
+ enum ConnectionType {
+ PRIVATE,
+ SHARED,
+ };
+
+ // Options used to create a Bus object.
+ struct Options {
+ 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
+ BusType bus_type;
+ ConnectionType connection_type;
+ // If the thread is set, the bus object will use this thread to
+ // process asynchronous method and signal calls. The thread has to be
+ // 1) already running. 2) has a MessageLoopForIO.
+ base::Thread* io_thread;
+ };
+
+ // Creates a Bus object. The actual connection will be established when
+ // Init() is called.
+ Bus(const Options& options);
stevenjb 2011/07/29 21:54:17 explicit
satorux1 2011/08/01 19:56:41 Done.
+ virtual ~Bus();
+
+ // Gets the object proxy for the given service name and the object path.
+ // |service_name| looks like "org.freedesktop.NetworkManager", and
+ // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
+ //
+ // This method never returns NULL. The caller should delete the object.
+ virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
+ const std::string& object_path);
+
+ // Returns true if the bus is configured to use the IO thread.
+ bool use_io_thread() {
+ return io_thread_ != NULL;
+ }
+
+ private:
+ 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.
+ DBusConnection* connection() { return connection_; }
+ base::Thread* io_thread() { return io_thread_; }
+
+ // Initializes the bus by establishing a connection with the D-Bus.
+ // Returns true on success.
+ //
+ // This is a bit tricky but we put off calling this function until when
+ // we first send a message to the D-Bus, as connecting to D-Bus is a
+ // blocking call, and we don't want to do this when the Bus object is
+ // created (that can be in the UI thread).
+ 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().
+
+ // Sets up things needed to start async operations.
+ void SetUpAsyncOperations();
+
+ // Processes the all incoming data to the connection, if any.
+ void ProcessAllIncomingDataIfAny(DBusConnection* connection);
+
+ // Posts the task to the message loop of the IO thread.
+ virtual void PostTaskToIoThread(
+ const tracked_objects::Location& from_here,
+ const base::Closure& task);
+
+ // Called when a watch object is added.
+ dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
+ // Called when a watch object is removed.
+ void OnRemoveWatch(DBusWatch* raw_watch);
+ // Called when the "enabled" status of |raw_watch| is toggled.
+ void OnToggleWatch(DBusWatch* raw_watch);
+ // Called when the dispatch status (i.e. if any incoming data is
+ // available) is changed.
+ void OnDispatchStatusChanged(DBusConnection* connection,
+ DBusDispatchStatus status);
+
+ // Redirects the function call to OnAddWatch().
+ static dbus_bool_t OnAddWatchStub(DBusWatch* raw_watch, void* data);
+ // Redirects the function call to OnRemoveWatch().
+ static void OnRemoveWatchStub(DBusWatch* raw_watch, void* data);
+ // Redirects the function call to OnToggleWatch().
+ static void OnToggleWatchStub(DBusWatch* raw_watch, void* data);
+ // Redirects the function call to OnDispatchStatusChanged().
+ static void OnDispatchStatusChangedStub(DBusConnection* connection,
+ DBusDispatchStatus status,
+ void* data);
+ const BusType bus_type_;
+ const ConnectionType connection_type_;
+ base::Thread* io_thread_;
+ DBusConnection* connection_;
+
+ DISALLOW_COPY_AND_ASSIGN(Bus);
+};
+
+} // namespace dbus
+
+#endif // DBUS_BUS_H_
« no previous file with comments | « no previous file | dbus/bus.cc » ('j') | dbus/bus.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698