Removes placeholder message string for aria role option
[chromium-blink-merge.git] / net / proxy / proxy_resolver_winhttp.cc
blobd52ceb2be56626c68d04ae54ad4fe4f6fd2cd6cd
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.
5 #include "net/proxy/proxy_resolver_winhttp.h"
7 #include <windows.h>
8 #include <winhttp.h>
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "net/base/net_errors.h"
13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver.h"
15 #include "url/gurl.h"
17 #pragma comment(lib, "winhttp.lib")
19 using base::TimeDelta;
20 using base::TimeTicks;
22 namespace net {
23 namespace {
25 static void FreeInfo(WINHTTP_PROXY_INFO* info) {
26 if (info->lpszProxy)
27 GlobalFree(info->lpszProxy);
28 if (info->lpszProxyBypass)
29 GlobalFree(info->lpszProxyBypass);
32 class ProxyResolverWinHttp : public ProxyResolver {
33 public:
34 ProxyResolverWinHttp(
35 const scoped_refptr<ProxyResolverScriptData>& script_data);
36 ~ProxyResolverWinHttp() override;
38 // ProxyResolver implementation:
39 int GetProxyForURL(const GURL& url,
40 ProxyInfo* results,
41 const CompletionCallback& /*callback*/,
42 RequestHandle* /*request*/,
43 const BoundNetLog& /*net_log*/) override;
44 void CancelRequest(RequestHandle request) override;
46 LoadState GetLoadState(RequestHandle request) const override;
48 private:
49 bool OpenWinHttpSession();
50 void CloseWinHttpSession();
52 // Proxy configuration is cached on the session handle.
53 HINTERNET session_handle_;
55 const GURL pac_url_;
57 DISALLOW_COPY_AND_ASSIGN(ProxyResolverWinHttp);
60 ProxyResolverWinHttp::ProxyResolverWinHttp(
61 const scoped_refptr<ProxyResolverScriptData>& script_data)
62 : session_handle_(NULL),
63 pac_url_(script_data->type() == ProxyResolverScriptData::TYPE_AUTO_DETECT
64 ? GURL("http://wpad/wpad.dat")
65 : script_data->url()) {
68 ProxyResolverWinHttp::~ProxyResolverWinHttp() {
69 CloseWinHttpSession();
72 int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url,
73 ProxyInfo* results,
74 const CompletionCallback& /*callback*/,
75 RequestHandle* /*request*/,
76 const BoundNetLog& /*net_log*/) {
77 // If we don't have a WinHTTP session, then create a new one.
78 if (!session_handle_ && !OpenWinHttpSession())
79 return ERR_FAILED;
81 // If we have been given an empty PAC url, then use auto-detection.
83 // NOTE: We just use DNS-based auto-detection here like Firefox. We do this
84 // to avoid WinHTTP's auto-detection code, which while more featureful (it
85 // supports DHCP based auto-detection) also appears to have issues.
87 WINHTTP_AUTOPROXY_OPTIONS options = {0};
88 options.fAutoLogonIfChallenged = FALSE;
89 options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
90 base::string16 pac_url16 = base::ASCIIToUTF16(pac_url_.spec());
91 options.lpszAutoConfigUrl = pac_url16.c_str();
93 WINHTTP_PROXY_INFO info = {0};
94 DCHECK(session_handle_);
96 // Per http://msdn.microsoft.com/en-us/library/aa383153(VS.85).aspx, it is
97 // necessary to first try resolving with fAutoLogonIfChallenged set to false.
98 // Otherwise, we fail over to trying it with a value of true. This way we
99 // get good performance in the case where WinHTTP uses an out-of-process
100 // resolver. This is important for Vista and Win2k3.
101 BOOL ok = WinHttpGetProxyForUrl(session_handle_,
102 base::ASCIIToUTF16(query_url.spec()).c_str(),
103 &options, &info);
104 if (!ok) {
105 if (ERROR_WINHTTP_LOGIN_FAILURE == GetLastError()) {
106 options.fAutoLogonIfChallenged = TRUE;
107 ok = WinHttpGetProxyForUrl(
108 session_handle_, base::ASCIIToUTF16(query_url.spec()).c_str(),
109 &options, &info);
111 if (!ok) {
112 DWORD error = GetLastError();
113 // If we got here because of RPC timeout during out of process PAC
114 // resolution, no further requests on this session are going to work.
115 if (ERROR_WINHTTP_TIMEOUT == error ||
116 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR == error) {
117 CloseWinHttpSession();
119 return ERR_FAILED; // TODO(darin): Bug 1189288: translate error code.
123 int rv = OK;
125 switch (info.dwAccessType) {
126 case WINHTTP_ACCESS_TYPE_NO_PROXY:
127 results->UseDirect();
128 break;
129 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
130 // According to MSDN:
132 // The proxy server list contains one or more of the following strings
133 // separated by semicolons or whitespace.
135 // ([<scheme>=][<scheme>"://"]<server>[":"<port>])
137 // Based on this description, ProxyInfo::UseNamedProxy() isn't
138 // going to handle all the variations (in particular <scheme>=).
140 // However in practice, it seems that WinHTTP is simply returning
141 // things like "foopy1:80;foopy2:80". It strips out the non-HTTP
142 // proxy types, and stops the list when PAC encounters a "DIRECT".
143 // So UseNamedProxy() should work OK.
144 results->UseNamedProxy(base::UTF16ToASCII(info.lpszProxy));
145 break;
146 default:
147 NOTREACHED();
148 rv = ERR_FAILED;
151 FreeInfo(&info);
152 return rv;
155 void ProxyResolverWinHttp::CancelRequest(RequestHandle request) {
156 // This is a synchronous ProxyResolver; no possibility for async requests.
157 NOTREACHED();
160 LoadState ProxyResolverWinHttp::GetLoadState(RequestHandle request) const {
161 NOTREACHED();
162 return LOAD_STATE_IDLE;
165 bool ProxyResolverWinHttp::OpenWinHttpSession() {
166 DCHECK(!session_handle_);
167 session_handle_ = WinHttpOpen(NULL,
168 WINHTTP_ACCESS_TYPE_NO_PROXY,
169 WINHTTP_NO_PROXY_NAME,
170 WINHTTP_NO_PROXY_BYPASS,
172 if (!session_handle_)
173 return false;
175 // Since this session handle will never be used for WinHTTP connections,
176 // these timeouts don't really mean much individually. However, WinHTTP's
177 // out of process PAC resolution will use a combined (sum of all timeouts)
178 // value to wait for an RPC reply.
179 BOOL rv = WinHttpSetTimeouts(session_handle_, 10000, 10000, 5000, 5000);
180 DCHECK(rv);
182 return true;
185 void ProxyResolverWinHttp::CloseWinHttpSession() {
186 if (session_handle_) {
187 WinHttpCloseHandle(session_handle_);
188 session_handle_ = NULL;
192 } // namespace
194 ProxyResolverFactoryWinHttp::ProxyResolverFactoryWinHttp()
195 : ProxyResolverFactory(false /*expects_pac_bytes*/) {
198 int ProxyResolverFactoryWinHttp::CreateProxyResolver(
199 const scoped_refptr<ProxyResolverScriptData>& pac_script,
200 scoped_ptr<ProxyResolver>* resolver,
201 const CompletionCallback& callback,
202 scoped_ptr<Request>* request) {
203 resolver->reset(new ProxyResolverWinHttp(pac_script));
204 return OK;
207 } // namespace net