dbus: Let ObjectProxy::CallMethod() take OnceCallback

Currently, ObjectProxy::CallMethod is implemented by creating ErrorCallback from ResponseCallback.
This does not work with OnceCallback because it's not allowed to create two callbacks from one OnceCallback.
To solve this, this change introduces CallMethodCallbackInternal to ObjectProxy which takes either Response* or ErrorResponse*, and dispatches the response to the appropriate callback.

Also, Gmock does not allow directly mocking functions which take move-only
arguments.
https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#mocking-methods-that-use-move-only-types
To workaround this, add DoCallMethod to MockObjetProxy.

BUG=740015
TEST=build

Change-Id: I1a98b5fd1d6d70f69674102a3d66bcec0c13214c
Reviewed-on: https://chromium-review.googlesource.com/578541
Commit-Queue: Ryo Hashimoto <hashimoto@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Satoru Takabayashi <satorux@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488615}

CrOS-Libchrome-Original-Commit: 2a363a8cb1791cb18a4a64d52afde428e92ba73e
diff --git a/dbus/object_proxy.h b/dbus/object_proxy.h
index 5de3904..48d8468 100644
--- a/dbus/object_proxy.h
+++ b/dbus/object_proxy.h
@@ -68,30 +68,30 @@
 
   // Called when an error response is returned or no response is returned.
   // Used for CallMethodWithErrorCallback().
-  typedef base::Callback<void(ErrorResponse*)> ErrorCallback;
+  using ErrorCallback = base::OnceCallback<void(ErrorResponse*)>;
 
   // Called when the response is returned. Used for CallMethod().
-  typedef base::Callback<void(Response*)> ResponseCallback;
+  using ResponseCallback = base::OnceCallback<void(Response*)>;
 
   // Called when a signal is received. Signal* is the incoming signal.
-  typedef base::Callback<void (Signal*)> SignalCallback;
+  using SignalCallback = base::Callback<void(Signal*)>;
 
   // Called when NameOwnerChanged signal is received.
-  typedef base::Callback<void(
-      const std::string& old_owner,
-      const std::string& new_owner)> NameOwnerChangedCallback;
+  using NameOwnerChangedCallback =
+      base::Callback<void(const std::string& old_owner,
+                          const std::string& new_owner)>;
 
   // Called when the service becomes available.
-  typedef base::Callback<void(
-      bool service_is_available)> WaitForServiceToBeAvailableCallback;
+  using WaitForServiceToBeAvailableCallback =
+      base::Callback<void(bool service_is_available)>;
 
   // Called when the object proxy is connected to the signal.
   // Parameters:
   // - the interface name.
   // - the signal name.
   // - whether it was successful or not.
-  typedef base::Callback<void (const std::string&, const std::string&, bool)>
-      OnConnectedCallback;
+  using OnConnectedCallback =
+      base::Callback<void(const std::string&, const std::string&, bool)>;
 
   // Calls the method of the remote object and blocks until the response
   // is returned. Returns NULL on error with the error details specified
@@ -201,40 +201,45 @@
  private:
   friend class base::RefCountedThreadSafe<ObjectProxy>;
 
+  using CallMethodInternalCallback =
+      base::OnceCallback<void(Response* response,
+                              ErrorResponse* error_response)>;
+
   // Struct of data we'll be passing from StartAsyncMethodCall() to
   // OnPendingCallIsCompleteThunk().
   struct OnPendingCallIsCompleteData {
     OnPendingCallIsCompleteData(ObjectProxy* in_object_proxy,
-                                ResponseCallback in_response_callback,
-                                ErrorCallback error_callback,
+                                CallMethodInternalCallback callback,
                                 base::TimeTicks start_time);
     ~OnPendingCallIsCompleteData();
 
     ObjectProxy* object_proxy;
-    ResponseCallback response_callback;
-    ErrorCallback error_callback;
+    CallMethodInternalCallback callback;
     base::TimeTicks start_time;
   };
 
+  // This is a helpr function to implement CallMethod() and
+  // CallMethodWithErrorCallback().
+  void CallMethodInternal(MethodCall* method_call,
+                          int timeout_ms,
+                          CallMethodInternalCallback callback);
+
   // Starts the async method call. This is a helper function to implement
   // CallMethod().
   void StartAsyncMethodCall(int timeout_ms,
                             DBusMessage* request_message,
-                            ResponseCallback response_callback,
-                            ErrorCallback error_callback,
+                            CallMethodInternalCallback callback,
                             base::TimeTicks start_time);
 
   // Called when the pending call is complete.
   void OnPendingCallIsComplete(DBusPendingCall* pending_call,
-                               ResponseCallback response_callback,
-                               ErrorCallback error_callback,
+                               CallMethodInternalCallback callback,
                                base::TimeTicks start_time);
 
-  // Runs the response callback with the given response object.
-  void RunResponseCallback(ResponseCallback response_callback,
-                           ErrorCallback error_callback,
-                           base::TimeTicks start_time,
-                           DBusMessage* response_message);
+  // Runs the CallMethodInternalCallback with the given response object.
+  void RunCallMethodInternalCallback(CallMethodInternalCallback callback,
+                                     base::TimeTicks start_time,
+                                     DBusMessage* response_message);
 
   // Redirects the function call to OnPendingCallIsComplete().
   static void OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call,
@@ -272,11 +277,14 @@
                             const base::StringPiece& error_name,
                             const base::StringPiece& error_message) const;
 
-  // Used as ErrorCallback by CallMethod().
-  void OnCallMethodError(const std::string& interface_name,
-                         const std::string& method_name,
-                         ResponseCallback response_callback,
-                         ErrorResponse* error_response);
+  // Used as CallMethodInternalCallback by CallMethod(). (i.e. ErrorCallback
+  // wasn't provided, hence response_callback will be used for handling the
+  // error response).
+  void OnCallMethod(const std::string& interface_name,
+                    const std::string& method_name,
+                    ResponseCallback response_callback,
+                    Response* response,
+                    ErrorResponse* error_response);
 
   // Adds the match rule to the bus and associate the callback with the signal.
   bool AddMatchRuleWithCallback(const std::string& match_rule,
@@ -310,7 +318,7 @@
 
   // The method table where keys are absolute signal names (i.e. interface
   // name + signal name), and values are lists of the corresponding callbacks.
-  typedef std::map<std::string, std::vector<SignalCallback> > MethodTable;
+  using MethodTable = std::map<std::string, std::vector<SignalCallback>>;
   MethodTable method_table_;
 
   // The callback called when NameOwnerChanged signal is received.