This source file includes following definitions.
- url_request_context_
- Cancel
- DidFinish
- GetResult
- GetPacScript
- GetPacURL
- GetPacURLForAdapter
- url
- ImplGetPacURLFromDhcp
- OnDhcpQueryDone
- OnTimeout
- OnFetcherDone
- TransitionToFinish
- state
- ImplCreateScriptFetcher
- ImplCreateDhcpQuery
- ImplGetTimeout
- GetPacURLFromDhcp
- SanitizeDhcpApiString
#include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task_runner.h"
#include "base/time/time.h"
#include "net/base/net_errors.h"
#include "net/proxy/dhcpcsvc_init_win.h"
#include "net/proxy/proxy_script_fetcher_impl.h"
#include "net/url_request/url_request_context.h"
#include <windows.h>
#include <winsock2.h>
#include <dhcpcsdk.h>
#pragma comment(lib, "dhcpcsvc.lib")
namespace {
const int kTimeoutMs = 2000;
}
namespace net {
DhcpProxyScriptAdapterFetcher::DhcpProxyScriptAdapterFetcher(
URLRequestContext* url_request_context,
scoped_refptr<base::TaskRunner> task_runner)
: task_runner_(task_runner),
state_(STATE_START),
result_(ERR_IO_PENDING),
url_request_context_(url_request_context) {
DCHECK(url_request_context_);
}
DhcpProxyScriptAdapterFetcher::~DhcpProxyScriptAdapterFetcher() {
Cancel();
}
void DhcpProxyScriptAdapterFetcher::Fetch(
const std::string& adapter_name, const CompletionCallback& callback) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(state_, STATE_START);
result_ = ERR_IO_PENDING;
pac_script_ = base::string16();
state_ = STATE_WAIT_DHCP;
callback_ = callback;
wait_timer_.Start(FROM_HERE, ImplGetTimeout(),
this, &DhcpProxyScriptAdapterFetcher::OnTimeout);
scoped_refptr<DhcpQuery> dhcp_query(ImplCreateDhcpQuery());
task_runner_->PostTaskAndReply(
FROM_HERE,
base::Bind(
&DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter,
dhcp_query.get(),
adapter_name),
base::Bind(
&DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone,
AsWeakPtr(),
dhcp_query));
}
void DhcpProxyScriptAdapterFetcher::Cancel() {
DCHECK(CalledOnValidThread());
callback_.Reset();
wait_timer_.Stop();
script_fetcher_.reset();
switch (state_) {
case STATE_WAIT_DHCP:
break;
case STATE_WAIT_URL:
break;
case STATE_START:
case STATE_FINISH:
case STATE_CANCEL:
break;
}
if (state_ != STATE_FINISH) {
result_ = ERR_ABORTED;
state_ = STATE_CANCEL;
}
}
bool DhcpProxyScriptAdapterFetcher::DidFinish() const {
DCHECK(CalledOnValidThread());
return state_ == STATE_FINISH;
}
int DhcpProxyScriptAdapterFetcher::GetResult() const {
DCHECK(CalledOnValidThread());
return result_;
}
base::string16 DhcpProxyScriptAdapterFetcher::GetPacScript() const {
DCHECK(CalledOnValidThread());
return pac_script_;
}
GURL DhcpProxyScriptAdapterFetcher::GetPacURL() const {
DCHECK(CalledOnValidThread());
return pac_url_;
}
DhcpProxyScriptAdapterFetcher::DhcpQuery::DhcpQuery() {
}
DhcpProxyScriptAdapterFetcher::DhcpQuery::~DhcpQuery() {
}
void DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter(
const std::string& adapter_name) {
url_ = ImplGetPacURLFromDhcp(adapter_name);
}
const std::string& DhcpProxyScriptAdapterFetcher::DhcpQuery::url() const {
return url_;
}
std::string
DhcpProxyScriptAdapterFetcher::DhcpQuery::ImplGetPacURLFromDhcp(
const std::string& adapter_name) {
return DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp(adapter_name);
}
void DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone(
scoped_refptr<DhcpQuery> dhcp_query) {
DCHECK(CalledOnValidThread());
DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_CANCEL ||
state_ == STATE_FINISH);
if (state_ != STATE_WAIT_DHCP)
return;
wait_timer_.Stop();
pac_url_ = GURL(dhcp_query->url());
if (pac_url_.is_empty() || !pac_url_.is_valid()) {
result_ = ERR_PAC_NOT_IN_DHCP;
TransitionToFinish();
} else {
state_ = STATE_WAIT_URL;
script_fetcher_.reset(ImplCreateScriptFetcher());
script_fetcher_->Fetch(
pac_url_, &pac_script_,
base::Bind(&DhcpProxyScriptAdapterFetcher::OnFetcherDone,
base::Unretained(this)));
}
}
void DhcpProxyScriptAdapterFetcher::OnTimeout() {
DCHECK_EQ(state_, STATE_WAIT_DHCP);
result_ = ERR_TIMED_OUT;
TransitionToFinish();
}
void DhcpProxyScriptAdapterFetcher::OnFetcherDone(int result) {
DCHECK(CalledOnValidThread());
DCHECK(state_ == STATE_WAIT_URL || state_ == STATE_CANCEL);
if (state_ == STATE_CANCEL)
return;
script_fetcher_.reset();
result_ = result;
TransitionToFinish();
}
void DhcpProxyScriptAdapterFetcher::TransitionToFinish() {
DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_WAIT_URL);
state_ = STATE_FINISH;
CompletionCallback callback = callback_;
callback_.Reset();
callback.Run(result_);
}
DhcpProxyScriptAdapterFetcher::State
DhcpProxyScriptAdapterFetcher::state() const {
return state_;
}
ProxyScriptFetcher* DhcpProxyScriptAdapterFetcher::ImplCreateScriptFetcher() {
return new ProxyScriptFetcherImpl(url_request_context_);
}
DhcpProxyScriptAdapterFetcher::DhcpQuery*
DhcpProxyScriptAdapterFetcher::ImplCreateDhcpQuery() {
return new DhcpQuery();
}
base::TimeDelta DhcpProxyScriptAdapterFetcher::ImplGetTimeout() const {
return base::TimeDelta::FromMilliseconds(kTimeoutMs);
}
std::string DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp(
const std::string& adapter_name) {
EnsureDhcpcsvcInit();
std::wstring adapter_name_wide = base::SysMultiByteToWide(adapter_name,
CP_ACP);
DHCPCAPI_PARAMS_ARRAY send_params = { 0, NULL };
BYTE option_data[] = { 1, 252 };
DHCPCAPI_PARAMS wpad_params = { 0 };
wpad_params.OptionId = 252;
wpad_params.IsVendor = FALSE;
DHCPCAPI_PARAMS_ARRAY request_params = { 0 };
request_params.nParams = 1;
request_params.Params = &wpad_params;
DWORD result_buffer_size = 4096;
scoped_ptr<BYTE, base::FreeDeleter> result_buffer;
int retry_count = 0;
DWORD res = NO_ERROR;
do {
result_buffer.reset(static_cast<BYTE*>(malloc(result_buffer_size)));
res = ::DhcpRequestParams(DHCPCAPI_REQUEST_SYNCHRONOUS,
NULL,
const_cast<LPWSTR>(adapter_name_wide.c_str()),
NULL,
send_params, request_params,
result_buffer.get(), &result_buffer_size,
NULL);
++retry_count;
} while (res == ERROR_MORE_DATA && retry_count <= 3);
if (res != NO_ERROR) {
LOG(WARNING) << "Error fetching PAC URL from DHCP: " << res;
UMA_HISTOGRAM_COUNTS("Net.DhcpWpadUnhandledDhcpError", 1);
} else if (wpad_params.nBytesData) {
return SanitizeDhcpApiString(
reinterpret_cast<const char*>(wpad_params.Data),
wpad_params.nBytesData);
}
return "";
}
std::string DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString(
const char* data, size_t count_bytes) {
std::string result(std::string(data, count_bytes).c_str());
base::TrimWhitespaceASCII(result, base::TRIM_TRAILING, &result);
return result;
}
}