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
3 // found in the LICENSE file.
5 #include "chrome/browser/local_discovery/service_discovery_client_mdns.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/local_discovery/service_discovery_host_client.h"
9 #include "content/public/browser/browser_thread.h"
11 namespace local_discovery {
13 using content::BrowserThread;
16 const int kMaxRestartAttempts = 10;
17 const int kRestartDelayOnNetworkChangeSeconds = 3;
18 const int kReportSuccessAfterSeconds = 10;
21 scoped_ptr<ServiceWatcher> ServiceDiscoveryClientMdns::CreateServiceWatcher(
22 const std::string& service_type,
23 const ServiceWatcher::UpdatedCallback& callback) {
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
25 return host_client_->CreateServiceWatcher(service_type, callback);
28 scoped_ptr<ServiceResolver> ServiceDiscoveryClientMdns::CreateServiceResolver(
29 const std::string& service_name,
30 const ServiceResolver::ResolveCompleteCallback& callback) {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32 return host_client_->CreateServiceResolver(service_name, callback);
35 scoped_ptr<LocalDomainResolver>
36 ServiceDiscoveryClientMdns::CreateLocalDomainResolver(
37 const std::string& domain,
38 net::AddressFamily address_family,
39 const LocalDomainResolver::IPAddressCallback& callback) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
41 return host_client_->CreateLocalDomainResolver(domain, address_family,
45 ServiceDiscoveryClientMdns::ServiceDiscoveryClientMdns()
46 : restart_attempts_(kMaxRestartAttempts),
47 weak_ptr_factory_(this) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
53 ServiceDiscoveryClientMdns::~ServiceDiscoveryClientMdns() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
56 host_client_->Shutdown();
59 void ServiceDiscoveryClientMdns::OnNetworkChanged(
60 net::NetworkChangeNotifier::ConnectionType type) {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62 // Only network changes resets kMaxRestartAttempts.
63 restart_attempts_ = kMaxRestartAttempts;
64 ScheduleStartNewClient();
67 void ServiceDiscoveryClientMdns::ScheduleStartNewClient() {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
69 host_client_->Shutdown();
70 weak_ptr_factory_.InvalidateWeakPtrs();
71 base::MessageLoop::current()->PostDelayedTask(
73 base::Bind(&ServiceDiscoveryClientMdns::StartNewClient,
74 weak_ptr_factory_.GetWeakPtr()),
75 base::TimeDelta::FromSeconds(kRestartDelayOnNetworkChangeSeconds));
78 void ServiceDiscoveryClientMdns::StartNewClient() {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 scoped_refptr<ServiceDiscoveryHostClient> old_client = host_client_;
81 if ((restart_attempts_--) > 0) {
82 host_client_ = new ServiceDiscoveryHostClient();
84 base::Bind(&ServiceDiscoveryClientMdns::ScheduleStartNewClient,
85 weak_ptr_factory_.GetWeakPtr()));
87 base::MessageLoop::current()->PostDelayedTask(
89 base::Bind(&ServiceDiscoveryClientMdns::ReportSuccess,
90 weak_ptr_factory_.GetWeakPtr()),
91 base::TimeDelta::FromSeconds(kReportSuccessAfterSeconds));
93 restart_attempts_ = -1;
96 // Run when host_client_ is created. Callbacks created by InvalidateWatchers
97 // may create new watchers.
99 old_client->InvalidateWatchers();
102 void ServiceDiscoveryClientMdns::ReportSuccess() {
103 UMA_HISTOGRAM_COUNTS_100("LocalDiscovery.ClientRestartAttempts",
104 kMaxRestartAttempts - restart_attempts_);
107 } // namespace local_discovery