1 // Copyright (c) 2012 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 8 #include <dbus/dbus.h> 9 10 #include <map> 11 #include <set> 12 #include <string> 13 #include <utility> 14 #include <vector> 15 16 #include "base/callback.h" 17 #include "base/memory/ref_counted.h" 18 #include "base/synchronization/waitable_event.h" 19 #include "base/threading/platform_thread.h" 20 #include "dbus/dbus_export.h" 21 #include "dbus/object_path.h" 22 23 namespace base { 24 class SequencedTaskRunner; 25 class SingleThreadTaskRunner; 26 } 27 28 namespace tracked_objects { 29 class Location; 30 } 31 32 namespace dbus { 33 34 class ExportedObject; 35 class ObjectManager; 36 class ObjectProxy; 37 38 // Bus is used to establish a connection with D-Bus, create object 39 // proxies, and export objects. 40 // 41 // For asynchronous operations such as an asynchronous method call, the 42 // bus object will use a task runner to monitor the underlying file 43 // descriptor used for D-Bus communication. By default, the bus will use 44 // the current thread's task runner. If |dbus_task_runner| option is 45 // specified, the bus will use that task runner instead. 46 // 47 // THREADING 48 // 49 // In the D-Bus library, we use the two threads: 50 // 51 // - The origin thread: the thread that created the Bus object. 52 // - The D-Bus thread: the thread servicing |dbus_task_runner|. 53 // 54 // The origin thread is usually Chrome's UI thread. The D-Bus thread is 55 // usually a dedicated thread for the D-Bus library. 56 // 57 // BLOCKING CALLS 58 // 59 // Functions that issue blocking calls are marked "BLOCKING CALL" and 60 // these functions should be called in the D-Bus thread (if 61 // supplied). AssertOnDBusThread() is placed in these functions. 62 // 63 // Note that it's hard to tell if a libdbus function is actually blocking 64 // or not (ex. dbus_bus_request_name() internally calls 65 // dbus_connection_send_with_reply_and_block(), which is a blocking 66 // call). To err on the safe side, we consider all libdbus functions that 67 // deal with the connection to dbus-daemon to be blocking. 68 // 69 // SHUTDOWN 70 // 71 // The Bus object must be shut down manually by ShutdownAndBlock() and 72 // friends. We require the manual shutdown to make the operation explicit 73 // rather than doing it silently in the destructor. 74 // 75 // EXAMPLE USAGE: 76 // 77 // Synchronous method call: 78 // 79 // dbus::Bus::Options options; 80 // // Set up the bus options here. 81 // ... 82 // dbus::Bus bus(options); 83 // 84 // dbus::ObjectProxy* object_proxy = 85 // bus.GetObjectProxy(service_name, object_path); 86 // 87 // dbus::MethodCall method_call(interface_name, method_name); 88 // scoped_ptr<dbus::Response> response( 89 // object_proxy.CallMethodAndBlock(&method_call, timeout_ms)); 90 // if (response.get() != NULL) { // Success. 91 // ... 92 // } 93 // 94 // Asynchronous method call: 95 // 96 // void OnResponse(dbus::Response* response) { 97 // // response is NULL if the method call failed. 98 // if (!response) 99 // return; 100 // } 101 // 102 // ... 103 // object_proxy.CallMethod(&method_call, timeout_ms, 104 // base::Bind(&OnResponse)); 105 // 106 // Exporting a method: 107 // 108 // void Echo(dbus::MethodCall* method_call, 109 // dbus::ExportedObject::ResponseSender response_sender) { 110 // // Do something with method_call. 111 // Response* response = Response::FromMethodCall(method_call); 112 // // Build response here. 113 // // Can send an immediate response here to implement a synchronous service 114 // // or store the response_sender and send a response later to implement an 115 // // asynchronous service. 116 // response_sender.Run(response); 117 // } 118 // 119 // void OnExported(const std::string& interface_name, 120 // const ObjectPath& object_path, 121 // bool success) { 122 // // success is true if the method was exported successfully. 123 // } 124 // 125 // ... 126 // dbus::ExportedObject* exported_object = 127 // bus.GetExportedObject(service_name, object_path); 128 // exported_object.ExportMethod(interface_name, method_name, 129 // base::Bind(&Echo), 130 // base::Bind(&OnExported)); 131 // 132 // WHY IS THIS A REF COUNTED OBJECT? 133 // 134 // Bus is a ref counted object, to ensure that |this| of the object is 135 // alive when callbacks referencing |this| are called. However, after the 136 // bus is shut down, |connection_| can be NULL. Hence, callbacks should 137 // not rely on that |connection_| is alive. 138 class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> { 139 public: 140 // Specifies the bus type. SESSION is used to communicate with per-user 141 // services like GNOME applications. SYSTEM is used to communicate with 142 // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to 143 // communicate with an user specified address. 144 enum BusType { 145 SESSION = DBUS_BUS_SESSION, 146 SYSTEM = DBUS_BUS_SYSTEM, 147 CUSTOM_ADDRESS, 148 }; 149 150 // Specifies the connection type. PRIVATE should usually be used unless 151 // you are sure that SHARED is safe for you, which is unlikely the case 152 // in Chrome. 153 // 154 // PRIVATE gives you a private connection, that won't be shared with 155 // other Bus objects. 156 // 157 // SHARED gives you a connection shared among other Bus objects, which 158 // is unsafe if the connection is shared with multiple threads. 159 enum ConnectionType { 160 PRIVATE, 161 SHARED, 162 }; 163 164 // Specifies whether the GetServiceOwnerAndBlock call should report or 165 // suppress errors. 166 enum GetServiceOwnerOption { 167 REPORT_ERRORS, 168 SUPPRESS_ERRORS, 169 }; 170 171 // Specifies service ownership options. 172 // 173 // REQUIRE_PRIMARY indicates that you require primary ownership of the 174 // service name. 175 // 176 // ALLOW_REPLACEMENT indicates that you'll allow another connection to 177 // steal ownership of this service name from you. 178 // 179 // REQUIRE_PRIMARY_ALLOW_REPLACEMENT does the obvious. 180 enum ServiceOwnershipOptions { 181 REQUIRE_PRIMARY = (DBUS_NAME_FLAG_DO_NOT_QUEUE | 182 DBUS_NAME_FLAG_REPLACE_EXISTING), 183 REQUIRE_PRIMARY_ALLOW_REPLACEMENT = (REQUIRE_PRIMARY | 184 DBUS_NAME_FLAG_ALLOW_REPLACEMENT), 185 }; 186 187 // Options used to create a Bus object. 188 struct CHROME_DBUS_EXPORT Options { 189 Options(); 190 ~Options(); 191 192 BusType bus_type; // SESSION by default. 193 ConnectionType connection_type; // PRIVATE by default. 194 // If dbus_task_runner is set, the bus object will use that 195 // task runner to process asynchronous operations. 196 // 197 // The thread servicing the task runner should meet the following 198 // requirements: 199 // 1) Already running. 200 // 2) Has a MessageLoopForIO. 201 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner; 202 203 // Specifies the server addresses to be connected. If you want to 204 // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to 205 // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to 206 // connect to. The format of this address value is the dbus address style 207 // which is described in 208 // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses 209 // 210 // EXAMPLE USAGE: 211 // dbus::Bus::Options options; 212 // options.bus_type = CUSTOM_ADDRESS; 213 // options.address.assign("unix:path=/tmp/dbus-XXXXXXX"); 214 // // Set up other options 215 // dbus::Bus bus(options); 216 // 217 // // Do something. 218 // 219 std::string address; 220 221 // If the connection with dbus-daemon is closed, |disconnected_callback| 222 // will be called on the origin thread. This is also called when the 223 // disonnection by ShutdownAndBlock. |disconnected_callback| can be null 224 // callback 225 base::Closure disconnected_callback; 226 }; 227 228 // Creates a Bus object. The actual connection will be established when 229 // Connect() is called. 230 explicit Bus(const Options& options); 231 232 // Called when an ownership request is complete. 233 // Parameters: 234 // - the requested service name. 235 // - whether ownership has been obtained or not. 236 typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback; 237 238 // Called when GetServiceOwner() completes. 239 // |service_owner| is the return value from GetServiceOwnerAndBlock(). 240 typedef base::Callback<void (const std::string& service_owner)> 241 GetServiceOwnerCallback; 242 243 // TODO(satorux): Remove the service name parameter as the caller of 244 // RequestOwnership() knows the service name. 245 246 // Gets the object proxy for the given service name and the object path. 247 // The caller must not delete the returned object. 248 // 249 // Returns an existing object proxy if the bus object already owns the 250 // object proxy for the given service name and the object path. 251 // Never returns NULL. 252 // 253 // The bus will own all object proxies created by the bus, to ensure 254 // that the object proxies are detached from remote objects at the 255 // shutdown time of the bus. 256 // 257 // The object proxy is used to call methods of remote objects, and 258 // receive signals from them. 259 // 260 // |service_name| looks like "org.freedesktop.NetworkManager", and 261 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0". 262 // 263 // Must be called in the origin thread. 264 virtual ObjectProxy* GetObjectProxy(const std::string& service_name, 265 const ObjectPath& object_path); 266 267 // Same as above, but also takes a bitfield of ObjectProxy::Options. 268 // See object_proxy.h for available options. 269 virtual ObjectProxy* GetObjectProxyWithOptions( 270 const std::string& service_name, 271 const ObjectPath& object_path, 272 int options); 273 274 // Removes the previously created object proxy for the given service 275 // name and the object path and releases its memory. 276 // 277 // If and object proxy for the given service name and object was 278 // created with GetObjectProxy, this function removes it from the 279 // bus object and detaches the ObjectProxy, invalidating any pointer 280 // previously acquired for it with GetObjectProxy. A subsequent call 281 // to GetObjectProxy will return a new object. 282 // 283 // All the object proxies are detached from remote objects at the 284 // shutdown time of the bus, but they can be detached early to reduce 285 // memory footprint and used match rules for the bus connection. 286 // 287 // |service_name| looks like "org.freedesktop.NetworkManager", and 288 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0". 289 // |callback| is called when the object proxy is successfully removed and 290 // detached. 291 // 292 // The function returns true when there is an object proxy matching the 293 // |service_name| and |object_path| to remove, and calls |callback| when it 294 // is removed. Otherwise, it returns false and the |callback| function is 295 // never called. The |callback| argument must not be null. 296 // 297 // Must be called in the origin thread. 298 virtual bool RemoveObjectProxy(const std::string& service_name, 299 const ObjectPath& object_path, 300 const base::Closure& callback); 301 302 // Same as above, but also takes a bitfield of ObjectProxy::Options. 303 // See object_proxy.h for available options. 304 virtual bool RemoveObjectProxyWithOptions( 305 const std::string& service_name, 306 const ObjectPath& object_path, 307 int options, 308 const base::Closure& callback); 309 310 // Gets the exported object for the given object path. 311 // The caller must not delete the returned object. 312 // 313 // Returns an existing exported object if the bus object already owns 314 // the exported object for the given object path. Never returns NULL. 315 // 316 // The bus will own all exported objects created by the bus, to ensure 317 // that the exported objects are unregistered at the shutdown time of 318 // the bus. 319 // 320 // The exported object is used to export methods of local objects, and 321 // send signal from them. 322 // 323 // Must be called in the origin thread. 324 virtual ExportedObject* GetExportedObject(const ObjectPath& object_path); 325 326 // Unregisters the exported object for the given object path |object_path|. 327 // 328 // Getting an exported object for the same object path after this call 329 // will return a new object, method calls on any remaining copies of the 330 // previous object will not be called. 331 // 332 // Must be called in the origin thread. 333 virtual void UnregisterExportedObject(const ObjectPath& object_path); 334 335 336 // Gets an object manager for the given remote object path |object_path| 337 // exported by the service |service_name|. 338 // 339 // Returns an existing object manager if the bus object already owns a 340 // matching object manager, never returns NULL. 341 // 342 // The caller must not delete the returned object, the bus retains ownership 343 // of all object managers. 344 // 345 // Must be called in the origin thread. 346 virtual ObjectManager* GetObjectManager(const std::string& service_name, 347 const ObjectPath& object_path); 348 349 // Unregisters the object manager for the given remote object path 350 // |object_path| exported by the srevice |service_name|. 351 // 352 // Getting an object manager for the same remote object after this call 353 // will return a new object, method calls on any remaining copies of the 354 // previous object are not permitted. 355 // 356 // Must be called in the origin thread. 357 virtual void RemoveObjectManager(const std::string& service_name, 358 const ObjectPath& object_path); 359 360 // Instructs all registered object managers to retrieve their set of managed 361 // objects from their respective remote objects. There is no need to call this 362 // manually, this is called automatically by the D-Bus thread manager once 363 // implementation classes are registered. 364 virtual void GetManagedObjects(); 365 366 // Shuts down the bus and blocks until it's done. More specifically, this 367 // function does the following: 368 // 369 // - Unregisters the object paths 370 // - Releases the service names 371 // - Closes the connection to dbus-daemon. 372 // 373 // This function can be called multiple times and it is no-op for the 2nd time 374 // calling. 375 // 376 // BLOCKING CALL. 377 virtual void ShutdownAndBlock(); 378 379 // Similar to ShutdownAndBlock(), but this function is used to 380 // synchronously shut down the bus that uses the D-Bus thread. This 381 // function is intended to be used at the very end of the browser 382 // shutdown, where it makes more sense to shut down the bus 383 // synchronously, than trying to make it asynchronous. 384 // 385 // BLOCKING CALL, but must be called in the origin thread. 386 virtual void ShutdownOnDBusThreadAndBlock(); 387 388 // Returns true if the shutdown has been completed. 389 bool shutdown_completed() { return shutdown_completed_; } 390 391 // 392 // The public functions below are not intended to be used in client 393 // code. These are used to implement ObjectProxy and ExportedObject. 394 // 395 396 // Connects the bus to the dbus-daemon. 397 // Returns true on success, or the bus is already connected. 398 // 399 // BLOCKING CALL. 400 virtual bool Connect(); 401 402 // Disconnects the bus from the dbus-daemon. 403 // Safe to call multiple times and no operation after the first call. 404 // Do not call for shared connection it will be released by libdbus. 405 // 406 // BLOCKING CALL. 407 virtual void ClosePrivateConnection(); 408 409 // Requests the ownership of the service name given by |service_name|. 410 // See also RequestOwnershipAndBlock(). 411 // 412 // |on_ownership_callback| is called when the service name is obtained 413 // or failed to be obtained, in the origin thread. 414 // 415 // Must be called in the origin thread. 416 virtual void RequestOwnership(const std::string& service_name, 417 ServiceOwnershipOptions options, 418 OnOwnershipCallback on_ownership_callback); 419 420 // Requests the ownership of the given service name. 421 // Returns true on success, or the the service name is already obtained. 422 // 423 // BLOCKING CALL. 424 virtual bool RequestOwnershipAndBlock(const std::string& service_name, 425 ServiceOwnershipOptions options); 426 427 // Releases the ownership of the given service name. 428 // Returns true on success. 429 // 430 // BLOCKING CALL. 431 virtual bool ReleaseOwnership(const std::string& service_name); 432 433 // Sets up async operations. 434 // Returns true on success, or it's already set up. 435 // This function needs to be called before starting async operations. 436 // 437 // BLOCKING CALL. 438 virtual bool SetUpAsyncOperations(); 439 440 // Sends a message to the bus and blocks until the response is 441 // received. Used to implement synchronous method calls. 442 // 443 // BLOCKING CALL. 444 virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request, 445 int timeout_ms, 446 DBusError* error); 447 448 // Requests to send a message to the bus. The reply is handled with 449 // |pending_call| at a later time. 450 // 451 // BLOCKING CALL. 452 virtual void SendWithReply(DBusMessage* request, 453 DBusPendingCall** pending_call, 454 int timeout_ms); 455 456 // Requests to send a message to the bus. The message serial number will 457 // be stored in |serial|. 458 // 459 // BLOCKING CALL. 460 virtual void Send(DBusMessage* request, uint32* serial); 461 462 // Adds the message filter function. |filter_function| will be called 463 // when incoming messages are received. Returns true on success. 464 // 465 // When a new incoming message arrives, filter functions are called in 466 // the order that they were added until the the incoming message is 467 // handled by a filter function. 468 // 469 // The same filter function associated with the same user data cannot be 470 // added more than once. Returns false for this case. 471 // 472 // BLOCKING CALL. 473 virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function, 474 void* user_data); 475 476 // Removes the message filter previously added by AddFilterFunction(). 477 // Returns true on success. 478 // 479 // BLOCKING CALL. 480 virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function, 481 void* user_data); 482 483 // Adds the match rule. Messages that match the rule will be processed 484 // by the filter functions added by AddFilterFunction(). 485 // 486 // You cannot specify which filter function to use for a match rule. 487 // Instead, you should check if an incoming message is what you are 488 // interested in, in the filter functions. 489 // 490 // The same match rule can be added more than once and should be removed 491 // as many times as it was added. 492 // 493 // The match rule looks like: 494 // "type='signal', interface='org.chromium.SomeInterface'". 495 // 496 // See "Message Bus Message Routing" section in the D-Bus specification 497 // for details about match rules: 498 // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing 499 // 500 // BLOCKING CALL. 501 virtual void AddMatch(const std::string& match_rule, DBusError* error); 502 503 // Removes the match rule previously added by AddMatch(). 504 // Returns false if the requested match rule is unknown or has already been 505 // removed. Otherwise, returns true and sets |error| accordingly. 506 // 507 // BLOCKING CALL. 508 virtual bool RemoveMatch(const std::string& match_rule, DBusError* error); 509 510 // Tries to register the object path. Returns true on success. 511 // Returns false if the object path is already registered. 512 // 513 // |message_function| in |vtable| will be called every time when a new 514 // |message sent to the object path arrives. 515 // 516 // The same object path must not be added more than once. 517 // 518 // See also documentation of |dbus_connection_try_register_object_path| at 519 // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html 520 // 521 // BLOCKING CALL. 522 virtual bool TryRegisterObjectPath(const ObjectPath& object_path, 523 const DBusObjectPathVTable* vtable, 524 void* user_data, 525 DBusError* error); 526 527 // Unregister the object path. 528 // 529 // BLOCKING CALL. 530 virtual void UnregisterObjectPath(const ObjectPath& object_path); 531 532 // Posts |task| to the task runner of the D-Bus thread. On completion, |reply| 533 // is posted to the origin thread. 534 virtual void PostTaskToDBusThreadAndReply( 535 const tracked_objects::Location& from_here, 536 const base::Closure& task, 537 const base::Closure& reply); 538 539 // Posts the task to the task runner of the thread that created the bus. 540 virtual void PostTaskToOriginThread( 541 const tracked_objects::Location& from_here, 542 const base::Closure& task); 543 544 // Posts the task to the task runner of the D-Bus thread. If D-Bus 545 // thread is not supplied, the task runner of the origin thread will be 546 // used. 547 virtual void PostTaskToDBusThread( 548 const tracked_objects::Location& from_here, 549 const base::Closure& task); 550 551 // Posts the delayed task to the task runner of the D-Bus thread. If 552 // D-Bus thread is not supplied, the task runner of the origin thread 553 // will be used. 554 virtual void PostDelayedTaskToDBusThread( 555 const tracked_objects::Location& from_here, 556 const base::Closure& task, 557 base::TimeDelta delay); 558 559 // Returns true if the bus has the D-Bus thread. 560 virtual bool HasDBusThread(); 561 562 // Check whether the current thread is on the origin thread (the thread 563 // that created the bus). If not, DCHECK will fail. 564 virtual void AssertOnOriginThread(); 565 566 // Check whether the current thread is on the D-Bus thread. If not, 567 // DCHECK will fail. If the D-Bus thread is not supplied, it calls 568 // AssertOnOriginThread(). 569 virtual void AssertOnDBusThread(); 570 571 // Gets the owner for |service_name| via org.freedesktop.DBus.GetNameOwner. 572 // Returns the owner name, if any, or an empty string on failure. 573 // |options| specifies where to printing error messages or not. 574 // 575 // BLOCKING CALL. 576 virtual std::string GetServiceOwnerAndBlock(const std::string& service_name, 577 GetServiceOwnerOption options); 578 579 // A non-blocking version of GetServiceOwnerAndBlock(). 580 // Must be called in the origin thread. 581 virtual void GetServiceOwner(const std::string& service_name, 582 const GetServiceOwnerCallback& callback); 583 584 // Whenever the owner for |service_name| changes, run |callback| with the 585 // name of the new owner. If the owner goes away, then |callback| receives 586 // an empty string. 587 // 588 // Any unique (service_name, callback) can be used. Duplicate are ignored. 589 // |service_name| must not be empty and |callback| must not be null. 590 // 591 // Must be called in the origin thread. 592 virtual void ListenForServiceOwnerChange( 593 const std::string& service_name, 594 const GetServiceOwnerCallback& callback); 595 596 // Stop listening for |service_name| owner changes for |callback|. 597 // Any unique (service_name, callback) can be used. Non-registered callbacks 598 // for a given service name are ignored. 599 // |service_name| must not be empty and |callback| must not be null. 600 // 601 // Must be called in the origin thread. 602 virtual void UnlistenForServiceOwnerChange( 603 const std::string& service_name, 604 const GetServiceOwnerCallback& callback); 605 606 // Returns true if the bus is connected to D-Bus. 607 bool is_connected() { return connection_ != NULL; } 608 609 protected: 610 // This is protected, so we can define sub classes. 611 virtual ~Bus(); 612 613 private: 614 friend class base::RefCountedThreadSafe<Bus>; 615 616 // Helper function used for RemoveObjectProxy(). 617 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy, 618 const base::Closure& callback); 619 620 // Helper function used for UnregisterExportedObject(). 621 void UnregisterExportedObjectInternal( 622 scoped_refptr<dbus::ExportedObject> exported_object); 623 624 // Helper function used for ShutdownOnDBusThreadAndBlock(). 625 void ShutdownOnDBusThreadAndBlockInternal(); 626 627 // Helper function used for RequestOwnership(). 628 void RequestOwnershipInternal(const std::string& service_name, 629 ServiceOwnershipOptions options, 630 OnOwnershipCallback on_ownership_callback); 631 632 // Helper function used for GetServiceOwner(). 633 void GetServiceOwnerInternal(const std::string& service_name, 634 const GetServiceOwnerCallback& callback); 635 636 // Helper function used for ListenForServiceOwnerChange(). 637 void ListenForServiceOwnerChangeInternal( 638 const std::string& service_name, 639 const GetServiceOwnerCallback& callback); 640 641 // Helper function used for UnListenForServiceOwnerChange(). 642 void UnlistenForServiceOwnerChangeInternal( 643 const std::string& service_name, 644 const GetServiceOwnerCallback& callback); 645 646 // Processes the all incoming data to the connection, if any. 647 // 648 // BLOCKING CALL. 649 void ProcessAllIncomingDataIfAny(); 650 651 // Called when a watch object is added. Used to start monitoring the 652 // file descriptor used for D-Bus communication. 653 dbus_bool_t OnAddWatch(DBusWatch* raw_watch); 654 655 // Called when a watch object is removed. 656 void OnRemoveWatch(DBusWatch* raw_watch); 657 658 // Called when the "enabled" status of |raw_watch| is toggled. 659 void OnToggleWatch(DBusWatch* raw_watch); 660 661 // Called when a timeout object is added. Used to start monitoring 662 // timeout for method calls. 663 dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout); 664 665 // Called when a timeout object is removed. 666 void OnRemoveTimeout(DBusTimeout* raw_timeout); 667 668 // Called when the "enabled" status of |raw_timeout| is toggled. 669 void OnToggleTimeout(DBusTimeout* raw_timeout); 670 671 // Called when the dispatch status (i.e. if any incoming data is 672 // available) is changed. 673 void OnDispatchStatusChanged(DBusConnection* connection, 674 DBusDispatchStatus status); 675 676 // Called when the connection is diconnected. 677 void OnConnectionDisconnected(DBusConnection* connection); 678 679 // Called when a service owner change occurs. 680 void OnServiceOwnerChanged(DBusMessage* message); 681 682 // Callback helper functions. Redirects to the corresponding member function. 683 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data); 684 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data); 685 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data); 686 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data); 687 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data); 688 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data); 689 static void OnDispatchStatusChangedThunk(DBusConnection* connection, 690 DBusDispatchStatus status, 691 void* data); 692 693 // Calls OnConnectionDisconnected if the Disconnected signal is received. 694 static DBusHandlerResult OnConnectionDisconnectedFilter( 695 DBusConnection* connection, 696 DBusMessage* message, 697 void* user_data); 698 699 // Calls OnServiceOwnerChanged for a NameOwnerChanged signal. 700 static DBusHandlerResult OnServiceOwnerChangedFilter( 701 DBusConnection* connection, 702 DBusMessage* message, 703 void* user_data); 704 705 const BusType bus_type_; 706 const ConnectionType connection_type_; 707 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_; 708 base::WaitableEvent on_shutdown_; 709 DBusConnection* connection_; 710 711 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; 712 base::PlatformThreadId origin_thread_id_; 713 714 std::set<std::string> owned_service_names_; 715 // The following sets are used to check if rules/object_paths/filters 716 // are properly cleaned up before destruction of the bus object. 717 // Since it's not an error to add the same match rule twice, the repeated 718 // match rules are counted in a map. 719 std::map<std::string, int> match_rules_added_; 720 std::set<ObjectPath> registered_object_paths_; 721 std::set<std::pair<DBusHandleMessageFunction, void*> > 722 filter_functions_added_; 723 724 // ObjectProxyTable is used to hold the object proxies created by the 725 // bus object. Key is a pair; the first part is a concatenated string of 726 // service name + object path, like 727 // "org.chromium.TestService/org/chromium/TestObject". 728 // The second part is the ObjectProxy::Options for the proxy. 729 typedef std::map<std::pair<std::string, int>, 730 scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable; 731 ObjectProxyTable object_proxy_table_; 732 733 // ExportedObjectTable is used to hold the exported objects created by 734 // the bus object. Key is a concatenated string of service name + 735 // object path, like "org.chromium.TestService/org/chromium/TestObject". 736 typedef std::map<const dbus::ObjectPath, 737 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable; 738 ExportedObjectTable exported_object_table_; 739 740 // ObjectManagerTable is used to hold the object managers created by the 741 // bus object. Key is a concatenated string of service name + object path, 742 // like "org.chromium.TestService/org/chromium/TestObject". 743 typedef std::map<std::string, 744 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable; 745 ObjectManagerTable object_manager_table_; 746 747 // A map of NameOwnerChanged signals to listen for and the callbacks to run 748 // on the origin thread when the owner changes. 749 // Only accessed on the DBus thread. 750 // Key: Service name 751 // Value: Vector of callbacks. Unique and expected to be small. Not using 752 // std::set here because base::Callbacks don't have a '<' operator. 753 typedef std::map<std::string, std::vector<GetServiceOwnerCallback> > 754 ServiceOwnerChangedListenerMap; 755 ServiceOwnerChangedListenerMap service_owner_changed_listener_map_; 756 757 bool async_operations_set_up_; 758 bool shutdown_completed_; 759 760 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and 761 // OnAddTimeout()/OnRemoveTimeou() are balanced. 762 int num_pending_watches_; 763 int num_pending_timeouts_; 764 765 std::string address_; 766 base::Closure on_disconnected_closure_; 767 768 DISALLOW_COPY_AND_ASSIGN(Bus); 769 }; 770 771 } // namespace dbus 772 773 #endif // DBUS_BUS_H_ 774