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

Side by Side Diff: net/proxy/dhcp_proxy_script_fetcher_win.cc

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: Responding to review comments. Created 9 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/proxy/dhcp_proxy_script_fetcher_win.h"
6
7 #include "net/base/net_errors.h"
8 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h"
9
10 #include <winsock2.h>
11 #include <iphlpapi.h>
12 #pragma comment(lib, "iphlpapi.lib")
13
14 namespace {
15
16 // How long to wait at maximum after we get results (a PAC file or
17 // knowledge that no PAC file is configured) from whichever network
18 // adapter finishes first.
19 const int kMaxWaitAfterFirstResultMs = 400;
20
21 } // namespace
22
23 namespace net {
24
25 WindowsDhcpProxyScriptFetcher::WindowsDhcpProxyScriptFetcher(
26 URLRequestContext* url_request_context)
27 : state_(STATE_START),
28 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_callback_(
29 this, &WindowsDhcpProxyScriptFetcher::OnFetcherDone)),
30 num_pending_fetchers_(0),
31 url_request_context_(url_request_context) {
32 DCHECK(url_request_context_);
33 }
34
35 WindowsDhcpProxyScriptFetcher::~WindowsDhcpProxyScriptFetcher() {
36 Cancel();
37 }
38
39 int WindowsDhcpProxyScriptFetcher::Fetch(string16* utf16_text,
40 CompletionCallback* callback) {
41 DCHECK(CalledOnValidThread());
42 if (state_ != STATE_START && state_ != STATE_DONE) {
43 NOTREACHED();
44 return ERR_UNEXPECTED;
45 }
46
47 std::set<std::string> adapter_names;
48 if (!ImplGetCandidateAdapterNames(&adapter_names)) {
49 return ERR_UNEXPECTED;
50 }
51 if (adapter_names.empty()) {
52 return ERR_PAC_NOT_IN_DHCP;
53 }
54
55 state_ = STATE_NO_RESULTS;
56
57 client_callback_ = callback;
58 destination_string_ = utf16_text;
59
60 for (std::set<std::string>::iterator it = adapter_names.begin();
61 it != adapter_names.end();
62 ++it) {
63 scoped_refptr<DhcpProxyScriptAdapterFetcher> fetcher(
64 ImplCreateAdapterFetcher());
65 fetcher->Fetch(*it, &fetcher_callback_);
66 fetchers_.push_back(fetcher);
67 }
68 num_pending_fetchers_ = fetchers_.size();
69
70 return ERR_IO_PENDING;
71 }
72
73 void WindowsDhcpProxyScriptFetcher::Cancel() {
74 DCHECK(CalledOnValidThread());
75 CancelImpl(true);
76 }
77
78 URLRequestContext* WindowsDhcpProxyScriptFetcher::GetRequestContext() const {
79 DCHECK(CalledOnValidThread());
80 return url_request_context_;
81 }
82
83 std::string WindowsDhcpProxyScriptFetcher::GetFetcherName() const {
84 DCHECK(CalledOnValidThread());
85 return "win";
86 }
87
88 GURL WindowsDhcpProxyScriptFetcher::GetPacURL() const {
89 DCHECK(CalledOnValidThread());
90 DCHECK(state_ == STATE_DONE);
91
92 return pac_url_;
93 }
94
95 void WindowsDhcpProxyScriptFetcher::CancelImpl(bool clear_fetchers) {
96 if (state_ != STATE_DONE) {
97 wait_timer_.Stop();
98 state_ = STATE_DONE;
99
100 for (FetcherList::iterator it = fetchers_.begin();
101 it != fetchers_.end();
102 ++it) {
103 (*it)->Cancel();
104 }
105
106 if (clear_fetchers) {
107 fetchers_.clear();
108 }
109 }
110 }
111
112 void WindowsDhcpProxyScriptFetcher::OnFetcherDone(int result) {
113 DCHECK(state_ == STATE_NO_RESULTS || state_ == STATE_SOME_RESULTS);
114
115 if (--num_pending_fetchers_ == 0) {
116 TransitionToDone();
117 return;
118 }
119
120 // If the only pending adapters are those less preferred than one
121 // with a valid PAC script, we do not need to wait any longer.
122 for (FetcherList::iterator it = fetchers_.begin();
123 it != fetchers_.end();
124 ++it) {
125 bool did_finish = (*it)->DidFinish();
126 int result = (*it)->result();
127 if (did_finish && result == OK) {
128 TransitionToDone();
129 return;
130 }
131 if (!did_finish || result != ERR_PAC_NOT_IN_DHCP) {
132 break;
133 }
134 }
135
136 // Once we have a single result, we set a maximum on how long to wait
137 // for the rest of the results.
138 if (state_ == STATE_NO_RESULTS) {
139 state_ = STATE_SOME_RESULTS;
140 wait_timer_.Start(
141 base::TimeDelta::FromMilliseconds(ImplGetMaxWaitMs()),
142 this, &WindowsDhcpProxyScriptFetcher::OnWaitTimer);
143 }
144 }
145
146 void WindowsDhcpProxyScriptFetcher::OnWaitTimer() {
147 DCHECK(state_ == STATE_SOME_RESULTS);
eroman 2011/05/13 05:03:32 DCHECK_EQ
Jói 2011/05/13 20:19:09 Done.
148 TransitionToDone();
149 }
150
151 void WindowsDhcpProxyScriptFetcher::TransitionToDone() {
152 DCHECK(state_ == STATE_NO_RESULTS || state_ == STATE_SOME_RESULTS);
153
154 // This does not alter state for fetchers that are already done.
155 CancelImpl(false);
156
157 // Should have returned immediately at Fetch() if no adapters to check.
158 DCHECK(!fetchers_.empty());
159
160 // Scan twice for the result; once through the whole list for success,
161 // then if no success, return result for most preferred network adapter,
162 // preferring "real" network errors to the ERR_PAC_NOT_IN_DHCP error.
163 // Default to ERR_ABORTED if no fetcher completed.
164 int result = ERR_ABORTED;
165 for (FetcherList::iterator it = fetchers_.begin();
166 it != fetchers_.end();
167 ++it) {
168 if ((*it)->DidFinish() && (*it)->result() == OK) {
169 result = OK;
170 *destination_string_ = (*it)->GetPacScript();
171 pac_url_ = (*it)->GetPacURL();
172 break;
173 }
174 }
175 if (result != OK) {
176 destination_string_->clear();
177 for (FetcherList::iterator it = fetchers_.begin();
178 it != fetchers_.end();
179 ++it) {
180 if ((*it)->DidFinish()) {
181 result = (*it)->result();
182 if (result != ERR_PAC_NOT_IN_DHCP) {
183 break;
184 }
185 }
186 }
187 }
188
189 client_callback_->Run(result);
190 fetchers_.clear();
191 state_ = STATE_DONE;
192 }
193
194 DhcpProxyScriptAdapterFetcher*
195 WindowsDhcpProxyScriptFetcher::ImplCreateAdapterFetcher() {
196 return new DhcpProxyScriptAdapterFetcher(url_request_context_);
197 }
198
199 bool WindowsDhcpProxyScriptFetcher::ImplGetCandidateAdapterNames(
200 std::set<std::string>* adapter_names) {
201 return GetCandidateAdapterNames(adapter_names);
202 }
203
204 int WindowsDhcpProxyScriptFetcher::ImplGetMaxWaitMs() {
205 return kMaxWaitAfterFirstResultMs;
206 }
207
208
209 bool WindowsDhcpProxyScriptFetcher::GetCandidateAdapterNames(
210 std::set<std::string>* adapter_names) {
211 DCHECK(adapter_names);
212 adapter_names->clear();
213
214 // The GetAdaptersAddresses MSDN page recommends using a size of 15000 to
215 // avoid reallocation.
216 ULONG adapters_size = 15000;
217 scoped_ptr_malloc<IP_ADAPTER_ADDRESSES> adapters;
218 ULONG error = ERROR_SUCCESS;
219 int num_tries = 0;
220 do {
221 adapters.reset(
222 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(malloc(adapters_size)));
223 // Return only unicast addresses, and skip information we do not need.
224 error = GetAdaptersAddresses(AF_UNSPEC,
225 GAA_FLAG_SKIP_ANYCAST |
226 GAA_FLAG_SKIP_MULTICAST |
227 GAA_FLAG_SKIP_DNS_SERVER |
228 GAA_FLAG_SKIP_FRIENDLY_NAME,
229 NULL,
230 adapters.get(),
231 &adapters_size);
232 ++num_tries;
233 } while (error == ERROR_BUFFER_OVERFLOW && num_tries <= 3);
234
235 if (error == ERROR_NO_DATA) {
236 // There are no adapters that we care about.
237 return true;
238 }
239
240 if (error != ERROR_SUCCESS) {
241 NOTREACHED();
eroman 2011/05/13 05:03:32 Rather than a NOTREACHED(), how about logging this
Jói 2011/05/13 20:19:09 Done.
242 return false;
243 }
244
245 IP_ADAPTER_ADDRESSES* adapter = NULL;
246 for (adapter = adapters.get(); adapter; adapter = adapter->Next) {
247 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
248 continue;
249 if ((adapter->Flags & IP_ADAPTER_DHCP_ENABLED) == 0)
250 continue;
251
252 DCHECK(adapter->AdapterName);
253 adapter_names->insert(adapter->AdapterName);
254 }
255
256 return true;
257 }
258
259 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698