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 "content/browser/devtools/protocol/tethering_handler.h" | 5 #include "content/browser/devtools/protocol/tethering_handler.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 #include "net/base/ip_address.h" | 10 #include "net/base/ip_address.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/socket/server_socket.h" | 12 #include "net/socket/server_socket.h" |
13 #include "net/socket/stream_socket.h" | 13 #include "net/socket/stream_socket.h" |
14 #include "net/socket/tcp_server_socket.h" | 14 #include "net/socket/tcp_server_socket.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 namespace devtools { | 17 namespace devtools { |
18 namespace tethering { | 18 namespace tethering { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 const int kListenBacklog = 5; | 22 const int kListenBacklog = 5; |
23 const int kBufferSize = 16 * 1024; | 23 const int kBufferSize = 16 * 1024; |
24 | 24 |
25 const int kMinTetheringPort = 1024; | 25 const int kMinTetheringPort = 1024; |
26 const int kMaxTetheringPort = 32767; | 26 const int kMaxTetheringPort = 32767; |
27 | 27 |
28 using Response = DevToolsProtocolClient::Response; | 28 using Response = DevToolsProtocolClient::Response; |
29 using CreateServerSocketCallback = | 29 using CreateServerSocketCallback = |
30 base::Callback<scoped_ptr<net::ServerSocket>(std::string*)>; | 30 base::Callback<std::unique_ptr<net::ServerSocket>(std::string*)>; |
31 | 31 |
32 class SocketPump { | 32 class SocketPump { |
33 public: | 33 public: |
34 SocketPump(net::StreamSocket* client_socket) | 34 SocketPump(net::StreamSocket* client_socket) |
35 : client_socket_(client_socket), | 35 : client_socket_(client_socket), |
36 pending_writes_(0), | 36 pending_writes_(0), |
37 pending_destruction_(false) { | 37 pending_destruction_(false) { |
38 } | 38 } |
39 | 39 |
40 std::string Init(const CreateServerSocketCallback& socket_callback) { | 40 std::string Init(const CreateServerSocketCallback& socket_callback) { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 void SelfDestruct() { | 139 void SelfDestruct() { |
140 if (pending_writes_ > 0) { | 140 if (pending_writes_ > 0) { |
141 pending_destruction_ = true; | 141 pending_destruction_ = true; |
142 return; | 142 return; |
143 } | 143 } |
144 delete this; | 144 delete this; |
145 } | 145 } |
146 | 146 |
147 | 147 |
148 private: | 148 private: |
149 scoped_ptr<net::StreamSocket> client_socket_; | 149 std::unique_ptr<net::StreamSocket> client_socket_; |
150 scoped_ptr<net::ServerSocket> server_socket_; | 150 std::unique_ptr<net::ServerSocket> server_socket_; |
151 scoped_ptr<net::StreamSocket> accepted_socket_; | 151 std::unique_ptr<net::StreamSocket> accepted_socket_; |
152 int pending_writes_; | 152 int pending_writes_; |
153 bool pending_destruction_; | 153 bool pending_destruction_; |
154 }; | 154 }; |
155 | 155 |
156 class BoundSocket { | 156 class BoundSocket { |
157 public: | 157 public: |
158 typedef base::Callback<void(uint16_t, const std::string&)> AcceptedCallback; | 158 typedef base::Callback<void(uint16_t, const std::string&)> AcceptedCallback; |
159 | 159 |
160 BoundSocket(AcceptedCallback accepted_callback, | 160 BoundSocket(AcceptedCallback accepted_callback, |
161 const CreateServerSocketCallback& socket_callback) | 161 const CreateServerSocketCallback& socket_callback) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 return; | 210 return; |
211 | 211 |
212 SocketPump* pump = new SocketPump(accept_socket_.release()); | 212 SocketPump* pump = new SocketPump(accept_socket_.release()); |
213 std::string name = pump->Init(socket_callback_); | 213 std::string name = pump->Init(socket_callback_); |
214 if (!name.empty()) | 214 if (!name.empty()) |
215 accepted_callback_.Run(port_, name); | 215 accepted_callback_.Run(port_, name); |
216 } | 216 } |
217 | 217 |
218 AcceptedCallback accepted_callback_; | 218 AcceptedCallback accepted_callback_; |
219 CreateServerSocketCallback socket_callback_; | 219 CreateServerSocketCallback socket_callback_; |
220 scoped_ptr<net::ServerSocket> socket_; | 220 std::unique_ptr<net::ServerSocket> socket_; |
221 scoped_ptr<net::StreamSocket> accept_socket_; | 221 std::unique_ptr<net::StreamSocket> accept_socket_; |
222 uint16_t port_; | 222 uint16_t port_; |
223 }; | 223 }; |
224 | 224 |
225 } // namespace | 225 } // namespace |
226 | 226 |
227 // TetheringHandler::TetheringImpl ------------------------------------------- | 227 // TetheringHandler::TetheringImpl ------------------------------------------- |
228 | 228 |
229 class TetheringHandler::TetheringImpl { | 229 class TetheringHandler::TetheringImpl { |
230 public: | 230 public: |
231 TetheringImpl( | 231 TetheringImpl( |
(...skipping 29 matching lines...) Expand all Loading... |
261 | 261 |
262 void TetheringHandler::TetheringImpl::Bind(DevToolsCommandId command_id, | 262 void TetheringHandler::TetheringImpl::Bind(DevToolsCommandId command_id, |
263 uint16_t port) { | 263 uint16_t port) { |
264 if (bound_sockets_.find(port) != bound_sockets_.end()) { | 264 if (bound_sockets_.find(port) != bound_sockets_.end()) { |
265 SendInternalError(command_id, "Port already bound"); | 265 SendInternalError(command_id, "Port already bound"); |
266 return; | 266 return; |
267 } | 267 } |
268 | 268 |
269 BoundSocket::AcceptedCallback callback = base::Bind( | 269 BoundSocket::AcceptedCallback callback = base::Bind( |
270 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); | 270 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); |
271 scoped_ptr<BoundSocket> bound_socket( | 271 std::unique_ptr<BoundSocket> bound_socket( |
272 new BoundSocket(callback, socket_callback_)); | 272 new BoundSocket(callback, socket_callback_)); |
273 if (!bound_socket->Listen(port)) { | 273 if (!bound_socket->Listen(port)) { |
274 SendInternalError(command_id, "Could not bind port"); | 274 SendInternalError(command_id, "Could not bind port"); |
275 return; | 275 return; |
276 } | 276 } |
277 | 277 |
278 bound_sockets_[port] = bound_socket.release(); | 278 bound_sockets_[port] = bound_socket.release(); |
279 BrowserThread::PostTask( | 279 BrowserThread::PostTask( |
280 BrowserThread::UI, | 280 BrowserThread::UI, |
281 FROM_HERE, | 281 FROM_HERE, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 weak_factory_(this) { | 331 weak_factory_(this) { |
332 } | 332 } |
333 | 333 |
334 TetheringHandler::~TetheringHandler() { | 334 TetheringHandler::~TetheringHandler() { |
335 if (is_active_) { | 335 if (is_active_) { |
336 task_runner_->DeleteSoon(FROM_HERE, impl_); | 336 task_runner_->DeleteSoon(FROM_HERE, impl_); |
337 impl_ = nullptr; | 337 impl_ = nullptr; |
338 } | 338 } |
339 } | 339 } |
340 | 340 |
341 void TetheringHandler::SetClient(scoped_ptr<Client> client) { | 341 void TetheringHandler::SetClient(std::unique_ptr<Client> client) { |
342 client_.swap(client); | 342 client_.swap(client); |
343 } | 343 } |
344 | 344 |
345 void TetheringHandler::Accepted(uint16_t port, const std::string& name) { | 345 void TetheringHandler::Accepted(uint16_t port, const std::string& name) { |
346 client_->Accepted(AcceptedParams::Create()->set_port(port) | 346 client_->Accepted(AcceptedParams::Create()->set_port(port) |
347 ->set_connection_id(name)); | 347 ->set_connection_id(name)); |
348 } | 348 } |
349 | 349 |
350 bool TetheringHandler::Activate() { | 350 bool TetheringHandler::Activate() { |
351 if (is_active_) | 351 if (is_active_) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 } | 391 } |
392 | 392 |
393 void TetheringHandler::SendInternalError(DevToolsCommandId command_id, | 393 void TetheringHandler::SendInternalError(DevToolsCommandId command_id, |
394 const std::string& message) { | 394 const std::string& message) { |
395 client_->SendError(command_id, Response::InternalError(message)); | 395 client_->SendError(command_id, Response::InternalError(message)); |
396 } | 396 } |
397 | 397 |
398 } // namespace tethering | 398 } // namespace tethering |
399 } // namespace devtools | 399 } // namespace devtools |
400 } // namespace content | 400 } // namespace content |
OLD | NEW |