Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / chromeos / network / dhcp_proxy_script_fetcher_chromeos.cc
blob41f7a4d5f2fab6ad06d8032d753bc9dd83ceba10
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.
5 #include "chromeos/network/dhcp_proxy_script_fetcher_chromeos.h"
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"
16 namespace chromeos {
18 namespace {
20 // Runs on NetworkHandler::Get()->message_loop().
21 std::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();
31 } // namespace
33 DhcpProxyScriptFetcherChromeos::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();
44 DhcpProxyScriptFetcherChromeos::~DhcpProxyScriptFetcherChromeos() {
47 int 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 CHECK(!callback.is_null());
53 base::PostTaskAndReplyWithResult(
54 network_handler_message_loop_.get(),
55 FROM_HERE,
56 base::Bind(&GetPacUrlFromDefaultNetwork),
57 base::Bind(&DhcpProxyScriptFetcherChromeos::ContinueFetch,
58 weak_ptr_factory_.GetWeakPtr(), utf16_text, callback));
59 return net::ERR_IO_PENDING;
62 void DhcpProxyScriptFetcherChromeos::Cancel() {
63 proxy_script_fetcher_->Cancel();
64 // Invalidate any pending callbacks (i.e. calls to ContinueFetch).
65 weak_ptr_factory_.InvalidateWeakPtrs();
68 const GURL& DhcpProxyScriptFetcherChromeos::GetPacURL() const {
69 return pac_url_;
72 std::string DhcpProxyScriptFetcherChromeos::GetFetcherName() const {
73 return "chromeos";
76 void DhcpProxyScriptFetcherChromeos::ContinueFetch(
77 base::string16* utf16_text,
78 net::CompletionCallback callback,
79 std::string pac_url) {
80 NET_LOG_EVENT("DhcpProxyScriptFetcher", pac_url);
81 pac_url_ = GURL(pac_url);
82 if (pac_url_.is_empty()) {
83 callback.Run(net::ERR_PAC_NOT_IN_DHCP);
84 return;
86 int res = proxy_script_fetcher_->Fetch(pac_url_, utf16_text, callback);
87 if (res != net::ERR_IO_PENDING)
88 callback.Run(res);
91 } // namespace chromeos