Chromium Code Reviews| 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 { | |
|
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.
| |
| 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 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
| |
| 44 void HandleRequestForTesting(const std::string& adb_path, | |
| 45 const std::string& device_serial, | |
| 46 int command, | |
| 47 int device_port, | |
| 48 int host_port, | |
| 49 std::unique_ptr<Socket> client_socket); | |
| 50 | |
| 51 private: | |
| 52 FRIEND_TEST_ALL_PREFIXES(HostControllersManagerTest, AdbNoExtraFds); | |
| 53 FRIEND_TEST_ALL_PREFIXES(HostControllersManagerTest, AdbArgumentSequence); | |
| 54 | |
| 55 typedef base::hash_map<std::string, linked_ptr<HostController>> | |
| 56 HostControllerMap; | |
| 57 | |
| 58 static std::string MakeHostControllerMapKey(int adb_port, int device_port); | |
| 59 | |
| 60 void InitOnce(); | |
| 61 | |
| 62 // Invoked when a HostController instance reports an error (e.g. due to a | |
| 63 // device connectivity issue). Note that this could be called after the | |
| 64 // controller manager was destroyed which is why a weak pointer is used. | |
| 65 static void DeleteHostController( | |
| 66 const base::WeakPtr<HostControllersManager>& manager_ptr, | |
| 67 std::unique_ptr<HostController> host_controller); | |
| 68 | |
| 69 void Map(const std::string& adb_path, | |
| 70 const std::string& device_serial, | |
| 71 int adb_port, | |
| 72 int device_port, | |
| 73 int host_port, | |
| 74 Socket* client_socket); | |
| 75 | |
| 76 void Unmap(const std::string& adb_path, | |
| 77 const std::string& device_serial, | |
| 78 int adb_port, | |
| 79 int device_port, | |
| 80 Socket* client_socket); | |
| 81 | |
| 82 void UnmapAll(const std::string& adb_path, | |
| 83 const std::string& device_serial, | |
| 84 int adb_port, | |
| 85 Socket* client_socket); | |
| 86 | |
| 87 bool Adb(const std::string& adb_path, | |
| 88 const std::string& device_serial, | |
| 89 const std::string& command, | |
| 90 std::string* output_and_error); | |
| 91 | |
| 92 void HandleRequestOnInternalThread(const std::string& adb_path, | |
| 93 const std::string& device_serial, | |
| 94 int command, | |
| 95 int device_port, | |
| 96 int host_port, | |
| 97 std::unique_ptr<Socket> client_socket); | |
| 98 | |
| 99 void LogExistingControllers(Socket* client_socket); | |
| 100 | |
| 101 void RemoveAdbPortForDeviceIfNeeded(const std::string& adb_path, | |
| 102 const std::string& device_serial); | |
| 103 | |
| 104 int GetAdbPortForDevice(const std::string adb_path, | |
| 105 const std::string& device_serial); | |
| 106 | |
| 107 bool SendMessage(const std::string& msg, Socket* client_socket); | |
| 108 | |
| 109 // This is a separate virtual method solely for easy mocking. The default | |
| 110 // implementation is a wrapper around base::GetAppOutputAndError. | |
| 111 virtual bool GetAppOutputAndError(const std::vector<std::string>& argv, | |
| 112 std::string* output); | |
| 113 | |
| 114 base::hash_map<std::string, int> device_serial_to_adb_port_map_; | |
| 115 std::unique_ptr<HostControllerMap> controllers_; | |
| 116 std::unique_ptr<base::AtExitManager> | |
| 117 at_exit_manager_; // Needed by base::Thread. | |
| 118 std::unique_ptr<base::Thread> thread_; | |
| 119 base::Callback<int()> exit_notifier_fd_callback_; | |
| 120 bool has_failed_; | |
| 121 base::WeakPtrFactory<HostControllersManager> weak_ptr_factory_; | |
| 122 }; | |
| 123 | |
| 124 } // namespace forwarder2 | |
| 125 | |
| 126 #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLERS_MANAGER_H_ | |
| OLD | NEW |