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 // TODO(satorux): | 5 // TODO(satorux): |
| 6 // - Handle "disconnected" signal. | 6 // - Handle "disconnected" signal. |
|
satorux1
2013/02/06 03:15:39
please remove this.
Seigo Nonaka
2013/02/06 04:57:22
Done.
| |
| 7 | 7 |
| 8 #include "dbus/bus.h" | 8 #include "dbus/bus.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/message_loop_proxy.h" | 13 #include "base/message_loop_proxy.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "dbus/exported_object.h" | 18 #include "dbus/exported_object.h" |
| 19 #include "dbus/object_path.h" | 19 #include "dbus/object_path.h" |
| 20 #include "dbus/object_proxy.h" | 20 #include "dbus/object_proxy.h" |
| 21 #include "dbus/scoped_dbus_error.h" | 21 #include "dbus/scoped_dbus_error.h" |
| 22 | 22 |
| 23 namespace dbus { | 23 namespace dbus { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kDisconnectedSignal[] = "Disconnected"; | |
| 28 const char kDisconnectedMatchRule[] = | |
| 29 "type='signal', path='/org/freedesktop/DBus/Local'," | |
| 30 "interface='org.freedesktop.DBus.Local', member='Disconnected'"; | |
| 31 | |
| 27 // The class is used for watching the file descriptor used for D-Bus | 32 // The class is used for watching the file descriptor used for D-Bus |
| 28 // communication. | 33 // communication. |
| 29 class Watch : public base::MessagePumpLibevent::Watcher { | 34 class Watch : public base::MessagePumpLibevent::Watcher { |
| 30 public: | 35 public: |
| 31 Watch(DBusWatch* watch) | 36 Watch(DBusWatch* watch) |
| 32 : raw_watch_(watch) { | 37 : raw_watch_(watch) { |
| 33 dbus_watch_set_data(raw_watch_, this, NULL); | 38 dbus_watch_set_data(raw_watch_, this, NULL); |
| 34 } | 39 } |
| 35 | 40 |
| 36 virtual ~Watch() { | 41 virtual ~Watch() { |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 // called internally. | 362 // called internally. |
| 358 if (!dbus_bus_register(connection_, error.get())) { | 363 if (!dbus_bus_register(connection_, error.get())) { |
| 359 LOG(ERROR) << "Failed to register the bus component: " | 364 LOG(ERROR) << "Failed to register the bus component: " |
| 360 << (error.is_set() ? error.message() : ""); | 365 << (error.is_set() ? error.message() : ""); |
| 361 return false; | 366 return false; |
| 362 } | 367 } |
| 363 } | 368 } |
| 364 // We shouldn't exit on the disconnected signal. | 369 // We shouldn't exit on the disconnected signal. |
| 365 dbus_connection_set_exit_on_disconnect(connection_, false); | 370 dbus_connection_set_exit_on_disconnect(connection_, false); |
| 366 | 371 |
| 372 // Watch Disconnected signal. | |
| 373 AddFilterFunction(Bus::OnConnectionDisconnectedFilter, this); | |
| 374 AddMatch(kDisconnectedMatchRule, error.get()); | |
| 375 | |
| 367 return true; | 376 return true; |
| 368 } | 377 } |
| 369 | 378 |
| 370 void Bus::ShutdownAndBlock() { | 379 void Bus::ShutdownAndBlock() { |
| 371 AssertOnDBusThread(); | 380 AssertOnDBusThread(); |
| 372 | 381 |
| 373 // Unregister the exported objects. | 382 // Unregister the exported objects. |
| 374 for (ExportedObjectTable::iterator iter = exported_object_table_.begin(); | 383 for (ExportedObjectTable::iterator iter = exported_object_table_.begin(); |
| 375 iter != exported_object_table_.end(); ++iter) { | 384 iter != exported_object_table_.end(); ++iter) { |
| 376 iter->second->Unregister(); | 385 iter->second->Unregister(); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 697 AssertOnDBusThread(); | 706 AssertOnDBusThread(); |
| 698 | 707 |
| 699 ShutdownAndBlock(); | 708 ShutdownAndBlock(); |
| 700 on_shutdown_.Signal(); | 709 on_shutdown_.Signal(); |
| 701 } | 710 } |
| 702 | 711 |
| 703 void Bus::ProcessAllIncomingDataIfAny() { | 712 void Bus::ProcessAllIncomingDataIfAny() { |
| 704 AssertOnDBusThread(); | 713 AssertOnDBusThread(); |
| 705 | 714 |
| 706 // As mentioned at the class comment in .h file, connection_ can be NULL. | 715 // As mentioned at the class comment in .h file, connection_ can be NULL. |
| 707 if (!connection_ || !dbus_connection_get_is_connected(connection_)) | 716 if (!connection_) |
| 708 return; | 717 return; |
| 709 | 718 |
| 719 // It is safe and necessary to call dbus_connection_get_dispatch_status even | |
| 720 // if the connection is lost. Otherwise we will miss "Disconnected" signal. | |
| 721 // (crbug.com/174431) | |
| 710 if (dbus_connection_get_dispatch_status(connection_) == | 722 if (dbus_connection_get_dispatch_status(connection_) == |
| 711 DBUS_DISPATCH_DATA_REMAINS) { | 723 DBUS_DISPATCH_DATA_REMAINS) { |
| 712 while (dbus_connection_dispatch(connection_) == | 724 while (dbus_connection_dispatch(connection_) == |
| 713 DBUS_DISPATCH_DATA_REMAINS); | 725 DBUS_DISPATCH_DATA_REMAINS); |
| 714 } | 726 } |
| 715 } | 727 } |
| 716 | 728 |
| 717 void Bus::PostTaskToOriginThread(const tracked_objects::Location& from_here, | 729 void Bus::PostTaskToOriginThread(const tracked_objects::Location& from_here, |
| 718 const base::Closure& task) { | 730 const base::Closure& task) { |
| 719 DCHECK(origin_message_loop_proxy_.get()); | 731 DCHECK(origin_message_loop_proxy_.get()); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 835 } else { | 847 } else { |
| 836 timeout->StopMonitoring(); | 848 timeout->StopMonitoring(); |
| 837 } | 849 } |
| 838 } | 850 } |
| 839 | 851 |
| 840 void Bus::OnDispatchStatusChanged(DBusConnection* connection, | 852 void Bus::OnDispatchStatusChanged(DBusConnection* connection, |
| 841 DBusDispatchStatus status) { | 853 DBusDispatchStatus status) { |
| 842 DCHECK_EQ(connection, connection_); | 854 DCHECK_EQ(connection, connection_); |
| 843 AssertOnDBusThread(); | 855 AssertOnDBusThread(); |
| 844 | 856 |
| 845 if (!dbus_connection_get_is_connected(connection)) | |
| 846 return; | |
| 847 | |
| 848 // We cannot call ProcessAllIncomingDataIfAny() here, as calling | 857 // We cannot call ProcessAllIncomingDataIfAny() here, as calling |
| 849 // dbus_connection_dispatch() inside DBusDispatchStatusFunction is | 858 // dbus_connection_dispatch() inside DBusDispatchStatusFunction is |
| 850 // prohibited by the D-Bus library. Hence, we post a task here instead. | 859 // prohibited by the D-Bus library. Hence, we post a task here instead. |
| 851 // See comments for dbus_connection_set_dispatch_status_function(). | 860 // See comments for dbus_connection_set_dispatch_status_function(). |
| 852 PostTaskToDBusThread(FROM_HERE, | 861 PostTaskToDBusThread(FROM_HERE, |
| 853 base::Bind(&Bus::ProcessAllIncomingDataIfAny, | 862 base::Bind(&Bus::ProcessAllIncomingDataIfAny, |
| 854 this)); | 863 this)); |
| 855 } | 864 } |
| 856 | 865 |
| 866 void Bus::OnConnectionDisconnected(DBusConnection* connection) { | |
|
satorux1
2013/02/06 03:15:39
on what thread this function is called? Please ann
Seigo Nonaka
2013/02/06 04:57:22
Done.
| |
| 867 if (!connection) | |
| 868 return; | |
| 869 | |
| 870 DCHECK_EQ(connection, connection_); | |
| 871 DCHECK(!dbus_connection_get_is_connected(connection)); | |
| 872 | |
| 873 ShutdownAndBlock(); | |
| 874 } | |
| 875 | |
| 857 dbus_bool_t Bus::OnAddWatchThunk(DBusWatch* raw_watch, void* data) { | 876 dbus_bool_t Bus::OnAddWatchThunk(DBusWatch* raw_watch, void* data) { |
| 858 Bus* self = static_cast<Bus*>(data); | 877 Bus* self = static_cast<Bus*>(data); |
| 859 return self->OnAddWatch(raw_watch); | 878 return self->OnAddWatch(raw_watch); |
| 860 } | 879 } |
| 861 | 880 |
| 862 void Bus::OnRemoveWatchThunk(DBusWatch* raw_watch, void* data) { | 881 void Bus::OnRemoveWatchThunk(DBusWatch* raw_watch, void* data) { |
| 863 Bus* self = static_cast<Bus*>(data); | 882 Bus* self = static_cast<Bus*>(data); |
| 864 self->OnRemoveWatch(raw_watch); | 883 self->OnRemoveWatch(raw_watch); |
| 865 } | 884 } |
| 866 | 885 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 884 self->OnToggleTimeout(raw_timeout); | 903 self->OnToggleTimeout(raw_timeout); |
| 885 } | 904 } |
| 886 | 905 |
| 887 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, | 906 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, |
| 888 DBusDispatchStatus status, | 907 DBusDispatchStatus status, |
| 889 void* data) { | 908 void* data) { |
| 890 Bus* self = static_cast<Bus*>(data); | 909 Bus* self = static_cast<Bus*>(data); |
| 891 self->OnDispatchStatusChanged(connection, status); | 910 self->OnDispatchStatusChanged(connection, status); |
| 892 } | 911 } |
| 893 | 912 |
| 913 DBusHandlerResult Bus::OnConnectionDisconnectedFilter( | |
| 914 DBusConnection *connection, | |
| 915 DBusMessage *message, | |
| 916 void *data) { | |
|
satorux1
2013/02/06 03:15:39
ditto. please annotate the thread.
Seigo Nonaka
2013/02/06 04:57:22
Done.
| |
| 917 if (dbus_message_is_signal(message, | |
| 918 DBUS_INTERFACE_LOCAL, | |
| 919 kDisconnectedSignal)) { | |
| 920 Bus* self = static_cast<Bus*>(data); | |
| 921 self->OnConnectionDisconnected(connection); | |
| 922 return DBUS_HANDLER_RESULT_HANDLED; | |
| 923 } | |
| 924 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
| 925 } | |
| 926 | |
| 894 } // namespace dbus | 927 } // namespace dbus |
| OLD | NEW |