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

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

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

Powered by Google App Engine
This is Rietveld 408576698