| 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 "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "content/public/browser/devtools_http_handler_delegate.h" | 8 #include "content/public/browser/devtools_http_handler_delegate.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 scoped_ptr<net::StreamSocket> client_socket_; | 148 scoped_ptr<net::StreamSocket> client_socket_; |
| 149 scoped_ptr<net::ServerSocket> server_socket_; | 149 scoped_ptr<net::ServerSocket> server_socket_; |
| 150 scoped_ptr<net::StreamSocket> accepted_socket_; | 150 scoped_ptr<net::StreamSocket> accepted_socket_; |
| 151 DevToolsHttpHandlerDelegate* delegate_; | 151 DevToolsHttpHandlerDelegate* delegate_; |
| 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(int, const std::string&)> AcceptedCallback; | 158 typedef base::Callback<void(uint16, const std::string&)> AcceptedCallback; |
| 159 | 159 |
| 160 BoundSocket(AcceptedCallback accepted_callback, | 160 BoundSocket(AcceptedCallback accepted_callback, |
| 161 DevToolsHttpHandlerDelegate* delegate) | 161 DevToolsHttpHandlerDelegate* delegate) |
| 162 : accepted_callback_(accepted_callback), | 162 : accepted_callback_(accepted_callback), |
| 163 delegate_(delegate), | 163 delegate_(delegate), |
| 164 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), | 164 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), |
| 165 port_(0) { | 165 port_(0) { |
| 166 } | 166 } |
| 167 | 167 |
| 168 virtual ~BoundSocket() { | 168 virtual ~BoundSocket() { |
| 169 } | 169 } |
| 170 | 170 |
| 171 bool Listen(int port) { | 171 bool Listen(uint16 port) { |
| 172 port_ = port; | 172 port_ = port; |
| 173 net::IPAddressNumber ip_number; | 173 net::IPAddressNumber ip_number; |
| 174 if (!net::ParseIPLiteralToNumber(kLocalhost, &ip_number)) | 174 if (!net::ParseIPLiteralToNumber(kLocalhost, &ip_number)) |
| 175 return false; | 175 return false; |
| 176 | 176 |
| 177 net::IPEndPoint end_point(ip_number, port); | 177 net::IPEndPoint end_point(ip_number, port); |
| 178 int result = socket_->Listen(end_point, kListenBacklog); | 178 int result = socket_->Listen(end_point, kListenBacklog); |
| 179 if (result < 0) | 179 if (result < 0) |
| 180 return false; | 180 return false; |
| 181 | 181 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 SocketPump* pump = new SocketPump(delegate_, accept_socket_.release()); | 216 SocketPump* pump = new SocketPump(delegate_, accept_socket_.release()); |
| 217 std::string name = pump->Init(); | 217 std::string name = pump->Init(); |
| 218 if (!name.empty()) | 218 if (!name.empty()) |
| 219 accepted_callback_.Run(port_, name); | 219 accepted_callback_.Run(port_, name); |
| 220 } | 220 } |
| 221 | 221 |
| 222 AcceptedCallback accepted_callback_; | 222 AcceptedCallback accepted_callback_; |
| 223 DevToolsHttpHandlerDelegate* delegate_; | 223 DevToolsHttpHandlerDelegate* delegate_; |
| 224 scoped_ptr<net::ServerSocket> socket_; | 224 scoped_ptr<net::ServerSocket> socket_; |
| 225 scoped_ptr<net::StreamSocket> accept_socket_; | 225 scoped_ptr<net::StreamSocket> accept_socket_; |
| 226 int port_; | 226 uint16 port_; |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 } // namespace | 229 } // namespace |
| 230 | 230 |
| 231 // TetheringHandler::TetheringImpl ------------------------------------------- | 231 // TetheringHandler::TetheringImpl ------------------------------------------- |
| 232 | 232 |
| 233 class TetheringHandler::TetheringImpl { | 233 class TetheringHandler::TetheringImpl { |
| 234 public: | 234 public: |
| 235 TetheringImpl( | 235 TetheringImpl( |
| 236 base::WeakPtr<TetheringHandler> handler, | 236 base::WeakPtr<TetheringHandler> handler, |
| 237 DevToolsHttpHandlerDelegate* delegate); | 237 DevToolsHttpHandlerDelegate* delegate); |
| 238 ~TetheringImpl(); | 238 ~TetheringImpl(); |
| 239 | 239 |
| 240 void Bind(scoped_refptr<DevToolsProtocol::Command> command, int port); | 240 void Bind(scoped_refptr<DevToolsProtocol::Command> command, uint16 port); |
| 241 void Unbind(scoped_refptr<DevToolsProtocol::Command> command, int port); | 241 void Unbind(scoped_refptr<DevToolsProtocol::Command> command, uint16 port); |
| 242 void Accepted(int port, const std::string& name); | 242 void Accepted(uint16 port, const std::string& name); |
| 243 | 243 |
| 244 private: | 244 private: |
| 245 void SendInternalError(scoped_refptr<DevToolsProtocol::Command> command, | 245 void SendInternalError(scoped_refptr<DevToolsProtocol::Command> command, |
| 246 const std::string& message); | 246 const std::string& message); |
| 247 | 247 |
| 248 base::WeakPtr<TetheringHandler> handler_; | 248 base::WeakPtr<TetheringHandler> handler_; |
| 249 DevToolsHttpHandlerDelegate* delegate_; | 249 DevToolsHttpHandlerDelegate* delegate_; |
| 250 | 250 |
| 251 typedef std::map<int, BoundSocket*> BoundSockets; | 251 typedef std::map<uint16, BoundSocket*> BoundSockets; |
| 252 BoundSockets bound_sockets_; | 252 BoundSockets bound_sockets_; |
| 253 }; | 253 }; |
| 254 | 254 |
| 255 TetheringHandler::TetheringImpl::TetheringImpl( | 255 TetheringHandler::TetheringImpl::TetheringImpl( |
| 256 base::WeakPtr<TetheringHandler> handler, | 256 base::WeakPtr<TetheringHandler> handler, |
| 257 DevToolsHttpHandlerDelegate* delegate) | 257 DevToolsHttpHandlerDelegate* delegate) |
| 258 : handler_(handler), | 258 : handler_(handler), |
| 259 delegate_(delegate) { | 259 delegate_(delegate) { |
| 260 } | 260 } |
| 261 | 261 |
| 262 TetheringHandler::TetheringImpl::~TetheringImpl() { | 262 TetheringHandler::TetheringImpl::~TetheringImpl() { |
| 263 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), | 263 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), |
| 264 bound_sockets_.end()); | 264 bound_sockets_.end()); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void TetheringHandler::TetheringImpl::Bind( | 267 void TetheringHandler::TetheringImpl::Bind( |
| 268 scoped_refptr<DevToolsProtocol::Command> command, int port) { | 268 scoped_refptr<DevToolsProtocol::Command> command, uint16 port) { |
| 269 if (bound_sockets_.find(port) != bound_sockets_.end()) { | 269 if (bound_sockets_.find(port) != bound_sockets_.end()) { |
| 270 SendInternalError(command, "Port already bound"); | 270 SendInternalError(command, "Port already bound"); |
| 271 return; | 271 return; |
| 272 } | 272 } |
| 273 | 273 |
| 274 BoundSocket::AcceptedCallback callback = base::Bind( | 274 BoundSocket::AcceptedCallback callback = base::Bind( |
| 275 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); | 275 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); |
| 276 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_)); | 276 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_)); |
| 277 if (!bound_socket->Listen(port)) { | 277 if (!bound_socket->Listen(port)) { |
| 278 SendInternalError(command, "Could not bind port"); | 278 SendInternalError(command, "Could not bind port"); |
| 279 return; | 279 return; |
| 280 } | 280 } |
| 281 | 281 |
| 282 bound_sockets_[port] = bound_socket.release(); | 282 bound_sockets_[port] = bound_socket.release(); |
| 283 BrowserThread::PostTask( | 283 BrowserThread::PostTask( |
| 284 BrowserThread::UI, | 284 BrowserThread::UI, |
| 285 FROM_HERE, | 285 FROM_HERE, |
| 286 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command)); | 286 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command)); |
| 287 } | 287 } |
| 288 | 288 |
| 289 void TetheringHandler::TetheringImpl::Unbind( | 289 void TetheringHandler::TetheringImpl::Unbind( |
| 290 scoped_refptr<DevToolsProtocol::Command> command, int port) { | 290 scoped_refptr<DevToolsProtocol::Command> command, uint16 port) { |
| 291 | 291 |
| 292 BoundSockets::iterator it = bound_sockets_.find(port); | 292 BoundSockets::iterator it = bound_sockets_.find(port); |
| 293 if (it == bound_sockets_.end()) { | 293 if (it == bound_sockets_.end()) { |
| 294 SendInternalError(command, "Port is not bound"); | 294 SendInternalError(command, "Port is not bound"); |
| 295 return; | 295 return; |
| 296 } | 296 } |
| 297 | 297 |
| 298 delete it->second; | 298 delete it->second; |
| 299 bound_sockets_.erase(it); | 299 bound_sockets_.erase(it); |
| 300 BrowserThread::PostTask( | 300 BrowserThread::PostTask( |
| 301 BrowserThread::UI, | 301 BrowserThread::UI, |
| 302 FROM_HERE, | 302 FROM_HERE, |
| 303 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command)); | 303 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command)); |
| 304 } | 304 } |
| 305 | 305 |
| 306 void TetheringHandler::TetheringImpl::Accepted( | 306 void TetheringHandler::TetheringImpl::Accepted( |
| 307 int port, const std::string& name) { | 307 uint16 port, const std::string& name) { |
| 308 BrowserThread::PostTask( | 308 BrowserThread::PostTask( |
| 309 BrowserThread::UI, | 309 BrowserThread::UI, |
| 310 FROM_HERE, | 310 FROM_HERE, |
| 311 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); | 311 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); |
| 312 } | 312 } |
| 313 | 313 |
| 314 void TetheringHandler::TetheringImpl::SendInternalError( | 314 void TetheringHandler::TetheringImpl::SendInternalError( |
| 315 scoped_refptr<DevToolsProtocol::Command> command, | 315 scoped_refptr<DevToolsProtocol::Command> command, |
| 316 const std::string& message) { | 316 const std::string& message) { |
| 317 BrowserThread::PostTask( | 317 BrowserThread::PostTask( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 340 if (is_active_) { | 340 if (is_active_) { |
| 341 message_loop_proxy_->DeleteSoon(FROM_HERE, impl_); | 341 message_loop_proxy_->DeleteSoon(FROM_HERE, impl_); |
| 342 impl_ = nullptr; | 342 impl_ = nullptr; |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 | 345 |
| 346 void TetheringHandler::SetClient(scoped_ptr<Client> client) { | 346 void TetheringHandler::SetClient(scoped_ptr<Client> client) { |
| 347 client_.swap(client); | 347 client_.swap(client); |
| 348 } | 348 } |
| 349 | 349 |
| 350 void TetheringHandler::Accepted(int port, const std::string& name) { | 350 void TetheringHandler::Accepted(uint16 port, const std::string& name) { |
| 351 client_->Accepted(AcceptedParams::Create()->set_port(port) | 351 client_->Accepted(AcceptedParams::Create()->set_port(port) |
| 352 ->set_connection_id(name)); | 352 ->set_connection_id(name)); |
| 353 } | 353 } |
| 354 | 354 |
| 355 bool TetheringHandler::Activate() { | 355 bool TetheringHandler::Activate() { |
| 356 if (is_active_) | 356 if (is_active_) |
| 357 return true; | 357 return true; |
| 358 if (impl_) | 358 if (impl_) |
| 359 return false; | 359 return false; |
| 360 is_active_ = true; | 360 is_active_ = true; |
| 361 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_); | 361 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_); |
| 362 return true; | 362 return true; |
| 363 } | 363 } |
| 364 | 364 |
| 365 scoped_refptr<DevToolsProtocol::Response> TetheringHandler::Bind( | 365 scoped_refptr<DevToolsProtocol::Response> TetheringHandler::Bind( |
| 366 int port, scoped_refptr<DevToolsProtocol::Command> command) { | 366 uint16 port, scoped_refptr<DevToolsProtocol::Command> command) { |
| 367 if (port < kMinTetheringPort || port > kMaxTetheringPort) | 367 if (port < kMinTetheringPort || port > kMaxTetheringPort) |
| 368 return command->InvalidParamResponse("port"); | 368 return command->InvalidParamResponse("port"); |
| 369 | 369 |
| 370 if (!Activate()) { | 370 if (!Activate()) { |
| 371 return command->ServerErrorResponse( | 371 return command->ServerErrorResponse( |
| 372 "Tethering is used by another connection"); | 372 "Tethering is used by another connection"); |
| 373 } | 373 } |
| 374 DCHECK(impl_); | 374 DCHECK(impl_); |
| 375 message_loop_proxy_->PostTask( | 375 message_loop_proxy_->PostTask( |
| 376 FROM_HERE, | 376 FROM_HERE, |
| 377 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), | 377 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), |
| 378 command, port)); | 378 command, port)); |
| 379 return command->AsyncResponsePromise(); | 379 return command->AsyncResponsePromise(); |
| 380 } | 380 } |
| 381 | 381 |
| 382 scoped_refptr<DevToolsProtocol::Response> TetheringHandler::Unbind( | 382 scoped_refptr<DevToolsProtocol::Response> TetheringHandler::Unbind( |
| 383 int port, scoped_refptr<DevToolsProtocol::Command> command) { | 383 uint16 port, scoped_refptr<DevToolsProtocol::Command> command) { |
| 384 if (!Activate()) { | 384 if (!Activate()) { |
| 385 return command->ServerErrorResponse( | 385 return command->ServerErrorResponse( |
| 386 "Tethering is used by another connection"); | 386 "Tethering is used by another connection"); |
| 387 } | 387 } |
| 388 DCHECK(impl_); | 388 DCHECK(impl_); |
| 389 message_loop_proxy_->PostTask( | 389 message_loop_proxy_->PostTask( |
| 390 FROM_HERE, | 390 FROM_HERE, |
| 391 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), | 391 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), |
| 392 command, port)); | 392 command, port)); |
| 393 return command->AsyncResponsePromise(); | 393 return command->AsyncResponsePromise(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 405 | 405 |
| 406 void TetheringHandler::SendInternalError( | 406 void TetheringHandler::SendInternalError( |
| 407 scoped_refptr<DevToolsProtocol::Command> command, | 407 scoped_refptr<DevToolsProtocol::Command> command, |
| 408 const std::string& message) { | 408 const std::string& message) { |
| 409 client_->SendInternalErrorResponse(command, message); | 409 client_->SendInternalErrorResponse(command, message); |
| 410 } | 410 } |
| 411 | 411 |
| 412 } // namespace tethering | 412 } // namespace tethering |
| 413 } // namespace devtools | 413 } // namespace devtools |
| 414 } // namespace content | 414 } // namespace content |
| OLD | NEW |