| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/android/forwarder2/host_controller.h" | 5 #include "tools/android/forwarder2/host_controller.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "tools/android/forwarder2/command.h" | 15 #include "tools/android/forwarder2/command.h" |
| 15 #include "tools/android/forwarder2/forwarder.h" | 16 #include "tools/android/forwarder2/forwarder.h" |
| 16 #include "tools/android/forwarder2/socket.h" | 17 #include "tools/android/forwarder2/socket.h" |
| 17 | 18 |
| 18 namespace forwarder2 { | 19 namespace forwarder2 { |
| 19 | 20 |
| 20 // static | 21 // static |
| 21 scoped_ptr<HostController> HostController::Create( | 22 scoped_ptr<HostController> HostController::Create( |
| 22 int device_port, | 23 int device_port, |
| 23 int host_port, | 24 int host_port, |
| 24 int adb_port, | 25 int adb_port, |
| 25 int exit_notifier_fd, | 26 int exit_notifier_fd, |
| 26 const ErrorCallback& error_callback) { | 27 const ErrorCallback& error_callback) { |
| 27 scoped_ptr<HostController> host_controller; | 28 scoped_ptr<HostController> host_controller; |
| 28 scoped_ptr<PipeNotifier> delete_controller_notifier(new PipeNotifier()); | 29 scoped_ptr<PipeNotifier> delete_controller_notifier(new PipeNotifier()); |
| 29 scoped_ptr<Socket> adb_control_socket(new Socket()); | 30 scoped_ptr<Socket> adb_control_socket(new Socket()); |
| 30 adb_control_socket->AddEventFd(exit_notifier_fd); | 31 adb_control_socket->AddEventFd(exit_notifier_fd); |
| 31 adb_control_socket->AddEventFd(delete_controller_notifier->receiver_fd()); | 32 adb_control_socket->AddEventFd(delete_controller_notifier->receiver_fd()); |
| 32 if (!adb_control_socket->ConnectTcp(std::string(), adb_port)) { | 33 if (!adb_control_socket->ConnectTcp(std::string(), adb_port)) { |
| 33 LOG(ERROR) << "Could not connect HostController socket on port: " | 34 LOG(ERROR) << "Could not connect HostController socket on port: " |
| 34 << adb_port; | 35 << adb_port; |
| 35 return host_controller.Pass(); | 36 return host_controller; |
| 36 } | 37 } |
| 37 // Send the command to the device start listening to the "device_forward_port" | 38 // Send the command to the device start listening to the "device_forward_port" |
| 38 bool send_command_success = SendCommand( | 39 bool send_command_success = SendCommand( |
| 39 command::LISTEN, device_port, adb_control_socket.get()); | 40 command::LISTEN, device_port, adb_control_socket.get()); |
| 40 CHECK(send_command_success); | 41 CHECK(send_command_success); |
| 41 int device_port_allocated; | 42 int device_port_allocated; |
| 42 command::Type command; | 43 command::Type command; |
| 43 if (!ReadCommand( | 44 if (!ReadCommand( |
| 44 adb_control_socket.get(), &device_port_allocated, &command) || | 45 adb_control_socket.get(), &device_port_allocated, &command) || |
| 45 command != command::BIND_SUCCESS) { | 46 command != command::BIND_SUCCESS) { |
| 46 LOG(ERROR) << "Device binding error using port " << device_port; | 47 LOG(ERROR) << "Device binding error using port " << device_port; |
| 47 return host_controller.Pass(); | 48 return host_controller; |
| 48 } | 49 } |
| 49 host_controller.reset( | 50 host_controller.reset(new HostController( |
| 50 new HostController( | 51 device_port_allocated, host_port, adb_port, exit_notifier_fd, |
| 51 device_port_allocated, host_port, adb_port, exit_notifier_fd, | 52 error_callback, std::move(adb_control_socket), |
| 52 error_callback, adb_control_socket.Pass(), | 53 std::move(delete_controller_notifier))); |
| 53 delete_controller_notifier.Pass())); | 54 return host_controller; |
| 54 return host_controller.Pass(); | |
| 55 } | 55 } |
| 56 | 56 |
| 57 HostController::~HostController() { | 57 HostController::~HostController() { |
| 58 DCHECK(deletion_task_runner_->RunsTasksOnCurrentThread()); | 58 DCHECK(deletion_task_runner_->RunsTasksOnCurrentThread()); |
| 59 delete_controller_notifier_->Notify(); | 59 delete_controller_notifier_->Notify(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void HostController::Start() { | 62 void HostController::Start() { |
| 63 thread_.Start(); | 63 thread_.Start(); |
| 64 ReadNextCommandSoon(); | 64 ReadNextCommandSoon(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 HostController::HostController( | 67 HostController::HostController( |
| 68 int device_port, | 68 int device_port, |
| 69 int host_port, | 69 int host_port, |
| 70 int adb_port, | 70 int adb_port, |
| 71 int exit_notifier_fd, | 71 int exit_notifier_fd, |
| 72 const ErrorCallback& error_callback, | 72 const ErrorCallback& error_callback, |
| 73 scoped_ptr<Socket> adb_control_socket, | 73 scoped_ptr<Socket> adb_control_socket, |
| 74 scoped_ptr<PipeNotifier> delete_controller_notifier) | 74 scoped_ptr<PipeNotifier> delete_controller_notifier) |
| 75 : self_deleter_helper_(this, error_callback), | 75 : self_deleter_helper_(this, error_callback), |
| 76 device_port_(device_port), | 76 device_port_(device_port), |
| 77 host_port_(host_port), | 77 host_port_(host_port), |
| 78 adb_port_(adb_port), | 78 adb_port_(adb_port), |
| 79 global_exit_notifier_fd_(exit_notifier_fd), | 79 global_exit_notifier_fd_(exit_notifier_fd), |
| 80 adb_control_socket_(adb_control_socket.Pass()), | 80 adb_control_socket_(std::move(adb_control_socket)), |
| 81 delete_controller_notifier_(delete_controller_notifier.Pass()), | 81 delete_controller_notifier_(std::move(delete_controller_notifier)), |
| 82 deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 82 deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 83 thread_("HostControllerThread") { | 83 thread_("HostControllerThread") {} |
| 84 } | |
| 85 | 84 |
| 86 void HostController::ReadNextCommandSoon() { | 85 void HostController::ReadNextCommandSoon() { |
| 87 thread_.task_runner()->PostTask( | 86 thread_.task_runner()->PostTask( |
| 88 FROM_HERE, | 87 FROM_HERE, |
| 89 base::Bind(&HostController::ReadCommandOnInternalThread, | 88 base::Bind(&HostController::ReadCommandOnInternalThread, |
| 90 base::Unretained(this))); | 89 base::Unretained(this))); |
| 91 } | 90 } |
| 92 | 91 |
| 93 void HostController::ReadCommandOnInternalThread() { | 92 void HostController::ReadCommandOnInternalThread() { |
| 94 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) { | 93 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 110 // re-try later. | 109 // re-try later. |
| 111 ReadNextCommandSoon(); | 110 ReadNextCommandSoon(); |
| 112 return; | 111 return; |
| 113 } | 112 } |
| 114 OnInternalThreadError(); | 113 OnInternalThreadError(); |
| 115 return; | 114 return; |
| 116 } | 115 } |
| 117 LOG(INFO) << "Will send HOST_SERVER_SUCCESS: " << host_port_; | 116 LOG(INFO) << "Will send HOST_SERVER_SUCCESS: " << host_port_; |
| 118 SendCommand( | 117 SendCommand( |
| 119 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get()); | 118 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get()); |
| 120 StartForwarder(host_server_data_socket.Pass()); | 119 StartForwarder(std::move(host_server_data_socket)); |
| 121 ReadNextCommandSoon(); | 120 ReadNextCommandSoon(); |
| 122 } | 121 } |
| 123 | 122 |
| 124 void HostController::StartForwarder( | 123 void HostController::StartForwarder( |
| 125 scoped_ptr<Socket> host_server_data_socket) { | 124 scoped_ptr<Socket> host_server_data_socket) { |
| 126 scoped_ptr<Socket> adb_data_socket(new Socket()); | 125 scoped_ptr<Socket> adb_data_socket(new Socket()); |
| 127 if (!adb_data_socket->ConnectTcp("", adb_port_)) { | 126 if (!adb_data_socket->ConnectTcp("", adb_port_)) { |
| 128 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; | 127 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; |
| 129 OnInternalThreadError(); | 128 OnInternalThreadError(); |
| 130 return; | 129 return; |
| 131 } | 130 } |
| 132 // Open the Adb data connection, and send a command with the | 131 // Open the Adb data connection, and send a command with the |
| 133 // |device_forward_port| as a way for the device to identify the connection. | 132 // |device_forward_port| as a way for the device to identify the connection. |
| 134 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get()); | 133 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get()); |
| 135 | 134 |
| 136 // Check that the device received the new Adb Data Connection. Note that this | 135 // Check that the device received the new Adb Data Connection. Note that this |
| 137 // check is done through the |adb_control_socket_| that is handled in the | 136 // check is done through the |adb_control_socket_| that is handled in the |
| 138 // DeviceListener thread just after the call to WaitForAdbDataSocket(). | 137 // DeviceListener thread just after the call to WaitForAdbDataSocket(). |
| 139 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, | 138 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, |
| 140 adb_control_socket_.get())) { | 139 adb_control_socket_.get())) { |
| 141 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; | 140 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; |
| 142 OnInternalThreadError(); | 141 OnInternalThreadError(); |
| 143 return; | 142 return; |
| 144 } | 143 } |
| 145 forwarders_manager_.CreateAndStartNewForwarder( | 144 forwarders_manager_.CreateAndStartNewForwarder( |
| 146 host_server_data_socket.Pass(), adb_data_socket.Pass()); | 145 std::move(host_server_data_socket), std::move(adb_data_socket)); |
| 147 } | 146 } |
| 148 | 147 |
| 149 void HostController::OnInternalThreadError() { | 148 void HostController::OnInternalThreadError() { |
| 150 UnmapPortOnDevice(); | 149 UnmapPortOnDevice(); |
| 151 self_deleter_helper_.MaybeSelfDeleteSoon(); | 150 self_deleter_helper_.MaybeSelfDeleteSoon(); |
| 152 } | 151 } |
| 153 | 152 |
| 154 void HostController::UnmapPortOnDevice() { | 153 void HostController::UnmapPortOnDevice() { |
| 155 Socket socket; | 154 Socket socket; |
| 156 if (!socket.ConnectTcp("", adb_port_)) { | 155 if (!socket.ConnectTcp("", adb_port_)) { |
| 157 LOG(ERROR) << "Could not connect to device on port " << adb_port_; | 156 LOG(ERROR) << "Could not connect to device on port " << adb_port_; |
| 158 return; | 157 return; |
| 159 } | 158 } |
| 160 if (!SendCommand(command::UNLISTEN, device_port_, &socket)) { | 159 if (!SendCommand(command::UNLISTEN, device_port_, &socket)) { |
| 161 LOG(ERROR) << "Could not send unmap command for port " << device_port_; | 160 LOG(ERROR) << "Could not send unmap command for port " << device_port_; |
| 162 return; | 161 return; |
| 163 } | 162 } |
| 164 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) { | 163 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) { |
| 165 LOG(ERROR) << "Unamp command failed for port " << device_port_; | 164 LOG(ERROR) << "Unamp command failed for port " << device_port_; |
| 166 return; | 165 return; |
| 167 } | 166 } |
| 168 } | 167 } |
| 169 | 168 |
| 170 } // namespace forwarder2 | 169 } // namespace forwarder2 |
| OLD | NEW |