OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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; |
pfeldman
2013/08/30 09:39:14
Lets balance this in destructor.
Vladislav Kaznacheev
2013/08/30 11:56:01
Done.
| |
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(); |
pfeldman
2013/08/30 09:39:14
You anyways die on UI thread.
Vladislav Kaznacheev
2013/08/30 11:56:01
AddRef is required. Otherwise the ref count here i
| |
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 } | |
360 | |
361 void PortForwardingController::Connection::OnPrefsChange() { | |
349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
350 | 363 |
351 ForwardingMap new_forwarding_map; | 364 ForwardingMap new_forwarding_map; |
352 | 365 |
353 PrefService* pref_service = pref_change_registrar_.prefs(); | 366 PrefService* pref_service = pref_change_registrar_.prefs(); |
354 bool enabled = | 367 bool enabled = |
355 pref_service->GetBoolean(prefs::kDevToolsPortForwardingEnabled); | 368 pref_service->GetBoolean(prefs::kDevToolsPortForwardingEnabled); |
356 if (enabled) { | 369 if (enabled) { |
357 const DictionaryValue* dict = | 370 const DictionaryValue* dict = |
358 pref_service->GetDictionary(prefs::kDevToolsPortForwardingConfig); | 371 pref_service->GetDictionary(prefs::kDevToolsPortForwardingConfig); |
359 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 372 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
360 int port_num; | 373 int port_num; |
361 std::string location; | 374 std::string location; |
362 if (base::StringToInt(it.key(), &port_num) && | 375 if (base::StringToInt(it.key(), &port_num) && |
363 dict->GetString(it.key(), &location)) | 376 dict->GetString(it.key(), &location)) |
364 new_forwarding_map[port_num] = location; | 377 new_forwarding_map[port_num] = location; |
365 } | 378 } |
366 } | 379 } |
367 | 380 |
368 adb_message_loop_->PostTask( | 381 bridge_->GetAdbMessageLoop()->PostTask( |
369 FROM_HERE, | 382 FROM_HERE, |
370 base::Bind(&TetheringAdbFilter::ChangeForwardingMap, | 383 base::Bind(&Connection::ChangeForwardingMap, |
371 this, new_forwarding_map)); | 384 this, new_forwarding_map)); |
372 } | 385 } |
373 | 386 |
374 void TetheringAdbFilter::ChangeForwardingMap(ForwardingMap new_forwarding_map) { | 387 void PortForwardingController::Connection::ChangeForwardingMap( |
375 DCHECK_EQ(base::MessageLoop::current(), adb_message_loop_); | 388 ForwardingMap new_forwarding_map) { |
389 DCHECK_EQ(base::MessageLoop::current(), bridge_->GetAdbMessageLoop()); | |
376 | 390 |
377 SerializeChanges(kTetheringUnbind, new_forwarding_map, forwarding_map_); | 391 SerializeChanges(kTetheringUnbind, new_forwarding_map, forwarding_map_); |
378 SerializeChanges(kTetheringBind, forwarding_map_, new_forwarding_map); | 392 SerializeChanges(kTetheringBind, forwarding_map_, new_forwarding_map); |
379 forwarding_map_ = new_forwarding_map; | 393 forwarding_map_ = new_forwarding_map; |
380 } | 394 } |
381 | 395 |
382 void TetheringAdbFilter::SerializeChanges(const std::string& method, | 396 void PortForwardingController::Connection::SerializeChanges( |
383 const ForwardingMap& old_map, | 397 const std::string& method, |
384 const ForwardingMap& new_map) { | 398 const ForwardingMap& old_map, |
399 const ForwardingMap& new_map) { | |
385 for (ForwardingMap::const_iterator new_it(new_map.begin()); | 400 for (ForwardingMap::const_iterator new_it(new_map.begin()); |
386 new_it != new_map.end(); ++new_it) { | 401 new_it != new_map.end(); ++new_it) { |
387 int port = new_it->first; | 402 int port = new_it->first; |
388 const std::string& location = new_it->second; | 403 const std::string& location = new_it->second; |
389 ForwardingMap::const_iterator old_it = old_map.find(port); | 404 ForwardingMap::const_iterator old_it = old_map.find(port); |
390 if (old_it != old_map.end() && old_it->second == location) | 405 if (old_it != old_map.end() && old_it->second == location) |
391 continue; // The port points to the same location in both configs, skip. | 406 continue; // The port points to the same location in both configs, skip. |
392 | 407 |
393 SendCommand(method, port); | 408 SendCommand(method, port); |
394 } | 409 } |
395 } | 410 } |
396 | 411 |
397 void TetheringAdbFilter::SendCommand(const std::string& method, int port) { | 412 void PortForwardingController::Connection::SendCommand( |
413 const std::string& method, int port) { | |
398 base::DictionaryValue params; | 414 base::DictionaryValue params; |
399 params.SetInteger(kPortAttribute, port); | 415 params.SetInteger(kPortAttribute, port); |
400 DevToolsProtocol::Command command(++command_id_, method, ¶ms); | 416 DevToolsProtocol::Command command(++command_id_, method, ¶ms); |
401 | 417 |
402 if (method == kTetheringBind) { | 418 if (method == kTetheringBind) { |
403 pending_responses_[command.id()] = | 419 pending_responses_[command.id()] = |
404 base::Bind(&TetheringAdbFilter::ProcessBindResponse, | 420 base::Bind(&Connection::ProcessBindResponse, |
405 base::Unretained(this), port); | 421 base::Unretained(this), port); |
406 #if defined(DEBUG_DEVTOOLS) | 422 #if defined(DEBUG_DEVTOOLS) |
407 port_status_[port] = kStatusConnecting; | 423 port_status_[port] = kStatusConnecting; |
408 UpdatePortStatusMap(); | 424 UpdatePortStatusMap(); |
409 #endif // defined(DEBUG_DEVTOOLS) | 425 #endif // defined(DEBUG_DEVTOOLS) |
410 } else { | 426 } else { |
411 DCHECK_EQ(kTetheringUnbind, method); | 427 DCHECK_EQ(kTetheringUnbind, method); |
412 | 428 |
413 PortStatusMap::iterator it = port_status_.find(port); | 429 PortStatusMap::iterator it = port_status_.find(port); |
414 if (it != port_status_.end() && it->second == kStatusError) { | 430 if (it != port_status_.end() && it->second == kStatusError) { |
415 // The bind command failed on this port, do not attempt unbind. | 431 // The bind command failed on this port, do not attempt unbind. |
416 port_status_.erase(it); | 432 port_status_.erase(it); |
417 UpdatePortStatusMap(); | 433 UpdatePortStatusMap(); |
418 return; | 434 return; |
419 } | 435 } |
420 | 436 |
421 pending_responses_[command.id()] = | 437 pending_responses_[command.id()] = |
422 base::Bind(&TetheringAdbFilter::ProcessUnbindResponse, | 438 base::Bind(&Connection::ProcessUnbindResponse, |
423 base::Unretained(this), port); | 439 base::Unretained(this), port); |
424 #if defined(DEBUG_DEVTOOLS) | 440 #if defined(DEBUG_DEVTOOLS) |
425 port_status_[port] = kStatusDisconnecting; | 441 port_status_[port] = kStatusDisconnecting; |
426 UpdatePortStatusMap(); | 442 UpdatePortStatusMap(); |
427 #endif // defined(DEBUG_DEVTOOLS) | 443 #endif // defined(DEBUG_DEVTOOLS) |
428 } | 444 } |
429 | 445 |
430 web_socket_->SendFrameOnHandlerThread(command.Serialize()); | 446 web_socket_->SendFrameOnHandlerThread(command.Serialize()); |
431 } | 447 } |
432 | 448 |
433 bool TetheringAdbFilter::ProcessResponse(const std::string& message) { | 449 bool PortForwardingController::Connection::ProcessResponse( |
450 const std::string& message) { | |
434 scoped_ptr<DevToolsProtocol::Response> response( | 451 scoped_ptr<DevToolsProtocol::Response> response( |
435 DevToolsProtocol::ParseResponse(message)); | 452 DevToolsProtocol::ParseResponse(message)); |
436 if (!response) | 453 if (!response) |
437 return false; | 454 return false; |
438 | 455 |
439 CommandCallbackMap::iterator it = pending_responses_.find(response->id()); | 456 CommandCallbackMap::iterator it = pending_responses_.find(response->id()); |
440 if (it == pending_responses_.end()) | 457 if (it == pending_responses_.end()) |
441 return false; | 458 return false; |
442 | 459 |
443 it->second.Run(response->error_code() ? kStatusError : kStatusOK); | 460 it->second.Run(response->error_code() ? kStatusError : kStatusOK); |
444 pending_responses_.erase(it); | 461 pending_responses_.erase(it); |
445 return true; | 462 return true; |
446 } | 463 } |
447 | 464 |
448 void TetheringAdbFilter::ProcessBindResponse(int port, PortStatus status) { | 465 void PortForwardingController::Connection::ProcessBindResponse( |
466 int port, PortStatus status) { | |
449 port_status_[port] = status; | 467 port_status_[port] = status; |
450 UpdatePortStatusMap(); | 468 UpdatePortStatusMap(); |
451 } | 469 } |
452 | 470 |
453 void TetheringAdbFilter::ProcessUnbindResponse(int port, PortStatus status) { | 471 void PortForwardingController::Connection::ProcessUnbindResponse( |
472 int port, PortStatus status) { | |
454 PortStatusMap::iterator it = port_status_.find(port); | 473 PortStatusMap::iterator it = port_status_.find(port); |
455 if (it == port_status_.end()) | 474 if (it == port_status_.end()) |
456 return; | 475 return; |
457 if (status == kStatusError) | 476 if (status == kStatusError) |
458 it->second = status; | 477 it->second = status; |
459 else | 478 else |
460 port_status_.erase(it); | 479 port_status_.erase(it); |
461 UpdatePortStatusMap(); | 480 UpdatePortStatusMap(); |
462 } | 481 } |
463 | 482 |
464 void TetheringAdbFilter::UpdateSocketCount(int port, int increment) { | 483 void PortForwardingController::Connection::UpdateSocketCount( |
484 int port, int increment) { | |
465 #if defined(DEBUG_DEVTOOLS) | 485 #if defined(DEBUG_DEVTOOLS) |
466 PortStatusMap::iterator it = port_status_.find(port); | 486 PortStatusMap::iterator it = port_status_.find(port); |
467 if (it == port_status_.end()) | 487 if (it == port_status_.end()) |
468 return; | 488 return; |
469 if (it->second < 0 || (it->second == 0 && increment < 0)) | 489 if (it->second < 0 || (it->second == 0 && increment < 0)) |
470 return; | 490 return; |
471 it->second += increment; | 491 it->second += increment; |
472 UpdatePortStatusMap(); | 492 UpdatePortStatusMap(); |
473 #endif // defined(DEBUG_DEVTOOLS) | 493 #endif // defined(DEBUG_DEVTOOLS) |
474 } | 494 } |
475 | 495 |
476 void TetheringAdbFilter::UpdatePortStatusMap() { | 496 void PortForwardingController::Connection::UpdatePortStatusMap() { |
477 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 497 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
478 base::Bind(&TetheringAdbFilter::UpdatePortStatusMapOnUIThread, | 498 base::Bind(&Connection::UpdatePortStatusMapOnUIThread, |
479 this, port_status_)); | 499 this, port_status_)); |
480 } | 500 } |
481 | 501 |
482 void TetheringAdbFilter::UpdatePortStatusMapOnUIThread( | 502 void PortForwardingController::Connection::UpdatePortStatusMapOnUIThread( |
483 const PortStatusMap& status_map) { | 503 const PortStatusMap& status_map) { |
484 port_status_on_ui_thread_ = status_map; | 504 port_status_on_ui_thread_ = status_map; |
485 } | 505 } |
486 | 506 |
487 const TetheringAdbFilter::PortStatusMap& | 507 const PortForwardingController::Connection::PortStatusMap& |
488 TetheringAdbFilter::GetPortStatusMap() { | 508 PortForwardingController::Connection::GetPortStatusMap() { |
489 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 509 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
490 return port_status_on_ui_thread_; | 510 return port_status_on_ui_thread_; |
491 } | 511 } |
492 | 512 |
493 bool TetheringAdbFilter::ProcessIncomingMessage(const std::string& message) { | 513 void PortForwardingController::Connection::OnSocketOpened() { |
494 DCHECK_EQ(base::MessageLoop::current(), adb_message_loop_); | 514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
515 if (!registry_) { | |
516 // Socket was created after Shutdown was called. Disconnect immediately. | |
517 web_socket_->Disconnect(); | |
518 return; | |
519 } | |
520 OnPrefsChange(); | |
521 base::Closure pref_callback = base::Bind( | |
522 &Connection::OnPrefsChange, base::Unretained(this)); | |
523 pref_change_registrar_.Add( | |
524 prefs::kDevToolsPortForwardingEnabled, pref_callback); | |
525 pref_change_registrar_.Add( | |
526 prefs::kDevToolsPortForwardingConfig, pref_callback); | |
527 } | |
495 | 528 |
529 void PortForwardingController::Connection::OnFrameRead( | |
530 const std::string& message) { | |
531 } | |
532 | |
533 void PortForwardingController::Connection::OnSocketClosed( | |
534 bool closed_by_device) { | |
535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
536 if (registry_) { | |
537 DCHECK(registry_->find(device_->serial()) != registry_->end()); | |
538 registry_->erase(device_->serial()); | |
539 } | |
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 |