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 #include "dbus/bus.h" | 5 #include "dbus/bus.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 : bus_type_(options.bus_type), | 182 : bus_type_(options.bus_type), |
183 connection_type_(options.connection_type), | 183 connection_type_(options.connection_type), |
184 dbus_thread_message_loop_proxy_(options.dbus_thread_message_loop_proxy), | 184 dbus_thread_message_loop_proxy_(options.dbus_thread_message_loop_proxy), |
185 on_shutdown_(false /* manual_reset */, false /* initially_signaled */), | 185 on_shutdown_(false /* manual_reset */, false /* initially_signaled */), |
186 connection_(NULL), | 186 connection_(NULL), |
187 origin_thread_id_(base::PlatformThread::CurrentId()), | 187 origin_thread_id_(base::PlatformThread::CurrentId()), |
188 async_operations_set_up_(false), | 188 async_operations_set_up_(false), |
189 shutdown_completed_(false), | 189 shutdown_completed_(false), |
190 num_pending_watches_(0), | 190 num_pending_watches_(0), |
191 num_pending_timeouts_(0), | 191 num_pending_timeouts_(0), |
192 address_(options.address) { | 192 address_(options.address), |
193 on_disconnected_closure_(options.disconnected_callback) { | |
193 // This is safe to call multiple times. | 194 // This is safe to call multiple times. |
194 dbus_threads_init_default(); | 195 dbus_threads_init_default(); |
195 // The origin message loop is unnecessary if the client uses synchronous | 196 // The origin message loop is unnecessary if the client uses synchronous |
196 // functions only. | 197 // functions only. |
197 if (MessageLoop::current()) | 198 if (MessageLoop::current()) |
198 origin_message_loop_proxy_ = MessageLoop::current()->message_loop_proxy(); | 199 origin_message_loop_proxy_ = MessageLoop::current()->message_loop_proxy(); |
199 } | 200 } |
200 | 201 |
201 Bus::~Bus() { | 202 Bus::~Bus() { |
202 DCHECK(!connection_); | 203 DCHECK(!connection_); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
366 // We shouldn't exit on the disconnected signal. | 367 // We shouldn't exit on the disconnected signal. |
367 dbus_connection_set_exit_on_disconnect(connection_, false); | 368 dbus_connection_set_exit_on_disconnect(connection_, false); |
368 | 369 |
369 // Watch Disconnected signal. | 370 // Watch Disconnected signal. |
370 AddFilterFunction(Bus::OnConnectionDisconnectedFilter, this); | 371 AddFilterFunction(Bus::OnConnectionDisconnectedFilter, this); |
371 AddMatch(kDisconnectedMatchRule, error.get()); | 372 AddMatch(kDisconnectedMatchRule, error.get()); |
372 | 373 |
373 return true; | 374 return true; |
374 } | 375 } |
375 | 376 |
377 void Bus::CloseConnection() { | |
378 // dbus_connection_close is blocking call. | |
379 AssertOnDBusThread(); | |
380 CHECK_EQ(PRIVATE, connection_type_) << "Do not close non private connection."; | |
satorux1
2013/02/15 04:25:12
Let's make it a DCHECK_EQ(). We rarely use CHECKs.
Seigo Nonaka
2013/02/15 04:36:10
Done.
| |
381 dbus_connection_close(connection_); | |
382 } | |
383 | |
376 void Bus::ShutdownAndBlock() { | 384 void Bus::ShutdownAndBlock() { |
377 AssertOnDBusThread(); | 385 AssertOnDBusThread(); |
378 | 386 |
379 if (shutdown_completed_) | 387 if (shutdown_completed_) |
380 return; // Already shutdowned, just return. | 388 return; // Already shutdowned, just return. |
381 | 389 |
382 // Unregister the exported objects. | 390 // Unregister the exported objects. |
383 for (ExportedObjectTable::iterator iter = exported_object_table_.begin(); | 391 for (ExportedObjectTable::iterator iter = exported_object_table_.begin(); |
384 iter != exported_object_table_.end(); ++iter) { | 392 iter != exported_object_table_.end(); ++iter) { |
385 iter->second->Unregister(); | 393 iter->second->Unregister(); |
(...skipping 25 matching lines...) Expand all Loading... | |
411 exported_object_table_.clear(); | 419 exported_object_table_.clear(); |
412 | 420 |
413 // Private connection should be closed. | 421 // Private connection should be closed. |
414 if (connection_) { | 422 if (connection_) { |
415 // Remove Disconnected watcher. | 423 // Remove Disconnected watcher. |
416 ScopedDBusError error; | 424 ScopedDBusError error; |
417 RemoveFilterFunction(Bus::OnConnectionDisconnectedFilter, this); | 425 RemoveFilterFunction(Bus::OnConnectionDisconnectedFilter, this); |
418 RemoveMatch(kDisconnectedMatchRule, error.get()); | 426 RemoveMatch(kDisconnectedMatchRule, error.get()); |
419 | 427 |
420 if (connection_type_ == PRIVATE) | 428 if (connection_type_ == PRIVATE) |
421 dbus_connection_close(connection_); | 429 CloseConnection(); |
422 // dbus_connection_close() won't unref. | 430 // dbus_connection_close() won't unref. |
423 dbus_connection_unref(connection_); | 431 dbus_connection_unref(connection_); |
424 } | 432 } |
425 | 433 |
426 connection_ = NULL; | 434 connection_ = NULL; |
427 shutdown_completed_ = true; | 435 shutdown_completed_ = true; |
428 } | 436 } |
429 | 437 |
430 void Bus::ShutdownOnDBusThreadAndBlock() { | 438 void Bus::ShutdownOnDBusThreadAndBlock() { |
431 AssertOnOriginThread(); | 439 AssertOnOriginThread(); |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
864 // prohibited by the D-Bus library. Hence, we post a task here instead. | 872 // prohibited by the D-Bus library. Hence, we post a task here instead. |
865 // See comments for dbus_connection_set_dispatch_status_function(). | 873 // See comments for dbus_connection_set_dispatch_status_function(). |
866 PostTaskToDBusThread(FROM_HERE, | 874 PostTaskToDBusThread(FROM_HERE, |
867 base::Bind(&Bus::ProcessAllIncomingDataIfAny, | 875 base::Bind(&Bus::ProcessAllIncomingDataIfAny, |
868 this)); | 876 this)); |
869 } | 877 } |
870 | 878 |
871 void Bus::OnConnectionDisconnected(DBusConnection* connection) { | 879 void Bus::OnConnectionDisconnected(DBusConnection* connection) { |
872 AssertOnDBusThread(); | 880 AssertOnDBusThread(); |
873 | 881 |
882 if (!on_disconnected_closure_.is_null()) | |
883 PostTaskToOriginThread(FROM_HERE, on_disconnected_closure_); | |
884 | |
874 if (!connection) | 885 if (!connection) |
875 return; | 886 return; |
876 DCHECK(!dbus_connection_get_is_connected(connection)); | 887 DCHECK(!dbus_connection_get_is_connected(connection)); |
877 | 888 |
878 ShutdownAndBlock(); | 889 ShutdownAndBlock(); |
879 } | 890 } |
880 | 891 |
881 dbus_bool_t Bus::OnAddWatchThunk(DBusWatch* raw_watch, void* data) { | 892 dbus_bool_t Bus::OnAddWatchThunk(DBusWatch* raw_watch, void* data) { |
882 Bus* self = static_cast<Bus*>(data); | 893 Bus* self = static_cast<Bus*>(data); |
883 return self->OnAddWatch(raw_watch); | 894 return self->OnAddWatch(raw_watch); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
924 kDisconnectedSignal)) { | 935 kDisconnectedSignal)) { |
925 Bus* self = static_cast<Bus*>(data); | 936 Bus* self = static_cast<Bus*>(data); |
926 self->AssertOnDBusThread(); | 937 self->AssertOnDBusThread(); |
927 self->OnConnectionDisconnected(connection); | 938 self->OnConnectionDisconnected(connection); |
928 return DBUS_HANDLER_RESULT_HANDLED; | 939 return DBUS_HANDLER_RESULT_HANDLED; |
929 } | 940 } |
930 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 941 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
931 } | 942 } |
932 | 943 |
933 } // namespace dbus | 944 } // namespace dbus |
OLD | NEW |