File: ui_proxy_config_service.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 (365 lines) | stat: -rw-r--r-- 14,251 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// 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/proxy/ui_proxy_config_service.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/proxy/proxy_config_handler.h"
#include "chromeos/ash/components/network/proxy/proxy_config_service_impl.h"
#include "chromeos/ash/components/network/tether_constants.h"
#include "components/prefs/pref_service.h"
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/proxy_config/proxy_prefs.h"
#include "net/proxy_resolution/proxy_config.h"

namespace ash {

namespace {

// Writes the proxy config of |network| to |proxy_config|.  Sets |onc_source| to
// the source of this configuration. Returns false if no proxy was configured
// for this network.
bool GetProxyConfig(const PrefService* profile_prefs,
                    const PrefService* local_state_prefs,
                    const NetworkState& network,
                    const NetworkProfileHandler* network_profile_handler,
                    net::ProxyConfigWithAnnotation* proxy_config,
                    onc::ONCSource* onc_source) {
  std::unique_ptr<ProxyConfigDictionary> proxy_dict =
      proxy_config::GetProxyConfigForNetwork(profile_prefs, local_state_prefs,
                                             network, network_profile_handler,
                                             onc_source);
  if (!proxy_dict)
    return false;
  return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
                                                           proxy_config);
}

std::string EffectiveConfigStateToOncSourceString(
    ProxyPrefs::ConfigState effective_config_state,
    bool is_local_state_config,
    onc::ONCSource onc_source) {
  // If source precedes prefs, the config came from prefs - set by either policy
  // or extensions.
  if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
    if (effective_config_state == ProxyPrefs::CONFIG_EXTENSION)
      return ::onc::kAugmentationActiveExtension;
    return is_local_state_config ? ::onc::kAugmentationDevicePolicy
                                 : ::onc::kAugmentationUserPolicy;
  }

  // If network is managed, proxy settings should be marked as policy managed,
  // even if the proxy settings are not set by policy - this reports the
  // default proxy settings as non-user modifiable.
  if (onc_source == onc::ONC_SOURCE_USER_POLICY)
    return ::onc::kAugmentationUserPolicy;
  if (onc_source == onc::ONC_SOURCE_DEVICE_POLICY)
    return ::onc::kAugmentationDevicePolicy;

  return std::string();
}

template <typename T>
base::Value::Dict CreateEffectiveValue(const std::string& source, T value) {
  base::Value::Dict dict;
  dict.Set(::onc::kAugmentationEffectiveSetting, source);
  // ActiveExtension is a special source type indicating that the Effective
  // value is the Active value and was set by an extension. It does not provide
  // a separate value.
  if (source != ::onc::kAugmentationActiveExtension) {
    dict.Set(source, value.Clone());
  }
  dict.Set(::onc::kAugmentationActiveSetting, std::move(value));
  dict.Set(::onc::kAugmentationUserEditable, false);
  return dict;
}

void SetManualProxy(base::Value::Dict* manual,
                    const std::string& source,
                    const std::string& key,
                    const net::ProxyList& proxy_list) {
  if (proxy_list.IsEmpty()) {
    manual->SetByDottedPath(base::JoinString({key, ::onc::proxy::kHost}, "."),
                            CreateEffectiveValue(source, base::Value("")));
    manual->SetByDottedPath(base::JoinString({key, ::onc::proxy::kPort}, "."),
                            CreateEffectiveValue(source, base::Value(0)));
    return;
  }

  const net::ProxyChain& chain = proxy_list.First();
  CHECK(chain.is_single_proxy());
  const net::ProxyServer& proxy = chain.First();
  manual->SetByDottedPath(
      base::JoinString({key, ::onc::proxy::kHost}, "."),
      CreateEffectiveValue(source, base::Value(proxy.host_port_pair().host())));
  manual->SetByDottedPath(
      base::JoinString({key, ::onc::proxy::kPort}, "."),
      CreateEffectiveValue(source, base::Value(proxy.host_port_pair().port())));
}

base::Value::Dict OncValueWithMode(const std::string& source,
                                   const std::string& mode) {
  return base::Value::Dict().Set(
      ::onc::network_config::kType,
      CreateEffectiveValue(source, base::Value(mode)));
}

std::optional<base::Value::Dict> OncValueForManualProxyList(
    const std::string& source,
    const net::ProxyList& for_http,
    const net::ProxyList& for_https,
    const net::ProxyList& fallback,
    const net::ProxyBypassRules& bypass_rules) {
  if (for_http.IsEmpty() && for_https.IsEmpty() && fallback.IsEmpty()) {
    return std::nullopt;
  }
  base::Value::Dict result = OncValueWithMode(source, ::onc::proxy::kManual);

  base::Value::Dict* manual =
      result.Set(::onc::proxy::kManual, base::Value::Dict())->GetIfDict();
  SetManualProxy(manual, source, ::onc::proxy::kHttp, for_http);
  SetManualProxy(manual, source, ::onc::proxy::kHttps, for_https);
  SetManualProxy(manual, source, ::onc::proxy::kSocks, fallback);

  base::Value::List exclude_domains;
  for (const auto& rule : bypass_rules.rules())
    exclude_domains.Append(rule->ToString());
  result.Set(::onc::proxy::kExcludeDomains,
             CreateEffectiveValue(source, std::move(exclude_domains)));

  return result;
}

std::optional<base::Value::Dict> OncValueForEmptyProxyRules(
    const net::ProxyConfig& net_config,
    const std::string& source) {
  if (!net_config.HasAutomaticSettings()) {
    return OncValueWithMode(source, ::onc::proxy::kDirect);
  }

  if (net_config.auto_detect()) {
    return OncValueWithMode(source, ::onc::proxy::kWPAD);
  }

  if (net_config.has_pac_url()) {
    base::Value::Dict result = OncValueWithMode(source, ::onc::proxy::kPAC);
    result.Set(
        ::onc::proxy::kPAC,
        CreateEffectiveValue(source, base::Value(net_config.pac_url().spec())));
    return result;
  }

  return std::nullopt;
}

std::optional<base::Value::Dict> NetProxyConfigAsOncValue(
    const net::ProxyConfig& net_config,
    const std::string& source) {
  switch (net_config.proxy_rules().type) {
    case net::ProxyConfig::ProxyRules::Type::EMPTY:
      return OncValueForEmptyProxyRules(net_config, source);
    case net::ProxyConfig::ProxyRules::Type::PROXY_LIST:
      return OncValueForManualProxyList(source,
                                        net_config.proxy_rules().single_proxies,
                                        net_config.proxy_rules().single_proxies,
                                        net_config.proxy_rules().single_proxies,
                                        net_config.proxy_rules().bypass_rules);
    case net::ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME:
      return OncValueForManualProxyList(
          source, net_config.proxy_rules().proxies_for_http,
          net_config.proxy_rules().proxies_for_https,
          net_config.proxy_rules().fallback_proxies,
          net_config.proxy_rules().bypass_rules);
  }
  return std::nullopt;
}

ProxyPrefs::ProxyMode OncStringToProxyMode(const std::string& onc_proxy_type) {
  if (onc_proxy_type == ::onc::proxy::kDirect) {
    return ProxyPrefs::ProxyMode::MODE_DIRECT;
  }
  if (onc_proxy_type == ::onc::proxy::kWPAD) {
    return ProxyPrefs::ProxyMode::MODE_AUTO_DETECT;
  }
  if (onc_proxy_type == ::onc::proxy::kPAC) {
    return ProxyPrefs::ProxyMode::MODE_PAC_SCRIPT;
  }
  if (onc_proxy_type == ::onc::proxy::kManual) {
    return ProxyPrefs::ProxyMode::MODE_FIXED_SERVERS;
  }
  NOTREACHED() << "Unsupported ONC proxy type: " << onc_proxy_type;
}

}  // namespace

UIProxyConfigService::UIProxyConfigService(
    PrefService* profile_prefs,
    PrefService* local_state_prefs,
    NetworkStateHandler* network_state_handler,
    NetworkProfileHandler* network_profile_handler)
    : profile_prefs_(profile_prefs), local_state_prefs_(local_state_prefs) {
  if (profile_prefs_) {
    profile_registrar_.Init(profile_prefs_);
    profile_registrar_.Add(
        ::proxy_config::prefs::kProxy,
        base::BindRepeating(&UIProxyConfigService::OnPreferenceChanged,
                            base::Unretained(this)));
    profile_registrar_.Add(
        ::proxy_config::prefs::kUseSharedProxies,
        base::BindRepeating(&UIProxyConfigService::OnPreferenceChanged,
                            base::Unretained(this)));
  }

  DCHECK(local_state_prefs_);
  local_state_registrar_.Init(local_state_prefs_);
  local_state_registrar_.Add(
      ::proxy_config::prefs::kProxy,
      base::BindRepeating(&UIProxyConfigService::OnPreferenceChanged,
                          base::Unretained(this)));
  network_state_handler_ = network_state_handler;
  network_profile_handler_ = network_profile_handler;
}

UIProxyConfigService::~UIProxyConfigService() = default;

bool UIProxyConfigService::MergeEnforcedProxyConfig(
    const std::string& network_guid,
    base::Value::Dict* proxy_settings) {
  current_ui_network_guid_ = network_guid;
  const NetworkState* network = nullptr;
  DCHECK(!network_guid.empty());
  DCHECK(proxy_settings);
  DCHECK(network_state_handler_);

  network = network_state_handler_->GetNetworkStateFromGuid(network_guid);
  if (!network) {
    NET_LOG(ERROR) << "No NetworkState for guid: " << network_guid;
    current_ui_network_guid_.clear();
    return false;
  }

  if (!network->IsNonProfileType() && !network->IsInProfile()) {
    NET_LOG(ERROR) << "Network not in profile: " << NetworkId(network);
    current_ui_network_guid_.clear();
    return false;
  }

  // The pref service to read proxy settings that apply to all networks.
  // Settings from the profile overrule local state.
  DCHECK(local_state_prefs_);
  DCHECK(network_profile_handler_);
  PrefService* top_pref_service =
      profile_prefs_ ? profile_prefs_.get() : local_state_prefs_.get();

  // Get prefs proxy config if available.
  net::ProxyConfigWithAnnotation pref_config;
  ProxyPrefs::ConfigState pref_state =
      ProxyConfigServiceImpl::ReadPrefConfig(top_pref_service, &pref_config);

  // Get network proxy config if available.
  net::ProxyConfigWithAnnotation network_config;
  net::ProxyConfigService::ConfigAvailability network_availability =
      net::ProxyConfigService::CONFIG_UNSET;
  onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
  if (GetProxyConfig(profile_prefs_, local_state_prefs_, *network,
                     network_profile_handler_, &network_config, &onc_source)) {
    // Network is private or shared with user using shared proxies.
    // Note: This is a common occurrence so we don't spam NET_LOG.
    VLOG(2) << "UIProxyConfigService for "
            << (profile_prefs_ ? "user" : "login")
            << ": using proxy of network: " << NetworkId(network);
    network_availability = net::ProxyConfigService::CONFIG_VALID;
  }

  // Determine effective proxy config, either from prefs or network.
  ProxyPrefs::ConfigState effective_config_state;
  net::ProxyConfigWithAnnotation effective_config;
  ProxyConfigServiceImpl::GetEffectiveProxyConfig(
      pref_state, pref_config, network_availability, network_config, false,
      &effective_config_state, &effective_config);

  const std::string source = EffectiveConfigStateToOncSourceString(
      effective_config_state, !profile_prefs_, onc_source);
  if (source.empty())
    return false;

  std::optional<base::Value::Dict> enforced_settings =
      NetProxyConfigAsOncValue(effective_config.value(), source);
  if (!enforced_settings)
    return false;

  proxy_settings->Merge(std::move(*enforced_settings));
  return true;
}

ProxyPrefs::ProxyMode UIProxyConfigService::ProxyModeForNetwork(
    const NetworkState* network) {
  onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
  std::unique_ptr<ProxyConfigDictionary> proxy_dict =
      proxy_config::GetProxyConfigForNetwork(nullptr, local_state_prefs_,
                                             *network, network_profile_handler_,
                                             &onc_source);
  // On the OOBE screen and/or tests.
  if (!network->IsInProfile()) {
    ProxyPrefs::ProxyMode mode;
    if (!proxy_dict || !proxy_dict->GetMode(&mode)) {
      return ProxyPrefs::MODE_DIRECT;
    }
    return mode;
  }

  base::Value::Dict proxy_settings;
  if (proxy_dict) {
    proxy_settings = proxy_dict->GetDictionary().Clone();
  }
  // Check for managed proxy settings.
  MergeEnforcedProxyConfig(network->guid(), &proxy_settings);
  if (!proxy_settings.empty()) {
    base::Value::Dict* proxy_type =
        proxy_settings.FindDict(::onc::network_config::kType);
    if (proxy_type) {
      std::string* proxy_active =
          proxy_type->FindString(::onc::kAugmentationActiveSetting);
      if (proxy_active) {
        return OncStringToProxyMode(*proxy_active);
      }
    }
  }

  if (!proxy_dict) {
    return ProxyPrefs::MODE_DIRECT;
  }

  // Check for user set proxy settings.
  ProxyPrefs::ProxyMode mode;
  if (proxy_dict->GetMode(&mode)) {
    return mode;
  }
  return ProxyPrefs::ProxyMode::MODE_DIRECT;
}

void UIProxyConfigService::OnPreferenceChanged(const std::string& pref_name) {
  DCHECK(network_state_handler_);
  // TODO(tbarzic): Send network update notifications for all networks that
  //     might be affected by the proxy pref change, not just the last network
  //     whose properties were fetched.
  if (current_ui_network_guid_.empty())
    return;
  const NetworkState* network =
      network_state_handler_->GetNetworkStateFromGuid(current_ui_network_guid_);
  if (network)
    network_state_handler_->SendUpdateNotificationForNetwork(network->path());
}

}  // namespace ash