OLD | NEW |
---|---|
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 "content/browser/devtools/tethering_handler.h" | 5 #include "content/browser/devtools/tethering_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 | 250 |
251 // TetheringHandler::TetheringImpl ------------------------------------------- | 251 // TetheringHandler::TetheringImpl ------------------------------------------- |
252 | 252 |
253 class TetheringHandler::TetheringImpl { | 253 class TetheringHandler::TetheringImpl { |
254 public: | 254 public: |
255 TetheringImpl( | 255 TetheringImpl( |
256 base::WeakPtr<TetheringHandler> handler, | 256 base::WeakPtr<TetheringHandler> handler, |
257 DevToolsHttpHandlerDelegate* delegate); | 257 DevToolsHttpHandlerDelegate* delegate); |
258 ~TetheringImpl(); | 258 ~TetheringImpl(); |
259 | 259 |
260 void Bind(scoped_refptr<DevToolsProtocol::Command> command); | 260 void Bind(scoped_refptr<DevToolsProtocol::Command> command, int port); |
261 void Unbind(scoped_refptr<DevToolsProtocol::Command> command); | 261 void Unbind(scoped_refptr<DevToolsProtocol::Command> command, int port); |
262 void Accepted(int port, const std::string& name); | 262 void Accepted(int port, const std::string& name); |
263 | 263 |
264 private: | 264 private: |
265 void SendAsyncResponse(scoped_refptr<DevToolsProtocol::Response> response); | 265 void SendInternalError(scoped_refptr<DevToolsProtocol::Command> command, |
266 const std::string& message); | |
266 | 267 |
267 base::WeakPtr<TetheringHandler> handler_; | 268 base::WeakPtr<TetheringHandler> handler_; |
268 DevToolsHttpHandlerDelegate* delegate_; | 269 DevToolsHttpHandlerDelegate* delegate_; |
269 | 270 |
270 typedef std::map<int, BoundSocket*> BoundSockets; | 271 typedef std::map<int, BoundSocket*> BoundSockets; |
271 BoundSockets bound_sockets_; | 272 BoundSockets bound_sockets_; |
272 }; | 273 }; |
273 | 274 |
274 TetheringHandler::TetheringImpl::TetheringImpl( | 275 TetheringHandler::TetheringImpl::TetheringImpl( |
275 base::WeakPtr<TetheringHandler> handler, | 276 base::WeakPtr<TetheringHandler> handler, |
276 DevToolsHttpHandlerDelegate* delegate) | 277 DevToolsHttpHandlerDelegate* delegate) |
277 : handler_(handler), | 278 : handler_(handler), |
278 delegate_(delegate) { | 279 delegate_(delegate) { |
279 } | 280 } |
280 | 281 |
281 TetheringHandler::TetheringImpl::~TetheringImpl() { | 282 TetheringHandler::TetheringImpl::~TetheringImpl() { |
282 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), | 283 STLDeleteContainerPairSecondPointers(bound_sockets_.begin(), |
283 bound_sockets_.end()); | 284 bound_sockets_.end()); |
284 } | 285 } |
285 | 286 |
286 void TetheringHandler::TetheringImpl::Bind( | 287 void TetheringHandler::TetheringImpl::Bind( |
287 scoped_refptr<DevToolsProtocol::Command> command) { | 288 scoped_refptr<DevToolsProtocol::Command> command, |
288 const std::string& portParamName = devtools::Tethering::bind::kParamPort; | 289 int port) { |
dgozman
2014/10/17 13:34:24
|port| should fit previous line.
vkuzkokov
2014/10/17 13:43:37
Done.
| |
289 int port = GetPort(command, portParamName); | 290 if (bound_sockets_.find(port) != bound_sockets_.end()) { |
290 if (port == 0) { | 291 SendInternalError(command, "Port already bound"); |
291 SendAsyncResponse(command->InvalidParamResponse(portParamName)); | |
292 return; | 292 return; |
293 } | 293 } |
294 | 294 |
295 if (bound_sockets_.find(port) != bound_sockets_.end()) { | |
296 SendAsyncResponse(command->InternalErrorResponse("Port already bound")); | |
297 return; | |
298 } | |
299 | |
300 BoundSocket::AcceptedCallback callback = base::Bind( | 295 BoundSocket::AcceptedCallback callback = base::Bind( |
301 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); | 296 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); |
302 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_)); | 297 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_)); |
303 if (!bound_socket->Listen(port)) { | 298 if (!bound_socket->Listen(port)) { |
304 SendAsyncResponse(command->InternalErrorResponse("Could not bind port")); | 299 SendInternalError(command, "Could not bind port"); |
305 return; | 300 return; |
306 } | 301 } |
307 | 302 |
308 bound_sockets_[port] = bound_socket.release(); | 303 bound_sockets_[port] = bound_socket.release(); |
309 SendAsyncResponse(command->SuccessResponse(NULL)); | 304 BrowserThread::PostTask( |
305 BrowserThread::UI, | |
306 FROM_HERE, | |
307 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command)); | |
310 } | 308 } |
311 | 309 |
312 void TetheringHandler::TetheringImpl::Unbind( | 310 void TetheringHandler::TetheringImpl::Unbind( |
313 scoped_refptr<DevToolsProtocol::Command> command) { | 311 scoped_refptr<DevToolsProtocol::Command> command, |
314 const std::string& portParamName = devtools::Tethering::unbind::kParamPort; | 312 int port) { |
dgozman
2014/10/17 13:34:24
ditto
vkuzkokov
2014/10/17 13:43:37
Done.
| |
315 int port = GetPort(command, portParamName); | |
316 if (port == 0) { | |
317 SendAsyncResponse(command->InvalidParamResponse(portParamName)); | |
318 return; | |
319 } | |
320 | 313 |
321 BoundSockets::iterator it = bound_sockets_.find(port); | 314 BoundSockets::iterator it = bound_sockets_.find(port); |
322 if (it == bound_sockets_.end()) { | 315 if (it == bound_sockets_.end()) { |
323 SendAsyncResponse(command->InternalErrorResponse("Port is not bound")); | 316 SendInternalError(command, "Port is not bound"); |
324 return; | 317 return; |
325 } | 318 } |
326 | 319 |
327 delete it->second; | 320 delete it->second; |
328 bound_sockets_.erase(it); | 321 bound_sockets_.erase(it); |
329 SendAsyncResponse(command->SuccessResponse(NULL)); | 322 BrowserThread::PostTask( |
323 BrowserThread::UI, | |
324 FROM_HERE, | |
325 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command)); | |
330 } | 326 } |
331 | 327 |
332 void TetheringHandler::TetheringImpl::Accepted( | 328 void TetheringHandler::TetheringImpl::Accepted( |
333 int port, const std::string& name) { | 329 int port, const std::string& name) { |
334 BrowserThread::PostTask( | 330 BrowserThread::PostTask( |
335 BrowserThread::UI, | 331 BrowserThread::UI, |
336 FROM_HERE, | 332 FROM_HERE, |
337 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); | 333 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); |
338 } | 334 } |
339 | 335 |
340 void TetheringHandler::TetheringImpl::SendAsyncResponse( | 336 void TetheringHandler::TetheringImpl::SendInternalError( |
341 scoped_refptr<DevToolsProtocol::Response> response) { | 337 scoped_refptr<DevToolsProtocol::Command> command, |
338 const std::string& message) { | |
342 BrowserThread::PostTask( | 339 BrowserThread::PostTask( |
343 BrowserThread::UI, | 340 BrowserThread::UI, |
344 FROM_HERE, | 341 FROM_HERE, |
345 base::Bind(&TetheringHandler::SendAsyncResponse, handler_, response)); | 342 base::Bind(&TetheringHandler::SendInternalError, handler_, |
343 command, message)); | |
346 } | 344 } |
347 | 345 |
346 | |
348 // TetheringHandler ---------------------------------------------------------- | 347 // TetheringHandler ---------------------------------------------------------- |
349 | 348 |
350 // static | 349 // static |
351 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; | 350 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; |
352 | 351 |
353 TetheringHandler::TetheringHandler( | 352 TetheringHandler::TetheringHandler( |
354 DevToolsHttpHandlerDelegate* delegate, | 353 DevToolsHttpHandlerDelegate* delegate, |
355 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) | 354 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) |
356 : delegate_(delegate), | 355 : delegate_(delegate), |
357 message_loop_proxy_(message_loop_proxy), | 356 message_loop_proxy_(message_loop_proxy), |
(...skipping 26 matching lines...) Expand all Loading... | |
384 return true; | 383 return true; |
385 if (impl_) | 384 if (impl_) |
386 return false; | 385 return false; |
387 is_active_ = true; | 386 is_active_ = true; |
388 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_); | 387 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_); |
389 return true; | 388 return true; |
390 } | 389 } |
391 | 390 |
392 scoped_refptr<DevToolsProtocol::Response> | 391 scoped_refptr<DevToolsProtocol::Response> |
393 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { | 392 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { |
393 const std::string& portParamName = devtools::Tethering::bind::kParamPort; | |
394 int port = GetPort(command, portParamName); | |
395 if (port == 0) | |
396 return command->InvalidParamResponse(portParamName); | |
397 | |
394 if (!Activate()) { | 398 if (!Activate()) { |
395 return command->ServerErrorResponse( | 399 return command->ServerErrorResponse( |
396 "Tethering is used by another connection"); | 400 "Tethering is used by another connection"); |
397 } | 401 } |
398 DCHECK(impl_); | 402 DCHECK(impl_); |
399 message_loop_proxy_->PostTask( | 403 message_loop_proxy_->PostTask( |
400 FROM_HERE, | 404 FROM_HERE, |
401 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), command)); | 405 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), |
406 command, port)); | |
402 return command->AsyncResponsePromise(); | 407 return command->AsyncResponsePromise(); |
403 } | 408 } |
404 | 409 |
405 scoped_refptr<DevToolsProtocol::Response> | 410 scoped_refptr<DevToolsProtocol::Response> |
406 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { | 411 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { |
412 const std::string& portParamName = devtools::Tethering::unbind::kParamPort; | |
413 int port = GetPort(command, portParamName); | |
414 if (port == 0) | |
415 return command->InvalidParamResponse(portParamName); | |
416 | |
407 if (!Activate()) { | 417 if (!Activate()) { |
408 return command->ServerErrorResponse( | 418 return command->ServerErrorResponse( |
409 "Tethering is used by another connection"); | 419 "Tethering is used by another connection"); |
410 } | 420 } |
411 DCHECK(impl_); | 421 DCHECK(impl_); |
412 message_loop_proxy_->PostTask( | 422 message_loop_proxy_->PostTask( |
413 FROM_HERE, | 423 FROM_HERE, |
414 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), command)); | 424 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), |
425 command, port)); | |
415 return command->AsyncResponsePromise(); | 426 return command->AsyncResponsePromise(); |
416 } | 427 } |
417 | 428 |
429 void TetheringHandler::SendBindSuccess( | |
430 scoped_refptr<DevToolsProtocol::Command> command) { | |
431 SendAsyncResponse(command->SuccessResponse(nullptr)); | |
432 } | |
433 | |
434 void TetheringHandler::SendUnbindSuccess( | |
435 scoped_refptr<DevToolsProtocol::Command> command) { | |
436 SendAsyncResponse(command->SuccessResponse(nullptr)); | |
437 } | |
438 | |
439 void TetheringHandler::SendInternalError( | |
440 scoped_refptr<DevToolsProtocol::Command> command, | |
441 const std::string& message) { | |
442 SendAsyncResponse(command->InternalErrorResponse(message)); | |
443 } | |
444 | |
418 } // namespace content | 445 } // namespace content |
OLD | NEW |