Home | History | Annotate | Download | only in base
      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 NET_BASE_NETWORK_DELEGATE_H_
      6 #define NET_BASE_NETWORK_DELEGATE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/strings/string16.h"
     12 #include "base/threading/non_thread_safe.h"
     13 #include "net/base/auth.h"
     14 #include "net/base/completion_callback.h"
     15 #include "net/cookies/canonical_cookie.h"
     16 
     17 class GURL;
     18 
     19 namespace base {
     20 class FilePath;
     21 }
     22 
     23 namespace net {
     24 
     25 // NOTE: Layering violations!
     26 // We decided to accept these violations (depending
     27 // on other net/ submodules from net/base/), because otherwise NetworkDelegate
     28 // would have to be broken up into too many smaller interfaces targeted to each
     29 // submodule. Also, since the lower levels in net/ may callback into higher
     30 // levels, we may encounter dangerous casting issues.
     31 //
     32 // NOTE: It is not okay to add any compile-time dependencies on symbols outside
     33 // of net/base here, because we have a net_base library. Forward declarations
     34 // are ok.
     35 class CookieOptions;
     36 class HttpRequestHeaders;
     37 class HttpResponseHeaders;
     38 class SocketStream;
     39 class URLRequest;
     40 
     41 class NET_EXPORT NetworkDelegate : public base::NonThreadSafe {
     42  public:
     43   // AuthRequiredResponse indicates how a NetworkDelegate handles an
     44   // OnAuthRequired call. It's placed in this file to prevent url_request.h
     45   // from having to include network_delegate.h.
     46   enum AuthRequiredResponse {
     47     AUTH_REQUIRED_RESPONSE_NO_ACTION,
     48     AUTH_REQUIRED_RESPONSE_SET_AUTH,
     49     AUTH_REQUIRED_RESPONSE_CANCEL_AUTH,
     50     AUTH_REQUIRED_RESPONSE_IO_PENDING,
     51   };
     52   typedef base::Callback<void(AuthRequiredResponse)> AuthCallback;
     53 
     54   virtual ~NetworkDelegate() {}
     55 
     56   // Notification interface called by the network stack. Note that these
     57   // functions mostly forward to the private virtuals. They also add some sanity
     58   // checking on parameters. See the corresponding virtuals for explanations of
     59   // the methods and their arguments.
     60   int NotifyBeforeURLRequest(URLRequest* request,
     61                              const CompletionCallback& callback,
     62                              GURL* new_url);
     63   int NotifyBeforeSendHeaders(URLRequest* request,
     64                               const CompletionCallback& callback,
     65                               HttpRequestHeaders* headers);
     66   void NotifySendHeaders(URLRequest* request,
     67                          const HttpRequestHeaders& headers);
     68   int NotifyHeadersReceived(
     69       URLRequest* request,
     70       const CompletionCallback& callback,
     71       const HttpResponseHeaders* original_response_headers,
     72       scoped_refptr<HttpResponseHeaders>* override_response_headers,
     73       GURL* allowed_unsafe_redirect_url);
     74   void NotifyBeforeRedirect(URLRequest* request,
     75                             const GURL& new_location);
     76   void NotifyResponseStarted(URLRequest* request);
     77   void NotifyRawBytesRead(const URLRequest& request, int bytes_read);
     78   void NotifyCompleted(URLRequest* request, bool started);
     79   void NotifyURLRequestDestroyed(URLRequest* request);
     80   void NotifyPACScriptError(int line_number, const base::string16& error);
     81   AuthRequiredResponse NotifyAuthRequired(URLRequest* request,
     82                                           const AuthChallengeInfo& auth_info,
     83                                           const AuthCallback& callback,
     84                                           AuthCredentials* credentials);
     85   bool CanGetCookies(const URLRequest& request,
     86                      const CookieList& cookie_list);
     87   bool CanSetCookie(const URLRequest& request,
     88                     const std::string& cookie_line,
     89                     CookieOptions* options);
     90   bool CanAccessFile(const URLRequest& request,
     91                      const base::FilePath& path) const;
     92   bool CanThrottleRequest(const URLRequest& request) const;
     93   bool CanEnablePrivacyMode(const GURL& url,
     94                             const GURL& first_party_for_cookies) const;
     95 
     96   int NotifyBeforeSocketStreamConnect(SocketStream* socket,
     97                                       const CompletionCallback& callback);
     98 
     99  private:
    100   // This is the interface for subclasses of NetworkDelegate to implement. These
    101   // member functions will be called by the respective public notification
    102   // member function, which will perform basic sanity checking.
    103 
    104   // Called before a request is sent. Allows the delegate to rewrite the URL
    105   // being fetched by modifying |new_url|. If set, the URL must be valid. The
    106   // reference fragment from the original URL is not automatically appended to
    107   // |new_url|; callers are responsible for copying the reference fragment if
    108   // desired.
    109   // |callback| and |new_url| are valid only until OnURLRequestDestroyed is
    110   // called for this request. Returns a net status code, generally either OK to
    111   // continue with the request or ERR_IO_PENDING if the result is not ready yet.
    112   // A status code other than OK and ERR_IO_PENDING will cancel the request and
    113   // report the status code as the reason.
    114   //
    115   // The default implementation returns OK (continue with request).
    116   virtual int OnBeforeURLRequest(URLRequest* request,
    117                                  const CompletionCallback& callback,
    118                                  GURL* new_url);
    119 
    120   // Called right before the HTTP headers are sent. Allows the delegate to
    121   // read/write |headers| before they get sent out. |callback| and |headers| are
    122   // valid only until OnCompleted or OnURLRequestDestroyed is called for this
    123   // request.
    124   // See OnBeforeURLRequest for return value description. Returns OK by default.
    125   virtual int OnBeforeSendHeaders(URLRequest* request,
    126                                   const CompletionCallback& callback,
    127                                   HttpRequestHeaders* headers);
    128 
    129   // Called right before the HTTP request(s) are being sent to the network.
    130   // |headers| is only valid until OnCompleted or OnURLRequestDestroyed is
    131   // called for this request.
    132   virtual void OnSendHeaders(URLRequest* request,
    133                              const HttpRequestHeaders& headers);
    134 
    135   // Called for HTTP requests when the headers have been received.
    136   // |original_response_headers| contains the headers as received over the
    137   // network, these must not be modified. |override_response_headers| can be set
    138   // to new values, that should be considered as overriding
    139   // |original_response_headers|.
    140   // If the response is a redirect, and the Location response header value is
    141   // identical to |allowed_unsafe_redirect_url|, then the redirect is never
    142   // blocked and the reference fragment is not copied from the original URL
    143   // to the redirection target.
    144   //
    145   // |callback|, |original_response_headers|, and |override_response_headers|
    146   // are only valid until OnURLRequestDestroyed is called for this request.
    147   // See OnBeforeURLRequest for return value description. Returns OK by default.
    148   virtual int OnHeadersReceived(
    149       URLRequest* request,
    150       const CompletionCallback& callback,
    151       const HttpResponseHeaders* original_response_headers,
    152       scoped_refptr<HttpResponseHeaders>* override_response_headers,
    153       GURL* allowed_unsafe_redirect_url);
    154 
    155   // Called right after a redirect response code was received.
    156   // |new_location| is only valid until OnURLRequestDestroyed is called for this
    157   // request.
    158   virtual void OnBeforeRedirect(URLRequest* request,
    159                                 const GURL& new_location);
    160 
    161   // This corresponds to URLRequestDelegate::OnResponseStarted.
    162   virtual void OnResponseStarted(URLRequest* request);
    163 
    164   // Called every time we read raw bytes.
    165   virtual void OnRawBytesRead(const URLRequest& request, int bytes_read);
    166 
    167   // Indicates that the URL request has been completed or failed.
    168   // |started| indicates whether the request has been started. If false,
    169   // some information like the socket address is not available.
    170   virtual void OnCompleted(URLRequest* request, bool started);
    171 
    172   // Called when an URLRequest is being destroyed. Note that the request is
    173   // being deleted, so it's not safe to call any methods that may result in
    174   // a virtual method call.
    175   virtual void OnURLRequestDestroyed(URLRequest* request);
    176 
    177   // Corresponds to ProxyResolverJSBindings::OnError.
    178   virtual void OnPACScriptError(int line_number,
    179                                 const base::string16& error);
    180 
    181   // Called when a request receives an authentication challenge
    182   // specified by |auth_info|, and is unable to respond using cached
    183   // credentials. |callback| and |credentials| must be non-NULL, and must
    184   // be valid until OnURLRequestDestroyed is called for |request|.
    185   //
    186   // The following return values are allowed:
    187   //  - AUTH_REQUIRED_RESPONSE_NO_ACTION: |auth_info| is observed, but
    188   //    no action is being taken on it.
    189   //  - AUTH_REQUIRED_RESPONSE_SET_AUTH: |credentials| is filled in with
    190   //    a username and password, which should be used in a response to
    191   //    |auth_info|.
    192   //  - AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: The authentication challenge
    193   //    should not be attempted.
    194   //  - AUTH_REQUIRED_RESPONSE_IO_PENDING: The action will be decided
    195   //    asynchronously. |callback| will be invoked when the decision is made,
    196   //    and one of the other AuthRequiredResponse values will be passed in with
    197   //    the same semantics as described above.
    198   virtual AuthRequiredResponse OnAuthRequired(
    199       URLRequest* request,
    200       const AuthChallengeInfo& auth_info,
    201       const AuthCallback& callback,
    202       AuthCredentials* credentials);
    203 
    204   // Called when reading cookies to allow the network delegate to block access
    205   // to the cookie. This method will never be invoked when
    206   // LOAD_DO_NOT_SEND_COOKIES is specified.
    207   virtual bool OnCanGetCookies(const URLRequest& request,
    208                                const CookieList& cookie_list);
    209 
    210   // Called when a cookie is set to allow the network delegate to block access
    211   // to the cookie. This method will never be invoked when
    212   // LOAD_DO_NOT_SAVE_COOKIES is specified.
    213   virtual bool OnCanSetCookie(const URLRequest& request,
    214                               const std::string& cookie_line,
    215                               CookieOptions* options);
    216 
    217   // Called when a file access is attempted to allow the network delegate to
    218   // allow or block access to the given file path.  Returns true if access is
    219   // allowed.
    220   virtual bool OnCanAccessFile(const URLRequest& request,
    221                                const base::FilePath& path) const;
    222 
    223   // Returns true if the given request may be rejected when the
    224   // URLRequestThrottlerManager believes the server servicing the
    225   // request is overloaded or down.
    226   virtual bool OnCanThrottleRequest(const URLRequest& request) const;
    227 
    228   // Returns true if the given |url| has to be requested over connection that
    229   // is not tracked by the server. Usually is false, unless user privacy
    230   // settings block cookies from being get or set.
    231   virtual bool OnCanEnablePrivacyMode(
    232       const GURL& url,
    233       const GURL& first_party_for_cookies) const;
    234 
    235   // Called before a SocketStream tries to connect.
    236   // See OnBeforeURLRequest for return value description. Returns OK by default.
    237   virtual int OnBeforeSocketStreamConnect(
    238       SocketStream* socket, const CompletionCallback& callback);
    239 };
    240 
    241 }  // namespace net
    242 
    243 #endif  // NET_BASE_NETWORK_DELEGATE_H_
    244