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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/proxy_config/proxy_prefs_utils.h"
#include "components/policy/core/common/policy_types.h"
#include "components/prefs/pref_service.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "net/base/proxy_string_util.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
namespace proxy_config {
url::SchemeHostPort ProxyOverrideRuleHostFromString(
std::string_view raw_value) {
GURL url(raw_value);
std::string scheme = url.GetScheme();
std::string host = url.GetHost();
int port = url.IntPort();
// If the value used to initialize `url` is missing parts (for example because
// it's only a hostname), then try to specify a replacement scheme and/or
// port. This gives flexibility to the policy field to only specify certain
// values.
if (host.empty()) {
// This path is reached when only the host is provided (ex. "foo.com").
// In this case, `url` will be invalid and see "foo.com" as a scheme, so if
// that happens the string is instead parsed as a host:port pair, with HTTP
// as the default scheme.
scheme = url::kHttpScheme;
if (!net::ParseHostAndPort(raw_value, &host, &port)) {
return url::SchemeHostPort();
}
}
if (scheme.empty()) {
scheme = url::kHttpScheme;
}
if (port == url::PORT_UNSPECIFIED) {
port = url::DefaultPortForScheme(scheme);
}
return url::SchemeHostPort(scheme, host, port);
}
net::ProxyChain ProxyOverrideRuleProxyFromString(std::string_view raw_value) {
GURL url(raw_value);
if (url.is_valid()) {
net::ProxyServer::Scheme scheme = net::GetSchemeFromUriScheme(url.scheme());
if (scheme == net::ProxyServer::SCHEME_INVALID) {
return net::ProxyChain();
}
return net::ProxyChain::FromSchemeHostAndPort(scheme, url.host(),
url.port());
}
return net::PacResultElementToProxyChain(raw_value);
}
bool ProxyOverrideRulesAllowed(const PrefService* pref_service) {
CHECK(pref_service);
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
if (pref_service->GetInteger(prefs::kProxyOverrideRulesScope) ==
policy::POLICY_SCOPE_USER &&
!pref_service->GetBoolean(prefs::kProxyOverrideRulesAffiliation)) {
return pref_service->GetInteger(
prefs::kEnableProxyOverrideRulesForAllUsers) == 1;
}
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
return true;
}
} // namespace proxy_config
|