fusebox: define template function to call the fusebox server

Calling the server is a frequent operation. To reduce boilerplate
some [1], define a C++ template for calling server methods.

Move the GetFuseBoxServerMethodCall and the template together, to
connect the idea of 1) getting a server D-BUS method_call, and 2)
calling that D-BUS method_call.

[1] Only hides dbus::ObjectProxy::TIMEOUT_USE_DEFAULT which D-BUS
defines as "infinite". In Chrome / ChromeOS D-BUS implementation,
the default timeout is 30 seconds.

BUG=chromium:1244007
TEST=emerge --board=${BOARD} fusebox && deploy

Change-Id: I408ccc534678cf8cf7002e34438726b042977656
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/3350276
Reviewed-by: François Degros <fdegros@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Tested-by: Noel Gordon <noel@chromium.org>
diff --git a/fusebox/main.cc b/fusebox/main.cc
index 50dc1a9..58dcf59 100644
--- a/fusebox/main.cc
+++ b/fusebox/main.cc
@@ -58,11 +58,6 @@
   return *inode_table;
 }
 
-dbus::MethodCall GetFuseBoxServerMethodCall(
-    const char* method = fusebox::kFuseBoxOperationMethod) {
-  return dbus::MethodCall(fusebox::kFuseBoxServiceInterface, method);
-}
-
 }  // namespace
 
 namespace fusebox {
@@ -110,6 +105,18 @@
     }
   }
 
+  static dbus::MethodCall GetFuseBoxServerMethod(
+      const char* method = fusebox::kFuseBoxOperationMethod) {
+    return dbus::MethodCall(fusebox::kFuseBoxServiceInterface, method);
+  }
+
+  template <typename Signature>
+  void CallFuseBoxServerMethod(dbus::MethodCall* method_call,
+                               base::OnceCallback<Signature> callback) {
+    constexpr auto timeout = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
+    dbus_proxy_->CallMethod(method_call, timeout, std::move(callback));
+  }
+
   void Init(void* userdata, struct fuse_conn_info*) override {
     CHECK(userdata);
   }
@@ -127,7 +134,7 @@
       return;
     }
 
-    dbus::MethodCall method = GetFuseBoxServerMethodCall();
+    dbus::MethodCall method = GetFuseBoxServerMethod();
     dbus::MessageWriter writer(&method);
 
     writer.AppendString("stat");
@@ -137,8 +144,7 @@
     auto stat_response =
         base::BindOnce(&FuseBoxClient::StatResponse, base::Unretained(this),
                        std::move(request), node->ino);
-    constexpr auto timeout = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
-    dbus_proxy_->CallMethod(&method, timeout, std::move(stat_response));
+    CallFuseBoxServerMethod(&method, std::move(stat_response));
   }
 
   const double kStatTimeoutSeconds = 5.0;
@@ -183,7 +189,7 @@
       return;
     }
 
-    dbus::MethodCall method = GetFuseBoxServerMethodCall();
+    dbus::MethodCall method = GetFuseBoxServerMethod();
     dbus::MessageWriter writer(&method);
 
     writer.AppendString("stat");
@@ -194,8 +200,7 @@
     auto lookup_response =
         base::BindOnce(&FuseBoxClient::LookupResponse, base::Unretained(this),
                        std::move(request), parent, std::string(name));
-    constexpr auto timeout = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
-    dbus_proxy_->CallMethod(&method, timeout, std::move(lookup_response));
+    CallFuseBoxServerMethod(&method, std::move(lookup_response));
   }
 
   const double kEntryTimeoutSeconds = 5.0;
@@ -283,7 +288,7 @@
       return;
     }
 
-    dbus::MethodCall method = GetFuseBoxServerMethodCall();
+    dbus::MethodCall method = GetFuseBoxServerMethod();
     dbus::MessageWriter writer(&method);
 
     writer.AppendString("readdir");
@@ -294,8 +299,7 @@
     auto readdir_response =
         base::BindOnce(&FuseBoxClient::ReadDirResponse, base::Unretained(this),
                        std::move(request), node->ino, handle);
-    constexpr auto timeout = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
-    dbus_proxy_->CallMethod(&method, timeout, std::move(readdir_response));
+    CallFuseBoxServerMethod(&method, std::move(readdir_response));
   }
 
   void ReadDirResponse(std::unique_ptr<DirEntryRequest> request,