iioservice: Remove UNIX Domain Socket Support from libiioservice_ipc

As we'll use DBus instead of UNIX domain socket to connect iioservice
and clients to Chromium, we should remove UNIX constants and helper
functions in libiioservice_ipc.

BUG=b:162189963
TEST=builds

Change-Id: I78f504756318c3376d63afe62949fa80adc070e1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2331111
Tested-by: Cheng-Hao Yang <chenghaoyang@chromium.org>
Auto-Submit: Cheng-Hao Yang <chenghaoyang@chromium.org>
Commit-Queue: Cheng-Hao Yang <chenghaoyang@chromium.org>
Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-by: Cheng-Hao Yang <chenghaoyang@chromium.org>
diff --git a/iioservice/include/constants.h b/iioservice/include/constants.h
index 0692597..ed468f7 100644
--- a/iioservice/include/constants.h
+++ b/iioservice/include/constants.h
@@ -9,10 +9,7 @@
 
 namespace iioservice {
 
-// Default size of StringPiece for mojo MessagePipe.
-const int kUnixTokenSize = 32;
-const char kIioserviceServerSocketPathString[] = "/run/iioservice/server.sock";
-const char kIioserviceClientSocketPathString[] = "/run/iioservice/client.sock";
+const double kFrequencyEpsilon = 0.001;  // Hz
 
 }  // namespace iioservice
 
diff --git a/iioservice/libiioservice_ipc/BUILD.gn b/iioservice/libiioservice_ipc/BUILD.gn
index f8d6b9c..1fe0658 100644
--- a/iioservice/libiioservice_ipc/BUILD.gn
+++ b/iioservice/libiioservice_ipc/BUILD.gn
@@ -19,10 +19,7 @@
 }
 
 shared_library("libiioservice_ipc") {
-  sources = [
-    "//iioservice/libiioservice_ipc/ipc_util.cc",
-    "//iioservice/libiioservice_ipc/sensor_client.cc",
-  ]
+  sources = [ "//iioservice/libiioservice_ipc/sensor_client.cc" ]
   output_name = "libiioservice_ipc"
   include_dirs = [
     "//iioservice",
diff --git a/iioservice/libiioservice_ipc/ipc_util.cc b/iioservice/libiioservice_ipc/ipc_util.cc
deleted file mode 100644
index 5d58b3d..0000000
--- a/iioservice/libiioservice_ipc/ipc_util.cc
+++ /dev/null
@@ -1,71 +0,0 @@
-// Copyright 2020 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "iioservice/libiioservice_ipc/ipc_util.h"
-
-#include <fcntl.h>
-
-#include <utility>
-#include <vector>
-
-#include <base/files/scoped_file.h>
-#include <base/posix/eintr_wrapper.h>
-#include <mojo/public/cpp/platform/named_platform_channel.h>
-#include <mojo/public/cpp/platform/platform_channel.h>
-#include <mojo/public/cpp/platform/socket_utils_posix.h>
-#include <mojo/public/cpp/system/invitation.h>
-
-#include "iioservice/include/common.h"
-#include "iioservice/include/constants.h"
-
-namespace cros {
-
-namespace iioservice {
-
-MojoResult CreateMojoChannelToParentByUnixDomainSocket(
-    const std::string& path, mojo::ScopedMessagePipeHandle* child_pipe) {
-  mojo::PlatformChannelEndpoint endpoint =
-      mojo::NamedPlatformChannel::ConnectToServer(path);
-  base::ScopedFD client_socket_fd = endpoint.TakePlatformHandle().TakeFD();
-
-  if (!client_socket_fd.is_valid()) {
-    LOGF(WARNING) << "Failed to connect to " << path;
-    return MOJO_RESULT_INTERNAL;
-  }
-
-  // Set socket to blocking
-  int flags = HANDLE_EINTR(fcntl(client_socket_fd.get(), F_GETFL));
-  if (flags == -1) {
-    PLOGF(ERROR) << "fcntl(F_GETFL) failed:";
-    return MOJO_RESULT_INTERNAL;
-  }
-  if (HANDLE_EINTR(
-          fcntl(client_socket_fd.get(), F_SETFL, flags & ~O_NONBLOCK)) == -1) {
-    PLOGF(ERROR) << "fcntl(F_SETFL) failed:";
-    return MOJO_RESULT_INTERNAL;
-  }
-
-  char token[kUnixTokenSize] = {};
-  std::vector<base::ScopedFD> platformHandles;
-  ssize_t result =
-      mojo::SocketRecvmsg(client_socket_fd.get(), token, sizeof(token),
-                          &platformHandles, true /* block */);
-  if (result != kUnixTokenSize) {
-    LOGF(ERROR) << "Unexpected read size: " << result;
-    return MOJO_RESULT_INTERNAL;
-  }
-  mojo::IncomingInvitation invitation =
-      mojo::IncomingInvitation::Accept(mojo::PlatformChannelEndpoint(
-          mojo::PlatformHandle(std::move(platformHandles.back()))));
-  platformHandles.pop_back();
-
-  *child_pipe =
-      invitation.ExtractMessagePipe(std::string(token, kUnixTokenSize));
-
-  return MOJO_RESULT_OK;
-}
-
-}  // namespace iioservice
-
-}  // namespace cros
diff --git a/iioservice/libiioservice_ipc/ipc_util.h b/iioservice/libiioservice_ipc/ipc_util.h
deleted file mode 100644
index d26eabd..0000000
--- a/iioservice/libiioservice_ipc/ipc_util.h
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2020 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IIOSERVICE_LIBIIOSERVICE_IPC_IPC_UTIL_H_
-#define IIOSERVICE_LIBIIOSERVICE_IPC_IPC_UTIL_H_
-
-#include <string>
-
-#include <mojo/public/cpp/system/message_pipe.h>
-
-#include "iioservice/include/export.h"
-#include "mojo/sensor.mojom.h"
-
-namespace base {
-class FilePath;
-}  // namespace base
-
-namespace cros {
-
-namespace iioservice {
-
-IIOSERVICE_EXPORT MojoResult CreateMojoChannelToParentByUnixDomainSocket(
-    const std::string& path, mojo::ScopedMessagePipeHandle* child_pipe);
-
-}  // namespace iioservice
-
-}  // namespace cros
-
-#endif  // IIOSERVICE_LIBIIOSERVICE_IPC_IPC_UTIL_H_
diff --git a/iioservice/libiioservice_ipc/sensor_client.cc b/iioservice/libiioservice_ipc/sensor_client.cc
index b0b8f23..7c1747a 100644
--- a/iioservice/libiioservice_ipc/sensor_client.cc
+++ b/iioservice/libiioservice_ipc/sensor_client.cc
@@ -11,7 +11,6 @@
 
 #include "iioservice/include/common.h"
 #include "iioservice/include/constants.h"
-#include "iioservice/libiioservice_ipc/ipc_util.h"
 
 namespace cros {
 
@@ -34,12 +33,13 @@
 // static
 SensorClient::ScopedSensorClient SensorClient::Create(
     scoped_refptr<base::SequencedTaskRunner> ipc_task_runner,
+    mojo::PendingReceiver<mojom::SensorHalClient> pending_receiver,
     SensorServiceReceivedCallback sensor_service_received_callback,
-    InitOnFailureCallback init_on_failure_callback) {
+    ClientOnFailureCallback client_on_failure_callback) {
   ScopedSensorClient client(
-      new SensorClient(ipc_task_runner,
+      new SensorClient(ipc_task_runner, std::move(pending_receiver),
                        std::move(sensor_service_received_callback),
-                       std::move(init_on_failure_callback)),
+                       std::move(client_on_failure_callback)),
       SensorClientDeleter);
 
   return client;
@@ -56,39 +56,15 @@
 
 SensorClient::SensorClient(
     scoped_refptr<base::SequencedTaskRunner> ipc_task_runner,
+    mojo::PendingReceiver<mojom::SensorHalClient> pending_receiver,
     SensorServiceReceivedCallback sensor_service_received_callback,
-    InitOnFailureCallback init_on_failure_callback)
+    ClientOnFailureCallback client_on_failure_callback)
     : ipc_task_runner_(ipc_task_runner),
       receiver_(this),
       sensor_service_received_callback_(
           std::move(sensor_service_received_callback)),
-      init_on_failure_callback_(std::move(init_on_failure_callback)) {
-  ipc_task_runner_->PostTask(
-      FROM_HERE,
-      base::BindOnce(&SensorClient::InitOnThread, weak_factory_.GetWeakPtr()));
-}
-
-void SensorClient::InitOnThread() {
-  DCHECK(ipc_task_runner_->RunsTasksInCurrentSequence());
-
-  mojo::ScopedMessagePipeHandle child_pipe;
-  MojoResult res = CreateMojoChannelToParentByUnixDomainSocket(
-      kIioserviceClientSocketPathString, &child_pipe);
-  if (res != MOJO_RESULT_OK) {
-    LOGF(ERROR) << "Failed to create mojo channel to broker";
-    std::move(init_on_failure_callback_).Run();  // -ENODEV
-    return;
-  }
-
-  receiver_.Bind(
-      mojo::PendingReceiver<mojom::SensorHalClient>(std::move(child_pipe)));
-
-  if (!receiver_.is_bound()) {
-    LOGF(ERROR) << "Failed to connect to broker";
-    std::move(init_on_failure_callback_).Run();  // -ENODEV
-    return;
-  }
-
+      client_on_failure_callback_(std::move(client_on_failure_callback)) {
+  receiver_.Bind(std::move(pending_receiver));
   receiver_.set_disconnect_handler(
       base::BindOnce(&SensorClient::OnClientError, base::Unretained(this)));
   LOGF(INFO) << "Connected to broker";
@@ -99,7 +75,7 @@
 
   LOGF(ERROR) << "Connection to broker lost";
   receiver_.reset();
-  InitOnThread();
+  client_on_failure_callback_.Run();
 }
 
 }  // namespace iioservice
diff --git a/iioservice/libiioservice_ipc/sensor_client.h b/iioservice/libiioservice_ipc/sensor_client.h
index 32041be..7b0bb15 100644
--- a/iioservice/libiioservice_ipc/sensor_client.h
+++ b/iioservice/libiioservice_ipc/sensor_client.h
@@ -28,7 +28,7 @@
  public:
   using SensorServiceReceivedCallback =
       base::RepeatingCallback<void(mojo::PendingRemote<mojom::SensorService>)>;
-  using InitOnFailureCallback = base::OnceCallback<void()>;
+  using ClientOnFailureCallback = base::RepeatingCallback<void()>;
 
   static void SensorClientDeleter(SensorClient* client);
   using ScopedSensorClient =
@@ -39,8 +39,9 @@
   // callback to abort when an error occurs.
   static ScopedSensorClient Create(
       scoped_refptr<base::SequencedTaskRunner> ipc_task_runner,
+      mojo::PendingReceiver<mojom::SensorHalClient> pending_receiver,
       SensorServiceReceivedCallback sensor_service_received_callback,
-      InitOnFailureCallback init_on_failure_callback);
+      ClientOnFailureCallback client_on_failure_callback);
 
   // Implementation of cros::mojom::SensorHalClient. Called by sensor HAL
   // dispatcher to provide the SensorService interface.
@@ -49,12 +50,9 @@
 
  private:
   SensorClient(scoped_refptr<base::SequencedTaskRunner> ipc_task_runner,
+               mojo::PendingReceiver<mojom::SensorHalClient> pending_receiver,
                SensorServiceReceivedCallback sensor_service_received_callback,
-               InitOnFailureCallback init_on_failure_callback);
-
-  void InitOnThread();
-  // void RegisterClient(RegisterClientCallback register_client_callback);
-  void RegisterClient();
+               ClientOnFailureCallback client_on_failure_callback);
 
   void OnClientError();
 
@@ -62,7 +60,7 @@
 
   mojo::Receiver<mojom::SensorHalClient> receiver_;
   SensorServiceReceivedCallback sensor_service_received_callback_;
-  InitOnFailureCallback init_on_failure_callback_;
+  ClientOnFailureCallback client_on_failure_callback_;
 
   base::WeakPtrFactory<SensorClient> weak_factory_{this};
 };