OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/devtools/port_forwarding_controller.h" | 5 #include "chrome/browser/devtools/port_forwarding_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 IsVersionLower(newest_version, current_version)) { | 255 IsVersionLower(newest_version, current_version)) { |
256 socket = browser->socket(); | 256 socket = browser->socket(); |
257 newest_version = current_version; | 257 newest_version = current_version; |
258 } | 258 } |
259 } | 259 } |
260 return socket; | 260 return socket; |
261 } | 261 } |
262 | 262 |
263 } // namespace | 263 } // namespace |
264 | 264 |
265 class TetheringAdbFilter : public base::RefCountedThreadSafe< | 265 class PortForwardingController::Connection |
266 TetheringAdbFilter, | 266 : public AdbWebSocket::Delegate, |
267 content::BrowserThread::DeleteOnUIThread> { | 267 public base::RefCountedThreadSafe< |
| 268 Connection, |
| 269 content::BrowserThread::DeleteOnUIThread> { |
268 public: | 270 public: |
269 typedef DevToolsAdbBridge::RemoteDevice::PortStatus PortStatus; | 271 typedef DevToolsAdbBridge::RemoteDevice::PortStatus PortStatus; |
270 typedef DevToolsAdbBridge::RemoteDevice::PortStatusMap PortStatusMap; | 272 typedef DevToolsAdbBridge::RemoteDevice::PortStatusMap PortStatusMap; |
271 | 273 |
272 TetheringAdbFilter(scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | 274 Connection(Registry* registry, |
273 base::MessageLoop* adb_message_loop, | 275 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, |
274 PrefService* pref_service, | 276 const std::string& socket, |
275 scoped_refptr<AdbWebSocket> web_socket); | 277 scoped_refptr<DevToolsAdbBridge> bridge, |
| 278 PrefService* pref_service); |
276 | 279 |
277 const PortStatusMap& GetPortStatusMap(); | 280 const PortStatusMap& GetPortStatusMap(); |
278 | 281 |
279 bool ProcessIncomingMessage(const std::string& message); | 282 void Shutdown(); |
280 | 283 |
281 private: | 284 private: |
282 friend struct content::BrowserThread::DeleteOnThread< | 285 friend struct content::BrowserThread::DeleteOnThread< |
283 content::BrowserThread::UI>; | 286 content::BrowserThread::UI>; |
284 friend class base::DeleteHelper<TetheringAdbFilter>; | 287 friend class base::DeleteHelper<Connection>; |
285 | 288 |
286 virtual ~TetheringAdbFilter(); | 289 virtual ~Connection(); |
287 | 290 |
288 typedef std::map<int, std::string> ForwardingMap; | 291 typedef std::map<int, std::string> ForwardingMap; |
289 | 292 |
290 typedef base::Callback<void(PortStatus)> CommandCallback; | 293 typedef base::Callback<void(PortStatus)> CommandCallback; |
291 typedef std::map<int, CommandCallback> CommandCallbackMap; | 294 typedef std::map<int, CommandCallback> CommandCallbackMap; |
292 | 295 |
293 void OnPrefsChange(); | 296 void OnPrefsChange(); |
294 | 297 |
295 void ChangeForwardingMap(ForwardingMap map); | 298 void ChangeForwardingMap(ForwardingMap map); |
296 | 299 |
297 void SerializeChanges(const std::string& method, | 300 void SerializeChanges(const std::string& method, |
298 const ForwardingMap& old_map, | 301 const ForwardingMap& old_map, |
299 const ForwardingMap& new_map); | 302 const ForwardingMap& new_map); |
300 | 303 |
301 void SendCommand(const std::string& method, int port); | 304 void SendCommand(const std::string& method, int port); |
302 bool ProcessResponse(const std::string& json); | 305 bool ProcessResponse(const std::string& json); |
303 | 306 |
304 void ProcessBindResponse(int port, PortStatus status); | 307 void ProcessBindResponse(int port, PortStatus status); |
305 void ProcessUnbindResponse(int port, PortStatus status); | 308 void ProcessUnbindResponse(int port, PortStatus status); |
306 void UpdateSocketCount(int port, int increment); | 309 void UpdateSocketCount(int port, int increment); |
307 void UpdatePortStatusMap(); | 310 void UpdatePortStatusMap(); |
308 void UpdatePortStatusMapOnUIThread(const PortStatusMap& status_map); | 311 void UpdatePortStatusMapOnUIThread(const PortStatusMap& status_map); |
309 | 312 |
| 313 // AdbWebSocket::Delegate implementation: |
| 314 virtual void OnSocketOpened() OVERRIDE; |
| 315 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
| 316 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; |
| 317 virtual bool ProcessIncomingMessage(const std::string& message) OVERRIDE; |
| 318 |
| 319 PortForwardingController::Registry* registry_; |
310 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device_; | 320 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device_; |
311 base::MessageLoop* adb_message_loop_; | 321 scoped_refptr<DevToolsAdbBridge> bridge_; |
312 PrefChangeRegistrar pref_change_registrar_; | 322 PrefChangeRegistrar pref_change_registrar_; |
313 scoped_refptr<AdbWebSocket> web_socket_; | 323 scoped_refptr<AdbWebSocket> web_socket_; |
314 int command_id_; | 324 int command_id_; |
315 ForwardingMap forwarding_map_; | 325 ForwardingMap forwarding_map_; |
316 CommandCallbackMap pending_responses_; | 326 CommandCallbackMap pending_responses_; |
317 PortStatusMap port_status_; | 327 PortStatusMap port_status_; |
318 PortStatusMap port_status_on_ui_thread_; | 328 PortStatusMap port_status_on_ui_thread_; |
319 | 329 |
320 DISALLOW_COPY_AND_ASSIGN(TetheringAdbFilter); | 330 DISALLOW_COPY_AND_ASSIGN(Connection); |
321 }; | 331 }; |
322 | 332 |
323 TetheringAdbFilter::TetheringAdbFilter( | 333 PortForwardingController::Connection::Connection( |
| 334 Registry* registry, |
324 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | 335 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, |
325 base::MessageLoop* adb_message_loop, | 336 const std::string& socket, |
326 PrefService* pref_service, | 337 scoped_refptr<DevToolsAdbBridge> bridge, |
327 scoped_refptr<AdbWebSocket> web_socket) | 338 PrefService* pref_service) |
328 : device_(device), | 339 : registry_(registry), |
329 adb_message_loop_(adb_message_loop), | 340 device_(device), |
330 web_socket_(web_socket), | 341 bridge_(bridge), |
331 command_id_(0) { | 342 command_id_(0) { |
332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
333 pref_change_registrar_.Init(pref_service); | 344 pref_change_registrar_.Init(pref_service); |
334 | 345 (*registry_)[device_->serial()] = this; |
335 OnPrefsChange(); | 346 web_socket_ = new AdbWebSocket( |
336 | 347 device, socket, kDevToolsRemoteBrowserTarget, |
337 base::Closure pref_callback = base::Bind( | 348 bridge_->GetAdbMessageLoop(), this); |
338 &TetheringAdbFilter::OnPrefsChange, base::Unretained(this)); | 349 AddRef(); // Balanced in OnSocketClosed(); |
339 pref_change_registrar_.Add( | |
340 prefs::kDevToolsPortForwardingEnabled, pref_callback); | |
341 pref_change_registrar_.Add( | |
342 prefs::kDevToolsPortForwardingConfig, pref_callback); | |
343 } | 350 } |
344 | 351 |
345 TetheringAdbFilter::~TetheringAdbFilter() { | 352 void PortForwardingController::Connection::Shutdown() { |
| 353 registry_ = NULL; |
| 354 // This will have no effect if the socket is not connected yet. |
| 355 web_socket_->Disconnect(); |
346 } | 356 } |
347 | 357 |
348 void TetheringAdbFilter::OnPrefsChange() { | 358 PortForwardingController::Connection::~Connection() { |
| 359 if (registry_) { |
| 360 DCHECK(registry_->find(device_->serial()) != registry_->end()); |
| 361 registry_->erase(device_->serial()); |
| 362 } |
| 363 } |
| 364 |
| 365 void PortForwardingController::Connection::OnPrefsChange() { |
349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 366 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
350 | 367 |
351 ForwardingMap new_forwarding_map; | 368 ForwardingMap new_forwarding_map; |
352 | 369 |
353 PrefService* pref_service = pref_change_registrar_.prefs(); | 370 PrefService* pref_service = pref_change_registrar_.prefs(); |
354 bool enabled = | 371 bool enabled = |
355 pref_service->GetBoolean(prefs::kDevToolsPortForwardingEnabled); | 372 pref_service->GetBoolean(prefs::kDevToolsPortForwardingEnabled); |
356 if (enabled) { | 373 if (enabled) { |
357 const DictionaryValue* dict = | 374 const DictionaryValue* dict = |
358 pref_service->GetDictionary(prefs::kDevToolsPortForwardingConfig); | 375 pref_service->GetDictionary(prefs::kDevToolsPortForwardingConfig); |
359 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 376 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
360 int port_num; | 377 int port_num; |
361 std::string location; | 378 std::string location; |
362 if (base::StringToInt(it.key(), &port_num) && | 379 if (base::StringToInt(it.key(), &port_num) && |
363 dict->GetString(it.key(), &location)) | 380 dict->GetString(it.key(), &location)) |
364 new_forwarding_map[port_num] = location; | 381 new_forwarding_map[port_num] = location; |
365 } | 382 } |
366 } | 383 } |
367 | 384 |
368 adb_message_loop_->PostTask( | 385 bridge_->GetAdbMessageLoop()->PostTask( |
369 FROM_HERE, | 386 FROM_HERE, |
370 base::Bind(&TetheringAdbFilter::ChangeForwardingMap, | 387 base::Bind(&Connection::ChangeForwardingMap, |
371 this, new_forwarding_map)); | 388 this, new_forwarding_map)); |
372 } | 389 } |
373 | 390 |
374 void TetheringAdbFilter::ChangeForwardingMap(ForwardingMap new_forwarding_map) { | 391 void PortForwardingController::Connection::ChangeForwardingMap( |
375 DCHECK_EQ(base::MessageLoop::current(), adb_message_loop_); | 392 ForwardingMap new_forwarding_map) { |
| 393 DCHECK_EQ(base::MessageLoop::current(), bridge_->GetAdbMessageLoop()); |
376 | 394 |
377 SerializeChanges(kTetheringUnbind, new_forwarding_map, forwarding_map_); | 395 SerializeChanges(kTetheringUnbind, new_forwarding_map, forwarding_map_); |
378 SerializeChanges(kTetheringBind, forwarding_map_, new_forwarding_map); | 396 SerializeChanges(kTetheringBind, forwarding_map_, new_forwarding_map); |
379 forwarding_map_ = new_forwarding_map; | 397 forwarding_map_ = new_forwarding_map; |
380 } | 398 } |
381 | 399 |
382 void TetheringAdbFilter::SerializeChanges(const std::string& method, | 400 void PortForwardingController::Connection::SerializeChanges( |
383 const ForwardingMap& old_map, | 401 const std::string& method, |
384 const ForwardingMap& new_map) { | 402 const ForwardingMap& old_map, |
| 403 const ForwardingMap& new_map) { |
385 for (ForwardingMap::const_iterator new_it(new_map.begin()); | 404 for (ForwardingMap::const_iterator new_it(new_map.begin()); |
386 new_it != new_map.end(); ++new_it) { | 405 new_it != new_map.end(); ++new_it) { |
387 int port = new_it->first; | 406 int port = new_it->first; |
388 const std::string& location = new_it->second; | 407 const std::string& location = new_it->second; |
389 ForwardingMap::const_iterator old_it = old_map.find(port); | 408 ForwardingMap::const_iterator old_it = old_map.find(port); |
390 if (old_it != old_map.end() && old_it->second == location) | 409 if (old_it != old_map.end() && old_it->second == location) |
391 continue; // The port points to the same location in both configs, skip. | 410 continue; // The port points to the same location in both configs, skip. |
392 | 411 |
393 SendCommand(method, port); | 412 SendCommand(method, port); |
394 } | 413 } |
395 } | 414 } |
396 | 415 |
397 void TetheringAdbFilter::SendCommand(const std::string& method, int port) { | 416 void PortForwardingController::Connection::SendCommand( |
| 417 const std::string& method, int port) { |
398 base::DictionaryValue params; | 418 base::DictionaryValue params; |
399 params.SetInteger(kPortAttribute, port); | 419 params.SetInteger(kPortAttribute, port); |
400 DevToolsProtocol::Command command(++command_id_, method, ¶ms); | 420 DevToolsProtocol::Command command(++command_id_, method, ¶ms); |
401 | 421 |
402 if (method == kTetheringBind) { | 422 if (method == kTetheringBind) { |
403 pending_responses_[command.id()] = | 423 pending_responses_[command.id()] = |
404 base::Bind(&TetheringAdbFilter::ProcessBindResponse, | 424 base::Bind(&Connection::ProcessBindResponse, |
405 base::Unretained(this), port); | 425 base::Unretained(this), port); |
406 #if defined(DEBUG_DEVTOOLS) | 426 #if defined(DEBUG_DEVTOOLS) |
407 port_status_[port] = kStatusConnecting; | 427 port_status_[port] = kStatusConnecting; |
408 UpdatePortStatusMap(); | 428 UpdatePortStatusMap(); |
409 #endif // defined(DEBUG_DEVTOOLS) | 429 #endif // defined(DEBUG_DEVTOOLS) |
410 } else { | 430 } else { |
411 DCHECK_EQ(kTetheringUnbind, method); | 431 DCHECK_EQ(kTetheringUnbind, method); |
412 | 432 |
413 PortStatusMap::iterator it = port_status_.find(port); | 433 PortStatusMap::iterator it = port_status_.find(port); |
414 if (it != port_status_.end() && it->second == kStatusError) { | 434 if (it != port_status_.end() && it->second == kStatusError) { |
415 // The bind command failed on this port, do not attempt unbind. | 435 // The bind command failed on this port, do not attempt unbind. |
416 port_status_.erase(it); | 436 port_status_.erase(it); |
417 UpdatePortStatusMap(); | 437 UpdatePortStatusMap(); |
418 return; | 438 return; |
419 } | 439 } |
420 | 440 |
421 pending_responses_[command.id()] = | 441 pending_responses_[command.id()] = |
422 base::Bind(&TetheringAdbFilter::ProcessUnbindResponse, | 442 base::Bind(&Connection::ProcessUnbindResponse, |
423 base::Unretained(this), port); | 443 base::Unretained(this), port); |
424 #if defined(DEBUG_DEVTOOLS) | 444 #if defined(DEBUG_DEVTOOLS) |
425 port_status_[port] = kStatusDisconnecting; | 445 port_status_[port] = kStatusDisconnecting; |
426 UpdatePortStatusMap(); | 446 UpdatePortStatusMap(); |
427 #endif // defined(DEBUG_DEVTOOLS) | 447 #endif // defined(DEBUG_DEVTOOLS) |
428 } | 448 } |
429 | 449 |
430 web_socket_->SendFrameOnHandlerThread(command.Serialize()); | 450 web_socket_->SendFrameOnHandlerThread(command.Serialize()); |
431 } | 451 } |
432 | 452 |
433 bool TetheringAdbFilter::ProcessResponse(const std::string& message) { | 453 bool PortForwardingController::Connection::ProcessResponse( |
| 454 const std::string& message) { |
434 scoped_ptr<DevToolsProtocol::Response> response( | 455 scoped_ptr<DevToolsProtocol::Response> response( |
435 DevToolsProtocol::ParseResponse(message)); | 456 DevToolsProtocol::ParseResponse(message)); |
436 if (!response) | 457 if (!response) |
437 return false; | 458 return false; |
438 | 459 |
439 CommandCallbackMap::iterator it = pending_responses_.find(response->id()); | 460 CommandCallbackMap::iterator it = pending_responses_.find(response->id()); |
440 if (it == pending_responses_.end()) | 461 if (it == pending_responses_.end()) |
441 return false; | 462 return false; |
442 | 463 |
443 it->second.Run(response->error_code() ? kStatusError : kStatusOK); | 464 it->second.Run(response->error_code() ? kStatusError : kStatusOK); |
444 pending_responses_.erase(it); | 465 pending_responses_.erase(it); |
445 return true; | 466 return true; |
446 } | 467 } |
447 | 468 |
448 void TetheringAdbFilter::ProcessBindResponse(int port, PortStatus status) { | 469 void PortForwardingController::Connection::ProcessBindResponse( |
| 470 int port, PortStatus status) { |
449 port_status_[port] = status; | 471 port_status_[port] = status; |
450 UpdatePortStatusMap(); | 472 UpdatePortStatusMap(); |
451 } | 473 } |
452 | 474 |
453 void TetheringAdbFilter::ProcessUnbindResponse(int port, PortStatus status) { | 475 void PortForwardingController::Connection::ProcessUnbindResponse( |
| 476 int port, PortStatus status) { |
454 PortStatusMap::iterator it = port_status_.find(port); | 477 PortStatusMap::iterator it = port_status_.find(port); |
455 if (it == port_status_.end()) | 478 if (it == port_status_.end()) |
456 return; | 479 return; |
457 if (status == kStatusError) | 480 if (status == kStatusError) |
458 it->second = status; | 481 it->second = status; |
459 else | 482 else |
460 port_status_.erase(it); | 483 port_status_.erase(it); |
461 UpdatePortStatusMap(); | 484 UpdatePortStatusMap(); |
462 } | 485 } |
463 | 486 |
464 void TetheringAdbFilter::UpdateSocketCount(int port, int increment) { | 487 void PortForwardingController::Connection::UpdateSocketCount( |
| 488 int port, int increment) { |
465 #if defined(DEBUG_DEVTOOLS) | 489 #if defined(DEBUG_DEVTOOLS) |
466 PortStatusMap::iterator it = port_status_.find(port); | 490 PortStatusMap::iterator it = port_status_.find(port); |
467 if (it == port_status_.end()) | 491 if (it == port_status_.end()) |
468 return; | 492 return; |
469 if (it->second < 0 || (it->second == 0 && increment < 0)) | 493 if (it->second < 0 || (it->second == 0 && increment < 0)) |
470 return; | 494 return; |
471 it->second += increment; | 495 it->second += increment; |
472 UpdatePortStatusMap(); | 496 UpdatePortStatusMap(); |
473 #endif // defined(DEBUG_DEVTOOLS) | 497 #endif // defined(DEBUG_DEVTOOLS) |
474 } | 498 } |
475 | 499 |
476 void TetheringAdbFilter::UpdatePortStatusMap() { | 500 void PortForwardingController::Connection::UpdatePortStatusMap() { |
477 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 501 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
478 base::Bind(&TetheringAdbFilter::UpdatePortStatusMapOnUIThread, | 502 base::Bind(&Connection::UpdatePortStatusMapOnUIThread, |
479 this, port_status_)); | 503 this, port_status_)); |
480 } | 504 } |
481 | 505 |
482 void TetheringAdbFilter::UpdatePortStatusMapOnUIThread( | 506 void PortForwardingController::Connection::UpdatePortStatusMapOnUIThread( |
483 const PortStatusMap& status_map) { | 507 const PortStatusMap& status_map) { |
484 port_status_on_ui_thread_ = status_map; | 508 port_status_on_ui_thread_ = status_map; |
485 } | 509 } |
486 | 510 |
487 const TetheringAdbFilter::PortStatusMap& | 511 const PortForwardingController::Connection::PortStatusMap& |
488 TetheringAdbFilter::GetPortStatusMap() { | 512 PortForwardingController::Connection::GetPortStatusMap() { |
489 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
490 return port_status_on_ui_thread_; | 514 return port_status_on_ui_thread_; |
491 } | 515 } |
492 | 516 |
493 bool TetheringAdbFilter::ProcessIncomingMessage(const std::string& message) { | 517 void PortForwardingController::Connection::OnSocketOpened() { |
494 DCHECK_EQ(base::MessageLoop::current(), adb_message_loop_); | 518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 519 if (!registry_) { |
| 520 // Socket was created after Shutdown was called. Disconnect immediately. |
| 521 web_socket_->Disconnect(); |
| 522 return; |
| 523 } |
| 524 OnPrefsChange(); |
| 525 base::Closure pref_callback = base::Bind( |
| 526 &Connection::OnPrefsChange, base::Unretained(this)); |
| 527 pref_change_registrar_.Add( |
| 528 prefs::kDevToolsPortForwardingEnabled, pref_callback); |
| 529 pref_change_registrar_.Add( |
| 530 prefs::kDevToolsPortForwardingConfig, pref_callback); |
| 531 } |
495 | 532 |
| 533 void PortForwardingController::Connection::OnFrameRead( |
| 534 const std::string& message) { |
| 535 } |
| 536 |
| 537 void PortForwardingController::Connection::OnSocketClosed( |
| 538 bool closed_by_device) { |
| 539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 540 Release(); // Balanced in the constructor. |
| 541 } |
| 542 |
| 543 bool PortForwardingController::Connection::ProcessIncomingMessage( |
| 544 const std::string& message) { |
| 545 DCHECK_EQ(base::MessageLoop::current(), bridge_->GetAdbMessageLoop()); |
496 if (ProcessResponse(message)) | 546 if (ProcessResponse(message)) |
497 return true; | 547 return true; |
498 | 548 |
499 scoped_ptr<DevToolsProtocol::Notification> notification( | 549 scoped_ptr<DevToolsProtocol::Notification> notification( |
500 DevToolsProtocol::ParseNotification(message)); | 550 DevToolsProtocol::ParseNotification(message)); |
501 if (!notification) | 551 if (!notification) |
502 return false; | 552 return false; |
503 | 553 |
504 if (notification->method() != kTetheringAccepted) | 554 if (notification->method() != kTetheringAccepted) |
505 return false; | 555 return false; |
506 | 556 |
507 DictionaryValue* params = notification->params(); | 557 DictionaryValue* params = notification->params(); |
508 if (!params) | 558 if (!params) |
509 return false; | 559 return false; |
510 | 560 |
511 int port; | 561 int port; |
512 std::string connection_id; | 562 std::string connection_id; |
513 if (!params->GetInteger(kPortAttribute, &port) || | 563 if (!params->GetInteger(kPortAttribute, &port) || |
514 !params->GetString(kConnectionIdAttribute, &connection_id)) | 564 !params->GetString(kConnectionIdAttribute, &connection_id)) |
515 return false; | 565 return false; |
516 | 566 |
517 std::map<int, std::string>::iterator it = forwarding_map_.find(port); | 567 std::map<int, std::string>::iterator it = forwarding_map_.find(port); |
518 if (it == forwarding_map_.end()) | 568 if (it == forwarding_map_.end()) |
519 return false; | 569 return false; |
520 | 570 |
521 std::string location = it->second; | 571 std::string location = it->second; |
522 | 572 |
523 SocketTunnel* tunnel = new SocketTunnel(location, | 573 SocketTunnel* tunnel = new SocketTunnel(location, |
524 base::Bind(&TetheringAdbFilter::UpdateSocketCount, this, port)); | 574 base::Bind(&Connection::UpdateSocketCount, this, port)); |
525 | 575 |
526 device_->OpenSocket(connection_id.c_str(), | 576 device_->OpenSocket(connection_id.c_str(), |
527 base::Bind(&SocketTunnel::Start, base::Unretained(tunnel))); | 577 base::Bind(&SocketTunnel::Start, base::Unretained(tunnel))); |
528 return true; | 578 return true; |
529 } | 579 } |
530 | 580 |
531 class PortForwardingController::Connection : public AdbWebSocket::Delegate { | |
532 public: | |
533 Connection( | |
534 Registry* registry, | |
535 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | |
536 const std::string& socket, | |
537 scoped_refptr<DevToolsAdbBridge> bridge, | |
538 PrefService* pref_service); | |
539 | |
540 void Shutdown(); | |
541 | |
542 TetheringAdbFilter::PortStatusMap port_status(); | |
543 | |
544 private: | |
545 virtual ~Connection(); | |
546 | |
547 virtual void OnSocketOpened() OVERRIDE; | |
548 virtual void OnFrameRead(const std::string& message) OVERRIDE; | |
549 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; | |
550 virtual bool ProcessIncomingMessage(const std::string& message) OVERRIDE; | |
551 | |
552 Registry* registry_; | |
553 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device_; | |
554 scoped_refptr<DevToolsAdbBridge> bridge_; | |
555 PrefService* pref_service_; | |
556 | |
557 scoped_refptr<TetheringAdbFilter> tethering_adb_filter_; | |
558 scoped_refptr<AdbWebSocket> web_socket_; | |
559 }; | |
560 | |
561 PortForwardingController::Connection::Connection( | |
562 Registry* registry, | |
563 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | |
564 const std::string& socket, | |
565 scoped_refptr<DevToolsAdbBridge> bridge, | |
566 PrefService* pref_service) | |
567 : registry_(registry), | |
568 device_(device), | |
569 bridge_(bridge), | |
570 pref_service_(pref_service) { | |
571 (*registry_)[device_->serial()] = this; | |
572 web_socket_ = new AdbWebSocket( | |
573 device, socket, kDevToolsRemoteBrowserTarget, | |
574 bridge->GetAdbMessageLoop(), this); | |
575 } | |
576 | |
577 void PortForwardingController::Connection::Shutdown() { | |
578 registry_ = NULL; | |
579 // This will have no effect if the socket is not connected yet. | |
580 web_socket_->Disconnect(); | |
581 } | |
582 | |
583 TetheringAdbFilter::PortStatusMap | |
584 PortForwardingController::Connection::port_status() { | |
585 if (tethering_adb_filter_) | |
586 return tethering_adb_filter_->GetPortStatusMap(); | |
587 else | |
588 return TetheringAdbFilter::PortStatusMap(); | |
589 } | |
590 | |
591 PortForwardingController::Connection::~Connection() { | |
592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
593 if (registry_) { | |
594 DCHECK(registry_->find(device_->serial()) != registry_->end()); | |
595 registry_->erase(device_->serial()); | |
596 } | |
597 } | |
598 | |
599 void PortForwardingController::Connection::OnSocketOpened() { | |
600 if (!registry_) { | |
601 // Socket was created after Shutdown was called. Disconnect immediately. | |
602 web_socket_->Disconnect(); | |
603 return; | |
604 } | |
605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
606 tethering_adb_filter_ = new TetheringAdbFilter( | |
607 device_, bridge_->GetAdbMessageLoop(), pref_service_, web_socket_); | |
608 } | |
609 | |
610 void PortForwardingController::Connection::OnFrameRead( | |
611 const std::string& message) { | |
612 } | |
613 | |
614 void PortForwardingController::Connection::OnSocketClosed( | |
615 bool closed_by_device) { | |
616 delete this; | |
617 } | |
618 | |
619 bool PortForwardingController::Connection::ProcessIncomingMessage( | |
620 const std::string& message) { | |
621 return tethering_adb_filter_->ProcessIncomingMessage(message); | |
622 } | |
623 | |
624 | |
625 PortForwardingController::PortForwardingController( | 581 PortForwardingController::PortForwardingController( |
626 scoped_refptr<DevToolsAdbBridge> bridge, | 582 scoped_refptr<DevToolsAdbBridge> bridge, |
627 PrefService* pref_service) | 583 PrefService* pref_service) |
628 : bridge_(bridge), | 584 : bridge_(bridge), |
629 pref_service_(pref_service) { | 585 pref_service_(pref_service) { |
630 } | 586 } |
631 | 587 |
632 PortForwardingController::~PortForwardingController() { | 588 PortForwardingController::~PortForwardingController() { |
633 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) | 589 for (Registry::iterator it = registry_.begin(); it != registry_.end(); ++it) |
634 it->second->Shutdown(); | 590 it->second->Shutdown(); |
635 } | 591 } |
636 | 592 |
637 void PortForwardingController::UpdateDeviceList( | 593 void PortForwardingController::UpdateDeviceList( |
638 const DevToolsAdbBridge::RemoteDevices& devices) { | 594 const DevToolsAdbBridge::RemoteDevices& devices) { |
639 for (DevToolsAdbBridge::RemoteDevices::const_iterator it = devices.begin(); | 595 for (DevToolsAdbBridge::RemoteDevices::const_iterator it = devices.begin(); |
640 it != devices.end(); ++it) { | 596 it != devices.end(); ++it) { |
641 Registry::iterator rit = registry_.find((*it)->serial()); | 597 Registry::iterator rit = registry_.find((*it)->serial()); |
642 if (rit == registry_.end()) { | 598 if (rit == registry_.end()) { |
643 std::string socket = FindBestSocketForTethering((*it)->browsers()); | 599 std::string socket = FindBestSocketForTethering((*it)->browsers()); |
644 if (!socket.empty() || (*it)->serial().empty()) { | 600 if (!socket.empty() || (*it)->serial().empty()) { |
645 // Will delete itself when disconnected. | 601 // Will delete itself when disconnected. |
646 new Connection( | 602 new Connection( |
647 ®istry_, (*it)->device(), socket, bridge_, pref_service_); | 603 ®istry_, (*it)->device(), socket, bridge_, pref_service_); |
648 } | 604 } |
649 } else { | 605 } else { |
650 (*it)->set_port_status((*rit).second->port_status()); | 606 (*it)->set_port_status((*rit).second->GetPortStatusMap()); |
651 } | 607 } |
652 } | 608 } |
653 } | 609 } |
OLD | NEW |