Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: chrome/browser/local_discovery/service_discovery_client_utility.cc

Issue 251023005: Renamed ServiceDiscoveryClientMdns to ServiceDiscoveryClientUtility. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sat 04/26/2014 18:41:53.09 Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/local_discovery/service_discovery_client_mdns.h" 5 #include "chrome/browser/local_discovery/service_discovery_client_utility.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/local_discovery/service_discovery_host_client.h" 8 #include "chrome/browser/local_discovery/service_discovery_host_client.h"
9 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
10 10
11 namespace local_discovery { 11 namespace local_discovery {
12 12
13 using content::BrowserThread; 13 using content::BrowserThread;
14 14
15 namespace { 15 namespace {
16 const int kMaxRestartAttempts = 10; 16 const int kMaxRestartAttempts = 10;
17 const int kRestartDelayOnNetworkChangeSeconds = 3; 17 const int kRestartDelayOnNetworkChangeSeconds = 3;
18 const int kReportSuccessAfterSeconds = 10; 18 const int kReportSuccessAfterSeconds = 10;
19 } 19 }
20 20
21 scoped_ptr<ServiceWatcher> ServiceDiscoveryClientMdns::CreateServiceWatcher( 21 scoped_ptr<ServiceWatcher> ServiceDiscoveryClientUtility::CreateServiceWatcher(
22 const std::string& service_type, 22 const std::string& service_type,
23 const ServiceWatcher::UpdatedCallback& callback) { 23 const ServiceWatcher::UpdatedCallback& callback) {
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
25 return host_client_->CreateServiceWatcher(service_type, callback); 25 return host_client_->CreateServiceWatcher(service_type, callback);
26 } 26 }
27 27
28 scoped_ptr<ServiceResolver> ServiceDiscoveryClientMdns::CreateServiceResolver( 28 scoped_ptr<ServiceResolver>
29 ServiceDiscoveryClientUtility::CreateServiceResolver(
29 const std::string& service_name, 30 const std::string& service_name,
30 const ServiceResolver::ResolveCompleteCallback& callback) { 31 const ServiceResolver::ResolveCompleteCallback& callback) {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32 return host_client_->CreateServiceResolver(service_name, callback); 33 return host_client_->CreateServiceResolver(service_name, callback);
33 } 34 }
34 35
35 scoped_ptr<LocalDomainResolver> 36 scoped_ptr<LocalDomainResolver>
36 ServiceDiscoveryClientMdns::CreateLocalDomainResolver( 37 ServiceDiscoveryClientUtility::CreateLocalDomainResolver(
37 const std::string& domain, 38 const std::string& domain,
38 net::AddressFamily address_family, 39 net::AddressFamily address_family,
39 const LocalDomainResolver::IPAddressCallback& callback) { 40 const LocalDomainResolver::IPAddressCallback& callback) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
41 return host_client_->CreateLocalDomainResolver(domain, address_family, 42 return host_client_->CreateLocalDomainResolver(domain, address_family,
42 callback); 43 callback);
43 } 44 }
44 45
45 ServiceDiscoveryClientMdns::ServiceDiscoveryClientMdns() 46 ServiceDiscoveryClientUtility::ServiceDiscoveryClientUtility()
46 : restart_attempts_(kMaxRestartAttempts), 47 : restart_attempts_(kMaxRestartAttempts),
47 weak_ptr_factory_(this) { 48 weak_ptr_factory_(this) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); 50 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
50 StartNewClient(); 51 StartNewClient();
51 } 52 }
52 53
53 ServiceDiscoveryClientMdns::~ServiceDiscoveryClientMdns() { 54 ServiceDiscoveryClientUtility::~ServiceDiscoveryClientUtility() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); 56 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
56 host_client_->Shutdown(); 57 host_client_->Shutdown();
57 } 58 }
58 59
59 void ServiceDiscoveryClientMdns::OnNetworkChanged( 60 void ServiceDiscoveryClientUtility::OnNetworkChanged(
60 net::NetworkChangeNotifier::ConnectionType type) { 61 net::NetworkChangeNotifier::ConnectionType type) {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62 // Only network changes resets kMaxRestartAttempts. 63 // Only network changes resets kMaxRestartAttempts.
63 restart_attempts_ = kMaxRestartAttempts; 64 restart_attempts_ = kMaxRestartAttempts;
64 ScheduleStartNewClient(); 65 ScheduleStartNewClient();
65 } 66 }
66 67
67 void ServiceDiscoveryClientMdns::ScheduleStartNewClient() { 68 void ServiceDiscoveryClientUtility::ScheduleStartNewClient() {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
69 host_client_->Shutdown(); 70 host_client_->Shutdown();
70 weak_ptr_factory_.InvalidateWeakPtrs(); 71 weak_ptr_factory_.InvalidateWeakPtrs();
71 base::MessageLoop::current()->PostDelayedTask( 72 base::MessageLoop::current()->PostDelayedTask(
72 FROM_HERE, 73 FROM_HERE,
73 base::Bind(&ServiceDiscoveryClientMdns::StartNewClient, 74 base::Bind(&ServiceDiscoveryClientUtility::StartNewClient,
74 weak_ptr_factory_.GetWeakPtr()), 75 weak_ptr_factory_.GetWeakPtr()),
75 base::TimeDelta::FromSeconds(kRestartDelayOnNetworkChangeSeconds)); 76 base::TimeDelta::FromSeconds(kRestartDelayOnNetworkChangeSeconds));
76 } 77 }
77 78
78 void ServiceDiscoveryClientMdns::StartNewClient() { 79 void ServiceDiscoveryClientUtility::StartNewClient() {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 scoped_refptr<ServiceDiscoveryHostClient> old_client = host_client_; 81 scoped_refptr<ServiceDiscoveryHostClient> old_client = host_client_;
81 if ((restart_attempts_--) > 0) { 82 if ((restart_attempts_--) > 0) {
82 host_client_ = new ServiceDiscoveryHostClient(); 83 host_client_ = new ServiceDiscoveryHostClient();
83 host_client_->Start( 84 host_client_->Start(
84 base::Bind(&ServiceDiscoveryClientMdns::ScheduleStartNewClient, 85 base::Bind(&ServiceDiscoveryClientUtility::ScheduleStartNewClient,
85 weak_ptr_factory_.GetWeakPtr())); 86 weak_ptr_factory_.GetWeakPtr()));
86 87
87 base::MessageLoop::current()->PostDelayedTask( 88 base::MessageLoop::current()->PostDelayedTask(
88 FROM_HERE, 89 FROM_HERE,
89 base::Bind(&ServiceDiscoveryClientMdns::ReportSuccess, 90 base::Bind(&ServiceDiscoveryClientUtility::ReportSuccess,
90 weak_ptr_factory_.GetWeakPtr()), 91 weak_ptr_factory_.GetWeakPtr()),
91 base::TimeDelta::FromSeconds(kReportSuccessAfterSeconds)); 92 base::TimeDelta::FromSeconds(kReportSuccessAfterSeconds));
92 } else { 93 } else {
93 restart_attempts_ = -1; 94 restart_attempts_ = -1;
94 ReportSuccess(); 95 ReportSuccess();
95 } 96 }
96 // Run when host_client_ is created. Callbacks created by InvalidateWatchers 97 // Run when host_client_ is created. Callbacks created by InvalidateWatchers
97 // may create new watchers. 98 // may create new watchers.
98 if (old_client) 99 if (old_client)
99 old_client->InvalidateWatchers(); 100 old_client->InvalidateWatchers();
100 } 101 }
101 102
102 void ServiceDiscoveryClientMdns::ReportSuccess() { 103 void ServiceDiscoveryClientUtility::ReportSuccess() {
103 UMA_HISTOGRAM_COUNTS_100("LocalDiscovery.ClientRestartAttempts", 104 UMA_HISTOGRAM_COUNTS_100("LocalDiscovery.ClientRestartAttempts",
104 kMaxRestartAttempts - restart_attempts_); 105 kMaxRestartAttempts - restart_attempts_);
105 } 106 }
106 107
107 } // namespace local_discovery 108 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698