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