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

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

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

Powered by Google App Engine
This is Rietveld 408576698