Remove redundant Tab constructors.
[chromium-blink-merge.git] / net / proxy / dhcp_proxy_script_fetcher_win.h
blob6110a019999bbfa491f4b4d77a86647c5f2b73ff
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_
8 #include <set>
9 #include <string>
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "net/proxy/dhcp_proxy_script_fetcher.h"
18 namespace base {
19 class SequencedWorkerPool;
22 namespace net {
24 class DhcpProxyScriptAdapterFetcher;
25 class URLRequestContext;
27 // Windows-specific implementation.
28 class NET_EXPORT_PRIVATE DhcpProxyScriptFetcherWin
29 : public DhcpProxyScriptFetcher,
30 public base::SupportsWeakPtr<DhcpProxyScriptFetcherWin>,
31 NON_EXPORTED_BASE(public base::NonThreadSafe) {
32 public:
33 // Creates a DhcpProxyScriptFetcherWin that issues requests through
34 // |url_request_context|. |url_request_context| must remain valid for
35 // the lifetime of DhcpProxyScriptFetcherWin.
36 explicit DhcpProxyScriptFetcherWin(URLRequestContext* url_request_context);
37 ~DhcpProxyScriptFetcherWin() override;
39 // DhcpProxyScriptFetcher implementation.
40 int Fetch(base::string16* utf16_text,
41 const CompletionCallback& callback) override;
42 void Cancel() override;
43 const GURL& GetPacURL() const override;
44 std::string GetFetcherName() const override;
46 // Sets |adapter_names| to contain the name of each network adapter on
47 // this machine that has DHCP enabled and is not a loop-back adapter. Returns
48 // false on error.
49 static bool GetCandidateAdapterNames(std::set<std::string>* adapter_names);
51 protected:
52 int num_pending_fetchers() const;
54 URLRequestContext* url_request_context() const;
56 scoped_refptr<base::TaskRunner> GetTaskRunner();
58 // This inner class encapsulate work done on a worker pool thread.
59 // The class calls GetCandidateAdapterNames, which can take a couple of
60 // hundred milliseconds.
61 class NET_EXPORT_PRIVATE AdapterQuery
62 : public base::RefCountedThreadSafe<AdapterQuery> {
63 public:
64 AdapterQuery();
66 // This is the method that runs on the worker pool thread.
67 void GetCandidateAdapterNames();
69 // This set is valid after GetCandidateAdapterNames has
70 // been run. Its lifetime is scoped by this object.
71 const std::set<std::string>& adapter_names() const;
73 protected:
74 // Virtual method introduced to allow unit testing.
75 virtual bool ImplGetCandidateAdapterNames(
76 std::set<std::string>* adapter_names);
78 friend class base::RefCountedThreadSafe<AdapterQuery>;
79 virtual ~AdapterQuery();
81 private:
82 // This is constructed on the originating thread, then used on the
83 // worker thread, then used again on the originating thread only when
84 // the task has completed on the worker thread. No locking required.
85 std::set<std::string> adapter_names_;
87 DISALLOW_COPY_AND_ASSIGN(AdapterQuery);
90 // Virtual methods introduced to allow unit testing.
91 virtual DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher();
92 virtual AdapterQuery* ImplCreateAdapterQuery();
93 virtual base::TimeDelta ImplGetMaxWait();
94 virtual void ImplOnGetCandidateAdapterNamesDone() {}
96 private:
97 // Event/state transition handlers
98 void CancelImpl();
99 void OnGetCandidateAdapterNamesDone(scoped_refptr<AdapterQuery> query);
100 void OnFetcherDone(int result);
101 void OnWaitTimer();
102 void TransitionToDone();
104 // This is the outer state machine for fetching PAC configuration from
105 // DHCP. It relies for sub-states on the state machine of the
106 // DhcpProxyScriptAdapterFetcher class.
108 // The goal of the implementation is to the following work in parallel
109 // for all network adapters that are using DHCP:
110 // a) Try to get the PAC URL configured in DHCP;
111 // b) If one is configured, try to fetch the PAC URL.
112 // c) Once this is done for all adapters, or a timeout has passed after
113 // it has completed for the fastest adapter, return the PAC file
114 // available for the most preferred network adapter, if any.
116 // The state machine goes from START->WAIT_ADAPTERS when it starts a
117 // worker thread to get the list of adapters with DHCP enabled.
118 // It then goes from WAIT_ADAPTERS->NO_RESULTS when it creates
119 // and starts an DhcpProxyScriptAdapterFetcher for each adapter. It goes
120 // from NO_RESULTS->SOME_RESULTS when it gets the first result; at this
121 // point a wait timer is started. It goes from SOME_RESULTS->DONE in
122 // two cases: All results are known, or the wait timer expired. A call
123 // to Cancel() will also go straight to DONE from any state. Any
124 // way the DONE state is entered, we will at that point cancel any
125 // outstanding work and return the best known PAC script or the empty
126 // string.
128 // The state machine is reset for each Fetch(), a call to which is
129 // only valid in states START and DONE, as only one Fetch() is
130 // allowed to be outstanding at any given time.
131 enum State {
132 STATE_START,
133 STATE_WAIT_ADAPTERS,
134 STATE_NO_RESULTS,
135 STATE_SOME_RESULTS,
136 STATE_DONE,
139 // Current state of this state machine.
140 State state_;
142 // Vector, in Windows' network adapter preference order, of
143 // DhcpProxyScriptAdapterFetcher objects that are or were attempting
144 // to fetch a PAC file based on DHCP configuration.
145 typedef ScopedVector<DhcpProxyScriptAdapterFetcher> FetcherVector;
146 FetcherVector fetchers_;
148 // Number of fetchers we are waiting for.
149 int num_pending_fetchers_;
151 // Lets our client know we're done. Not valid in states START or DONE.
152 CompletionCallback callback_;
154 // Pointer to string we will write results to. Not valid in states
155 // START and DONE.
156 base::string16* destination_string_;
158 // PAC URL retrieved from DHCP, if any. Valid only in state STATE_DONE.
159 GURL pac_url_;
161 base::OneShotTimer<DhcpProxyScriptFetcherWin> wait_timer_;
163 URLRequestContext* const url_request_context_;
165 // NULL or the AdapterQuery currently in flight.
166 scoped_refptr<AdapterQuery> last_query_;
168 // Time |Fetch()| was last called, 0 if never.
169 base::TimeTicks fetch_start_time_;
171 // Worker pool we use for all DHCP lookup tasks.
172 scoped_refptr<base::SequencedWorkerPool> worker_pool_;
174 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptFetcherWin);
177 } // namespace net
179 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_