dhcp_proxy_script_fetcher_chromeos.cc revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright (c) 2013 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 "chromeos/network/dhcp_proxy_script_fetcher_chromeos.h"
6
7#include "base/task_runner_util.h"
8#include "chromeos/network/network_event_log.h"
9#include "chromeos/network/network_handler.h"
10#include "chromeos/network/network_state.h"
11#include "chromeos/network/network_state_handler.h"
12#include "net/proxy/proxy_script_fetcher.h"
13#include "net/proxy/proxy_script_fetcher_impl.h"
14#include "net/url_request/url_request_context.h"
15
16namespace chromeos {
17
18namespace {
19
20// Runs on NetworkHandler::Get()->message_loop().
21std::string GetPacUrlFromDefaultNetwork() {
22  if (!NetworkHandler::IsInitialized())
23    return std::string();
24  const NetworkState* default_network =
25      NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
26  if (default_network)
27    return default_network->web_proxy_auto_discovery_url().spec();
28  return std::string();
29}
30
31}  // namespace
32
33DhcpProxyScriptFetcherChromeos::DhcpProxyScriptFetcherChromeos(
34    net::URLRequestContext* url_request_context)
35    : url_request_context_(url_request_context),
36      weak_ptr_factory_(this) {
37  DCHECK(url_request_context_);
38  proxy_script_fetcher_.reset(
39      new net::ProxyScriptFetcherImpl(url_request_context_));
40  if (NetworkHandler::IsInitialized())
41    network_handler_message_loop_ = NetworkHandler::Get()->message_loop();
42}
43
44DhcpProxyScriptFetcherChromeos::~DhcpProxyScriptFetcherChromeos() {
45}
46
47int DhcpProxyScriptFetcherChromeos::Fetch(
48    base::string16* utf16_text,
49    const net::CompletionCallback& callback) {
50  if (!network_handler_message_loop_.get())
51    return net::ERR_PAC_NOT_IN_DHCP;
52  base::PostTaskAndReplyWithResult(
53      network_handler_message_loop_.get(),
54      FROM_HERE,
55      base::Bind(&GetPacUrlFromDefaultNetwork),
56      base::Bind(&DhcpProxyScriptFetcherChromeos::ContinueFetch,
57                 weak_ptr_factory_.GetWeakPtr(), utf16_text, callback));
58  return net::ERR_IO_PENDING;
59}
60
61void DhcpProxyScriptFetcherChromeos::Cancel() {
62  proxy_script_fetcher_->Cancel();
63}
64
65const GURL& DhcpProxyScriptFetcherChromeos::GetPacURL() const {
66  return pac_url_;
67}
68
69std::string DhcpProxyScriptFetcherChromeos::GetFetcherName() const {
70  return "chromeos";
71}
72
73void DhcpProxyScriptFetcherChromeos::ContinueFetch(
74    base::string16* utf16_text,
75    const net::CompletionCallback& callback,
76    std::string pac_url) {
77  NET_LOG_EVENT("DhcpProxyScriptFetcher", pac_url);
78  pac_url_ = GURL(pac_url);
79  if (pac_url_.is_empty()) {
80    callback.Run(net::ERR_PAC_NOT_IN_DHCP);
81    return;
82  }
83  int res = proxy_script_fetcher_->Fetch(pac_url_, utf16_text, callback);
84  if (res != net::ERR_IO_PENDING)
85    callback.Run(res);
86}
87
88}  // namespace chromeos
89