Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: net/proxy/init_proxy_resolver.h

Issue 6831025: Adds support for the DHCP portion of the WPAD (proxy auto-discovery) protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add timeout on Win32 DHCP API. Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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;
20 class ProxyConfig; 22 class ProxyConfig;
21 class ProxyResolver; 23 class ProxyResolver;
22 class ProxyScriptFetcher; 24 class URLProxyScriptFetcher;
25 class URLRequestContext;
23 26
24 // InitProxyResolver is a helper class used by ProxyService to 27 // InitProxyResolver is a helper class used by ProxyService to
25 // initialize a ProxyResolver with the PAC script data specified 28 // initialize a ProxyResolver with the PAC script data specified
26 // by a particular ProxyConfig. 29 // by a particular ProxyConfig.
27 // 30 //
28 // This involves trying to use PAC scripts in this order: 31 // This involves trying to use PAC scripts in this order:
29 // 32 //
30 // (1) WPAD (DNS) if auto-detect is on. 33 // (1) WPAD (DNS) if auto-detect is on.
31 // (2) Custom PAC script if a URL was given. 34 // (2) Custom PAC script if a URL was given.
32 // 35 //
33 // If no PAC script was successfully downloaded + parsed, then it fails with 36 // If no PAC script was successfully downloaded + parsed, then it fails with
34 // a network error. Otherwise the proxy resolver is left initialized with 37 // a network error. Otherwise the proxy resolver is left initialized with
35 // the PAC script. 38 // the PAC script.
36 // 39 //
37 // Deleting InitProxyResolver while Init() is in progress, will 40 // Deleting InitProxyResolver while Init() is in progress, will
38 // cancel the request. 41 // cancel the request.
39 // 42 //
40 class InitProxyResolver { 43 class InitProxyResolver {
41 public: 44 public:
42 // |resolver|, |proxy_script_fetcher| and |net_log| must remain valid for 45 // |resolver|, |proxy_script_fetcher| and |net_log| must remain valid for
43 // the lifespan of InitProxyResolver. 46 // the lifespan of InitProxyResolver.
44 InitProxyResolver(ProxyResolver* resolver, 47 InitProxyResolver(ProxyResolver* resolver,
45 ProxyScriptFetcher* proxy_script_fetcher, 48 URLProxyScriptFetcher* proxy_script_fetcher,
46 NetLog* net_log); 49 NetLog* net_log);
47 50
48 // Aborts any in-progress request. 51 // Aborts any in-progress request.
49 ~InitProxyResolver(); 52 virtual ~InitProxyResolver();
50 53
51 // Applies the PAC settings of |config| to |resolver_|. 54 // Applies the PAC settings of |config| to |resolver_|.
52 // If |wait_delay| is positive, the initialization will pause for this 55 // If |wait_delay| is positive, the initialization will pause for this
53 // amount of time before getting started. 56 // amount of time before getting started.
54 // If |effective_config| is non-NULL, then on successful initialization of 57 // If |effective_config| is non-NULL, then on successful initialization of
55 // |resolver_| the "effective" proxy settings we ended up using will be 58 // |resolver_| the "effective" proxy settings we ended up using will be
56 // written out to |*effective_config|. Note that this may differ from 59 // written out to |*effective_config|. Note that this may differ from
57 // |config| since we will have stripped any manual settings, and decided 60 // |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 61 // 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. 62 // was used we may now have resolved that to a specific script URL.
60 int Init(const ProxyConfig& config, 63 int Init(const ProxyConfig& config,
61 const base::TimeDelta wait_delay, 64 const base::TimeDelta wait_delay,
62 ProxyConfig* effective_config, 65 ProxyConfig* effective_config,
63 CompletionCallback* callback); 66 CompletionCallback* callback);
64 67
68 protected:
69 // Method introduced to let unit tests stub out dependencies.
70 virtual DhcpProxyScriptFetcher* ImplCreateDhcpProxyScriptFetcher(
eroman 2011/04/21 05:22:48 I don't like using virtuals in this manner, can yo
Jói 2011/05/03 21:20:59 I've switched to using dependency injection in Ini
71 URLRequestContext* url_request_context);
72
65 private: 73 private:
66 struct PacURL { 74 // Represents the sources from which we can get PAC files; two types of
67 PacURL(bool auto_detect, const GURL& url) 75 // auto-detect or a custom URL.
68 : auto_detect(auto_detect), url(url) {} 76 struct PacSource {
69 bool auto_detect; 77 enum Type {
70 GURL url; 78 WPAD_DHCP,
79 WPAD_DNS,
80 CUSTOM
81 };
82
83 PacSource(Type type, const GURL& url)
84 : type(type), url(url) {}
85
86 Type type;
87 GURL url; // Empty unless |type == PAC_SOURCE_CUSTOM|.
71 }; 88 };
72 89
73 typedef std::vector<PacURL> UrlList; 90 typedef std::vector<PacSource> PacSourceList;
74 91
75 enum State { 92 enum State {
76 STATE_NONE, 93 STATE_NONE,
77 STATE_WAIT, 94 STATE_WAIT,
78 STATE_WAIT_COMPLETE, 95 STATE_WAIT_COMPLETE,
79 STATE_FETCH_PAC_SCRIPT, 96 STATE_FETCH_PAC_SCRIPT,
80 STATE_FETCH_PAC_SCRIPT_COMPLETE, 97 STATE_FETCH_PAC_SCRIPT_COMPLETE,
81 STATE_SET_PAC_SCRIPT, 98 STATE_SET_PAC_SCRIPT,
82 STATE_SET_PAC_SCRIPT_COMPLETE, 99 STATE_SET_PAC_SCRIPT_COMPLETE,
83 }; 100 };
84 101
85 // Returns ordered list of PAC urls to try for |config|. 102 // Returns ordered list of PAC urls to try for |config|.
86 UrlList BuildPacUrlsFallbackList(const ProxyConfig& config) const; 103 PacSourceList BuildPacSourcesFallbackList(const ProxyConfig& config) const;
87 104
88 void OnIOCompletion(int result); 105 void OnIOCompletion(int result);
89 int DoLoop(int result); 106 int DoLoop(int result);
90 void DoCallback(int result); 107 void DoCallback(int result);
91 108
92 int DoWait(); 109 int DoWait();
93 int DoWaitComplete(int result); 110 int DoWaitComplete(int result);
94 111
95 int DoFetchPacScript(); 112 int DoFetchPacScript();
96 int DoFetchPacScriptComplete(int result); 113 int DoFetchPacScriptComplete(int result);
97 114
98 int DoSetPacScript(); 115 int DoSetPacScript();
99 int DoSetPacScriptComplete(int result); 116 int DoSetPacScriptComplete(int result);
100 117
101 // Tries restarting using the next fallback PAC URL: 118 // Tries restarting using the next fallback PAC URL:
102 // |pac_urls_[++current_pac_url_index]|. 119 // |pac_sources_[++current_pac_source_index]|.
103 // Returns OK and rewinds the state machine when there 120 // Returns OK and rewinds the state machine when there
104 // is something to try, otherwise returns |error|. 121 // is something to try, otherwise returns |error|.
105 int TryToFallbackPacUrl(int error); 122 int TryToFallbackPacSource(int error);
106 123
107 // Gets the initial state (we skip fetching when the 124 // Gets the initial state (we skip fetching when the
108 // ProxyResolver doesn't |expect_pac_bytes()|. 125 // ProxyResolver doesn't |expect_pac_bytes()|.
109 State GetStartState() const; 126 State GetStartState() const;
110 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 URLProxyScriptFetcher* proxy_script_fetcher_;
137 scoped_ptr<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 UrlList pac_urls_; 147 PacSourceList pac_sources_;
130 State next_state_; 148 State next_state_;
131 149
132 BoundNetLog net_log_; 150 BoundNetLog net_log_;
133 151
134 base::TimeDelta wait_delay_; 152 base::TimeDelta wait_delay_;
135 base::OneShotTimer<InitProxyResolver> wait_timer_; 153 base::OneShotTimer<InitProxyResolver> wait_timer_;
136 154
137 ProxyConfig* effective_config_; 155 ProxyConfig* effective_config_;
138 156
139 DISALLOW_COPY_AND_ASSIGN(InitProxyResolver); 157 DISALLOW_COPY_AND_ASSIGN(InitProxyResolver);
140 }; 158 };
141 159
142 } // namespace net 160 } // namespace net
143 161
144 #endif // NET_PROXY_INIT_PROXY_RESOLVER_H_ 162 #endif // NET_PROXY_INIT_PROXY_RESOLVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698