| 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/device_controller.h" | 5 #include "tools/android/forwarder2/device_controller.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 15 #include "tools/android/forwarder2/command.h" | 15 #include "tools/android/forwarder2/command.h" |
| 16 #include "tools/android/forwarder2/device_listener.h" | 16 #include "tools/android/forwarder2/device_listener.h" |
| 17 #include "tools/android/forwarder2/socket.h" | 17 #include "tools/android/forwarder2/socket.h" |
| 18 #include "tools/android/forwarder2/util.h" | 18 #include "tools/android/forwarder2/util.h" |
| 19 | 19 |
| 20 namespace forwarder2 { | 20 namespace forwarder2 { |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 scoped_ptr<DeviceController> DeviceController::Create( | 23 std::unique_ptr<DeviceController> DeviceController::Create( |
| 24 const std::string& adb_unix_socket, | 24 const std::string& adb_unix_socket, |
| 25 int exit_notifier_fd) { | 25 int exit_notifier_fd) { |
| 26 scoped_ptr<DeviceController> device_controller; | 26 std::unique_ptr<DeviceController> device_controller; |
| 27 scoped_ptr<Socket> host_socket(new Socket()); | 27 std::unique_ptr<Socket> host_socket(new Socket()); |
| 28 if (!host_socket->BindUnix(adb_unix_socket)) { | 28 if (!host_socket->BindUnix(adb_unix_socket)) { |
| 29 PLOG(ERROR) << "Could not BindAndListen DeviceController socket on port " | 29 PLOG(ERROR) << "Could not BindAndListen DeviceController socket on port " |
| 30 << adb_unix_socket << ": "; | 30 << adb_unix_socket << ": "; |
| 31 return device_controller; | 31 return device_controller; |
| 32 } | 32 } |
| 33 LOG(INFO) << "Listening on Unix Domain Socket " << adb_unix_socket; | 33 LOG(INFO) << "Listening on Unix Domain Socket " << adb_unix_socket; |
| 34 device_controller.reset( | 34 device_controller.reset( |
| 35 new DeviceController(std::move(host_socket), exit_notifier_fd)); | 35 new DeviceController(std::move(host_socket), exit_notifier_fd)); |
| 36 return device_controller; | 36 return device_controller; |
| 37 } | 37 } |
| 38 | 38 |
| 39 DeviceController::~DeviceController() { | 39 DeviceController::~DeviceController() { |
| 40 DCHECK(construction_task_runner_->RunsTasksOnCurrentThread()); | 40 DCHECK(construction_task_runner_->RunsTasksOnCurrentThread()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void DeviceController::Start() { | 43 void DeviceController::Start() { |
| 44 AcceptHostCommandSoon(); | 44 AcceptHostCommandSoon(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 DeviceController::DeviceController(scoped_ptr<Socket> host_socket, | 47 DeviceController::DeviceController(std::unique_ptr<Socket> host_socket, |
| 48 int exit_notifier_fd) | 48 int exit_notifier_fd) |
| 49 : host_socket_(std::move(host_socket)), | 49 : host_socket_(std::move(host_socket)), |
| 50 exit_notifier_fd_(exit_notifier_fd), | 50 exit_notifier_fd_(exit_notifier_fd), |
| 51 construction_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 51 construction_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 52 weak_ptr_factory_(this) { | 52 weak_ptr_factory_(this) { |
| 53 host_socket_->AddEventFd(exit_notifier_fd); | 53 host_socket_->AddEventFd(exit_notifier_fd); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void DeviceController::AcceptHostCommandSoon() { | 56 void DeviceController::AcceptHostCommandSoon() { |
| 57 base::ThreadTaskRunnerHandle::Get()->PostTask( | 57 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 58 FROM_HERE, base::Bind(&DeviceController::AcceptHostCommandInternal, | 58 FROM_HERE, base::Bind(&DeviceController::AcceptHostCommandInternal, |
| 59 base::Unretained(this))); | 59 base::Unretained(this))); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void DeviceController::AcceptHostCommandInternal() { | 62 void DeviceController::AcceptHostCommandInternal() { |
| 63 scoped_ptr<Socket> socket(new Socket); | 63 std::unique_ptr<Socket> socket(new Socket); |
| 64 if (!host_socket_->Accept(socket.get())) { | 64 if (!host_socket_->Accept(socket.get())) { |
| 65 if (!host_socket_->DidReceiveEvent()) | 65 if (!host_socket_->DidReceiveEvent()) |
| 66 PLOG(ERROR) << "Could not Accept DeviceController socket"; | 66 PLOG(ERROR) << "Could not Accept DeviceController socket"; |
| 67 else | 67 else |
| 68 LOG(INFO) << "Received exit notification"; | 68 LOG(INFO) << "Received exit notification"; |
| 69 return; | 69 return; |
| 70 } | 70 } |
| 71 base::ScopedClosureRunner accept_next_client( | 71 base::ScopedClosureRunner accept_next_client( |
| 72 base::Bind(&DeviceController::AcceptHostCommandSoon, | 72 base::Bind(&DeviceController::AcceptHostCommandSoon, |
| 73 base::Unretained(this))); | 73 base::Unretained(this))); |
| 74 // So that |socket| doesn't block on read if it has notifications. | 74 // So that |socket| doesn't block on read if it has notifications. |
| 75 socket->AddEventFd(exit_notifier_fd_); | 75 socket->AddEventFd(exit_notifier_fd_); |
| 76 int port; | 76 int port; |
| 77 command::Type command; | 77 command::Type command; |
| 78 if (!ReadCommand(socket.get(), &port, &command)) { | 78 if (!ReadCommand(socket.get(), &port, &command)) { |
| 79 LOG(ERROR) << "Invalid command received."; | 79 LOG(ERROR) << "Invalid command received."; |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 const ListenersMap::iterator listener_it = listeners_.find(port); | 82 const ListenersMap::iterator listener_it = listeners_.find(port); |
| 83 DeviceListener* const listener = listener_it == listeners_.end() | 83 DeviceListener* const listener = listener_it == listeners_.end() |
| 84 ? static_cast<DeviceListener*>(NULL) : listener_it->second.get(); | 84 ? static_cast<DeviceListener*>(NULL) : listener_it->second.get(); |
| 85 switch (command) { | 85 switch (command) { |
| 86 case command::LISTEN: { | 86 case command::LISTEN: { |
| 87 if (listener != NULL) { | 87 if (listener != NULL) { |
| 88 LOG(WARNING) << "Already forwarding port " << port | 88 LOG(WARNING) << "Already forwarding port " << port |
| 89 << ". Attempting to restart the listener.\n"; | 89 << ". Attempting to restart the listener.\n"; |
| 90 DeleteRefCountedValueInMapFromIterator(listener_it, &listeners_); | 90 DeleteRefCountedValueInMapFromIterator(listener_it, &listeners_); |
| 91 } | 91 } |
| 92 scoped_ptr<DeviceListener> new_listener(DeviceListener::Create( | 92 std::unique_ptr<DeviceListener> new_listener(DeviceListener::Create( |
| 93 std::move(socket), port, | 93 std::move(socket), port, |
| 94 base::Bind(&DeviceController::DeleteListenerOnError, | 94 base::Bind(&DeviceController::DeleteListenerOnError, |
| 95 weak_ptr_factory_.GetWeakPtr()))); | 95 weak_ptr_factory_.GetWeakPtr()))); |
| 96 if (!new_listener) | 96 if (!new_listener) |
| 97 return; | 97 return; |
| 98 new_listener->Start(); | 98 new_listener->Start(); |
| 99 // |port| can be zero, to allow dynamically allocated port, so instead, we | 99 // |port| can be zero, to allow dynamically allocated port, so instead, we |
| 100 // call DeviceListener::listener_port() to retrieve the currently | 100 // call DeviceListener::listener_port() to retrieve the currently |
| 101 // allocated port to this new listener. | 101 // allocated port to this new listener. |
| 102 const int listener_port = new_listener->listener_port(); | 102 const int listener_port = new_listener->listener_port(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 129 break; | 129 break; |
| 130 default: | 130 default: |
| 131 // TODO(felipeg): add a KillAllListeners command. | 131 // TODO(felipeg): add a KillAllListeners command. |
| 132 LOG(ERROR) << "Invalid command received. Port: " << port | 132 LOG(ERROR) << "Invalid command received. Port: " << port |
| 133 << " Command: " << command; | 133 << " Command: " << command; |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 // static | 137 // static |
| 138 void DeviceController::DeleteListenerOnError( | 138 void DeviceController::DeleteListenerOnError( |
| 139 const base::WeakPtr<DeviceController>& device_controller_ptr, | 139 const base::WeakPtr<DeviceController>& device_controller_ptr, |
| 140 scoped_ptr<DeviceListener> device_listener) { | 140 std::unique_ptr<DeviceListener> device_listener) { |
| 141 DeviceListener* const listener = device_listener.release(); | 141 DeviceListener* const listener = device_listener.release(); |
| 142 DeviceController* const controller = device_controller_ptr.get(); | 142 DeviceController* const controller = device_controller_ptr.get(); |
| 143 if (!controller) { | 143 if (!controller) { |
| 144 // |listener| was already deleted by the controller that did have its | 144 // |listener| was already deleted by the controller that did have its |
| 145 // ownership. | 145 // ownership. |
| 146 return; | 146 return; |
| 147 } | 147 } |
| 148 DCHECK(controller->construction_task_runner_->RunsTasksOnCurrentThread()); | 148 DCHECK(controller->construction_task_runner_->RunsTasksOnCurrentThread()); |
| 149 bool listener_did_exist = DeleteRefCountedValueInMap( | 149 bool listener_did_exist = DeleteRefCountedValueInMap( |
| 150 listener->listener_port(), &controller->listeners_); | 150 listener->listener_port(), &controller->listeners_); |
| 151 DCHECK(listener_did_exist); | 151 DCHECK(listener_did_exist); |
| 152 // Note that |listener| was deleted by DeleteRefCountedValueInMap(). | 152 // Note that |listener| was deleted by DeleteRefCountedValueInMap(). |
| 153 } | 153 } |
| 154 | 154 |
| 155 } // namespace forwarder | 155 } // namespace forwarder |
| OLD | NEW |