| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DBUS_BUS_H_ | 5 #ifndef DBUS_BUS_H_ |
| 6 #define DBUS_BUS_H_ | 6 #define DBUS_BUS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <dbus/dbus.h> | 12 #include <dbus/dbus.h> |
| 13 | 13 |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 19 #include "base/tracked_objects.h" | 18 #include "base/tracked_objects.h" |
| 20 | 19 |
| 21 class MessageLoop; | 20 class MessageLoop; |
| 22 | 21 |
| 23 namespace base { | 22 namespace base { |
| 24 class Thread; | 23 class Thread; |
| 24 class MessageLoopProxy; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace dbus { | 27 namespace dbus { |
| 28 | 28 |
| 29 class ExportedObject; | 29 class ExportedObject; |
| 30 class ObjectProxy; | 30 class ObjectProxy; |
| 31 | 31 |
| 32 // Bus is used to establish a connection with D-Bus, create object | 32 // Bus is used to establish a connection with D-Bus, create object |
| 33 // proxies, and export objects. | 33 // proxies, and export objects. |
| 34 // | 34 // |
| 35 // For asynchronous operations such as an asynchronous method call, the | 35 // For asynchronous operations such as an asynchronous method call, the |
| 36 // bus object will use a message loop to monitor the underlying file | 36 // bus object will use a message loop to monitor the underlying file |
| 37 // descriptor used for D-Bus communication. By default, the bus will use | 37 // descriptor used for D-Bus communication. By default, the bus will use |
| 38 // the current thread's MessageLoopForIO. If |dbus_thread| option is | 38 // the current thread's MessageLoopForIO. If |dbus_thread_message_loop_proxy| |
| 39 // specified, the bus will use the D-Bus thread's message loop. | 39 // option is specified, the bus will use that message loop instead. |
| 40 // | 40 // |
| 41 // THREADING | 41 // THREADING |
| 42 // | 42 // |
| 43 // In the D-Bus library, we use the two threads: | 43 // In the D-Bus library, we use the two threads: |
| 44 // | 44 // |
| 45 // - The origin thread: the thread that created the Bus object. | 45 // - The origin thread: the thread that created the Bus object. |
| 46 // - The D-Bus thread: the thread supplied by |dbus_thread| option. | 46 // - The D-Bus thread: the thread servicing |dbus_thread_message_loop_proxy|. |
| 47 // | 47 // |
| 48 // The origin thread is usually Chrome's UI thread. The D-Bus thread is | 48 // The origin thread is usually Chrome's UI thread. The D-Bus thread is |
| 49 // usually a dedicated thread for the D-Bus library. | 49 // usually a dedicated thread for the D-Bus library. |
| 50 // | 50 // |
| 51 // BLOCKING CALLS | 51 // BLOCKING CALLS |
| 52 // | 52 // |
| 53 // Functions that issue blocking calls are marked "BLOCKING CALL" and | 53 // Functions that issue blocking calls are marked "BLOCKING CALL" and |
| 54 // these functions should be called in the D-Bus thread (if | 54 // these functions should be called in the D-Bus thread (if |
| 55 // supplied). AssertOnDBusThread() is placed in these functions. | 55 // supplied). AssertOnDBusThread() is placed in these functions. |
| 56 // | 56 // |
| (...skipping 15 matching lines...) Expand all Loading... |
| 72 // | 72 // |
| 73 // dbus::Bus::Options options; | 73 // dbus::Bus::Options options; |
| 74 // // Set up the bus options here. | 74 // // Set up the bus options here. |
| 75 // ... | 75 // ... |
| 76 // dbus::Bus bus(options); | 76 // dbus::Bus bus(options); |
| 77 // | 77 // |
| 78 // dbus::ObjectProxy* object_proxy = | 78 // dbus::ObjectProxy* object_proxy = |
| 79 // bus.GetObjectProxy(service_name, object_path); | 79 // bus.GetObjectProxy(service_name, object_path); |
| 80 // | 80 // |
| 81 // dbus::MethodCall method_call(interface_name, method_name); | 81 // dbus::MethodCall method_call(interface_name, method_name); |
| 82 // dbus::Response response; | 82 // scoped_ptr<dbus::Response> response( |
| 83 // bool success = | 83 // object_proxy.CallMethodAndBlock(&method_call, timeout_ms)); |
| 84 // object_proxy.CallMethodAndBlock(&method_call, timeout_ms, &response); | 84 // if (response.get() != NULL) { // Success. |
| 85 // ... |
| 86 // } |
| 85 // | 87 // |
| 86 // Asynchronous method call: | 88 // Asynchronous method call: |
| 87 // | 89 // |
| 88 // void OnResponse(dbus::Response* response) { | 90 // void OnResponse(dbus::Response* response) { |
| 89 // // response is NULL if the method call failed. | 91 // // response is NULL if the method call failed. |
| 90 // if (!response) | 92 // if (!response) |
| 91 // return; | 93 // return; |
| 92 // } | 94 // } |
| 93 // | 95 // |
| 94 // ... | 96 // ... |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 SHARED, | 149 SHARED, |
| 148 }; | 150 }; |
| 149 | 151 |
| 150 // Options used to create a Bus object. | 152 // Options used to create a Bus object. |
| 151 struct Options { | 153 struct Options { |
| 152 Options(); | 154 Options(); |
| 153 ~Options(); | 155 ~Options(); |
| 154 | 156 |
| 155 BusType bus_type; // SESSION by default. | 157 BusType bus_type; // SESSION by default. |
| 156 ConnectionType connection_type; // PRIVATE by default. | 158 ConnectionType connection_type; // PRIVATE by default. |
| 157 // If the thread is set, the bus object will use the message loop | 159 // If dbus_thread_message_loop_proxy is set, the bus object will use that |
| 158 // attached to the thread to process asynchronous operations. | 160 // message loop to process asynchronous operations. |
| 159 // | 161 // |
| 160 // The thread should meet the following requirements: | 162 // The thread servicing the message loop proxy should meet the following |
| 163 // requirements: |
| 161 // 1) Already running. | 164 // 1) Already running. |
| 162 // 2) Has a MessageLoopForIO. | 165 // 2) Has a MessageLoopForIO. |
| 163 // 3) Outlives the bus. | 166 scoped_refptr<base::MessageLoopProxy> dbus_thread_message_loop_proxy; |
| 164 base::Thread* dbus_thread; // NULL by default. | |
| 165 }; | 167 }; |
| 166 | 168 |
| 167 // Creates a Bus object. The actual connection will be established when | 169 // Creates a Bus object. The actual connection will be established when |
| 168 // Connect() is called. | 170 // Connect() is called. |
| 169 explicit Bus(const Options& options); | 171 explicit Bus(const Options& options); |
| 170 | 172 |
| 171 // Gets the object proxy for the given service name and the object path. | 173 // Gets the object proxy for the given service name and the object path. |
| 172 // The caller must not delete the returned object. | 174 // The caller must not delete the returned object. |
| 173 // | 175 // |
| 174 // Returns an existing object proxy if the bus object already owns the | 176 // Returns an existing object proxy if the bus object already owns the |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data); | 425 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data); |
| 424 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data); | 426 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data); |
| 425 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 427 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
| 426 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 428 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
| 427 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 429 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
| 428 static void OnDispatchStatusChangedThunk(DBusConnection* connection, | 430 static void OnDispatchStatusChangedThunk(DBusConnection* connection, |
| 429 DBusDispatchStatus status, | 431 DBusDispatchStatus status, |
| 430 void* data); | 432 void* data); |
| 431 const BusType bus_type_; | 433 const BusType bus_type_; |
| 432 const ConnectionType connection_type_; | 434 const ConnectionType connection_type_; |
| 433 base::Thread* dbus_thread_; | 435 scoped_refptr<base::MessageLoopProxy> dbus_thread_message_loop_proxy_; |
| 434 base::WaitableEvent on_shutdown_; | 436 base::WaitableEvent on_shutdown_; |
| 435 DBusConnection* connection_; | 437 DBusConnection* connection_; |
| 436 | 438 |
| 437 MessageLoop* origin_loop_; | 439 MessageLoop* origin_loop_; |
| 438 base::PlatformThreadId origin_thread_id_; | 440 base::PlatformThreadId origin_thread_id_; |
| 439 base::PlatformThreadId dbus_thread_id_; | |
| 440 | 441 |
| 441 std::set<std::string> owned_service_names_; | 442 std::set<std::string> owned_service_names_; |
| 442 // The following sets are used to check if rules/object_paths/filters | 443 // The following sets are used to check if rules/object_paths/filters |
| 443 // are properly cleaned up before destruction of the bus object. | 444 // are properly cleaned up before destruction of the bus object. |
| 444 std::set<std::string> match_rules_added_; | 445 std::set<std::string> match_rules_added_; |
| 445 std::set<std::string> registered_object_paths_; | 446 std::set<std::string> registered_object_paths_; |
| 446 std::set<DBusHandleMessageFunction> filter_functions_added_; | 447 std::set<DBusHandleMessageFunction> filter_functions_added_; |
| 447 | 448 |
| 448 // ObjectProxyTable is used to hold the object proxies created by the | 449 // ObjectProxyTable is used to hold the object proxies created by the |
| 449 // bus object. Key is a concatenated string of service name + object path, | 450 // bus object. Key is a concatenated string of service name + object path, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 466 // OnAddTimeout()/OnRemoveTimeou() are balanced. | 467 // OnAddTimeout()/OnRemoveTimeou() are balanced. |
| 467 int num_pending_watches_; | 468 int num_pending_watches_; |
| 468 int num_pending_timeouts_; | 469 int num_pending_timeouts_; |
| 469 | 470 |
| 470 DISALLOW_COPY_AND_ASSIGN(Bus); | 471 DISALLOW_COPY_AND_ASSIGN(Bus); |
| 471 }; | 472 }; |
| 472 | 473 |
| 473 } // namespace dbus | 474 } // namespace dbus |
| 474 | 475 |
| 475 #endif // DBUS_BUS_H_ | 476 #endif // DBUS_BUS_H_ |
| OLD | NEW |