| 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/location.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 listener_ = mdns_client_->CreateListener( | 71 listener_ = mdns_client_->CreateListener( |
| 72 net::dns_protocol::kTypePTR, service_type_, this); | 72 net::dns_protocol::kTypePTR, service_type_, this); |
| 73 started_ = listener_->Start(); | 73 started_ = listener_->Start(); |
| 74 if (started_) | 74 if (started_) |
| 75 ReadCachedServices(); | 75 ReadCachedServices(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 ServiceWatcherImpl::~ServiceWatcherImpl() { | 78 ServiceWatcherImpl::~ServiceWatcherImpl() { |
| 79 } | 79 } |
| 80 | 80 |
| 81 void ServiceWatcherImpl::DiscoverNewServices(bool force_update) { | 81 void ServiceWatcherImpl::DiscoverNewServices() { |
| 82 DCHECK(started_); | 82 DCHECK(started_); |
| 83 if (force_update) | 83 SendQuery(kInitialRequeryTimeSeconds); |
| 84 services_.clear(); | |
| 85 SendQuery(kInitialRequeryTimeSeconds, force_update); | |
| 86 } | 84 } |
| 87 | 85 |
| 88 void ServiceWatcherImpl::SetActivelyRefreshServices( | 86 void ServiceWatcherImpl::SetActivelyRefreshServices( |
| 89 bool actively_refresh_services) { | 87 bool actively_refresh_services) { |
| 90 DCHECK(started_); | 88 DCHECK(started_); |
| 91 actively_refresh_services_ = actively_refresh_services; | 89 actively_refresh_services_ = actively_refresh_services; |
| 92 | 90 |
| 93 for (auto& it : services_) | 91 for (auto& it : services_) |
| 94 it.second->SetActiveRefresh(actively_refresh_services); | 92 it.second->SetActiveRefresh(actively_refresh_services); |
| 95 } | 93 } |
| 96 | 94 |
| 97 void ServiceWatcherImpl::ReadCachedServices() { | 95 void ServiceWatcherImpl::ReadCachedServices() { |
| 98 DCHECK(started_); | 96 DCHECK(started_); |
| 99 CreateTransaction(false /*network*/, true /*cache*/, false /*force refresh*/, | 97 CreateTransaction(false /*network*/, true /*cache*/, &transaction_cache_); |
| 100 &transaction_cache_); | |
| 101 } | 98 } |
| 102 | 99 |
| 103 bool ServiceWatcherImpl::CreateTransaction( | 100 bool ServiceWatcherImpl::CreateTransaction( |
| 104 bool network, | 101 bool network, |
| 105 bool cache, | 102 bool cache, |
| 106 bool force_refresh, | |
| 107 std::unique_ptr<net::MDnsTransaction>* transaction) { | 103 std::unique_ptr<net::MDnsTransaction>* transaction) { |
| 108 int transaction_flags = 0; | 104 int transaction_flags = 0; |
| 109 if (network) | 105 if (network) |
| 110 transaction_flags |= net::MDnsTransaction::QUERY_NETWORK; | 106 transaction_flags |= net::MDnsTransaction::QUERY_NETWORK; |
| 111 | 107 |
| 112 if (cache) | 108 if (cache) |
| 113 transaction_flags |= net::MDnsTransaction::QUERY_CACHE; | 109 transaction_flags |= net::MDnsTransaction::QUERY_CACHE; |
| 114 | 110 |
| 115 // TODO(noamsml): Add flag for force_refresh when supported. | |
| 116 if (transaction_flags) { | 111 if (transaction_flags) { |
| 117 *transaction = mdns_client_->CreateTransaction( | 112 *transaction = mdns_client_->CreateTransaction( |
| 118 net::dns_protocol::kTypePTR, service_type_, transaction_flags, | 113 net::dns_protocol::kTypePTR, service_type_, transaction_flags, |
| 119 base::Bind(&ServiceWatcherImpl::OnTransactionResponse, | 114 base::Bind(&ServiceWatcherImpl::OnTransactionResponse, |
| 120 AsWeakPtr(), transaction)); | 115 AsWeakPtr(), transaction)); |
| 121 return (*transaction)->Start(); | 116 return (*transaction)->Start(); |
| 122 } | 117 } |
| 123 | 118 |
| 124 return true; | 119 return true; |
| 125 } | 120 } |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 unsigned rrtype) { | 306 unsigned rrtype) { |
| 312 // Do nothing. It is an error for hosts to broadcast an NSEC record for PTR | 307 // Do nothing. It is an error for hosts to broadcast an NSEC record for PTR |
| 313 // on any name. | 308 // on any name. |
| 314 } | 309 } |
| 315 | 310 |
| 316 void ServiceWatcherImpl::ScheduleQuery(int timeout_seconds) { | 311 void ServiceWatcherImpl::ScheduleQuery(int timeout_seconds) { |
| 317 if (timeout_seconds <= kMaxRequeryTimeSeconds) { | 312 if (timeout_seconds <= kMaxRequeryTimeSeconds) { |
| 318 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 313 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 319 FROM_HERE, | 314 FROM_HERE, |
| 320 base::BindOnce(&ServiceWatcherImpl::SendQuery, AsWeakPtr(), | 315 base::BindOnce(&ServiceWatcherImpl::SendQuery, AsWeakPtr(), |
| 321 timeout_seconds * 2 /*next_timeout_seconds*/, | 316 timeout_seconds * 2 /*next_timeout_seconds*/), |
| 322 false /*force_update*/), | |
| 323 base::TimeDelta::FromSeconds(timeout_seconds)); | 317 base::TimeDelta::FromSeconds(timeout_seconds)); |
| 324 } | 318 } |
| 325 } | 319 } |
| 326 | 320 |
| 327 void ServiceWatcherImpl::SendQuery(int next_timeout_seconds, | 321 void ServiceWatcherImpl::SendQuery(int next_timeout_seconds) { |
| 328 bool force_update) { | 322 CreateTransaction(true /*network*/, false /*cache*/, &transaction_network_); |
| 329 CreateTransaction(true /*network*/, false /*cache*/, force_update, | |
| 330 &transaction_network_); | |
| 331 ScheduleQuery(next_timeout_seconds); | 323 ScheduleQuery(next_timeout_seconds); |
| 332 } | 324 } |
| 333 | 325 |
| 334 ServiceResolverImpl::ServiceResolverImpl( | 326 ServiceResolverImpl::ServiceResolverImpl( |
| 335 const std::string& service_name, | 327 const std::string& service_name, |
| 336 const ResolveCompleteCallback& callback, | 328 const ResolveCompleteCallback& callback, |
| 337 net::MDnsClient* mdns_client) | 329 net::MDnsClient* mdns_client) |
| 338 : service_name_(service_name), callback_(callback), | 330 : service_name_(service_name), callback_(callback), |
| 339 metadata_resolved_(false), address_resolved_(false), | 331 metadata_resolved_(false), address_resolved_(false), |
| 340 mdns_client_(mdns_client) { | 332 mdns_client_(mdns_client) { |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 } | 548 } |
| 557 | 549 |
| 558 void LocalDomainResolverImpl::SendResolvedAddresses() { | 550 void LocalDomainResolverImpl::SendResolvedAddresses() { |
| 559 transaction_a_.reset(); | 551 transaction_a_.reset(); |
| 560 transaction_aaaa_.reset(); | 552 transaction_aaaa_.reset(); |
| 561 timeout_callback_.Cancel(); | 553 timeout_callback_.Cancel(); |
| 562 callback_.Run(IsSuccess(), address_ipv4_, address_ipv6_); | 554 callback_.Run(IsSuccess(), address_ipv4_, address_ipv6_); |
| 563 } | 555 } |
| 564 | 556 |
| 565 } // namespace local_discovery | 557 } // namespace local_discovery |
| OLD | NEW |