blob: b917393a6589abfc9b2afc14b70f2ca21a989580 [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.
// This class represents contextual information (cookies, cache, etc.)
// that's necessary when processing resource requests.
#ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
#define NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_
#include <memory>
#include <set>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "base/trace_event/memory_dump_provider.h"
#include "net/base/net_export.h"
#include "net/base/request_priority.h"
#include "net/http/http_network_session.h"
#include "net/http/http_server_properties.h"
#include "net/http/transport_security_state.h"
#include "net/ssl/ssl_config_service.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request.h"
namespace base {
namespace trace_event {
class ProcessMemoryDump;
}
}
namespace net {
class CertVerifier;
class ChannelIDService;
class CookieStore;
class CTPolicyEnforcer;
class CTVerifier;
class HostResolver;
class HttpAuthHandlerFactory;
class HttpTransactionFactory;
class HttpUserAgentSettings;
class NetLog;
class NetworkDelegate;
class NetworkErrorLoggingDelegate;
class NetworkQualityEstimator;
class ReportingService;
class SdchManager;
class ProxyService;
class URLRequest;
class URLRequestBackoffManager;
class URLRequestJobFactory;
class URLRequestThrottlerManager;
// Subclass to provide application-specific context for URLRequest
// instances. URLRequestContext does not own these member variables, since they
// may be shared with other contexts. URLRequestContextStorage can be used for
// automatic lifetime management. Most callers should use an existing
// URLRequestContext rather than creating a new one, as guaranteeing that the
// URLRequestContext is destroyed before its members can be difficult.
class NET_EXPORT URLRequestContext
: public base::trace_event::MemoryDumpProvider {
public:
URLRequestContext();
~URLRequestContext() override;
// Copies the state from |other| into this context.
void CopyFrom(const URLRequestContext* other);
// May return nullptr if this context doesn't have an associated network
// session.
const HttpNetworkSession::Params* GetNetworkSessionParams() const;
// May return nullptr if this context doesn't have an associated network
// session.
const HttpNetworkSession::Context* GetNetworkSessionContext() const;
// This function should not be used in Chromium, please use the version with
// NetworkTrafficAnnotationTag in the future.
std::unique_ptr<URLRequest> CreateRequest(
const GURL& url,
RequestPriority priority,
URLRequest::Delegate* delegate) const;
// |traffic_annotation| is metadata about the network traffic send via this
// URLRequest, see net::DefineNetworkTrafficAnnotation. Note that:
// - net provides the API for tagging requests with an opaque identifier.
// - tools/traffic_annotation/traffic_annotation.proto contains the Chrome
// specific .proto describing the verbose annotation format that Chrome's
// callsites are expected to follow.
// - tools/traffic_annotation/ contains sample and template for annotation and
// tools will be added for verification following crbug.com/690323.
std::unique_ptr<URLRequest> CreateRequest(
const GURL& url,
RequestPriority priority,
URLRequest::Delegate* delegate,
NetworkTrafficAnnotationTag traffic_annotation) const;
NetLog* net_log() const { return net_log_; }
void set_net_log(NetLog* net_log) {
net_log_ = net_log;
}
HostResolver* host_resolver() const {
return host_resolver_;
}
void set_host_resolver(HostResolver* host_resolver) {
host_resolver_ = host_resolver;
}
CertVerifier* cert_verifier() const {
return cert_verifier_;
}
void set_cert_verifier(CertVerifier* cert_verifier) {
cert_verifier_ = cert_verifier;
}
ChannelIDService* channel_id_service() const {
return channel_id_service_;
}
void set_channel_id_service(
ChannelIDService* channel_id_service) {
channel_id_service_ = channel_id_service;
}
// Get the proxy service for this context.
ProxyService* proxy_service() const { return proxy_service_; }
void set_proxy_service(ProxyService* proxy_service) {
proxy_service_ = proxy_service;
}
// Get the ssl config service for this context.
SSLConfigService* ssl_config_service() const {
return ssl_config_service_.get();
}
void set_ssl_config_service(SSLConfigService* service) {
ssl_config_service_ = service;
}
// Gets the HTTP Authentication Handler Factory for this context.
// The factory is only valid for the lifetime of this URLRequestContext
HttpAuthHandlerFactory* http_auth_handler_factory() const {
return http_auth_handler_factory_;
}
void set_http_auth_handler_factory(HttpAuthHandlerFactory* factory) {
http_auth_handler_factory_ = factory;
}
// Gets the http transaction factory for this context.
HttpTransactionFactory* http_transaction_factory() const {
return http_transaction_factory_;
}
void set_http_transaction_factory(HttpTransactionFactory* factory) {
http_transaction_factory_ = factory;
}
void set_network_delegate(NetworkDelegate* network_delegate) {
network_delegate_ = network_delegate;
}
NetworkDelegate* network_delegate() const { return network_delegate_; }
void set_http_server_properties(
HttpServerProperties* http_server_properties) {
http_server_properties_ = http_server_properties;
}
HttpServerProperties* http_server_properties() const {
return http_server_properties_;
}
// Gets the cookie store for this context (may be null, in which case
// cookies are not stored).
CookieStore* cookie_store() const { return cookie_store_; }
void set_cookie_store(CookieStore* cookie_store);
TransportSecurityState* transport_security_state() const {
return transport_security_state_;
}
void set_transport_security_state(
TransportSecurityState* state) {
transport_security_state_ = state;
}
CTVerifier* cert_transparency_verifier() const {
return cert_transparency_verifier_;
}
void set_cert_transparency_verifier(CTVerifier* verifier) {
cert_transparency_verifier_ = verifier;
}
CTPolicyEnforcer* ct_policy_enforcer() const { return ct_policy_enforcer_; }
void set_ct_policy_enforcer(CTPolicyEnforcer* enforcer) {
ct_policy_enforcer_ = enforcer;
}
const URLRequestJobFactory* job_factory() const { return job_factory_; }
void set_job_factory(const URLRequestJobFactory* job_factory) {
job_factory_ = job_factory;
}
// May return nullptr.
URLRequestThrottlerManager* throttler_manager() const {
return throttler_manager_;
}
void set_throttler_manager(URLRequestThrottlerManager* throttler_manager) {
throttler_manager_ = throttler_manager;
}
// May return nullptr.
URLRequestBackoffManager* backoff_manager() const { return backoff_manager_; }
void set_backoff_manager(URLRequestBackoffManager* backoff_manager) {
backoff_manager_ = backoff_manager;
}
// May return nullptr.
// TODO(xunjieli): Remove this when SdchManager is removed. crbug.com/762686
SdchManager* sdch_manager() const { return nullptr; }
void set_sdch_manager(SdchManager* sdch_manager) { NOTREACHED(); }
// Gets the URLRequest objects that hold a reference to this
// URLRequestContext.
const std::set<const URLRequest*>& url_requests() const {
return url_requests_;
}
void InsertURLRequest(const URLRequest* request) const;
void RemoveURLRequest(const URLRequest* request) const;
// CHECKs that no URLRequests using this context remain. Subclasses should
// additionally call AssertNoURLRequests() within their own destructor,
// prior to implicit destruction of subclass-owned state.
void AssertNoURLRequests() const;
// CHECKs that the passed URLRequest is present on this context.
// Added for http://crbug.com/754704; remove when that bug is resolved.
void AssertURLRequestPresent(const URLRequest* request) const;
// Get the underlying |HttpUserAgentSettings| implementation that provides
// the HTTP Accept-Language and User-Agent header values.
const HttpUserAgentSettings* http_user_agent_settings() const {
return http_user_agent_settings_;
}
void set_http_user_agent_settings(
const HttpUserAgentSettings* http_user_agent_settings) {
http_user_agent_settings_ = http_user_agent_settings;
}
// Gets the NetworkQualityEstimator associated with this context.
// May return nullptr.
NetworkQualityEstimator* network_quality_estimator() const {
return network_quality_estimator_;
}
void set_network_quality_estimator(
NetworkQualityEstimator* network_quality_estimator) {
network_quality_estimator_ = network_quality_estimator;
}
ReportingService* reporting_service() const { return reporting_service_; }
void set_reporting_service(ReportingService* reporting_service) {
reporting_service_ = reporting_service;
}
NetworkErrorLoggingDelegate* network_error_logging_delegate() const {
return network_error_logging_delegate_;
}
void set_network_error_logging_delegate(
NetworkErrorLoggingDelegate* network_error_logging_delegate) {
network_error_logging_delegate_ = network_error_logging_delegate;
}
void set_enable_brotli(bool enable_brotli) { enable_brotli_ = enable_brotli; }
bool enable_brotli() const { return enable_brotli_; }
// Sets the |check_cleartext_permitted| flag, which controls whether to check
// system policy before allowing a cleartext http or ws request.
void set_check_cleartext_permitted(bool check_cleartext_permitted) {
check_cleartext_permitted_ = check_cleartext_permitted;
}
// Returns current value of the |check_cleartext_permitted| flag.
bool check_cleartext_permitted() const { return check_cleartext_permitted_; }
// Sets a name for this URLRequestContext. Currently the name is used in
// MemoryDumpProvier to annotate memory usage. The name does not need to be
// unique.
void set_name(const char* name) { name_ = name; }
// MemoryDumpProvider implementation:
// This is reported as
// "memory:chrome:all_processes:reported_by_chrome:net:effective_size_avg."
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) override;
void AssertCalledOnValidThread() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
private:
// ---------------------------------------------------------------------------
// Important: When adding any new members below, consider whether they need to
// be added to CopyFrom.
// ---------------------------------------------------------------------------
// Ownership for these members are not defined here. Clients should either
// provide storage elsewhere or have a subclass take ownership.
NetLog* net_log_;
HostResolver* host_resolver_;
CertVerifier* cert_verifier_;
ChannelIDService* channel_id_service_;
HttpAuthHandlerFactory* http_auth_handler_factory_;
ProxyService* proxy_service_;
scoped_refptr<SSLConfigService> ssl_config_service_;
NetworkDelegate* network_delegate_;
HttpServerProperties* http_server_properties_;
const HttpUserAgentSettings* http_user_agent_settings_;
CookieStore* cookie_store_;
TransportSecurityState* transport_security_state_;
CTVerifier* cert_transparency_verifier_;
CTPolicyEnforcer* ct_policy_enforcer_;
HttpTransactionFactory* http_transaction_factory_;
const URLRequestJobFactory* job_factory_;
URLRequestThrottlerManager* throttler_manager_;
URLRequestBackoffManager* backoff_manager_;
NetworkQualityEstimator* network_quality_estimator_;
ReportingService* reporting_service_;
NetworkErrorLoggingDelegate* network_error_logging_delegate_;
// ---------------------------------------------------------------------------
// Important: When adding any new members below, consider whether they need to
// be added to CopyFrom.
// ---------------------------------------------------------------------------
mutable std::set<const URLRequest*> url_requests_;
// Enables Brotli Content-Encoding support.
bool enable_brotli_;
// Enables checking system policy before allowing a cleartext http or ws
// request. Only used on Android.
bool check_cleartext_permitted_;
// An optional name which can be set to describe this URLRequestContext.
// Used in MemoryDumpProvier to annotate memory usage. The name does not need
// to be unique.
const char* name_;
// The largest number of outstanding URLRequests that have been created by
// |this| and are not yet destroyed. This doesn't need to be in CopyFrom.
mutable size_t largest_outstanding_requests_count_seen_;
THREAD_CHECKER(thread_checker_);
DISALLOW_COPY_AND_ASSIGN(URLRequestContext);
};
} // namespace net
#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_H_