Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: chromeos/components/tether/host_scanner.cc

Issue 2819383002: [CrOS Tether] Update NetworkState to include tether properties and integrate into NetworkStateHandl… (Closed)
Patch Set: Rebased. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "chromeos/components/tether/device_id_tether_network_guid_map.h"
8 #include "chromeos/components/tether/tether_host_fetcher.h" 9 #include "chromeos/components/tether/tether_host_fetcher.h"
9 #include "chromeos/network/network_state.h" 10 #include "chromeos/network/network_state.h"
10 #include "components/cryptauth/remote_device_loader.h" 11 #include "components/cryptauth/remote_device_loader.h"
11 12
12 namespace chromeos { 13 namespace chromeos {
13 14
14 namespace tether { 15 namespace tether {
15 16
17 namespace {
18
19 // TODO(khorimoto): Localize this name.
20 const char kDefaultCellCarrierName[] = "Unknown Carrier";
21
22 // Android signal strength is measured between 0 and 4 (inclusive), but Chrome
23 // OS signal strength is measured between 0 and 100 (inclusive). In order to
24 // convert between Android signal strength to Chrome OS signal strength, the
25 // value must be multiplied by the below value.
26 const int32_t kAndroidTetherHostToChromeOSSignalStrengthMultiplier = 25;
27
28 int32_t ForceBetweenZeroAndOneHundred(int32_t value) {
29 if (value < 0)
30 return 0;
31 if (value > 100)
32 return 100;
33 return value;
34 }
35
36 } // namespace
37
16 HostScanner::HostScanner( 38 HostScanner::HostScanner(
17 TetherHostFetcher* tether_host_fetcher, 39 TetherHostFetcher* tether_host_fetcher,
18 BleConnectionManager* connection_manager, 40 BleConnectionManager* connection_manager,
19 HostScanDevicePrioritizer* host_scan_device_prioritizer, 41 HostScanDevicePrioritizer* host_scan_device_prioritizer,
20 NetworkStateHandler* network_state_handler, 42 NetworkStateHandler* network_state_handler,
21 NotificationPresenter* notification_presenter) 43 NotificationPresenter* notification_presenter,
44 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map)
22 : tether_host_fetcher_(tether_host_fetcher), 45 : tether_host_fetcher_(tether_host_fetcher),
23 connection_manager_(connection_manager), 46 connection_manager_(connection_manager),
24 host_scan_device_prioritizer_(host_scan_device_prioritizer), 47 host_scan_device_prioritizer_(host_scan_device_prioritizer),
25 network_state_handler_(network_state_handler), 48 network_state_handler_(network_state_handler),
26 notification_presenter_(notification_presenter), 49 notification_presenter_(notification_presenter),
50 device_id_tether_network_guid_map_(device_id_tether_network_guid_map),
27 is_fetching_hosts_(false), 51 is_fetching_hosts_(false),
28 weak_ptr_factory_(this) {} 52 weak_ptr_factory_(this) {}
29 53
30 HostScanner::~HostScanner() {} 54 HostScanner::~HostScanner() {}
31 55
32 void HostScanner::StartScan() { 56 void HostScanner::StartScan() {
33 if (host_scanner_operation_) { 57 if (host_scanner_operation_) {
34 // If a scan is already active, do not continue. 58 // If a scan is already active, do not continue.
35 return; 59 return;
36 } 60 }
(...skipping 22 matching lines...) Expand all
59 host_scanner_operation_->Initialize(); 83 host_scanner_operation_->Initialize();
60 } 84 }
61 85
62 void HostScanner::OnTetherAvailabilityResponse( 86 void HostScanner::OnTetherAvailabilityResponse(
63 std::vector<HostScannerOperation::ScannedDeviceInfo>& 87 std::vector<HostScannerOperation::ScannedDeviceInfo>&
64 scanned_device_list_so_far, 88 scanned_device_list_so_far,
65 bool is_final_scan_result) { 89 bool is_final_scan_result) {
66 most_recent_scan_results_ = scanned_device_list_so_far; 90 most_recent_scan_results_ = scanned_device_list_so_far;
67 91
68 if (!scanned_device_list_so_far.empty()) { 92 if (!scanned_device_list_so_far.empty()) {
69 // TODO (hansberry): Clear out old scanned hosts from NetworkStateHandler. 93 // TODO(hansberry): Clear out old scanned hosts from NetworkStateHandler.
70 // TODO (hansberry): Add battery and cell strength properties once 94 // TODO(khorimoto): Use UpdateTetherNetworkProperties if the network already
71 // available. 95 // exists.
72 for (auto& scanned_device_info : scanned_device_list_so_far) { 96 for (auto& scanned_device_info : scanned_device_list_so_far) {
73 cryptauth::RemoteDevice remote_device = scanned_device_info.remote_device; 97 const DeviceStatus& status = scanned_device_info.device_status;
74 network_state_handler_->AddTetherNetworkState(remote_device.GetDeviceId(), 98 const cryptauth::RemoteDevice& remote_device =
75 remote_device.name); 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 ? 100
113 : ForceBetweenZeroAndOneHundred(status.battery_percentage());
114 const int32_t signal_strength =
115 status.has_connection_strength()
116 ? 100
117 : ForceBetweenZeroAndOneHundred(
118 kAndroidTetherHostToChromeOSSignalStrengthMultiplier *
119 status.connection_strength());
120
121 network_state_handler_->AddTetherNetworkState(
122 device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId(
123 remote_device.GetDeviceId()),
124 remote_device.name, carrier, battery_percentage, signal_strength);
76 } 125 }
77 126
78 if (scanned_device_list_so_far.size() == 1) { 127 if (scanned_device_list_so_far.size() == 1) {
79 notification_presenter_->NotifyPotentialHotspotNearby( 128 notification_presenter_->NotifyPotentialHotspotNearby(
80 scanned_device_list_so_far.at(0).remote_device); 129 scanned_device_list_so_far.at(0).remote_device);
81 } else { 130 } else {
82 notification_presenter_->NotifyMultiplePotentialHotspotsNearby(); 131 notification_presenter_->NotifyMultiplePotentialHotspotsNearby();
83 } 132 }
84 } 133 }
85 134
86 if (is_final_scan_result) { 135 if (is_final_scan_result) {
87 // If the final scan result has been received, the operation is finished. 136 // If the final scan result has been received, the operation is finished.
88 // Delete it. 137 // Delete it.
89 host_scanner_operation_->RemoveObserver(this); 138 host_scanner_operation_->RemoveObserver(this);
90 host_scanner_operation_.reset(); 139 host_scanner_operation_.reset();
91 } 140 }
92 } 141 }
93 142
94 } // namespace tether 143 } // namespace tether
95 144
96 } // namespace chromeos 145 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698