blob: a5ce925df69d02be4c349c4c85c170e55dd44bb9 [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.
#ifndef COMPONENTS_PROXY_CONFIG_PROXY_CONFIG_SERVICE_IMPL_H_
#define COMPONENTS_PROXY_CONFIG_PROXY_CONFIG_SERVICE_IMPL_H_
#include <memory>
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
#include "components/proxy_config/proxy_prefs.h"
#include "net/proxy_resolution/proxy_config_service.h"
#include "net/proxy_resolution/proxy_config_with_annotation.h"
namespace proxy_config {
// A `net::ProxyConfigService` implementation that applies preference proxy
// settings (pushed from `PrefProxyConfigTrackerImpl`) as overrides to the proxy
// configuration determined by a baseline delegate `ProxyConfigService` on
// non-ChromeOS platforms. ChromeOS has its own implementation of overrides in
// `ash::ProxyConfigServiceImpl`.
class ProxyConfigServiceImpl : public net::ProxyConfigService,
public net::ProxyConfigService::Observer {
public:
ProxyConfigServiceImpl(std::unique_ptr<net::ProxyConfigService> base_service,
ProxyPrefs::ConfigState initial_config_state,
const net::ProxyConfigWithAnnotation& initial_config);
ProxyConfigServiceImpl(const ProxyConfigServiceImpl&) = delete;
ProxyConfigServiceImpl& operator=(const ProxyConfigServiceImpl&) = delete;
~ProxyConfigServiceImpl() override;
// net::ProxyConfigService implementation:
void AddObserver(net::ProxyConfigService::Observer* observer) override;
void RemoveObserver(net::ProxyConfigService::Observer* observer) override;
ConfigAvailability GetLatestProxyConfig(
net::ProxyConfigWithAnnotation* config) override;
void OnLazyPoll() override;
bool UsesPolling() override;
// Method on IO/network thread that receives the preference proxy settings
// pushed from `PrefProxyConfigTrackerImpl`.
void UpdateProxyConfig(ProxyPrefs::ConfigState config_state,
const net::ProxyConfigWithAnnotation& config);
base::WeakPtr<ProxyConfigServiceImpl> AsWeakPtr();
private:
// net::ProxyConfigService::Observer implementation:
void OnProxyConfigChanged(const net::ProxyConfigWithAnnotation& config,
ConfigAvailability availability) override;
// Makes sure that the observer registration with the base service is set up.
void RegisterObserver();
std::unique_ptr<net::ProxyConfigService> base_service_;
base::ObserverList<net::ProxyConfigService::Observer, true>::Unchecked
observers_;
// Tracks configuration state of `pref_config_`. `pref_config_` is valid only
// if `pref_config_state_` is not `CONFIG_UNSET`.
ProxyPrefs::ConfigState pref_config_state_;
// Configuration as defined by prefs.
net::ProxyConfigWithAnnotation pref_config_;
// Indicates whether the base service registration is done.
bool registered_observer_ = false;
THREAD_CHECKER(thread_checker_);
base::WeakPtrFactory<ProxyConfigServiceImpl> weak_ptr_factory_{this};
};
} // namespace proxy_config
#endif // COMPONENTS_PROXY_CONFIG_PROXY_CONFIG_SERVICE_IMPL_H_