#ifndef TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_
#define TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_
#include <memory>
#include <string>
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "tools/android/forwarder2/forwarders_manager.h"
#include "tools/android/forwarder2/pipe_notifier.h"
#include "tools/android/forwarder2/self_deleter_helper.h"
#include "tools/android/forwarder2/socket.h"
namespace forwarder2 {
class HostController {
public:
using ErrorCallback =
base::OnceCallback<void(std::unique_ptr<HostController>)>;
static std::unique_ptr<HostController> Create(
const std::string& device_serial,
int device_port,
int host_port,
int adb_port,
int exit_notifier_fd,
ErrorCallback error_callback);
HostController(const HostController&) = delete;
HostController& operator=(const HostController&) = delete;
~HostController();
void Start();
int adb_port() const { return adb_port_; }
int device_port() const { return device_port_; }
private:
HostController(const std::string& device_serial,
int device_port,
int host_port,
int adb_port,
ErrorCallback error_callback,
std::unique_ptr<Socket> adb_control_socket,
std::unique_ptr<PipeNotifier> delete_controller_notifier);
void ReadNextCommandSoon();
void ReadCommandOnInternalThread();
bool StartForwarder(std::unique_ptr<Socket> host_server_data_socket);
void OnInternalThreadError();
void UnmapPortOnDevice();
SelfDeleterHelper<HostController> self_deleter_helper_;
const std::string device_serial_;
const int device_port_;
const int host_port_;
const int adb_port_;
std::unique_ptr<Socket> adb_control_socket_;
std::unique_ptr<PipeNotifier> delete_controller_notifier_;
const scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_;
base::Thread thread_;
ForwardersManager forwarders_manager_;
};
}
#endif