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 "net/dns/mdns_client_impl.h" | 5 #include "net/dns/mdns_client_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/time/default_clock.h" | 10 #include "base/time/default_clock.h" |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 } | 427 } |
428 | 428 |
429 void MDnsClientImpl::Core::QueryCache( | 429 void MDnsClientImpl::Core::QueryCache( |
430 uint16 rrtype, const std::string& name, | 430 uint16 rrtype, const std::string& name, |
431 std::vector<const RecordParsed*>* records) const { | 431 std::vector<const RecordParsed*>* records) const { |
432 cache_.FindDnsRecords(rrtype, name, records, base::Time::Now()); | 432 cache_.FindDnsRecords(rrtype, name, records, base::Time::Now()); |
433 } | 433 } |
434 | 434 |
435 MDnsClientImpl::MDnsClientImpl( | 435 MDnsClientImpl::MDnsClientImpl( |
436 scoped_ptr<MDnsConnection::SocketFactory> socket_factory) | 436 scoped_ptr<MDnsConnection::SocketFactory> socket_factory) |
437 : listen_refs_(0), socket_factory_(socket_factory.Pass()) { | 437 : socket_factory_(socket_factory.Pass()) { |
438 } | 438 } |
439 | 439 |
440 MDnsClientImpl::~MDnsClientImpl() { | 440 MDnsClientImpl::~MDnsClientImpl() { |
441 } | 441 } |
442 | 442 |
443 bool MDnsClientImpl::AddListenRef() { | 443 bool MDnsClientImpl::StartListening() { |
444 if (!core_.get()) { | 444 DCHECK(!core_.get()); |
445 core_.reset(new Core(this, socket_factory_.get())); | 445 core_.reset(new Core(this, socket_factory_.get())); |
446 if (!core_->Init()) { | 446 if (!core_->Init()) { |
447 core_.reset(); | 447 core_.reset(); |
448 return false; | 448 return false; |
449 } | |
450 } | 449 } |
451 listen_refs_++; | |
452 return true; | 450 return true; |
453 } | 451 } |
454 | 452 |
455 void MDnsClientImpl::SubtractListenRef() { | 453 void MDnsClientImpl::StopListening() { |
456 listen_refs_--; | 454 core_.reset(); |
457 if (listen_refs_ == 0) { | |
458 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
459 &MDnsClientImpl::Shutdown, base::Unretained(this))); | |
460 } | |
461 } | 455 } |
462 | 456 |
463 void MDnsClientImpl::Shutdown() { | 457 bool MDnsClientImpl::IsListening() const { |
464 // We need to check that new listeners haven't been created. | |
465 if (listen_refs_ == 0) { | |
466 core_.reset(); | |
467 } | |
468 } | |
469 | |
470 bool MDnsClientImpl::IsListeningForTests() { | |
471 return core_.get() != NULL; | 458 return core_.get() != NULL; |
472 } | 459 } |
473 | 460 |
474 scoped_ptr<MDnsListener> MDnsClientImpl::CreateListener( | 461 scoped_ptr<MDnsListener> MDnsClientImpl::CreateListener( |
475 uint16 rrtype, | 462 uint16 rrtype, |
476 const std::string& name, | 463 const std::string& name, |
477 MDnsListener::Delegate* delegate) { | 464 MDnsListener::Delegate* delegate) { |
478 return scoped_ptr<net::MDnsListener>( | 465 return scoped_ptr<net::MDnsListener>( |
479 new MDnsListenerImpl(rrtype, name, delegate, this)); | 466 new MDnsListenerImpl(rrtype, name, delegate, this)); |
480 } | 467 } |
(...skipping 12 matching lines...) Expand all Loading... |
493 const std::string& name, | 480 const std::string& name, |
494 MDnsListener::Delegate* delegate, | 481 MDnsListener::Delegate* delegate, |
495 MDnsClientImpl* client) | 482 MDnsClientImpl* client) |
496 : rrtype_(rrtype), name_(name), client_(client), delegate_(delegate), | 483 : rrtype_(rrtype), name_(name), client_(client), delegate_(delegate), |
497 started_(false) { | 484 started_(false) { |
498 } | 485 } |
499 | 486 |
500 bool MDnsListenerImpl::Start() { | 487 bool MDnsListenerImpl::Start() { |
501 DCHECK(!started_); | 488 DCHECK(!started_); |
502 | 489 |
503 if (!client_->AddListenRef()) return false; | |
504 started_ = true; | 490 started_ = true; |
505 | 491 |
506 DCHECK(client_->core()); | 492 DCHECK(client_->core()); |
507 client_->core()->AddListener(this); | 493 client_->core()->AddListener(this); |
508 | 494 |
509 return true; | 495 return true; |
510 } | 496 } |
511 | 497 |
512 MDnsListenerImpl::~MDnsListenerImpl() { | 498 MDnsListenerImpl::~MDnsListenerImpl() { |
513 if (started_) { | 499 if (started_) { |
514 DCHECK(client_->core()); | 500 DCHECK(client_->core()); |
515 client_->core()->RemoveListener(this); | 501 client_->core()->RemoveListener(this); |
516 client_->SubtractListenRef(); | |
517 } | 502 } |
518 } | 503 } |
519 | 504 |
520 const std::string& MDnsListenerImpl::GetName() const { | 505 const std::string& MDnsListenerImpl::GetName() const { |
521 return name_; | 506 return name_; |
522 } | 507 } |
523 | 508 |
524 uint16 MDnsListenerImpl::GetType() const { | 509 uint16 MDnsListenerImpl::GetType() const { |
525 return rrtype_; | 510 return rrtype_; |
526 } | 511 } |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 | 663 |
679 void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) { | 664 void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) { |
680 TriggerCallback(RESULT_NSEC, NULL); | 665 TriggerCallback(RESULT_NSEC, NULL); |
681 } | 666 } |
682 | 667 |
683 void MDnsTransactionImpl::OnCachePurged() { | 668 void MDnsTransactionImpl::OnCachePurged() { |
684 // TODO(noamsml): Cache purge situations not yet implemented | 669 // TODO(noamsml): Cache purge situations not yet implemented |
685 } | 670 } |
686 | 671 |
687 } // namespace net | 672 } // namespace net |
OLD | NEW |