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