Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: tools/android/forwarder2/host_controller.cc

Issue 60033002: Make HostController/DeviceListener deletion fully synchronous. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/host_controller.h" 5 #include "tools/android/forwarder2/host_controller.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h"
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "tools/android/forwarder2/command.h" 14 #include "tools/android/forwarder2/command.h"
14 #include "tools/android/forwarder2/forwarder.h" 15 #include "tools/android/forwarder2/forwarder.h"
15 #include "tools/android/forwarder2/socket.h" 16 #include "tools/android/forwarder2/socket.h"
16 17
17 namespace forwarder2 { 18 namespace forwarder2 {
18 19
(...skipping 30 matching lines...) Expand all
49 new HostController( 50 new HostController(
50 device_port_allocated, host_port, adb_port, exit_notifier_fd, 51 device_port_allocated, host_port, adb_port, exit_notifier_fd,
51 error_callback, adb_control_socket.Pass(), 52 error_callback, adb_control_socket.Pass(),
52 delete_controller_notifier.Pass())); 53 delete_controller_notifier.Pass()));
53 return host_controller.Pass(); 54 return host_controller.Pass();
54 } 55 }
55 56
56 HostController::~HostController() { 57 HostController::~HostController() {
57 DCHECK(deletion_task_runner_->RunsTasksOnCurrentThread()); 58 DCHECK(deletion_task_runner_->RunsTasksOnCurrentThread());
58 delete_controller_notifier_->Notify(); 59 delete_controller_notifier_->Notify();
59 // Note that the Forwarder instance (that also received a delete notification)
60 // might still be running on its own thread at this point. This is not a
61 // problem since it will self-delete once the socket that it is operating on
62 // is closed.
63 } 60 }
64 61
65 void HostController::Start() { 62 void HostController::Start() {
66 thread_.Start(); 63 thread_.Start();
67 ReadNextCommandSoon(); 64 ReadNextCommandSoon();
68 } 65 }
69 66
70 HostController::HostController( 67 HostController::HostController(
71 int device_port, 68 int device_port,
72 int host_port, 69 int host_port,
(...skipping 21 matching lines...) Expand all
94 } 91 }
95 92
96 void HostController::ReadCommandOnInternalThread() { 93 void HostController::ReadCommandOnInternalThread() {
97 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) { 94 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) {
98 LOG(ERROR) << "Did not receive ACCEPT_SUCCESS for port: " 95 LOG(ERROR) << "Did not receive ACCEPT_SUCCESS for port: "
99 << host_port_; 96 << host_port_;
100 OnInternalThreadError(); 97 OnInternalThreadError();
101 return; 98 return;
102 } 99 }
103 // Try to connect to host server. 100 // Try to connect to host server.
104 scoped_ptr<Socket> host_server_data_socket(CreateSocket()); 101 scoped_ptr<Socket> host_server_data_socket(new Socket());
105 if (!host_server_data_socket->ConnectTcp(std::string(), host_port_)) { 102 if (!host_server_data_socket->ConnectTcp(std::string(), host_port_)) {
106 LOG(ERROR) << "Could not Connect HostServerData socket on port: " 103 LOG(ERROR) << "Could not Connect HostServerData socket on port: "
107 << host_port_; 104 << host_port_;
108 SendCommand( 105 SendCommand(
109 command::HOST_SERVER_ERROR, device_port_, adb_control_socket_.get()); 106 command::HOST_SERVER_ERROR, device_port_, adb_control_socket_.get());
110 if (ReceivedCommand(command::ACK, adb_control_socket_.get())) { 107 if (ReceivedCommand(command::ACK, adb_control_socket_.get())) {
111 // It can continue if the host forwarder could not connect to the host 108 // It can continue if the host forwarder could not connect to the host
112 // server but the device acknowledged that, so that the device could 109 // server but the device acknowledged that, so that the device could
113 // re-try later. 110 // re-try later.
114 ReadNextCommandSoon(); 111 ReadNextCommandSoon();
115 return; 112 return;
116 } 113 }
117 LOG(ERROR) << "Will delete host controller: " << host_port_;
118 OnInternalThreadError(); 114 OnInternalThreadError();
119 return; 115 return;
120 } 116 }
121 LOG(INFO) << "Will send HOST_SERVER_SUCCESS: " << host_port_; 117 LOG(INFO) << "Will send HOST_SERVER_SUCCESS: " << host_port_;
122 SendCommand( 118 SendCommand(
123 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get()); 119 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get());
124 StartForwarder(host_server_data_socket.Pass()); 120 StartForwarder(host_server_data_socket.Pass());
125 ReadNextCommandSoon(); 121 ReadNextCommandSoon();
126 } 122 }
127 123
128 void HostController::StartForwarder( 124 void HostController::StartForwarder(
129 scoped_ptr<Socket> host_server_data_socket) { 125 scoped_ptr<Socket> host_server_data_socket) {
130 scoped_ptr<Socket> adb_data_socket(CreateSocket()); 126 scoped_ptr<Socket> adb_data_socket(new Socket());
131 if (!adb_data_socket->ConnectTcp("", adb_port_)) { 127 if (!adb_data_socket->ConnectTcp("", adb_port_)) {
132 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; 128 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_;
133 OnInternalThreadError(); 129 OnInternalThreadError();
134 return; 130 return;
135 } 131 }
136 // Open the Adb data connection, and send a command with the 132 // Open the Adb data connection, and send a command with the
137 // |device_forward_port| as a way for the device to identify the connection. 133 // |device_forward_port| as a way for the device to identify the connection.
138 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get()); 134 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get());
139 135
140 // Check that the device received the new Adb Data Connection. Note that this 136 // Check that the device received the new Adb Data Connection. Note that this
141 // check is done through the |adb_control_socket_| that is handled in the 137 // check is done through the |adb_control_socket_| that is handled in the
142 // DeviceListener thread just after the call to WaitForAdbDataSocket(). 138 // DeviceListener thread just after the call to WaitForAdbDataSocket().
143 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, 139 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS,
144 adb_control_socket_.get())) { 140 adb_control_socket_.get())) {
145 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; 141 LOG(ERROR) << "Device could not handle the new Adb Data Connection.";
146 OnInternalThreadError(); 142 OnInternalThreadError();
147 return; 143 return;
148 } 144 }
149 forwarder2::StartForwarder( 145 forwarders_manager_.CreateAndStartNewForwarder(
150 host_server_data_socket.Pass(), adb_data_socket.Pass()); 146 host_server_data_socket.Pass(), adb_data_socket.Pass());
151 } 147 }
152 148
153 scoped_ptr<Socket> HostController::CreateSocket() {
154 scoped_ptr<Socket> socket(new Socket());
155 socket->AddEventFd(global_exit_notifier_fd_);
156 socket->AddEventFd(delete_controller_notifier_->receiver_fd());
157 return socket.Pass();
158 }
159
160 void HostController::OnInternalThreadError() { 149 void HostController::OnInternalThreadError() {
161 UnmapPortOnDevice(); 150 UnmapPortOnDevice();
162 self_deleter_helper_.MaybeSelfDeleteSoon(); 151 self_deleter_helper_.MaybeSelfDeleteSoon();
163 } 152 }
164 153
165 void HostController::UnmapPortOnDevice() { 154 void HostController::UnmapPortOnDevice() {
166 Socket socket; 155 Socket socket;
167 if (!socket.ConnectTcp("", adb_port_)) { 156 if (!socket.ConnectTcp("", adb_port_)) {
168 LOG(ERROR) << "Could not connect to device on port " << adb_port_; 157 LOG(ERROR) << "Could not connect to device on port " << adb_port_;
169 return; 158 return;
170 } 159 }
171 if (!SendCommand(command::UNLISTEN, device_port_, &socket)) { 160 if (!SendCommand(command::UNLISTEN, device_port_, &socket)) {
172 LOG(ERROR) << "Could not send unmap command for port " << device_port_; 161 LOG(ERROR) << "Could not send unmap command for port " << device_port_;
173 return; 162 return;
174 } 163 }
175 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) { 164 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) {
176 LOG(ERROR) << "Unamp command failed for port " << device_port_; 165 LOG(ERROR) << "Unamp command failed for port " << device_port_;
177 return; 166 return;
178 } 167 }
179 } 168 }
180 169
181 } // namespace forwarder2 170 } // namespace forwarder2
OLDNEW
« no previous file with comments | « tools/android/forwarder2/host_controller.h ('k') | tools/android/forwarder2/self_deleter_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698