File: network_profile_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 (268 lines) | stat: -rw-r--r-- 8,987 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2013 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/network_profile_handler.h"

#include <stddef.h>

#include <algorithm>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/dbus/shill/shill_profile_client.h"
#include "chromeos/ash/components/network/network_profile_observer.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

namespace {

bool ConvertListValueToStringVector(const base::Value::List& string_list,
                                    std::vector<std::string>* result) {
  for (const base::Value& i : string_list) {
    const std::string* str = i.GetIfString();
    if (!str)
      return false;
    result->push_back(*str);
  }
  return true;
}

void LogProfileRequestError(const std::string& profile_path,
                            const std::string& error_name,
                            const std::string& error_message) {
  LOG(ERROR) << "Error when requesting properties for profile "
             << profile_path << ": " << error_message;
}

void LogError(const std::string& name,
              const std::string& profile_path,
              const std::string& dbus_error_name,
              const std::string& dbus_error_message) {
  LOG(ERROR) << name << " failed:"
             << " profile=" << profile_path
             << " dbus-error-name=" << dbus_error_name
             << " dbus-error-msg=" << dbus_error_message;
}

}  // namespace

// static
std::string NetworkProfileHandler::GetSharedProfilePath() {
  return ShillProfileClient::GetSharedProfilePath();
}

void NetworkProfileHandler::AddObserver(NetworkProfileObserver* observer) {
  observers_.AddObserver(observer);
}

void NetworkProfileHandler::RemoveObserver(NetworkProfileObserver* observer) {
  observers_.RemoveObserver(observer);
}

bool NetworkProfileHandler::HasObserver(NetworkProfileObserver* observer) {
  return observers_.HasObserver(observer);
}

void NetworkProfileHandler::GetManagerPropertiesCallback(
    std::optional<base::Value::Dict> properties) {
  if (!properties) {
    LOG(ERROR) << "Error when requesting manager properties.";
    return;
  }

  const base::Value* profiles = properties->Find(shill::kProfilesProperty);
  if (!profiles) {
    LOG(ERROR) << "Manager properties returned from Shill don't contain "
               << "the field " << shill::kProfilesProperty;
    return;
  }
  OnPropertyChanged(shill::kProfilesProperty, *profiles);
}

void NetworkProfileHandler::OnPropertyChanged(const std::string& name,
                                              const base::Value& value) {
  if (name != shill::kProfilesProperty)
    return;

  DCHECK(value.is_list());

  std::vector<std::string> new_profile_paths;
  bool result =
      ConvertListValueToStringVector(value.GetList(), &new_profile_paths);
  DCHECK(result);

  VLOG(2) << "Profiles: " << profiles_.size();
  // Search for removed profiles.
  std::vector<std::string> removed_profile_paths;
  for (ProfileList::const_iterator it = profiles_.begin();
       it != profiles_.end(); ++it) {
    if (!base::Contains(new_profile_paths, it->path)) {
      removed_profile_paths.push_back(it->path);
    }
  }

  for (const std::string& profile_path : removed_profile_paths) {
    RemoveProfile(profile_path);
    // Also stop pending creations of this profile.
    pending_profile_creations_.erase(profile_path);
  }

  for (std::vector<std::string>::const_iterator it = new_profile_paths.begin();
       it != new_profile_paths.end(); ++it) {
    // Skip known profiles. The associated userhash should never change.
    if (GetProfileForPath(*it) || pending_profile_creations_.count(*it) > 0)
      continue;
    pending_profile_creations_.insert(*it);

    VLOG(2) << "Requesting properties of profile path " << *it << ".";
    ShillProfileClient::Get()->GetProperties(
        dbus::ObjectPath(*it),
        base::BindOnce(&NetworkProfileHandler::GetProfilePropertiesCallback,
                       weak_ptr_factory_.GetWeakPtr(), *it),
        base::BindOnce(&LogProfileRequestError, *it));
  }
}

void NetworkProfileHandler::GetProfilePropertiesCallback(
    const std::string& profile_path,
    base::Value::Dict properties) {
  if (pending_profile_creations_.erase(profile_path) == 0) {
    VLOG(1) << "Ignore received properties, profile was removed.";
    return;
  }
  if (GetProfileForPath(profile_path)) {
    VLOG(1) << "Ignore received properties, profile is already created.";
    return;
  }
  const std::string* userhash = properties.FindString(shill::kUserHashProperty);

  AddProfile(NetworkProfile(profile_path, userhash ? *userhash : ""));
}

void NetworkProfileHandler::AddProfile(const NetworkProfile& profile) {
  VLOG(2) << "Adding profile " << profile.ToDebugString() << ".";
  profiles_.push_back(profile);
  for (auto& observer : observers_) {
    observer.OnProfileAdded(profiles_.back());
  }
}

void NetworkProfileHandler::RemoveProfile(const std::string& profile_path) {
  VLOG(2) << "Removing profile for path " << profile_path << ".";
  ProfileList::iterator found =
      std::ranges::find(profiles_, profile_path, &NetworkProfile::path);
  if (found == profiles_.end()) {
    return;
  }
  NetworkProfile profile = *found;
  profiles_.erase(found);
  for (auto& observer : observers_) {
    observer.OnProfileRemoved(profile);
  }
}

const NetworkProfile* NetworkProfileHandler::GetProfileForPath(
    const std::string& profile_path) const {
  ProfileList::const_iterator found =
      std::ranges::find(profiles_, profile_path, &NetworkProfile::path);

  if (found == profiles_.end()) {
    return nullptr;
  }
  return &*found;
}

const NetworkProfile* NetworkProfileHandler::GetProfileForUserhash(
    const std::string& userhash) const {
  for (const auto& profile : profiles_) {
    if (profile.userhash == userhash) {
      return &profile;
    }
  }
  return nullptr;
}

const NetworkProfile* NetworkProfileHandler::GetDefaultUserProfile() const {
  for (const auto& profile : profiles_) {
    if (!profile.userhash.empty()) {
      return &profile;
    }
  }
  return nullptr;
}

void NetworkProfileHandler::GetAlwaysOnVpnConfiguration(
    const std::string& profile_path,
    base::OnceCallback<void(std::string, std::string)> callback) {
  ShillProfileClient::Get()->GetProperties(
      dbus::ObjectPath(profile_path),
      base::BindOnce(
          &NetworkProfileHandler::GetAlwaysOnVpnConfigurationCallback,
          weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
      base::BindOnce(&LogError, shill::kGetPropertiesFunction, profile_path));
}

void NetworkProfileHandler::GetAlwaysOnVpnConfigurationCallback(
    base::OnceCallback<void(std::string, std::string)> callback,
    base::Value::Dict properties) {
  // A profile always contains the mode.
  std::string* mode = properties.FindString(shill::kAlwaysOnVpnModeProperty);
  DCHECK(mode);
  std::string* service =
      properties.FindString(shill::kAlwaysOnVpnServiceProperty);
  std::move(callback).Run(*mode, service ? *service : std::string());
}

void NetworkProfileHandler::SetAlwaysOnVpnMode(const std::string& profile_path,
                                               const std::string& mode) {
  ShillProfileClient::Get()->SetProperty(
      dbus::ObjectPath(profile_path), shill::kAlwaysOnVpnModeProperty,
      base::Value(mode), base::DoNothing(),
      base::BindOnce(&LogError, shill::kSetPropertyFunction, profile_path));
}

void NetworkProfileHandler::SetAlwaysOnVpnService(
    const std::string& profile_path,
    const std::string& service_path) {
  ShillProfileClient::Get()->SetObjectPathProperty(
      dbus::ObjectPath(profile_path), shill::kAlwaysOnVpnServiceProperty,
      dbus::ObjectPath(service_path), base::DoNothing(),
      base::BindOnce(&LogError, shill::kSetPropertyFunction, profile_path));
}

NetworkProfileHandler::NetworkProfileHandler() {}

void NetworkProfileHandler::Init() {
  ShillManagerClient::Get()->AddPropertyChangedObserver(this);

  // Request the initial profile list.
  ShillManagerClient::Get()->GetProperties(
      base::BindOnce(&NetworkProfileHandler::GetManagerPropertiesCallback,
                     weak_ptr_factory_.GetWeakPtr()));
}

NetworkProfileHandler::~NetworkProfileHandler() {
  if (!ShillManagerClient::Get())
    return;

  ShillManagerClient::Get()->RemovePropertyChangedObserver(this);
}

// static
std::unique_ptr<NetworkProfileHandler>
NetworkProfileHandler::InitializeForTesting() {
  auto* handler = new NetworkProfileHandler();
  handler->Init();
  return base::WrapUnique(handler);
}

}  // namespace ash