Commit 65e8e25e authored by satorux@chromium.org's avatar satorux@chromium.org

dbus: Add ObjectProxy::EmptyResponseCallback().

This can be used when the caller is not interested in the response
from the D-bus method.

BUG=none
TEST=added a unit test


Review URL: http://codereview.chromium.org/8536007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@109598 0039d316-1c4b-4281-b951-d872f2087c98
parent 442d0ba8
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "dbus/bus.h" #include "dbus/bus.h"
...@@ -225,6 +226,28 @@ TEST_F(EndToEndAsyncTest, BrokenMethod) { ...@@ -225,6 +226,28 @@ TEST_F(EndToEndAsyncTest, BrokenMethod) {
ASSERT_EQ("", response_strings_[0]); 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) { TEST_F(EndToEndAsyncTest, TestSignal) {
const char kMessage[] = "hello, world"; const char kMessage[] = "hello, world";
// Send the test signal from the exported object. // Send the test signal from the exported object.
......
...@@ -29,6 +29,10 @@ std::string GetAbsoluteSignalName( ...@@ -29,6 +29,10 @@ std::string GetAbsoluteSignalName(
return interface_name + "." + signal_name; return interface_name + "." + signal_name;
} }
// An empty function used for ObjectProxy::EmptyResponseCallback().
void EmptyResponseCallbackBody(dbus::Response* unused_response) {
}
} // namespace } // namespace
namespace dbus { namespace dbus {
...@@ -140,6 +144,11 @@ void ObjectProxy::Detach() { ...@@ -140,6 +144,11 @@ void ObjectProxy::Detach() {
} }
} }
// static
ObjectProxy::ResponseCallback ObjectProxy::EmptyResponseCallback() {
return base::Bind(&EmptyResponseCallbackBody);
}
ObjectProxy::OnPendingCallIsCompleteData::OnPendingCallIsCompleteData( ObjectProxy::OnPendingCallIsCompleteData::OnPendingCallIsCompleteData(
ObjectProxy* in_object_proxy, ObjectProxy* in_object_proxy,
ResponseCallback in_response_callback, ResponseCallback in_response_callback,
...@@ -212,7 +221,7 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, ...@@ -212,7 +221,7 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback,
DBusMessage* response_message) { DBusMessage* response_message) {
bus_->AssertOnOriginThread(); bus_->AssertOnOriginThread();
bool response_callback_called = false; bool method_call_successful = false;
if (!response_message) { if (!response_message) {
// The response is not received. // The response is not received.
response_callback.Run(NULL); response_callback.Run(NULL);
...@@ -235,14 +244,14 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback, ...@@ -235,14 +244,14 @@ void ObjectProxy::RunResponseCallback(ResponseCallback response_callback,
dbus::Response::FromRawMessage(response_message)); dbus::Response::FromRawMessage(response_message));
// The response is successfully received. // The response is successfully received.
response_callback.Run(response.get()); response_callback.Run(response.get());
response_callback_called = true; method_call_successful = true;
// Record time spent for the method call. Don't include failures. // Record time spent for the method call. Don't include failures.
UMA_HISTOGRAM_TIMES("DBus.AsyncMethodCallTime", UMA_HISTOGRAM_TIMES("DBus.AsyncMethodCallTime",
base::TimeTicks::Now() - start_time); base::TimeTicks::Now() - start_time);
} }
// Record if the method call is successful, or not. 1 if successful. // Record if the method call is successful, or not. 1 if successful.
UMA_HISTOGRAM_ENUMERATION("DBus.AsyncMethodCallSuccess", UMA_HISTOGRAM_ENUMERATION("DBus.AsyncMethodCallSuccess",
response_callback_called, method_call_successful,
kSuccessRatioHistogramMaxValue); kSuccessRatioHistogramMaxValue);
} }
......
...@@ -73,7 +73,9 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> { ...@@ -73,7 +73,9 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> {
// |callback| will be called in the origin thread, once the method call // |callback| will be called in the origin thread, once the method call
// is complete. As it's called in the origin thread, |callback| can // 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 // 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 // If the method call is successful, a pointer to Response object will
// be passed to the callback. If unsuccessful, NULL will be passed to // be passed to the callback. If unsuccessful, NULL will be passed to
...@@ -106,6 +108,10 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> { ...@@ -106,6 +108,10 @@ class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> {
// BLOCKING CALL. // BLOCKING CALL.
virtual void Detach(); virtual void Detach();
// Returns an empty callback that does nothing. Can be used for
// CallMethod().
static ResponseCallback EmptyResponseCallback();
protected: protected:
// This is protected, so we can define sub classes. // This is protected, so we can define sub classes.
virtual ~ObjectProxy(); virtual ~ObjectProxy();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment