diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-26 04:32:29 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-26 04:32:29 +0000 |
commit | 8609be26ac387460e7035a8422260cdc29486e52 (patch) | |
tree | 048c6f1652e433d72dfcce7240ca6c038254a5e9 /dbus | |
parent | 31afdf7decb4ee984a6bb4f4fee84380476849a6 (diff) | |
download | chromium_src-8609be26ac387460e7035a8422260cdc29486e52.zip chromium_src-8609be26ac387460e7035a8422260cdc29486e52.tar.gz chromium_src-8609be26ac387460e7035a8422260cdc29486e52.tar.bz2 |
dbus: Replace PostTaskTo*Thread methods with Get*TaskRunner
By exposing TaskRunners,
we'll be able to use utility functions like base::PostTaskAndReplyWithResult
BUG=None
TEST=git cl try
R=satorux@chromium.org
Review URL: https://codereview.chromium.org/24554002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@225324 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'dbus')
-rw-r--r-- | dbus/bus.cc | 135 | ||||
-rw-r--r-- | dbus/bus.h | 32 | ||||
-rw-r--r-- | dbus/end_to_end_async_unittest.cc | 6 | ||||
-rw-r--r-- | dbus/exported_object.cc | 52 | ||||
-rw-r--r-- | dbus/mock_bus.h | 12 | ||||
-rw-r--r-- | dbus/object_proxy.cc | 56 |
6 files changed, 115 insertions, 178 deletions
diff --git a/dbus/bus.cc b/dbus/bus.cc index ec2417e..3f8e6d7f 100644 --- a/dbus/bus.cc +++ b/dbus/bus.cc @@ -124,10 +124,10 @@ class Timeout : public base::RefCountedThreadSafe<Timeout> { // Starts monitoring the timeout. void StartMonitoring(Bus* bus) { - bus->PostDelayedTaskToDBusThread(FROM_HERE, - base::Bind(&Timeout::HandleTimeout, - this), - GetInterval()); + bus->GetDBusTaskRunner()->PostDelayedTask( + FROM_HERE, + base::Bind(&Timeout::HandleTimeout, this), + GetInterval()); monitoring_is_active_ = true; } @@ -266,9 +266,10 @@ bool Bus::RemoveObjectProxyWithOptions(const std::string& service_name, ObjectProxyTable::iterator iter = object_proxy_table_.find(key); if (iter != object_proxy_table_.end()) { // Object is present. Remove it now and Detach in the DBus thread. - PostTaskToDBusThread(FROM_HERE, base::Bind( - &Bus::RemoveObjectProxyInternal, - this, iter->second, callback)); + GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&Bus::RemoveObjectProxyInternal, + this, iter->second, callback)); object_proxy_table_.erase(iter); return true; @@ -282,7 +283,7 @@ void Bus::RemoveObjectProxyInternal(scoped_refptr<ObjectProxy> object_proxy, object_proxy.get()->Detach(); - PostTaskToOriginThread(FROM_HERE, callback); + GetOriginTaskRunner()->PostTask(FROM_HERE, callback); } ExportedObject* Bus::GetExportedObject(const ObjectPath& object_path) { @@ -318,9 +319,10 @@ void Bus::UnregisterExportedObject(const ObjectPath& object_path) { // TryRegisterObjectPath(), and the task runner we post to is a // SequencedTaskRunner, there is a guarantee that this will happen before any // future registration call. - PostTaskToDBusThread(FROM_HERE, - base::Bind(&Bus::UnregisterExportedObjectInternal, - this, exported_object)); + GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&Bus::UnregisterExportedObjectInternal, + this, exported_object)); } void Bus::UnregisterExportedObjectInternal( @@ -485,9 +487,9 @@ void Bus::ShutdownOnDBusThreadAndBlock() { AssertOnOriginThread(); DCHECK(dbus_task_runner_.get()); - PostTaskToDBusThread(FROM_HERE, base::Bind( - &Bus::ShutdownOnDBusThreadAndBlockInternal, - this)); + GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&Bus::ShutdownOnDBusThreadAndBlockInternal, this)); // http://crbug.com/125222 base::ThreadRestrictions::ScopedAllowWait allow_wait; @@ -505,9 +507,10 @@ void Bus::RequestOwnership(const std::string& service_name, OnOwnershipCallback on_ownership_callback) { AssertOnOriginThread(); - PostTaskToDBusThread(FROM_HERE, base::Bind( - &Bus::RequestOwnershipInternal, - this, service_name, options, on_ownership_callback)); + GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&Bus::RequestOwnershipInternal, + this, service_name, options, on_ownership_callback)); } void Bus::RequestOwnershipInternal(const std::string& service_name, @@ -519,10 +522,10 @@ void Bus::RequestOwnershipInternal(const std::string& service_name, if (success) success = RequestOwnershipAndBlock(service_name, options); - PostTaskToOriginThread(FROM_HERE, - base::Bind(on_ownership_callback, - service_name, - success)); + GetOriginTaskRunner()->PostTask(FROM_HERE, + base::Bind(on_ownership_callback, + service_name, + success)); } bool Bus::RequestOwnershipAndBlock(const std::string& service_name, @@ -790,61 +793,16 @@ void Bus::ProcessAllIncomingDataIfAny() { } } -void Bus::PostTaskToDBusThreadAndReply( - const tracked_objects::Location& from_here, - const base::Closure& task, - const base::Closure& reply) { - AssertOnOriginThread(); - - if (dbus_task_runner_.get()) { - if (!dbus_task_runner_->PostTaskAndReply(from_here, task, reply)) { - LOG(WARNING) << "Failed to post a task to the D-Bus thread message loop"; - } - } else { - DCHECK(origin_task_runner_.get()); - if (!origin_task_runner_->PostTaskAndReply(from_here, task, reply)) { - LOG(WARNING) << "Failed to post a task to the origin message loop"; - } - } +base::TaskRunner* Bus::GetDBusTaskRunner() { + if (dbus_task_runner_.get()) + return dbus_task_runner_.get(); + else + return GetOriginTaskRunner(); } -void Bus::PostTaskToOriginThread(const tracked_objects::Location& from_here, - const base::Closure& task) { +base::TaskRunner* Bus::GetOriginTaskRunner() { DCHECK(origin_task_runner_.get()); - if (!origin_task_runner_->PostTask(from_here, task)) { - LOG(WARNING) << "Failed to post a task to the origin message loop"; - } -} - -void Bus::PostTaskToDBusThread(const tracked_objects::Location& from_here, - const base::Closure& task) { - if (dbus_task_runner_.get()) { - if (!dbus_task_runner_->PostTask(from_here, task)) { - LOG(WARNING) << "Failed to post a task to the D-Bus thread message loop"; - } - } else { - DCHECK(origin_task_runner_.get()); - if (!origin_task_runner_->PostTask(from_here, task)) { - LOG(WARNING) << "Failed to post a task to the origin message loop"; - } - } -} - -void Bus::PostDelayedTaskToDBusThread( - const tracked_objects::Location& from_here, - const base::Closure& task, - base::TimeDelta delay) { - if (dbus_task_runner_.get()) { - if (!dbus_task_runner_->PostDelayedTask( - from_here, task, delay)) { - LOG(WARNING) << "Failed to post a task to the D-Bus thread message loop"; - } - } else { - DCHECK(origin_task_runner_.get()); - if (!origin_task_runner_->PostDelayedTask(from_here, task, delay)) { - LOG(WARNING) << "Failed to post a task to the origin message loop"; - } - } + return origin_task_runner_.get(); } bool Bus::HasDBusThread() { @@ -908,7 +866,7 @@ void Bus::GetServiceOwner(const std::string& service_name, const GetServiceOwnerCallback& callback) { AssertOnOriginThread(); - PostTaskToDBusThread( + GetDBusTaskRunner()->PostTask( FROM_HERE, base::Bind(&Bus::GetServiceOwnerInternal, this, service_name, callback)); } @@ -920,7 +878,8 @@ void Bus::GetServiceOwnerInternal(const std::string& service_name, std::string service_owner; if (Connect()) service_owner = GetServiceOwnerAndBlock(service_name, SUPPRESS_ERRORS); - PostTaskToOriginThread(FROM_HERE, base::Bind(callback, service_owner)); + GetOriginTaskRunner()->PostTask(FROM_HERE, + base::Bind(callback, service_owner)); } void Bus::ListenForServiceOwnerChange( @@ -930,9 +889,10 @@ void Bus::ListenForServiceOwnerChange( DCHECK(!service_name.empty()); DCHECK(!callback.is_null()); - PostTaskToDBusThread(FROM_HERE, - base::Bind(&Bus::ListenForServiceOwnerChangeInternal, - this, service_name, callback)); + GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&Bus::ListenForServiceOwnerChangeInternal, + this, service_name, callback)); } void Bus::ListenForServiceOwnerChangeInternal( @@ -986,9 +946,10 @@ void Bus::UnlistenForServiceOwnerChange( DCHECK(!service_name.empty()); DCHECK(!callback.is_null()); - PostTaskToDBusThread(FROM_HERE, - base::Bind(&Bus::UnlistenForServiceOwnerChangeInternal, - this, service_name, callback)); + GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&Bus::UnlistenForServiceOwnerChangeInternal, + this, service_name, callback)); } void Bus::UnlistenForServiceOwnerChangeInternal( @@ -1103,16 +1064,16 @@ void Bus::OnDispatchStatusChanged(DBusConnection* connection, // dbus_connection_dispatch() inside DBusDispatchStatusFunction is // prohibited by the D-Bus library. Hence, we post a task here instead. // See comments for dbus_connection_set_dispatch_status_function(). - PostTaskToDBusThread(FROM_HERE, - base::Bind(&Bus::ProcessAllIncomingDataIfAny, - this)); + GetDBusTaskRunner()->PostTask(FROM_HERE, + base::Bind(&Bus::ProcessAllIncomingDataIfAny, + this)); } void Bus::OnConnectionDisconnected(DBusConnection* connection) { AssertOnDBusThread(); if (!on_disconnected_closure_.is_null()) - PostTaskToOriginThread(FROM_HERE, on_disconnected_closure_); + GetOriginTaskRunner()->PostTask(FROM_HERE, on_disconnected_closure_); if (!connection) return; @@ -1154,8 +1115,8 @@ void Bus::OnServiceOwnerChanged(DBusMessage* message) { const std::vector<GetServiceOwnerCallback>& callbacks = it->second; for (size_t i = 0; i < callbacks.size(); ++i) { - PostTaskToOriginThread(FROM_HERE, - base::Bind(callbacks[i], new_owner)); + GetOriginTaskRunner()->PostTask(FROM_HERE, + base::Bind(callbacks[i], new_owner)); } } @@ -23,6 +23,7 @@ namespace base { class SequencedTaskRunner; class SingleThreadTaskRunner; +class TaskRunner; } namespace tracked_objects { @@ -529,32 +530,11 @@ class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> { // BLOCKING CALL. virtual void UnregisterObjectPath(const ObjectPath& object_path); - // Posts |task| to the task runner of the D-Bus thread. On completion, |reply| - // is posted to the origin thread. - virtual void PostTaskToDBusThreadAndReply( - const tracked_objects::Location& from_here, - const base::Closure& task, - const base::Closure& reply); - - // Posts the task to the task runner of the thread that created the bus. - virtual void PostTaskToOriginThread( - const tracked_objects::Location& from_here, - const base::Closure& task); - - // Posts the task to the task runner of the D-Bus thread. If D-Bus - // thread is not supplied, the task runner of the origin thread will be - // used. - virtual void PostTaskToDBusThread( - const tracked_objects::Location& from_here, - const base::Closure& task); - - // Posts the delayed task to the task runner of the D-Bus thread. If - // D-Bus thread is not supplied, the task runner of the origin thread - // will be used. - virtual void PostDelayedTaskToDBusThread( - const tracked_objects::Location& from_here, - const base::Closure& task, - base::TimeDelta delay); + // Returns the task runner of the D-Bus thread. + virtual base::TaskRunner* GetDBusTaskRunner(); + + // Returns the task runner of the thread that created the bus. + virtual base::TaskRunner* GetOriginTaskRunner(); // Returns true if the bus has the D-Bus thread. virtual bool HasDBusThread(); diff --git a/dbus/end_to_end_async_unittest.cc b/dbus/end_to_end_async_unittest.cc index 1f2538a..fb8030b 100644 --- a/dbus/end_to_end_async_unittest.cc +++ b/dbus/end_to_end_async_unittest.cc @@ -580,9 +580,9 @@ TEST_F(EndToEndAsyncTest, TestHugeSignal) { } TEST_F(EndToEndAsyncTest, DisconnectedSignal) { - bus_->PostTaskToDBusThread(FROM_HERE, - base::Bind(&Bus::ClosePrivateConnection, - base::Unretained(bus_.get()))); + bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, + base::Bind(&Bus::ClosePrivateConnection, + base::Unretained(bus_.get()))); // OnDisconnected callback quits message loop. message_loop_.Run(); EXPECT_EQ(1, on_disconnected_call_count_); diff --git a/dbus/exported_object.cc b/dbus/exported_object.cc index 2ba6d78..1a4fbb5 100644 --- a/dbus/exported_object.cc +++ b/dbus/exported_object.cc @@ -84,7 +84,7 @@ void ExportedObject::ExportMethod(const std::string& interface_name, method_name, method_call_callback, on_exported_calback); - bus_->PostTaskToDBusThread(FROM_HERE, task); + bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, task); } void ExportedObject::SendSignal(Signal* signal) { @@ -99,11 +99,12 @@ void ExportedObject::SendSignal(Signal* signal) { dbus_message_ref(signal_message); const base::TimeTicks start_time = base::TimeTicks::Now(); - bus_->PostTaskToDBusThread(FROM_HERE, - base::Bind(&ExportedObject::SendSignalInternal, - this, - start_time, - signal_message)); + bus_->GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&ExportedObject::SendSignalInternal, + this, + start_time, + signal_message)); } void ExportedObject::Unregister() { @@ -126,13 +127,13 @@ void ExportedObject::ExportMethodInternal( const bool success = ExportMethodAndBlock(interface_name, method_name, method_call_callback); - bus_->PostTaskToOriginThread(FROM_HERE, - base::Bind(&ExportedObject::OnExported, - this, - on_exported_calback, - interface_name, - method_name, - success)); + bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, + base::Bind(&ExportedObject::OnExported, + this, + on_exported_calback, + interface_name, + method_name, + success)); } void ExportedObject::OnExported(OnExportedCallback on_exported_callback, @@ -214,12 +215,12 @@ DBusHandlerResult ExportedObject::HandleMessage( const base::TimeTicks start_time = base::TimeTicks::Now(); if (bus_->HasDBusThread()) { // Post a task to run the method in the origin thread. - bus_->PostTaskToOriginThread(FROM_HERE, - base::Bind(&ExportedObject::RunMethod, - this, - iter->second, - base::Passed(&method_call), - start_time)); + bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, + base::Bind(&ExportedObject::RunMethod, + this, + iter->second, + base::Passed(&method_call), + start_time)); } else { // If the D-Bus thread is not used, just call the method directly. MethodCall* method = method_call.get(); @@ -252,12 +253,13 @@ void ExportedObject::SendResponse(base::TimeTicks start_time, scoped_ptr<Response> response) { DCHECK(method_call); if (bus_->HasDBusThread()) { - bus_->PostTaskToDBusThread(FROM_HERE, - base::Bind(&ExportedObject::OnMethodCompleted, - this, - base::Passed(&method_call), - base::Passed(&response), - start_time)); + bus_->GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&ExportedObject::OnMethodCompleted, + this, + base::Passed(&method_call), + base::Passed(&response), + start_time)); } else { OnMethodCompleted(method_call.Pass(), response.Pass(), start_time); } diff --git a/dbus/mock_bus.h b/dbus/mock_bus.h index c3991f5..6000067 100644 --- a/dbus/mock_bus.h +++ b/dbus/mock_bus.h @@ -60,16 +60,8 @@ class MockBus : public Bus { void* user_data, DBusError* error)); MOCK_METHOD1(UnregisterObjectPath, void(const ObjectPath& object_path)); - MOCK_METHOD2(PostTaskToOriginThread, void( - const tracked_objects::Location& from_here, - const base::Closure& task)); - MOCK_METHOD2(PostTaskToDBusThread, void( - const tracked_objects::Location& from_here, - const base::Closure& task)); - MOCK_METHOD3(PostDelayedTaskToDBusThread, void( - const tracked_objects::Location& from_here, - const base::Closure& task, - base::TimeDelta delay)); + MOCK_METHOD0(GetDBusTaskRunner, base::TaskRunner*()); + MOCK_METHOD0(GetOriginTaskRunner, base::TaskRunner*()); MOCK_METHOD0(HasDBusThread, bool()); MOCK_METHOD0(AssertOnOriginThread, void()); MOCK_METHOD0(AssertOnDBusThread, void()); diff --git a/dbus/object_proxy.cc b/dbus/object_proxy.cc index ea76fd7..11dc069 100644 --- a/dbus/object_proxy.cc +++ b/dbus/object_proxy.cc @@ -140,7 +140,7 @@ void ObjectProxy::CallMethodWithErrorCallback(MethodCall* method_call, error_callback, start_time, response_message); - bus_->PostTaskToOriginThread(FROM_HERE, task); + bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); return; } @@ -162,7 +162,7 @@ void ObjectProxy::CallMethodWithErrorCallback(MethodCall* method_call, method_call->GetMember()); // Wait for the response in the D-Bus thread. - bus_->PostTaskToDBusThread(FROM_HERE, task); + bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, task); } void ObjectProxy::ConnectToSignal(const std::string& interface_name, @@ -171,13 +171,14 @@ void ObjectProxy::ConnectToSignal(const std::string& interface_name, OnConnectedCallback on_connected_callback) { bus_->AssertOnOriginThread(); - bus_->PostTaskToDBusThread(FROM_HERE, - base::Bind(&ObjectProxy::ConnectToSignalInternal, - this, - interface_name, - signal_name, - signal_callback, - on_connected_callback)); + bus_->GetDBusTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&ObjectProxy::ConnectToSignalInternal, + this, + interface_name, + signal_name, + signal_callback, + on_connected_callback)); } void ObjectProxy::Detach() { @@ -236,7 +237,7 @@ void ObjectProxy::StartAsyncMethodCall(int timeout_ms, error_callback, start_time, response_message); - bus_->PostTaskToOriginThread(FROM_HERE, task); + bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); dbus_message_unref(request_message); return; @@ -278,7 +279,7 @@ void ObjectProxy::OnPendingCallIsComplete(DBusPendingCall* pending_call, error_callback, start_time, response_message); - bus_->PostTaskToOriginThread(FROM_HERE, task); + bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, task); } void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, @@ -298,7 +299,7 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, ErrorResponse::FromRawMessage(response_message)); error_callback.Run(error_response.get()); // Delete the message on the D-Bus thread. See below for why. - bus_->PostTaskToDBusThread( + bus_->GetDBusTaskRunner()->PostTask( FROM_HERE, base::Bind(&base::DeletePointer<ErrorResponse>, error_response.release())); @@ -325,7 +326,7 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, // The monitoring of the socket is done on the D-Bus thread (see Watch // class in bus.cc), hence we should stop the monitoring from D-Bus // thread, not from the current thread here, which is likely UI thread. - bus_->PostTaskToDBusThread( + bus_->GetDBusTaskRunner()->PostTask( FROM_HERE, base::Bind(&base::DeletePointer<Response>, response.release())); @@ -404,7 +405,7 @@ void ObjectProxy::ConnectToSignalInternal( } // Run on_connected_callback in the origin thread. - bus_->PostTaskToOriginThread( + bus_->GetOriginTaskRunner()->PostTask( FROM_HERE, base::Bind(&ObjectProxy::OnConnected, this, @@ -484,12 +485,12 @@ DBusHandlerResult ObjectProxy::HandleMessage( // Transfer the ownership of |signal| to RunMethod(). // |released_signal| will be deleted in RunMethod(). Signal* released_signal = signal.release(); - bus_->PostTaskToOriginThread(FROM_HERE, - base::Bind(&ObjectProxy::RunMethod, - this, - start_time, - iter->second, - released_signal)); + bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, + base::Bind(&ObjectProxy::RunMethod, + this, + start_time, + iter->second, + released_signal)); } else { const base::TimeTicks start_time = base::TimeTicks::Now(); // If the D-Bus thread is not used, just call the callback on the @@ -512,7 +513,7 @@ void ObjectProxy::RunMethod(base::TimeTicks start_time, // Delete the message on the D-Bus thread. See comments in // RunResponseCallback(). - bus_->PostTaskToDBusThread( + bus_->GetDBusTaskRunner()->PostTask( FROM_HERE, base::Bind(&base::DeletePointer<Signal>, signal)); @@ -641,12 +642,13 @@ DBusHandlerResult ObjectProxy::HandleNameOwnerChanged( Signal* released_signal = signal.release(); std::vector<SignalCallback> callbacks; callbacks.push_back(name_owner_changed_callback_); - bus_->PostTaskToOriginThread(FROM_HERE, - base::Bind(&ObjectProxy::RunMethod, - this, - start_time, - callbacks, - released_signal)); + bus_->GetOriginTaskRunner()->PostTask( + FROM_HERE, + base::Bind(&ObjectProxy::RunMethod, + this, + start_time, + callbacks, + released_signal)); } } } |