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

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

Issue 11887008: Deprecate ShillNetworkClient and add GeolocationHandler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unittest Created 7 years, 11 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chromeos/network/geolocation_handler.h"
6
7 #include "base/bind.h"
8 #include "base/string_number_conversions.h"
9 #include "base/values.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_manager_client.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
13
14 namespace chromeos {
15
16 static GeolocationHandler* g_geolocation_handler = NULL;
17
18 GeolocationHandler::GeolocationHandler()
19 : wifi_enabled_(false),
20 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
21 }
22
23 GeolocationHandler::~GeolocationHandler() {
24 ShillManagerClient* manager_client =
25 DBusThreadManager::Get()->GetShillManagerClient();
26 if (manager_client)
27 manager_client->RemovePropertyChangedObserver(this);
28 }
29
30 void GeolocationHandler::Init() {
31 ShillManagerClient* manager_client =
32 DBusThreadManager::Get()->GetShillManagerClient();
33 manager_client->GetProperties(
34 base::Bind(&GeolocationHandler::ManagerPropertiesCallback,
35 weak_ptr_factory_.GetWeakPtr()));
36 manager_client->AddPropertyChangedObserver(this);
37 }
38
39 // static
40 void GeolocationHandler::Initialize() {
41 CHECK(!g_geolocation_handler);
42 g_geolocation_handler = new GeolocationHandler();
43 g_geolocation_handler->Init();
44 }
45
46 // static
47 void GeolocationHandler::Shutdown() {
48 CHECK(g_geolocation_handler);
49 delete g_geolocation_handler;
50 g_geolocation_handler = NULL;
51 }
52
53 // static
54 GeolocationHandler* GeolocationHandler::Get() {
55 CHECK(g_geolocation_handler)
56 << "GeolocationHandler::Get() called before Initialize()";
57 return g_geolocation_handler;
58 }
59
60 bool GeolocationHandler::GetWifiAccessPoints(
61 WifiAccessPointVector* access_points, int64* age_ms) {
62 if (!wifi_enabled_)
63 return false;
64 // Always request updated access points.
65 RequestWifiAccessPoints();
66 // If no data has been received, return false.
67 if (geolocation_received_time_.is_null())
68 return false;
69 if (access_points)
70 *access_points = wifi_access_points_;
71 if (age_ms) {
72 base::TimeDelta dtime = base::Time::Now() - geolocation_received_time_;
73 *age_ms = dtime.InMilliseconds();
74 }
75 return true;
76 }
77
78 void GeolocationHandler::OnPropertyChanged(const std::string& key,
79 const base::Value& value) {
80 HandlePropertyChanged(key, value);
81 }
82
83 //------------------------------------------------------------------------------
84 // Private methods
85
86 void GeolocationHandler::ManagerPropertiesCallback(
87 DBusMethodCallStatus call_status,
88 const base::DictionaryValue& properties) {
89 const base::Value* value = NULL;
90 if (properties.Get(flimflam::kEnabledTechnologiesProperty, &value) && value)
91 HandlePropertyChanged(flimflam::kEnabledTechnologiesProperty, *value);
92 }
93
94 void GeolocationHandler::HandlePropertyChanged(const std::string& key,
95 const base::Value& value) {
96 if (key != flimflam::kEnabledTechnologiesProperty)
97 return;
98 const base::ListValue* technologies = NULL;
99 if (!value.GetAsList(&technologies) || !technologies)
100 return;
101 bool wifi_was_enabled = wifi_enabled_;
102 wifi_enabled_ = false;
103 for (base::ListValue::const_iterator iter = technologies->begin();
104 iter != technologies->end(); ++iter) {
105 std::string technology;
106 (*iter)->GetAsString(&technology);
107 if (technology == flimflam::kTypeWifi) {
108 wifi_enabled_ = true;
109 break;
110 }
111 }
112 if (!wifi_was_enabled && wifi_enabled_)
113 RequestWifiAccessPoints(); // Request initial location data.
114 }
115
116 void GeolocationHandler::RequestWifiAccessPoints() {
117 DBusThreadManager::Get()->GetShillManagerClient()->GetNetworksForGeolocation(
118 base::Bind(&GeolocationHandler::GeolocationCallback,
119 weak_ptr_factory_.GetWeakPtr()));
120 }
121
122 void GeolocationHandler::GeolocationCallback(
123 DBusMethodCallStatus call_status,
124 const base::DictionaryValue& properties) {
125 if (call_status != DBUS_METHOD_CALL_SUCCESS) {
126 LOG(ERROR) << "Failed to get Geolocation data: " << call_status;
127 return;
128 }
129 wifi_access_points_.clear();
130 if (properties.empty())
131 return; // No enabled devices, don't update received time.
132
133 // Dictionary<device_type, entry_list>
134 for (base::DictionaryValue::Iterator iter(properties);
135 iter.HasNext(); iter.Advance()) {
136 const base::ListValue* entry_list = NULL;
137 if (!iter.value().GetAsList(&entry_list)) {
138 LOG(WARNING) << "Geolocation dictionary value not a List: " << iter.key();
139 continue;
140 }
141 // List[Dictionary<key, value_str>]
142 for (size_t i = 0; i < entry_list->GetSize(); ++i) {
143 const base::DictionaryValue* entry = NULL;
144 if (!entry_list->GetDictionary(i, &entry) || !entry) {
145 LOG(WARNING) << "Geolocation list value not a Dictionary: " << i;
146 continue;
147 }
148 // Docs: developers.google.com/maps/documentation/business/geolocation
149 WifiAccessPoint wap;
150 entry->GetString(shill::kGeoMacAddressProperty, &wap.mac_address);
151 std::string age_str;
152 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) {
153 int64 age_ms;
154 if (base::StringToInt64(age_str, &age_ms)) {
155 wap.timestamp =
156 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms);
157 }
158 }
159 std::string strength_str;
160 if (entry->GetString(shill::kGeoSignalStrengthProperty, &strength_str))
161 base::StringToInt(strength_str, &wap.signal_strength);
162 std::string signal_str;
163 if (entry->GetString(shill::kGeoSignalToNoiseRatioProperty, &signal_str))
164 base::StringToInt(signal_str, &wap.signal_to_noise);
165 std::string channel_str;
166 if (entry->GetString(shill::kGeoChannelProperty, &channel_str))
167 base::StringToInt(channel_str, &wap.channel);
168 wifi_access_points_.push_back(wap);
169 }
170 }
171 geolocation_received_time_ = base::Time::Now();
172 }
173
174 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698