Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Unified Diff: dbus/object_proxy.cc

Issue 24557002: dbus: Rewrite ObjectProxy::ConnectToSignal with PostTaskAndReplyWithResult (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dbus/object_proxy.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dbus/object_proxy.cc
diff --git a/dbus/object_proxy.cc b/dbus/object_proxy.cc
index 11dc06966f8ee6d46f52b84bf4f0e9b41787e850..5ff0b862fd1666aeea1fee77c2c6c74c1d4ea3d3 100644
--- a/dbus/object_proxy.cc
+++ b/dbus/object_proxy.cc
@@ -10,6 +10,7 @@
#include "base/metrics/histogram.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
+#include "base/task_runner_util.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "dbus/dbus_statistics.h"
@@ -171,14 +172,17 @@ void ObjectProxy::ConnectToSignal(const std::string& interface_name,
OnConnectedCallback on_connected_callback) {
bus_->AssertOnOriginThread();
- bus_->GetDBusTaskRunner()->PostTask(
+ base::PostTaskAndReplyWithResult(
+ bus_->GetDBusTaskRunner(),
FROM_HERE,
base::Bind(&ObjectProxy::ConnectToSignalInternal,
this,
interface_name,
signal_name,
- signal_callback,
- on_connected_callback));
+ signal_callback),
+ base::Bind(on_connected_callback,
+ interface_name,
+ signal_name));
}
void ObjectProxy::Detach() {
@@ -353,75 +357,54 @@ void ObjectProxy::OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call,
delete data;
}
-void ObjectProxy::ConnectToSignalInternal(
- const std::string& interface_name,
- const std::string& signal_name,
- SignalCallback signal_callback,
- OnConnectedCallback on_connected_callback) {
+bool ObjectProxy::ConnectToSignalInternal(const std::string& interface_name,
+ const std::string& signal_name,
+ SignalCallback signal_callback) {
bus_->AssertOnDBusThread();
const std::string absolute_signal_name =
GetAbsoluteSignalName(interface_name, signal_name);
- // Will become true, if everything is successful.
- bool success = false;
-
- if (bus_->Connect() && bus_->SetUpAsyncOperations()) {
- // We should add the filter only once. Otherwise, HandleMessage() will
- // be called more than once.
- if (!filter_added_) {
- if (bus_->AddFilterFunction(&ObjectProxy::HandleMessageThunk, this)) {
- filter_added_ = true;
- } else {
- LOG(ERROR) << "Failed to add filter function";
- }
- }
- // Add a match rule so the signal goes through HandleMessage().
- const std::string match_rule =
- base::StringPrintf("type='signal', interface='%s', path='%s'",
- interface_name.c_str(),
- object_path_.value().c_str());
- // Add a match_rule listening NameOwnerChanged for the well-known name
- // |service_name_|.
- const std::string name_owner_changed_match_rule =
- base::StringPrintf(
- "type='signal',interface='org.freedesktop.DBus',"
- "member='NameOwnerChanged',path='/org/freedesktop/DBus',"
- "sender='org.freedesktop.DBus',arg0='%s'",
- service_name_.c_str());
- if (AddMatchRuleWithCallback(match_rule,
- absolute_signal_name,
- signal_callback) &&
- AddMatchRuleWithoutCallback(name_owner_changed_match_rule,
- "org.freedesktop.DBus.NameOwnerChanged")) {
- success = true;
- }
+ if (!bus_->Connect() || !bus_->SetUpAsyncOperations())
+ return false;
- // Try getting the current name owner. It's not guaranteed that we can get
- // the name owner at this moment, as the service may not yet be started. If
- // that's the case, we'll get the name owner via NameOwnerChanged signal,
- // as soon as the service is started.
- UpdateNameOwnerAndBlock();
+ // We should add the filter only once. Otherwise, HandleMessage() will
+ // be called more than once.
+ if (!filter_added_) {
+ if (bus_->AddFilterFunction(&ObjectProxy::HandleMessageThunk, this)) {
+ filter_added_ = true;
+ } else {
+ LOG(ERROR) << "Failed to add filter function";
+ }
}
-
- // Run on_connected_callback in the origin thread.
- bus_->GetOriginTaskRunner()->PostTask(
- FROM_HERE,
- base::Bind(&ObjectProxy::OnConnected,
- this,
- on_connected_callback,
- interface_name,
- signal_name,
- success));
-}
-
-void ObjectProxy::OnConnected(OnConnectedCallback on_connected_callback,
- const std::string& interface_name,
- const std::string& signal_name,
- bool success) {
- bus_->AssertOnOriginThread();
-
- on_connected_callback.Run(interface_name, signal_name, success);
+ // Add a match rule so the signal goes through HandleMessage().
+ const std::string match_rule =
+ base::StringPrintf("type='signal', interface='%s', path='%s'",
+ interface_name.c_str(),
+ object_path_.value().c_str());
+ // Add a match_rule listening NameOwnerChanged for the well-known name
+ // |service_name_|.
+ const std::string name_owner_changed_match_rule =
+ base::StringPrintf(
+ "type='signal',interface='org.freedesktop.DBus',"
+ "member='NameOwnerChanged',path='/org/freedesktop/DBus',"
+ "sender='org.freedesktop.DBus',arg0='%s'",
+ service_name_.c_str());
+
+ const bool success =
+ AddMatchRuleWithCallback(match_rule,
+ absolute_signal_name,
+ signal_callback) &&
+ AddMatchRuleWithoutCallback(name_owner_changed_match_rule,
+ "org.freedesktop.DBus.NameOwnerChanged");
+
+ // Try getting the current name owner. It's not guaranteed that we can get
+ // the name owner at this moment, as the service may not yet be started. If
+ // that's the case, we'll get the name owner via NameOwnerChanged signal,
+ // as soon as the service is started.
+ UpdateNameOwnerAndBlock();
+
+ return success;
}
void ObjectProxy::SetNameOwnerChangedCallback(SignalCallback callback) {
« no previous file with comments | « dbus/object_proxy.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698