OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/devtools/device/port_forwarding_controller.h" | 5 #include "chrome/browser/devtools/device/port_forwarding_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 int result, | 61 int result, |
62 net::StreamSocket* socket) { | 62 net::StreamSocket* socket) { |
63 if (result < 0) | 63 if (result < 0) |
64 return; | 64 return; |
65 SocketTunnel* tunnel = new SocketTunnel(callback); | 65 SocketTunnel* tunnel = new SocketTunnel(callback); |
66 tunnel->Start(socket, host, port); | 66 tunnel->Start(socket, host, port); |
67 } | 67 } |
68 | 68 |
69 private: | 69 private: |
70 explicit SocketTunnel(const CounterCallback& callback) | 70 explicit SocketTunnel(const CounterCallback& callback) |
71 : pending_writes_(0), | 71 : pending_io_(0), |
72 pending_destruction_(false), | 72 pending_destruction_(false), |
73 callback_(callback), | 73 callback_(callback), |
74 about_to_destroy_(false) { | 74 about_to_destroy_(false) { |
75 callback_.Run(1); | 75 callback_.Run(1); |
76 } | 76 } |
77 | 77 |
78 void Start(net::StreamSocket* socket, const std::string& host, int port) { | 78 void Start(net::StreamSocket* socket, const std::string& host, int port) { |
79 remote_socket_.reset(socket); | 79 remote_socket_.reset(socket); |
80 | 80 |
81 host_resolver_ = net::HostResolver::CreateDefaultResolver(NULL); | 81 host_resolver_ = net::HostResolver::CreateDefaultResolver(NULL); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 SelfDestruct(); | 119 SelfDestruct(); |
120 return; | 120 return; |
121 } | 121 } |
122 | 122 |
123 Pump(host_socket_.get(), remote_socket_.get()); | 123 Pump(host_socket_.get(), remote_socket_.get()); |
124 Pump(remote_socket_.get(), host_socket_.get()); | 124 Pump(remote_socket_.get(), host_socket_.get()); |
125 } | 125 } |
126 | 126 |
127 void Pump(net::StreamSocket* from, net::StreamSocket* to) { | 127 void Pump(net::StreamSocket* from, net::StreamSocket* to) { |
128 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kBufferSize); | 128 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kBufferSize); |
129 pending_io_++; | |
129 int result = from->Read( | 130 int result = from->Read( |
130 buffer.get(), | 131 buffer.get(), |
131 kBufferSize, | 132 kBufferSize, |
132 base::Bind( | 133 base::Bind( |
133 &SocketTunnel::OnRead, base::Unretained(this), from, to, buffer)); | 134 &SocketTunnel::OnRead, base::Unretained(this), from, to, buffer)); |
134 if (result != net::ERR_IO_PENDING) | 135 if (result != net::ERR_IO_PENDING) |
135 OnRead(from, to, buffer, result); | 136 OnRead(from, to, buffer, result); |
136 } | 137 } |
137 | 138 |
138 void OnRead(net::StreamSocket* from, | 139 void OnRead(net::StreamSocket* from, |
139 net::StreamSocket* to, | 140 net::StreamSocket* to, |
140 scoped_refptr<net::IOBuffer> buffer, | 141 scoped_refptr<net::IOBuffer> buffer, |
141 int result) { | 142 int result) { |
143 pending_io_--; | |
pfeldman
2014/05/18 06:17:06
To recap offline discussion: we don't have read ti
| |
142 if (result <= 0) { | 144 if (result <= 0) { |
143 SelfDestruct(); | 145 SelfDestruct(); |
144 return; | 146 return; |
145 } | 147 } |
146 | 148 |
147 int total = result; | 149 int total = result; |
148 scoped_refptr<net::DrainableIOBuffer> drainable = | 150 scoped_refptr<net::DrainableIOBuffer> drainable = |
149 new net::DrainableIOBuffer(buffer.get(), total); | 151 new net::DrainableIOBuffer(buffer.get(), total); |
150 | 152 |
151 ++pending_writes_; | 153 ++pending_io_; |
152 result = to->Write(drainable.get(), | 154 result = to->Write(drainable.get(), |
153 total, | 155 total, |
154 base::Bind(&SocketTunnel::OnWritten, | 156 base::Bind(&SocketTunnel::OnWritten, |
155 base::Unretained(this), | 157 base::Unretained(this), |
156 drainable, | 158 drainable, |
157 from, | 159 from, |
158 to)); | 160 to)); |
159 if (result != net::ERR_IO_PENDING) | 161 if (result != net::ERR_IO_PENDING) |
160 OnWritten(drainable, from, to, result); | 162 OnWritten(drainable, from, to, result); |
161 } | 163 } |
162 | 164 |
163 void OnWritten(scoped_refptr<net::DrainableIOBuffer> drainable, | 165 void OnWritten(scoped_refptr<net::DrainableIOBuffer> drainable, |
164 net::StreamSocket* from, | 166 net::StreamSocket* from, |
165 net::StreamSocket* to, | 167 net::StreamSocket* to, |
166 int result) { | 168 int result) { |
167 --pending_writes_; | 169 --pending_io_; |
168 if (result < 0) { | 170 if (result < 0) { |
169 SelfDestruct(); | 171 SelfDestruct(); |
170 return; | 172 return; |
171 } | 173 } |
172 | 174 |
173 drainable->DidConsume(result); | 175 drainable->DidConsume(result); |
174 if (drainable->BytesRemaining() > 0) { | 176 if (drainable->BytesRemaining() > 0) { |
175 ++pending_writes_; | 177 ++pending_io_; |
176 result = to->Write(drainable.get(), | 178 result = to->Write(drainable.get(), |
177 drainable->BytesRemaining(), | 179 drainable->BytesRemaining(), |
178 base::Bind(&SocketTunnel::OnWritten, | 180 base::Bind(&SocketTunnel::OnWritten, |
179 base::Unretained(this), | 181 base::Unretained(this), |
180 drainable, | 182 drainable, |
181 from, | 183 from, |
182 to)); | 184 to)); |
183 if (result != net::ERR_IO_PENDING) | 185 if (result != net::ERR_IO_PENDING) |
184 OnWritten(drainable, from, to, result); | 186 OnWritten(drainable, from, to, result); |
185 return; | 187 return; |
186 } | 188 } |
187 | 189 |
188 if (pending_destruction_) { | 190 if (pending_destruction_) { |
189 SelfDestruct(); | 191 SelfDestruct(); |
190 return; | 192 return; |
191 } | 193 } |
192 Pump(from, to); | 194 Pump(from, to); |
193 } | 195 } |
194 | 196 |
195 void SelfDestruct() { | 197 void SelfDestruct() { |
196 // In case one of the connections closes, we could get here | 198 // In case one of the connections closes, we could get here |
197 // from another one due to Disconnect firing back on all | 199 // from another one due to Disconnect firing back on all |
198 // read callbacks. | 200 // read callbacks. |
199 if (about_to_destroy_) | 201 if (about_to_destroy_) |
200 return; | 202 return; |
201 if (pending_writes_ > 0) { | 203 if (pending_io_ > 0) { |
202 pending_destruction_ = true; | 204 pending_destruction_ = true; |
203 return; | 205 return; |
204 } | 206 } |
205 delete this; | 207 delete this; |
206 } | 208 } |
207 | 209 |
208 scoped_ptr<net::StreamSocket> remote_socket_; | 210 scoped_ptr<net::StreamSocket> remote_socket_; |
209 scoped_ptr<net::StreamSocket> host_socket_; | 211 scoped_ptr<net::StreamSocket> host_socket_; |
210 scoped_ptr<net::HostResolver> host_resolver_; | 212 scoped_ptr<net::HostResolver> host_resolver_; |
211 net::AddressList address_list_; | 213 net::AddressList address_list_; |
212 int pending_writes_; | 214 int pending_io_; |
213 bool pending_destruction_; | 215 bool pending_destruction_; |
214 CounterCallback callback_; | 216 CounterCallback callback_; |
215 bool about_to_destroy_; | 217 bool about_to_destroy_; |
216 }; | 218 }; |
217 | 219 |
218 typedef DevToolsAndroidBridge::RemoteBrowser::ParsedVersion ParsedVersion; | 220 typedef DevToolsAndroidBridge::RemoteBrowser::ParsedVersion ParsedVersion; |
219 | 221 |
220 static bool IsVersionLower(const ParsedVersion& left, | 222 static bool IsVersionLower(const ParsedVersion& left, |
221 const ParsedVersion& right) { | 223 const ParsedVersion& right) { |
222 return std::lexicographical_compare( | 224 return std::lexicographical_compare( |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
663 "PortForwardingController", | 665 "PortForwardingController", |
664 BrowserContextDependencyManager::GetInstance()) {} | 666 BrowserContextDependencyManager::GetInstance()) {} |
665 | 667 |
666 PortForwardingController::Factory::~Factory() {} | 668 PortForwardingController::Factory::~Factory() {} |
667 | 669 |
668 KeyedService* PortForwardingController::Factory::BuildServiceInstanceFor( | 670 KeyedService* PortForwardingController::Factory::BuildServiceInstanceFor( |
669 content::BrowserContext* context) const { | 671 content::BrowserContext* context) const { |
670 Profile* profile = Profile::FromBrowserContext(context); | 672 Profile* profile = Profile::FromBrowserContext(context); |
671 return new PortForwardingController(profile); | 673 return new PortForwardingController(profile); |
672 } | 674 } |
OLD | NEW |