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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 // static | 22 // static |
23 scoped_ptr<DeviceController> DeviceController::Create( | 23 scoped_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 scoped_ptr<DeviceController> device_controller; |
27 scoped_ptr<Socket> host_socket(new Socket()); | 27 scoped_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.Pass(); | 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(host_socket.Pass(), exit_notifier_fd)); | 35 new DeviceController(std::move(host_socket), exit_notifier_fd)); |
36 return device_controller.Pass(); | 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(scoped_ptr<Socket> host_socket, |
48 int exit_notifier_fd) | 48 int exit_notifier_fd) |
49 : host_socket_(host_socket.Pass()), | 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))); |
(...skipping 22 matching lines...) Expand all Loading... |
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( | 92 scoped_ptr<DeviceListener> new_listener(DeviceListener::Create( |
93 DeviceListener::Create( | 93 std::move(socket), port, |
94 socket.Pass(), port, | 94 base::Bind(&DeviceController::DeleteListenerOnError, |
95 base::Bind(&DeviceController::DeleteListenerOnError, | 95 weak_ptr_factory_.GetWeakPtr()))); |
96 weak_ptr_factory_.GetWeakPtr()))); | |
97 if (!new_listener) | 96 if (!new_listener) |
98 return; | 97 return; |
99 new_listener->Start(); | 98 new_listener->Start(); |
100 // |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 |
101 // call DeviceListener::listener_port() to retrieve the currently | 100 // call DeviceListener::listener_port() to retrieve the currently |
102 // allocated port to this new listener. | 101 // allocated port to this new listener. |
103 const int listener_port = new_listener->listener_port(); | 102 const int listener_port = new_listener->listener_port(); |
104 listeners_.insert( | 103 listeners_.insert( |
105 std::make_pair(listener_port, | 104 std::make_pair(listener_port, |
106 linked_ptr<DeviceListener>(new_listener.release()))); | 105 linked_ptr<DeviceListener>(new_listener.release()))); |
107 LOG(INFO) << "Forwarding device port " << listener_port << " to host."; | 106 LOG(INFO) << "Forwarding device port " << listener_port << " to host."; |
108 break; | 107 break; |
109 } | 108 } |
110 case command::DATA_CONNECTION: | 109 case command::DATA_CONNECTION: |
111 if (listener == NULL) { | 110 if (listener == NULL) { |
112 LOG(ERROR) << "Data Connection command received, but " | 111 LOG(ERROR) << "Data Connection command received, but " |
113 << "listener has not been set up yet for port " << port; | 112 << "listener has not been set up yet for port " << port; |
114 // After this point it is assumed that, once we close our Adb Data | 113 // After this point it is assumed that, once we close our Adb Data |
115 // socket, the Adb forwarder command will propagate the closing of | 114 // socket, the Adb forwarder command will propagate the closing of |
116 // sockets all the way to the host side. | 115 // sockets all the way to the host side. |
117 break; | 116 break; |
118 } | 117 } |
119 listener->SetAdbDataSocket(socket.Pass()); | 118 listener->SetAdbDataSocket(std::move(socket)); |
120 break; | 119 break; |
121 case command::UNLISTEN: | 120 case command::UNLISTEN: |
122 LOG(INFO) << "Unmapping port " << port; | 121 LOG(INFO) << "Unmapping port " << port; |
123 if (!listener) { | 122 if (!listener) { |
124 LOG(ERROR) << "No listener found for port " << port; | 123 LOG(ERROR) << "No listener found for port " << port; |
125 SendCommand(command::UNLISTEN_ERROR, port, socket.get()); | 124 SendCommand(command::UNLISTEN_ERROR, port, socket.get()); |
126 break; | 125 break; |
127 } | 126 } |
128 DeleteRefCountedValueInMapFromIterator(listener_it, &listeners_); | 127 DeleteRefCountedValueInMapFromIterator(listener_it, &listeners_); |
129 SendCommand(command::UNLISTEN_SUCCESS, port, socket.get()); | 128 SendCommand(command::UNLISTEN_SUCCESS, port, socket.get()); |
(...skipping 17 matching lines...) Expand all Loading... |
147 return; | 146 return; |
148 } | 147 } |
149 DCHECK(controller->construction_task_runner_->RunsTasksOnCurrentThread()); | 148 DCHECK(controller->construction_task_runner_->RunsTasksOnCurrentThread()); |
150 bool listener_did_exist = DeleteRefCountedValueInMap( | 149 bool listener_did_exist = DeleteRefCountedValueInMap( |
151 listener->listener_port(), &controller->listeners_); | 150 listener->listener_port(), &controller->listeners_); |
152 DCHECK(listener_did_exist); | 151 DCHECK(listener_did_exist); |
153 // Note that |listener| was deleted by DeleteRefCountedValueInMap(). | 152 // Note that |listener| was deleted by DeleteRefCountedValueInMap(). |
154 } | 153 } |
155 | 154 |
156 } // namespace forwarder | 155 } // namespace forwarder |
OLD | NEW |