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