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> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
8 #include "chromeos/components/tether/tether_host_fetcher.h" | 11 #include "chromeos/components/tether/tether_host_fetcher.h" |
9 #include "chromeos/network/network_state.h" | 12 #include "chromeos/network/network_state.h" |
10 #include "components/cryptauth/remote_device_loader.h" | 13 #include "components/cryptauth/remote_device_loader.h" |
11 | 14 |
12 namespace chromeos { | 15 namespace chromeos { |
13 | 16 |
14 namespace tether { | 17 namespace tether { |
15 | 18 |
| 19 namespace { |
| 20 |
| 21 const char kDefaultCellCarrierName[] = "unknown-carrier"; |
| 22 |
| 23 // 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 // convert between Android signal strength to Chrome OS signal strength, the |
| 26 // value must be multiplied by the below value. |
| 27 const int32_t kAndroidTetherHostToChromeOSSignalStrengthMultiplier = 25; |
| 28 |
| 29 int32_t ForceBetweenZeroAndOneHundred(int32_t value) { |
| 30 return std::min(std::max(value, 0), 100); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
16 HostScanner::HostScanner( | 35 HostScanner::HostScanner( |
17 TetherHostFetcher* tether_host_fetcher, | 36 TetherHostFetcher* tether_host_fetcher, |
18 BleConnectionManager* connection_manager, | 37 BleConnectionManager* connection_manager, |
19 HostScanDevicePrioritizer* host_scan_device_prioritizer, | 38 HostScanDevicePrioritizer* host_scan_device_prioritizer, |
20 NetworkStateHandler* network_state_handler, | 39 NetworkStateHandler* network_state_handler, |
21 NotificationPresenter* notification_presenter) | 40 NotificationPresenter* notification_presenter, |
| 41 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map) |
22 : tether_host_fetcher_(tether_host_fetcher), | 42 : tether_host_fetcher_(tether_host_fetcher), |
23 connection_manager_(connection_manager), | 43 connection_manager_(connection_manager), |
24 host_scan_device_prioritizer_(host_scan_device_prioritizer), | 44 host_scan_device_prioritizer_(host_scan_device_prioritizer), |
25 network_state_handler_(network_state_handler), | 45 network_state_handler_(network_state_handler), |
26 notification_presenter_(notification_presenter), | 46 notification_presenter_(notification_presenter), |
| 47 device_id_tether_network_guid_map_(device_id_tether_network_guid_map), |
27 is_fetching_hosts_(false), | 48 is_fetching_hosts_(false), |
28 weak_ptr_factory_(this) {} | 49 weak_ptr_factory_(this) {} |
29 | 50 |
30 HostScanner::~HostScanner() {} | 51 HostScanner::~HostScanner() {} |
31 | 52 |
32 void HostScanner::StartScan() { | 53 void HostScanner::StartScan() { |
33 if (host_scanner_operation_) { | 54 if (host_scanner_operation_) { |
34 // If a scan is already active, do not continue. | 55 // If a scan is already active, do not continue. |
35 return; | 56 return; |
36 } | 57 } |
(...skipping 22 matching lines...) Expand all Loading... |
59 host_scanner_operation_->Initialize(); | 80 host_scanner_operation_->Initialize(); |
60 } | 81 } |
61 | 82 |
62 void HostScanner::OnTetherAvailabilityResponse( | 83 void HostScanner::OnTetherAvailabilityResponse( |
63 std::vector<HostScannerOperation::ScannedDeviceInfo>& | 84 std::vector<HostScannerOperation::ScannedDeviceInfo>& |
64 scanned_device_list_so_far, | 85 scanned_device_list_so_far, |
65 bool is_final_scan_result) { | 86 bool is_final_scan_result) { |
66 most_recent_scan_results_ = scanned_device_list_so_far; | 87 most_recent_scan_results_ = scanned_device_list_so_far; |
67 | 88 |
68 if (!scanned_device_list_so_far.empty()) { | 89 if (!scanned_device_list_so_far.empty()) { |
69 // TODO (hansberry): Clear out old scanned hosts from NetworkStateHandler. | 90 // TODO(hansberry): Clear out old scanned hosts from NetworkStateHandler. |
70 // TODO (hansberry): Add battery and cell strength properties once | 91 // TODO(khorimoto): Use UpdateTetherNetworkProperties if the network already |
71 // available. | 92 // exists. |
72 for (auto& scanned_device_info : scanned_device_list_so_far) { | 93 for (auto& scanned_device_info : scanned_device_list_so_far) { |
73 cryptauth::RemoteDevice remote_device = scanned_device_info.remote_device; | 94 const DeviceStatus& status = scanned_device_info.device_status; |
74 network_state_handler_->AddTetherNetworkState(remote_device.GetDeviceId(), | 95 const cryptauth::RemoteDevice& remote_device = |
75 remote_device.name); | 96 scanned_device_info.remote_device; |
| 97 |
| 98 const std::string carrier = |
| 99 (!status.has_cell_provider() || status.cell_provider().empty()) |
| 100 ? kDefaultCellCarrierName |
| 101 : status.cell_provider(); |
| 102 |
| 103 // If battery or signal strength are missing, assume they are 100. For |
| 104 // battery percentage, force the value to be between 0 and 100. For signal |
| 105 // strength, convert from Android signal strength to Chrome OS signal |
| 106 // strength and force the value to be between 0 and 100. |
| 107 const int32_t battery_percentage = |
| 108 status.has_battery_percentage() |
| 109 ? ForceBetweenZeroAndOneHundred(status.battery_percentage()) |
| 110 : 100; |
| 111 const int32_t signal_strength = |
| 112 status.has_connection_strength() |
| 113 ? ForceBetweenZeroAndOneHundred( |
| 114 kAndroidTetherHostToChromeOSSignalStrengthMultiplier * |
| 115 status.connection_strength()) |
| 116 : 100; |
| 117 |
| 118 network_state_handler_->AddTetherNetworkState( |
| 119 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( |
| 120 remote_device.GetDeviceId()), |
| 121 remote_device.name, carrier, battery_percentage, signal_strength); |
76 } | 122 } |
77 | 123 |
78 if (scanned_device_list_so_far.size() == 1) { | 124 if (scanned_device_list_so_far.size() == 1) { |
79 notification_presenter_->NotifyPotentialHotspotNearby( | 125 notification_presenter_->NotifyPotentialHotspotNearby( |
80 scanned_device_list_so_far.at(0).remote_device); | 126 scanned_device_list_so_far.at(0).remote_device); |
81 } else { | 127 } else { |
82 notification_presenter_->NotifyMultiplePotentialHotspotsNearby(); | 128 notification_presenter_->NotifyMultiplePotentialHotspotsNearby(); |
83 } | 129 } |
84 } | 130 } |
85 | 131 |
86 if (is_final_scan_result) { | 132 if (is_final_scan_result) { |
87 // If the final scan result has been received, the operation is finished. | 133 // If the final scan result has been received, the operation is finished. |
88 // Delete it. | 134 // Delete it. |
89 host_scanner_operation_->RemoveObserver(this); | 135 host_scanner_operation_->RemoveObserver(this); |
90 host_scanner_operation_.reset(); | 136 host_scanner_operation_.reset(); |
91 } | 137 } |
92 } | 138 } |
93 | 139 |
94 } // namespace tether | 140 } // namespace tether |
95 | 141 |
96 } // namespace chromeos | 142 } // namespace chromeos |
OLD | NEW |