Chromium Code Reviews| 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_SCRIPT_DECIDER_H |
| 6 #define NET_PROXY_INIT_PROXY_RESOLVER_H_ | 6 #define NET_PROXY_SCRIPT_DECIDER_H |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "base/timer.h" | 14 #include "base/timer.h" |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
| 17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 19 #include "net/proxy/proxy_config.h" | |
| 20 #include "net/proxy/proxy_resolver.h" | |
| 19 | 21 |
| 20 namespace net { | 22 namespace net { |
| 21 | 23 |
| 22 class DhcpProxyScriptFetcher; | 24 class DhcpProxyScriptFetcher; |
| 23 class NetLogParameter; | 25 class NetLogParameter; |
| 24 class ProxyConfig; | |
| 25 class ProxyResolver; | 26 class ProxyResolver; |
| 26 class ProxyScriptFetcher; | 27 class ProxyScriptFetcher; |
| 27 | 28 |
| 28 // InitProxyResolver is a helper class used by ProxyService to | 29 // ProxyScriptDecider is a helper class used by ProxyService to determine which |
| 29 // initialize a ProxyResolver with the PAC script data specified | 30 // PAC script to use given our proxy configuration. |
| 30 // by a particular ProxyConfig. | |
| 31 // | 31 // |
| 32 // This involves trying to use PAC scripts in this order: | 32 // This involves trying to use PAC scripts in this order: |
| 33 // | 33 // |
| 34 // (1) WPAD (DNS) if auto-detect is on. | 34 // (1) WPAD (DHCP) if auto-detect is on. |
| 35 // (2) Custom PAC script if a URL was given. | 35 // (2) WPAD (DNS) if auto-detect is on. |
| 36 // (3) Custom PAC script if a URL was given. | |
| 36 // | 37 // |
| 37 // If no PAC script was successfully downloaded + parsed, then it fails with | 38 // If no PAC script was successfully selected, then it fails with either a |
| 38 // a network error. Otherwise the proxy resolver is left initialized with | 39 // network error, or PAC_SCRIPT_FAILED (indicating it did not pass our |
| 39 // the PAC script. | 40 // validation). |
| 40 // | 41 // |
| 41 // Deleting InitProxyResolver while Init() is in progress, will | 42 // On successful completion, the fetched PAC script data can be accessed using |
| 43 // script_data(). | |
| 44 // | |
| 45 // Deleting ProxyScriptDecider while Init() is in progress, will | |
| 42 // cancel the request. | 46 // cancel the request. |
| 43 // | 47 // |
| 44 class NET_EXPORT_PRIVATE InitProxyResolver { | 48 class NET_EXPORT_PRIVATE ProxyScriptDecider { |
| 45 public: | 49 public: |
| 46 // |resolver|, |proxy_script_fetcher|, |dhcp_proxy_script_fetcher| and | 50 // |proxy_script_fetcher|, |dhcp_proxy_script_fetcher| and |
| 47 // |net_log| must remain valid for the lifespan of InitProxyResolver. | 51 // |net_log| must remain valid for the lifespan of ProxyScriptDecider. |
| 48 InitProxyResolver(ProxyResolver* resolver, | 52 ProxyScriptDecider(ProxyScriptFetcher* proxy_script_fetcher, |
| 49 ProxyScriptFetcher* proxy_script_fetcher, | 53 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, |
| 50 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, | 54 NetLog* net_log); |
| 51 NetLog* net_log); | |
| 52 | 55 |
| 53 // Aborts any in-progress request. | 56 // Aborts any in-progress request. |
| 54 ~InitProxyResolver(); | 57 ~ProxyScriptDecider(); |
| 55 | 58 |
| 56 // Applies the PAC settings of |config| to |resolver_|. | 59 // Evaluates the effective proxy settings for |config|, and downloads the |
| 60 // associated PAC script. | |
| 57 // If |wait_delay| is positive, the initialization will pause for this | 61 // If |wait_delay| is positive, the initialization will pause for this |
| 58 // amount of time before getting started. | 62 // amount of time before getting started. |
| 59 // If |effective_config| is non-NULL, then on successful initialization of | 63 // On successful completion, the "effective" proxy settings we ended up |
| 60 // |resolver_| the "effective" proxy settings we ended up using will be | 64 // deciding on will be available vial the effective_settings() accessor. |
| 61 // written out to |*effective_config|. Note that this may differ from | 65 // Note that this may differ from |config| since we will have stripped any |
| 62 // |config| since we will have stripped any manual settings, and decided | 66 // manual settings, and decided whether to use auto-detect or the custom PAC |
| 63 // whether to use auto-detect or the custom PAC URL. Finally, if auto-detect | 67 // URL. Finally, if auto-detect was used we may now have resolved that to a |
| 64 // was used we may now have resolved that to a specific script URL. | 68 // specific script URL. |
| 65 int Init(const ProxyConfig& config, | 69 int Start(const ProxyConfig& config, |
| 66 const base::TimeDelta wait_delay, | 70 const base::TimeDelta wait_delay, |
| 67 ProxyConfig* effective_config, | 71 bool fetch_pac_bytes, |
| 68 OldCompletionCallback* callback); | 72 OldCompletionCallback* callback); |
| 73 | |
| 74 const ProxyConfig& effective_config() const { | |
| 75 DCHECK_EQ(STATE_NONE, next_state_); | |
| 76 return effective_config_; | |
| 77 } | |
| 78 | |
| 79 // TODO(eroman): Return a const-pointer. | |
|
willchan no longer on Chromium
2011/12/12 23:53:47
Is there a reason you didn't do this now? Is this
eroman
2011/12/13 00:10:26
Unfortunately it is non-trivial since one of the i
| |
| 80 ProxyResolverScriptData* script_data() const { | |
| 81 DCHECK_EQ(STATE_NONE, next_state_); | |
| 82 return script_data_.get(); | |
| 83 } | |
| 69 | 84 |
| 70 private: | 85 private: |
| 71 // Represents the sources from which we can get PAC files; two types of | 86 // Represents the sources from which we can get PAC files; two types of |
| 72 // auto-detect or a custom URL. | 87 // auto-detect or a custom URL. |
| 73 struct PacSource { | 88 struct PacSource { |
| 74 enum Type { | 89 enum Type { |
| 75 WPAD_DHCP, | 90 WPAD_DHCP, |
| 76 WPAD_DNS, | 91 WPAD_DNS, |
| 77 CUSTOM | 92 CUSTOM |
| 78 }; | 93 }; |
| 79 | 94 |
| 80 PacSource(Type type, const GURL& url) | 95 PacSource(Type type, const GURL& url) |
| 81 : type(type), url(url) {} | 96 : type(type), url(url) {} |
| 82 | 97 |
| 83 Type type; | 98 Type type; |
| 84 GURL url; // Empty unless |type == PAC_SOURCE_CUSTOM|. | 99 GURL url; // Empty unless |type == PAC_SOURCE_CUSTOM|. |
| 85 }; | 100 }; |
| 86 | 101 |
| 87 typedef std::vector<PacSource> PacSourceList; | 102 typedef std::vector<PacSource> PacSourceList; |
| 88 | 103 |
| 89 enum State { | 104 enum State { |
| 90 STATE_NONE, | 105 STATE_NONE, |
| 91 STATE_WAIT, | 106 STATE_WAIT, |
| 92 STATE_WAIT_COMPLETE, | 107 STATE_WAIT_COMPLETE, |
| 93 STATE_FETCH_PAC_SCRIPT, | 108 STATE_FETCH_PAC_SCRIPT, |
| 94 STATE_FETCH_PAC_SCRIPT_COMPLETE, | 109 STATE_FETCH_PAC_SCRIPT_COMPLETE, |
| 95 STATE_SET_PAC_SCRIPT, | 110 STATE_VERIFY_PAC_SCRIPT, // (Synchronous step). |
| 96 STATE_SET_PAC_SCRIPT_COMPLETE, | |
| 97 }; | 111 }; |
| 98 | 112 |
| 99 // Returns ordered list of PAC urls to try for |config|. | 113 // Returns ordered list of PAC urls to try for |config|. |
| 100 PacSourceList BuildPacSourcesFallbackList(const ProxyConfig& config) const; | 114 PacSourceList BuildPacSourcesFallbackList(const ProxyConfig& config) const; |
| 101 | 115 |
| 102 void OnIOCompletion(int result); | 116 void OnIOCompletion(int result); |
| 103 int DoLoop(int result); | 117 int DoLoop(int result); |
| 104 void DoCallback(int result); | 118 void DoCallback(int result); |
| 105 | 119 |
| 106 int DoWait(); | 120 int DoWait(); |
| 107 int DoWaitComplete(int result); | 121 int DoWaitComplete(int result); |
| 108 | 122 |
| 109 int DoFetchPacScript(); | 123 int DoFetchPacScript(); |
| 110 int DoFetchPacScriptComplete(int result); | 124 int DoFetchPacScriptComplete(int result); |
| 111 | 125 |
| 112 int DoSetPacScript(); | 126 int DoVerifyPacScript(); |
| 113 int DoSetPacScriptComplete(int result); | |
| 114 | 127 |
| 115 // Tries restarting using the next fallback PAC URL: | 128 // Tries restarting using the next fallback PAC URL: |
| 116 // |pac_sources_[++current_pac_source_index]|. | 129 // |pac_sources_[++current_pac_source_index]|. |
| 117 // Returns OK and rewinds the state machine when there | 130 // Returns OK and rewinds the state machine when there |
| 118 // is something to try, otherwise returns |error|. | 131 // is something to try, otherwise returns |error|. |
| 119 int TryToFallbackPacSource(int error); | 132 int TryToFallbackPacSource(int error); |
| 120 | 133 |
| 121 // Gets the initial state (we skip fetching when the | 134 // Gets the initial state (we skip fetching when the |
| 122 // ProxyResolver doesn't |expect_pac_bytes()|. | 135 // ProxyResolver doesn't |expect_pac_bytes()|. |
| 123 State GetStartState() const; | 136 State GetStartState() const; |
| 124 | 137 |
| 125 NetLogStringParameter* CreateNetLogParameterAndDetermineURL( | 138 NetLogStringParameter* CreateNetLogParameterAndDetermineURL( |
| 126 const PacSource& pac_source, GURL* effective_pac_url); | 139 const PacSource& pac_source, GURL* effective_pac_url); |
| 127 | 140 |
| 128 // Returns the current PAC URL we are fetching/testing. | 141 // Returns the current PAC URL we are fetching/testing. |
| 129 const PacSource& current_pac_source() const; | 142 const PacSource& current_pac_source() const; |
| 130 | 143 |
| 131 void OnWaitTimerFired(); | 144 void OnWaitTimerFired(); |
| 132 void DidCompleteInit(); | 145 void DidComplete(); |
| 133 void Cancel(); | 146 void Cancel(); |
| 134 | 147 |
| 135 ProxyResolver* resolver_; | 148 ProxyResolver* resolver_; |
| 136 ProxyScriptFetcher* proxy_script_fetcher_; | 149 ProxyScriptFetcher* proxy_script_fetcher_; |
| 137 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher_; | 150 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher_; |
| 138 | 151 |
| 139 OldCompletionCallbackImpl<InitProxyResolver> io_callback_; | 152 OldCompletionCallbackImpl<ProxyScriptDecider> io_callback_; |
| 140 OldCompletionCallback* user_callback_; | 153 OldCompletionCallback* user_callback_; |
| 141 | 154 |
| 142 size_t current_pac_source_index_; | 155 size_t current_pac_source_index_; |
| 143 | 156 |
| 144 // Filled when the PAC script fetch completes. | 157 // Filled when the PAC script fetch completes. |
| 145 string16 pac_script_; | 158 string16 pac_script_; |
| 146 | 159 |
| 147 // Flag indicating whether the caller requested a mandatory pac script | 160 // Flag indicating whether the caller requested a mandatory pac script |
| 148 // (i.e. fallback to direct connections are prohibited). | 161 // (i.e. fallback to direct connections are prohibited). |
| 149 bool pac_mandatory_; | 162 bool pac_mandatory_; |
| 150 | 163 |
| 151 PacSourceList pac_sources_; | 164 PacSourceList pac_sources_; |
| 152 State next_state_; | 165 State next_state_; |
| 153 | 166 |
| 154 BoundNetLog net_log_; | 167 BoundNetLog net_log_; |
| 155 | 168 |
| 169 bool fetch_pac_bytes_; | |
| 170 | |
| 156 base::TimeDelta wait_delay_; | 171 base::TimeDelta wait_delay_; |
| 157 base::OneShotTimer<InitProxyResolver> wait_timer_; | 172 base::OneShotTimer<ProxyScriptDecider> wait_timer_; |
| 158 | 173 |
| 159 ProxyConfig* effective_config_; | 174 // Results. |
| 175 ProxyConfig effective_config_; | |
| 176 scoped_refptr<ProxyResolverScriptData> script_data_; | |
| 160 | 177 |
| 161 DISALLOW_COPY_AND_ASSIGN(InitProxyResolver); | 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(ProxyScriptDecider); | |
| 162 }; | 180 }; |
| 163 | 181 |
| 164 } // namespace net | 182 } // namespace net |
| 165 | 183 |
| 166 #endif // NET_PROXY_INIT_PROXY_RESOLVER_H_ | 184 #endif // NET_PROXY_SCRIPT_DECIDER_H |
| OLD | NEW |