File: hotspot_controller.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 (148 lines) | stat: -rw-r--r-- 5,995 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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_CONTROLLER_H_
#define CHROMEOS_ASH_COMPONENTS_NETWORK_HOTSPOT_CONTROLLER_H_

#include <memory>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/elapsed_timer.h"
#include "chromeos/ash/components/network/hotspot_capabilities_provider.h"
#include "chromeos/ash/components/network/hotspot_state_handler.h"
#include "chromeos/ash/components/network/technology_state_controller.h"
#include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom-forward.h"

namespace ash {

class HotspotFeatureUsageMetrics;

// Handles enable or disable hotspot.
//
// Enabling the hotspot involves the following operations:
// 1. Check hotspot capabilities
// 2. Check tethering readiness
// 3. Enable tethering from Shill
//
// Enable or disable requests executed as they come in but are ignored if there
// is already a pending request.
class COMPONENT_EXPORT(CHROMEOS_NETWORK) HotspotController
    : public TechnologyStateController::HotspotOperationDelegate,
      public HotspotStateHandler::Observer {
 public:
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;

    virtual void OnHotspotTurnedOn() = 0;

    virtual void OnHotspotTurnedOff(
        hotspot_config::mojom::DisableReason disable_reason) = 0;
  };
  HotspotController();
  HotspotController(const HotspotController&) = delete;
  HotspotController& operator=(const HotspotController&) = delete;
  ~HotspotController() override;

  void Init(HotspotCapabilitiesProvider* hotspot_capabilities_provider,
            HotspotFeatureUsageMetrics* hotspot_feature_usage_metrics,
            HotspotStateHandler* hotspot_state_handler,
            TechnologyStateController* technolog_state_controller);

  // Return callback for the EnableHotspot or DisableHotspot method.
  using HotspotControlCallback = base::OnceCallback<void(
      hotspot_config::mojom::HotspotControlResult control_result)>;

  // Checks if there is an existing request and if there isn't one, proceeds to
  // execute it.
  void EnableHotspot(HotspotControlCallback callback);
  void DisableHotspot(HotspotControlCallback callback,
                      hotspot_config::mojom::DisableReason disable_reason);

  // Set whether Hotspot should be allowed/disallowed by policy.
  void SetPolicyAllowHotspot(bool allow_hotspot);

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

 private:
  friend class HotspotControllerTest;
  friend class HotspotControllerConcurrencyApiTest;
  friend class HotspotEnabledStateNotifierTest;

  // Represents hotspot enable or disable control request parameters. Requests
  // are executed as they come in.
  struct HotspotControlRequest {
    HotspotControlRequest(
        bool enabled,
        std::optional<hotspot_config::mojom::DisableReason> disable_reason,
        HotspotControlCallback callback);
    HotspotControlRequest(const HotspotControlRequest&) = delete;
    HotspotControlRequest& operator=(const HotspotControlRequest&) = delete;
    ~HotspotControlRequest();

    bool enabled;
    bool abort = false;
    // Set for disable requests and will be nullopt for enable requests.
    std::optional<hotspot_config::mojom::DisableReason> disable_reason;
    // Tracks the latency of enable hotspot operation and will be nullopt for
    // disable requests.
    std::optional<base::ElapsedTimer> enable_latency_timer;
    HotspotControlCallback callback;
  };

  // TechnologyStateController::HotspotOperationDelegate:
  void PrepareEnableWifi(
      base::OnceCallback<void(bool prepare_success)> callback) override;

  // HotspotStateHandler::Observer:
  void OnHotspotStatusChanged() override;

  void OnCheckTetheringReadiness(
      HotspotCapabilitiesProvider::CheckTetheringReadinessResult result);
  void PerformSetTetheringEnabled(bool enabled);
  void OnSetTetheringEnabledSuccess(const bool& enabled,
                                    const std::string& result);
  void OnSetTetheringEnabledFailure(const bool& enabled,
                                    const std::string& error_name,
                                    const std::string& error_message);
  void OnPrepareEnableHotspotCompleted(bool prepare_success,
                                       bool wifi_turned_off);
  void OnPrepareEnableWifiCompleted(
      base::OnceCallback<void(bool success)> callback,
      hotspot_config::mojom::HotspotControlResult control_result);
  void CompleteCurrentRequest(
      const bool& enabled,
      hotspot_config::mojom::HotspotControlResult result);
  void CompleteEnableRequest(
      hotspot_config::mojom::HotspotControlResult result);
  void CompleteDisableRequest(
      hotspot_config::mojom::HotspotControlResult result);
  void NotifyHotspotTurnedOn();
  void NotifyHotspotTurnedOff(
      hotspot_config::mojom::DisableReason disable_reason);

  std::unique_ptr<HotspotControlRequest> current_enable_request_;
  std::unique_ptr<HotspotControlRequest> current_disable_request_;

  bool allow_hotspot_ = true;
  // Store whether the WiFi was turned off due to the start of hotspot. Need to
  // restore the WiFi status once hotspot is turned off.
  bool wifi_turned_off_ = false;

  raw_ptr<HotspotCapabilitiesProvider> hotspot_capabilities_provider_ = nullptr;
  raw_ptr<HotspotFeatureUsageMetrics> hotspot_feature_usage_metrics_ = nullptr;
  raw_ptr<HotspotStateHandler> hotspot_state_handler_ = nullptr;
  raw_ptr<TechnologyStateController> technology_state_controller_ = nullptr;

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

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_NETWORK_HOTSPOT_CONTROLLER_H_