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