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

Side by Side Diff: chrome/browser/extensions/api/dial/dial_registry.cc

Issue 2702503003: [Dial] Refactor DialRegistry and DeviceDescriptionFetcher so they can be used by MediaSinkService (Closed)
Patch Set: resolve code review comments from Derek 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/extensions/api/dial/dial_registry.h" 5 #include "chrome/browser/extensions/api/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"
(...skipping 11 matching lines...) Expand all
22 22
23 using base::Time; 23 using base::Time;
24 using base::TimeDelta; 24 using base::TimeDelta;
25 using content::BrowserThread; 25 using content::BrowserThread;
26 using net::NetworkChangeNotifier; 26 using net::NetworkChangeNotifier;
27 27
28 namespace extensions { 28 namespace extensions {
29 namespace api { 29 namespace api {
30 namespace dial { 30 namespace dial {
31 31
32 DialRegistry::DialRegistry(Observer* dial_api, 32 DialRegistry::DialRegistry(base::TimeDelta refresh_interval,
33 const base::TimeDelta& refresh_interval, 33 base::TimeDelta expiration,
34 const base::TimeDelta& expiration,
35 const size_t max_devices) 34 const size_t max_devices)
36 : num_listeners_(0), 35 : num_listeners_(0),
37 registry_generation_(0), 36 registry_generation_(0),
38 last_event_registry_generation_(0), 37 last_event_registry_generation_(0),
39 label_count_(0), 38 label_count_(0),
40 refresh_interval_delta_(refresh_interval), 39 refresh_interval_delta_(refresh_interval),
41 expiration_delta_(expiration), 40 expiration_delta_(expiration),
42 max_devices_(max_devices), 41 max_devices_(max_devices) {
43 dial_api_(dial_api) {
44 DCHECK_CURRENTLY_ON(BrowserThread::IO); 42 DCHECK_CURRENTLY_ON(BrowserThread::IO);
45 DCHECK_GT(max_devices_, 0U); 43 DCHECK_GT(max_devices_, 0U);
46 NetworkChangeNotifier::AddNetworkChangeObserver(this); 44 NetworkChangeNotifier::AddNetworkChangeObserver(this);
47 } 45 }
48 46
49 DialRegistry::~DialRegistry() { 47 DialRegistry::~DialRegistry() {
50 DCHECK_CURRENTLY_ON(BrowserThread::IO); 48 DCHECK_CURRENTLY_ON(BrowserThread::IO);
51 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); 49 NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
52 } 50 }
53 51
(...skipping 24 matching lines...) Expand all
78 76
79 void DialRegistry::OnListenerRemoved() { 77 void DialRegistry::OnListenerRemoved() {
80 DCHECK_CURRENTLY_ON(BrowserThread::IO); 78 DCHECK_CURRENTLY_ON(BrowserThread::IO);
81 DCHECK_GT(num_listeners_, 0); 79 DCHECK_GT(num_listeners_, 0);
82 if (--num_listeners_ == 0) { 80 if (--num_listeners_ == 0) {
83 VLOG(2) << "Listeners removed; stopping periodic discovery."; 81 VLOG(2) << "Listeners removed; stopping periodic discovery.";
84 StopPeriodicDiscovery(); 82 StopPeriodicDiscovery();
85 } 83 }
86 } 84 }
87 85
86 void DialRegistry::RegisterObserver(Observer* observer) {
87 DCHECK_CURRENTLY_ON(BrowserThread::IO);
88 observers_.AddObserver(observer);
89 }
90
91 void DialRegistry::UnregisterObserver(Observer* observer) {
92 DCHECK_CURRENTLY_ON(BrowserThread::IO);
93 observers_.RemoveObserver(observer);
94 }
95
88 GURL DialRegistry::GetDeviceDescriptionURL(const std::string& label) const { 96 GURL DialRegistry::GetDeviceDescriptionURL(const std::string& label) const {
89 const auto device_it = device_by_label_map_.find(label); 97 const auto device_it = device_by_label_map_.find(label);
90 if (device_it != device_by_label_map_.end()) 98 if (device_it != device_by_label_map_.end())
91 return device_it->second->device_description_url(); 99 return device_it->second->device_description_url();
92 100
93 return GURL(); 101 return GURL();
94 } 102 }
95 103
96 void DialRegistry::AddDeviceForTest(const DialDeviceData& device_data) { 104 void DialRegistry::AddDeviceForTest(const DialDeviceData& device_data) {
97 std::unique_ptr<DialDeviceData> test_data = 105 std::unique_ptr<DialDeviceData> test_data =
98 base::MakeUnique<DialDeviceData>(device_data); 106 base::MakeUnique<DialDeviceData>(device_data);
99 device_by_label_map_.insert( 107 device_by_label_map_.insert(
100 std::make_pair(device_data.label(), test_data.get())); 108 std::make_pair(device_data.label(), test_data.get()));
101 device_by_id_map_.insert( 109 device_by_id_map_.insert(
102 std::make_pair(device_data.device_id(), std::move(test_data))); 110 std::make_pair(device_data.device_id(), std::move(test_data)));
103 } 111 }
104 112
105 bool DialRegistry::ReadyToDiscover() { 113 bool DialRegistry::ReadyToDiscover() {
106 if (num_listeners_ == 0) { 114 if (num_listeners_ == 0) {
107 dial_api_->OnDialError(DIAL_NO_LISTENERS); 115 OnDialError(DIAL_NO_LISTENERS);
108 return false; 116 return false;
109 } 117 }
110 if (NetworkChangeNotifier::IsOffline()) { 118 if (NetworkChangeNotifier::IsOffline()) {
111 dial_api_->OnDialError(DIAL_NETWORK_DISCONNECTED); 119 OnDialError(DIAL_NETWORK_DISCONNECTED);
112 return false; 120 return false;
113 } 121 }
114 if (NetworkChangeNotifier::IsConnectionCellular( 122 if (NetworkChangeNotifier::IsConnectionCellular(
115 NetworkChangeNotifier::GetConnectionType())) { 123 NetworkChangeNotifier::GetConnectionType())) {
116 dial_api_->OnDialError(DIAL_CELLULAR_NETWORK); 124 OnDialError(DIAL_CELLULAR_NETWORK);
117 return false; 125 return false;
118 } 126 }
119 return true; 127 return true;
120 } 128 }
121 129
122 bool DialRegistry::DiscoverNow() { 130 bool DialRegistry::DiscoverNow() {
123 DCHECK_CURRENTLY_ON(BrowserThread::IO); 131 DCHECK_CURRENTLY_ON(BrowserThread::IO);
124 if (!ReadyToDiscover()) { 132 if (!ReadyToDiscover()) {
125 return false; 133 return false;
126 } 134 }
127 if (!dial_) { 135 if (!dial_) {
128 dial_api_->OnDialError(DIAL_UNKNOWN); 136 OnDialError(DIAL_UNKNOWN);
129 return false; 137 return false;
130 } 138 }
131 139
132 if (!dial_->HasObserver(this)) 140 if (!dial_->HasObserver(this))
133 NOTREACHED() << "DiscoverNow() called without observing dial_"; 141 NOTREACHED() << "DiscoverNow() called without observing dial_";
134 142
135 // Force increment |registry_generation_| to ensure an event is sent even if 143 // Force increment |registry_generation_| to ensure an event is sent even if
136 // the device list did not change. 144 // the device list did not change.
137 bool started = dial_->Discover(); 145 bool started = dial_->Discover();
138 if (started) 146 if (started)
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 if (needs_event) 236 if (needs_event)
229 SendEvent(); 237 SendEvent();
230 } 238 }
231 239
232 void DialRegistry::SendEvent() { 240 void DialRegistry::SendEvent() {
233 DeviceList device_list; 241 DeviceList device_list;
234 for (DeviceByLabelMap::const_iterator it = device_by_label_map_.begin(); 242 for (DeviceByLabelMap::const_iterator it = device_by_label_map_.begin();
235 it != device_by_label_map_.end(); ++it) { 243 it != device_by_label_map_.end(); ++it) {
236 device_list.push_back(*(it->second)); 244 device_list.push_back(*(it->second));
237 } 245 }
238 dial_api_->OnDialDeviceEvent(device_list); 246 OnDialDeviceEvent(device_list);
239 247
240 // Reset watermark. 248 // Reset watermark.
241 last_event_registry_generation_ = registry_generation_; 249 last_event_registry_generation_ = registry_generation_;
242 } 250 }
243 251
244 std::string DialRegistry::NextLabel() { 252 std::string DialRegistry::NextLabel() {
245 DCHECK_CURRENTLY_ON(BrowserThread::IO); 253 DCHECK_CURRENTLY_ON(BrowserThread::IO);
246 return base::IntToString(++label_count_); 254 return base::IntToString(++label_count_);
247 } 255 }
248 256
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 if (PruneExpiredDevices()) 311 if (PruneExpiredDevices())
304 registry_generation_++; 312 registry_generation_++;
305 MaybeSendEvent(); 313 MaybeSendEvent();
306 } 314 }
307 315
308 void DialRegistry::OnError(DialService* service, 316 void DialRegistry::OnError(DialService* service,
309 const DialService::DialServiceErrorCode& code) { 317 const DialService::DialServiceErrorCode& code) {
310 DCHECK_CURRENTLY_ON(BrowserThread::IO); 318 DCHECK_CURRENTLY_ON(BrowserThread::IO);
311 switch (code) { 319 switch (code) {
312 case DialService::DIAL_SERVICE_SOCKET_ERROR: 320 case DialService::DIAL_SERVICE_SOCKET_ERROR:
313 dial_api_->OnDialError(DIAL_SOCKET_ERROR); 321 OnDialError(DIAL_SOCKET_ERROR);
314 break; 322 break;
315 case DialService::DIAL_SERVICE_NO_INTERFACES: 323 case DialService::DIAL_SERVICE_NO_INTERFACES:
316 dial_api_->OnDialError(DIAL_NO_INTERFACES); 324 OnDialError(DIAL_NO_INTERFACES);
317 break; 325 break;
318 default: 326 default:
319 NOTREACHED(); 327 NOTREACHED();
320 dial_api_->OnDialError(DIAL_UNKNOWN); 328 OnDialError(DIAL_UNKNOWN);
321 break; 329 break;
322 } 330 }
323 } 331 }
324 332
325 void DialRegistry::OnNetworkChanged( 333 void DialRegistry::OnNetworkChanged(
326 NetworkChangeNotifier::ConnectionType type) { 334 NetworkChangeNotifier::ConnectionType type) {
327 switch (type) { 335 switch (type) {
328 case NetworkChangeNotifier::CONNECTION_NONE: 336 case NetworkChangeNotifier::CONNECTION_NONE:
329 if (dial_) { 337 if (dial_) {
330 VLOG(2) << "Lost connection, shutting down discovery and clearing" 338 VLOG(2) << "Lost connection, shutting down discovery and clearing"
331 << " list."; 339 << " list.";
332 dial_api_->OnDialError(DIAL_NETWORK_DISCONNECTED); 340 OnDialError(DIAL_NETWORK_DISCONNECTED);
333 341
334 StopPeriodicDiscovery(); 342 StopPeriodicDiscovery();
335 // TODO(justinlin): As an optimization, we can probably keep our device 343 // TODO(justinlin): As an optimization, we can probably keep our device
336 // list around and restore it if we reconnected to the exact same 344 // list around and restore it if we reconnected to the exact same
337 // network. 345 // network.
338 Clear(); 346 Clear();
339 MaybeSendEvent(); 347 MaybeSendEvent();
340 } 348 }
341 break; 349 break;
342 case NetworkChangeNotifier::CONNECTION_2G: 350 case NetworkChangeNotifier::CONNECTION_2G:
343 case NetworkChangeNotifier::CONNECTION_3G: 351 case NetworkChangeNotifier::CONNECTION_3G:
344 case NetworkChangeNotifier::CONNECTION_4G: 352 case NetworkChangeNotifier::CONNECTION_4G:
345 case NetworkChangeNotifier::CONNECTION_ETHERNET: 353 case NetworkChangeNotifier::CONNECTION_ETHERNET:
346 case NetworkChangeNotifier::CONNECTION_WIFI: 354 case NetworkChangeNotifier::CONNECTION_WIFI:
347 case NetworkChangeNotifier::CONNECTION_UNKNOWN: 355 case NetworkChangeNotifier::CONNECTION_UNKNOWN:
348 case NetworkChangeNotifier::CONNECTION_BLUETOOTH: 356 case NetworkChangeNotifier::CONNECTION_BLUETOOTH:
349 if (!dial_) { 357 if (!dial_) {
350 VLOG(2) << "Connection detected, restarting discovery."; 358 VLOG(2) << "Connection detected, restarting discovery.";
351 StartPeriodicDiscovery(); 359 StartPeriodicDiscovery();
352 } 360 }
353 break; 361 break;
354 } 362 }
355 } 363 }
356 364
365 void DialRegistry::OnDialDeviceEvent(const DeviceList& devices) {
366 for (auto& observer : observers_)
367 observer.OnDialDeviceEvent(devices);
368 }
369
370 void DialRegistry::OnDialError(DialErrorCode type) {
371 for (auto& observer : observers_)
372 observer.OnDialError(type);
373 }
374
357 } // namespace dial 375 } // namespace dial
358 } // namespace api 376 } // namespace api
359 } // namespace extensions 377 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/dial/dial_registry.h ('k') | chrome/browser/extensions/api/dial/dial_registry_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698