OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_PROXY_INIT_PROXY_RESOLVER_H_ | 5 #ifndef NET_PROXY_INIT_PROXY_RESOLVER_H_ |
6 #define NET_PROXY_INIT_PROXY_RESOLVER_H_ | 6 #define NET_PROXY_INIT_PROXY_RESOLVER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "base/timer.h" | 13 #include "base/timer.h" |
| 14 #include "base/scoped_ptr.h" |
14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
15 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
16 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
| 21 class DhcpProxyScriptFetcher; |
| 22 class NetLogParameter; |
20 class ProxyConfig; | 23 class ProxyConfig; |
21 class ProxyResolver; | 24 class ProxyResolver; |
22 class ProxyScriptFetcher; | 25 class ProxyScriptFetcher; |
| 26 class URLRequestContext; |
23 | 27 |
24 // InitProxyResolver is a helper class used by ProxyService to | 28 // InitProxyResolver is a helper class used by ProxyService to |
25 // initialize a ProxyResolver with the PAC script data specified | 29 // initialize a ProxyResolver with the PAC script data specified |
26 // by a particular ProxyConfig. | 30 // by a particular ProxyConfig. |
27 // | 31 // |
28 // This involves trying to use PAC scripts in this order: | 32 // This involves trying to use PAC scripts in this order: |
29 // | 33 // |
30 // (1) WPAD (DNS) if auto-detect is on. | 34 // (1) WPAD (DNS) if auto-detect is on. |
31 // (2) Custom PAC script if a URL was given. | 35 // (2) Custom PAC script if a URL was given. |
32 // | 36 // |
33 // If no PAC script was successfully downloaded + parsed, then it fails with | 37 // If no PAC script was successfully downloaded + parsed, then it fails with |
34 // a network error. Otherwise the proxy resolver is left initialized with | 38 // a network error. Otherwise the proxy resolver is left initialized with |
35 // the PAC script. | 39 // the PAC script. |
36 // | 40 // |
37 // Deleting InitProxyResolver while Init() is in progress, will | 41 // Deleting InitProxyResolver while Init() is in progress, will |
38 // cancel the request. | 42 // cancel the request. |
39 // | 43 // |
40 class InitProxyResolver { | 44 class InitProxyResolver { |
41 public: | 45 public: |
42 // |resolver|, |proxy_script_fetcher| and |net_log| must remain valid for | 46 // |resolver|, |proxy_script_fetcher|, |dhcp_proxy_script_fetcher| and |
43 // the lifespan of InitProxyResolver. | 47 // |net_log| must remain valid for the lifespan of InitProxyResolver. |
44 InitProxyResolver(ProxyResolver* resolver, | 48 InitProxyResolver(ProxyResolver* resolver, |
45 ProxyScriptFetcher* proxy_script_fetcher, | 49 ProxyScriptFetcher* proxy_script_fetcher, |
| 50 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, |
46 NetLog* net_log); | 51 NetLog* net_log); |
47 | 52 |
48 // Aborts any in-progress request. | 53 // Aborts any in-progress request. |
49 ~InitProxyResolver(); | 54 ~InitProxyResolver(); |
50 | 55 |
51 // Applies the PAC settings of |config| to |resolver_|. | 56 // Applies the PAC settings of |config| to |resolver_|. |
52 // If |wait_delay| is positive, the initialization will pause for this | 57 // If |wait_delay| is positive, the initialization will pause for this |
53 // amount of time before getting started. | 58 // amount of time before getting started. |
54 // If |effective_config| is non-NULL, then on successful initialization of | 59 // If |effective_config| is non-NULL, then on successful initialization of |
55 // |resolver_| the "effective" proxy settings we ended up using will be | 60 // |resolver_| the "effective" proxy settings we ended up using will be |
56 // written out to |*effective_config|. Note that this may differ from | 61 // written out to |*effective_config|. Note that this may differ from |
57 // |config| since we will have stripped any manual settings, and decided | 62 // |config| since we will have stripped any manual settings, and decided |
58 // whether to use auto-detect or the custom PAC URL. Finally, if auto-detect | 63 // whether to use auto-detect or the custom PAC URL. Finally, if auto-detect |
59 // was used we may now have resolved that to a specific script URL. | 64 // was used we may now have resolved that to a specific script URL. |
60 int Init(const ProxyConfig& config, | 65 int Init(const ProxyConfig& config, |
61 const base::TimeDelta wait_delay, | 66 const base::TimeDelta wait_delay, |
62 ProxyConfig* effective_config, | 67 ProxyConfig* effective_config, |
63 CompletionCallback* callback); | 68 CompletionCallback* callback); |
64 | 69 |
65 private: | 70 private: |
66 struct PacURL { | 71 // Represents the sources from which we can get PAC files; two types of |
67 PacURL(bool auto_detect, const GURL& url) | 72 // auto-detect or a custom URL. |
68 : auto_detect(auto_detect), url(url) {} | 73 struct PacSource { |
69 bool auto_detect; | 74 enum Type { |
70 GURL url; | 75 WPAD_DHCP, |
| 76 WPAD_DNS, |
| 77 CUSTOM |
| 78 }; |
| 79 |
| 80 PacSource(Type type, const GURL& url) |
| 81 : type(type), url(url) {} |
| 82 |
| 83 Type type; |
| 84 GURL url; // Empty unless |type == PAC_SOURCE_CUSTOM|. |
71 }; | 85 }; |
72 | 86 |
73 typedef std::vector<PacURL> UrlList; | 87 typedef std::vector<PacSource> PacSourceList; |
74 | 88 |
75 enum State { | 89 enum State { |
76 STATE_NONE, | 90 STATE_NONE, |
77 STATE_WAIT, | 91 STATE_WAIT, |
78 STATE_WAIT_COMPLETE, | 92 STATE_WAIT_COMPLETE, |
79 STATE_FETCH_PAC_SCRIPT, | 93 STATE_FETCH_PAC_SCRIPT, |
80 STATE_FETCH_PAC_SCRIPT_COMPLETE, | 94 STATE_FETCH_PAC_SCRIPT_COMPLETE, |
81 STATE_SET_PAC_SCRIPT, | 95 STATE_SET_PAC_SCRIPT, |
82 STATE_SET_PAC_SCRIPT_COMPLETE, | 96 STATE_SET_PAC_SCRIPT_COMPLETE, |
83 }; | 97 }; |
84 | 98 |
85 // Returns ordered list of PAC urls to try for |config|. | 99 // Returns ordered list of PAC urls to try for |config|. |
86 UrlList BuildPacUrlsFallbackList(const ProxyConfig& config) const; | 100 PacSourceList BuildPacSourcesFallbackList(const ProxyConfig& config) const; |
87 | 101 |
88 void OnIOCompletion(int result); | 102 void OnIOCompletion(int result); |
89 int DoLoop(int result); | 103 int DoLoop(int result); |
90 void DoCallback(int result); | 104 void DoCallback(int result); |
91 | 105 |
92 int DoWait(); | 106 int DoWait(); |
93 int DoWaitComplete(int result); | 107 int DoWaitComplete(int result); |
94 | 108 |
95 int DoFetchPacScript(); | 109 int DoFetchPacScript(); |
96 int DoFetchPacScriptComplete(int result); | 110 int DoFetchPacScriptComplete(int result); |
97 | 111 |
98 int DoSetPacScript(); | 112 int DoSetPacScript(); |
99 int DoSetPacScriptComplete(int result); | 113 int DoSetPacScriptComplete(int result); |
100 | 114 |
101 // Tries restarting using the next fallback PAC URL: | 115 // Tries restarting using the next fallback PAC URL: |
102 // |pac_urls_[++current_pac_url_index]|. | 116 // |pac_sources_[++current_pac_source_index]|. |
103 // Returns OK and rewinds the state machine when there | 117 // Returns OK and rewinds the state machine when there |
104 // is something to try, otherwise returns |error|. | 118 // is something to try, otherwise returns |error|. |
105 int TryToFallbackPacUrl(int error); | 119 int TryToFallbackPacSource(int error); |
106 | 120 |
107 // Gets the initial state (we skip fetching when the | 121 // Gets the initial state (we skip fetching when the |
108 // ProxyResolver doesn't |expect_pac_bytes()|. | 122 // ProxyResolver doesn't |expect_pac_bytes()|. |
109 State GetStartState() const; | 123 State GetStartState() const; |
110 | 124 |
| 125 NetLogStringParameter* CreateNetLogParameterAndDetermineURL( |
| 126 const PacSource& pac_source, GURL* effective_pac_url); |
| 127 |
111 // Returns the current PAC URL we are fetching/testing. | 128 // Returns the current PAC URL we are fetching/testing. |
112 const PacURL& current_pac_url() const; | 129 const PacSource& current_pac_source() const; |
113 | 130 |
114 void OnWaitTimerFired(); | 131 void OnWaitTimerFired(); |
115 void DidCompleteInit(); | 132 void DidCompleteInit(); |
116 void Cancel(); | 133 void Cancel(); |
117 | 134 |
118 ProxyResolver* resolver_; | 135 ProxyResolver* resolver_; |
119 ProxyScriptFetcher* proxy_script_fetcher_; | 136 ProxyScriptFetcher* proxy_script_fetcher_; |
| 137 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher_; |
120 | 138 |
121 CompletionCallbackImpl<InitProxyResolver> io_callback_; | 139 CompletionCallbackImpl<InitProxyResolver> io_callback_; |
122 CompletionCallback* user_callback_; | 140 CompletionCallback* user_callback_; |
123 | 141 |
124 size_t current_pac_url_index_; | 142 size_t current_pac_source_index_; |
125 | 143 |
126 // Filled when the PAC script fetch completes. | 144 // Filled when the PAC script fetch completes. |
127 string16 pac_script_; | 145 string16 pac_script_; |
128 | 146 |
129 // Flag indicating whether the caller requested a mandatory pac script | 147 // Flag indicating whether the caller requested a mandatory pac script |
130 // (i.e. fallback to direct connections are prohibited). | 148 // (i.e. fallback to direct connections are prohibited). |
131 bool pac_mandatory_; | 149 bool pac_mandatory_; |
132 | 150 |
133 UrlList pac_urls_; | 151 PacSourceList pac_sources_; |
134 State next_state_; | 152 State next_state_; |
135 | 153 |
136 BoundNetLog net_log_; | 154 BoundNetLog net_log_; |
137 | 155 |
138 base::TimeDelta wait_delay_; | 156 base::TimeDelta wait_delay_; |
139 base::OneShotTimer<InitProxyResolver> wait_timer_; | 157 base::OneShotTimer<InitProxyResolver> wait_timer_; |
140 | 158 |
141 ProxyConfig* effective_config_; | 159 ProxyConfig* effective_config_; |
142 | 160 |
143 DISALLOW_COPY_AND_ASSIGN(InitProxyResolver); | 161 DISALLOW_COPY_AND_ASSIGN(InitProxyResolver); |
144 }; | 162 }; |
145 | 163 |
146 } // namespace net | 164 } // namespace net |
147 | 165 |
148 #endif // NET_PROXY_INIT_PROXY_RESOLVER_H_ | 166 #endif // NET_PROXY_INIT_PROXY_RESOLVER_H_ |
OLD | NEW |