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