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

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

Issue 2624843003: Add support for cellular geolocation (Closed)
Patch Set: Add support for cellular geolocation 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 const std::string kDevicePropertyNames[] = {shill::kGeoWifiAccessPointsProperty,
22 shill::kGeoCellTowersProperty};
stevenjb 2017/02/03 02:12:03 constexpr
can Skylar cook 2017/02/03 21:47:06 Done.
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 access points.
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;
62 }
63
64 bool GeolocationHandler::GetCellTowers(CellTowerVector* cell_towers,
65 int64_t* age_ms) {
66 if (!cellular_enabled_)
67 return false;
68
69 // Always request updated info
70 RequestGeolocationObjects();
71
72 // If no data has been received, return false.
73 if (cell_towers_received_time_.is_null())
74 return false;
75
76 if (cell_towers)
77 *cell_towers = cell_towers_;
78
79 if (age_ms) {
80 base::TimeDelta dtime = base::Time::Now() - cell_towers_received_time_;
81 *age_ms = dtime.InMilliseconds();
82 }
83 return true;
57 } 84 }
58 85
59 void GeolocationHandler::OnPropertyChanged(const std::string& key, 86 void GeolocationHandler::OnPropertyChanged(const std::string& key,
60 const base::Value& value) { 87 const base::Value& value) {
61 HandlePropertyChanged(key, value); 88 HandlePropertyChanged(key, value);
62 } 89 }
63 90
64 //------------------------------------------------------------------------------ 91 //------------------------------------------------------------------------------
65 // Private methods 92 // Private methods
66 93
67 void GeolocationHandler::ManagerPropertiesCallback( 94 void GeolocationHandler::ManagerPropertiesCallback(
68 DBusMethodCallStatus call_status, 95 DBusMethodCallStatus call_status,
69 const base::DictionaryValue& properties) { 96 const base::DictionaryValue& properties) {
70 const base::Value* value = NULL; 97 const base::Value* value = NULL;
71 if (properties.Get(shill::kEnabledTechnologiesProperty, &value) && value) 98 if (properties.Get(shill::kEnabledTechnologiesProperty, &value) && value)
72 HandlePropertyChanged(shill::kEnabledTechnologiesProperty, *value); 99 HandlePropertyChanged(shill::kEnabledTechnologiesProperty, *value);
73 } 100 }
74 101
75 void GeolocationHandler::HandlePropertyChanged(const std::string& key, 102 void GeolocationHandler::HandlePropertyChanged(const std::string& key,
76 const base::Value& value) { 103 const base::Value& value) {
77 if (key != shill::kEnabledTechnologiesProperty) 104 if (key != shill::kEnabledTechnologiesProperty)
78 return; 105 return;
79 const base::ListValue* technologies = NULL; 106 const base::ListValue* technologies = NULL;
80 if (!value.GetAsList(&technologies) || !technologies) 107 if (!value.GetAsList(&technologies) || !technologies)
81 return; 108 return;
82 bool wifi_was_enabled = wifi_enabled_; 109 bool wifi_was_enabled = wifi_enabled_;
110 bool cellular_was_enabled = cellular_enabled_;
111 cellular_enabled_ = false;
83 wifi_enabled_ = false; 112 wifi_enabled_ = false;
84 for (base::ListValue::const_iterator iter = technologies->begin(); 113 for (base::ListValue::const_iterator iter = technologies->begin();
85 iter != technologies->end(); ++iter) { 114 iter != technologies->end(); ++iter) {
86 std::string technology; 115 std::string technology;
87 (*iter)->GetAsString(&technology); 116 (*iter)->GetAsString(&technology);
88 if (technology == shill::kTypeWifi) { 117 if (technology == shill::kTypeWifi) {
89 wifi_enabled_ = true; 118 wifi_enabled_ = true;
119 } else if (technology == shill::kTypeCellular) {
120 cellular_enabled_ = true;
121 }
122 if (wifi_enabled_ && cellular_enabled_)
90 break; 123 break;
91 }
92 } 124 }
93 if (!wifi_was_enabled && wifi_enabled_) 125
94 RequestWifiAccessPoints(); // Request initial location data. 126 // Request initial location data.
127 if ((!wifi_was_enabled && wifi_enabled_) ||
128 (!cellular_was_enabled && cellular_enabled_)) {
129 RequestGeolocationObjects();
130 }
95 } 131 }
96 132
97 void GeolocationHandler::RequestWifiAccessPoints() { 133 void GeolocationHandler::RequestGeolocationObjects() {
98 DBusThreadManager::Get()->GetShillManagerClient()->GetNetworksForGeolocation( 134 DBusThreadManager::Get()->GetShillManagerClient()->GetNetworksForGeolocation(
99 base::Bind(&GeolocationHandler::GeolocationCallback, 135 base::Bind(&GeolocationHandler::GeolocationCallback,
100 weak_ptr_factory_.GetWeakPtr())); 136 weak_ptr_factory_.GetWeakPtr()));
101 } 137 }
102 138
103 void GeolocationHandler::GeolocationCallback( 139 void GeolocationHandler::GeolocationCallback(
104 DBusMethodCallStatus call_status, 140 DBusMethodCallStatus call_status,
105 const base::DictionaryValue& properties) { 141 const base::DictionaryValue& properties) {
106 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 142 if (call_status != DBUS_METHOD_CALL_SUCCESS) {
107 LOG(ERROR) << "Failed to get Geolocation data: " << call_status; 143 LOG(ERROR) << "Failed to get Geolocation data: " << call_status;
108 return; 144 return;
109 } 145 }
110 wifi_access_points_.clear(); 146 wifi_access_points_.clear();
147 cell_towers_.clear();
111 if (properties.empty()) 148 if (properties.empty())
112 return; // No enabled devices, don't update received time. 149 return; // No enabled devices, don't update received time.
113 150
114 // Dictionary<device_type, entry_list> 151 // Dictionary<device_type, entry_list>
115 for (base::DictionaryValue::Iterator iter(properties); 152 // Example dict returned from shill:
116 !iter.IsAtEnd(); iter.Advance()) { 153 // {
117 const base::ListValue* entry_list = NULL; 154 // kGeoWifiAccessPointsProperty: [ {kGeoMacAddressProperty: mac_value, ...},
118 if (!iter.value().GetAsList(&entry_list)) { 155 // ...
119 LOG(WARNING) << "Geolocation dictionary value not a List: " << iter.key(); 156 // ],
157 // kGeoCellTowersProperty: [ {kGeoCellIdProperty: cell_id_value, ...}, ... ]
158 // }
159 for (auto device_type : kDevicePropertyNames) {
160 if (!properties.HasKey(device_type)) {
120 continue; 161 continue;
121 } 162 }
163
164 const base::ListValue* entry_list = NULL;
stevenjb 2017/02/03 02:12:02 nullptr (feel free to fix everywhere in this file.
can Skylar cook 2017/02/03 21:47:06 Done.
165 if (!properties.GetList(device_type, &entry_list)) {
166 LOG(WARNING) << "Geolocation dictionary value not a List: "
167 << device_type;
168 continue;
169 }
170
122 // List[Dictionary<key, value_str>] 171 // List[Dictionary<key, value_str>]
123 for (size_t i = 0; i < entry_list->GetSize(); ++i) { 172 for (size_t i = 0; i < entry_list->GetSize(); ++i) {
124 const base::DictionaryValue* entry = NULL; 173 const base::DictionaryValue* entry = NULL;
125 if (!entry_list->GetDictionary(i, &entry) || !entry) { 174 if (!entry_list->GetDictionary(i, &entry) || !entry) {
126 LOG(WARNING) << "Geolocation list value not a Dictionary: " << i; 175 LOG(WARNING) << "Geolocation list value not a Dictionary: " << i;
127 continue; 176 continue;
128 } 177 }
129 // Docs: developers.google.com/maps/documentation/business/geolocation 178 // Docs: developers.google.com/maps/documentation/business/geolocation
130 WifiAccessPoint wap; 179 if (device_type == shill::kGeoWifiAccessPointsProperty) {
131 entry->GetString(shill::kGeoMacAddressProperty, &wap.mac_address); 180 WifiAccessPoint wap;
132 std::string age_str; 181
133 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) { 182 std::string age_str;
134 int64_t age_ms; 183 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) {
135 if (base::StringToInt64(age_str, &age_ms)) { 184 int64_t age_ms;
136 wap.timestamp = 185 if (base::StringToInt64(age_str, &age_ms)) {
137 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms); 186 wap.timestamp =
187 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms);
188 }
138 } 189 }
190 entry->GetString(shill::kGeoMacAddressProperty, &wap.mac_address);
191
192 std::string strength_str;
193 if (entry->GetString(shill::kGeoSignalStrengthProperty, &strength_str))
194 base::StringToInt(strength_str, &wap.signal_strength);
195
196 std::string signal_str;
197 if (entry->GetString(shill::kGeoSignalToNoiseRatioProperty,
198 &signal_str)) {
199 base::StringToInt(signal_str, &wap.signal_to_noise);
200 }
201
202 std::string channel_str;
203 if (entry->GetString(shill::kGeoChannelProperty, &channel_str))
204 base::StringToInt(channel_str, &wap.channel);
stevenjb 2017/02/03 02:12:02 Looks like the above could be easily moved to a he
can Skylar cook 2017/02/03 21:47:06 Done.
205
206 wifi_access_points_.push_back(wap);
207 access_point_received_time_ = base::Time::Now();
208
209 } else if (device_type == shill::kGeoCellTowersProperty) {
210 CellTower ct;
211
212 std::string age_str;
213 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) {
214 int64_t age_ms;
215 if (base::StringToInt64(age_str, &age_ms)) {
216 ct.timestamp =
217 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms);
218 }
219 }
220 entry->GetString(shill::kGeoCellIdProperty, &ct.ci);
221 entry->GetString(shill::kGeoLocationAreaCodeProperty, &ct.lac);
222 entry->GetString(shill::kGeoMobileCountryCodeProperty, &ct.mcc);
223 entry->GetString(shill::kGeoMobileNetworkCodeProperty, &ct.mnc);
stevenjb 2017/02/03 02:12:02 Helper function?
can Skylar cook 2017/02/03 21:47:07 Done.
224
225 cell_towers_.push_back(ct);
226 cell_towers_received_time_ = base::Time::Now();
139 } 227 }
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 } 228 }
151 } 229 }
152 geolocation_received_time_ = base::Time::Now();
153 } 230 }
154 231
155 } // namespace chromeos 232 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698