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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 return; | 95 return; |
96 // Delete the controllers on the thread they were created on. | 96 // Delete the controllers on the thread they were created on. |
97 thread_->task_runner()->DeleteSoon( | 97 thread_->task_runner()->DeleteSoon( |
98 FROM_HERE, controllers_.release()); | 98 FROM_HERE, controllers_.release()); |
99 } | 99 } |
100 | 100 |
101 void HandleRequest(const std::string& adb_path, | 101 void HandleRequest(const std::string& adb_path, |
102 const std::string& device_serial, | 102 const std::string& device_serial, |
103 int device_port, | 103 int device_port, |
104 int host_port, | 104 int host_port, |
105 scoped_ptr<Socket> client_socket) { | 105 std::unique_ptr<Socket> client_socket) { |
106 // Lazy initialize so that the CLI process doesn't get this thread created. | 106 // Lazy initialize so that the CLI process doesn't get this thread created. |
107 InitOnce(); | 107 InitOnce(); |
108 thread_->task_runner()->PostTask( | 108 thread_->task_runner()->PostTask( |
109 FROM_HERE, | 109 FROM_HERE, |
110 base::Bind(&HostControllersManager::HandleRequestOnInternalThread, | 110 base::Bind(&HostControllersManager::HandleRequestOnInternalThread, |
111 base::Unretained(this), adb_path, device_serial, device_port, | 111 base::Unretained(this), adb_path, device_serial, device_port, |
112 host_port, base::Passed(&client_socket))); | 112 host_port, base::Passed(&client_socket))); |
113 } | 113 } |
114 | 114 |
115 bool has_failed() const { return has_failed_; } | 115 bool has_failed() const { return has_failed_; } |
(...skipping 12 matching lines...) Expand all Loading... |
128 at_exit_manager_.reset(new base::AtExitManager()); | 128 at_exit_manager_.reset(new base::AtExitManager()); |
129 thread_.reset(new base::Thread("HostControllersManagerThread")); | 129 thread_.reset(new base::Thread("HostControllersManagerThread")); |
130 thread_->Start(); | 130 thread_->Start(); |
131 } | 131 } |
132 | 132 |
133 // Invoked when a HostController instance reports an error (e.g. due to a | 133 // Invoked when a HostController instance reports an error (e.g. due to a |
134 // device connectivity issue). Note that this could be called after the | 134 // device connectivity issue). Note that this could be called after the |
135 // controller manager was destroyed which is why a weak pointer is used. | 135 // controller manager was destroyed which is why a weak pointer is used. |
136 static void DeleteHostController( | 136 static void DeleteHostController( |
137 const base::WeakPtr<HostControllersManager>& manager_ptr, | 137 const base::WeakPtr<HostControllersManager>& manager_ptr, |
138 scoped_ptr<HostController> host_controller) { | 138 std::unique_ptr<HostController> host_controller) { |
139 HostController* const controller = host_controller.release(); | 139 HostController* const controller = host_controller.release(); |
140 HostControllersManager* const manager = manager_ptr.get(); | 140 HostControllersManager* const manager = manager_ptr.get(); |
141 if (!manager) { | 141 if (!manager) { |
142 // Note that |controller| is not leaked in this case since the host | 142 // Note that |controller| is not leaked in this case since the host |
143 // controllers manager owns the controllers. If the manager was deleted | 143 // controllers manager owns the controllers. If the manager was deleted |
144 // then all the controllers (including |controller|) were also deleted. | 144 // then all the controllers (including |controller|) were also deleted. |
145 return; | 145 return; |
146 } | 146 } |
147 DCHECK(manager->thread_->task_runner()->RunsTasksOnCurrentThread()); | 147 DCHECK(manager->thread_->task_runner()->RunsTasksOnCurrentThread()); |
148 // Note that this will delete |controller| which is owned by the map. | 148 // Note that this will delete |controller| which is owned by the map. |
149 DeleteRefCountedValueInMap( | 149 DeleteRefCountedValueInMap( |
150 MakeHostControllerMapKey( | 150 MakeHostControllerMapKey( |
151 controller->adb_port(), controller->device_port()), | 151 controller->adb_port(), controller->device_port()), |
152 manager->controllers_.get()); | 152 manager->controllers_.get()); |
153 } | 153 } |
154 | 154 |
155 void HandleRequestOnInternalThread(const std::string& adb_path, | 155 void HandleRequestOnInternalThread(const std::string& adb_path, |
156 const std::string& device_serial, | 156 const std::string& device_serial, |
157 int device_port, | 157 int device_port, |
158 int host_port, | 158 int host_port, |
159 scoped_ptr<Socket> client_socket) { | 159 std::unique_ptr<Socket> client_socket) { |
160 const int adb_port = GetAdbPortForDevice(adb_path, device_serial); | 160 const int adb_port = GetAdbPortForDevice(adb_path, device_serial); |
161 if (adb_port < 0) { | 161 if (adb_port < 0) { |
162 SendMessage( | 162 SendMessage( |
163 "ERROR: could not get adb port for device. You might need to add " | 163 "ERROR: could not get adb port for device. You might need to add " |
164 "'adb' to your PATH or provide the device serial id.", | 164 "'adb' to your PATH or provide the device serial id.", |
165 client_socket.get()); | 165 client_socket.get()); |
166 return; | 166 return; |
167 } | 167 } |
168 if (device_port < 0) { | 168 if (device_port < 0) { |
169 // Remove the previously created host controller. | 169 // Remove the previously created host controller. |
(...skipping 21 matching lines...) Expand all Loading... |
191 adb_port, device_port); | 191 adb_port, device_port); |
192 if (controllers_->find(controller_key) != controllers_->end()) { | 192 if (controllers_->find(controller_key) != controllers_->end()) { |
193 LOG(INFO) << "Already forwarding device port " << device_port | 193 LOG(INFO) << "Already forwarding device port " << device_port |
194 << " to host port " << host_port; | 194 << " to host port " << host_port; |
195 SendMessage(base::StringPrintf("%d:%d", device_port, host_port), | 195 SendMessage(base::StringPrintf("%d:%d", device_port, host_port), |
196 client_socket.get()); | 196 client_socket.get()); |
197 return; | 197 return; |
198 } | 198 } |
199 } | 199 } |
200 // Create a new host controller. | 200 // Create a new host controller. |
201 scoped_ptr<HostController> host_controller( | 201 std::unique_ptr<HostController> host_controller(HostController::Create( |
202 HostController::Create( | 202 device_port, host_port, adb_port, GetExitNotifierFD(), |
203 device_port, host_port, adb_port, GetExitNotifierFD(), | 203 base::Bind(&HostControllersManager::DeleteHostController, |
204 base::Bind(&HostControllersManager::DeleteHostController, | 204 weak_ptr_factory_.GetWeakPtr()))); |
205 weak_ptr_factory_.GetWeakPtr()))); | |
206 if (!host_controller.get()) { | 205 if (!host_controller.get()) { |
207 has_failed_ = true; | 206 has_failed_ = true; |
208 SendMessage("ERROR: Connection to device failed.", client_socket.get()); | 207 SendMessage("ERROR: Connection to device failed.", client_socket.get()); |
209 LogExistingControllers(client_socket); | 208 LogExistingControllers(client_socket); |
210 return; | 209 return; |
211 } | 210 } |
212 // Get the current allocated port. | 211 // Get the current allocated port. |
213 device_port = host_controller->device_port(); | 212 device_port = host_controller->device_port(); |
214 LOG(INFO) << "Forwarding device port " << device_port << " to host port " | 213 LOG(INFO) << "Forwarding device port " << device_port << " to host port " |
215 << host_port; | 214 << host_port; |
216 const std::string msg = base::StringPrintf("%d:%d", device_port, host_port); | 215 const std::string msg = base::StringPrintf("%d:%d", device_port, host_port); |
217 if (!SendMessage(msg, client_socket.get())) | 216 if (!SendMessage(msg, client_socket.get())) |
218 return; | 217 return; |
219 host_controller->Start(); | 218 host_controller->Start(); |
220 controllers_->insert( | 219 controllers_->insert( |
221 std::make_pair(MakeHostControllerMapKey(adb_port, device_port), | 220 std::make_pair(MakeHostControllerMapKey(adb_port, device_port), |
222 linked_ptr<HostController>(host_controller.release()))); | 221 linked_ptr<HostController>(host_controller.release()))); |
223 } | 222 } |
224 | 223 |
225 void LogExistingControllers(const scoped_ptr<Socket>& client_socket) { | 224 void LogExistingControllers(const std::unique_ptr<Socket>& client_socket) { |
226 SendMessage("ERROR: Existing controllers:", client_socket.get()); | 225 SendMessage("ERROR: Existing controllers:", client_socket.get()); |
227 for (const auto& controller : *controllers_) { | 226 for (const auto& controller : *controllers_) { |
228 SendMessage(base::StringPrintf("ERROR: %s", controller.first.c_str()), | 227 SendMessage(base::StringPrintf("ERROR: %s", controller.first.c_str()), |
229 client_socket.get()); | 228 client_socket.get()); |
230 } | 229 } |
231 } | 230 } |
232 | 231 |
233 void RemoveAdbPortForDeviceIfNeeded(const std::string& adb_path, | 232 void RemoveAdbPortForDeviceIfNeeded(const std::string& adb_path, |
234 const std::string& device_serial) { | 233 const std::string& device_serial) { |
235 base::hash_map<std::string, int>::const_iterator it = | 234 base::hash_map<std::string, int>::const_iterator it = |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 | 301 |
303 bool SendMessage(const std::string& msg, Socket* client_socket) { | 302 bool SendMessage(const std::string& msg, Socket* client_socket) { |
304 bool result = client_socket->WriteString(msg); | 303 bool result = client_socket->WriteString(msg); |
305 DCHECK(result); | 304 DCHECK(result); |
306 if (!result) | 305 if (!result) |
307 has_failed_ = true; | 306 has_failed_ = true; |
308 return result; | 307 return result; |
309 } | 308 } |
310 | 309 |
311 base::hash_map<std::string, int> device_serial_to_adb_port_map_; | 310 base::hash_map<std::string, int> device_serial_to_adb_port_map_; |
312 scoped_ptr<HostControllerMap> controllers_; | 311 std::unique_ptr<HostControllerMap> controllers_; |
313 bool has_failed_; | 312 bool has_failed_; |
314 scoped_ptr<base::AtExitManager> at_exit_manager_; // Needed by base::Thread. | 313 std::unique_ptr<base::AtExitManager> |
315 scoped_ptr<base::Thread> thread_; | 314 at_exit_manager_; // Needed by base::Thread. |
| 315 std::unique_ptr<base::Thread> thread_; |
316 base::WeakPtrFactory<HostControllersManager> weak_ptr_factory_; | 316 base::WeakPtrFactory<HostControllersManager> weak_ptr_factory_; |
317 }; | 317 }; |
318 | 318 |
319 class ServerDelegate : public Daemon::ServerDelegate { | 319 class ServerDelegate : public Daemon::ServerDelegate { |
320 public: | 320 public: |
321 ServerDelegate(const std::string& adb_path) | 321 ServerDelegate(const std::string& adb_path) |
322 : adb_path_(adb_path), has_failed_(false) {} | 322 : adb_path_(adb_path), has_failed_(false) {} |
323 | 323 |
324 bool has_failed() const { | 324 bool has_failed() const { |
325 return has_failed_ || controllers_manager_.has_failed(); | 325 return has_failed_ || controllers_manager_.has_failed(); |
326 } | 326 } |
327 | 327 |
328 // Daemon::ServerDelegate: | 328 // Daemon::ServerDelegate: |
329 void Init() override { | 329 void Init() override { |
330 LOG(INFO) << "Starting host process daemon (pid=" << getpid() << ")"; | 330 LOG(INFO) << "Starting host process daemon (pid=" << getpid() << ")"; |
331 DCHECK(!g_notifier); | 331 DCHECK(!g_notifier); |
332 g_notifier = new PipeNotifier(); | 332 g_notifier = new PipeNotifier(); |
333 signal(SIGTERM, KillHandler); | 333 signal(SIGTERM, KillHandler); |
334 signal(SIGINT, KillHandler); | 334 signal(SIGINT, KillHandler); |
335 } | 335 } |
336 | 336 |
337 void OnClientConnected(scoped_ptr<Socket> client_socket) override { | 337 void OnClientConnected(std::unique_ptr<Socket> client_socket) override { |
338 char buf[kBufSize]; | 338 char buf[kBufSize]; |
339 const int bytes_read = client_socket->Read(buf, sizeof(buf)); | 339 const int bytes_read = client_socket->Read(buf, sizeof(buf)); |
340 if (bytes_read <= 0) { | 340 if (bytes_read <= 0) { |
341 if (client_socket->DidReceiveEvent()) | 341 if (client_socket->DidReceiveEvent()) |
342 return; | 342 return; |
343 PError("Read()"); | 343 PError("Read()"); |
344 has_failed_ = true; | 344 has_failed_ = true; |
345 return; | 345 return; |
346 } | 346 } |
347 const base::Pickle command_pickle(buf, bytes_read); | 347 const base::Pickle command_pickle(buf, bytes_read); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 | 471 |
472 return client_delegate.has_failed() || daemon_delegate.has_failed(); | 472 return client_delegate.has_failed() || daemon_delegate.has_failed(); |
473 } | 473 } |
474 | 474 |
475 } // namespace | 475 } // namespace |
476 } // namespace forwarder2 | 476 } // namespace forwarder2 |
477 | 477 |
478 int main(int argc, char** argv) { | 478 int main(int argc, char** argv) { |
479 return forwarder2::RunHostForwarder(argc, argv); | 479 return forwarder2::RunHostForwarder(argc, argv); |
480 } | 480 } |
OLD | NEW |