Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Side by Side Diff: content/browser/devtools/tethering_handler.cc

Issue 645033005: [DevTools] Refactor tethering to allow for minimal changes when moving to generated handler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/singleUse
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/devtools/tethering_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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, int port) {
288 const std::string& portParamName = devtools::Tethering::bind::kParamPort; 289 if (bound_sockets_.find(port) != bound_sockets_.end()) {
289 int port = GetPort(command, portParamName); 290 SendInternalError(command, "Port already bound");
290 if (port == 0) {
291 SendAsyncResponse(command->InvalidParamResponse(portParamName));
292 return; 291 return;
293 } 292 }
294 293
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( 294 BoundSocket::AcceptedCallback callback = base::Bind(
301 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this)); 295 &TetheringHandler::TetheringImpl::Accepted, base::Unretained(this));
302 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_)); 296 scoped_ptr<BoundSocket> bound_socket(new BoundSocket(callback, delegate_));
303 if (!bound_socket->Listen(port)) { 297 if (!bound_socket->Listen(port)) {
304 SendAsyncResponse(command->InternalErrorResponse("Could not bind port")); 298 SendInternalError(command, "Could not bind port");
305 return; 299 return;
306 } 300 }
307 301
308 bound_sockets_[port] = bound_socket.release(); 302 bound_sockets_[port] = bound_socket.release();
309 SendAsyncResponse(command->SuccessResponse(NULL)); 303 BrowserThread::PostTask(
304 BrowserThread::UI,
305 FROM_HERE,
306 base::Bind(&TetheringHandler::SendBindSuccess, handler_, command));
310 } 307 }
311 308
312 void TetheringHandler::TetheringImpl::Unbind( 309 void TetheringHandler::TetheringImpl::Unbind(
313 scoped_refptr<DevToolsProtocol::Command> command) { 310 scoped_refptr<DevToolsProtocol::Command> command, int port) {
314 const std::string& portParamName = devtools::Tethering::unbind::kParamPort;
315 int port = GetPort(command, portParamName);
316 if (port == 0) {
317 SendAsyncResponse(command->InvalidParamResponse(portParamName));
318 return;
319 }
320 311
321 BoundSockets::iterator it = bound_sockets_.find(port); 312 BoundSockets::iterator it = bound_sockets_.find(port);
322 if (it == bound_sockets_.end()) { 313 if (it == bound_sockets_.end()) {
323 SendAsyncResponse(command->InternalErrorResponse("Port is not bound")); 314 SendInternalError(command, "Port is not bound");
324 return; 315 return;
325 } 316 }
326 317
327 delete it->second; 318 delete it->second;
328 bound_sockets_.erase(it); 319 bound_sockets_.erase(it);
329 SendAsyncResponse(command->SuccessResponse(NULL)); 320 BrowserThread::PostTask(
321 BrowserThread::UI,
322 FROM_HERE,
323 base::Bind(&TetheringHandler::SendUnbindSuccess, handler_, command));
330 } 324 }
331 325
332 void TetheringHandler::TetheringImpl::Accepted( 326 void TetheringHandler::TetheringImpl::Accepted(
333 int port, const std::string& name) { 327 int port, const std::string& name) {
334 BrowserThread::PostTask( 328 BrowserThread::PostTask(
335 BrowserThread::UI, 329 BrowserThread::UI,
336 FROM_HERE, 330 FROM_HERE,
337 base::Bind(&TetheringHandler::Accepted, handler_, port, name)); 331 base::Bind(&TetheringHandler::Accepted, handler_, port, name));
338 } 332 }
339 333
340 void TetheringHandler::TetheringImpl::SendAsyncResponse( 334 void TetheringHandler::TetheringImpl::SendInternalError(
341 scoped_refptr<DevToolsProtocol::Response> response) { 335 scoped_refptr<DevToolsProtocol::Command> command,
336 const std::string& message) {
342 BrowserThread::PostTask( 337 BrowserThread::PostTask(
343 BrowserThread::UI, 338 BrowserThread::UI,
344 FROM_HERE, 339 FROM_HERE,
345 base::Bind(&TetheringHandler::SendAsyncResponse, handler_, response)); 340 base::Bind(&TetheringHandler::SendInternalError, handler_,
341 command, message));
346 } 342 }
347 343
344
348 // TetheringHandler ---------------------------------------------------------- 345 // TetheringHandler ----------------------------------------------------------
349 346
350 // static 347 // static
351 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr; 348 TetheringHandler::TetheringImpl* TetheringHandler::impl_ = nullptr;
352 349
353 TetheringHandler::TetheringHandler( 350 TetheringHandler::TetheringHandler(
354 DevToolsHttpHandlerDelegate* delegate, 351 DevToolsHttpHandlerDelegate* delegate,
355 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) 352 scoped_refptr<base::MessageLoopProxy> message_loop_proxy)
356 : delegate_(delegate), 353 : delegate_(delegate),
357 message_loop_proxy_(message_loop_proxy), 354 message_loop_proxy_(message_loop_proxy),
(...skipping 26 matching lines...) Expand all
384 return true; 381 return true;
385 if (impl_) 382 if (impl_)
386 return false; 383 return false;
387 is_active_ = true; 384 is_active_ = true;
388 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_); 385 impl_ = new TetheringImpl(weak_factory_.GetWeakPtr(), delegate_);
389 return true; 386 return true;
390 } 387 }
391 388
392 scoped_refptr<DevToolsProtocol::Response> 389 scoped_refptr<DevToolsProtocol::Response>
393 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) { 390 TetheringHandler::OnBind(scoped_refptr<DevToolsProtocol::Command> command) {
391 const std::string& portParamName = devtools::Tethering::bind::kParamPort;
392 int port = GetPort(command, portParamName);
393 if (port == 0)
394 return command->InvalidParamResponse(portParamName);
395
394 if (!Activate()) { 396 if (!Activate()) {
395 return command->ServerErrorResponse( 397 return command->ServerErrorResponse(
396 "Tethering is used by another connection"); 398 "Tethering is used by another connection");
397 } 399 }
398 DCHECK(impl_); 400 DCHECK(impl_);
399 message_loop_proxy_->PostTask( 401 message_loop_proxy_->PostTask(
400 FROM_HERE, 402 FROM_HERE,
401 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_), command)); 403 base::Bind(&TetheringImpl::Bind, base::Unretained(impl_),
404 command, port));
402 return command->AsyncResponsePromise(); 405 return command->AsyncResponsePromise();
403 } 406 }
404 407
405 scoped_refptr<DevToolsProtocol::Response> 408 scoped_refptr<DevToolsProtocol::Response>
406 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) { 409 TetheringHandler::OnUnbind(scoped_refptr<DevToolsProtocol::Command> command) {
410 const std::string& portParamName = devtools::Tethering::unbind::kParamPort;
411 int port = GetPort(command, portParamName);
412 if (port == 0)
413 return command->InvalidParamResponse(portParamName);
414
407 if (!Activate()) { 415 if (!Activate()) {
408 return command->ServerErrorResponse( 416 return command->ServerErrorResponse(
409 "Tethering is used by another connection"); 417 "Tethering is used by another connection");
410 } 418 }
411 DCHECK(impl_); 419 DCHECK(impl_);
412 message_loop_proxy_->PostTask( 420 message_loop_proxy_->PostTask(
413 FROM_HERE, 421 FROM_HERE,
414 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_), command)); 422 base::Bind(&TetheringImpl::Unbind, base::Unretained(impl_),
423 command, port));
415 return command->AsyncResponsePromise(); 424 return command->AsyncResponsePromise();
416 } 425 }
417 426
427 void TetheringHandler::SendBindSuccess(
428 scoped_refptr<DevToolsProtocol::Command> command) {
429 SendAsyncResponse(command->SuccessResponse(nullptr));
430 }
431
432 void TetheringHandler::SendUnbindSuccess(
433 scoped_refptr<DevToolsProtocol::Command> command) {
434 SendAsyncResponse(command->SuccessResponse(nullptr));
435 }
436
437 void TetheringHandler::SendInternalError(
438 scoped_refptr<DevToolsProtocol::Command> command,
439 const std::string& message) {
440 SendAsyncResponse(command->InternalErrorResponse(message));
441 }
442
418 } // namespace content 443 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/tethering_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698