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

Side by Side Diff: chrome/browser/media/router/discovery/dial/dial_registry.cc

Issue 2754703005: [Device Discovery] Make DialRegistry a Singleton (Closed)
Patch Set: make DialRegistry singleton Created 3 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/media/router/discovery/dial/dial_registry.h" 5 #include "chrome/browser/media/router/discovery/dial/dial_registry.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" 16 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h"
17 #include "chrome/browser/media/router/discovery/dial/dial_service.h" 17 #include "chrome/browser/media/router/discovery/dial/dial_service.h"
18 #include "chrome/common/extensions/api/dial.h" 18 #include "chrome/common/extensions/api/dial.h"
19 #include "components/net_log/chrome_net_log.h" 19 #include "components/net_log/chrome_net_log.h"
20 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
21 21
22 using base::Time; 22 using base::Time;
23 using base::TimeDelta; 23 using base::TimeDelta;
24 using content::BrowserThread; 24 using content::BrowserThread;
25 using net::NetworkChangeNotifier; 25 using net::NetworkChangeNotifier;
26 26
27 namespace {
28
29 // How often to poll for devices.
30 const int kDialRefreshIntervalSecs = 120;
31
32 // We prune a device if it does not respond after this time.
33 const int kDialExpirationSecs = 240;
34
35 // The maximum number of devices retained at once in the registry.
36 const size_t kDialMaxDevices = 256;
37
38 } // namespace
39
27 namespace media_router { 40 namespace media_router {
28 41
29 DialRegistry::DialRegistry(base::TimeDelta refresh_interval, 42 DialRegistry::DialRegistry()
30 base::TimeDelta expiration,
31 const size_t max_devices)
32 : num_listeners_(0), 43 : num_listeners_(0),
33 registry_generation_(0), 44 registry_generation_(0),
34 last_event_registry_generation_(0), 45 last_event_registry_generation_(0),
35 label_count_(0), 46 label_count_(0),
36 refresh_interval_delta_(refresh_interval), 47 refresh_interval_delta_(
37 expiration_delta_(expiration), 48 base::TimeDelta::FromSeconds(kDialRefreshIntervalSecs)),
38 max_devices_(max_devices) { 49 expiration_delta_(base::TimeDelta::FromSeconds(kDialExpirationSecs)),
50 max_devices_(kDialMaxDevices) {
39 DCHECK_CURRENTLY_ON(BrowserThread::IO); 51 DCHECK_CURRENTLY_ON(BrowserThread::IO);
40 DCHECK_GT(max_devices_, 0U); 52 DCHECK_GT(max_devices_, 0U);
41 NetworkChangeNotifier::AddNetworkChangeObserver(this); 53 NetworkChangeNotifier::AddNetworkChangeObserver(this);
42 } 54 }
43 55
44 DialRegistry::~DialRegistry() { 56 DialRegistry::~DialRegistry() {
57 // Singleton is destroyed on main thread.
58 NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
59 }
60
61 // static
62 DialRegistry* DialRegistry::GetInstance() {
45 DCHECK_CURRENTLY_ON(BrowserThread::IO); 63 DCHECK_CURRENTLY_ON(BrowserThread::IO);
46 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); 64 return base::Singleton<DialRegistry>::get();
47 } 65 }
48 66
49 std::unique_ptr<DialService> DialRegistry::CreateDialService() { 67 std::unique_ptr<DialService> DialRegistry::CreateDialService() {
50 DCHECK(g_browser_process->net_log()); 68 DCHECK(g_browser_process->net_log());
51 return base::MakeUnique<DialServiceImpl>(g_browser_process->net_log()); 69 return base::MakeUnique<DialServiceImpl>(g_browser_process->net_log());
52 } 70 }
53 71
54 void DialRegistry::ClearDialService() { 72 void DialRegistry::ClearDialService() {
55 dial_.reset(); 73 dial_.reset();
56 } 74 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 165 }
148 166
149 void DialRegistry::StartPeriodicDiscovery() { 167 void DialRegistry::StartPeriodicDiscovery() {
150 DCHECK_CURRENTLY_ON(BrowserThread::IO); 168 DCHECK_CURRENTLY_ON(BrowserThread::IO);
151 if (!ReadyToDiscover() || dial_) 169 if (!ReadyToDiscover() || dial_)
152 return; 170 return;
153 171
154 dial_ = CreateDialService(); 172 dial_ = CreateDialService();
155 dial_->AddObserver(this); 173 dial_->AddObserver(this);
156 DoDiscovery(); 174 DoDiscovery();
157 repeating_timer_.Start(FROM_HERE, refresh_interval_delta_, this, 175 repeating_timer_.reset(new base::RepeatingTimer());
158 &DialRegistry::DoDiscovery); 176 repeating_timer_->Start(FROM_HERE, refresh_interval_delta_, this,
177 &DialRegistry::DoDiscovery);
159 } 178 }
160 179
161 void DialRegistry::DoDiscovery() { 180 void DialRegistry::DoDiscovery() {
162 DCHECK_CURRENTLY_ON(BrowserThread::IO); 181 DCHECK_CURRENTLY_ON(BrowserThread::IO);
163 DCHECK(dial_); 182 DCHECK(dial_);
164 VLOG(2) << "About to discover!"; 183 VLOG(2) << "About to discover!";
165 dial_->Discover(); 184 dial_->Discover();
166 } 185 }
167 186
168 void DialRegistry::StopPeriodicDiscovery() { 187 void DialRegistry::StopPeriodicDiscovery() {
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); 188 DCHECK_CURRENTLY_ON(BrowserThread::IO);
170 if (!dial_) 189 if (!dial_)
171 return; 190 return;
172 191
173 repeating_timer_.Stop(); 192 repeating_timer_->Stop();
193 repeating_timer_.reset();
mark a. foltz 2017/03/22 18:37:25 Thanks for cleaning this up :)
174 dial_->RemoveObserver(this); 194 dial_->RemoveObserver(this);
175 ClearDialService(); 195 ClearDialService();
176 } 196 }
177 197
178 bool DialRegistry::PruneExpiredDevices() { 198 bool DialRegistry::PruneExpiredDevices() {
179 DCHECK_CURRENTLY_ON(BrowserThread::IO); 199 DCHECK_CURRENTLY_ON(BrowserThread::IO);
180 bool pruned_device = false; 200 bool pruned_device = false;
181 DeviceByLabelMap::iterator it = device_by_label_map_.begin(); 201 DeviceByLabelMap::iterator it = device_by_label_map_.begin();
182 while (it != device_by_label_map_.end()) { 202 while (it != device_by_label_map_.end()) {
183 auto* device = it->second; 203 auto* device = it->second;
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 for (auto& observer : observers_) 382 for (auto& observer : observers_)
363 observer.OnDialDeviceEvent(devices); 383 observer.OnDialDeviceEvent(devices);
364 } 384 }
365 385
366 void DialRegistry::OnDialError(DialErrorCode type) { 386 void DialRegistry::OnDialError(DialErrorCode type) {
367 for (auto& observer : observers_) 387 for (auto& observer : observers_)
368 observer.OnDialError(type); 388 observer.OnDialError(type);
369 } 389 }
370 390
371 } // namespace media_router 391 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698