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::Shutdown(OnShutdownCallback callback) { |
312 AssertOnOriginThread(); | 317 AssertOnOriginThread(); |
313 | 318 |
314 PostTaskToDBusThread(FROM_HERE, base::Bind(&Bus::ShutdownInternal, | 319 PostTaskToDBusThread(FROM_HERE, base::Bind(&Bus::ShutdownInternal, |
315 this, | 320 this, |
316 callback)); | 321 callback)); |
317 } | 322 } |
318 | 323 |
| 324 void Bus::ShutdownAndBlockWithDBusThread() { |
| 325 AssertOnOriginThread(); |
| 326 DCHECK(dbus_thread_); |
| 327 |
| 328 PostTaskToDBusThread(FROM_HERE, base::Bind( |
| 329 &Bus::ShutdownAndBlockWithDBusThreadInternal, |
| 330 this)); |
| 331 |
| 332 // Wait until the shutdown is complete on the D-Bus thread. |
| 333 // The shutdown should not hang, but set timeout just in case. |
| 334 const int kTimeoutSecs = 3; |
| 335 const base::TimeDelta timeout(base::TimeDelta::FromSeconds(kTimeoutSecs)); |
| 336 const bool signaled = on_shutdown_.TimedWait(timeout); |
| 337 LOG_IF(ERROR, !signaled) << "Failed to shutdown the bus"; |
| 338 } |
| 339 |
319 bool Bus::RequestOwnership(const std::string& service_name) { | 340 bool Bus::RequestOwnership(const std::string& service_name) { |
320 DCHECK(connection_); | 341 DCHECK(connection_); |
321 // dbus_bus_request_name() is a blocking call. | 342 // dbus_bus_request_name() is a blocking call. |
322 AssertOnDBusThread(); | 343 AssertOnDBusThread(); |
323 | 344 |
324 // Check if we already own the service name. | 345 // Check if we already own the service name. |
325 if (owned_service_names_.find(service_name) != owned_service_names_.end()) { | 346 if (owned_service_names_.find(service_name) != owned_service_names_.end()) { |
326 return true; | 347 return true; |
327 } | 348 } |
328 | 349 |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 registered_object_paths_.erase(object_path); | 556 registered_object_paths_.erase(object_path); |
536 } | 557 } |
537 | 558 |
538 void Bus::ShutdownInternal(OnShutdownCallback callback) { | 559 void Bus::ShutdownInternal(OnShutdownCallback callback) { |
539 AssertOnDBusThread(); | 560 AssertOnDBusThread(); |
540 | 561 |
541 ShutdownAndBlock(); | 562 ShutdownAndBlock(); |
542 PostTaskToOriginThread(FROM_HERE, callback); | 563 PostTaskToOriginThread(FROM_HERE, callback); |
543 } | 564 } |
544 | 565 |
| 566 void Bus::ShutdownAndBlockWithDBusThreadInternal() { |
| 567 AssertOnDBusThread(); |
| 568 |
| 569 ShutdownAndBlock(); |
| 570 on_shutdown_.Signal(); |
| 571 } |
| 572 |
545 void Bus::ProcessAllIncomingDataIfAny() { | 573 void Bus::ProcessAllIncomingDataIfAny() { |
546 AssertOnDBusThread(); | 574 AssertOnDBusThread(); |
547 | 575 |
548 // As mentioned at the class comment in .h file, connection_ can be NULL. | 576 // As mentioned at the class comment in .h file, connection_ can be NULL. |
549 if (!connection_ || !dbus_connection_get_is_connected(connection_)) | 577 if (!connection_ || !dbus_connection_get_is_connected(connection_)) |
550 return; | 578 return; |
551 | 579 |
552 if (dbus_connection_get_dispatch_status(connection_) == | 580 if (dbus_connection_get_dispatch_status(connection_) == |
553 DBUS_DISPATCH_DATA_REMAINS) { | 581 DBUS_DISPATCH_DATA_REMAINS) { |
554 while (dbus_connection_dispatch(connection_) == | 582 while (dbus_connection_dispatch(connection_) == |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 } | 738 } |
711 | 739 |
712 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, | 740 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, |
713 DBusDispatchStatus status, | 741 DBusDispatchStatus status, |
714 void* data) { | 742 void* data) { |
715 Bus* self = static_cast<Bus*>(data); | 743 Bus* self = static_cast<Bus*>(data); |
716 return self->OnDispatchStatusChanged(connection, status); | 744 return self->OnDispatchStatusChanged(connection, status); |
717 } | 745 } |
718 | 746 |
719 } // namespace dbus | 747 } // namespace dbus |
OLD | NEW |