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/location.h"
8 #include "base/task_runner_util.h"
9 #include "chromeos/network/network_event_log.h"
10 #include "chromeos/network/network_handler.h"
11 #include "chromeos/network/network_state.h"
12 #include "chromeos/network/network_state_handler.h"
13 #include "net/proxy/proxy_script_fetcher.h"
14 #include "net/proxy/proxy_script_fetcher_impl.h"
15 #include "net/url_request/url_request_context.h"
21 // Runs on NetworkHandler::Get()->message_loop().
22 std::string
GetPacUrlFromDefaultNetwork() {
23 if (!NetworkHandler::IsInitialized())
25 const NetworkState
* default_network
=
26 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
28 return default_network
->web_proxy_auto_discovery_url().spec();
34 DhcpProxyScriptFetcherChromeos::DhcpProxyScriptFetcherChromeos(
35 net::URLRequestContext
* url_request_context
)
36 : url_request_context_(url_request_context
),
37 weak_ptr_factory_(this) {
38 DCHECK(url_request_context_
);
39 proxy_script_fetcher_
.reset(
40 new net::ProxyScriptFetcherImpl(url_request_context_
));
41 if (NetworkHandler::IsInitialized())
42 network_handler_task_runner_
= NetworkHandler::Get()->task_runner();
45 DhcpProxyScriptFetcherChromeos::~DhcpProxyScriptFetcherChromeos() {
48 int DhcpProxyScriptFetcherChromeos::Fetch(
49 base::string16
* utf16_text
,
50 const net::CompletionCallback
& callback
) {
51 if (!network_handler_task_runner_
.get())
52 return net::ERR_PAC_NOT_IN_DHCP
;
53 CHECK(!callback
.is_null());
54 base::PostTaskAndReplyWithResult(
55 network_handler_task_runner_
.get(), 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 {
72 std::string
DhcpProxyScriptFetcherChromeos::GetFetcherName() const {
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
);
86 int res
= proxy_script_fetcher_
->Fetch(pac_url_
, utf16_text
, callback
);
87 if (res
!= net::ERR_IO_PENDING
)
91 } // namespace chromeos