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. If the owner goes away, then |callback| receives |
| 568 // an empty string. |
| 569 // |
| 570 // Any unique (service_name, callback) can be used. Duplicate are ignored. |
| 571 // |service_name| must not be empty and |callback| must not be null. |
| 572 // |
| 573 // Must be called in the origin thread. |
| 574 virtual void ListenForServiceOwnerChange( |
| 575 const std::string& service_name, |
| 576 const GetServiceOwnerCallback& callback); |
| 577 |
| 578 // Stop listening for |service_name| owner changes for |callback|. |
| 579 // Any unique (service_name, callback) can be used. Non-registered callbacks |
| 580 // for a given service name are ignored. |
| 581 // |service_name| must not be empty and |callback| must not be null. |
| 582 // |
| 583 // Must be called in the origin thread. |
| 584 virtual void UnlistenForServiceOwnerChange( |
| 585 const std::string& service_name, |
| 586 const GetServiceOwnerCallback& callback); |
| 587 |
566 // Returns true if the bus is connected to D-Bus. | 588 // Returns true if the bus is connected to D-Bus. |
567 bool is_connected() { return connection_ != NULL; } | 589 bool is_connected() { return connection_ != NULL; } |
568 | 590 |
569 protected: | 591 protected: |
570 // This is protected, so we can define sub classes. | 592 // This is protected, so we can define sub classes. |
571 virtual ~Bus(); | 593 virtual ~Bus(); |
572 | 594 |
573 private: | 595 private: |
574 friend class base::RefCountedThreadSafe<Bus>; | 596 friend class base::RefCountedThreadSafe<Bus>; |
575 | 597 |
576 // Helper function used for RemoveObjectProxy(). | 598 // Helper function used for RemoveObjectProxy(). |
577 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy, | 599 void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy, |
578 const base::Closure& callback); | 600 const base::Closure& callback); |
579 | 601 |
580 // Helper function used for UnregisterExportedObject(). | 602 // Helper function used for UnregisterExportedObject(). |
581 void UnregisterExportedObjectInternal( | 603 void UnregisterExportedObjectInternal( |
582 scoped_refptr<dbus::ExportedObject> exported_object); | 604 scoped_refptr<dbus::ExportedObject> exported_object); |
583 | 605 |
584 // Helper function used for ShutdownOnDBusThreadAndBlock(). | 606 // Helper function used for ShutdownOnDBusThreadAndBlock(). |
585 void ShutdownOnDBusThreadAndBlockInternal(); | 607 void ShutdownOnDBusThreadAndBlockInternal(); |
586 | 608 |
587 // Helper function used for RequestOwnership(). | 609 // Helper function used for RequestOwnership(). |
588 void RequestOwnershipInternal(const std::string& service_name, | 610 void RequestOwnershipInternal(const std::string& service_name, |
589 OnOwnershipCallback on_ownership_callback); | 611 OnOwnershipCallback on_ownership_callback); |
590 | 612 |
591 // Helper function used for GetServiceOwner(). | 613 // Helper function used for GetServiceOwner(). |
592 void GetServiceOwnerInternal(const std::string& service_name, | 614 void GetServiceOwnerInternal(const std::string& service_name, |
593 const GetServiceOwnerCallback& callback); | 615 const GetServiceOwnerCallback& callback); |
594 | 616 |
| 617 // Helper function used for ListenForServiceOwnerChange(). |
| 618 void ListenForServiceOwnerChangeInternal( |
| 619 const std::string& service_name, |
| 620 const GetServiceOwnerCallback& callback); |
| 621 |
| 622 // Helper function used for UnListenForServiceOwnerChange(). |
| 623 void UnlistenForServiceOwnerChangeInternal( |
| 624 const std::string& service_name, |
| 625 const GetServiceOwnerCallback& callback); |
| 626 |
595 // Processes the all incoming data to the connection, if any. | 627 // Processes the all incoming data to the connection, if any. |
596 // | 628 // |
597 // BLOCKING CALL. | 629 // BLOCKING CALL. |
598 void ProcessAllIncomingDataIfAny(); | 630 void ProcessAllIncomingDataIfAny(); |
599 | 631 |
600 // Called when a watch object is added. Used to start monitoring the | 632 // Called when a watch object is added. Used to start monitoring the |
601 // file descriptor used for D-Bus communication. | 633 // file descriptor used for D-Bus communication. |
602 dbus_bool_t OnAddWatch(DBusWatch* raw_watch); | 634 dbus_bool_t OnAddWatch(DBusWatch* raw_watch); |
603 | 635 |
604 // Called when a watch object is removed. | 636 // Called when a watch object is removed. |
(...skipping 13 matching lines...) Expand all Loading... |
618 void OnToggleTimeout(DBusTimeout* raw_timeout); | 650 void OnToggleTimeout(DBusTimeout* raw_timeout); |
619 | 651 |
620 // Called when the dispatch status (i.e. if any incoming data is | 652 // Called when the dispatch status (i.e. if any incoming data is |
621 // available) is changed. | 653 // available) is changed. |
622 void OnDispatchStatusChanged(DBusConnection* connection, | 654 void OnDispatchStatusChanged(DBusConnection* connection, |
623 DBusDispatchStatus status); | 655 DBusDispatchStatus status); |
624 | 656 |
625 // Called when the connection is diconnected. | 657 // Called when the connection is diconnected. |
626 void OnConnectionDisconnected(DBusConnection* connection); | 658 void OnConnectionDisconnected(DBusConnection* connection); |
627 | 659 |
| 660 // Called when a service owner change occurs. |
| 661 void OnServiceOwnerChanged(DBusMessage* message); |
| 662 |
628 // Callback helper functions. Redirects to the corresponding member function. | 663 // Callback helper functions. Redirects to the corresponding member function. |
629 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data); | 664 static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data); |
630 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data); | 665 static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data); |
631 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data); | 666 static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data); |
632 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 667 static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
633 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 668 static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
634 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data); | 669 static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data); |
635 static void OnDispatchStatusChangedThunk(DBusConnection* connection, | 670 static void OnDispatchStatusChangedThunk(DBusConnection* connection, |
636 DBusDispatchStatus status, | 671 DBusDispatchStatus status, |
637 void* data); | 672 void* data); |
638 | 673 |
639 // Calls OnConnectionDisconnected if the Diconnected signal is received. | 674 // Calls OnConnectionDisconnected if the Disconnected signal is received. |
640 static DBusHandlerResult OnConnectionDisconnectedFilter( | 675 static DBusHandlerResult OnConnectionDisconnectedFilter( |
641 DBusConnection* connection, | 676 DBusConnection* connection, |
642 DBusMessage* message, | 677 DBusMessage* message, |
643 void* user_data); | 678 void* user_data); |
644 | 679 |
| 680 // Calls OnServiceOwnerChanged for a NameOwnerChanged signal. |
| 681 static DBusHandlerResult OnServiceOwnerChangedFilter( |
| 682 DBusConnection* connection, |
| 683 DBusMessage* message, |
| 684 void* user_data); |
| 685 |
645 const BusType bus_type_; | 686 const BusType bus_type_; |
646 const ConnectionType connection_type_; | 687 const ConnectionType connection_type_; |
647 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_; | 688 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_; |
648 base::WaitableEvent on_shutdown_; | 689 base::WaitableEvent on_shutdown_; |
649 DBusConnection* connection_; | 690 DBusConnection* connection_; |
650 | 691 |
651 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | 692 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
652 base::PlatformThreadId origin_thread_id_; | 693 base::PlatformThreadId origin_thread_id_; |
653 | 694 |
654 std::set<std::string> owned_service_names_; | 695 std::set<std::string> owned_service_names_; |
(...skipping 22 matching lines...) Expand all Loading... |
677 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable; | 718 scoped_refptr<dbus::ExportedObject> > ExportedObjectTable; |
678 ExportedObjectTable exported_object_table_; | 719 ExportedObjectTable exported_object_table_; |
679 | 720 |
680 // ObjectManagerTable is used to hold the object managers created by the | 721 // ObjectManagerTable is used to hold the object managers created by the |
681 // bus object. Key is a concatenated string of service name + object path, | 722 // bus object. Key is a concatenated string of service name + object path, |
682 // like "org.chromium.TestService/org/chromium/TestObject". | 723 // like "org.chromium.TestService/org/chromium/TestObject". |
683 typedef std::map<std::string, | 724 typedef std::map<std::string, |
684 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable; | 725 scoped_refptr<dbus::ObjectManager> > ObjectManagerTable; |
685 ObjectManagerTable object_manager_table_; | 726 ObjectManagerTable object_manager_table_; |
686 | 727 |
| 728 // A map of NameOwnerChanged signals to listen for and the callbacks to run |
| 729 // on the origin thread when the owner changes. |
| 730 // Only accessed on the DBus thread. |
| 731 // Key: Service name |
| 732 // Value: Vector of callbacks. Unique and expected to be small. Not using |
| 733 // std::set here because base::Callbacks don't have a '<' operator. |
| 734 typedef std::map<std::string, std::vector<GetServiceOwnerCallback> > |
| 735 ServiceOwnerChangedListenerMap; |
| 736 ServiceOwnerChangedListenerMap service_owner_changed_listener_map_; |
| 737 |
687 bool async_operations_set_up_; | 738 bool async_operations_set_up_; |
688 bool shutdown_completed_; | 739 bool shutdown_completed_; |
689 | 740 |
690 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and | 741 // Counters to make sure that OnAddWatch()/OnRemoveWatch() and |
691 // OnAddTimeout()/OnRemoveTimeou() are balanced. | 742 // OnAddTimeout()/OnRemoveTimeou() are balanced. |
692 int num_pending_watches_; | 743 int num_pending_watches_; |
693 int num_pending_timeouts_; | 744 int num_pending_timeouts_; |
694 | 745 |
695 std::string address_; | 746 std::string address_; |
696 base::Closure on_disconnected_closure_; | 747 base::Closure on_disconnected_closure_; |
697 | 748 |
698 DISALLOW_COPY_AND_ASSIGN(Bus); | 749 DISALLOW_COPY_AND_ASSIGN(Bus); |
699 }; | 750 }; |
700 | 751 |
701 } // namespace dbus | 752 } // namespace dbus |
702 | 753 |
703 #endif // DBUS_BUS_H_ | 754 #endif // DBUS_BUS_H_ |
OLD | NEW |