| 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/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 231 |
| 232 // TetheringHandler::TetheringImpl ------------------------------------------- | 232 // TetheringHandler::TetheringImpl ------------------------------------------- |
| 233 | 233 |
| 234 class TetheringHandler::TetheringImpl { | 234 class TetheringHandler::TetheringImpl { |
| 235 public: | 235 public: |
| 236 TetheringImpl( | 236 TetheringImpl( |
| 237 base::WeakPtr<TetheringHandler> handler, | 237 base::WeakPtr<TetheringHandler> handler, |
| 238 const CreateServerSocketCallback& socket_callback); | 238 const CreateServerSocketCallback& socket_callback); |
| 239 ~TetheringImpl(); | 239 ~TetheringImpl(); |
| 240 | 240 |
| 241 void Bind(DevToolsCommandId command_id, uint16 port); | 241 void Bind(int session_id, DevToolsCommandId command_id, uint16 port); |
| 242 void Unbind(DevToolsCommandId command_id, uint16 port); | 242 void Unbind(int session_id, DevToolsCommandId command_id, uint16 port); |
| 243 void Accepted(uint16 port, const std::string& name); | 243 void Accepted(uint16 port, const std::string& name); |
| 244 | 244 |
| 245 private: | 245 private: |
| 246 void SendInternalError(DevToolsCommandId command_id, | 246 void SendInternalError(int session_id, |
| 247 DevToolsCommandId command_id, |
| 247 const std::string& message); | 248 const std::string& message); |
| 248 | 249 |
| 249 base::WeakPtr<TetheringHandler> handler_; | 250 base::WeakPtr<TetheringHandler> handler_; |
| 250 CreateServerSocketCallback socket_callback_; | 251 CreateServerSocketCallback socket_callback_; |
| 251 | 252 |
| 252 typedef std::map<uint16, BoundSocket*> BoundSockets; | 253 typedef std::map<uint16, BoundSocket*> BoundSockets; |
| 253 BoundSockets bound_sockets_; | 254 BoundSockets bound_sockets_; |
| 254 }; | 255 }; |
| 255 | 256 |
| 256 TetheringHandler::TetheringImpl::TetheringImpl( | 257 TetheringHandler::TetheringImpl::TetheringImpl( |
| 257 base::WeakPtr<TetheringHandler> handler, | 258 base::WeakPtr<TetheringHandler> handler, |
| 258 const CreateServerSocketCallback& socket_callback) | 259 const CreateServerSocketCallback& socket_callback) |
| 259 : handler_(handler), | 260 : handler_(handler), |
| 260 socket_callback_(socket_callback) { | 261 socket_callback_(socket_callback) { |
| 261 } | 262 } |
| 262 | 263 |
| 263 TetheringHandler::TetheringImpl::~TetheringImpl() { | 264 TetheringHandler::TetheringImpl::~TetheringImpl() { |
| 264 STLDeleteValues(&bound_sockets_); | 265 STLDeleteValues(&bound_sockets_); |
| 265 } | 266 } |
| 266 | 267 |
| 267 void TetheringHandler::TetheringImpl::Bind( | 268 void TetheringHandler::TetheringImpl::Bind(int session_id, |
| 268 DevToolsCommandId command_id, uint16 port) { | 269 DevToolsCommandId command_id, |
| 270 uint16 port) { |
| 269 if (bound_sockets_.find(port) != bound_sockets_.end()) { | 271 if (bound_sockets_.find(port) != bound_sockets_.end()) { |
| 270 SendInternalError(command_id, "Port already bound"); | 272 SendInternalError(session_id, command_id, "Port already bound"); |
| 271 return; | 273 return; |
| 272 } | 274 } |
| 273 | 275 |
| 274 BoundSocket::AcceptedCallback callback = base::Bind( | 276 BoundSocket::AcceptedCallback callback = base::Bind( |
| 275 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); | 277 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); |
| 276 scoped_ptr<BoundSocket> bound_socket( | 278 scoped_ptr<BoundSocket> bound_socket( |
| 277 new BoundSocket(callback, socket_callback_)); | 279 new BoundSocket(callback, socket_callback_)); |
| 278 if (!bound_socket->Listen(port)) { | 280 if (!bound_socket->Listen(port)) { |
| 279 SendInternalError(command_id, "Could not bind port"); | 281 SendInternalError(session_id, command_id, "Could not bind port"); |
| 280 return; | 282 return; |
| 281 } | 283 } |
| 282 | 284 |
| 283 bound_sockets_[port] = bound_socket.release(); | 285 bound_sockets_[port] = bound_socket.release(); |
| 284 BrowserThread::PostTask( | 286 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 285 BrowserThread::UI, | 287 base::Bind(&TetheringHandler::SendBindSuccess, |
| 286 FROM_HERE, | 288 handler_, session_id, command_id)); |
| 287 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command_id)); | |
| 288 } | 289 } |
| 289 | 290 |
| 290 void TetheringHandler::TetheringImpl::Unbind( | 291 void TetheringHandler::TetheringImpl::Unbind(int session_id, |
| 291 DevToolsCommandId command_id, uint16 port) { | 292 DevToolsCommandId command_id, |
| 292 | 293 uint16 port) { |
| 293 BoundSockets::iterator it = bound_sockets_.find(port); | 294 BoundSockets::iterator it = bound_sockets_.find(port); |
| 294 if (it == bound_sockets_.end()) { | 295 if (it == bound_sockets_.end()) { |
| 295 SendInternalError(command_id, "Port is not bound"); | 296 SendInternalError(session_id, command_id, "Port is not bound"); |
| 296 return; | 297 return; |
| 297 } | 298 } |
| 298 | 299 |
| 299 delete it->second; | 300 delete it->second; |
| 300 bound_sockets_.erase(it); | 301 bound_sockets_.erase(it); |
| 301 BrowserThread::PostTask( | 302 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 302 BrowserThread::UI, | 303 base::Bind(&TetheringHandler::SendUnbindSuccess, |
| 303 FROM_HERE, | 304 handler_, session_id, command_id)); |
| 304 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command_id)); | |
| 305 } | 305 } |
| 306 | 306 |
| 307 void TetheringHandler::TetheringImpl::Accepted( | 307 void TetheringHandler::TetheringImpl::Accepted( |
| 308 uint16 port, const std::string& name) { | 308 uint16 port, const std::string& name) { |
| 309 BrowserThread::PostTask( | 309 BrowserThread::PostTask( |
| 310 BrowserThread::UI, | 310 BrowserThread::UI, |
| 311 FROM_HERE, | 311 FROM_HERE, |
| 312 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); | 312 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); |
| 313 } | 313 } |
| 314 | 314 |
| 315 void TetheringHandler::TetheringImpl::SendInternalError( | 315 void TetheringHandler::TetheringImpl::SendInternalError( |
| 316 int session_id, |
| 316 DevToolsCommandId command_id, | 317 DevToolsCommandId command_id, |
| 317 const std::string& message) { | 318 const std::string& message) { |
| 318 BrowserThread::PostTask( | 319 BrowserThread::PostTask( |
| 319 BrowserThread::UI, | 320 BrowserThread::UI, FROM_HERE, |
| 320 FROM_HERE, | 321 base::Bind(&TetheringHandler::SendInternalError, handler_, session_id, |
| 321 base::Bind(&TetheringHandler::SendInternalError, handler_, | |
| 322 command_id, message)); | 322 command_id, message)); |
| 323 } | 323 } |
| 324 | 324 |
| 325 | 325 |
| 326 // TetheringHandler ---------------------------------------------------------- | 326 // TetheringHandler ---------------------------------------------------------- |
| 327 | 327 |
| 328 // static | 328 // static |
| 329 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; | 329 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; |
| 330 | 330 |
| 331 TetheringHandler::TetheringHandler( | 331 TetheringHandler::TetheringHandler( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 356 bool TetheringHandler::Activate() { | 356 bool TetheringHandler::Activate() { |
| 357 if (is_active_) | 357 if (is_active_) |
| 358 return true; | 358 return true; |
| 359 if (impl_) | 359 if (impl_) |
| 360 return false; | 360 return false; |
| 361 is_active_ = true; | 361 is_active_ = true; |
| 362 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), socket_callback_); | 362 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), socket_callback_); |
| 363 return true; | 363 return true; |
| 364 } | 364 } |
| 365 | 365 |
| 366 Response TetheringHandler::Bind(DevToolsCommandId command_id, int port) { | 366 Response TetheringHandler::Bind(int session_id, |
| 367 DevToolsCommandId command_id, |
| 368 int port) { |
| 367 if (port < kMinTetheringPort || port > kMaxTetheringPort) | 369 if (port < kMinTetheringPort || port > kMaxTetheringPort) |
| 368 return Response::InvalidParams("port"); | 370 return Response::InvalidParams("port"); |
| 369 | 371 |
| 370 if (!Activate()) | 372 if (!Activate()) |
| 371 return Response::ServerError("Tethering is used by another connection"); | 373 return Response::ServerError("Tethering is used by another connection"); |
| 372 | 374 |
| 373 DCHECK(impl_); | 375 DCHECK(impl_); |
| 374 task_runner_->PostTask( | 376 task_runner_->PostTask( |
| 375 FROM_HERE, base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), | 377 FROM_HERE, base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), |
| 376 command_id, port)); | 378 session_id, command_id, port)); |
| 377 return Response::OK(); | 379 return Response::OK(); |
| 378 } | 380 } |
| 379 | 381 |
| 380 Response TetheringHandler::Unbind(DevToolsCommandId command_id, int port) { | 382 Response TetheringHandler::Unbind(int session_id, |
| 383 DevToolsCommandId command_id, |
| 384 int port) { |
| 381 if (!Activate()) | 385 if (!Activate()) |
| 382 return Response::ServerError("Tethering is used by another connection"); | 386 return Response::ServerError("Tethering is used by another connection"); |
| 383 | 387 |
| 384 DCHECK(impl_); | 388 DCHECK(impl_); |
| 385 task_runner_->PostTask( | 389 task_runner_->PostTask( |
| 386 FROM_HERE, base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), | 390 FROM_HERE, base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), |
| 387 command_id, port)); | 391 session_id, command_id, port)); |
| 388 return Response::OK(); | 392 return Response::OK(); |
| 389 } | 393 } |
| 390 | 394 |
| 391 void TetheringHandler::SendBindSuccess(DevToolsCommandId command_id) { | 395 void TetheringHandler::SendBindSuccess(int session_id, |
| 392 client_->SendBindResponse(command_id, BindResponse::Create()); | 396 DevToolsCommandId command_id) { |
| 397 client_->SendBindResponse(session_id, command_id, BindResponse::Create()); |
| 393 } | 398 } |
| 394 | 399 |
| 395 void TetheringHandler::SendUnbindSuccess(DevToolsCommandId command_id) { | 400 void TetheringHandler::SendUnbindSuccess(int session_id, |
| 396 client_->SendUnbindResponse(command_id, UnbindResponse::Create()); | 401 DevToolsCommandId command_id) { |
| 402 client_->SendUnbindResponse(session_id, command_id, UnbindResponse::Create()); |
| 397 } | 403 } |
| 398 | 404 |
| 399 void TetheringHandler::SendInternalError(DevToolsCommandId command_id, | 405 void TetheringHandler::SendInternalError(int session_id, |
| 406 DevToolsCommandId command_id, |
| 400 const std::string& message) { | 407 const std::string& message) { |
| 401 client_->SendError(command_id, Response::InternalError(message)); | 408 client_->SendError(session_id, command_id, Response::InternalError(message)); |
| 402 } | 409 } |
| 403 | 410 |
| 404 } // namespace tethering | 411 } // namespace tethering |
| 405 } // namespace devtools | 412 } // namespace devtools |
| 406 } // namespace content | 413 } // namespace content |
| OLD | NEW |