blob: 2c58c61886e84d29ec8f67624c99e833fb2461f5 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_piece.h"
#include "build/build_config.h"
#include "chrome/browser/net/chrome_network_delegate.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/common/features.h"
#include "components/metrics/data_use_tracker.h"
#include "components/prefs/pref_member.h"
#include "components/ssl_config/ssl_config_service_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/browser_thread_delegate.h"
#include "extensions/features/features.h"
#include "net/base/network_change_notifier.h"
#include "net/nqe/network_quality_estimator.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/url_request_context_owner.h"
class PrefRegistrySimple;
class PrefService;
class SystemNetworkContextManager;
#if defined(OS_ANDROID)
namespace android {
class ExternalDataUseObserver;
#endif // defined(OS_ANDROID)
namespace certificate_transparency {
class TreeStateTracker;
namespace chrome_browser_net {
class DnsProbeService;
namespace data_usage {
class DataUseAggregator;
namespace data_use_measurement {
class ChromeDataUseAscriber;
namespace extensions {
class EventRouterForwarder;
namespace net {
class CTLogVerifier;
class HostResolver;
class HttpAuthHandlerFactory;
class HttpAuthPreferences;
class NetworkQualityEstimator;
class RTTAndThroughputEstimatesObserver;
class SSLConfigService;
class URLRequestContext;
class URLRequestContextGetter;
namespace ct {
class STHObserver;
} // namespace net
namespace net_log {
class ChromeNetLog;
namespace network {
class URLRequestContextBuilderMojo;
namespace policy {
class PolicyService;
} // namespace policy
// Contains state associated with, initialized and cleaned up on, and
// primarily used on, the IO thread.
// If you are looking to interact with the IO thread (e.g. post tasks
// to it or check if it is the current thread), see
// content::BrowserThread.
class IOThread : public content::BrowserThreadDelegate {
struct Globals {
class SystemRequestContextLeakChecker {
explicit SystemRequestContextLeakChecker(Globals* globals);
Globals* const globals_;
bool quic_disabled = false;
// Ascribes all data use in Chrome to a source, such as page loads.
// Global aggregator of data use. It must outlive the
// |system_network_delegate|.
std::unique_ptr<data_usage::DataUseAggregator> data_use_aggregator;
#if defined(OS_ANDROID)
// An external observer of data use.
#endif // defined(OS_ANDROID)
std::vector<scoped_refptr<const net::CTLogVerifier>> ct_logs;
std::unique_ptr<net::HttpAuthPreferences> http_auth_preferences;
// When the network service is enabled, this holds on to a
// content::NetworkContext class that owns |system_request_context|.
std::unique_ptr<network::mojom::NetworkContext> system_network_context;
// When the network service is disabled, this owns |system_request_context|.
network::URLRequestContextOwner system_request_context_owner;
net::URLRequestContext* system_request_context;
SystemRequestContextLeakChecker system_request_context_leak_checker;
std::unique_ptr<net::NetworkQualityEstimator> network_quality_estimator;
// NetErrorTabHelper uses |dns_probe_service| to send DNS probes when a
// main frame load fails with a DNS error in order to provide more useful
// information to the renderer so it can show a more specific error page.
std::unique_ptr<chrome_browser_net::DnsProbeService> dns_probe_service;
// |net_log| must either outlive the IOThread or be NULL.
IOThread(PrefService* local_state,
policy::PolicyService* policy_service,
net_log::ChromeNetLog* net_log,
extensions::EventRouterForwarder* extension_event_router_forwarder,
SystemNetworkContextManager* system_network_context_manager);
~IOThread() override;
static void RegisterPrefs(PrefRegistrySimple* registry);
// Can only be called on the IO thread.
Globals* globals();
// Allows overriding Globals in tests where IOThread::Init() and
// IOThread::CleanUp() are not called. This allows for injecting mocks into
// IOThread global objects.
void SetGlobalsForTesting(Globals* globals);
net_log::ChromeNetLog* net_log();
// Handles changing to On The Record mode, discarding confidential data.
void ChangedToOnTheRecord();
// Returns a getter for the URLRequestContext. Only called on the UI thread.
net::URLRequestContextGetter* system_url_request_context_getter();
// Clears the host cache. Intended to be used to prevent exposing recently
// visited sites on about:net-internals/#dns and about:dns pages. Must be
// called on the IO thread. If |host_filter| is not null, only hosts matched
// by it are deleted from the cache.
void ClearHostCache(
const base::Callback<bool(const std::string&)>& host_filter);
// Dynamically disables QUIC for all NetworkContexts using the IOThread's
// NetworkService. Re-enabling Quic dynamically is not supported for
// simplicity and requires a browser restart. May only be called on the IO
// thread.
void DisableQuic();
// Returns the callback for updating data use prefs.
metrics::UpdateUsagePrefCallbackType GetMetricsDataUseForwarder();
// Registers the |observer| for new STH notifications.
void RegisterSTHObserver(net::ct::STHObserver* observer);
// Un-registers the |observer|.
void UnregisterSTHObserver(net::ct::STHObserver* observer);
// Returns true if the indicated proxy resolution features are
// enabled. These features are controlled through
// preferences/policy/commandline.
// For a description of what these features are, and how they are
// configured, see the comments in for
// |kQuickCheckEnabled| and |kPacHttpsUrlStrippingEnabled
// respectively.
bool WpadQuickCheckEnabled() const;
bool PacHttpsUrlStrippingEnabled() const;
// Configures |builder|'s ProxyResolutionService based on prefs and policies.
void SetUpProxyService(network::URLRequestContextBuilderMojo* builder) const;
certificate_transparency::TreeStateTracker* ct_tree_tracker() const;
// BrowserThreadDelegate implementation, runs on the IO thread.
// This handles initialization and destruction of state that must
// live on the IO thread.
void Init() override;
void CleanUp() override;
std::unique_ptr<net::HttpAuthHandlerFactory> CreateDefaultAuthHandlerFactory(
net::HostResolver* host_resolver);
// Returns an SSLConfigService instance.
net::SSLConfigService* GetSSLConfigService();
void ChangedToOnTheRecordOnIOThread();
void UpdateDnsClientEnabled();
void UpdateServerWhitelist();
void UpdateDelegateWhitelist();
void UpdateAndroidAuthNegotiateAccountType();
void UpdateNegotiateDisableCnameLookup();
void UpdateNegotiateEnablePort();
#if defined(OS_POSIX)
void UpdateNtlmV2Enabled();
extensions::EventRouterForwarder* extension_event_router_forwarder() {
return extension_event_router_forwarder_;
return NULL;
void ConstructSystemRequestContext();
// The NetLog is owned by the browser process, to allow logging from other
// threads during shutdown, but is used most frequently on the IOThread.
net_log::ChromeNetLog* net_log_;
// The extensions::EventRouterForwarder allows for sending events to
// extensions from the IOThread.
extensions::EventRouterForwarder* extension_event_router_forwarder_;
// These member variables are basically global, but their lifetimes are tied
// to the IOThread. IOThread owns them all, despite not using scoped_ptr.
// This is because the destructor of IOThread runs on the wrong thread. All
// member variables should be deleted in CleanUp().
// These member variables are initialized in Init() and do not change for the
// lifetime of the IO thread.
Globals* globals_;
std::unique_ptr<certificate_transparency::TreeStateTracker> ct_tree_tracker_;
BooleanPrefMember system_enable_referrers_;
BooleanPrefMember dns_client_enabled_;
BooleanPrefMember quick_check_enabled_;
BooleanPrefMember pac_https_url_stripping_enabled_;
StringListPrefMember dns_over_https_servers_;
StringListPrefMember dns_over_https_server_methods_;
// Store HTTP Auth-related policies in this thread.
// TODO(aberent) Make the list of auth schemes a PrefMember, so that the
// policy can change after startup (https://crbug/549273).
std::string auth_schemes_;
BooleanPrefMember negotiate_disable_cname_lookup_;
BooleanPrefMember negotiate_enable_port_;
#if defined(OS_POSIX)
BooleanPrefMember ntlm_v2_enabled_;
StringPrefMember auth_server_whitelist_;
StringPrefMember auth_delegate_whitelist_;
#if defined(OS_ANDROID)
StringPrefMember auth_android_negotiate_account_type_;
#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
// No PrefMember for the GSSAPI library name, since changing it after startup
// requires unloading the existing GSSAPI library, which could cause all sorts
// of problems for, for example, active Negotiate transactions.
std::string gssapi_library_name_;
#if defined(OS_CHROMEOS)
bool allow_gssapi_library_load_;
// These are set on the UI thread, and then consumed during initialization on
// the IO thread.
network::mojom::NetworkContextRequest network_context_request_;
network::mojom::NetworkContextParamsPtr network_context_params_;
// This is an instance of the default SSLConfigServiceManager for the current
// platform and it gets SSL preferences from local_state object.
// True if QUIC is initially enabled.
bool is_quic_allowed_on_init_;
base::WeakPtrFactory<IOThread> weak_factory_;