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"; | |
29 | |
30 // Timeout in milliseconds used for GetNameOwner method call. | |
31 const int kGetNameOwnerCallTimeoutMs = 1000; | |
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(). |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 filter_added_ = true; | 361 filter_added_ = true; |
356 } else { | 362 } else { |
357 LOG(ERROR) << "Failed to add filter function"; | 363 LOG(ERROR) << "Failed to add filter function"; |
358 } | 364 } |
359 } | 365 } |
360 // Add a match rule so the signal goes through HandleMessage(). | 366 // Add a match rule so the signal goes through HandleMessage(). |
361 const std::string match_rule = | 367 const std::string match_rule = |
362 base::StringPrintf("type='signal', interface='%s', path='%s'", | 368 base::StringPrintf("type='signal', interface='%s', path='%s'", |
363 interface_name.c_str(), | 369 interface_name.c_str(), |
364 object_path_.value().c_str()); | 370 object_path_.value().c_str()); |
365 | 371 // Add a match_rule listening NameOwnerChanged for the well-known name |
366 // Add the match rule if we don't have it. | 372 // |service_name_|. |
367 if (match_rules_.find(match_rule) == match_rules_.end()) { | 373 const std::string name_owner_changed_match_rule = |
368 ScopedDBusError error; | 374 base::StringPrintf( |
369 bus_->AddMatch(match_rule, error.get());; | 375 "type='signal',interface='org.freedesktop.DBus'," |
370 if (error.is_set()) { | 376 "member='NameOwnerChanged',path='/org/freedesktop/DBus'," |
371 LOG(ERROR) << "Failed to add match rule: " << match_rule; | 377 "sender='org.freedesktop.DBus',arg0='%s'", |
372 } else { | 378 service_name_.c_str()); |
373 // Store the match rule, so that we can remove this in Detach(). | 379 if (AddMatchRuleWithCallback(match_rule, |
374 match_rules_.insert(match_rule); | 380 absolute_signal_name, |
375 // Add the signal callback to the method table. | 381 signal_callback) && |
376 method_table_[absolute_signal_name] = signal_callback; | 382 AddMatchRuleWithoutCallback(name_owner_changed_match_rule, |
377 success = true; | 383 "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; | 384 success = true; |
383 } | 385 } |
386 | |
387 // Try getting the current name owner. It's not guaranteed that we can get | |
388 // the name owner at this moment, as the service may not yet be started. If | |
389 // that's the case, we'll get the name owner via NameOwnerChanged signal, | |
390 // as soon as the service is started. | |
391 UpdateNameOwnerAndBlock(); | |
384 } | 392 } |
385 | 393 |
386 // Run on_connected_callback in the origin thread. | 394 // Run on_connected_callback in the origin thread. |
387 bus_->PostTaskToOriginThread( | 395 bus_->PostTaskToOriginThread( |
388 FROM_HERE, | 396 FROM_HERE, |
389 base::Bind(&ObjectProxy::OnConnected, | 397 base::Bind(&ObjectProxy::OnConnected, |
390 this, | 398 this, |
391 on_connected_callback, | 399 on_connected_callback, |
392 interface_name, | 400 interface_name, |
393 signal_name, | 401 signal_name, |
394 success)); | 402 success)); |
395 } | 403 } |
396 | 404 |
397 void ObjectProxy::OnConnected(OnConnectedCallback on_connected_callback, | 405 void ObjectProxy::OnConnected(OnConnectedCallback on_connected_callback, |
398 const std::string& interface_name, | 406 const std::string& interface_name, |
399 const std::string& signal_name, | 407 const std::string& signal_name, |
400 bool success) { | 408 bool success) { |
401 bus_->AssertOnOriginThread(); | 409 bus_->AssertOnOriginThread(); |
402 | 410 |
403 on_connected_callback.Run(interface_name, signal_name, success); | 411 on_connected_callback.Run(interface_name, signal_name, success); |
404 } | 412 } |
405 | 413 |
406 DBusHandlerResult ObjectProxy::HandleMessage( | 414 DBusHandlerResult ObjectProxy::HandleMessage( |
407 DBusConnection* connection, | 415 DBusConnection* connection, |
408 DBusMessage* raw_message) { | 416 DBusMessage* raw_message) { |
409 bus_->AssertOnDBusThread(); | 417 bus_->AssertOnDBusThread(); |
418 | |
410 if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL) | 419 if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL) |
411 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 420 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
412 | 421 |
413 // raw_message will be unrefed on exit of the function. Increment the | 422 // raw_message will be unrefed on exit of the function. Increment the |
414 // reference so we can use it in Signal. | 423 // reference so we can use it in Signal. |
415 dbus_message_ref(raw_message); | 424 dbus_message_ref(raw_message); |
416 scoped_ptr<Signal> signal( | 425 scoped_ptr<Signal> signal( |
417 Signal::FromRawMessage(raw_message)); | 426 Signal::FromRawMessage(raw_message)); |
418 | 427 |
419 // Verify the signal comes from the object we're proxying for, this is | 428 // 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 | 429 // our last chance to return DBUS_HANDLER_RESULT_NOT_YET_HANDLED and |
421 // allow other object proxies to handle instead. | 430 // allow other object proxies to handle instead. |
422 const dbus::ObjectPath path = signal->GetPath(); | 431 const dbus::ObjectPath path = signal->GetPath(); |
423 if (path != object_path_) { | 432 if (path != object_path_) { |
433 if (path.value() == kDbusSystemObjectPath && | |
434 signal->GetMember() == "NameOwnerChanged") { | |
435 // Handle NameOwnerChanged separately | |
436 return HandleNameOwnerChanged(signal.get()); | |
437 } | |
424 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 438 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
425 } | 439 } |
426 | 440 |
427 const std::string interface = signal->GetInterface(); | 441 const std::string interface = signal->GetInterface(); |
428 const std::string member = signal->GetMember(); | 442 const std::string member = signal->GetMember(); |
429 | 443 |
430 // Check if we know about the signal. | 444 // Check if we know about the signal. |
431 const std::string absolute_signal_name = GetAbsoluteSignalName( | 445 const std::string absolute_signal_name = GetAbsoluteSignalName( |
432 interface, member); | 446 interface, member); |
433 MethodTable::const_iterator iter = method_table_.find(absolute_signal_name); | 447 MethodTable::const_iterator iter = method_table_.find(absolute_signal_name); |
434 if (iter == method_table_.end()) { | 448 if (iter == method_table_.end()) { |
435 // Don't know about the signal. | 449 // Don't know about the signal. |
436 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 450 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
437 } | 451 } |
438 VLOG(1) << "Signal received: " << signal->ToString(); | 452 VLOG(1) << "Signal received: " << signal->ToString(); |
439 | 453 |
454 std::string sender = signal->GetSender(); | |
455 if (service_name_owner_ != sender) { | |
456 LOG(ERROR) << "Rejecting a message from a wrong sender."; | |
457 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1); | |
458 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
459 } | |
460 | |
440 const base::TimeTicks start_time = base::TimeTicks::Now(); | 461 const base::TimeTicks start_time = base::TimeTicks::Now(); |
441 if (bus_->HasDBusThread()) { | 462 if (bus_->HasDBusThread()) { |
442 // Post a task to run the method in the origin thread. | 463 // Post a task to run the method in the origin thread. |
443 // Transfer the ownership of |signal| to RunMethod(). | 464 // Transfer the ownership of |signal| to RunMethod(). |
444 // |released_signal| will be deleted in RunMethod(). | 465 // |released_signal| will be deleted in RunMethod(). |
445 Signal* released_signal = signal.release(); | 466 Signal* released_signal = signal.release(); |
446 bus_->PostTaskToOriginThread(FROM_HERE, | 467 bus_->PostTaskToOriginThread(FROM_HERE, |
447 base::Bind(&ObjectProxy::RunMethod, | 468 base::Bind(&ObjectProxy::RunMethod, |
448 this, | 469 this, |
449 start_time, | 470 start_time, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
508 std::string error_message; | 529 std::string error_message; |
509 reader.PopString(&error_message); | 530 reader.PopString(&error_message); |
510 LogMethodCallFailure(interface_name, | 531 LogMethodCallFailure(interface_name, |
511 method_name, | 532 method_name, |
512 error_response->GetErrorName(), | 533 error_response->GetErrorName(), |
513 error_message); | 534 error_message); |
514 } | 535 } |
515 response_callback.Run(NULL); | 536 response_callback.Run(NULL); |
516 } | 537 } |
517 | 538 |
539 bool ObjectProxy::AddMatchRuleWithCallback( | |
540 const std::string& match_rule, | |
541 const std::string& absolute_signal_name, | |
542 SignalCallback signal_callback) { | |
543 DCHECK(!match_rule.empty()); | |
544 DCHECK(!absolute_signal_name.empty()); | |
545 bus_->AssertOnDBusThread(); | |
546 | |
547 if (match_rules_.find(match_rule) == match_rules_.end()) { | |
548 ScopedDBusError error; | |
549 bus_->AddMatch(match_rule, error.get()); | |
550 if (error.is_set()) { | |
551 LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " << | |
552 error.name() << ": " << error.message(); | |
553 return false; | |
554 } else { | |
555 // Store the match rule, so that we can remove this in Detach(). | |
556 match_rules_.insert(match_rule); | |
557 // Add the signal callback to the method table. | |
558 method_table_[absolute_signal_name] = signal_callback; | |
559 return true; | |
560 } | |
561 } else { | |
562 // We already have the match rule. | |
563 method_table_[absolute_signal_name] = signal_callback; | |
564 return true; | |
565 } | |
566 } | |
567 | |
568 bool ObjectProxy::AddMatchRuleWithoutCallback( | |
569 const std::string& match_rule, | |
570 const std::string& absolute_signal_name) { | |
571 DCHECK(!match_rule.empty()); | |
572 DCHECK(!absolute_signal_name.empty()); | |
573 bus_->AssertOnDBusThread(); | |
574 | |
575 if (match_rules_.find(match_rule) != match_rules_.end()) | |
576 return true; | |
577 | |
578 ScopedDBusError error; | |
579 bus_->AddMatch(match_rule, error.get()); | |
580 if (error.is_set()) { | |
581 LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " << | |
582 error.name() << ": " << error.message(); | |
583 return false; | |
584 } | |
585 // Store the match rule, so that we can remove this in Detach(). | |
586 match_rules_.insert(match_rule); | |
587 return true; | |
588 } | |
589 | |
590 void ObjectProxy::UpdateNameOwnerAndBlock() { | |
591 bus_->AssertOnDBusThread(); | |
592 | |
593 MethodCall get_name_owner_call("org.freedesktop.DBus", "GetNameOwner"); | |
594 MessageWriter writer(&get_name_owner_call); | |
595 writer.AppendString(service_name_); | |
596 VLOG(1) << "Method call: " << get_name_owner_call.ToString(); | |
597 | |
598 const dbus::ObjectPath obj_path("/org/freedesktop/DBus"); | |
599 ScopedDBusError error; | |
600 if (!get_name_owner_call.SetDestination("org.freedesktop.DBus") || | |
601 !get_name_owner_call.SetPath(obj_path)) { | |
602 LOG(ERROR) << "Failed to get name owner."; | |
603 return; | |
604 } | |
605 | |
606 DBusMessage* response_message = bus_->SendWithReplyAndBlock( | |
607 get_name_owner_call.raw_message(), | |
608 kGetNameOwnerCallTimeoutMs, | |
609 error.get()); | |
610 if (!response_message) { | |
611 LOG(ERROR) << "Failed to get name owner. Got " << error.name() << ": " << | |
612 error.message(); | |
613 return; | |
614 } | |
615 scoped_ptr<Response> response(Response::FromRawMessage(response_message)); | |
616 MessageReader reader(response.get()); | |
617 reader.PopString(&service_name_owner_); | |
satorux1
2012/10/26 06:07:28
We should check the return value as it's not guara
Haruki Sato
2012/10/26 07:40:28
Good catch! Thank you very much!
| |
618 } | |
619 | |
620 DBusHandlerResult ObjectProxy::HandleNameOwnerChanged(Signal* signal) { | |
621 DCHECK(signal); | |
622 bus_->AssertOnDBusThread(); | |
623 | |
624 // Confirm the validity of the NameOwnerChanged signal. | |
625 if (signal->GetMember() == "NameOwnerChanged" && | |
626 signal->GetInterface() == "org.freedesktop.DBus" && | |
627 signal->GetSender() == "org.freedesktop.DBus") { | |
628 MessageReader reader(signal); | |
629 std::string name, old_owner, new_owner; | |
630 if (reader.PopString(&name) && | |
631 reader.PopString(&old_owner) && | |
632 reader.PopString(&new_owner) && | |
633 name == service_name_) { | |
634 service_name_owner_ = new_owner; | |
635 return DBUS_HANDLER_RESULT_HANDLED; | |
636 } | |
637 } | |
638 | |
639 // Untrusted or uninteresting signal | |
640 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
641 } | |
642 | |
518 } // namespace dbus | 643 } // namespace dbus |
OLD | NEW |