chrome_network_delegate.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_ 6#define CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/compiler_specific.h" 12#include "base/files/file_path.h" 13#include "base/memory/ref_counted.h" 14#include "base/memory/scoped_ptr.h" 15#include "base/values.h" 16#include "chrome/browser/net/chrome_network_data_saving_metrics.h" 17#include "net/base/network_delegate.h" 18 19class ClientHints; 20class CookieSettings; 21class PrefService; 22template<class T> class PrefMember; 23 24typedef PrefMember<bool> BooleanPrefMember; 25 26namespace base { 27class Value; 28} 29 30namespace chrome_browser_net { 31class ConnectInterceptor; 32class LoadTimeStats; 33class Predictor; 34} 35 36namespace extensions { 37class EventRouterForwarder; 38class InfoMap; 39} 40 41namespace net { 42class URLRequest; 43} 44 45namespace policy { 46class URLBlacklistManager; 47} 48 49// ChromeNetworkDelegate is the central point from within the chrome code to 50// add hooks into the network stack. 51class ChromeNetworkDelegate : public net::NetworkDelegate { 52 public: 53 // |enable_referrers| (and all of the other optional PrefMembers) should be 54 // initialized on the UI thread (see below) beforehand. This object's owner is 55 // responsible for cleaning them up at shutdown. 56 ChromeNetworkDelegate(extensions::EventRouterForwarder* event_router, 57 BooleanPrefMember* enable_referrers); 58 virtual ~ChromeNetworkDelegate(); 59 60 // Not inlined because we assign a scoped_refptr, which requires us to include 61 // the header file. 62 void set_extension_info_map(extensions::InfoMap* extension_info_map); 63 64 void set_url_blacklist_manager( 65 const policy::URLBlacklistManager* url_blacklist_manager) { 66 url_blacklist_manager_ = url_blacklist_manager; 67 } 68 69 // If |profile| is NULL or not set, events will be broadcast to all profiles, 70 // otherwise they will only be sent to the specified profile. 71 void set_profile(void* profile) { 72 profile_ = profile; 73 } 74 75 // |profile_path| is used to locate the "Downloads" folder on Chrome OS. If it 76 // is set, the location of the Downloads folder for the profile is added to 77 // the whitelist for accesses via file: scheme. 78 void set_profile_path(const base::FilePath& profile_path) { 79 profile_path_ = profile_path; 80 } 81 82 // If |cookie_settings| is NULL or not set, all cookies are enabled, 83 // otherwise the settings are enforced on all observed network requests. 84 // Not inlined because we assign a scoped_refptr, which requires us to include 85 // the header file. Here we just forward-declare it. 86 void set_cookie_settings(CookieSettings* cookie_settings); 87 88 // Causes requested URLs to be fed to |predictor| via ConnectInterceptor. 89 void set_predictor(chrome_browser_net::Predictor* predictor); 90 91 void set_load_time_stats(chrome_browser_net::LoadTimeStats* load_time_stats) { 92 load_time_stats_ = load_time_stats; 93 } 94 95 void set_enable_do_not_track(BooleanPrefMember* enable_do_not_track) { 96 enable_do_not_track_ = enable_do_not_track; 97 } 98 99 void set_force_google_safe_search( 100 BooleanPrefMember* force_google_safe_search) { 101 force_google_safe_search_ = force_google_safe_search; 102 } 103 104 // Adds the Client Hints header to HTTP requests. 105 void SetEnableClientHints(); 106 107 // Causes |OnCanThrottleRequest| to always return false, for all 108 // instances of this object. 109 static void NeverThrottleRequests(); 110 111 // Binds the pref members to |pref_service| and moves them to the IO thread. 112 // |enable_referrers| cannot be NULL, the others can. 113 // This method should be called on the UI thread. 114 static void InitializePrefsOnUIThread( 115 BooleanPrefMember* enable_referrers, 116 BooleanPrefMember* enable_do_not_track, 117 BooleanPrefMember* force_google_safe_search, 118 PrefService* pref_service); 119 120 // When called, all file:// URLs will now be accessible. If this is not 121 // called, then some platforms restrict access to file:// paths. 122 static void AllowAccessToAllFiles(); 123 124 // Creates a Value summary of the persistent state of the network session. 125 // The caller is responsible for deleting the returned value. 126 // Must be called on the UI thread. 127 static Value* HistoricNetworkStatsInfoToValue(); 128 129 // Creates a Value summary of the state of the network session. The caller is 130 // responsible for deleting the returned value. 131 Value* SessionNetworkStatsInfoToValue() const; 132 133 private: 134 friend class ChromeNetworkDelegateTest; 135 136 // NetworkDelegate implementation. 137 virtual int OnBeforeURLRequest(net::URLRequest* request, 138 const net::CompletionCallback& callback, 139 GURL* new_url) OVERRIDE; 140 virtual int OnBeforeSendHeaders(net::URLRequest* request, 141 const net::CompletionCallback& callback, 142 net::HttpRequestHeaders* headers) OVERRIDE; 143 virtual void OnSendHeaders(net::URLRequest* request, 144 const net::HttpRequestHeaders& headers) OVERRIDE; 145 virtual int OnHeadersReceived( 146 net::URLRequest* request, 147 const net::CompletionCallback& callback, 148 const net::HttpResponseHeaders* original_response_headers, 149 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) 150 OVERRIDE; 151 virtual void OnBeforeRedirect(net::URLRequest* request, 152 const GURL& new_location) OVERRIDE; 153 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; 154 virtual void OnRawBytesRead(const net::URLRequest& request, 155 int bytes_read) OVERRIDE; 156 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE; 157 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE; 158 virtual void OnPACScriptError(int line_number, 159 const base::string16& error) OVERRIDE; 160 virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired( 161 net::URLRequest* request, 162 const net::AuthChallengeInfo& auth_info, 163 const AuthCallback& callback, 164 net::AuthCredentials* credentials) OVERRIDE; 165 virtual bool OnCanGetCookies(const net::URLRequest& request, 166 const net::CookieList& cookie_list) OVERRIDE; 167 virtual bool OnCanSetCookie(const net::URLRequest& request, 168 const std::string& cookie_line, 169 net::CookieOptions* options) OVERRIDE; 170 virtual bool OnCanAccessFile(const net::URLRequest& request, 171 const base::FilePath& path) const OVERRIDE; 172 virtual bool OnCanThrottleRequest( 173 const net::URLRequest& request) const OVERRIDE; 174 virtual bool OnCanEnablePrivacyMode( 175 const GURL& url, 176 const GURL& first_party_for_cookies) const OVERRIDE; 177 virtual int OnBeforeSocketStreamConnect( 178 net::SocketStream* stream, 179 const net::CompletionCallback& callback) OVERRIDE; 180 virtual void OnRequestWaitStateChange(const net::URLRequest& request, 181 RequestWaitState state) OVERRIDE; 182 183 void AccumulateContentLength( 184 int64 received_payload_byte_count, int64 original_payload_byte_count, 185 chrome_browser_net::DataReductionRequestType data_reduction_type); 186 187 scoped_refptr<extensions::EventRouterForwarder> event_router_; 188 void* profile_; 189 base::FilePath profile_path_; 190 scoped_refptr<CookieSettings> cookie_settings_; 191 192 scoped_refptr<extensions::InfoMap> extension_info_map_; 193 194 scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_; 195 196 // Weak, owned by our owner. 197 BooleanPrefMember* enable_referrers_; 198 BooleanPrefMember* enable_do_not_track_; 199 BooleanPrefMember* force_google_safe_search_; 200 201 // Weak, owned by our owner. 202 const policy::URLBlacklistManager* url_blacklist_manager_; 203 204 // When true, allow access to all file:// URLs. 205 static bool g_allow_file_access_; 206 207 // True if OnCanThrottleRequest should always return false. 208 // 209 // Note: This needs to be static as the instance of 210 // ChromeNetworkDelegate used may change over time, and we need to 211 // set this variable once at start-up time. It is effectively 212 // static anyway since it is based on a command-line flag. 213 static bool g_never_throttle_requests_; 214 215 // Pointer to IOThread global, should outlive ChromeNetworkDelegate. 216 chrome_browser_net::LoadTimeStats* load_time_stats_; 217 218 // Total size of all content (excluding headers) that has been received 219 // over the network. 220 int64 received_content_length_; 221 222 // Total original size of all content before it was transferred. 223 int64 original_content_length_; 224 225 scoped_ptr<ClientHints> client_hints_; 226 227 DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate); 228}; 229 230#endif // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_ 231