host_controllers_manager.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLERS_MANAGER_H_
  5. #define TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLERS_MANAGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "base/at_exit.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "tools/android/forwarder2/host_controller.h"
  13. #include "tools/android/forwarder2/socket.h"
  14. namespace forwarder2 {
  15. enum : int {
  16. MAP = 0,
  17. UNMAP = 1,
  18. UNMAP_ALL = 2,
  19. };
  20. // Manages HostController instances. There is one HostController instance for
  21. // each connection being forwarded. Note that forwarding can happen with many
  22. // devices (identified with a serial id).
  23. class HostControllersManager {
  24. public:
  25. explicit HostControllersManager(
  26. base::RepeatingCallback<int()> exit_notifier_fd_callback);
  27. ~HostControllersManager();
  28. void HandleRequest(const std::string& adb_path,
  29. const std::string& device_serial,
  30. int command,
  31. int device_port,
  32. int host_port,
  33. std::unique_ptr<Socket> client_socket);
  34. bool has_failed() const { return has_failed_; }
  35. private:
  36. FRIEND_TEST_ALL_PREFIXES(HostControllersManagerTest, AdbNoExtraFds);
  37. FRIEND_TEST_ALL_PREFIXES(HostControllersManagerTest, AdbArgumentSequence);
  38. using HostControllerMap =
  39. std::unordered_map<std::string, std::unique_ptr<HostController>>;
  40. static std::string MakeHostControllerMapKey(int adb_port, int device_port);
  41. void InitOnce();
  42. // Invoked when a HostController instance reports an error (e.g. due to a
  43. // device connectivity issue). Note that this could be called after the
  44. // controller manager was destroyed which is why a weak pointer is used.
  45. static void DeleteHostController(
  46. const base::WeakPtr<HostControllersManager>& manager_ptr,
  47. std::unique_ptr<HostController> host_controller);
  48. void Map(const std::string& adb_path,
  49. const std::string& device_serial,
  50. int adb_port,
  51. int device_port,
  52. int host_port,
  53. Socket* client_socket);
  54. void Unmap(const std::string& adb_path,
  55. const std::string& device_serial,
  56. int adb_port,
  57. int device_port,
  58. Socket* client_socket);
  59. void UnmapAll(const std::string& adb_path,
  60. const std::string& device_serial,
  61. int adb_port,
  62. Socket* client_socket);
  63. bool Adb(const std::string& adb_path,
  64. const std::string& device_serial,
  65. const std::string& command,
  66. std::string* output_and_error);
  67. void HandleRequestOnInternalThread(const std::string& adb_path,
  68. const std::string& device_serial,
  69. int command,
  70. int device_port,
  71. int host_port,
  72. std::unique_ptr<Socket> client_socket);
  73. void LogExistingControllers(Socket* client_socket);
  74. void RemoveAdbPortForDeviceIfNeeded(const std::string& adb_path,
  75. const std::string& device_serial);
  76. int GetAdbPortForDevice(const std::string adb_path,
  77. const std::string& device_serial);
  78. bool SendMessage(const std::string& msg, Socket* client_socket);
  79. // This is a separate virtual method solely for easy mocking. The default
  80. // implementation is a wrapper around base::GetAppOutputAndError.
  81. virtual bool GetAppOutputAndError(const std::vector<std::string>& argv,
  82. std::string* output);
  83. std::unordered_map<std::string, int> device_serial_to_adb_port_map_;
  84. std::unique_ptr<HostControllerMap> controllers_;
  85. std::unique_ptr<base::AtExitManager>
  86. at_exit_manager_; // Needed by base::Thread.
  87. std::unique_ptr<base::Thread> thread_;
  88. base::RepeatingCallback<int()> exit_notifier_fd_callback_;
  89. bool has_failed_;
  90. base::WeakPtrFactory<HostControllersManager> weak_ptr_factory_;
  91. };
  92. } // namespace forwarder2
  93. #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLERS_MANAGER_H_