File: hotspot_state_handler.cc

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 (191 lines) | stat: -rw-r--r-- 5,903 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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.

#include "chromeos/ash/components/network/hotspot_state_handler.h"

#include "base/containers/contains.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/network/hotspot_util.h"
#include "chromeos/ash/components/network/metrics/hotspot_metrics_helper.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/dbus/power/power_policy_controller.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {

namespace {

size_t GetActiveClientCount(const base::Value::Dict& status) {
  const base::Value::List* active_clients =
      status.FindList(shill::kTetheringStatusClientsProperty);
  if (!active_clients) {
    NET_LOG(ERROR) << shill::kTetheringStatusClientsProperty << " not found in "
                   << shill::kTetheringStatusProperty;
    return 0;
  }
  return active_clients->size();
}

}  // namespace

HotspotStateHandler::ActiveClientCount::ActiveClientCount() = default;

HotspotStateHandler::ActiveClientCount::~ActiveClientCount() {
  DisableWakeLock();
}

void HotspotStateHandler::ActiveClientCount::Set(size_t value) {
  value_ = value;
  if (value_ > 0) {
    EnableWakeLock();
  } else {
    DisableWakeLock();
  }
}

size_t HotspotStateHandler::ActiveClientCount::Get() const {
  return value_;
}

void HotspotStateHandler::ActiveClientCount::EnableWakeLock() {
  if (!wake_lock_id_.has_value()) {
    NET_LOG(EVENT) << "Enable wake lock";
    wake_lock_id_ = chromeos::PowerPolicyController::Get()->AddSystemWakeLock(
        chromeos::PowerPolicyController::WakeLockReason::REASON_OTHER,
        "Clients connected to hotspot");
  }
}

void HotspotStateHandler::ActiveClientCount::DisableWakeLock() {
  if (wake_lock_id_.has_value()) {
    NET_LOG(EVENT) << "Disable wake lock";
    chromeos::PowerPolicyController::Get()->RemoveWakeLock(*wake_lock_id_);
    wake_lock_id_.reset();
  }
}

HotspotStateHandler::HotspotStateHandler() = default;

HotspotStateHandler::~HotspotStateHandler() {
  if (ShillManagerClient::Get()) {
    ShillManagerClient::Get()->RemovePropertyChangedObserver(this);
  }
}

void HotspotStateHandler::Init() {
  // Add as an observer here so that new hotspot state updated after this call
  // are recognized.
  ShillManagerClient::Get()->AddPropertyChangedObserver(this);
  ShillManagerClient::Get()->GetProperties(
      base::BindOnce(&HotspotStateHandler::OnManagerProperties,
                     weak_ptr_factory_.GetWeakPtr()));
}

void HotspotStateHandler::AddObserver(Observer* observer) {
  observer_list_.AddObserver(observer);
}

void HotspotStateHandler::RemoveObserver(Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

bool HotspotStateHandler::HasObserver(Observer* observer) const {
  return observer_list_.HasObserver(observer);
}

const hotspot_config::mojom::HotspotState&
HotspotStateHandler::GetHotspotState() const {
  return hotspot_state_;
}

const std::optional<hotspot_config::mojom::DisableReason>
HotspotStateHandler::GetDisableReason() const {
  return disable_reason_;
}

size_t HotspotStateHandler::GetHotspotActiveClientCount() const {
  return active_client_count_.Get();
}

void HotspotStateHandler::OnPropertyChanged(const std::string& key,
                                            const base::Value& value) {
  if (key == shill::kTetheringStatusProperty) {
    UpdateHotspotStatus(value.GetDict());
  }
}

void HotspotStateHandler::OnManagerProperties(
    std::optional<base::Value::Dict> properties) {
  if (!properties) {
    NET_LOG(ERROR) << "HotspotStateHandler: Failed to get manager properties.";
    return;
  }

  const base::Value::Dict* status =
      properties->FindDict(shill::kTetheringStatusProperty);
  if (!status) {
    NET_LOG(EVENT) << "HotspotStateHandler: No dict value for: "
                   << shill::kTetheringStatusProperty;
  } else {
    UpdateHotspotStatus(*status);
  }
}

void HotspotStateHandler::UpdateHotspotStatus(const base::Value::Dict& status) {
  const std::string* state =
      status.FindString(shill::kTetheringStatusStateProperty);
  if (!state) {
    NET_LOG(EVENT) << "HotspotStateHandler: No string value for: "
                   << shill::kTetheringStatusStateProperty << " in "
                   << shill::kTetheringStatusProperty;
    return;
  }

  hotspot_config::mojom::HotspotState mojom_state =
      ShillTetheringStateToMojomState(*state);
  if (mojom_state != hotspot_state_) {
    hotspot_state_ = mojom_state;
    UpdateDisableReason(status);
    NotifyHotspotStatusChanged();
  }

  if (mojom_state != hotspot_config::mojom::HotspotState::kEnabled) {
    active_client_count_.Set(0);
    return;
  }
  size_t active_client_count = GetActiveClientCount(status);
  if (active_client_count == active_client_count_.Get()) {
    return;
  }

  active_client_count_.Set(active_client_count);

  NotifyHotspotStatusChanged();
}

void HotspotStateHandler::UpdateDisableReason(const base::Value::Dict& status) {
  const std::string* idle_reason =
      status.FindString(shill::kTetheringStatusIdleReasonProperty);
  if (!idle_reason) {
    disable_reason_ = std::nullopt;
    NET_LOG(EVENT) << "HotspotStateHandler: No string value for: "
                   << shill::kTetheringStatusIdleReasonProperty << " in "
                   << shill::kTetheringStatusProperty;
    return;
  }

  if (*idle_reason != shill::kTetheringIdleReasonInitialState) {
    hotspot_config::mojom::DisableReason disable_reason =
        ShillTetheringIdleReasonToMojomState(*idle_reason);
    disable_reason_ = disable_reason;
  }
}

void HotspotStateHandler::NotifyHotspotStatusChanged() {
  for (auto& observer : observer_list_) {
    observer.OnHotspotStatusChanged();
  }
}

}  // namespace ash