Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(900)

Side by Side Diff: dbus/bus.cc

Issue 12211022: Call get_dispatch_status function to handle Disconnected signal. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressing comments Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dbus/bus.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 //
5 // TODO(satorux):
6 // - Handle "disconnected" signal.
7 4
8 #include "dbus/bus.h" 5 #include "dbus/bus.h"
9 6
10 #include "base/bind.h" 7 #include "base/bind.h"
11 #include "base/logging.h" 8 #include "base/logging.h"
12 #include "base/message_loop.h" 9 #include "base/message_loop.h"
13 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
14 #include "base/stl_util.h" 11 #include "base/stl_util.h"
15 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
16 #include "base/threading/thread_restrictions.h" 13 #include "base/threading/thread_restrictions.h"
17 #include "base/time.h" 14 #include "base/time.h"
18 #include "dbus/exported_object.h" 15 #include "dbus/exported_object.h"
19 #include "dbus/object_path.h" 16 #include "dbus/object_path.h"
20 #include "dbus/object_proxy.h" 17 #include "dbus/object_proxy.h"
21 #include "dbus/scoped_dbus_error.h" 18 #include "dbus/scoped_dbus_error.h"
22 19
23 namespace dbus { 20 namespace dbus {
24 21
25 namespace { 22 namespace {
26 23
24 const char kDisconnectedSignal[] = "Disconnected";
25 const char kDisconnectedMatchRule[] =
26 "type='signal', path='/org/freedesktop/DBus/Local',"
27 "interface='org.freedesktop.DBus.Local', member='Disconnected'";
28
27 // The class is used for watching the file descriptor used for D-Bus 29 // The class is used for watching the file descriptor used for D-Bus
28 // communication. 30 // communication.
29 class Watch : public base::MessagePumpLibevent::Watcher { 31 class Watch : public base::MessagePumpLibevent::Watcher {
30 public: 32 public:
31 Watch(DBusWatch* watch) 33 Watch(DBusWatch* watch)
32 : raw_watch_(watch) { 34 : raw_watch_(watch) {
33 dbus_watch_set_data(raw_watch_, this, NULL); 35 dbus_watch_set_data(raw_watch_, this, NULL);
34 } 36 }
35 37
36 virtual ~Watch() { 38 virtual ~Watch() {
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 // called internally. 359 // called internally.
358 if (!dbus_bus_register(connection_, error.get())) { 360 if (!dbus_bus_register(connection_, error.get())) {
359 LOG(ERROR) << "Failed to register the bus component: " 361 LOG(ERROR) << "Failed to register the bus component: "
360 << (error.is_set() ? error.message() : ""); 362 << (error.is_set() ? error.message() : "");
361 return false; 363 return false;
362 } 364 }
363 } 365 }
364 // We shouldn't exit on the disconnected signal. 366 // We shouldn't exit on the disconnected signal.
365 dbus_connection_set_exit_on_disconnect(connection_, false); 367 dbus_connection_set_exit_on_disconnect(connection_, false);
366 368
369 // Watch Disconnected signal.
370 AddFilterFunction(Bus::OnConnectionDisconnectedFilter, this);
371 AddMatch(kDisconnectedMatchRule, error.get());
372
367 return true; 373 return true;
368 } 374 }
369 375
370 void Bus::ShutdownAndBlock() { 376 void Bus::ShutdownAndBlock() {
371 AssertOnDBusThread(); 377 AssertOnDBusThread();
372 378
379 if (shutdown_completed_)
380 return; // Already shutdowned, just return.
satorux1 2013/02/08 13:39:06 Do we need this? Looks unnecessary.
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();
377 } 386 }
378 387
379 // Release all service names. 388 // Release all service names.
380 for (std::set<std::string>::iterator iter = owned_service_names_.begin(); 389 for (std::set<std::string>::iterator iter = owned_service_names_.begin();
381 iter != owned_service_names_.end();) { 390 iter != owned_service_names_.end();) {
382 // This is a bit tricky but we should increment the iter here as 391 // This is a bit tricky but we should increment the iter here as
(...skipping 13 matching lines...) Expand all
396 } 405 }
397 406
398 // Release object proxies and exported objects here. We should do this 407 // Release object proxies and exported objects here. We should do this
399 // here rather than in the destructor to avoid memory leaks due to 408 // here rather than in the destructor to avoid memory leaks due to
400 // cyclic references. 409 // cyclic references.
401 object_proxy_table_.clear(); 410 object_proxy_table_.clear();
402 exported_object_table_.clear(); 411 exported_object_table_.clear();
403 412
404 // Private connection should be closed. 413 // Private connection should be closed.
405 if (connection_) { 414 if (connection_) {
415 // Remove Disconnected watcher.
416 ScopedDBusError error;
417 RemoveFilterFunction(Bus::OnConnectionDisconnectedFilter, this);
418 RemoveMatch(kDisconnectedMatchRule, error.get());
419
406 if (connection_type_ == PRIVATE) 420 if (connection_type_ == PRIVATE)
407 dbus_connection_close(connection_); 421 dbus_connection_close(connection_);
408 // dbus_connection_close() won't unref. 422 // dbus_connection_close() won't unref.
409 dbus_connection_unref(connection_); 423 dbus_connection_unref(connection_);
410 } 424 }
411 425
412 connection_ = NULL; 426 connection_ = NULL;
413 shutdown_completed_ = true; 427 shutdown_completed_ = true;
414 } 428 }
415 429
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 AssertOnDBusThread(); 711 AssertOnDBusThread();
698 712
699 ShutdownAndBlock(); 713 ShutdownAndBlock();
700 on_shutdown_.Signal(); 714 on_shutdown_.Signal();
701 } 715 }
702 716
703 void Bus::ProcessAllIncomingDataIfAny() { 717 void Bus::ProcessAllIncomingDataIfAny() {
704 AssertOnDBusThread(); 718 AssertOnDBusThread();
705 719
706 // As mentioned at the class comment in .h file, connection_ can be NULL. 720 // As mentioned at the class comment in .h file, connection_ can be NULL.
707 if (!connection_ || !dbus_connection_get_is_connected(connection_)) 721 if (!connection_)
708 return; 722 return;
709 723
724 // It is safe and necessary to call dbus_connection_get_dispatch_status even
725 // if the connection is lost. Otherwise we will miss "Disconnected" signal.
726 // (crbug.com/174431)
710 if (dbus_connection_get_dispatch_status(connection_) == 727 if (dbus_connection_get_dispatch_status(connection_) ==
711 DBUS_DISPATCH_DATA_REMAINS) { 728 DBUS_DISPATCH_DATA_REMAINS) {
712 while (dbus_connection_dispatch(connection_) == 729 while (dbus_connection_dispatch(connection_) ==
713 DBUS_DISPATCH_DATA_REMAINS); 730 DBUS_DISPATCH_DATA_REMAINS);
714 } 731 }
715 } 732 }
716 733
717 void Bus::PostTaskToOriginThread(const tracked_objects::Location& from_here, 734 void Bus::PostTaskToOriginThread(const tracked_objects::Location& from_here,
718 const base::Closure& task) { 735 const base::Closure& task) {
719 DCHECK(origin_message_loop_proxy_.get()); 736 DCHECK(origin_message_loop_proxy_.get());
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 } else { 852 } else {
836 timeout->StopMonitoring(); 853 timeout->StopMonitoring();
837 } 854 }
838 } 855 }
839 856
840 void Bus::OnDispatchStatusChanged(DBusConnection* connection, 857 void Bus::OnDispatchStatusChanged(DBusConnection* connection,
841 DBusDispatchStatus status) { 858 DBusDispatchStatus status) {
842 DCHECK_EQ(connection, connection_); 859 DCHECK_EQ(connection, connection_);
843 AssertOnDBusThread(); 860 AssertOnDBusThread();
844 861
845 if (!dbus_connection_get_is_connected(connection))
846 return;
847
848 // We cannot call ProcessAllIncomingDataIfAny() here, as calling 862 // We cannot call ProcessAllIncomingDataIfAny() here, as calling
849 // dbus_connection_dispatch() inside DBusDispatchStatusFunction is 863 // dbus_connection_dispatch() inside DBusDispatchStatusFunction is
850 // prohibited by the D-Bus library. Hence, we post a task here instead. 864 // prohibited by the D-Bus library. Hence, we post a task here instead.
851 // See comments for dbus_connection_set_dispatch_status_function(). 865 // See comments for dbus_connection_set_dispatch_status_function().
852 PostTaskToDBusThread(FROM_HERE, 866 PostTaskToDBusThread(FROM_HERE,
853 base::Bind(&Bus::ProcessAllIncomingDataIfAny, 867 base::Bind(&Bus::ProcessAllIncomingDataIfAny,
854 this)); 868 this));
855 } 869 }
856 870
871 void Bus::OnConnectionDisconnected(DBusConnection* connection) {
872 AssertOnDBusThread();
873
874 if (!connection)
875 return;
876 DCHECK(!dbus_connection_get_is_connected(connection));
877
878 if (shutdown_completed_)
879 return; // Do nothing if the shutdown is already completed.
880
881 // Unexpected disconnection, maybe the peer closes the connection.
882 DCHECK_EQ(connection, connection_);
883 ShutdownAndBlock();
884 }
885
857 dbus_bool_t Bus::OnAddWatchThunk(DBusWatch* raw_watch, void* data) { 886 dbus_bool_t Bus::OnAddWatchThunk(DBusWatch* raw_watch, void* data) {
858 Bus* self = static_cast<Bus*>(data); 887 Bus* self = static_cast<Bus*>(data);
859 return self->OnAddWatch(raw_watch); 888 return self->OnAddWatch(raw_watch);
860 } 889 }
861 890
862 void Bus::OnRemoveWatchThunk(DBusWatch* raw_watch, void* data) { 891 void Bus::OnRemoveWatchThunk(DBusWatch* raw_watch, void* data) {
863 Bus* self = static_cast<Bus*>(data); 892 Bus* self = static_cast<Bus*>(data);
864 self->OnRemoveWatch(raw_watch); 893 self->OnRemoveWatch(raw_watch);
865 } 894 }
866 895
(...skipping 17 matching lines...) Expand all
884 self->OnToggleTimeout(raw_timeout); 913 self->OnToggleTimeout(raw_timeout);
885 } 914 }
886 915
887 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, 916 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection,
888 DBusDispatchStatus status, 917 DBusDispatchStatus status,
889 void* data) { 918 void* data) {
890 Bus* self = static_cast<Bus*>(data); 919 Bus* self = static_cast<Bus*>(data);
891 self->OnDispatchStatusChanged(connection, status); 920 self->OnDispatchStatusChanged(connection, status);
892 } 921 }
893 922
923 DBusHandlerResult Bus::OnConnectionDisconnectedFilter(
924 DBusConnection *connection,
925 DBusMessage *message,
926 void *data) {
927 if (dbus_message_is_signal(message,
928 DBUS_INTERFACE_LOCAL,
929 kDisconnectedSignal)) {
930 Bus* self = static_cast<Bus*>(data);
931 self->AssertOnDBusThread();
932 self->OnConnectionDisconnected(connection);
933 return DBUS_HANDLER_RESULT_HANDLED;
934 }
935 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
936 }
937
894 } // namespace dbus 938 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/bus.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698