Chromium Code Reviews| 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/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "dbus/message.h" | 15 #include "dbus/message.h" |
| 16 #include "dbus/object_path.h" | 16 #include "dbus/object_path.h" |
| 17 #include "dbus/object_proxy.h" | 17 #include "dbus/object_proxy.h" |
| 18 #include "dbus/scoped_dbus_error.h" | 18 #include "dbus/scoped_dbus_error.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown"; | 22 const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown"; |
| 23 | 23 |
| 24 // Used for success ratio histograms. 1 for success, 0 for failure. | 24 // Used for success ratio histograms. 1 for success, 0 for failure. |
| 25 const int kSuccessRatioHistogramMaxValue = 2; | 25 const int kSuccessRatioHistogramMaxValue = 2; |
| 26 | 26 |
| 27 // The path of D-Bus Object sending NameOwnerChanged signal. | |
| 28 const char* kDbusSystemObjectPath = "/org/freedesktop/DBus"; | |
|
satorux1
2012/10/25 01:55:16
nit: please do:
const char kDbusSystemObjectPath[
Haruki Sato
2012/10/25 07:29:52
Done.
Thanks.
| |
| 29 | |
| 30 // Timeout in milliseconds used for GetNameOwner method call. | |
| 31 const static int kGetNameOwnerCallTimeoutMs = 1000; | |
|
satorux1
2012/10/25 01:55:16
remove static.
Haruki Sato
2012/10/25 07:29:52
Done.
Thanks.
| |
| 32 | |
| 27 // Gets the absolute signal name by concatenating the interface name and | 33 // Gets the absolute signal name by concatenating the interface name and |
| 28 // the signal name. Used for building keys for method_table_ in | 34 // the signal name. Used for building keys for method_table_ in |
| 29 // ObjectProxy. | 35 // ObjectProxy. |
| 30 std::string GetAbsoluteSignalName( | 36 std::string GetAbsoluteSignalName( |
| 31 const std::string& interface_name, | 37 const std::string& interface_name, |
| 32 const std::string& signal_name) { | 38 const std::string& signal_name) { |
| 33 return interface_name + "." + signal_name; | 39 return interface_name + "." + signal_name; |
| 34 } | 40 } |
| 35 | 41 |
| 36 // An empty function used for ObjectProxy::EmptyResponseCallback(). | 42 // An empty function used for ObjectProxy::EmptyResponseCallback(). |
| 37 void EmptyResponseCallbackBody(dbus::Response* unused_response) { | 43 void EmptyResponseCallbackBody(dbus::Response* unused_response) { |
| 38 } | 44 } |
| 39 | 45 |
| 46 dbus::Signal* CloneSignalMessage(dbus::Signal* orig) { | |
|
satorux1
2012/10/25 01:55:16
function comment is missing.
orig -> signal ?
Haruki Sato
2012/10/25 07:29:52
Done.
Thanks.
| |
| 47 DBusMessage* clone = dbus_message_copy(orig->raw_message()); | |
| 48 return dbus::Signal::FromRawMessage(clone); | |
| 49 } | |
| 50 | |
| 40 } // namespace | 51 } // namespace |
| 41 | 52 |
| 42 namespace dbus { | 53 namespace dbus { |
| 43 | 54 |
| 44 ObjectProxy::ObjectProxy(Bus* bus, | 55 ObjectProxy::ObjectProxy(Bus* bus, |
| 45 const std::string& service_name, | 56 const std::string& service_name, |
| 46 const ObjectPath& object_path, | 57 const ObjectPath& object_path, |
| 47 int options) | 58 int options) |
| 48 : bus_(bus), | 59 : bus_(bus), |
| 49 service_name_(service_name), | 60 service_name_(service_name), |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 filter_added_ = true; | 366 filter_added_ = true; |
| 356 } else { | 367 } else { |
| 357 LOG(ERROR) << "Failed to add filter function"; | 368 LOG(ERROR) << "Failed to add filter function"; |
| 358 } | 369 } |
| 359 } | 370 } |
| 360 // Add a match rule so the signal goes through HandleMessage(). | 371 // Add a match rule so the signal goes through HandleMessage(). |
| 361 const std::string match_rule = | 372 const std::string match_rule = |
| 362 base::StringPrintf("type='signal', interface='%s', path='%s'", | 373 base::StringPrintf("type='signal', interface='%s', path='%s'", |
| 363 interface_name.c_str(), | 374 interface_name.c_str(), |
| 364 object_path_.value().c_str()); | 375 object_path_.value().c_str()); |
| 365 | 376 // Add a match_rule listening NameOwnerChanged for the well-known name |
| 366 // Add the match rule if we don't have it. | 377 // |service_name_|. |
| 367 if (match_rules_.find(match_rule) == match_rules_.end()) { | 378 const std::string name_owner_changed_match_rule = |
| 368 ScopedDBusError error; | 379 base::StringPrintf( |
| 369 bus_->AddMatch(match_rule, error.get());; | 380 "type='signal',interface='org.freedesktop.DBus'," |
| 370 if (error.is_set()) { | 381 "member='NameOwnerChanged',path='/org/freedesktop/DBus'," |
| 371 LOG(ERROR) << "Failed to add match rule: " << match_rule; | 382 "sender='org.freedesktop.DBus',arg0='%s'", |
| 372 } else { | 383 service_name_.c_str()); |
| 373 // Store the match rule, so that we can remove this in Detach(). | 384 if (AddMatchRuleWithCallback(match_rule, |
| 374 match_rules_.insert(match_rule); | 385 absolute_signal_name, |
| 375 // Add the signal callback to the method table. | 386 signal_callback) && |
| 376 method_table_[absolute_signal_name] = signal_callback; | 387 AddMatchRuleWithoutCallback(name_owner_changed_match_rule, |
| 377 success = true; | 388 "org.freedesktop.DBus.NameOwnerChanged")) { |
| 378 } | |
| 379 } else { | |
| 380 // We already have the match rule. | |
| 381 method_table_[absolute_signal_name] = signal_callback; | |
| 382 success = true; | 389 success = true; |
| 383 } | 390 } |
| 391 | |
| 392 // Try getting the latest name owner. No pending signal. | |
|
satorux1
2012/10/25 01:55:16
What does "no pending signal" mean? Please describ
Haruki Sato
2012/10/25 07:29:52
Done.
| |
| 393 UpdateNameOwnerAsync(scoped_ptr<Signal>(NULL)); | |
| 384 } | 394 } |
| 385 | 395 |
| 386 // Run on_connected_callback in the origin thread. | 396 // Run on_connected_callback in the origin thread. |
| 387 bus_->PostTaskToOriginThread( | 397 bus_->PostTaskToOriginThread( |
| 388 FROM_HERE, | 398 FROM_HERE, |
| 389 base::Bind(&ObjectProxy::OnConnected, | 399 base::Bind(&ObjectProxy::OnConnected, |
| 390 this, | 400 this, |
| 391 on_connected_callback, | 401 on_connected_callback, |
| 392 interface_name, | 402 interface_name, |
| 393 signal_name, | 403 signal_name, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 414 // reference so we can use it in Signal. | 424 // reference so we can use it in Signal. |
| 415 dbus_message_ref(raw_message); | 425 dbus_message_ref(raw_message); |
| 416 scoped_ptr<Signal> signal( | 426 scoped_ptr<Signal> signal( |
| 417 Signal::FromRawMessage(raw_message)); | 427 Signal::FromRawMessage(raw_message)); |
| 418 | 428 |
| 419 // Verify the signal comes from the object we're proxying for, this is | 429 // Verify the signal comes from the object we're proxying for, this is |
| 420 // our last chance to return DBUS_HANDLER_RESULT_NOT_YET_HANDLED and | 430 // our last chance to return DBUS_HANDLER_RESULT_NOT_YET_HANDLED and |
| 421 // allow other object proxies to handle instead. | 431 // allow other object proxies to handle instead. |
| 422 const dbus::ObjectPath path = signal->GetPath(); | 432 const dbus::ObjectPath path = signal->GetPath(); |
| 423 if (path != object_path_) { | 433 if (path != object_path_) { |
| 434 if (path.value() == kDbusSystemObjectPath && | |
| 435 signal->GetMember() == "NameOwnerChanged") { | |
| 436 // Handle NameOwnerChanged separately | |
| 437 return HandleNameOwnerChanged(signal.get()); | |
| 438 } | |
| 424 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 439 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 425 } | 440 } |
| 426 | 441 |
| 442 // If the name owner is not known yet, get it before verifying the sender. | |
| 443 if (service_name_owner_.empty()) { | |
| 444 return UpdateNameOwnerAsync(signal.Pass()); | |
| 445 } | |
| 446 | |
| 447 // Verify the sender, check if we know about the signal, and invoke the | |
| 448 // callback if any. | |
| 449 return VerifySenderAndDispatch(signal.Pass()); | |
| 450 } | |
| 451 | |
| 452 DBusHandlerResult ObjectProxy::VerifySenderAndDispatch( | |
| 453 scoped_ptr<dbus::Signal> signal) { | |
| 454 DCHECK(signal); | |
| 455 | |
| 427 const std::string interface = signal->GetInterface(); | 456 const std::string interface = signal->GetInterface(); |
| 428 const std::string member = signal->GetMember(); | 457 const std::string member = signal->GetMember(); |
| 429 | 458 |
| 430 // Check if we know about the signal. | 459 // Check if we know about the signal. |
| 431 const std::string absolute_signal_name = GetAbsoluteSignalName( | 460 const std::string absolute_signal_name = GetAbsoluteSignalName( |
| 432 interface, member); | 461 interface, member); |
| 433 MethodTable::const_iterator iter = method_table_.find(absolute_signal_name); | 462 MethodTable::const_iterator iter = method_table_.find(absolute_signal_name); |
| 434 if (iter == method_table_.end()) { | 463 if (iter == method_table_.end()) { |
| 435 // Don't know about the signal. | 464 // Don't know about the signal. |
| 436 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 465 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 437 } | 466 } |
| 438 VLOG(1) << "Signal received: " << signal->ToString(); | 467 VLOG(1) << "Signal received: " << signal->ToString(); |
| 439 | 468 |
| 469 std::string sender = signal->GetSender(); | |
| 470 if (service_name_owner_ != sender) { | |
| 471 LOG(ERROR) << "Rejecting a message from a wrong sender."; | |
| 472 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1); | |
| 473 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
| 474 } | |
| 475 | |
| 440 const base::TimeTicks start_time = base::TimeTicks::Now(); | 476 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 441 if (bus_->HasDBusThread()) { | 477 if (bus_->HasDBusThread()) { |
| 442 // Post a task to run the method in the origin thread. | 478 // Post a task to run the method in the origin thread. |
| 443 // Transfer the ownership of |signal| to RunMethod(). | 479 // Transfer the ownership of |signal| to RunMethod(). |
| 444 // |released_signal| will be deleted in RunMethod(). | 480 // |released_signal| will be deleted in RunMethod(). |
| 445 Signal* released_signal = signal.release(); | 481 Signal* released_signal = signal.release(); |
| 446 bus_->PostTaskToOriginThread(FROM_HERE, | 482 bus_->PostTaskToOriginThread(FROM_HERE, |
| 447 base::Bind(&ObjectProxy::RunMethod, | 483 base::Bind(&ObjectProxy::RunMethod, |
| 448 this, | 484 this, |
| 449 start_time, | 485 start_time, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 508 std::string error_message; | 544 std::string error_message; |
| 509 reader.PopString(&error_message); | 545 reader.PopString(&error_message); |
| 510 LogMethodCallFailure(interface_name, | 546 LogMethodCallFailure(interface_name, |
| 511 method_name, | 547 method_name, |
| 512 error_response->GetErrorName(), | 548 error_response->GetErrorName(), |
| 513 error_message); | 549 error_message); |
| 514 } | 550 } |
| 515 response_callback.Run(NULL); | 551 response_callback.Run(NULL); |
| 516 } | 552 } |
| 517 | 553 |
| 554 bool ObjectProxy::AddMatchRuleWithCallback( | |
| 555 const std::string& match_rule, | |
| 556 const std::string& absolute_signal_name, | |
| 557 SignalCallback signal_callback) { | |
| 558 DCHECK(!match_rule.empty()); | |
| 559 DCHECK(!absolute_signal_name.empty()); | |
| 560 bus_->AssertOnDBusThread(); | |
| 561 | |
| 562 if (match_rules_.find(match_rule) == match_rules_.end()) { | |
| 563 ScopedDBusError error; | |
| 564 bus_->AddMatch(match_rule, error.get()); | |
| 565 if (error.is_set()) { | |
| 566 LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " << | |
| 567 error.name() << ": " << error.message(); | |
| 568 return false; | |
| 569 } else { | |
| 570 // Store the match rule, so that we can remove this in Detach(). | |
| 571 match_rules_.insert(match_rule); | |
| 572 // Add the signal callback to the method table. | |
| 573 method_table_[absolute_signal_name] = signal_callback; | |
| 574 return true; | |
| 575 } | |
| 576 } else { | |
| 577 // We already have the match rule. | |
| 578 method_table_[absolute_signal_name] = signal_callback; | |
| 579 return true; | |
| 580 } | |
| 581 } | |
| 582 | |
| 583 bool ObjectProxy::AddMatchRuleWithoutCallback( | |
| 584 const std::string& match_rule, | |
| 585 const std::string& absolute_signal_name) { | |
| 586 DCHECK(!match_rule.empty()); | |
| 587 DCHECK(!absolute_signal_name.empty()); | |
| 588 bus_->AssertOnDBusThread(); | |
| 589 | |
| 590 if (match_rules_.find(match_rule) != match_rules_.end()) | |
| 591 return true; | |
| 592 | |
| 593 ScopedDBusError error; | |
| 594 bus_->AddMatch(match_rule, error.get()); | |
| 595 if (error.is_set()) { | |
| 596 LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " << | |
| 597 error.name() << ": " << error.message(); | |
| 598 return false; | |
| 599 } | |
| 600 // Store the match rule, so that we can remove this in Detach(). | |
| 601 match_rules_.insert(match_rule); | |
| 602 return true; | |
| 603 } | |
| 604 | |
| 605 DBusHandlerResult ObjectProxy::UpdateNameOwnerAsync( | |
| 606 scoped_ptr<Signal> pending_signal) { | |
| 607 bus_->AssertOnDBusThread(); | |
|
satorux1
2012/10/25 01:55:16
please put a blank line here.
Haruki Sato
2012/10/25 07:29:52
Done.
Thanks.
| |
| 608 scoped_ptr<MethodCall> get_name_owner_call( | |
| 609 new MethodCall("org.freedesktop.DBus", "GetNameOwner")); | |
| 610 MessageWriter writer(get_name_owner_call.get()); | |
| 611 writer.AppendString(service_name_); | |
| 612 VLOG(1) << "Method call: " << get_name_owner_call->ToString(); | |
| 613 | |
| 614 const dbus::ObjectPath obj_path("/org/freedesktop/DBus"); | |
|
satorux1
2012/10/25 01:55:16
object_path in favor of less abbreviation.
Haruki Sato
2012/10/25 07:29:52
Done.
| |
| 615 ScopedDBusError error; | |
| 616 if (!get_name_owner_call->SetDestination("org.freedesktop.DBus") || | |
| 617 !get_name_owner_call->SetPath(obj_path)) { | |
| 618 LOG(ERROR) << "Failed to get name owner."; | |
| 619 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
| 620 } | |
| 621 | |
| 622 Signal* clone = NULL; | |
| 623 if (pending_signal) { | |
| 624 // This depends on the incoming reply to GetNameOwner method call. | |
| 625 // Copy the pending signal and delete the original in order to free | |
| 626 // libdbus's messaging queue and let it accept the next incoming message. | |
| 627 // The limitation of the queue size seems to be the one described in | |
| 628 // ObjectProxy::RunResponseCallback() | |
| 629 Signal* released_signal = pending_signal.release(); | |
| 630 clone = CloneSignalMessage(released_signal); | |
|
satorux1
2012/10/25 01:55:16
maybe:
clone = CloneSignalMessage(pending_signal
Haruki Sato
2012/10/25 07:29:52
Done.
Thanks!
| |
| 631 delete released_signal; | |
| 632 bus_->ProcessAllIncomingDataIfAny(); | |
|
satorux1
2012/10/25 01:55:16
why do we need to call this? We rarely need to cal
Haruki Sato
2012/10/25 07:29:52
Ah! I somehow added this call here. Confirmed that
| |
| 633 } | |
| 634 DBusMessage* request_message = get_name_owner_call->raw_message(); | |
| 635 dbus_message_ref(request_message); | |
| 636 | |
| 637 const base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 638 StartAsyncMethodCall( | |
| 639 kGetNameOwnerCallTimeoutMs, | |
| 640 request_message, | |
| 641 base::Bind(&ObjectProxy::OnGetNameOwner, this, clone), | |
| 642 base::Bind(&ObjectProxy::OnGetNameOwnerError, this, clone), | |
| 643 start_time); | |
| 644 return DBUS_HANDLER_RESULT_HANDLED; | |
| 645 } | |
| 646 | |
| 647 DBusHandlerResult ObjectProxy::HandleNameOwnerChanged(Signal* signal) { | |
| 648 DCHECK(signal); | |
| 649 bus_->AssertOnDBusThread(); | |
|
satorux1
2012/10/25 01:55:16
put a blank line here.
Haruki Sato
2012/10/25 07:29:52
Done.
| |
| 650 // Confirm the validity of the NameOwnerChanged signal. | |
| 651 if (signal->GetMember() == "NameOwnerChanged" && | |
| 652 signal->GetInterface() == "org.freedesktop.DBus" && | |
| 653 signal->GetSender() == "org.freedesktop.DBus") { | |
| 654 MessageReader reader(signal); | |
| 655 std::string name, old_owner, new_owner; | |
| 656 if (reader.PopString(&name) && | |
| 657 reader.PopString(&old_owner) && | |
| 658 reader.PopString(&new_owner) && | |
| 659 name == service_name_) { | |
| 660 service_name_owner_ = new_owner; | |
| 661 return DBUS_HANDLER_RESULT_HANDLED; | |
| 662 } | |
| 663 } | |
| 664 | |
| 665 // Untrusted or uninteresting signal | |
| 666 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
| 667 } | |
| 668 | |
| 669 void ObjectProxy::OnGetNameOwner(Signal* signal, Response* response) { | |
|
satorux1
2012/10/25 01:55:16
signal -> pending_signal?
DCHECK(pending_signal);
Haruki Sato
2012/10/25 07:29:52
Done.
| |
| 670 bus_->PostTaskToDBusThread( | |
| 671 FROM_HERE, | |
| 672 base::Bind(&ObjectProxy::OnGetNameOwnerOnDbusThread, | |
| 673 this, | |
| 674 signal, | |
| 675 response)); | |
| 676 } | |
| 677 | |
| 678 void ObjectProxy::OnGetNameOwnerOnDbusThread( | |
| 679 Signal* signal, Response* response) { | |
|
satorux1
2012/10/25 01:55:16
ditto.
Haruki Sato
2012/10/25 07:29:52
Done.
| |
| 680 bus_->AssertOnDBusThread(); | |
|
satorux1
2012/10/25 01:55:16
put a blank line here.
Haruki Sato
2012/10/25 07:29:52
Done.
| |
| 681 MessageReader reader(response); | |
| 682 if (!reader.PopString(&service_name_owner_)) { | |
| 683 LOG(ERROR) << "Can not interpret the result of GetNameOwner: " << | |
|
satorux1
2012/10/25 01:55:16
LOG(ERROR) << "Can not interpret the result of Get
Haruki Sato
2012/10/25 07:29:52
Done.
Thanks.
| |
| 684 response->ToString(); | |
| 685 } | |
| 686 | |
| 687 if (signal) | |
|
satorux1
2012/10/25 01:55:16
signal should always be non-null here?
Haruki Sato
2012/10/25 07:29:52
I am passing NULL as pending_signal in LINE 393,
w
| |
| 688 VerifySenderAndDispatch(scoped_ptr<Signal>(signal)); | |
| 689 } | |
| 690 | |
| 691 void ObjectProxy::OnGetNameOwnerError(Signal* signal, ErrorResponse* response) { | |
| 692 bus_->PostTaskToDBusThread( | |
| 693 FROM_HERE, | |
| 694 base::Bind(&ObjectProxy::OnGetNameOwnerErrorOnDbusThread, | |
| 695 this, | |
| 696 signal, | |
| 697 response)); | |
| 698 } | |
| 699 | |
| 700 void ObjectProxy::OnGetNameOwnerErrorOnDbusThread( | |
| 701 Signal* signal, ErrorResponse* response) { | |
| 702 bus_->AssertOnDBusThread(); | |
| 703 if (signal) | |
|
satorux1
2012/10/25 01:55:16
ditto.
| |
| 704 VerifySenderAndDispatch(scoped_ptr<Signal>(signal)); | |
| 705 } | |
| 706 | |
| 518 } // namespace dbus | 707 } // namespace dbus |
| OLD | NEW |