Chromium Code Reviews| 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" |
| 11 #include "chromeos/components/tether/host_scan_cache.h" | |
| 11 #include "chromeos/components/tether/tether_host_fetcher.h" | 12 #include "chromeos/components/tether/tether_host_fetcher.h" |
| 12 #include "chromeos/network/network_state.h" | 13 #include "chromeos/network/network_state.h" |
| 13 #include "components/cryptauth/remote_device_loader.h" | 14 #include "components/cryptauth/remote_device_loader.h" |
| 14 | 15 |
| 15 namespace chromeos { | 16 namespace chromeos { |
| 16 | 17 |
| 17 namespace tether { | 18 namespace tether { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const char kDefaultCellCarrierName[] = "unknown-carrier"; | 22 const char kDefaultCellCarrierName[] = "unknown-carrier"; |
| 22 | 23 |
| 23 // Android signal strength is measured between 0 and 4 (inclusive), but Chrome | 24 // Android signal strength is measured between 0 and 4 (inclusive), but Chrome |
| 24 // OS signal strength is measured between 0 and 100 (inclusive). In order to | 25 // OS signal strength is measured between 0 and 100 (inclusive). In order to |
| 25 // convert between Android signal strength to Chrome OS signal strength, the | 26 // convert between Android signal strength to Chrome OS signal strength, the |
| 26 // value must be multiplied by the below value. | 27 // value must be multiplied by the below value. |
| 27 const int32_t kAndroidTetherHostToChromeOSSignalStrengthMultiplier = 25; | 28 const int32_t kAndroidTetherHostToChromeOSSignalStrengthMultiplier = 25; |
| 28 | 29 |
| 29 int32_t ForceBetweenZeroAndOneHundred(int32_t value) { | 30 int32_t ForceBetweenZeroAndOneHundred(int32_t value) { |
| 30 return std::min(std::max(value, 0), 100); | 31 return std::min(std::max(value, 0), 100); |
| 31 } | 32 } |
| 32 | 33 |
| 33 } // namespace | 34 } // namespace |
| 34 | 35 |
| 35 HostScanner::HostScanner( | 36 HostScanner::HostScanner( |
| 36 TetherHostFetcher* tether_host_fetcher, | 37 TetherHostFetcher* tether_host_fetcher, |
| 37 BleConnectionManager* connection_manager, | 38 BleConnectionManager* connection_manager, |
| 38 HostScanDevicePrioritizer* host_scan_device_prioritizer, | 39 HostScanDevicePrioritizer* host_scan_device_prioritizer, |
| 39 TetherHostResponseRecorder* tether_host_response_recorder, | 40 TetherHostResponseRecorder* tether_host_response_recorder, |
| 40 NetworkStateHandler* network_state_handler, | |
| 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 : tether_host_fetcher_(tether_host_fetcher), | 44 : tether_host_fetcher_(tether_host_fetcher), |
| 44 connection_manager_(connection_manager), | 45 connection_manager_(connection_manager), |
| 45 host_scan_device_prioritizer_(host_scan_device_prioritizer), | 46 host_scan_device_prioritizer_(host_scan_device_prioritizer), |
| 46 tether_host_response_recorder_(tether_host_response_recorder), | 47 tether_host_response_recorder_(tether_host_response_recorder), |
| 47 network_state_handler_(network_state_handler), | |
| 48 notification_presenter_(notification_presenter), | 48 notification_presenter_(notification_presenter), |
| 49 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), | 49 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), |
| 50 host_scan_cache_(host_scan_cache), | |
| 50 is_fetching_hosts_(false), | 51 is_fetching_hosts_(false), |
| 51 weak_ptr_factory_(this) {} | 52 weak_ptr_factory_(this) {} |
| 52 | 53 |
| 53 HostScanner::~HostScanner() {} | 54 HostScanner::~HostScanner() {} |
| 54 | 55 |
| 55 void HostScanner::StartScan() { | 56 void HostScanner::StartScan() { |
| 56 if (host_scanner_operation_) { | 57 if (host_scanner_operation_) { |
| 57 // If a scan is already active, do not continue. | 58 // If a scan is already active, do not continue. |
| 58 return; | 59 return; |
| 59 } | 60 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 80 tether_hosts, connection_manager_, host_scan_device_prioritizer_, | 81 tether_hosts, connection_manager_, host_scan_device_prioritizer_, |
| 81 tether_host_response_recorder_); | 82 tether_host_response_recorder_); |
| 82 host_scanner_operation_->AddObserver(this); | 83 host_scanner_operation_->AddObserver(this); |
| 83 host_scanner_operation_->Initialize(); | 84 host_scanner_operation_->Initialize(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void HostScanner::OnTetherAvailabilityResponse( | 87 void HostScanner::OnTetherAvailabilityResponse( |
| 87 std::vector<HostScannerOperation::ScannedDeviceInfo>& | 88 std::vector<HostScannerOperation::ScannedDeviceInfo>& |
| 88 scanned_device_list_so_far, | 89 scanned_device_list_so_far, |
| 89 bool is_final_scan_result) { | 90 bool is_final_scan_result) { |
| 90 most_recent_scan_results_ = scanned_device_list_so_far; | 91 if (scanned_device_list_so_far.empty()) { |
| 91 | 92 // If a new scan is just starting up, remove existing cache entries; if they |
| 92 if (!scanned_device_list_so_far.empty()) { | 93 // are still within range to communicate with the current device, they will |
| 93 // TODO(hansberry): Clear out old scanned hosts from NetworkStateHandler. | 94 // show |
|
Ryan Hansberry
2017/04/28 22:31:04
Finish this sentence.
Kyle Horimoto
2017/04/28 22:45:04
Done.
| |
| 94 // TODO(khorimoto): Use UpdateTetherNetworkProperties if the network already | 95 host_scan_cache_->ClearCacheExceptForActiveHost(); |
| 95 // exists. | 96 } else { |
| 97 // Add all results received so far to the cache. | |
| 96 for (auto& scanned_device_info : scanned_device_list_so_far) { | 98 for (auto& scanned_device_info : scanned_device_list_so_far) { |
| 97 const DeviceStatus& status = scanned_device_info.device_status; | 99 SetCacheEntry(scanned_device_info); |
| 98 const cryptauth::RemoteDevice& remote_device = | |
| 99 scanned_device_info.remote_device; | |
| 100 | |
| 101 const std::string carrier = | |
| 102 (!status.has_cell_provider() || status.cell_provider().empty()) | |
| 103 ? kDefaultCellCarrierName | |
| 104 : status.cell_provider(); | |
| 105 | |
| 106 // If battery or signal strength are missing, assume they are 100. For | |
| 107 // battery percentage, force the value to be between 0 and 100. For signal | |
| 108 // strength, convert from Android signal strength to Chrome OS signal | |
| 109 // strength and force the value to be between 0 and 100. | |
| 110 const int32_t battery_percentage = | |
| 111 status.has_battery_percentage() | |
| 112 ? ForceBetweenZeroAndOneHundred(status.battery_percentage()) | |
| 113 : 100; | |
| 114 const int32_t signal_strength = | |
| 115 status.has_connection_strength() | |
| 116 ? ForceBetweenZeroAndOneHundred( | |
| 117 kAndroidTetherHostToChromeOSSignalStrengthMultiplier * | |
| 118 status.connection_strength()) | |
| 119 : 100; | |
| 120 | |
| 121 // TODO(khorimoto): Pass a HasConnectedToHost parameter to this function. | |
| 122 network_state_handler_->AddTetherNetworkState( | |
| 123 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( | |
| 124 remote_device.GetDeviceId()), | |
| 125 remote_device.name, carrier, battery_percentage, signal_strength); | |
| 126 } | 100 } |
| 127 | 101 |
| 128 if (scanned_device_list_so_far.size() == 1) { | 102 if (scanned_device_list_so_far.size() == 1) { |
| 129 notification_presenter_->NotifyPotentialHotspotNearby( | 103 notification_presenter_->NotifyPotentialHotspotNearby( |
| 130 scanned_device_list_so_far.at(0).remote_device); | 104 scanned_device_list_so_far.at(0).remote_device); |
| 131 } else { | 105 } else { |
| 132 notification_presenter_->NotifyMultiplePotentialHotspotsNearby(); | 106 notification_presenter_->NotifyMultiplePotentialHotspotsNearby(); |
| 133 } | 107 } |
| 134 } | 108 } |
| 135 | 109 |
| 136 if (is_final_scan_result) { | 110 if (is_final_scan_result) { |
| 137 // If the final scan result has been received, the operation is finished. | 111 // If the final scan result has been received, the operation is finished. |
| 138 // Delete it. | 112 // Delete it. |
| 139 host_scanner_operation_->RemoveObserver(this); | 113 host_scanner_operation_->RemoveObserver(this); |
| 140 host_scanner_operation_.reset(); | 114 host_scanner_operation_.reset(); |
| 141 } | 115 } |
| 142 } | 116 } |
| 143 | 117 |
| 118 void HostScanner::SetCacheEntry( | |
| 119 const HostScannerOperation::ScannedDeviceInfo& scanned_device_info) { | |
| 120 const DeviceStatus& status = scanned_device_info.device_status; | |
| 121 const cryptauth::RemoteDevice& remote_device = | |
| 122 scanned_device_info.remote_device; | |
| 123 | |
| 124 // 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. | |
| 126 const std::string carrier = | |
| 127 (!status.has_cell_provider() || status.cell_provider().empty()) | |
| 128 ? kDefaultCellCarrierName | |
| 129 : status.cell_provider(); | |
| 130 | |
| 131 // If battery or signal strength are missing, assume they are 100. For | |
| 132 // battery percentage, force the value to be between 0 and 100. For signal | |
| 133 // strength, convert from Android signal strength to Chrome OS signal | |
| 134 // strength and force the value to be between 0 and 100. | |
| 135 const int32_t battery_percentage = | |
| 136 status.has_battery_percentage() | |
| 137 ? ForceBetweenZeroAndOneHundred(status.battery_percentage()) | |
| 138 : 100; | |
| 139 const int32_t signal_strength = | |
| 140 status.has_connection_strength() | |
| 141 ? ForceBetweenZeroAndOneHundred( | |
| 142 kAndroidTetherHostToChromeOSSignalStrengthMultiplier * | |
| 143 status.connection_strength()) | |
| 144 : 100; | |
| 145 | |
| 146 host_scan_cache_->SetHostScanResult( | |
| 147 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( | |
| 148 remote_device.GetDeviceId()), | |
| 149 remote_device.name, carrier, battery_percentage, signal_strength); | |
| 150 } | |
| 151 | |
| 144 } // namespace tether | 152 } // namespace tether |
| 145 | 153 |
| 146 } // namespace chromeos | 154 } // namespace chromeos |
| OLD | NEW |