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