blob: cd532bb3a2c972a5bb007ab5e9d99f59bab07795 [file]
// Copyright 2026 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_config_service_impl.h"
#include <utility>
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
namespace proxy_config {
ProxyConfigServiceImpl::ProxyConfigServiceImpl(
std::unique_ptr<net::ProxyConfigService> base_service,
ProxyPrefs::ConfigState initial_config_state,
const net::ProxyConfigWithAnnotation& initial_config)
: base_service_(std::move(base_service)),
pref_config_state_(initial_config_state),
pref_config_(initial_config) {
// ProxyConfigServiceImpl is created on the UI thread, but used on the network
// thread.
DETACH_FROM_THREAD(thread_checker_);
}
ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
if (registered_observer_ && base_service_.get()) {
base_service_->RemoveObserver(this);
}
}
void ProxyConfigServiceImpl::AddObserver(
net::ProxyConfigService::Observer* observer) {
RegisterObserver();
observers_.AddObserver(observer);
}
void ProxyConfigServiceImpl::RemoveObserver(
net::ProxyConfigService::Observer* observer) {
observers_.RemoveObserver(observer);
}
net::ProxyConfigService::ConfigAvailability
ProxyConfigServiceImpl::GetLatestProxyConfig(
net::ProxyConfigWithAnnotation* config) {
RegisterObserver();
// Ask the base service if available.
net::ProxyConfigWithAnnotation system_config;
ConfigAvailability system_availability =
net::ProxyConfigService::CONFIG_UNSET;
if (base_service_) {
system_availability = base_service_->GetLatestProxyConfig(&system_config);
}
ProxyPrefs::ConfigState config_state;
return PrefProxyConfigTrackerImpl::GetEffectiveProxyConfig(
pref_config_state_, pref_config_, system_availability, system_config,
false, &config_state, config);
}
void ProxyConfigServiceImpl::OnLazyPoll() {
if (base_service_) {
base_service_->OnLazyPoll();
}
}
bool ProxyConfigServiceImpl::UsesPolling() {
return base_service_ && base_service_->UsesPolling();
}
void ProxyConfigServiceImpl::UpdateProxyConfig(
ProxyPrefs::ConfigState config_state,
const net::ProxyConfigWithAnnotation& config) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
pref_config_state_ = config_state;
pref_config_ = config;
if (observers_.empty()) {
return;
}
// Evaluate the proxy configuration. If GetLatestProxyConfig returns
// CONFIG_PENDING, we are using the system proxy service, but it doesn't have
// a valid configuration yet. Once it is ready, OnProxyConfigChanged() will be
// called and broadcast the proxy configuration.
// Note: If a switch between a preference proxy configuration and the system
// proxy configuration occurs an unnecessary notification might get sent if
// the two configurations agree. This case should be rare however, so we don't
// handle that case specially.
net::ProxyConfigWithAnnotation new_config;
ConfigAvailability availability = GetLatestProxyConfig(&new_config);
if (availability != CONFIG_PENDING) {
for (net::ProxyConfigService::Observer& observer : observers_) {
observer.OnProxyConfigChanged(new_config, availability);
}
}
}
void ProxyConfigServiceImpl::OnProxyConfigChanged(
const net::ProxyConfigWithAnnotation& config,
ConfigAvailability availability) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// Check whether there is a proxy configuration defined by preferences. In
// this case that proxy configuration takes precedence and the change event
// from the delegate proxy config service can be disregarded.
if (!PrefProxyConfigTrackerImpl::PrefPrecedes(pref_config_state_)) {
net::ProxyConfigWithAnnotation actual_config;
availability = GetLatestProxyConfig(&actual_config);
for (net::ProxyConfigService::Observer& observer : observers_) {
observer.OnProxyConfigChanged(actual_config, availability);
}
}
}
void ProxyConfigServiceImpl::RegisterObserver() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (!registered_observer_ && base_service_.get()) {
base_service_->AddObserver(this);
registered_observer_ = true;
}
}
base::WeakPtr<ProxyConfigServiceImpl> ProxyConfigServiceImpl::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace proxy_config