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 <errno.h> | 5 #include <errno.h> |
6 #include <signal.h> | 6 #include <signal.h> |
7 #include <sys/types.h> | 7 #include <sys/types.h> |
8 #include <sys/wait.h> | 8 #include <sys/wait.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 } | 168 } |
169 if (device_port < 0) { | 169 if (device_port < 0) { |
170 // Remove the previously created host controller. | 170 // Remove the previously created host controller. |
171 const std::string controller_key = MakeHostControllerMapKey( | 171 const std::string controller_key = MakeHostControllerMapKey( |
172 adb_port, -device_port); | 172 adb_port, -device_port); |
173 const HostControllerMap::size_type removed_elements = controllers_->erase( | 173 const HostControllerMap::size_type removed_elements = controllers_->erase( |
174 controller_key); | 174 controller_key); |
175 SendMessage( | 175 SendMessage( |
176 !removed_elements ? "ERROR: could not unmap port" : "OK", | 176 !removed_elements ? "ERROR: could not unmap port" : "OK", |
177 client_socket.get()); | 177 client_socket.get()); |
| 178 |
| 179 RemoveAdbPortForDeviceIfNeeded(device_serial); |
178 return; | 180 return; |
179 } | 181 } |
180 if (host_port < 0) { | 182 if (host_port < 0) { |
181 SendMessage("ERROR: missing host port", client_socket.get()); | 183 SendMessage("ERROR: missing host port", client_socket.get()); |
182 return; | 184 return; |
183 } | 185 } |
184 const bool use_dynamic_port_allocation = device_port == 0; | 186 const bool use_dynamic_port_allocation = device_port == 0; |
185 if (!use_dynamic_port_allocation) { | 187 if (!use_dynamic_port_allocation) { |
186 const std::string controller_key = MakeHostControllerMapKey( | 188 const std::string controller_key = MakeHostControllerMapKey( |
187 adb_port, device_port); | 189 adb_port, device_port); |
(...skipping 22 matching lines...) Expand all Loading... |
210 << host_port; | 212 << host_port; |
211 const std::string msg = base::StringPrintf("%d:%d", device_port, host_port); | 213 const std::string msg = base::StringPrintf("%d:%d", device_port, host_port); |
212 if (!SendMessage(msg, client_socket.get())) | 214 if (!SendMessage(msg, client_socket.get())) |
213 return; | 215 return; |
214 host_controller->Start(); | 216 host_controller->Start(); |
215 controllers_->insert( | 217 controllers_->insert( |
216 std::make_pair(MakeHostControllerMapKey(adb_port, device_port), | 218 std::make_pair(MakeHostControllerMapKey(adb_port, device_port), |
217 linked_ptr<HostController>(host_controller.release()))); | 219 linked_ptr<HostController>(host_controller.release()))); |
218 } | 220 } |
219 | 221 |
| 222 void RemoveAdbPortForDeviceIfNeeded(const std::string& device_serial) { |
| 223 base::hash_map<std::string, int>::const_iterator it = |
| 224 device_serial_to_adb_port_map_.find(device_serial); |
| 225 if (it == device_serial_to_adb_port_map_.end()) |
| 226 return; |
| 227 |
| 228 int port = it->second; |
| 229 const std::string prefix = base::StringPrintf("%d:", port); |
| 230 for (HostControllerMap::const_iterator others = controllers_->begin(); |
| 231 others != controllers_->end(); ++others) { |
| 232 if (others->first.find(prefix) == 0U) |
| 233 return; |
| 234 } |
| 235 // No other port is being forwarded to this device: |
| 236 // - Remove it from our internal serial -> adb port map. |
| 237 // - Remove from "adb forward" command. |
| 238 device_serial_to_adb_port_map_.erase(device_serial); |
| 239 const std::string serial_part = device_serial.empty() ? |
| 240 std::string() : std::string("-s ") + device_serial; |
| 241 const std::string command = base::StringPrintf( |
| 242 "adb %s forward --remove tcp:%d", |
| 243 serial_part.c_str(), |
| 244 port); |
| 245 LOG(INFO) << command; |
| 246 const int ret = system(command.c_str()); |
| 247 DCHECK(ret == 0); |
| 248 } |
| 249 |
220 int GetAdbPortForDevice(const std::string& device_serial) { | 250 int GetAdbPortForDevice(const std::string& device_serial) { |
221 base::hash_map<std::string, int>::const_iterator it = | 251 base::hash_map<std::string, int>::const_iterator it = |
222 device_serial_to_adb_port_map_.find(device_serial); | 252 device_serial_to_adb_port_map_.find(device_serial); |
223 if (it != device_serial_to_adb_port_map_.end()) | 253 if (it != device_serial_to_adb_port_map_.end()) |
224 return it->second; | 254 return it->second; |
225 Socket bind_socket; | 255 Socket bind_socket; |
226 CHECK(bind_socket.BindTcp("127.0.0.1", 0)); | 256 CHECK(bind_socket.BindTcp("127.0.0.1", 0)); |
227 const int port = bind_socket.GetPort(); | 257 const int port = bind_socket.GetPort(); |
228 bind_socket.Close(); | 258 bind_socket.Close(); |
229 const std::string serial_part = device_serial.empty() ? | 259 const std::string serial_part = device_serial.empty() ? |
230 std::string() : std::string("-s ") + device_serial; | 260 std::string() : std::string("-s ") + device_serial; |
231 const std::string command = base::StringPrintf( | 261 const std::string command = base::StringPrintf( |
232 "adb %s forward tcp:%d localabstract:chrome_device_forwarder", | 262 "adb %s forward tcp:%d localabstract:chrome_device_forwarder", |
233 device_serial.empty() ? "" : serial_part.c_str(), | 263 serial_part.c_str(), |
234 port); | 264 port); |
235 LOG(INFO) << command; | 265 LOG(INFO) << command; |
236 const int ret = system(command.c_str()); | 266 const int ret = system(command.c_str()); |
237 if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) | 267 if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) |
238 return -1; | 268 return -1; |
239 device_serial_to_adb_port_map_[device_serial] = port; | 269 device_serial_to_adb_port_map_[device_serial] = port; |
240 return port; | 270 return port; |
241 } | 271 } |
242 | 272 |
243 bool SendMessage(const std::string& msg, Socket* client_socket) { | 273 bool SendMessage(const std::string& msg, Socket* client_socket) { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 | 435 |
406 return client_delegate.has_failed() || daemon_delegate.has_failed(); | 436 return client_delegate.has_failed() || daemon_delegate.has_failed(); |
407 } | 437 } |
408 | 438 |
409 } // namespace | 439 } // namespace |
410 } // namespace forwarder2 | 440 } // namespace forwarder2 |
411 | 441 |
412 int main(int argc, char** argv) { | 442 int main(int argc, char** argv) { |
413 return forwarder2::RunHostForwarder(argc, argv); | 443 return forwarder2::RunHostForwarder(argc, argv); |
414 } | 444 } |
OLD | NEW |