| OLD | NEW |
| 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 #ifndef CHROMEOS_NETWORK_GEOLOCATION_HANDLER_H_ | 5 #ifndef CHROMEOS_NETWORK_GEOLOCATION_HANDLER_H_ |
| 6 #define CHROMEOS_NETWORK_GEOLOCATION_HANDLER_H_ | 6 #define CHROMEOS_NETWORK_GEOLOCATION_HANDLER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chromeos/dbus/dbus_method_call_status.h" | 13 #include "chromeos/dbus/dbus_method_call_status.h" |
| 14 #include "chromeos/dbus/shill_property_changed_observer.h" | 14 #include "chromeos/dbus/shill_property_changed_observer.h" |
| 15 #include "chromeos/network/network_handler.h" | 15 #include "chromeos/network/network_handler.h" |
| 16 #include "chromeos/network/network_util.h" | 16 #include "chromeos/network/network_util.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class DictionaryValue; | 19 class DictionaryValue; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace chromeos { | 22 namespace chromeos { |
| 23 | 23 |
| 24 // This class provices Shill Wifi Access Point data. It currently relies on | 24 // This class provices Shill Wifi Access Point and Cell Tower data. It |
| 25 // polling because that is the usage model in content::WifiDataProvider. This | 25 // currently relies on polling because that is the usage model in |
| 26 // class requests data asynchronously, returning the most recent available data. | 26 // content::WifiDataProvider. This class requests data asynchronously, |
| 27 // A typical usage pattern, assuming a wifi device is enabled, is: | 27 // returning the most recent available data. A typical usage pattern, |
| 28 // assuming a wifi device is enabled, is: |
| 28 // Initialize(); // Makes an initial request | 29 // Initialize(); // Makes an initial request |
| 29 // GetWifiAccessPoints(); // returns true + inital data, requests update | 30 // GetWifiAccessPoints(); // returns true + inital data, requests update |
| 30 // (Delay some amount of time, ~10s) | 31 // (Delay some amount of time, ~10s) |
| 31 // GetWifiAccessPoints(); // returns true + updated data, requests update | 32 // GetWifiAccessPoints(); // returns true + updated data, requests update |
| 32 // (Delay some amount of time after data changed, ~10s) | 33 // (Delay some amount of time after data changed, ~10s) |
| 33 // GetWifiAccessPoints(); // returns true + same data, requests update | 34 // GetWifiAccessPoints(); // returns true + same data, requests update |
| 34 // (Delay some amount of time after data did not change, ~2 mins) | 35 // (Delay some amount of time after data did not change, ~2 mins) |
| 35 | 36 |
| 36 class CHROMEOS_EXPORT GeolocationHandler : public ShillPropertyChangedObserver { | 37 class CHROMEOS_EXPORT GeolocationHandler : public ShillPropertyChangedObserver { |
| 37 public: | 38 public: |
| 38 ~GeolocationHandler() override; | 39 ~GeolocationHandler() override; |
| 39 | 40 |
| 40 // This sends a request for wifi access point data. If data is already | 41 // This sends a request for geolocation (both wifi AP and cell tower) data. |
| 41 // available, returns |true|, fills |access_points| with the latest access | 42 // If AP data is already available, fills |access_points| with the latest |
| 42 // point data, and sets |age_ms| to the time since the last update in MS. | 43 // access point data, and similarly for cell tower data and |cell_towers|. |
| 44 // Returns |true| if either type of data is already available upon call. |
| 45 bool GetNetworkInformation(WifiAccessPointVector* access_points, |
| 46 CellTowerVector* cell_towers); |
| 47 |
| 48 // This sends a request for geolocation (both wifi AP and cell tower) data. |
| 49 // If wifi data is already available, returns |true|, fills |access_points| |
| 50 // with the latest access point data, and sets |age_ms| to the time |
| 51 // since the last update in MS. |
| 43 bool GetWifiAccessPoints(WifiAccessPointVector* access_points, | 52 bool GetWifiAccessPoints(WifiAccessPointVector* access_points, |
| 44 int64_t* age_ms); | 53 int64_t* age_ms); |
| 45 | 54 |
| 46 bool wifi_enabled() const { return wifi_enabled_; } | 55 bool wifi_enabled() const { return wifi_enabled_; } |
| 47 | 56 |
| 48 // ShillPropertyChangedObserver overrides | 57 // ShillPropertyChangedObserver overrides |
| 49 void OnPropertyChanged(const std::string& key, | 58 void OnPropertyChanged(const std::string& key, |
| 50 const base::Value& value) override; | 59 const base::Value& value) override; |
| 51 | 60 |
| 52 private: | 61 private: |
| 53 friend class NetworkHandler; | 62 friend class NetworkHandler; |
| 54 friend class GeolocationHandlerTest; | 63 friend class GeolocationHandlerTest; |
| 55 friend class SimpleGeolocationWiFiTest; | 64 friend class SimpleGeolocationWirelessTest; |
| 56 | 65 |
| 57 GeolocationHandler(); | 66 GeolocationHandler(); |
| 58 | 67 |
| 59 void Init(); | 68 void Init(); |
| 60 | 69 |
| 61 // ShillManagerClient callback | 70 // ShillManagerClient callback |
| 62 void ManagerPropertiesCallback(DBusMethodCallStatus call_status, | 71 void ManagerPropertiesCallback(DBusMethodCallStatus call_status, |
| 63 const base::DictionaryValue& properties); | 72 const base::DictionaryValue& properties); |
| 64 | 73 |
| 65 // Called from OnPropertyChanged or ManagerPropertiesCallback. | 74 // Called from OnPropertyChanged or ManagerPropertiesCallback. |
| 66 void HandlePropertyChanged(const std::string& key, const base::Value& value); | 75 void HandlePropertyChanged(const std::string& key, const base::Value& value); |
| 67 | 76 |
| 68 // Asynchronously request wifi access points from Shill.Manager. | 77 // Asynchronously request geolocation objects (wifi access points and |
| 69 void RequestWifiAccessPoints(); | 78 // cell towers) from Shill.Manager. |
| 79 void RequestGeolocationObjects(); |
| 70 | 80 |
| 71 // Callback for receiving Geolocation data. | 81 // Callback for receiving Geolocation data. |
| 72 void GeolocationCallback(DBusMethodCallStatus call_status, | 82 void GeolocationCallback(DBusMethodCallStatus call_status, |
| 73 const base::DictionaryValue& properties); | 83 const base::DictionaryValue& properties); |
| 74 | 84 |
| 75 // Wifi enabled state | 85 bool cellular_enabled_; |
| 76 bool wifi_enabled_; | 86 bool wifi_enabled_; |
| 77 | 87 |
| 78 // Cached wifi access points and update time | 88 void AddCellTowerFromDict(const base::DictionaryValue* entry); |
| 89 void AddAccessPointFromDict(const base::DictionaryValue* entry); |
| 90 |
| 91 // Cached netork information and update time |
| 79 WifiAccessPointVector wifi_access_points_; | 92 WifiAccessPointVector wifi_access_points_; |
| 93 CellTowerVector cell_towers_; |
| 80 base::Time geolocation_received_time_; | 94 base::Time geolocation_received_time_; |
| 81 | 95 |
| 82 // For Shill client callbacks | 96 // For Shill client callbacks |
| 83 base::WeakPtrFactory<GeolocationHandler> weak_ptr_factory_; | 97 base::WeakPtrFactory<GeolocationHandler> weak_ptr_factory_; |
| 84 | 98 |
| 85 DISALLOW_COPY_AND_ASSIGN(GeolocationHandler); | 99 DISALLOW_COPY_AND_ASSIGN(GeolocationHandler); |
| 86 }; | 100 }; |
| 87 | 101 |
| 88 } // namespace chromeos | 102 } // namespace chromeos |
| 89 | 103 |
| 90 #endif // CHROMEOS_NETWORK_GEOLOCATION_HANDLER_H_ | 104 #endif // CHROMEOS_NETWORK_GEOLOCATION_HANDLER_H_ |
| OLD | NEW |