| 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 "chromeos/components/tether/host_scanner.h" | 5 #include "chromeos/components/tether/host_scanner.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | 10 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 HostScanner::HostScanner( | 36 HostScanner::HostScanner( |
| 37 TetherHostFetcher* tether_host_fetcher, | 37 TetherHostFetcher* tether_host_fetcher, |
| 38 BleConnectionManager* connection_manager, | 38 BleConnectionManager* connection_manager, |
| 39 HostScanDevicePrioritizer* host_scan_device_prioritizer, | 39 HostScanDevicePrioritizer* host_scan_device_prioritizer, |
| 40 TetherHostResponseRecorder* tether_host_response_recorder, | 40 TetherHostResponseRecorder* tether_host_response_recorder, |
| 41 NotificationPresenter* notification_presenter, | 41 NotificationPresenter* notification_presenter, |
| 42 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, | 42 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map, |
| 43 HostScanCache* host_scan_cache) | 43 HostScanCache* host_scan_cache, |
| 44 base::Clock* clock) |
| 44 : tether_host_fetcher_(tether_host_fetcher), | 45 : tether_host_fetcher_(tether_host_fetcher), |
| 45 connection_manager_(connection_manager), | 46 connection_manager_(connection_manager), |
| 46 host_scan_device_prioritizer_(host_scan_device_prioritizer), | 47 host_scan_device_prioritizer_(host_scan_device_prioritizer), |
| 47 tether_host_response_recorder_(tether_host_response_recorder), | 48 tether_host_response_recorder_(tether_host_response_recorder), |
| 48 notification_presenter_(notification_presenter), | 49 notification_presenter_(notification_presenter), |
| 49 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), | 50 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), |
| 50 host_scan_cache_(host_scan_cache), | 51 host_scan_cache_(host_scan_cache), |
| 52 clock_(clock), |
| 51 is_fetching_hosts_(false), | 53 is_fetching_hosts_(false), |
| 52 weak_ptr_factory_(this) {} | 54 weak_ptr_factory_(this) {} |
| 53 | 55 |
| 54 HostScanner::~HostScanner() {} | 56 HostScanner::~HostScanner() {} |
| 55 | 57 |
| 58 bool HostScanner::IsScanActive() { |
| 59 return is_fetching_hosts_ || host_scanner_operation_; |
| 60 } |
| 61 |
| 62 bool HostScanner::HasRecentlyScanned() { |
| 63 if (previous_scan_time_.is_null()) |
| 64 return false; |
| 65 |
| 66 base::TimeDelta difference = clock_->Now() - previous_scan_time_; |
| 67 return difference.InMinutes() < |
| 68 HostScanCache::kNumMinutesBeforeCacheEntryExpires; |
| 69 } |
| 70 |
| 56 void HostScanner::StartScan() { | 71 void HostScanner::StartScan() { |
| 57 if (host_scanner_operation_) { | 72 if (IsScanActive()) { |
| 58 // If a scan is already active, do not continue. | |
| 59 return; | 73 return; |
| 60 } | 74 } |
| 61 | 75 |
| 62 if (is_fetching_hosts_) { | |
| 63 // If fetching tether hosts is active, stop and wait for them to load. | |
| 64 return; | |
| 65 } | |
| 66 is_fetching_hosts_ = true; | 76 is_fetching_hosts_ = true; |
| 67 | |
| 68 tether_host_fetcher_->FetchAllTetherHosts(base::Bind( | 77 tether_host_fetcher_->FetchAllTetherHosts(base::Bind( |
| 69 &HostScanner::OnTetherHostsFetched, weak_ptr_factory_.GetWeakPtr())); | 78 &HostScanner::OnTetherHostsFetched, weak_ptr_factory_.GetWeakPtr())); |
| 70 } | 79 } |
| 71 | 80 |
| 72 bool HostScanner::IsScanActive() { | |
| 73 return is_fetching_hosts_ || host_scanner_operation_; | |
| 74 } | |
| 75 | |
| 76 void HostScanner::OnTetherHostsFetched( | 81 void HostScanner::OnTetherHostsFetched( |
| 77 const cryptauth::RemoteDeviceList& tether_hosts) { | 82 const cryptauth::RemoteDeviceList& tether_hosts) { |
| 78 is_fetching_hosts_ = false; | 83 is_fetching_hosts_ = false; |
| 79 | 84 |
| 80 host_scanner_operation_ = HostScannerOperation::Factory::NewInstance( | 85 host_scanner_operation_ = HostScannerOperation::Factory::NewInstance( |
| 81 tether_hosts, connection_manager_, host_scan_device_prioritizer_, | 86 tether_hosts, connection_manager_, host_scan_device_prioritizer_, |
| 82 tether_host_response_recorder_); | 87 tether_host_response_recorder_); |
| 83 host_scanner_operation_->AddObserver(this); | 88 host_scanner_operation_->AddObserver(this); |
| 84 host_scanner_operation_->Initialize(); | 89 host_scanner_operation_->Initialize(); |
| 85 } | 90 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 105 } else { | 110 } else { |
| 106 notification_presenter_->NotifyMultiplePotentialHotspotsNearby(); | 111 notification_presenter_->NotifyMultiplePotentialHotspotsNearby(); |
| 107 } | 112 } |
| 108 } | 113 } |
| 109 | 114 |
| 110 if (is_final_scan_result) { | 115 if (is_final_scan_result) { |
| 111 // If the final scan result has been received, the operation is finished. | 116 // If the final scan result has been received, the operation is finished. |
| 112 // Delete it. | 117 // Delete it. |
| 113 host_scanner_operation_->RemoveObserver(this); | 118 host_scanner_operation_->RemoveObserver(this); |
| 114 host_scanner_operation_.reset(); | 119 host_scanner_operation_.reset(); |
| 120 NotifyScanFinished(); |
| 121 previous_scan_time_ = clock_->Now(); |
| 115 } | 122 } |
| 116 } | 123 } |
| 117 | 124 |
| 125 void HostScanner::AddObserver(Observer* observer) { |
| 126 observer_list_.AddObserver(observer); |
| 127 } |
| 128 |
| 129 void HostScanner::RemoveObserver(Observer* observer) { |
| 130 observer_list_.RemoveObserver(observer); |
| 131 } |
| 132 |
| 133 void HostScanner::NotifyScanFinished() { |
| 134 for (auto& observer : observer_list_) { |
| 135 observer.ScanFinished(); |
| 136 } |
| 137 } |
| 138 |
| 118 void HostScanner::SetCacheEntry( | 139 void HostScanner::SetCacheEntry( |
| 119 const HostScannerOperation::ScannedDeviceInfo& scanned_device_info) { | 140 const HostScannerOperation::ScannedDeviceInfo& scanned_device_info) { |
| 120 const DeviceStatus& status = scanned_device_info.device_status; | 141 const DeviceStatus& status = scanned_device_info.device_status; |
| 121 const cryptauth::RemoteDevice& remote_device = | 142 const cryptauth::RemoteDevice& remote_device = |
| 122 scanned_device_info.remote_device; | 143 scanned_device_info.remote_device; |
| 123 | 144 |
| 124 // Use a sentinel value if carrier information is not available. This value is | 145 // Use a sentinel value if carrier information is not available. This value is |
| 125 // special-cased and replaced with a localized string in the settings UI. | 146 // special-cased and replaced with a localized string in the settings UI. |
| 126 const std::string carrier = | 147 const std::string carrier = |
| 127 (!status.has_cell_provider() || status.cell_provider().empty()) | 148 (!status.has_cell_provider() || status.cell_provider().empty()) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 145 | 166 |
| 146 host_scan_cache_->SetHostScanResult( | 167 host_scan_cache_->SetHostScanResult( |
| 147 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( | 168 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
| 148 remote_device.GetDeviceId()), | 169 remote_device.GetDeviceId()), |
| 149 remote_device.name, carrier, battery_percentage, signal_strength); | 170 remote_device.name, carrier, battery_percentage, signal_strength); |
| 150 } | 171 } |
| 151 | 172 |
| 152 } // namespace tether | 173 } // namespace tether |
| 153 | 174 |
| 154 } // namespace chromeos | 175 } // namespace chromeos |
| OLD | NEW |