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

Side by Side Diff: chromeos/network/geolocation_handler.cc

Issue 2624843003: Add support for cellular geolocation (Closed)
Patch Set: Remove GetCellTowers, fix indicator flag logic Created 3 years, 10 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/network/geolocation_handler.h" 5 #include "chromeos/network/geolocation_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chromeos/dbus/dbus_thread_manager.h" 13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_manager_client.h" 14 #include "chromeos/dbus/shill_manager_client.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h" 15 #include "third_party/cros_system_api/dbus/service_constants.h"
16 16
17 namespace chromeos { 17 namespace chromeos {
18 18
19 namespace {
20
21 constexpr const char* kDevicePropertyNames[] = {
22 shill::kGeoWifiAccessPointsProperty, shill::kGeoCellTowersProperty};
23
24 } // namespace
25
19 GeolocationHandler::GeolocationHandler() 26 GeolocationHandler::GeolocationHandler()
20 : wifi_enabled_(false), 27 : cellular_enabled_(false), wifi_enabled_(false), weak_ptr_factory_(this) {}
21 weak_ptr_factory_(this) {
22 }
23 28
24 GeolocationHandler::~GeolocationHandler() { 29 GeolocationHandler::~GeolocationHandler() {
25 ShillManagerClient* manager_client = 30 ShillManagerClient* manager_client =
26 DBusThreadManager::Get()->GetShillManagerClient(); 31 DBusThreadManager::Get()->GetShillManagerClient();
27 if (manager_client) 32 if (manager_client)
28 manager_client->RemovePropertyChangedObserver(this); 33 manager_client->RemovePropertyChangedObserver(this);
29 } 34 }
30 35
31 void GeolocationHandler::Init() { 36 void GeolocationHandler::Init() {
32 ShillManagerClient* manager_client = 37 ShillManagerClient* manager_client =
33 DBusThreadManager::Get()->GetShillManagerClient(); 38 DBusThreadManager::Get()->GetShillManagerClient();
34 manager_client->GetProperties( 39 manager_client->GetProperties(
35 base::Bind(&GeolocationHandler::ManagerPropertiesCallback, 40 base::Bind(&GeolocationHandler::ManagerPropertiesCallback,
36 weak_ptr_factory_.GetWeakPtr())); 41 weak_ptr_factory_.GetWeakPtr()));
37 manager_client->AddPropertyChangedObserver(this); 42 manager_client->AddPropertyChangedObserver(this);
38 } 43 }
39 44
40 bool GeolocationHandler::GetWifiAccessPoints( 45 bool GeolocationHandler::GetWifiAccessPoints(
41 WifiAccessPointVector* access_points, 46 WifiAccessPointVector* access_points,
42 int64_t* age_ms) { 47 int64_t* age_ms) {
43 if (!wifi_enabled_) 48 if (!wifi_enabled_)
44 return false; 49 return false;
45 // Always request updated access points. 50 // Always request updated info.
46 RequestWifiAccessPoints(); 51 RequestGeolocationObjects();
47 // If no data has been received, return false. 52 // If no data has been received, return false.
48 if (geolocation_received_time_.is_null()) 53 if (access_point_received_time_.is_null())
49 return false; 54 return false;
50 if (access_points) 55 if (access_points)
51 *access_points = wifi_access_points_; 56 *access_points = wifi_access_points_;
52 if (age_ms) { 57 if (age_ms) {
53 base::TimeDelta dtime = base::Time::Now() - geolocation_received_time_; 58 base::TimeDelta dtime = base::Time::Now() - access_point_received_time_;
54 *age_ms = dtime.InMilliseconds(); 59 *age_ms = dtime.InMilliseconds();
55 } 60 }
56 return true; 61 return true;
57 } 62 }
58 63
64 bool GeolocationHandler::GetNetworkInformation(
65 WifiAccessPointVector* access_points,
66 CellTowerVector* cell_towers) {
67 if (!cellular_enabled_ && !wifi_enabled_)
68 return false;
69
70 // Always request updated info.
71 RequestGeolocationObjects();
72
73 // If no data has been received, return false.
74 if (cell_towers_received_time_.is_null() &&
75 access_point_received_time_.is_null()) {
76 return false;
77 }
78
79 if (cell_towers)
80 *cell_towers = cell_towers_;
81 if (access_points)
82 *access_points = wifi_access_points_;
83
84 return true;
85 }
86
59 void GeolocationHandler::OnPropertyChanged(const std::string& key, 87 void GeolocationHandler::OnPropertyChanged(const std::string& key,
60 const base::Value& value) { 88 const base::Value& value) {
61 HandlePropertyChanged(key, value); 89 HandlePropertyChanged(key, value);
62 } 90 }
63 91
64 //------------------------------------------------------------------------------ 92 //------------------------------------------------------------------------------
65 // Private methods 93 // Private methods
66 94
67 void GeolocationHandler::ManagerPropertiesCallback( 95 void GeolocationHandler::ManagerPropertiesCallback(
68 DBusMethodCallStatus call_status, 96 DBusMethodCallStatus call_status,
69 const base::DictionaryValue& properties) { 97 const base::DictionaryValue& properties) {
70 const base::Value* value = NULL; 98 const base::Value* value = nullptr;
71 if (properties.Get(shill::kEnabledTechnologiesProperty, &value) && value) 99 if (properties.Get(shill::kEnabledTechnologiesProperty, &value) && value)
72 HandlePropertyChanged(shill::kEnabledTechnologiesProperty, *value); 100 HandlePropertyChanged(shill::kEnabledTechnologiesProperty, *value);
73 } 101 }
74 102
75 void GeolocationHandler::HandlePropertyChanged(const std::string& key, 103 void GeolocationHandler::HandlePropertyChanged(const std::string& key,
76 const base::Value& value) { 104 const base::Value& value) {
77 if (key != shill::kEnabledTechnologiesProperty) 105 if (key != shill::kEnabledTechnologiesProperty)
78 return; 106 return;
79 const base::ListValue* technologies = NULL; 107 const base::ListValue* technologies = nullptr;
80 if (!value.GetAsList(&technologies) || !technologies) 108 if (!value.GetAsList(&technologies) || !technologies)
81 return; 109 return;
82 bool wifi_was_enabled = wifi_enabled_; 110 bool wifi_was_enabled = wifi_enabled_;
111 bool cellular_was_enabled = cellular_enabled_;
112 cellular_enabled_ = false;
83 wifi_enabled_ = false; 113 wifi_enabled_ = false;
84 for (base::ListValue::const_iterator iter = technologies->begin(); 114 for (base::ListValue::const_iterator iter = technologies->begin();
85 iter != technologies->end(); ++iter) { 115 iter != technologies->end(); ++iter) {
86 std::string technology; 116 std::string technology;
87 (*iter)->GetAsString(&technology); 117 (*iter)->GetAsString(&technology);
88 if (technology == shill::kTypeWifi) { 118 if (technology == shill::kTypeWifi) {
89 wifi_enabled_ = true; 119 wifi_enabled_ = true;
120 } else if (technology == shill::kTypeCellular) {
121 cellular_enabled_ = true;
122 }
123 if (wifi_enabled_ && cellular_enabled_)
90 break; 124 break;
91 }
92 } 125 }
93 if (!wifi_was_enabled && wifi_enabled_) 126
94 RequestWifiAccessPoints(); // Request initial location data. 127 // Request initial location data.
128 if ((!wifi_was_enabled && wifi_enabled_) ||
129 (!cellular_was_enabled && cellular_enabled_)) {
130 RequestGeolocationObjects();
131 }
95 } 132 }
96 133
97 void GeolocationHandler::RequestWifiAccessPoints() { 134 void GeolocationHandler::RequestGeolocationObjects() {
98 DBusThreadManager::Get()->GetShillManagerClient()->GetNetworksForGeolocation( 135 DBusThreadManager::Get()->GetShillManagerClient()->GetNetworksForGeolocation(
99 base::Bind(&GeolocationHandler::GeolocationCallback, 136 base::Bind(&GeolocationHandler::GeolocationCallback,
100 weak_ptr_factory_.GetWeakPtr())); 137 weak_ptr_factory_.GetWeakPtr()));
101 } 138 }
102 139
103 void GeolocationHandler::GeolocationCallback( 140 void GeolocationHandler::GeolocationCallback(
104 DBusMethodCallStatus call_status, 141 DBusMethodCallStatus call_status,
105 const base::DictionaryValue& properties) { 142 const base::DictionaryValue& properties) {
106 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 143 if (call_status != DBUS_METHOD_CALL_SUCCESS) {
107 LOG(ERROR) << "Failed to get Geolocation data: " << call_status; 144 LOG(ERROR) << "Failed to get Geolocation data: " << call_status;
108 return; 145 return;
109 } 146 }
110 wifi_access_points_.clear(); 147 wifi_access_points_.clear();
148 cell_towers_.clear();
111 if (properties.empty()) 149 if (properties.empty())
112 return; // No enabled devices, don't update received time. 150 return; // No enabled devices, don't update received time.
113 151
114 // Dictionary<device_type, entry_list> 152 // Dictionary<device_type, entry_list>
115 for (base::DictionaryValue::Iterator iter(properties); 153 // Example dict returned from shill:
116 !iter.IsAtEnd(); iter.Advance()) { 154 // {
117 const base::ListValue* entry_list = NULL; 155 // kGeoWifiAccessPointsProperty: [ {kGeoMacAddressProperty: mac_value, ...},
118 if (!iter.value().GetAsList(&entry_list)) { 156 // ...
119 LOG(WARNING) << "Geolocation dictionary value not a List: " << iter.key(); 157 // ],
158 // kGeoCellTowersProperty: [ {kGeoCellIdProperty: cell_id_value, ...}, ... ]
159 // }
160 for (auto device_type : kDevicePropertyNames) {
161 if (!properties.HasKey(device_type)) {
120 continue; 162 continue;
121 } 163 }
164
165 const base::ListValue* entry_list = nullptr;
166 if (!properties.GetList(device_type, &entry_list)) {
167 LOG(WARNING) << "Geolocation dictionary value not a List: "
168 << device_type;
169 continue;
170 }
171
122 // List[Dictionary<key, value_str>] 172 // List[Dictionary<key, value_str>]
123 for (size_t i = 0; i < entry_list->GetSize(); ++i) { 173 for (size_t i = 0; i < entry_list->GetSize(); ++i) {
124 const base::DictionaryValue* entry = NULL; 174 const base::DictionaryValue* entry = nullptr;
125 if (!entry_list->GetDictionary(i, &entry) || !entry) { 175 if (!entry_list->GetDictionary(i, &entry) || !entry) {
126 LOG(WARNING) << "Geolocation list value not a Dictionary: " << i; 176 LOG(WARNING) << "Geolocation list value not a Dictionary: " << i;
127 continue; 177 continue;
128 } 178 }
129 // Docs: developers.google.com/maps/documentation/business/geolocation 179 if (device_type == shill::kGeoWifiAccessPointsProperty) {
130 WifiAccessPoint wap; 180 AddAccessPointFromDict(entry);
131 entry->GetString(shill::kGeoMacAddressProperty, &wap.mac_address); 181 } else if (device_type == shill::kGeoCellTowersProperty) {
132 std::string age_str; 182 AddCellTowerFromDict(entry);
133 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) {
134 int64_t age_ms;
135 if (base::StringToInt64(age_str, &age_ms)) {
136 wap.timestamp =
137 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms);
138 }
139 } 183 }
140 std::string strength_str;
141 if (entry->GetString(shill::kGeoSignalStrengthProperty, &strength_str))
142 base::StringToInt(strength_str, &wap.signal_strength);
143 std::string signal_str;
144 if (entry->GetString(shill::kGeoSignalToNoiseRatioProperty, &signal_str))
145 base::StringToInt(signal_str, &wap.signal_to_noise);
146 std::string channel_str;
147 if (entry->GetString(shill::kGeoChannelProperty, &channel_str))
148 base::StringToInt(channel_str, &wap.channel);
149 wifi_access_points_.push_back(wap);
150 } 184 }
151 } 185 }
152 geolocation_received_time_ = base::Time::Now(); 186 }
187
188 void GeolocationHandler::AddAccessPointFromDict(
189 const base::DictionaryValue* entry) {
190 // Docs: developers.google.com/maps/documentation/business/geolocation
191 WifiAccessPoint wap;
192
193 std::string age_str;
194 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) {
195 int64_t age_ms;
196 if (base::StringToInt64(age_str, &age_ms)) {
197 wap.timestamp =
198 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms);
199 }
200 }
201 entry->GetString(shill::kGeoMacAddressProperty, &wap.mac_address);
202
203 std::string strength_str;
204 if (entry->GetString(shill::kGeoSignalStrengthProperty, &strength_str))
205 base::StringToInt(strength_str, &wap.signal_strength);
206
207 std::string signal_str;
208 if (entry->GetString(shill::kGeoSignalToNoiseRatioProperty, &signal_str)) {
209 base::StringToInt(signal_str, &wap.signal_to_noise);
210 }
211
212 std::string channel_str;
213 if (entry->GetString(shill::kGeoChannelProperty, &channel_str))
214 base::StringToInt(channel_str, &wap.channel);
215
216 wifi_access_points_.push_back(wap);
217 access_point_received_time_ = base::Time::Now();
stevenjb 2017/02/07 23:54:29 This is redundant and a bit confusing. We should s
can Skylar cook 2017/02/08 21:15:49 Done.
218 }
219
220 void GeolocationHandler::AddCellTowerFromDict(
221 const base::DictionaryValue* entry) {
222 // Docs: developers.google.com/maps/documentation/business/geolocation
223 CellTower ct;
224
225 std::string age_str;
226 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) {
227 int64_t age_ms;
228 if (base::StringToInt64(age_str, &age_ms)) {
229 ct.timestamp =
230 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms);
231 }
232 }
233 entry->GetString(shill::kGeoCellIdProperty, &ct.ci);
234 entry->GetString(shill::kGeoLocationAreaCodeProperty, &ct.lac);
235 entry->GetString(shill::kGeoMobileCountryCodeProperty, &ct.mcc);
236 entry->GetString(shill::kGeoMobileNetworkCodeProperty, &ct.mnc);
237
238 cell_towers_.push_back(ct);
239 cell_towers_received_time_ = base::Time::Now();
153 } 240 }
154 241
155 } // namespace chromeos 242 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698