File: geolocation_handler.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 3,911 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_NETWORK_GEOLOCATION_HANDLER_H_
#define CHROMEOS_ASH_COMPONENTS_NETWORK_GEOLOCATION_HANDLER_H_

#include <stdint.h>

#include <optional>

#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/shill_property_changed_observer.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_util.h"

namespace ash {

// This class provides Shill Wifi Access Point and Cell Tower data. It
// currently relies on polling because that is the usage model in
// content::WifiDataProvider. This class requests data asynchronously,
// returning the most recent available data. A typical usage pattern,
// assuming a wifi device is enabled, is:
//   Initialize();  // Makes an initial request
//   GetWifiAccessPoints();  // returns true + inital data, requests update
//   (Delay some amount of time, ~10s)
//   GetWifiAccessPoints();  // returns true + updated data, requests update
//   (Delay some amount of time after data changed, ~10s)
//   GetWifiAccessPoints();  // returns true + same data, requests update
//   (Delay some amount of time after data did not change, ~2 mins)

class COMPONENT_EXPORT(CHROMEOS_NETWORK) GeolocationHandler
    : public ShillPropertyChangedObserver {
 public:
  GeolocationHandler(const GeolocationHandler&) = delete;
  GeolocationHandler& operator=(const GeolocationHandler&) = delete;

  ~GeolocationHandler() override;

  // This sends a request for geolocation (both wifi AP and cell tower) data.
  // If AP data is already available, fills |access_points| with the latest
  // access point data, and similarly for cell tower data and |cell_towers|.
  // Returns |true| if either type of data is already available upon call.
  bool GetNetworkInformation(WifiAccessPointVector* access_points,
                             CellTowerVector* cell_towers);

  // This sends a request for geolocation (both wifi AP and cell tower) data.
  // If wifi data is already available, returns |true|, fills |access_points|
  // with the latest access point data, and sets |age_ms| to the time
  // since the last update in MS.
  bool GetWifiAccessPoints(WifiAccessPointVector* access_points,
                           int64_t* age_ms);

  bool wifi_enabled() const { return wifi_enabled_; }

  // ShillPropertyChangedObserver overrides
  void OnPropertyChanged(const std::string& key,
                         const base::Value& value) override;

 private:
  friend class NetworkHandler;
  friend class GeolocationHandlerTest;
  friend class SimpleGeolocationWirelessTest;

  GeolocationHandler();

  void Init();

  // ShillManagerClient callback
  void ManagerPropertiesCallback(std::optional<base::Value::Dict> properties);

  // Called from OnPropertyChanged or ManagerPropertiesCallback.
  void HandlePropertyChanged(const std::string& key, const base::Value& value);

  // Asynchronously request geolocation objects (wifi access points and
  // cell towers) from Shill.Manager.
  void RequestGeolocationObjects();

  // Callback for receiving Geolocation data.
  void GeolocationCallback(std::optional<base::Value::Dict> properties);

  bool cellular_enabled_ = false;
  bool wifi_enabled_ = false;

  void AddCellTowerFromDict(const base::Value::Dict& entry);
  void AddAccessPointFromDict(const base::Value::Dict& entry);

  // Cached network information and update time
  WifiAccessPointVector wifi_access_points_;
  CellTowerVector cell_towers_;
  base::Time geolocation_received_time_;

  // For Shill client callbacks
  base::WeakPtrFactory<GeolocationHandler> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_NETWORK_GEOLOCATION_HANDLER_H_