Home | History | Annotate | Download | only in spdyproxy
      1 // Copyright 2013 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_SPDYPROXY_PROXY_ADVISOR_H_
      6 #define CHROME_BROWSER_NET_SPDYPROXY_PROXY_ADVISOR_H_
      7 
      8 #include <set>
      9 
     10 #include "base/prefs/pref_member.h"
     11 #include "chrome/browser/net/url_info.h"
     12 #include "net/url_request/url_request.h"
     13 
     14 namespace net {
     15 class URLRequestContextGetter;
     16 }
     17 
     18 class PrefService;
     19 
     20 // Accessory class for net/preconnect to be used in conjunction with the
     21 // data reduction proxy. An instance of this class will accept Advise()
     22 // calls and will send HEAD requests to an endpoint URL on the proxy
     23 // which notify the proxy of preconnection opportunities.
     24 // The HEAD requests have a header of the form:
     25 //   Proxy-Host-Advisory: <motivation> <url>
     26 // Where <motivation> is a string describing what is motivating the
     27 // preconnection ('learned_referral', 'omnibox', etc.).
     28 //
     29 // The ProxyAdvisor owns the HEAD requests. Since we don't care about any
     30 // responses from the requests, they are deleted as soon as a response
     31 // code is received.
     32 //
     33 // ProxyAdvisor monitors the state of the proxy preference; if it is
     34 // disabled, all in-flight requests are canceled.
     35 //
     36 // ProxyAdvisor instances should be created on the UI thread.
     37 class ProxyAdvisor : public net::URLRequest::Delegate {
     38  public:
     39   ProxyAdvisor(PrefService* pref_service,
     40                net::URLRequestContextGetter* context_getter);
     41   virtual ~ProxyAdvisor();
     42 
     43   // net::URLRequest::Delegate callbacks.
     44   virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
     45   virtual void OnReadCompleted(net::URLRequest* request,
     46                                int bytes_read) OVERRIDE;
     47 
     48   // Tell the advisor that |url| is being preconnected or pre-resolved and why.
     49   // If |url| would be proxied (according to WouldProxyURL()), the ProxyAdvisor
     50   // will send a HEAD request to the proxy, giving it an opportunity to
     51   // preconnect or pre-resolve hostnames prior to the browser sending actual
     52   // requests.
     53   // If WouldProxyURL returns a false positive, then Advise() will send an
     54   // advisory HEAD request to the proxy, but |url| will be fetched by the
     55   // browser directly.
     56   // |motivation| and |is_preconnect| are used to determine a motivation string
     57   // that is passed as part of the request (if |is_preconnect| is false, the
     58   // advisory is interprered as being of lower priority).
     59   // Advise() may only be called on the IO thread.
     60   virtual void Advise(const GURL& url,
     61               chrome_browser_net::UrlInfo::ResolutionMotivation motivation,
     62               bool is_preconnect);
     63 
     64   // Returns true if, under the current proxy settings, |url| is likely to be
     65   // proxied. This is quick and dirty, rather that doing full proxy resolution
     66   // (which may involve PAC file execution and checking bad proxy lists).
     67   // TODO(marq): Make this method part of DataReductionProxySettings.
     68   virtual bool WouldProxyURL(const GURL& url);
     69 
     70  private:
     71   // Removes |request| from |inflight_requests_|.
     72   void RequestComplete(net::URLRequest* request);
     73 
     74   // Checks prefs::kSpdyProxyAuthEnabled and updates |proxy_enabled_|
     75   // accordingly. If the proxy has turned off, cancels all inflight requests.
     76   void UpdateProxyState();
     77 
     78   scoped_refptr<net::URLRequestContextGetter> context_getter_;
     79 
     80   BooleanPrefMember proxy_pref_member_;
     81 
     82   std::set<net::URLRequest*> inflight_requests_;
     83 };
     84 
     85 #endif  // CHROME_BROWSER_NET_SPDYPROXY_PROXY_ADVISOR_H_
     86 
     87