OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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. |
7 // - Collect metrics (ex. # of method calls, method call time, etc.) | 7 // - Collect metrics (ex. # of method calls, method call time, etc.) |
8 | 8 |
9 #include "dbus/bus.h" | 9 #include "dbus/bus.h" |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.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 "dbus/exported_object.h" | 18 #include "dbus/exported_object.h" |
18 #include "dbus/object_proxy.h" | 19 #include "dbus/object_proxy.h" |
19 #include "dbus/scoped_dbus_error.h" | 20 #include "dbus/scoped_dbus_error.h" |
20 | 21 |
21 namespace dbus { | 22 namespace dbus { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // The class is used for watching the file descriptor used for D-Bus | 26 // The class is used for watching the file descriptor used for D-Bus |
26 // communication. | 27 // communication. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 dbus_thread(NULL) { | 172 dbus_thread(NULL) { |
172 } | 173 } |
173 | 174 |
174 Bus::Options::~Options() { | 175 Bus::Options::~Options() { |
175 } | 176 } |
176 | 177 |
177 Bus::Bus(const Options& options) | 178 Bus::Bus(const Options& options) |
178 : bus_type_(options.bus_type), | 179 : bus_type_(options.bus_type), |
179 connection_type_(options.connection_type), | 180 connection_type_(options.connection_type), |
180 dbus_thread_(options.dbus_thread), | 181 dbus_thread_(options.dbus_thread), |
| 182 on_shutdown_(false /* manual_reset */, false /* initially_signaled */), |
181 connection_(NULL), | 183 connection_(NULL), |
182 origin_loop_(MessageLoop::current()), | 184 origin_loop_(MessageLoop::current()), |
183 origin_thread_id_(base::PlatformThread::CurrentId()), | 185 origin_thread_id_(base::PlatformThread::CurrentId()), |
184 dbus_thread_id_(base::kInvalidThreadId), | 186 dbus_thread_id_(base::kInvalidThreadId), |
185 async_operations_set_up_(false), | 187 async_operations_set_up_(false), |
| 188 shutdown_completed_(false), |
186 num_pending_watches_(0), | 189 num_pending_watches_(0), |
187 num_pending_timeouts_(0) { | 190 num_pending_timeouts_(0) { |
188 if (dbus_thread_) { | 191 if (dbus_thread_) { |
189 dbus_thread_id_ = dbus_thread_->thread_id(); | 192 dbus_thread_id_ = dbus_thread_->thread_id(); |
190 DCHECK(dbus_thread_->IsRunning()) | 193 DCHECK(dbus_thread_->IsRunning()) |
191 << "The D-Bus thread should be running"; | 194 << "The D-Bus thread should be running"; |
192 DCHECK_EQ(MessageLoop::TYPE_IO, | 195 DCHECK_EQ(MessageLoop::TYPE_IO, |
193 dbus_thread_->message_loop()->type()) | 196 dbus_thread_->message_loop()->type()) |
194 << "The D-Bus thread should have an MessageLoopForIO attached"; | 197 << "The D-Bus thread should have an MessageLoopForIO attached"; |
195 } | 198 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 << owned_service_names_.size(); | 295 << owned_service_names_.size(); |
293 } | 296 } |
294 | 297 |
295 // Detach from the remote objects. | 298 // Detach from the remote objects. |
296 for (ObjectProxyTable::iterator iter = object_proxy_table_.begin(); | 299 for (ObjectProxyTable::iterator iter = object_proxy_table_.begin(); |
297 iter != object_proxy_table_.end(); ++iter) { | 300 iter != object_proxy_table_.end(); ++iter) { |
298 iter->second->Detach(); | 301 iter->second->Detach(); |
299 } | 302 } |
300 | 303 |
301 // Private connection should be closed. | 304 // Private connection should be closed. |
302 if (connection_ && connection_type_ == PRIVATE) { | 305 if (connection_) { |
303 dbus_connection_close(connection_); | 306 if (connection_type_ == PRIVATE) |
| 307 dbus_connection_close(connection_); |
| 308 // dbus_connection_close() won't unref. |
| 309 dbus_connection_unref(connection_); |
304 } | 310 } |
305 // dbus_connection_close() won't unref. | |
306 dbus_connection_unref(connection_); | |
307 | 311 |
308 connection_ = NULL; | 312 connection_ = NULL; |
| 313 shutdown_completed_ = true; |
309 } | 314 } |
310 | 315 |
311 void Bus::Shutdown(OnShutdownCallback callback) { | 316 void Bus::ShutdownOnDBusThreadAndBlock() { |
312 AssertOnOriginThread(); | 317 AssertOnOriginThread(); |
| 318 DCHECK(dbus_thread_); |
313 | 319 |
314 PostTaskToDBusThread(FROM_HERE, base::Bind(&Bus::ShutdownInternal, | 320 PostTaskToDBusThread(FROM_HERE, base::Bind( |
315 this, | 321 &Bus::ShutdownOnDBusThreadAndBlockInternal, |
316 callback)); | 322 this)); |
| 323 |
| 324 // Wait until the shutdown is complete on the D-Bus thread. |
| 325 // The shutdown should not hang, but set timeout just in case. |
| 326 const int kTimeoutSecs = 3; |
| 327 const base::TimeDelta timeout(base::TimeDelta::FromSeconds(kTimeoutSecs)); |
| 328 const bool signaled = on_shutdown_.TimedWait(timeout); |
| 329 LOG_IF(ERROR, !signaled) << "Failed to shutdown the bus"; |
317 } | 330 } |
318 | 331 |
319 bool Bus::RequestOwnership(const std::string& service_name) { | 332 bool Bus::RequestOwnership(const std::string& service_name) { |
320 DCHECK(connection_); | 333 DCHECK(connection_); |
321 // dbus_bus_request_name() is a blocking call. | 334 // dbus_bus_request_name() is a blocking call. |
322 AssertOnDBusThread(); | 335 AssertOnDBusThread(); |
323 | 336 |
324 // Check if we already own the service name. | 337 // Check if we already own the service name. |
325 if (owned_service_names_.find(service_name) != owned_service_names_.end()) { | 338 if (owned_service_names_.find(service_name) != owned_service_names_.end()) { |
326 return true; | 339 return true; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 return; | 541 return; |
529 } | 542 } |
530 | 543 |
531 const bool success = dbus_connection_unregister_object_path( | 544 const bool success = dbus_connection_unregister_object_path( |
532 connection_, | 545 connection_, |
533 object_path.c_str()); | 546 object_path.c_str()); |
534 CHECK(success) << "Unable to allocate memory"; | 547 CHECK(success) << "Unable to allocate memory"; |
535 registered_object_paths_.erase(object_path); | 548 registered_object_paths_.erase(object_path); |
536 } | 549 } |
537 | 550 |
538 void Bus::ShutdownInternal(OnShutdownCallback callback) { | 551 void Bus::ShutdownOnDBusThreadAndBlockInternal() { |
539 AssertOnDBusThread(); | 552 AssertOnDBusThread(); |
540 | 553 |
541 ShutdownAndBlock(); | 554 ShutdownAndBlock(); |
542 PostTaskToOriginThread(FROM_HERE, callback); | 555 on_shutdown_.Signal(); |
543 } | 556 } |
544 | 557 |
545 void Bus::ProcessAllIncomingDataIfAny() { | 558 void Bus::ProcessAllIncomingDataIfAny() { |
546 AssertOnDBusThread(); | 559 AssertOnDBusThread(); |
547 | 560 |
548 // As mentioned at the class comment in .h file, connection_ can be NULL. | 561 // As mentioned at the class comment in .h file, connection_ can be NULL. |
549 if (!connection_ || !dbus_connection_get_is_connected(connection_)) | 562 if (!connection_ || !dbus_connection_get_is_connected(connection_)) |
550 return; | 563 return; |
551 | 564 |
552 if (dbus_connection_get_dispatch_status(connection_) == | 565 if (dbus_connection_get_dispatch_status(connection_) == |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 } | 723 } |
711 | 724 |
712 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, | 725 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, |
713 DBusDispatchStatus status, | 726 DBusDispatchStatus status, |
714 void* data) { | 727 void* data) { |
715 Bus* self = static_cast<Bus*>(data); | 728 Bus* self = static_cast<Bus*>(data); |
716 return self->OnDispatchStatusChanged(connection, status); | 729 return self->OnDispatchStatusChanged(connection, status); |
717 } | 730 } |
718 | 731 |
719 } // namespace dbus | 732 } // namespace dbus |
OLD | NEW |