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 "chrome/browser/local_discovery/service_discovery_client_utility.h" | 5 #include "chrome/browser/local_discovery/service_discovery_client_utility.h" |
6 | 6 |
| 7 #include "base/location.h" |
7 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" |
8 #include "chrome/browser/local_discovery/service_discovery_host_client.h" | 11 #include "chrome/browser/local_discovery/service_discovery_host_client.h" |
9 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
10 | 13 |
11 namespace local_discovery { | 14 namespace local_discovery { |
12 | 15 |
13 using content::BrowserThread; | 16 using content::BrowserThread; |
14 | 17 |
15 namespace { | 18 namespace { |
16 const int kMaxRestartAttempts = 10; | 19 const int kMaxRestartAttempts = 10; |
17 const int kRestartDelayOnNetworkChangeSeconds = 3; | 20 const int kRestartDelayOnNetworkChangeSeconds = 3; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
63 // Only network changes resets kMaxRestartAttempts. | 66 // Only network changes resets kMaxRestartAttempts. |
64 restart_attempts_ = kMaxRestartAttempts; | 67 restart_attempts_ = kMaxRestartAttempts; |
65 ScheduleStartNewClient(); | 68 ScheduleStartNewClient(); |
66 } | 69 } |
67 | 70 |
68 void ServiceDiscoveryClientUtility::ScheduleStartNewClient() { | 71 void ServiceDiscoveryClientUtility::ScheduleStartNewClient() { |
69 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 72 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
70 host_client_->Shutdown(); | 73 host_client_->Shutdown(); |
71 weak_ptr_factory_.InvalidateWeakPtrs(); | 74 weak_ptr_factory_.InvalidateWeakPtrs(); |
72 base::MessageLoop::current()->PostDelayedTask( | 75 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
73 FROM_HERE, | 76 FROM_HERE, base::Bind(&ServiceDiscoveryClientUtility::StartNewClient, |
74 base::Bind(&ServiceDiscoveryClientUtility::StartNewClient, | 77 weak_ptr_factory_.GetWeakPtr()), |
75 weak_ptr_factory_.GetWeakPtr()), | |
76 base::TimeDelta::FromSeconds(kRestartDelayOnNetworkChangeSeconds)); | 78 base::TimeDelta::FromSeconds(kRestartDelayOnNetworkChangeSeconds)); |
77 } | 79 } |
78 | 80 |
79 void ServiceDiscoveryClientUtility::StartNewClient() { | 81 void ServiceDiscoveryClientUtility::StartNewClient() { |
80 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 82 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
81 scoped_refptr<ServiceDiscoveryHostClient> old_client = host_client_; | 83 scoped_refptr<ServiceDiscoveryHostClient> old_client = host_client_; |
82 if ((restart_attempts_--) > 0) { | 84 if ((restart_attempts_--) > 0) { |
83 host_client_ = new ServiceDiscoveryHostClient(); | 85 host_client_ = new ServiceDiscoveryHostClient(); |
84 host_client_->Start( | 86 host_client_->Start( |
85 base::Bind(&ServiceDiscoveryClientUtility::ScheduleStartNewClient, | 87 base::Bind(&ServiceDiscoveryClientUtility::ScheduleStartNewClient, |
86 weak_ptr_factory_.GetWeakPtr())); | 88 weak_ptr_factory_.GetWeakPtr())); |
87 | 89 |
88 base::MessageLoop::current()->PostDelayedTask( | 90 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
89 FROM_HERE, | 91 FROM_HERE, base::Bind(&ServiceDiscoveryClientUtility::ReportSuccess, |
90 base::Bind(&ServiceDiscoveryClientUtility::ReportSuccess, | 92 weak_ptr_factory_.GetWeakPtr()), |
91 weak_ptr_factory_.GetWeakPtr()), | |
92 base::TimeDelta::FromSeconds(kReportSuccessAfterSeconds)); | 93 base::TimeDelta::FromSeconds(kReportSuccessAfterSeconds)); |
93 } else { | 94 } else { |
94 restart_attempts_ = -1; | 95 restart_attempts_ = -1; |
95 ReportSuccess(); | 96 ReportSuccess(); |
96 } | 97 } |
97 // Run when host_client_ is created. Callbacks created by InvalidateWatchers | 98 // Run when host_client_ is created. Callbacks created by InvalidateWatchers |
98 // may create new watchers. | 99 // may create new watchers. |
99 if (old_client.get()) | 100 if (old_client.get()) |
100 old_client->InvalidateWatchers(); | 101 old_client->InvalidateWatchers(); |
101 } | 102 } |
102 | 103 |
103 void ServiceDiscoveryClientUtility::ReportSuccess() { | 104 void ServiceDiscoveryClientUtility::ReportSuccess() { |
104 UMA_HISTOGRAM_COUNTS_100("LocalDiscovery.ClientRestartAttempts", | 105 UMA_HISTOGRAM_COUNTS_100("LocalDiscovery.ClientRestartAttempts", |
105 kMaxRestartAttempts - restart_attempts_); | 106 kMaxRestartAttempts - restart_attempts_); |
106 } | 107 } |
107 | 108 |
108 } // namespace local_discovery | 109 } // namespace local_discovery |
OLD | NEW |