File: hotspot_state_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 (114 lines) | stat: -rw-r--r-- 3,787 bytes parent folder | download | duplicates (8)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2022 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_HOTSPOT_STATE_HANDLER_H_
#define CHROMEOS_ASH_COMPONENTS_NETWORK_HOTSPOT_STATE_HANDLER_H_

#include <memory>
#include <optional>
#include <vector>

#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/shill_property_changed_observer.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom.h"

namespace ash {

// This class caches hotspot related status and implements methods to get
// current state and active client count.
class COMPONENT_EXPORT(CHROMEOS_NETWORK) HotspotStateHandler
    : public ShillPropertyChangedObserver {
 public:
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;

    // Invoked when hotspot state, active client count or hotspot config is
    // changed.
    virtual void OnHotspotStatusChanged() = 0;
  };

  HotspotStateHandler();
  HotspotStateHandler(const HotspotStateHandler&) = delete;
  HotspotStateHandler& operator=(const HotspotStateHandler&) = delete;
  ~HotspotStateHandler() override;

  void Init();

  // Return the latest hotspot state
  const hotspot_config::mojom::HotspotState& GetHotspotState() const;

  // Returns the reason for hotspot being disabled. nullopt is returned when the
  // disable reason isn't set
  const std::optional<hotspot_config::mojom::DisableReason> GetDisableReason()
      const;

  // Return the latest hotspot active client count
  size_t GetHotspotActiveClientCount() const;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);
  bool HasObserver(Observer* observer) const;

 private:
  // Stores the count of the active client and enables the system wake lock when
  // the count is not 0.
  class ActiveClientCount {
   public:
    ActiveClientCount();
    ~ActiveClientCount();

    // Setter/getter method for the active client count.
    void Set(size_t value);
    size_t Get() const;

   private:
    // Enables/Disables the system wake lock.
    void EnableWakeLock();
    void DisableWakeLock();

    // The value of the active client count.
    size_t value_ = 0;

    // The wake lock id. It has values if and only if the wake lock is enabled.
    std::optional<int> wake_lock_id_ = std::nullopt;
  };

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

  // Callback to handle the manager properties with hotspot related properties.
  void OnManagerProperties(std::optional<base::Value::Dict> properties);

  // Update the cached hotspot_state_ and active_client_count_ from hotspot
  // status in Shill.
  void UpdateHotspotStatus(const base::Value::Dict& status);

  // Updates the reason for hotspot getting disabled and notifies observers.
  void UpdateDisableReason(const base::Value::Dict& status);

  // Notify observers that hotspot state or active client count was changed.
  void NotifyHotspotStatusChanged();

  hotspot_config::mojom::HotspotState hotspot_state_ =
      hotspot_config::mojom::HotspotState::kDisabled;

  std::optional<hotspot_config::mojom::DisableReason> disable_reason_ =
      std::nullopt;

  ActiveClientCount active_client_count_;

  base::ObserverList<Observer> observer_list_;
  base::WeakPtrFactory<HotspotStateHandler> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_NETWORK_HOTSPOT_STATE_HANDLER_H_