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 // |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 // bool success = !!response.get(); |
satorux1
2011/09/06 17:15:57
Thank you for updating this!
!! is a bit tricky.
Mike Mammarella
2011/09/06 19:11:08
Done.
| |
85 // | 85 // |
86 // Asynchronous method call: | 86 // Asynchronous method call: |
87 // | 87 // |
88 // void OnResponse(dbus::Response* response) { | 88 // void OnResponse(dbus::Response* response) { |
89 // // response is NULL if the method call failed. | 89 // // response is NULL if the method call failed. |
90 // if (!response) | 90 // if (!response) |
91 // return; | 91 // return; |
92 // } | 92 // } |
93 // | 93 // |
94 // ... | 94 // ... |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 SHARED, | 147 SHARED, |
148 }; | 148 }; |
149 | 149 |
150 // Options used to create a Bus object. | 150 // Options used to create a Bus object. |
151 struct Options { | 151 struct Options { |
152 Options(); | 152 Options(); |
153 ~Options(); | 153 ~Options(); |
154 | 154 |
155 BusType bus_type; // SESSION by default. | 155 BusType bus_type; // SESSION by default. |
156 ConnectionType connection_type; // PRIVATE by default. | 156 ConnectionType connection_type; // PRIVATE by default. |
157 // If the thread is set, the bus object will use the message loop | 157 // If dbus_thread is set, the bus object will use that message loop to |
158 // attached to the thread to process asynchronous operations. | 158 // process asynchronous operations. While the type of dbus_thread is |
159 // MessageLoopProxy, this is only for convenience so it can be set with | |
160 // BrowserThread::GetMessageLoopProxyForThread(). | |
satorux1
2011/09/06 17:15:57
I like the idea of using MessageLoopProxy.
The na
Mike Mammarella
2011/09/06 19:11:08
Done.
| |
159 // | 161 // |
160 // The thread should meet the following requirements: | 162 // The thread should meet the following requirements: |
161 // 1) Already running. | 163 // 1) Already running. |
162 // 2) Has a MessageLoopForIO. | 164 // 2) Has a MessageLoopForIO. |
163 // 3) Outlives the bus. | 165 // 3) Outlives the bus. |
satorux1
2011/09/06 17:15:57
3) may be now irrelevant, as MessageLoopProxy take
Mike Mammarella
2011/09/06 19:11:08
Done.
| |
164 base::Thread* dbus_thread; // NULL by default. | 166 scoped_refptr<base::MessageLoopProxy> 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_; |
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 |