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