Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / local_discovery / service_discovery_client_utility.cc
blob4b9242ac4f73b8091ccc987ac3acbb9fd0926000
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
3 // found in the LICENSE file.
5 #include "chrome/browser/local_discovery/service_discovery_client_utility.h"
7 #include "base/location.h"
8 #include "base/metrics/histogram.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/local_discovery/service_discovery_host_client.h"
12 #include "content/public/browser/browser_thread.h"
14 namespace local_discovery {
16 using content::BrowserThread;
18 namespace {
19 const int kMaxRestartAttempts = 10;
20 const int kRestartDelayOnNetworkChangeSeconds = 3;
21 const int kReportSuccessAfterSeconds = 10;
24 scoped_ptr<ServiceWatcher> ServiceDiscoveryClientUtility::CreateServiceWatcher(
25 const std::string& service_type,
26 const ServiceWatcher::UpdatedCallback& callback) {
27 DCHECK_CURRENTLY_ON(BrowserThread::UI);
28 return host_client_->CreateServiceWatcher(service_type, callback);
31 scoped_ptr<ServiceResolver>
32 ServiceDiscoveryClientUtility::CreateServiceResolver(
33 const std::string& service_name,
34 const ServiceResolver::ResolveCompleteCallback& callback) {
35 DCHECK_CURRENTLY_ON(BrowserThread::UI);
36 return host_client_->CreateServiceResolver(service_name, callback);
39 scoped_ptr<LocalDomainResolver>
40 ServiceDiscoveryClientUtility::CreateLocalDomainResolver(
41 const std::string& domain,
42 net::AddressFamily address_family,
43 const LocalDomainResolver::IPAddressCallback& callback) {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI);
45 return host_client_->CreateLocalDomainResolver(domain, address_family,
46 callback);
49 ServiceDiscoveryClientUtility::ServiceDiscoveryClientUtility()
50 : restart_attempts_(kMaxRestartAttempts),
51 weak_ptr_factory_(this) {
52 DCHECK_CURRENTLY_ON(BrowserThread::UI);
53 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
54 StartNewClient();
57 ServiceDiscoveryClientUtility::~ServiceDiscoveryClientUtility() {
58 DCHECK_CURRENTLY_ON(BrowserThread::UI);
59 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
60 host_client_->Shutdown();
63 void ServiceDiscoveryClientUtility::OnNetworkChanged(
64 net::NetworkChangeNotifier::ConnectionType type) {
65 DCHECK_CURRENTLY_ON(BrowserThread::UI);
66 // Only network changes resets kMaxRestartAttempts.
67 restart_attempts_ = kMaxRestartAttempts;
68 ScheduleStartNewClient();
71 void ServiceDiscoveryClientUtility::ScheduleStartNewClient() {
72 DCHECK_CURRENTLY_ON(BrowserThread::UI);
73 host_client_->Shutdown();
74 weak_ptr_factory_.InvalidateWeakPtrs();
75 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
76 FROM_HERE, base::Bind(&ServiceDiscoveryClientUtility::StartNewClient,
77 weak_ptr_factory_.GetWeakPtr()),
78 base::TimeDelta::FromSeconds(kRestartDelayOnNetworkChangeSeconds));
81 void ServiceDiscoveryClientUtility::StartNewClient() {
82 DCHECK_CURRENTLY_ON(BrowserThread::UI);
83 scoped_refptr<ServiceDiscoveryHostClient> old_client = host_client_;
84 if ((restart_attempts_--) > 0) {
85 host_client_ = new ServiceDiscoveryHostClient();
86 host_client_->Start(
87 base::Bind(&ServiceDiscoveryClientUtility::ScheduleStartNewClient,
88 weak_ptr_factory_.GetWeakPtr()));
90 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
91 FROM_HERE, base::Bind(&ServiceDiscoveryClientUtility::ReportSuccess,
92 weak_ptr_factory_.GetWeakPtr()),
93 base::TimeDelta::FromSeconds(kReportSuccessAfterSeconds));
94 } else {
95 restart_attempts_ = -1;
96 ReportSuccess();
98 // Run when host_client_ is created. Callbacks created by InvalidateWatchers
99 // may create new watchers.
100 if (old_client.get())
101 old_client->InvalidateWatchers();
104 void ServiceDiscoveryClientUtility::ReportSuccess() {
105 UMA_HISTOGRAM_COUNTS_100("LocalDiscovery.ClientRestartAttempts",
106 kMaxRestartAttempts - restart_attempts_);
109 } // namespace local_discovery