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.
5 #ifndef CHROME_BROWSER_NET_SPDYPROXY_PROXY_ADVISOR_H_
6 #define CHROME_BROWSER_NET_SPDYPROXY_PROXY_ADVISOR_H_
10 #include "base/prefs/pref_member.h"
11 #include "chrome/browser/net/url_info.h"
12 #include "net/url_request/url_request.h"
15 class URLRequestContextGetter
;
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.).
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
33 // ProxyAdvisor monitors the state of the proxy preference; if it is
34 // disabled, all in-flight requests are canceled.
36 // ProxyAdvisor instances should be created on the UI thread.
37 class ProxyAdvisor
: public net::URLRequest::Delegate
{
39 ProxyAdvisor(PrefService
* pref_service
,
40 net::URLRequestContextGetter
* context_getter
);
41 virtual ~ProxyAdvisor();
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
;
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
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
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
,
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
);
71 // Removes |request| from |inflight_requests_|.
72 void RequestComplete(net::URLRequest
* request
);
74 // Checks prefs::kSpdyProxyAuthEnabled and updates |proxy_enabled_|
75 // accordingly. If the proxy has turned off, cancels all inflight requests.
76 void UpdateProxyState();
78 scoped_refptr
<net::URLRequestContextGetter
> context_getter_
;
80 BooleanPrefMember proxy_pref_member_
;
82 std::set
<net::URLRequest
*> inflight_requests_
;
85 #endif // CHROME_BROWSER_NET_SPDYPROXY_PROXY_ADVISOR_H_