Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
Greg Spencer (Chromium)
2013/01/12 00:52:37
2013
stevenjb
2013/01/12 01:55:59
Done.
| |
| 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 // Time until geolocation data becomes invalid. This is intentionally large; | |
| 15 // the geolocation code itself should do periodic polling for more accuracy. | |
| 16 const int64 kGeolocationValidSeconds = 10 * 60; | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 static GeolocationHandler* g_geolocation_handler = NULL; | |
| 21 | |
| 22 GeolocationHandler::GeolocationHandler() | |
| 23 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 24 } | |
| 25 | |
| 26 GeolocationHandler::~GeolocationHandler() { | |
| 27 } | |
| 28 | |
| 29 // static | |
| 30 void GeolocationHandler::Initialize() { | |
| 31 CHECK(!g_geolocation_handler); | |
| 32 g_geolocation_handler = new GeolocationHandler(); | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 void GeolocationHandler::Shutdown() { | |
| 37 CHECK(g_geolocation_handler); | |
| 38 delete g_geolocation_handler; | |
| 39 g_geolocation_handler = NULL; | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 GeolocationHandler* GeolocationHandler::Get() { | |
| 44 CHECK(g_geolocation_handler) | |
| 45 << "GeolocationHandler::Get() called before Initialize()"; | |
| 46 return g_geolocation_handler; | |
| 47 } | |
| 48 | |
| 49 bool GeolocationHandler::GetWifiAccessPoints( | |
| 50 WifiAccessPointVector* access_points) { | |
| 51 // Always request updated access points. | |
| 52 DBusThreadManager::Get()->GetShillManagerClient()->GetNetworksForGeolocation( | |
| 53 base::Bind(&GeolocationHandler::GeolocationCallback, | |
| 54 weak_ptr_factory_.GetWeakPtr())); | |
| 55 // Return latest access point data if valid. | |
| 56 base::TimeDelta dtime = base::Time::Now() - geolocation_received_time_; | |
| 57 if (dtime.InSeconds() < kGeolocationValidSeconds) { | |
| 58 *access_points = wifi_access_points_; | |
| 59 return true; | |
| 60 } | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 void GeolocationHandler::GeolocationCallback( | |
| 65 DBusMethodCallStatus call_status, | |
| 66 const base::DictionaryValue& properties) { | |
| 67 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | |
| 68 LOG(ERROR) << "Failed to get Geolocation data: " << call_status; | |
| 69 return; | |
| 70 } | |
| 71 wifi_access_points_.clear(); | |
| 72 if (properties.empty()) | |
| 73 return; // No enabled devices, don't update received time. | |
| 74 | |
| 75 // Dictionary<device_type, entry_list> | |
| 76 for (base::DictionaryValue::Iterator iter(properties); | |
| 77 iter.HasNext(); iter.Advance()) { | |
| 78 const base::ListValue* entry_list = NULL; | |
| 79 if (!iter.value().GetAsList(&entry_list)) { | |
| 80 LOG(WARNING) << "Geolocation dictionary value not a List: " << iter.key(); | |
|
Greg Spencer (Chromium)
2013/01/12 00:52:37
Shouldn't this be a DCHECK and log an error? Afte
| |
| 81 continue; | |
| 82 } | |
| 83 // List[Dictionary<key, value_str>] | |
| 84 for (size_t i = 0; i< entry_list->GetSize(); ++i) { | |
| 85 const base::DictionaryValue* entry = NULL; | |
| 86 if (!entry_list->GetDictionary(i, &entry) || !entry) { | |
| 87 LOG(WARNING) << "Geolocation list value not a Dictionary: " << i; | |
| 88 continue; | |
| 89 } | |
| 90 // Docs: developers.google.com/maps/documentation/business/geolocation | |
| 91 WifiAccessPoint wap; | |
| 92 entry->GetString(shill::kGeoMacAddressProperty, &wap.mac_address); | |
| 93 std::string age_str; | |
| 94 if (entry->GetString(shill::kGeoAgeProperty, &age_str)) { | |
| 95 int64 age_ms; | |
| 96 if (base::StringToInt64(age_str, &age_ms)) { | |
| 97 wap.timestamp = | |
| 98 base::Time::Now() - base::TimeDelta::FromMilliseconds(age_ms); | |
| 99 } | |
| 100 } | |
| 101 std::string strength_str; | |
| 102 if (entry->GetString(shill::kGeoSignalStrengthProperty, &strength_str)) | |
| 103 base::StringToInt(strength_str, &wap.signal_strength); | |
| 104 std::string signal_str; | |
| 105 if (entry->GetString(shill::kGeoSignalToNoiseRatioProperty, &signal_str)) | |
| 106 base::StringToInt(signal_str, &wap.signal_to_noise); | |
| 107 std::string channel_str; | |
| 108 if (entry->GetString(shill::kGeoChannelProperty, &channel_str)) | |
| 109 base::StringToInt(channel_str, &wap.channel); | |
| 110 wifi_access_points_.push_back(wap); | |
| 111 } | |
| 112 } | |
| 113 geolocation_received_time_ = base::Time::Now(); | |
| 114 } | |
| 115 | |
| 116 } // namespace chromeos | |
| OLD | NEW |