| 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 10 matching lines...) Expand all Loading... |
| 21 #include "base/command_line.h" | 21 #include "base/command_line.h" |
| 22 #include "base/compiler_specific.h" | 22 #include "base/compiler_specific.h" |
| 23 #include "base/containers/hash_tables.h" | 23 #include "base/containers/hash_tables.h" |
| 24 #include "base/files/file_path.h" | 24 #include "base/files/file_path.h" |
| 25 #include "base/files/file_util.h" | 25 #include "base/files/file_util.h" |
| 26 #include "base/logging.h" | 26 #include "base/logging.h" |
| 27 #include "base/memory/linked_ptr.h" | 27 #include "base/memory/linked_ptr.h" |
| 28 #include "base/memory/scoped_vector.h" | 28 #include "base/memory/scoped_vector.h" |
| 29 #include "base/memory/weak_ptr.h" | 29 #include "base/memory/weak_ptr.h" |
| 30 #include "base/pickle.h" | 30 #include "base/pickle.h" |
| 31 #include "base/safe_strerror_posix.h" | |
| 32 #include "base/strings/string_number_conversions.h" | 31 #include "base/strings/string_number_conversions.h" |
| 33 #include "base/strings/string_piece.h" | 32 #include "base/strings/string_piece.h" |
| 34 #include "base/strings/string_split.h" | 33 #include "base/strings/string_split.h" |
| 35 #include "base/strings/string_util.h" | 34 #include "base/strings/string_util.h" |
| 36 #include "base/strings/stringprintf.h" | 35 #include "base/strings/stringprintf.h" |
| 37 #include "base/task_runner.h" | 36 #include "base/task_runner.h" |
| 38 #include "base/threading/thread.h" | 37 #include "base/threading/thread.h" |
| 39 #include "tools/android/forwarder2/common.h" | 38 #include "tools/android/forwarder2/common.h" |
| 40 #include "tools/android/forwarder2/daemon.h" | 39 #include "tools/android/forwarder2/daemon.h" |
| 41 #include "tools/android/forwarder2/host_controller.h" | 40 #include "tools/android/forwarder2/host_controller.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 HostControllersManager() | 86 HostControllersManager() |
| 88 : controllers_(new HostControllerMap()), | 87 : controllers_(new HostControllerMap()), |
| 89 has_failed_(false), | 88 has_failed_(false), |
| 90 weak_ptr_factory_(this) { | 89 weak_ptr_factory_(this) { |
| 91 } | 90 } |
| 92 | 91 |
| 93 ~HostControllersManager() { | 92 ~HostControllersManager() { |
| 94 if (!thread_.get()) | 93 if (!thread_.get()) |
| 95 return; | 94 return; |
| 96 // Delete the controllers on the thread they were created on. | 95 // Delete the controllers on the thread they were created on. |
| 97 thread_->message_loop_proxy()->DeleteSoon( | 96 thread_->task_runner()->DeleteSoon(FROM_HERE, controllers_.release()); |
| 98 FROM_HERE, controllers_.release()); | |
| 99 } | 97 } |
| 100 | 98 |
| 101 void HandleRequest(const std::string& adb_path, | 99 void HandleRequest(const std::string& adb_path, |
| 102 const std::string& device_serial, | 100 const std::string& device_serial, |
| 103 int device_port, | 101 int device_port, |
| 104 int host_port, | 102 int host_port, |
| 105 scoped_ptr<Socket> client_socket) { | 103 scoped_ptr<Socket> client_socket) { |
| 106 // Lazy initialize so that the CLI process doesn't get this thread created. | 104 // Lazy initialize so that the CLI process doesn't get this thread created. |
| 107 InitOnce(); | 105 InitOnce(); |
| 108 thread_->message_loop_proxy()->PostTask( | 106 thread_->task_runner()->PostTask( |
| 109 FROM_HERE, | 107 FROM_HERE, |
| 110 base::Bind(&HostControllersManager::HandleRequestOnInternalThread, | 108 base::Bind(&HostControllersManager::HandleRequestOnInternalThread, |
| 111 base::Unretained(this), adb_path, device_serial, device_port, | 109 base::Unretained(this), adb_path, device_serial, device_port, |
| 112 host_port, base::Passed(&client_socket))); | 110 host_port, base::Passed(&client_socket))); |
| 113 } | 111 } |
| 114 | 112 |
| 115 bool has_failed() const { return has_failed_; } | 113 bool has_failed() const { return has_failed_; } |
| 116 | 114 |
| 117 private: | 115 private: |
| 118 typedef base::hash_map< | 116 typedef base::hash_map< |
| (...skipping 18 matching lines...) Expand all Loading... |
| 137 const base::WeakPtr<HostControllersManager>& manager_ptr, | 135 const base::WeakPtr<HostControllersManager>& manager_ptr, |
| 138 scoped_ptr<HostController> host_controller) { | 136 scoped_ptr<HostController> host_controller) { |
| 139 HostController* const controller = host_controller.release(); | 137 HostController* const controller = host_controller.release(); |
| 140 HostControllersManager* const manager = manager_ptr.get(); | 138 HostControllersManager* const manager = manager_ptr.get(); |
| 141 if (!manager) { | 139 if (!manager) { |
| 142 // Note that |controller| is not leaked in this case since the host | 140 // Note that |controller| is not leaked in this case since the host |
| 143 // controllers manager owns the controllers. If the manager was deleted | 141 // controllers manager owns the controllers. If the manager was deleted |
| 144 // then all the controllers (including |controller|) were also deleted. | 142 // then all the controllers (including |controller|) were also deleted. |
| 145 return; | 143 return; |
| 146 } | 144 } |
| 147 DCHECK(manager->thread_->message_loop_proxy()->RunsTasksOnCurrentThread()); | 145 DCHECK(manager->thread_->task_runner()->RunsTasksOnCurrentThread()); |
| 148 // Note that this will delete |controller| which is owned by the map. | 146 // Note that this will delete |controller| which is owned by the map. |
| 149 DeleteRefCountedValueInMap( | 147 DeleteRefCountedValueInMap( |
| 150 MakeHostControllerMapKey( | 148 MakeHostControllerMapKey( |
| 151 controller->adb_port(), controller->device_port()), | 149 controller->adb_port(), controller->device_port()), |
| 152 manager->controllers_.get()); | 150 manager->controllers_.get()); |
| 153 } | 151 } |
| 154 | 152 |
| 155 void HandleRequestOnInternalThread(const std::string& adb_path, | 153 void HandleRequestOnInternalThread(const std::string& adb_path, |
| 156 const std::string& device_serial, | 154 const std::string& device_serial, |
| 157 int device_port, | 155 int device_port, |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 void OnClientConnected(scoped_ptr<Socket> client_socket) override { | 323 void OnClientConnected(scoped_ptr<Socket> client_socket) override { |
| 326 char buf[kBufSize]; | 324 char buf[kBufSize]; |
| 327 const int bytes_read = client_socket->Read(buf, sizeof(buf)); | 325 const int bytes_read = client_socket->Read(buf, sizeof(buf)); |
| 328 if (bytes_read <= 0) { | 326 if (bytes_read <= 0) { |
| 329 if (client_socket->DidReceiveEvent()) | 327 if (client_socket->DidReceiveEvent()) |
| 330 return; | 328 return; |
| 331 PError("Read()"); | 329 PError("Read()"); |
| 332 has_failed_ = true; | 330 has_failed_ = true; |
| 333 return; | 331 return; |
| 334 } | 332 } |
| 335 const Pickle command_pickle(buf, bytes_read); | 333 const base::Pickle command_pickle(buf, bytes_read); |
| 336 PickleIterator pickle_it(command_pickle); | 334 base::PickleIterator pickle_it(command_pickle); |
| 337 std::string device_serial; | 335 std::string device_serial; |
| 338 CHECK(pickle_it.ReadString(&device_serial)); | 336 CHECK(pickle_it.ReadString(&device_serial)); |
| 339 int device_port; | 337 int device_port; |
| 340 if (!pickle_it.ReadInt(&device_port)) { | 338 if (!pickle_it.ReadInt(&device_port)) { |
| 341 client_socket->WriteString("ERROR: missing device port"); | 339 client_socket->WriteString("ERROR: missing device port"); |
| 342 return; | 340 return; |
| 343 } | 341 } |
| 344 int host_port; | 342 int host_port; |
| 345 if (!pickle_it.ReadInt(&host_port)) | 343 if (!pickle_it.ReadInt(&host_port)) |
| 346 host_port = -1; | 344 host_port = -1; |
| 347 controllers_manager_.HandleRequest(adb_path_, device_serial, device_port, | 345 controllers_manager_.HandleRequest(adb_path_, device_serial, device_port, |
| 348 host_port, client_socket.Pass()); | 346 host_port, client_socket.Pass()); |
| 349 } | 347 } |
| 350 | 348 |
| 351 private: | 349 private: |
| 352 std::string adb_path_; | 350 std::string adb_path_; |
| 353 bool has_failed_; | 351 bool has_failed_; |
| 354 HostControllersManager controllers_manager_; | 352 HostControllersManager controllers_manager_; |
| 355 | 353 |
| 356 DISALLOW_COPY_AND_ASSIGN(ServerDelegate); | 354 DISALLOW_COPY_AND_ASSIGN(ServerDelegate); |
| 357 }; | 355 }; |
| 358 | 356 |
| 359 class ClientDelegate : public Daemon::ClientDelegate { | 357 class ClientDelegate : public Daemon::ClientDelegate { |
| 360 public: | 358 public: |
| 361 ClientDelegate(const Pickle& command_pickle) | 359 ClientDelegate(const base::Pickle& command_pickle) |
| 362 : command_pickle_(command_pickle), | 360 : command_pickle_(command_pickle), has_failed_(false) {} |
| 363 has_failed_(false) { | |
| 364 } | |
| 365 | 361 |
| 366 bool has_failed() const { return has_failed_; } | 362 bool has_failed() const { return has_failed_; } |
| 367 | 363 |
| 368 // Daemon::ClientDelegate: | 364 // Daemon::ClientDelegate: |
| 369 void OnDaemonReady(Socket* daemon_socket) override { | 365 void OnDaemonReady(Socket* daemon_socket) override { |
| 370 // Send the forward command to the daemon. | 366 // Send the forward command to the daemon. |
| 371 CHECK_EQ(static_cast<long>(command_pickle_.size()), | 367 CHECK_EQ(static_cast<long>(command_pickle_.size()), |
| 372 daemon_socket->WriteNumBytes(command_pickle_.data(), | 368 daemon_socket->WriteNumBytes(command_pickle_.data(), |
| 373 command_pickle_.size())); | 369 command_pickle_.size())); |
| 374 char buf[kBufSize]; | 370 char buf[kBufSize]; |
| 375 const int bytes_read = daemon_socket->Read( | 371 const int bytes_read = daemon_socket->Read( |
| 376 buf, sizeof(buf) - 1 /* leave space for null terminator */); | 372 buf, sizeof(buf) - 1 /* leave space for null terminator */); |
| 377 CHECK_GT(bytes_read, 0); | 373 CHECK_GT(bytes_read, 0); |
| 378 DCHECK(static_cast<size_t>(bytes_read) < sizeof(buf)); | 374 DCHECK(static_cast<size_t>(bytes_read) < sizeof(buf)); |
| 379 buf[bytes_read] = 0; | 375 buf[bytes_read] = 0; |
| 380 base::StringPiece msg(buf, bytes_read); | 376 base::StringPiece msg(buf, bytes_read); |
| 381 if (msg.starts_with("ERROR")) { | 377 if (msg.starts_with("ERROR")) { |
| 382 LOG(ERROR) << msg; | 378 LOG(ERROR) << msg; |
| 383 has_failed_ = true; | 379 has_failed_ = true; |
| 384 return; | 380 return; |
| 385 } | 381 } |
| 386 printf("%s\n", buf); | 382 printf("%s\n", buf); |
| 387 } | 383 } |
| 388 | 384 |
| 389 private: | 385 private: |
| 390 const Pickle command_pickle_; | 386 const base::Pickle command_pickle_; |
| 391 bool has_failed_; | 387 bool has_failed_; |
| 392 }; | 388 }; |
| 393 | 389 |
| 394 void ExitWithUsage() { | 390 void ExitWithUsage() { |
| 395 std::cerr << "Usage: host_forwarder [options]\n\n" | 391 std::cerr << "Usage: host_forwarder [options]\n\n" |
| 396 "Options:\n" | 392 "Options:\n" |
| 397 " --serial-id=[0-9A-Z]{16}]\n" | 393 " --serial-id=[0-9A-Z]{16}]\n" |
| 398 " --map DEVICE_PORT HOST_PORT\n" | 394 " --map DEVICE_PORT HOST_PORT\n" |
| 399 " --unmap DEVICE_PORT\n" | 395 " --unmap DEVICE_PORT\n" |
| 400 " --adb PATH_TO_ADB\n" | 396 " --adb PATH_TO_ADB\n" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 412 } | 408 } |
| 413 return value; | 409 return value; |
| 414 } | 410 } |
| 415 | 411 |
| 416 int RunHostForwarder(int argc, char** argv) { | 412 int RunHostForwarder(int argc, char** argv) { |
| 417 base::CommandLine::Init(argc, argv); | 413 base::CommandLine::Init(argc, argv); |
| 418 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess(); | 414 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess(); |
| 419 std::string adb_path = "adb"; | 415 std::string adb_path = "adb"; |
| 420 bool kill_server = false; | 416 bool kill_server = false; |
| 421 | 417 |
| 422 Pickle pickle; | 418 base::Pickle pickle; |
| 423 pickle.WriteString( | 419 pickle.WriteString( |
| 424 cmd_line.HasSwitch("serial-id") ? | 420 cmd_line.HasSwitch("serial-id") ? |
| 425 cmd_line.GetSwitchValueASCII("serial-id") : std::string()); | 421 cmd_line.GetSwitchValueASCII("serial-id") : std::string()); |
| 426 | 422 |
| 427 const std::vector<std::string> args = cmd_line.GetArgs(); | 423 const std::vector<std::string> args = cmd_line.GetArgs(); |
| 428 if (cmd_line.HasSwitch("kill-server")) { | 424 if (cmd_line.HasSwitch("kill-server")) { |
| 429 kill_server = true; | 425 kill_server = true; |
| 430 } else if (cmd_line.HasSwitch("unmap")) { | 426 } else if (cmd_line.HasSwitch("unmap")) { |
| 431 if (args.size() != 1) | 427 if (args.size() != 1) |
| 432 ExitWithUsage(); | 428 ExitWithUsage(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 461 | 457 |
| 462 return client_delegate.has_failed() || daemon_delegate.has_failed(); | 458 return client_delegate.has_failed() || daemon_delegate.has_failed(); |
| 463 } | 459 } |
| 464 | 460 |
| 465 } // namespace | 461 } // namespace |
| 466 } // namespace forwarder2 | 462 } // namespace forwarder2 |
| 467 | 463 |
| 468 int main(int argc, char** argv) { | 464 int main(int argc, char** argv) { |
| 469 return forwarder2::RunHostForwarder(argc, argv); | 465 return forwarder2::RunHostForwarder(argc, argv); |
| 470 } | 466 } |
| OLD | NEW |