Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 | 7 |
| 8 #include <dbus/dbus.h> | 8 #include <dbus/dbus.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | |
| 14 | 15 |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
| 19 #include "dbus/dbus_export.h" | 20 #include "dbus/dbus_export.h" |
| 20 #include "dbus/object_path.h" | 21 #include "dbus/object_path.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 class SequencedTaskRunner; | 24 class SequencedTaskRunner; |
| 24 class SingleThreadTaskRunner; | 25 class SingleThreadTaskRunner; |
| 25 class Thread; | |
| 26 } | 26 } |
| 27 | 27 |
| 28 namespace tracked_objects { | 28 namespace tracked_objects { |
| 29 class Location; | 29 class Location; |
| 30 } | 30 } |
| 31 | 31 |
| 32 namespace dbus { | 32 namespace dbus { |
| 33 | 33 |
| 34 class ExportedObject; | 34 class ExportedObject; |
| 35 class ObjectManager; | 35 class ObjectManager; |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 // | 556 // |
| 557 // BLOCKING CALL. | 557 // BLOCKING CALL. |
| 558 virtual std::string GetServiceOwnerAndBlock(const std::string& service_name, | 558 virtual std::string GetServiceOwnerAndBlock(const std::string& service_name, |
| 559 GetServiceOwnerOption options); | 559 GetServiceOwnerOption options); |
| 560 | 560 |
| 561 // A non-blocking version of GetServiceOwnerAndBlock(). | 561 // A non-blocking version of GetServiceOwnerAndBlock(). |
| 562 // Must be called in the origin thread. | 562 // Must be called in the origin thread. |
| 563 virtual void GetServiceOwner(const std::string& service_name, | 563 virtual void GetServiceOwner(const std::string& service_name, |
| 564 const GetServiceOwnerCallback& callback); | 564 const GetServiceOwnerCallback& callback); |
| 565 | 565 |
| 566 // Whenever the owner for |service_name| changes, run |callback| with the | |
| 567 // name of the new owner. | |
|
satorux1
2013/06/03 02:26:06
What's gonna happen if the service name just disap
Lei Zhang
2013/06/04 00:35:53
Done.
| |
| 568 // Must be called in the origin thread. | |
| 569 virtual void ListenForServiceOwnerChange( | |
| 570 const std::string& service_name, | |
| 571 const GetServiceOwnerCallback& callback); | |
| 572 | |
| 573 // Stop listening for |service_name| owner changes for |callback|. | |
| 574 // Must be called in the origin thread. | |
| 575 virtual void UnlistenForServiceOwnerChange( | |
| 576 const std::string& service_name, | |
| 577 const GetServiceOwnerCallback& callback); | |
|
satorux1
2013/06/03 02:26:06
Could you write unit tests for the new functions?
| |
| 578 | |
| 566 // Returns true if the bus is connected to D-Bus. | 579 // Returns true if the bus is connected to D-Bus. |
| 567 bool is_connected() { return connection_ != NULL; } | 580 bool is_connected() { return connection_ != NULL; } |
| 568 | 581 |
| 569 protected: | 582 protected: |
| 570 // This is protected, so we can define sub classes. | 583 // This is protected, so we can define sub classes. |
| 571 virtual ~Bus(); | 584 virtual ~Bus(); |
| 572 | 585 |
| 573 private: | 586 private: |
| 574 friend class base::RefCountedThreadSafe<Bus>; | 587 friend class base::RefCountedThreadSafe<Bus>; |
| 575 | 588 |
| 576 // Helper function used for RemoveObjectProxy(). | 589 // Helper function used for RemoveObjectProxy(). |
| 577 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy, | 590 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy, |
| 578 const base::Closure& callback); | 591 const base::Closure& callback); |
| 579 | 592 |
| 580 // Helper function used for UnregisterExportedObject(). | 593 // Helper function used for UnregisterExportedObject(). |
| 581 void UnregisterExportedObjectInternal( | 594 void UnregisterExportedObjectInternal( |
| 582 scoped_refptr<dbus::ExportedObject> exported_object); | 595 scoped_refptr<dbus::ExportedObject> exported_object); |
| 583 | 596 |
| 584 // Helper function used for ShutdownOnDBusThreadAndBlock(). | 597 // Helper function used for ShutdownOnDBusThreadAndBlock(). |
| 585 void ShutdownOnDBusThreadAndBlockInternal(); | 598 void ShutdownOnDBusThreadAndBlockInternal(); |
| 586 | 599 |
| 587 // Helper function used for RequestOwnership(). | 600 // Helper function used for RequestOwnership(). |
| 588 void RequestOwnershipInternal(const std::string& service_name, | 601 void RequestOwnershipInternal(const std::string& service_name, |
| 589 OnOwnershipCallback on_ownership_callback); | 602 OnOwnershipCallback on_ownership_callback); |
| 590 | 603 |
| 591 // Helper function used for GetServiceOwner(). | 604 // Helper function used for GetServiceOwner(). |
| 592 void GetServiceOwnerInternal(const std::string& service_name, | 605 void GetServiceOwnerInternal(const std::string& service_name, |
| 593 const GetServiceOwnerCallback& callback); | 606 const GetServiceOwnerCallback& callback); |
| 594 | 607 |
| 608 // Helper function used for ListenForServiceOwnerChange(). | |
| 609 void ListenForServiceOwnerChangeInternal( | |
| 610 const std::string& service_name, | |
| 611 const GetServiceOwnerCallback& callback); | |
| 612 | |
| 613 // Helper function used for UnListenForServiceOwnerChange(). | |
| 614 void UnlistenForServiceOwnerChangeInternal( | |
| 615 const std::string& service_name, | |
| 616 const GetServiceOwnerCallback& callback); | |
| 617 | |
| 595 // Processes the all incoming data to the connection, if any. | 618 // Processes the all incoming data to the connection, if any. |
| 596 // | 619 // |
| 597 // BLOCKING CALL. | 620 // BLOCKING CALL. |
| 598 void ProcessAllIncomingDataIfAny(); | 621 void ProcessAllIncomingDataIfAny(); |
| 599 | 622 |
| 600 // Called when a watch object is added. Used to start monitoring the | 623 // Called when a watch object is added. Used to start monitoring the |
| 601 // file descriptor used for D-Bus communication. | 624 // file descriptor used for D-Bus communication. |
| 602 dbus_bool_t OnAddWatch(DBusWatch* raw_watch); | 625 dbus_bool_t OnAddWatch(DBusWatch* raw_watch); |
| 603 | 626 |
| 604 // Called when a watch object is removed. | 627 // Called when a watch object is removed. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 618 void OnToggleTimeout(DBusTimeout* raw_timeout); | 641 void OnToggleTimeout(DBusTimeout* raw_timeout); |
| 619 | 642 |
| 620 // Called when the dispatch status (i.e. if any incoming data is | 643 // Called when the dispatch status (i.e. if any incoming data is |
| 621 // available) is changed. | 644 // available) is changed. |
| 622 void OnDispatchStatusChanged(DBusConnection* connection, | 645 void OnDispatchStatusChanged(DBusConnection* connection, |
| 623 DBusDispatchStatus status); | 646 DBusDispatchStatus status); |
| 624 | 647 |
| 625 // Called when the connection is diconnected. | 648 // Called when the connection is diconnected. |
| 626 void OnConnectionDisconnected(DBusConnection* connection); | 649 void OnConnectionDisconnected(DBusConnection* connection); |
| 627 | 650 |
| 651 // Called when a service owner change occurs. | |
| 652 DBusHandlerResult OnServiceOwnerChanged(DBusMessage* message); | |
| 653 | |
| 628 // Callback helper functions. Redirects to the corresponding member function. | 654 // Callback helper functions. Redirects to the corresponding member function. |
| 629 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data); | 655 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data); |
| 630 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data); | 656 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data); |
| 631 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data); | 657 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data); |
| 632 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 658 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
| 633 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 659 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
| 634 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 660 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
| 635 static void OnDispatchStatusChangedThunk(DBusConnection* connection, | 661 static void OnDispatchStatusChangedThunk(DBusConnection* connection, |
| 636 DBusDispatchStatus status, | 662 DBusDispatchStatus status, |
| 637 void* data); | 663 void* data); |
| 638 | 664 |
| 639 // Calls OnConnectionDisconnected if the Diconnected signal is received. | 665 // Calls OnConnectionDisconnected if the Disconnected signal is received. |
| 640 static DBusHandlerResult OnConnectionDisconnectedFilter( | 666 static DBusHandlerResult OnConnectionDisconnectedFilter( |
| 641 DBusConnection* connection, | 667 DBusConnection* connection, |
| 642 DBusMessage* message, | 668 DBusMessage* message, |
| 643 void* user_data); | 669 void* user_data); |
| 644 | 670 |
| 671 // Calls OnServiceOwnerChanged for a NameOwnerChanged signal. | |
| 672 static DBusHandlerResult OnServiceOwnerChangedFilter( | |
| 673 DBusConnection* connection, | |
| 674 DBusMessage* message, | |
| 675 void* user_data); | |
| 676 | |
| 645 const BusType bus_type_; | 677 const BusType bus_type_; |
| 646 const ConnectionType connection_type_; | 678 const ConnectionType connection_type_; |
| 647 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_; | 679 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_; |
| 648 base::WaitableEvent on_shutdown_; | 680 base::WaitableEvent on_shutdown_; |
| 649 DBusConnection* connection_; | 681 DBusConnection* connection_; |
| 650 | 682 |
| 651 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | 683 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
| 652 base::PlatformThreadId origin_thread_id_; | 684 base::PlatformThreadId origin_thread_id_; |
| 653 | 685 |
| 654 std::set<std::string> owned_service_names_; | 686 std::set<std::string> owned_service_names_; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 677 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable; | 709 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable; |
| 678 ExportedObjectTable exported_object_table_; | 710 ExportedObjectTable exported_object_table_; |
| 679 | 711 |
| 680 // ObjectManagerTable is used to hold the object managers created by the | 712 // ObjectManagerTable is used to hold the object managers created by the |
| 681 // bus object. Key is a concatenated string of service name + object path, | 713 // bus object. Key is a concatenated string of service name + object path, |
| 682 // like "org.chromium.TestService/org/chromium/TestObject". | 714 // like "org.chromium.TestService/org/chromium/TestObject". |
| 683 typedef std::map<std::string, | 715 typedef std::map<std::string, |
| 684 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable; | 716 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable; |
| 685 ObjectManagerTable object_manager_table_; | 717 ObjectManagerTable object_manager_table_; |
| 686 | 718 |
| 719 // A map of NameOwnerChanged signals to listen for and the callbacks to run | |
| 720 // on the origin thread when the owner changes. | |
| 721 // Only accessed on the DBus thread. | |
| 722 // Key: Service name | |
| 723 // Value: Vector of callbacks. Unique and expected to be small. Not using | |
| 724 // std::set here because base::Callbacks don't have a '<' operator. | |
| 725 typedef std::map<std::string, std::vector<GetServiceOwnerCallback> > | |
| 726 ServiceOwnerChangedListenerMap; | |
| 727 ServiceOwnerChangedListenerMap service_owner_changed_listener_map_; | |
| 728 | |
| 687 bool async_operations_set_up_; | 729 bool async_operations_set_up_; |
| 688 bool shutdown_completed_; | 730 bool shutdown_completed_; |
| 689 | 731 |
| 690 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and | 732 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and |
| 691 // OnAddTimeout()/OnRemoveTimeou() are balanced. | 733 // OnAddTimeout()/OnRemoveTimeou() are balanced. |
| 692 int num_pending_watches_; | 734 int num_pending_watches_; |
| 693 int num_pending_timeouts_; | 735 int num_pending_timeouts_; |
| 694 | 736 |
| 695 std::string address_; | 737 std::string address_; |
| 696 base::Closure on_disconnected_closure_; | 738 base::Closure on_disconnected_closure_; |
| 697 | 739 |
| 698 DISALLOW_COPY_AND_ASSIGN(Bus); | 740 DISALLOW_COPY_AND_ASSIGN(Bus); |
| 699 }; | 741 }; |
| 700 | 742 |
| 701 } // namespace dbus | 743 } // namespace dbus |
| 702 | 744 |
| 703 #endif // DBUS_BUS_H_ | 745 #endif // DBUS_BUS_H_ |
| OLD | NEW |