Chromium Code Reviews| Index: tools/android/forwarder2/host_controllers_manager.h |
| diff --git a/tools/android/forwarder2/host_controllers_manager.h b/tools/android/forwarder2/host_controllers_manager.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2b10822b73989e10f218b41af728ce9bb5703bde |
| --- /dev/null |
| +++ b/tools/android/forwarder2/host_controllers_manager.h |
| @@ -0,0 +1,126 @@ |
| +// Copyright 2017 The Chromium 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 TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLERS_MANAGER_H_ |
| +#define TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLERS_MANAGER_H_ |
| + |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/at_exit.h" |
| +#include "base/containers/hash_tables.h" |
| +#include "base/gtest_prod_util.h" |
| +#include "base/memory/linked_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "tools/android/forwarder2/host_controller.h" |
| +#include "tools/android/forwarder2/socket.h" |
| + |
| +namespace forwarder2 { |
| + |
| +enum : int { |
|
Ted C
2017/03/28 23:57:37
does this need to be in the header or can it be in
jbudorick
2017/03/29 01:55:41
host_forwarder_main.cc uses it as well.
|
| + MAP = 0, |
| + UNMAP = 1, |
| + UNMAP_ALL = 2, |
| +}; |
| + |
| +// Manages HostController instances. There is one HostController instance for |
| +// each connection being forwarded. Note that forwarding can happen with many |
| +// devices (identified with a serial id). |
| +class HostControllersManager { |
| + public: |
| + explicit HostControllersManager( |
| + base::Callback<int()> exit_notifier_fd_callback); |
| + ~HostControllersManager(); |
| + void HandleRequest(const std::string& adb_path, |
| + const std::string& device_serial, |
| + int command, |
| + int device_port, |
| + int host_port, |
| + std::unique_ptr<Socket> client_socket); |
| + bool has_failed() const { return has_failed_; } |
| + |
| + void SetAdbPortForDeviceForTesting(const std::string& serial, int adb_port); |
|
Ted C
2017/03/28 23:57:37
at a quick glance the ordering of the .cc file doe
jbudorick
2017/03/29 01:55:41
Both of these are leftover from some intermediate
|
| + void HandleRequestForTesting(const std::string& adb_path, |
| + const std::string& device_serial, |
| + int command, |
| + int device_port, |
| + int host_port, |
| + std::unique_ptr<Socket> client_socket); |
| + |
| + private: |
| + FRIEND_TEST_ALL_PREFIXES(HostControllersManagerTest, AdbNoExtraFds); |
| + FRIEND_TEST_ALL_PREFIXES(HostControllersManagerTest, AdbArgumentSequence); |
| + |
| + typedef base::hash_map<std::string, linked_ptr<HostController>> |
| + HostControllerMap; |
| + |
| + static std::string MakeHostControllerMapKey(int adb_port, int device_port); |
| + |
| + void InitOnce(); |
| + |
| + // Invoked when a HostController instance reports an error (e.g. due to a |
| + // device connectivity issue). Note that this could be called after the |
| + // controller manager was destroyed which is why a weak pointer is used. |
| + static void DeleteHostController( |
| + const base::WeakPtr<HostControllersManager>& manager_ptr, |
| + std::unique_ptr<HostController> host_controller); |
| + |
| + void Map(const std::string& adb_path, |
| + const std::string& device_serial, |
| + int adb_port, |
| + int device_port, |
| + int host_port, |
| + Socket* client_socket); |
| + |
| + void Unmap(const std::string& adb_path, |
| + const std::string& device_serial, |
| + int adb_port, |
| + int device_port, |
| + Socket* client_socket); |
| + |
| + void UnmapAll(const std::string& adb_path, |
| + const std::string& device_serial, |
| + int adb_port, |
| + Socket* client_socket); |
| + |
| + bool Adb(const std::string& adb_path, |
| + const std::string& device_serial, |
| + const std::string& command, |
| + std::string* output_and_error); |
| + |
| + void HandleRequestOnInternalThread(const std::string& adb_path, |
| + const std::string& device_serial, |
| + int command, |
| + int device_port, |
| + int host_port, |
| + std::unique_ptr<Socket> client_socket); |
| + |
| + void LogExistingControllers(Socket* client_socket); |
| + |
| + void RemoveAdbPortForDeviceIfNeeded(const std::string& adb_path, |
| + const std::string& device_serial); |
| + |
| + int GetAdbPortForDevice(const std::string adb_path, |
| + const std::string& device_serial); |
| + |
| + bool SendMessage(const std::string& msg, Socket* client_socket); |
| + |
| + // This is a separate virtual method solely for easy mocking. The default |
| + // implementation is a wrapper around base::GetAppOutputAndError. |
| + virtual bool GetAppOutputAndError(const std::vector<std::string>& argv, |
| + std::string* output); |
| + |
| + base::hash_map<std::string, int> device_serial_to_adb_port_map_; |
| + std::unique_ptr<HostControllerMap> controllers_; |
| + std::unique_ptr<base::AtExitManager> |
| + at_exit_manager_; // Needed by base::Thread. |
| + std::unique_ptr<base::Thread> thread_; |
| + base::Callback<int()> exit_notifier_fd_callback_; |
| + bool has_failed_; |
| + base::WeakPtrFactory<HostControllersManager> weak_ptr_factory_; |
| +}; |
| + |
| +} // namespace forwarder2 |
| + |
| +#endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLERS_MANAGER_H_ |