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/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 if (!method_call->SetDestination(service_name_) || | 133 if (!method_call->SetDestination(service_name_) || |
134 !method_call->SetPath(object_path_)) { | 134 !method_call->SetPath(object_path_)) { |
135 // In case of a failure, run the error callback with NULL. | 135 // In case of a failure, run the error callback with NULL. |
136 DBusMessage* response_message = NULL; | 136 DBusMessage* response_message = NULL; |
137 base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, | 137 base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, |
138 this, | 138 this, |
139 callback, | 139 callback, |
140 error_callback, | 140 error_callback, |
141 start_time, | 141 start_time, |
142 response_message); | 142 response_message); |
143 bus_->PostTaskToOriginThread(FROM_HERE, task); | 143 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); |
144 return; | 144 return; |
145 } | 145 } |
146 | 146 |
147 // Increment the reference count so we can safely reference the | 147 // Increment the reference count so we can safely reference the |
148 // underlying request message until the method call is complete. This | 148 // underlying request message until the method call is complete. This |
149 // will be unref'ed in StartAsyncMethodCall(). | 149 // will be unref'ed in StartAsyncMethodCall(). |
150 DBusMessage* request_message = method_call->raw_message(); | 150 DBusMessage* request_message = method_call->raw_message(); |
151 dbus_message_ref(request_message); | 151 dbus_message_ref(request_message); |
152 | 152 |
153 base::Closure task = base::Bind(&ObjectProxy::StartAsyncMethodCall, | 153 base::Closure task = base::Bind(&ObjectProxy::StartAsyncMethodCall, |
154 this, | 154 this, |
155 timeout_ms, | 155 timeout_ms, |
156 request_message, | 156 request_message, |
157 callback, | 157 callback, |
158 error_callback, | 158 error_callback, |
159 start_time); | 159 start_time); |
160 statistics::AddSentMethodCall(service_name_, | 160 statistics::AddSentMethodCall(service_name_, |
161 method_call->GetInterface(), | 161 method_call->GetInterface(), |
162 method_call->GetMember()); | 162 method_call->GetMember()); |
163 | 163 |
164 // Wait for the response in the D-Bus thread. | 164 // Wait for the response in the D-Bus thread. |
165 bus_->PostTaskToDBusThread(FROM_HERE, task); | 165 bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, task); |
166 } | 166 } |
167 | 167 |
168 void ObjectProxy::ConnectToSignal(const std::string& interface_name, | 168 void ObjectProxy::ConnectToSignal(const std::string& interface_name, |
169 const std::string& signal_name, | 169 const std::string& signal_name, |
170 SignalCallback signal_callback, | 170 SignalCallback signal_callback, |
171 OnConnectedCallback on_connected_callback) { | 171 OnConnectedCallback on_connected_callback) { |
172 bus_->AssertOnOriginThread(); | 172 bus_->AssertOnOriginThread(); |
173 | 173 |
174 bus_->PostTaskToDBusThread(FROM_HERE, | 174 bus_->GetDBusTaskRunner()->PostTask( |
175 base::Bind(&ObjectProxy::ConnectToSignalInternal, | 175 FROM_HERE, |
176 this, | 176 base::Bind(&ObjectProxy::ConnectToSignalInternal, |
177 interface_name, | 177 this, |
178 signal_name, | 178 interface_name, |
179 signal_callback, | 179 signal_name, |
180 on_connected_callback)); | 180 signal_callback, |
| 181 on_connected_callback)); |
181 } | 182 } |
182 | 183 |
183 void ObjectProxy::Detach() { | 184 void ObjectProxy::Detach() { |
184 bus_->AssertOnDBusThread(); | 185 bus_->AssertOnDBusThread(); |
185 | 186 |
186 if (filter_added_) { | 187 if (filter_added_) { |
187 if (!bus_->RemoveFilterFunction(&ObjectProxy::HandleMessageThunk, this)) { | 188 if (!bus_->RemoveFilterFunction(&ObjectProxy::HandleMessageThunk, this)) { |
188 LOG(ERROR) << "Failed to remove filter function"; | 189 LOG(ERROR) << "Failed to remove filter function"; |
189 } | 190 } |
190 } | 191 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 230 |
230 if (!bus_->Connect() || !bus_->SetUpAsyncOperations()) { | 231 if (!bus_->Connect() || !bus_->SetUpAsyncOperations()) { |
231 // In case of a failure, run the error callback with NULL. | 232 // In case of a failure, run the error callback with NULL. |
232 DBusMessage* response_message = NULL; | 233 DBusMessage* response_message = NULL; |
233 base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, | 234 base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, |
234 this, | 235 this, |
235 response_callback, | 236 response_callback, |
236 error_callback, | 237 error_callback, |
237 start_time, | 238 start_time, |
238 response_message); | 239 response_message); |
239 bus_->PostTaskToOriginThread(FROM_HERE, task); | 240 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); |
240 | 241 |
241 dbus_message_unref(request_message); | 242 dbus_message_unref(request_message); |
242 return; | 243 return; |
243 } | 244 } |
244 | 245 |
245 DBusPendingCall* pending_call = NULL; | 246 DBusPendingCall* pending_call = NULL; |
246 | 247 |
247 bus_->SendWithReply(request_message, &pending_call, timeout_ms); | 248 bus_->SendWithReply(request_message, &pending_call, timeout_ms); |
248 | 249 |
249 // Prepare the data we'll be passing to OnPendingCallIsCompleteThunk(). | 250 // Prepare the data we'll be passing to OnPendingCallIsCompleteThunk(). |
(...skipping 21 matching lines...) Expand all Loading... |
271 base::TimeTicks start_time) { | 272 base::TimeTicks start_time) { |
272 bus_->AssertOnDBusThread(); | 273 bus_->AssertOnDBusThread(); |
273 | 274 |
274 DBusMessage* response_message = dbus_pending_call_steal_reply(pending_call); | 275 DBusMessage* response_message = dbus_pending_call_steal_reply(pending_call); |
275 base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, | 276 base::Closure task = base::Bind(&ObjectProxy::RunResponseCallback, |
276 this, | 277 this, |
277 response_callback, | 278 response_callback, |
278 error_callback, | 279 error_callback, |
279 start_time, | 280 start_time, |
280 response_message); | 281 response_message); |
281 bus_->PostTaskToOriginThread(FROM_HERE, task); | 282 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); |
282 } | 283 } |
283 | 284 |
284 void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, | 285 void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, |
285 ErrorCallback error_callback, | 286 ErrorCallback error_callback, |
286 base::TimeTicks start_time, | 287 base::TimeTicks start_time, |
287 DBusMessage* response_message) { | 288 DBusMessage* response_message) { |
288 bus_->AssertOnOriginThread(); | 289 bus_->AssertOnOriginThread(); |
289 | 290 |
290 bool method_call_successful = false; | 291 bool method_call_successful = false; |
291 if (!response_message) { | 292 if (!response_message) { |
292 // The response is not received. | 293 // The response is not received. |
293 error_callback.Run(NULL); | 294 error_callback.Run(NULL); |
294 } else if (dbus_message_get_type(response_message) == | 295 } else if (dbus_message_get_type(response_message) == |
295 DBUS_MESSAGE_TYPE_ERROR) { | 296 DBUS_MESSAGE_TYPE_ERROR) { |
296 // This will take |response_message| and release (unref) it. | 297 // This will take |response_message| and release (unref) it. |
297 scoped_ptr<ErrorResponse> error_response( | 298 scoped_ptr<ErrorResponse> error_response( |
298 ErrorResponse::FromRawMessage(response_message)); | 299 ErrorResponse::FromRawMessage(response_message)); |
299 error_callback.Run(error_response.get()); | 300 error_callback.Run(error_response.get()); |
300 // Delete the message on the D-Bus thread. See below for why. | 301 // Delete the message on the D-Bus thread. See below for why. |
301 bus_->PostTaskToDBusThread( | 302 bus_->GetDBusTaskRunner()->PostTask( |
302 FROM_HERE, | 303 FROM_HERE, |
303 base::Bind(&base::DeletePointer<ErrorResponse>, | 304 base::Bind(&base::DeletePointer<ErrorResponse>, |
304 error_response.release())); | 305 error_response.release())); |
305 } else { | 306 } else { |
306 // This will take |response_message| and release (unref) it. | 307 // This will take |response_message| and release (unref) it. |
307 scoped_ptr<Response> response(Response::FromRawMessage(response_message)); | 308 scoped_ptr<Response> response(Response::FromRawMessage(response_message)); |
308 // The response is successfully received. | 309 // The response is successfully received. |
309 response_callback.Run(response.get()); | 310 response_callback.Run(response.get()); |
310 // The message should be deleted on the D-Bus thread for a complicated | 311 // The message should be deleted on the D-Bus thread for a complicated |
311 // reason: | 312 // reason: |
312 // | 313 // |
313 // libdbus keeps track of the number of bytes in the incoming message | 314 // libdbus keeps track of the number of bytes in the incoming message |
314 // queue to ensure that the data size in the queue is manageable. The | 315 // queue to ensure that the data size in the queue is manageable. The |
315 // bookkeeping is partly done via dbus_message_unref(), and immediately | 316 // bookkeeping is partly done via dbus_message_unref(), and immediately |
316 // asks the client code (Chrome) to stop monitoring the underlying | 317 // asks the client code (Chrome) to stop monitoring the underlying |
317 // socket, if the number of bytes exceeds a certian number, which is set | 318 // socket, if the number of bytes exceeds a certian number, which is set |
318 // to 63MB, per dbus-transport.cc: | 319 // to 63MB, per dbus-transport.cc: |
319 // | 320 // |
320 // /* Try to default to something that won't totally hose the system, | 321 // /* Try to default to something that won't totally hose the system, |
321 // * but doesn't impose too much of a limitation. | 322 // * but doesn't impose too much of a limitation. |
322 // */ | 323 // */ |
323 // transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63; | 324 // transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63; |
324 // | 325 // |
325 // The monitoring of the socket is done on the D-Bus thread (see Watch | 326 // The monitoring of the socket is done on the D-Bus thread (see Watch |
326 // class in bus.cc), hence we should stop the monitoring from D-Bus | 327 // class in bus.cc), hence we should stop the monitoring from D-Bus |
327 // thread, not from the current thread here, which is likely UI thread. | 328 // thread, not from the current thread here, which is likely UI thread. |
328 bus_->PostTaskToDBusThread( | 329 bus_->GetDBusTaskRunner()->PostTask( |
329 FROM_HERE, | 330 FROM_HERE, |
330 base::Bind(&base::DeletePointer<Response>, response.release())); | 331 base::Bind(&base::DeletePointer<Response>, response.release())); |
331 | 332 |
332 method_call_successful = true; | 333 method_call_successful = true; |
333 // Record time spent for the method call. Don't include failures. | 334 // Record time spent for the method call. Don't include failures. |
334 UMA_HISTOGRAM_TIMES("DBus.AsyncMethodCallTime", | 335 UMA_HISTOGRAM_TIMES("DBus.AsyncMethodCallTime", |
335 base::TimeTicks::Now() - start_time); | 336 base::TimeTicks::Now() - start_time); |
336 } | 337 } |
337 // Record if the method call is successful, or not. 1 if successful. | 338 // Record if the method call is successful, or not. 1 if successful. |
338 UMA_HISTOGRAM_ENUMERATION("DBus.AsyncMethodCallSuccess", | 339 UMA_HISTOGRAM_ENUMERATION("DBus.AsyncMethodCallSuccess", |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 } | 398 } |
398 | 399 |
399 // Try getting the current name owner. It's not guaranteed that we can get | 400 // Try getting the current name owner. It's not guaranteed that we can get |
400 // the name owner at this moment, as the service may not yet be started. If | 401 // the name owner at this moment, as the service may not yet be started. If |
401 // that's the case, we'll get the name owner via NameOwnerChanged signal, | 402 // that's the case, we'll get the name owner via NameOwnerChanged signal, |
402 // as soon as the service is started. | 403 // as soon as the service is started. |
403 UpdateNameOwnerAndBlock(); | 404 UpdateNameOwnerAndBlock(); |
404 } | 405 } |
405 | 406 |
406 // Run on_connected_callback in the origin thread. | 407 // Run on_connected_callback in the origin thread. |
407 bus_->PostTaskToOriginThread( | 408 bus_->GetOriginTaskRunner()->PostTask( |
408 FROM_HERE, | 409 FROM_HERE, |
409 base::Bind(&ObjectProxy::OnConnected, | 410 base::Bind(&ObjectProxy::OnConnected, |
410 this, | 411 this, |
411 on_connected_callback, | 412 on_connected_callback, |
412 interface_name, | 413 interface_name, |
413 signal_name, | 414 signal_name, |
414 success)); | 415 success)); |
415 } | 416 } |
416 | 417 |
417 void ObjectProxy::OnConnected(OnConnectedCallback on_connected_callback, | 418 void ObjectProxy::OnConnected(OnConnectedCallback on_connected_callback, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1); | 478 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1); |
478 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 479 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
479 } | 480 } |
480 | 481 |
481 const base::TimeTicks start_time = base::TimeTicks::Now(); | 482 const base::TimeTicks start_time = base::TimeTicks::Now(); |
482 if (bus_->HasDBusThread()) { | 483 if (bus_->HasDBusThread()) { |
483 // Post a task to run the method in the origin thread. | 484 // Post a task to run the method in the origin thread. |
484 // Transfer the ownership of |signal| to RunMethod(). | 485 // Transfer the ownership of |signal| to RunMethod(). |
485 // |released_signal| will be deleted in RunMethod(). | 486 // |released_signal| will be deleted in RunMethod(). |
486 Signal* released_signal = signal.release(); | 487 Signal* released_signal = signal.release(); |
487 bus_->PostTaskToOriginThread(FROM_HERE, | 488 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, |
488 base::Bind(&ObjectProxy::RunMethod, | 489 base::Bind(&ObjectProxy::RunMethod, |
489 this, | 490 this, |
490 start_time, | 491 start_time, |
491 iter->second, | 492 iter->second, |
492 released_signal)); | 493 released_signal)); |
493 } else { | 494 } else { |
494 const base::TimeTicks start_time = base::TimeTicks::Now(); | 495 const base::TimeTicks start_time = base::TimeTicks::Now(); |
495 // If the D-Bus thread is not used, just call the callback on the | 496 // If the D-Bus thread is not used, just call the callback on the |
496 // current thread. Transfer the ownership of |signal| to RunMethod(). | 497 // current thread. Transfer the ownership of |signal| to RunMethod(). |
497 Signal* released_signal = signal.release(); | 498 Signal* released_signal = signal.release(); |
498 RunMethod(start_time, iter->second, released_signal); | 499 RunMethod(start_time, iter->second, released_signal); |
499 } | 500 } |
500 | 501 |
501 return DBUS_HANDLER_RESULT_HANDLED; | 502 return DBUS_HANDLER_RESULT_HANDLED; |
502 } | 503 } |
503 | 504 |
504 void ObjectProxy::RunMethod(base::TimeTicks start_time, | 505 void ObjectProxy::RunMethod(base::TimeTicks start_time, |
505 std::vector<SignalCallback> signal_callbacks, | 506 std::vector<SignalCallback> signal_callbacks, |
506 Signal* signal) { | 507 Signal* signal) { |
507 bus_->AssertOnOriginThread(); | 508 bus_->AssertOnOriginThread(); |
508 | 509 |
509 for (std::vector<SignalCallback>::iterator iter = signal_callbacks.begin(); | 510 for (std::vector<SignalCallback>::iterator iter = signal_callbacks.begin(); |
510 iter != signal_callbacks.end(); ++iter) | 511 iter != signal_callbacks.end(); ++iter) |
511 iter->Run(signal); | 512 iter->Run(signal); |
512 | 513 |
513 // Delete the message on the D-Bus thread. See comments in | 514 // Delete the message on the D-Bus thread. See comments in |
514 // RunResponseCallback(). | 515 // RunResponseCallback(). |
515 bus_->PostTaskToDBusThread( | 516 bus_->GetDBusTaskRunner()->PostTask( |
516 FROM_HERE, | 517 FROM_HERE, |
517 base::Bind(&base::DeletePointer<Signal>, signal)); | 518 base::Bind(&base::DeletePointer<Signal>, signal)); |
518 | 519 |
519 // Record time spent for handling the signal. | 520 // Record time spent for handling the signal. |
520 UMA_HISTOGRAM_TIMES("DBus.SignalHandleTime", | 521 UMA_HISTOGRAM_TIMES("DBus.SignalHandleTime", |
521 base::TimeTicks::Now() - start_time); | 522 base::TimeTicks::Now() - start_time); |
522 } | 523 } |
523 | 524 |
524 DBusHandlerResult ObjectProxy::HandleMessageThunk( | 525 DBusHandlerResult ObjectProxy::HandleMessageThunk( |
525 DBusConnection* connection, | 526 DBusConnection* connection, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 if (reader.PopString(&name) && | 635 if (reader.PopString(&name) && |
635 reader.PopString(&old_owner) && | 636 reader.PopString(&old_owner) && |
636 reader.PopString(&new_owner) && | 637 reader.PopString(&new_owner) && |
637 name == service_name_) { | 638 name == service_name_) { |
638 service_name_owner_ = new_owner; | 639 service_name_owner_ = new_owner; |
639 if (!name_owner_changed_callback_.is_null()) { | 640 if (!name_owner_changed_callback_.is_null()) { |
640 const base::TimeTicks start_time = base::TimeTicks::Now(); | 641 const base::TimeTicks start_time = base::TimeTicks::Now(); |
641 Signal* released_signal = signal.release(); | 642 Signal* released_signal = signal.release(); |
642 std::vector<SignalCallback> callbacks; | 643 std::vector<SignalCallback> callbacks; |
643 callbacks.push_back(name_owner_changed_callback_); | 644 callbacks.push_back(name_owner_changed_callback_); |
644 bus_->PostTaskToOriginThread(FROM_HERE, | 645 bus_->GetOriginTaskRunner()->PostTask( |
645 base::Bind(&ObjectProxy::RunMethod, | 646 FROM_HERE, |
646 this, | 647 base::Bind(&ObjectProxy::RunMethod, |
647 start_time, | 648 this, |
648 callbacks, | 649 start_time, |
649 released_signal)); | 650 callbacks, |
| 651 released_signal)); |
650 } | 652 } |
651 } | 653 } |
652 } | 654 } |
653 | 655 |
654 // Always return unhandled to let other object proxies handle the same | 656 // Always return unhandled to let other object proxies handle the same |
655 // signal. | 657 // signal. |
656 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 658 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
657 } | 659 } |
658 | 660 |
659 } // namespace dbus | 661 } // namespace dbus |
OLD | NEW |