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 <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "chrome/utility/local_discovery/service_discovery_client_impl.h" | 11 #include "chrome/utility/local_discovery/service_discovery_client_impl.h" |
12 #include "net/dns/dns_protocol.h" | 12 #include "net/dns/dns_protocol.h" |
13 #include "net/dns/record_rdata.h" | 13 #include "net/dns/record_rdata.h" |
14 | 14 |
15 namespace local_discovery { | 15 namespace local_discovery { |
16 | 16 |
17 namespace { | 17 namespace { |
18 // TODO(noamsml): Make this configurable through the LocalDomainResolver | 18 // TODO(noamsml): Make this configurable through the LocalDomainResolver |
19 // interface. | 19 // interface. |
20 const int kLocalDomainSecondAddressTimeoutMs = 100; | 20 const int kLocalDomainSecondAddressTimeoutMs = 100; |
| 21 |
| 22 const int kInitialRequeryTimeSeconds = 1; |
| 23 const int kMaxRequeryTimeSeconds = 2; // Time for last requery |
21 } | 24 } |
22 | 25 |
23 ServiceDiscoveryClientImpl::ServiceDiscoveryClientImpl( | 26 ServiceDiscoveryClientImpl::ServiceDiscoveryClientImpl( |
24 net::MDnsClient* mdns_client) : mdns_client_(mdns_client) { | 27 net::MDnsClient* mdns_client) : mdns_client_(mdns_client) { |
25 } | 28 } |
26 | 29 |
27 ServiceDiscoveryClientImpl::~ServiceDiscoveryClientImpl() { | 30 ServiceDiscoveryClientImpl::~ServiceDiscoveryClientImpl() { |
28 } | 31 } |
29 | 32 |
30 scoped_ptr<ServiceWatcher> ServiceDiscoveryClientImpl::CreateServiceWatcher( | 33 scoped_ptr<ServiceWatcher> ServiceDiscoveryClientImpl::CreateServiceWatcher( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 ReadCachedServices(); | 70 ReadCachedServices(); |
68 } | 71 } |
69 | 72 |
70 ServiceWatcherImpl::~ServiceWatcherImpl() { | 73 ServiceWatcherImpl::~ServiceWatcherImpl() { |
71 } | 74 } |
72 | 75 |
73 void ServiceWatcherImpl::DiscoverNewServices(bool force_update) { | 76 void ServiceWatcherImpl::DiscoverNewServices(bool force_update) { |
74 DCHECK(started_); | 77 DCHECK(started_); |
75 if (force_update) | 78 if (force_update) |
76 services_.clear(); | 79 services_.clear(); |
77 CreateTransaction(true /*network*/, false /*cache*/, force_update, | 80 SendQuery(kInitialRequeryTimeSeconds, force_update); |
78 &transaction_network_); | |
79 } | 81 } |
80 | 82 |
81 void ServiceWatcherImpl::ReadCachedServices() { | 83 void ServiceWatcherImpl::ReadCachedServices() { |
82 DCHECK(started_); | 84 DCHECK(started_); |
83 CreateTransaction(false /*network*/, true /*cache*/, false /*force refresh*/, | 85 CreateTransaction(false /*network*/, true /*cache*/, false /*force refresh*/, |
84 &transaction_cache_); | 86 &transaction_cache_); |
85 } | 87 } |
86 | 88 |
87 bool ServiceWatcherImpl::CreateTransaction( | 89 bool ServiceWatcherImpl::CreateTransaction( |
88 bool network, bool cache, bool force_refresh, | 90 bool network, bool cache, bool force_refresh, |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 callback_.Run(UPDATE_REMOVED, service); | 229 callback_.Run(UPDATE_REMOVED, service); |
228 } | 230 } |
229 } | 231 } |
230 | 232 |
231 void ServiceWatcherImpl::OnNsecRecord(const std::string& name, | 233 void ServiceWatcherImpl::OnNsecRecord(const std::string& name, |
232 unsigned rrtype) { | 234 unsigned rrtype) { |
233 // Do nothing. It is an error for hosts to broadcast an NSEC record for PTR | 235 // Do nothing. It is an error for hosts to broadcast an NSEC record for PTR |
234 // on any name. | 236 // on any name. |
235 } | 237 } |
236 | 238 |
| 239 void ServiceWatcherImpl::ScheduleQuery(int timeout_seconds) { |
| 240 if (timeout_seconds <= kMaxRequeryTimeSeconds) { |
| 241 base::MessageLoop::current()->PostDelayedTask( |
| 242 FROM_HERE, |
| 243 base::Bind(&ServiceWatcherImpl::SendQuery, |
| 244 AsWeakPtr(), |
| 245 timeout_seconds * 2 /*next_timeout_seconds*/, |
| 246 false /*force_update*/), |
| 247 base::TimeDelta::FromSeconds(timeout_seconds)); |
| 248 } |
| 249 } |
| 250 |
| 251 void ServiceWatcherImpl::SendQuery(int next_timeout_seconds, |
| 252 bool force_update) { |
| 253 CreateTransaction(true /*network*/, false /*cache*/, force_update, |
| 254 &transaction_network_); |
| 255 ScheduleQuery(next_timeout_seconds); |
| 256 } |
| 257 |
237 ServiceResolverImpl::ServiceResolverImpl( | 258 ServiceResolverImpl::ServiceResolverImpl( |
238 const std::string& service_name, | 259 const std::string& service_name, |
239 const ResolveCompleteCallback& callback, | 260 const ResolveCompleteCallback& callback, |
240 net::MDnsClient* mdns_client) | 261 net::MDnsClient* mdns_client) |
241 : service_name_(service_name), callback_(callback), | 262 : service_name_(service_name), callback_(callback), |
242 metadata_resolved_(false), address_resolved_(false), | 263 metadata_resolved_(false), address_resolved_(false), |
243 mdns_client_(mdns_client) { | 264 mdns_client_(mdns_client) { |
244 } | 265 } |
245 | 266 |
246 void ServiceResolverImpl::StartResolving() { | 267 void ServiceResolverImpl::StartResolving() { |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 } | 487 } |
467 | 488 |
468 void LocalDomainResolverImpl::SendResolvedAddresses() { | 489 void LocalDomainResolverImpl::SendResolvedAddresses() { |
469 transaction_a_.reset(); | 490 transaction_a_.reset(); |
470 transaction_aaaa_.reset(); | 491 transaction_aaaa_.reset(); |
471 timeout_callback_.Cancel(); | 492 timeout_callback_.Cancel(); |
472 callback_.Run(IsSuccess(), address_ipv4_, address_ipv6_); | 493 callback_.Run(IsSuccess(), address_ipv4_, address_ipv6_); |
473 } | 494 } |
474 | 495 |
475 } // namespace local_discovery | 496 } // namespace local_discovery |
OLD | NEW |