| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <utility> | 5 #include <utility> |
| 6 | 6 |
| 7 #include "base/location.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/single_thread_task_runner.h" |
| 10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 11 #include "chrome/common/local_discovery/service_discovery_client_impl.h" | 13 #include "chrome/common/local_discovery/service_discovery_client_impl.h" |
| 12 #include "net/dns/dns_protocol.h" | 14 #include "net/dns/dns_protocol.h" |
| 13 #include "net/dns/record_rdata.h" | 15 #include "net/dns/record_rdata.h" |
| 14 | 16 |
| 15 namespace local_discovery { | 17 namespace local_discovery { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 // TODO(noamsml): Make this configurable through the LocalDomainResolver | 20 // TODO(noamsml): Make this configurable through the LocalDomainResolver |
| 19 // interface. | 21 // interface. |
| 20 const int kLocalDomainSecondAddressTimeoutMs = 100; | 22 const int kLocalDomainSecondAddressTimeoutMs = 100; |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 found->second->set_has_srv(true); | 265 found->second->set_has_srv(true); |
| 264 } | 266 } |
| 265 } | 267 } |
| 266 | 268 |
| 267 void ServiceWatcherImpl::DeferUpdate(ServiceWatcher::UpdateType update_type, | 269 void ServiceWatcherImpl::DeferUpdate(ServiceWatcher::UpdateType update_type, |
| 268 const std::string& service_name) { | 270 const std::string& service_name) { |
| 269 ServiceListenersMap::iterator found = services_.find(service_name); | 271 ServiceListenersMap::iterator found = services_.find(service_name); |
| 270 | 272 |
| 271 if (found != services_.end() && !found->second->update_pending()) { | 273 if (found != services_.end() && !found->second->update_pending()) { |
| 272 found->second->set_update_pending(true); | 274 found->second->set_update_pending(true); |
| 273 base::MessageLoop::current()->PostTask( | 275 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 274 FROM_HERE, | 276 FROM_HERE, base::Bind(&ServiceWatcherImpl::DeliverDeferredUpdate, |
| 275 base::Bind(&ServiceWatcherImpl::DeliverDeferredUpdate, AsWeakPtr(), | 277 AsWeakPtr(), update_type, service_name)); |
| 276 update_type, service_name)); | |
| 277 } | 278 } |
| 278 } | 279 } |
| 279 | 280 |
| 280 void ServiceWatcherImpl::DeliverDeferredUpdate( | 281 void ServiceWatcherImpl::DeliverDeferredUpdate( |
| 281 ServiceWatcher::UpdateType update_type, const std::string& service_name) { | 282 ServiceWatcher::UpdateType update_type, const std::string& service_name) { |
| 282 ServiceListenersMap::iterator found = services_.find(service_name); | 283 ServiceListenersMap::iterator found = services_.find(service_name); |
| 283 | 284 |
| 284 if (found != services_.end()) { | 285 if (found != services_.end()) { |
| 285 found->second->set_update_pending(false); | 286 found->second->set_update_pending(false); |
| 286 if (!callback_.is_null()) | 287 if (!callback_.is_null()) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 } | 320 } |
| 320 | 321 |
| 321 void ServiceWatcherImpl::OnNsecRecord(const std::string& name, | 322 void ServiceWatcherImpl::OnNsecRecord(const std::string& name, |
| 322 unsigned rrtype) { | 323 unsigned rrtype) { |
| 323 // Do nothing. It is an error for hosts to broadcast an NSEC record for PTR | 324 // Do nothing. It is an error for hosts to broadcast an NSEC record for PTR |
| 324 // on any name. | 325 // on any name. |
| 325 } | 326 } |
| 326 | 327 |
| 327 void ServiceWatcherImpl::ScheduleQuery(int timeout_seconds) { | 328 void ServiceWatcherImpl::ScheduleQuery(int timeout_seconds) { |
| 328 if (timeout_seconds <= kMaxRequeryTimeSeconds) { | 329 if (timeout_seconds <= kMaxRequeryTimeSeconds) { |
| 329 base::MessageLoop::current()->PostDelayedTask( | 330 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 330 FROM_HERE, | 331 FROM_HERE, base::Bind(&ServiceWatcherImpl::SendQuery, AsWeakPtr(), |
| 331 base::Bind(&ServiceWatcherImpl::SendQuery, | 332 timeout_seconds * 2 /*next_timeout_seconds*/, |
| 332 AsWeakPtr(), | 333 false /*force_update*/), |
| 333 timeout_seconds * 2 /*next_timeout_seconds*/, | |
| 334 false /*force_update*/), | |
| 335 base::TimeDelta::FromSeconds(timeout_seconds)); | 334 base::TimeDelta::FromSeconds(timeout_seconds)); |
| 336 } | 335 } |
| 337 } | 336 } |
| 338 | 337 |
| 339 void ServiceWatcherImpl::SendQuery(int next_timeout_seconds, | 338 void ServiceWatcherImpl::SendQuery(int next_timeout_seconds, |
| 340 bool force_update) { | 339 bool force_update) { |
| 341 CreateTransaction(true /*network*/, false /*cache*/, force_update, | 340 CreateTransaction(true /*network*/, false /*cache*/, force_update, |
| 342 &transaction_network_); | 341 &transaction_network_); |
| 343 ScheduleQuery(next_timeout_seconds); | 342 ScheduleQuery(next_timeout_seconds); |
| 344 } | 343 } |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 address_ipv6_ = rdata->address(); | 552 address_ipv6_ = rdata->address(); |
| 554 } | 553 } |
| 555 } | 554 } |
| 556 | 555 |
| 557 if (transactions_finished_ == 1 && | 556 if (transactions_finished_ == 1 && |
| 558 address_family_ == net::ADDRESS_FAMILY_UNSPECIFIED) { | 557 address_family_ == net::ADDRESS_FAMILY_UNSPECIFIED) { |
| 559 timeout_callback_.Reset(base::Bind( | 558 timeout_callback_.Reset(base::Bind( |
| 560 &LocalDomainResolverImpl::SendResolvedAddresses, | 559 &LocalDomainResolverImpl::SendResolvedAddresses, |
| 561 base::Unretained(this))); | 560 base::Unretained(this))); |
| 562 | 561 |
| 563 base::MessageLoop::current()->PostDelayedTask( | 562 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 564 FROM_HERE, | 563 FROM_HERE, timeout_callback_.callback(), |
| 565 timeout_callback_.callback(), | |
| 566 base::TimeDelta::FromMilliseconds(kLocalDomainSecondAddressTimeoutMs)); | 564 base::TimeDelta::FromMilliseconds(kLocalDomainSecondAddressTimeoutMs)); |
| 567 } else if (transactions_finished_ == 2 | 565 } else if (transactions_finished_ == 2 |
| 568 || address_family_ != net::ADDRESS_FAMILY_UNSPECIFIED) { | 566 || address_family_ != net::ADDRESS_FAMILY_UNSPECIFIED) { |
| 569 SendResolvedAddresses(); | 567 SendResolvedAddresses(); |
| 570 } | 568 } |
| 571 } | 569 } |
| 572 | 570 |
| 573 bool LocalDomainResolverImpl::IsSuccess() { | 571 bool LocalDomainResolverImpl::IsSuccess() { |
| 574 return !address_ipv4_.empty() || !address_ipv6_.empty(); | 572 return !address_ipv4_.empty() || !address_ipv6_.empty(); |
| 575 } | 573 } |
| 576 | 574 |
| 577 void LocalDomainResolverImpl::SendResolvedAddresses() { | 575 void LocalDomainResolverImpl::SendResolvedAddresses() { |
| 578 transaction_a_.reset(); | 576 transaction_a_.reset(); |
| 579 transaction_aaaa_.reset(); | 577 transaction_aaaa_.reset(); |
| 580 timeout_callback_.Cancel(); | 578 timeout_callback_.Cancel(); |
| 581 callback_.Run(IsSuccess(), address_ipv4_, address_ipv6_); | 579 callback_.Run(IsSuccess(), address_ipv4_, address_ipv6_); |
| 582 } | 580 } |
| 583 | 581 |
| 584 } // namespace local_discovery | 582 } // namespace local_discovery |
| OLD | NEW |