File: hotspot_configuration_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 (179 lines) | stat: -rw-r--r-- 6,705 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
// Copyright 2023 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_configuration_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 "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {

HotspotConfigurationHandler::HotspotConfigurationHandler() = default;

HotspotConfigurationHandler::~HotspotConfigurationHandler() {
  if (ShillManagerClient::Get()) {
    ShillManagerClient::Get()->RemovePropertyChangedObserver(this);
  }
  if (LoginState::IsInitialized()) {
    LoginState::Get()->RemoveObserver(this);
  }
}

void HotspotConfigurationHandler::Init() {
  if (LoginState::IsInitialized()) {
    LoginState::Get()->AddObserver(this);
  }

  // Add as an observer here because shill will signal "Profiles" property
  // change when tethering config is fully loaded from persistent storage.
  ShillManagerClient::Get()->AddPropertyChangedObserver(this);
  if (LoginState::IsInitialized()) {
    LoggedInStateChanged();
  }
}

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

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

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

hotspot_config::mojom::HotspotConfigPtr
HotspotConfigurationHandler::GetHotspotConfig() const {
  if (!hotspot_config_) {
    return nullptr;
  }

  return ShillTetheringConfigToMojomConfig(*hotspot_config_);
}

void HotspotConfigurationHandler::SetHotspotConfig(
    hotspot_config::mojom::HotspotConfigPtr mojom_config,
    SetHotspotConfigCallback callback) {
  using SetHotspotConfigResult = hotspot_config::mojom::SetHotspotConfigResult;

  if (!LoginState::Get()->IsUserLoggedIn()) {
    NET_LOG(ERROR) << "Could not set hotspot config without login first.";
    HotspotMetricsHelper::RecordSetHotspotConfigResult(
        SetHotspotConfigResult::kFailedNotLogin);
    std::move(callback).Run(SetHotspotConfigResult::kFailedNotLogin);
    return;
  }

  if (!mojom_config) {
    NET_LOG(ERROR) << "Invalid hotspot configurations.";
    HotspotMetricsHelper::RecordSetHotspotConfigResult(
        SetHotspotConfigResult::kFailedInvalidConfiguration);
    std::move(callback).Run(
        SetHotspotConfigResult::kFailedInvalidConfiguration);
    return;
  }

  base::Value::Dict shill_tethering_config =
      MojomConfigToShillConfig(std::move(mojom_config));
  auto callback_split = base::SplitOnceCallback(std::move(callback));
  ShillManagerClient::Get()->SetProperty(
      shill::kTetheringConfigProperty,
      base::Value(std::move(shill_tethering_config)),
      base::BindOnce(&HotspotConfigurationHandler::OnSetHotspotConfigSuccess,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(callback_split.first)),
      base::BindOnce(&HotspotConfigurationHandler::OnSetHotspotConfigFailure,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(callback_split.second)));
}

void HotspotConfigurationHandler::OnSetHotspotConfigSuccess(
    SetHotspotConfigCallback callback) {
  HotspotMetricsHelper::RecordSetHotspotConfigResult(
      hotspot_config::mojom::SetHotspotConfigResult::kSuccess);
  ShillManagerClient::Get()->GetProperties(base::BindOnce(
      &HotspotConfigurationHandler::UpdateHotspotConfigAndRunCallback,
      weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void HotspotConfigurationHandler::OnSetHotspotConfigFailure(
    SetHotspotConfigCallback callback,
    const std::string& error_name,
    const std::string& error_message) {
  using SetHotspotConfigResult = hotspot_config::mojom::SetHotspotConfigResult;

  NET_LOG(ERROR) << "Error setting hotspot config, error name:" << error_name
                 << ", message" << error_message;

  HotspotMetricsHelper::RecordSetHotspotConfigResult(
      SetHotspotConfigResult::kFailedShillOperation, error_name);
  std::move(callback).Run(
      error_name == shill::kErrorResultInvalidArguments
          ? SetHotspotConfigResult::kFailedInvalidConfiguration
          : SetHotspotConfigResult::kFailedShillOperation);
}

void HotspotConfigurationHandler::LoggedInStateChanged() {
  if (!LoginState::Get()->IsUserLoggedIn()) {
    if (hotspot_config_) {
      hotspot_config_ = std::nullopt;
      NotifyHotspotConfigurationChanged();
    }
    return;
  }
  ShillManagerClient::Get()->GetProperties(base::BindOnce(
      &HotspotConfigurationHandler::UpdateHotspotConfigAndRunCallback,
      weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));
}

void HotspotConfigurationHandler::UpdateHotspotConfigAndRunCallback(
    SetHotspotConfigCallback callback,
    std::optional<base::Value::Dict> properties) {
  if (!properties) {
    NET_LOG(ERROR) << "Error getting Shill manager properties.";
    std::move(callback).Run(
        hotspot_config::mojom::SetHotspotConfigResult::kSuccess);
    return;
  }
  const base::Value::Dict* shill_tethering_config =
      properties->FindDict(shill::kTetheringConfigProperty);
  if (!shill_tethering_config) {
    NET_LOG(ERROR) << "Error getting " << shill::kTetheringConfigProperty
                   << " in Shill manager properties";
    std::move(callback).Run(
        hotspot_config::mojom::SetHotspotConfigResult::kSuccess);
    return;
  }

  hotspot_config_ = shill_tethering_config->Clone();
  std::move(callback).Run(
      hotspot_config::mojom::SetHotspotConfigResult::kSuccess);
  NotifyHotspotConfigurationChanged();
}

void HotspotConfigurationHandler::OnPropertyChanged(const std::string& key,
                                                    const base::Value& value) {
  if (key == shill::kProfilesProperty) {
    // Shill initializes the tethering config with random value and signals
    // "Profiles" property changes when the tethering config is fully loaded
    // from persistent storage.
    ShillManagerClient::Get()->GetProperties(base::BindOnce(
        &HotspotConfigurationHandler::UpdateHotspotConfigAndRunCallback,
        weak_ptr_factory_.GetWeakPtr(), base::DoNothing()));
  }
}

void HotspotConfigurationHandler::NotifyHotspotConfigurationChanged() {
  for (auto& observer : observer_list_) {
    observer.OnHotspotConfigurationChanged();
  }
}

}  // namespace ash