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/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 | 494 |
495 // Wait until the shutdown is complete on the D-Bus thread. | 495 // Wait until the shutdown is complete on the D-Bus thread. |
496 // The shutdown should not hang, but set timeout just in case. | 496 // The shutdown should not hang, but set timeout just in case. |
497 const int kTimeoutSecs = 3; | 497 const int kTimeoutSecs = 3; |
498 const base::TimeDelta timeout(base::TimeDelta::FromSeconds(kTimeoutSecs)); | 498 const base::TimeDelta timeout(base::TimeDelta::FromSeconds(kTimeoutSecs)); |
499 const bool signaled = on_shutdown_.TimedWait(timeout); | 499 const bool signaled = on_shutdown_.TimedWait(timeout); |
500 LOG_IF(ERROR, !signaled) << "Failed to shutdown the bus"; | 500 LOG_IF(ERROR, !signaled) << "Failed to shutdown the bus"; |
501 } | 501 } |
502 | 502 |
503 void Bus::RequestOwnership(const std::string& service_name, | 503 void Bus::RequestOwnership(const std::string& service_name, |
| 504 ServiceOwnershipOptions options, |
504 OnOwnershipCallback on_ownership_callback) { | 505 OnOwnershipCallback on_ownership_callback) { |
505 AssertOnOriginThread(); | 506 AssertOnOriginThread(); |
506 | 507 |
507 PostTaskToDBusThread(FROM_HERE, base::Bind( | 508 PostTaskToDBusThread(FROM_HERE, base::Bind( |
508 &Bus::RequestOwnershipInternal, | 509 &Bus::RequestOwnershipInternal, |
509 this, service_name, on_ownership_callback)); | 510 this, service_name, options, on_ownership_callback)); |
510 } | 511 } |
511 | 512 |
512 void Bus::RequestOwnershipInternal(const std::string& service_name, | 513 void Bus::RequestOwnershipInternal(const std::string& service_name, |
| 514 ServiceOwnershipOptions options, |
513 OnOwnershipCallback on_ownership_callback) { | 515 OnOwnershipCallback on_ownership_callback) { |
514 AssertOnDBusThread(); | 516 AssertOnDBusThread(); |
515 | 517 |
516 bool success = Connect(); | 518 bool success = Connect(); |
517 if (success) | 519 if (success) |
518 success = RequestOwnershipAndBlock(service_name); | 520 success = RequestOwnershipAndBlock(service_name, options); |
519 | 521 |
520 PostTaskToOriginThread(FROM_HERE, | 522 PostTaskToOriginThread(FROM_HERE, |
521 base::Bind(on_ownership_callback, | 523 base::Bind(on_ownership_callback, |
522 service_name, | 524 service_name, |
523 success)); | 525 success)); |
524 } | 526 } |
525 | 527 |
526 bool Bus::RequestOwnershipAndBlock(const std::string& service_name) { | 528 bool Bus::RequestOwnershipAndBlock(const std::string& service_name, |
| 529 ServiceOwnershipOptions options) { |
527 DCHECK(connection_); | 530 DCHECK(connection_); |
528 // dbus_bus_request_name() is a blocking call. | 531 // dbus_bus_request_name() is a blocking call. |
529 AssertOnDBusThread(); | 532 AssertOnDBusThread(); |
530 | 533 |
531 // Check if we already own the service name. | 534 // Check if we already own the service name. |
532 if (owned_service_names_.find(service_name) != owned_service_names_.end()) { | 535 if (owned_service_names_.find(service_name) != owned_service_names_.end()) { |
533 return true; | 536 return true; |
534 } | 537 } |
535 | 538 |
536 ScopedDBusError error; | 539 ScopedDBusError error; |
537 const int result = dbus_bus_request_name(connection_, | 540 const int result = dbus_bus_request_name(connection_, |
538 service_name.c_str(), | 541 service_name.c_str(), |
539 DBUS_NAME_FLAG_DO_NOT_QUEUE, | 542 options, |
540 error.get()); | 543 error.get()); |
541 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { | 544 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
542 LOG(ERROR) << "Failed to get the ownership of " << service_name << ": " | 545 LOG(ERROR) << "Failed to get the ownership of " << service_name << ": " |
543 << (error.is_set() ? error.message() : ""); | 546 << (error.is_set() ? error.message() : ""); |
544 return false; | 547 return false; |
545 } | 548 } |
546 owned_service_names_.insert(service_name); | 549 owned_service_names_.insert(service_name); |
547 return true; | 550 return true; |
548 } | 551 } |
549 | 552 |
(...skipping 11 matching lines...) Expand all Loading... |
561 } | 564 } |
562 | 565 |
563 ScopedDBusError error; | 566 ScopedDBusError error; |
564 const int result = dbus_bus_release_name(connection_, service_name.c_str(), | 567 const int result = dbus_bus_release_name(connection_, service_name.c_str(), |
565 error.get()); | 568 error.get()); |
566 if (result == DBUS_RELEASE_NAME_REPLY_RELEASED) { | 569 if (result == DBUS_RELEASE_NAME_REPLY_RELEASED) { |
567 owned_service_names_.erase(found); | 570 owned_service_names_.erase(found); |
568 return true; | 571 return true; |
569 } else { | 572 } else { |
570 LOG(ERROR) << "Failed to release the ownership of " << service_name << ": " | 573 LOG(ERROR) << "Failed to release the ownership of " << service_name << ": " |
571 << (error.is_set() ? error.message() : ""); | 574 << (error.is_set() ? error.message() : "") |
| 575 << ", result code: " << result; |
572 return false; | 576 return false; |
573 } | 577 } |
574 } | 578 } |
575 | 579 |
576 bool Bus::SetUpAsyncOperations() { | 580 bool Bus::SetUpAsyncOperations() { |
577 DCHECK(connection_); | 581 DCHECK(connection_); |
578 AssertOnDBusThread(); | 582 AssertOnDBusThread(); |
579 | 583 |
580 if (async_operations_set_up_) | 584 if (async_operations_set_up_) |
581 return true; | 585 return true; |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1224 kNameOwnerChangedSignal)) { | 1228 kNameOwnerChangedSignal)) { |
1225 Bus* self = static_cast<Bus*>(data); | 1229 Bus* self = static_cast<Bus*>(data); |
1226 self->OnServiceOwnerChanged(message); | 1230 self->OnServiceOwnerChanged(message); |
1227 } | 1231 } |
1228 // Always return unhandled to let others, e.g. ObjectProxies, handle the same | 1232 // Always return unhandled to let others, e.g. ObjectProxies, handle the same |
1229 // signal. | 1233 // signal. |
1230 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 1234 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
1231 } | 1235 } |
1232 | 1236 |
1233 } // namespace dbus | 1237 } // namespace dbus |
OLD | NEW |