File: kiosk_network_state_observer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (220 lines) | stat: -rw-r--r-- 7,971 bytes parent folder | download | duplicates (6)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/app_mode/kiosk_network_state_observer.h"

#include <optional>
#include <string>
#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/dbus/shill/shill_service_client.h"
#include "chromeos/ash/components/network/network_configuration_handler.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_profile_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_type_pattern.h"
#include "chromeos/ash/components/network/shill_property_util.h"
#include "components/device_event_log/device_event_log.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {
namespace {

// TODO(b/365490226): replace this error with shill constant.
constexpr char kConfigFailureTemporaryServiceConfiguredButNotUsable[] =
    "Config.CreateConfiguration Temporary service configured but not usable";

void CopyPropertyIfExists(std::string_view key,
                          const base::Value::Dict& shill_properties,
                          base::Value::Dict& new_properties) {
  const base::Value* property_value = shill_properties.Find(key);
  if (property_value) {
    new_properties.Set(key, (*property_value).Clone());
  }
}

}  // namespace

KioskNetworkStateObserver::KioskNetworkStateObserver(PrefService* pref_service)
    : pref_service_(pref_service) {
  CHECK(pref_service_);
  pref_change_registrar_.Init(pref_service);
  pref_change_registrar_.Add(
      prefs::kKioskActiveWiFiCredentialsScopeChangeEnabled,
      base::BindRepeating(
          &KioskNetworkStateObserver::PolicyChanged,
          // It is safe to use `base::Unretained` since this class
          // owns `pref_change_registrar_` and  it is destroyed before `this` .
          base::Unretained(this)));

  if (IsPolicyEnabled()) {
    StartActiveWifiExposureProcess();
  }
}

KioskNetworkStateObserver::~KioskNetworkStateObserver() = default;

bool KioskNetworkStateObserver::IsPolicyEnabled() const {
  return pref_service_->GetBoolean(
      prefs::kKioskActiveWiFiCredentialsScopeChangeEnabled);
}

void KioskNetworkStateObserver::SetWifiExposureAttemptCallbackForTesting(
    base::RepeatingCallback<void(bool is_successful_attempt)> callback) {
  wifi_exposure_attempt_callback_ = std::move(callback);
}

void KioskNetworkStateObserver::ActiveNetworksChanged(
    const std::vector<const NetworkState*>& active_networks) {
  ExposeActiveWifiConfiguration();
}

void KioskNetworkStateObserver::StartActiveWifiExposureProcess() {
  if (active_wifi_exposed_) {
    return;
  }
  network_state_handler_observation_.Observe(
      NetworkHandler::Get()->network_state_handler());
  ExposeActiveWifiConfiguration();
}

void KioskNetworkStateObserver::StopActiveWifiExposureProcess() {
  network_state_handler_observation_.Reset();
}

void KioskNetworkStateObserver::ExposeActiveWifiConfiguration() {
  if (active_wifi_exposed_) {
    return;
  }

  const auto* network_state =
      ash::NetworkHandler::Get()->network_state_handler()->ActiveNetworkByType(
          NetworkTypePattern::WiFi());
  if (!network_state) {
    MaybeRunWifiExposureAttemptCallback(false);
    return;
  }

  active_wifi_exposed_ = true;
  ShillServiceClient::Get()->GetWiFiPassphrase(
      dbus::ObjectPath(network_state->path()),
      base::BindOnce(&KioskNetworkStateObserver::OnGetWifiPassphraseResult,
                     weak_ptr_factory_.GetWeakPtr(), network_state->path()),
      base::BindOnce(&KioskNetworkStateObserver::OnGetWifiPassphraseError,
                     weak_ptr_factory_.GetWeakPtr()));
}

void KioskNetworkStateObserver::OnGetWifiPassphraseResult(
    const std::string& service_path,
    const std::string& passphrase) {
  NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
      service_path,
      base::BindOnce(&KioskNetworkStateObserver::ReceiveProperties,
                     weak_ptr_factory_.GetWeakPtr(), passphrase));
}

void KioskNetworkStateObserver::OnGetWifiPassphraseError(
    const std::string& error_name,
    const std::string& error_message) {
  NET_LOG(ERROR) << "Wi-Fi passphrase error:" << error_name << " "
                 << error_message;
  FailCurrentAttempt();
}

void KioskNetworkStateObserver::ReceiveProperties(
    const std::string& passphrase,
    const std::string& service_path,
    std::optional<base::Value::Dict> optional_shill_properties) {
  if (!optional_shill_properties) {
    NET_LOG(ERROR) << "Received shill properties are empty.";
    return;
  }
  const base::Value::Dict& shill_properties = optional_shill_properties.value();
  base::Value::Dict properties;

  properties.Set(shill::kProfileProperty,
                 NetworkProfileHandler::GetSharedProfilePath());
  properties.Set(shill::kPassphraseProperty, passphrase);

  CopyPropertyIfExists(shill::kSecurityClassProperty, shill_properties,
                       properties);
  CopyPropertyIfExists(shill::kWifiHexSsid, shill_properties, properties);
  CopyPropertyIfExists(shill::kGuidProperty, shill_properties, properties);
  CopyPropertyIfExists(shill::kAutoConnectProperty, shill_properties,
                       properties);
  CopyPropertyIfExists(shill::kTypeProperty, shill_properties, properties);
  CopyPropertyIfExists(shill::kSaveCredentialsProperty, shill_properties,
                       properties);
  CopyPropertyIfExists(shill::kModeProperty, shill_properties, properties);
  CopyPropertyIfExists(shill::kONCSourceProperty, shill_properties, properties);
  CopyPropertyIfExists(shill::kWifiHiddenSsid, shill_properties, properties);

  NetworkHandler::Get()
      ->network_configuration_handler()
      ->CreateShillConfiguration(
          std::move(properties),
          base::BindOnce(
              &KioskNetworkStateObserver::OnCreatedShillConfigSuccess,
              weak_ptr_factory_.GetWeakPtr()),
          base::BindOnce(
              &KioskNetworkStateObserver::OnCreatedShillConfigFailure,
              weak_ptr_factory_.GetWeakPtr()));
}

void KioskNetworkStateObserver::OnCreatedShillConfigSuccess(
    const std::string&,
    const std::string&) {
  NET_LOG(DEBUG)
      << "Active WiFi configuration is successfully copied to the device "
         "level settings.";
  // Copy only the first active wifi configuration.
  StopActiveWifiExposureProcess();
  MaybeRunWifiExposureAttemptCallback(true);
}

void KioskNetworkStateObserver::OnCreatedShillConfigFailure(
    const std::string& error) {
  if (error == kConfigFailureTemporaryServiceConfiguredButNotUsable) {
    // The service is moved to the default profile, which means the successful
    // WiFi scope change.
    OnCreatedShillConfigSuccess("", "");
    return;
  }

  NET_LOG(ERROR) << "Shill config failure: " << error;
  FailCurrentAttempt();
}

void KioskNetworkStateObserver::PolicyChanged() {
  if (IsPolicyEnabled()) {
    StartActiveWifiExposureProcess();
  } else {
    StopActiveWifiExposureProcess();
  }
}

void KioskNetworkStateObserver::FailCurrentAttempt() {
  // We set `active_wifi_exposed_` to true when we attempt to expose the active
  // WiFi. If the attempt fails, we need to reset it.
  active_wifi_exposed_ = false;

  if (++wifi_exposure_attempts_ >= kMaxWifiExposureAttempts) {
    StopActiveWifiExposureProcess();
  }
  MaybeRunWifiExposureAttemptCallback(false);
}

void KioskNetworkStateObserver::MaybeRunWifiExposureAttemptCallback(
    bool is_successful_attempt) {
  if (wifi_exposure_attempt_callback_) {
    wifi_exposure_attempt_callback_.Run(is_successful_attempt);
  }
}

}  // namespace ash