diff options
-rw-r--r-- | dbus/end_to_end_async_unittest.cc | 23 | ||||
-rw-r--r-- | dbus/object_proxy.cc | 15 | ||||
-rw-r--r-- | dbus/object_proxy.h | 8 |
3 files changed, 42 insertions, 4 deletions
diff --git a/dbus/end_to_end_async_unittest.cc b/dbus/end_to_end_async_unittest.cc index 7d52130..7ba782b 100644 --- a/dbus/end_to_end_async_unittest.cc +++ b/dbus/end_to_end_async_unittest.cc @@ -10,6 +10,7 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "base/stl_util.h" +#include "base/test/test_timeouts.h" #include "base/threading/thread.h" #include "base/threading/thread_restrictions.h" #include "dbus/bus.h" @@ -225,6 +226,28 @@ TEST_F(EndToEndAsyncTest, BrokenMethod) { ASSERT_EQ("", response_strings_[0]); } +TEST_F(EndToEndAsyncTest, EmptyResponseCallback) { + const char* kHello = "hello"; + + // Create the method call. + dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); + dbus::MessageWriter writer(&method_call); + writer.AppendString(kHello); + + // Call the method with an empty callback. + const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT; + object_proxy_->CallMethod(&method_call, + timeout_ms, + dbus::ObjectProxy::EmptyResponseCallback()); + // Post a delayed task to quit the message loop. + message_loop_.PostDelayedTask(FROM_HERE, + MessageLoop::QuitClosure(), + TestTimeouts::tiny_timeout_ms()); + message_loop_.Run(); + // We cannot tell if the empty callback is called, but at least we can + // check if the test does not crash. +} + TEST_F(EndToEndAsyncTest, TestSignal) { const char kMessage[] = "hello, world"; // Send the test signal from the exported object. diff --git a/dbus/object_proxy.cc b/dbus/object_proxy.cc index 1a3b7e0..d9a53d9 100644 --- a/dbus/object_proxy.cc +++ b/dbus/object_proxy.cc @@ -29,6 +29,10 @@ std::string GetAbsoluteSignalName( return interface_name + "." + signal_name; } +// An empty function used for ObjectProxy::EmptyResponseCallback(). +void EmptyResponseCallbackBody(dbus::Response* unused_response) { +} + } // namespace namespace dbus { @@ -140,6 +144,11 @@ void ObjectProxy::Detach() { } } +// static +ObjectProxy::ResponseCallback ObjectProxy::EmptyResponseCallback() { + return base::Bind(&EmptyResponseCallbackBody); +} + ObjectProxy::OnPendingCallIsCompleteData::OnPendingCallIsCompleteData( ObjectProxy* in_object_proxy, ResponseCallback in_response_callback, @@ -212,7 +221,7 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, DBusMessage* response_message) { bus_->AssertOnOriginThread(); - bool response_callback_called = false; + bool method_call_successful = false; if (!response_message) { // The response is not received. response_callback.Run(NULL); @@ -235,14 +244,14 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, dbus::Response::FromRawMessage(response_message)); // The response is successfully received. response_callback.Run(response.get()); - response_callback_called = true; + method_call_successful = true; // Record time spent for the method call. Don't include failures. UMA_HISTOGRAM_TIMES("DBus.AsyncMethodCallTime", base::TimeTicks::Now() - start_time); } // Record if the method call is successful, or not. 1 if successful. UMA_HISTOGRAM_ENUMERATION("DBus.AsyncMethodCallSuccess", - response_callback_called, + method_call_successful, kSuccessRatioHistogramMaxValue); } diff --git a/dbus/object_proxy.h b/dbus/object_proxy.h index 5fc4197..484f107 100644 --- a/dbus/object_proxy.h +++ b/dbus/object_proxy.h @@ -73,7 +73,9 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> { // |callback| will be called in the origin thread, once the method call // is complete. As it's called in the origin thread, |callback| can // safely reference objects in the origin thread (i.e. UI thread in most - // cases). + // cases). If the caller is not interested in the response from the + // method (i.e. calling a method that does not return a value), + // EmptyResponseCallback() can be passed to the |callback| parameter. // // If the method call is successful, a pointer to Response object will // be passed to the callback. If unsuccessful, NULL will be passed to @@ -106,6 +108,10 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> { // BLOCKING CALL. virtual void Detach(); + // Returns an empty callback that does nothing. Can be used for + // CallMethod(). + static ResponseCallback EmptyResponseCallback(); + protected: // This is protected, so we can define sub classes. virtual ~ObjectProxy(); |