1 // Copyright (c) 2012 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 #ifndef NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_
6 #define NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "net/proxy/dhcp_proxy_script_fetcher.h"
20 class SequencedWorkerPool
;
25 class DhcpProxyScriptAdapterFetcher
;
26 class URLRequestContext
;
28 // Windows-specific implementation.
29 class NET_EXPORT_PRIVATE DhcpProxyScriptFetcherWin
30 : public DhcpProxyScriptFetcher
,
31 public base::SupportsWeakPtr
<DhcpProxyScriptFetcherWin
>,
32 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
34 // Creates a DhcpProxyScriptFetcherWin that issues requests through
35 // |url_request_context|. |url_request_context| must remain valid for
36 // the lifetime of DhcpProxyScriptFetcherWin.
37 explicit DhcpProxyScriptFetcherWin(URLRequestContext
* url_request_context
);
38 virtual ~DhcpProxyScriptFetcherWin();
40 // DhcpProxyScriptFetcher implementation.
41 int Fetch(base::string16
* utf16_text
,
42 const net::CompletionCallback
& callback
) override
;
43 void Cancel() override
;
44 const GURL
& GetPacURL() const override
;
45 std::string
GetFetcherName() const override
;
47 // Sets |adapter_names| to contain the name of each network adapter on
48 // this machine that has DHCP enabled and is not a loop-back adapter. Returns
50 static bool GetCandidateAdapterNames(std::set
<std::string
>* adapter_names
);
53 int num_pending_fetchers() const;
55 URLRequestContext
* url_request_context() const;
57 scoped_refptr
<base::TaskRunner
> GetTaskRunner();
59 // This inner class encapsulate work done on a worker pool thread.
60 // The class calls GetCandidateAdapterNames, which can take a couple of
61 // hundred milliseconds.
62 class NET_EXPORT_PRIVATE AdapterQuery
63 : public base::RefCountedThreadSafe
<AdapterQuery
> {
66 virtual ~AdapterQuery();
68 // This is the method that runs on the worker pool thread.
69 void GetCandidateAdapterNames();
71 // This set is valid after GetCandidateAdapterNames has
72 // been run. Its lifetime is scoped by this object.
73 const std::set
<std::string
>& adapter_names() const;
76 // Virtual method introduced to allow unit testing.
77 virtual bool ImplGetCandidateAdapterNames(
78 std::set
<std::string
>* adapter_names
);
81 // This is constructed on the originating thread, then used on the
82 // worker thread, then used again on the originating thread only when
83 // the task has completed on the worker thread. No locking required.
84 std::set
<std::string
> adapter_names_
;
86 DISALLOW_COPY_AND_ASSIGN(AdapterQuery
);
89 // Virtual methods introduced to allow unit testing.
90 virtual DhcpProxyScriptAdapterFetcher
* ImplCreateAdapterFetcher();
91 virtual AdapterQuery
* ImplCreateAdapterQuery();
92 virtual base::TimeDelta
ImplGetMaxWait();
93 virtual void ImplOnGetCandidateAdapterNamesDone() {}
96 // Event/state transition handlers
98 void OnGetCandidateAdapterNamesDone(scoped_refptr
<AdapterQuery
> query
);
99 void OnFetcherDone(int result
);
101 void TransitionToDone();
103 // This is the outer state machine for fetching PAC configuration from
104 // DHCP. It relies for sub-states on the state machine of the
105 // DhcpProxyScriptAdapterFetcher class.
107 // The goal of the implementation is to the following work in parallel
108 // for all network adapters that are using DHCP:
109 // a) Try to get the PAC URL configured in DHCP;
110 // b) If one is configured, try to fetch the PAC URL.
111 // c) Once this is done for all adapters, or a timeout has passed after
112 // it has completed for the fastest adapter, return the PAC file
113 // available for the most preferred network adapter, if any.
115 // The state machine goes from START->WAIT_ADAPTERS when it starts a
116 // worker thread to get the list of adapters with DHCP enabled.
117 // It then goes from WAIT_ADAPTERS->NO_RESULTS when it creates
118 // and starts an DhcpProxyScriptAdapterFetcher for each adapter. It goes
119 // from NO_RESULTS->SOME_RESULTS when it gets the first result; at this
120 // point a wait timer is started. It goes from SOME_RESULTS->DONE in
121 // two cases: All results are known, or the wait timer expired. A call
122 // to Cancel() will also go straight to DONE from any state. Any
123 // way the DONE state is entered, we will at that point cancel any
124 // outstanding work and return the best known PAC script or the empty
127 // The state machine is reset for each Fetch(), a call to which is
128 // only valid in states START and DONE, as only one Fetch() is
129 // allowed to be outstanding at any given time.
138 // Current state of this state machine.
141 // Vector, in Windows' network adapter preference order, of
142 // DhcpProxyScriptAdapterFetcher objects that are or were attempting
143 // to fetch a PAC file based on DHCP configuration.
144 typedef ScopedVector
<DhcpProxyScriptAdapterFetcher
> FetcherVector
;
145 FetcherVector fetchers_
;
147 // Number of fetchers we are waiting for.
148 int num_pending_fetchers_
;
150 // Lets our client know we're done. Not valid in states START or DONE.
151 net::CompletionCallback callback_
;
153 // Pointer to string we will write results to. Not valid in states
155 base::string16
* destination_string_
;
157 // PAC URL retrieved from DHCP, if any. Valid only in state STATE_DONE.
160 base::OneShotTimer
<DhcpProxyScriptFetcherWin
> wait_timer_
;
162 URLRequestContext
* const url_request_context_
;
164 // NULL or the AdapterQuery currently in flight.
165 scoped_refptr
<AdapterQuery
> last_query_
;
167 // Time |Fetch()| was last called, 0 if never.
168 base::TimeTicks fetch_start_time_
;
170 // Worker pool we use for all DHCP lookup tasks.
171 scoped_refptr
<base::SequencedWorkerPool
> worker_pool_
;
173 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptFetcherWin
);
178 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_