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