Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: net/proxy/dhcp_proxy_script_fetcher_win.h

Issue 7189016: Do GetAdaptersAddresses on a worker thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix handling of cancellation. Add regression test. Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_ 5 #ifndef NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_
6 #define NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_ 6 #define NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_
7 #pragma once 7 #pragma once
8 8
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 11
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h" 13 #include "base/memory/scoped_vector.h"
14 #include "base/message_loop_proxy.h"
14 #include "base/threading/non_thread_safe.h" 15 #include "base/threading/non_thread_safe.h"
15 #include "base/time.h" 16 #include "base/time.h"
16 #include "base/timer.h" 17 #include "base/timer.h"
17 #include "net/proxy/dhcp_proxy_script_fetcher.h" 18 #include "net/proxy/dhcp_proxy_script_fetcher.h"
18 19
19 namespace net { 20 namespace net {
20 21
21 class DhcpProxyScriptAdapterFetcher; 22 class DhcpProxyScriptAdapterFetcher;
22 class URLRequestContext; 23 class URLRequestContext;
23 24
24 // Windows-specific implementation. 25 // Windows-specific implementation.
25 class NET_TEST DhcpProxyScriptFetcherWin 26 class NET_TEST DhcpProxyScriptFetcherWin
26 : public DhcpProxyScriptFetcher, 27 : public DhcpProxyScriptFetcher,
28 public base::SupportsWeakPtr<DhcpProxyScriptFetcherWin>,
27 NON_EXPORTED_BASE(public base::NonThreadSafe) { 29 NON_EXPORTED_BASE(public base::NonThreadSafe) {
28 public: 30 public:
29 // Creates a DhcpProxyScriptFetcherWin that issues requests through 31 // Creates a DhcpProxyScriptFetcherWin that issues requests through
30 // |url_request_context|. |url_request_context| must remain valid for 32 // |url_request_context|. |url_request_context| must remain valid for
31 // the lifetime of DhcpProxyScriptFetcherWin. 33 // the lifetime of DhcpProxyScriptFetcherWin.
32 explicit DhcpProxyScriptFetcherWin(URLRequestContext* url_request_context); 34 explicit DhcpProxyScriptFetcherWin(URLRequestContext* url_request_context);
33 virtual ~DhcpProxyScriptFetcherWin(); 35 virtual ~DhcpProxyScriptFetcherWin();
34 36
35 // DhcpProxyScriptFetcher implementation. 37 // DhcpProxyScriptFetcher implementation.
36 int Fetch(string16* utf16_text, CompletionCallback* callback) OVERRIDE; 38 int Fetch(string16* utf16_text, CompletionCallback* callback) OVERRIDE;
37 void Cancel() OVERRIDE; 39 void Cancel() OVERRIDE;
38 const GURL& GetPacURL() const OVERRIDE; 40 const GURL& GetPacURL() const OVERRIDE;
39 std::string GetFetcherName() const OVERRIDE; 41 std::string GetFetcherName() const OVERRIDE;
40 42
41 // Sets |adapter_names| to contain the name of each network adapter on 43 // Sets |adapter_names| to contain the name of each network adapter on
42 // this machine that has DHCP enabled and is not a loop-back adapter. Returns 44 // this machine that has DHCP enabled and is not a loop-back adapter. Returns
43 // false on error. 45 // false on error.
44 static bool GetCandidateAdapterNames(std::set<std::string>* adapter_names); 46 static bool GetCandidateAdapterNames(std::set<std::string>* adapter_names);
45 47
46 protected: 48 protected:
47 int num_pending_fetchers() const; 49 int num_pending_fetchers() const;
48 50
49 URLRequestContext* url_request_context() const; 51 URLRequestContext* url_request_context() const;
50 52
53 // This inner class is used to encapsulate a worker thread that calls
54 // GetCandidateAdapterNames as it can take a couple of hundred
55 // milliseconds.
56 //
57 // TODO(joi): Replace with PostTaskAndReply once http://crbug.com/86301
58 // has been implemented.
59 class NET_TEST WorkerThread
60 : public base::RefCountedThreadSafe<WorkerThread> {
61 public:
62 // Creates and initializes (but does not start) the worker thread.
63 explicit WorkerThread(
64 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner);
65 virtual ~WorkerThread();
66
67 // Starts the worker thread.
68 void Start();
69
70 protected:
71 WorkerThread(); // To override in unit tests only.
72 void Init(const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner);
73
74 // Virtual method introduced to allow unit testing.
75 virtual bool ImplGetCandidateAdapterNames(
76 std::set<std::string>* adapter_names);
77
78 // Callback for ThreadFunc; this executes back on the main thread,
79 // not the worker thread. May be overridden by unit tests.
80 virtual void OnThreadDone();
81
82 private:
83 // This is the method that runs on the worker thread.
84 void ThreadFunc();
85
86 // All work except ThreadFunc and (sometimes) destruction should occur
87 // on the thread that constructs the object.
88 base::ThreadChecker thread_checker_;
89
90 // May only be accessed on the thread that constructs the object.
91 base::WeakPtr<DhcpProxyScriptFetcherWin> owner_;
92
93 // Used by worker thread to post a message back to the original
94 // thread. Fine to use a proxy since in the case where the original
95 // thread has gone away, that would mean the |owner_| object is gone
96 // anyway, so there is nobody to receive the result.
97 scoped_refptr<base::MessageLoopProxy> origin_loop_;
98
99 // This is constructed on the originating thread, then used on the
100 // worker thread, then used again on the originating thread only when
101 // the task has completed on the worker thread. No locking required.
102 std::set<std::string> adapter_names_;
103
104 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
105 };
106
51 // Virtual methods introduced to allow unit testing. 107 // Virtual methods introduced to allow unit testing.
52 virtual DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher(); 108 virtual DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher();
53 virtual bool ImplGetCandidateAdapterNames( 109 virtual WorkerThread* ImplCreateWorkerThread(
54 std::set<std::string>* adapter_names); 110 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner);
55 virtual int ImplGetMaxWaitMs(); 111 virtual int ImplGetMaxWaitMs();
56 112
57 private: 113 private:
58 // Event/state transition handlers 114 // Event/state transition handlers
59 void CancelImpl(); 115 void CancelImpl();
116 void OnGetCandidateAdapterNamesDone(
117 const std::set<std::string>& adapter_names);
60 void OnFetcherDone(int result); 118 void OnFetcherDone(int result);
61 void OnWaitTimer(); 119 void OnWaitTimer();
62 void TransitionToDone(); 120 void TransitionToDone();
63 121
64 // This is the outer state machine for fetching PAC configuration from 122 // This is the outer state machine for fetching PAC configuration from
65 // DHCP. It relies for sub-states on the state machine of the 123 // DHCP. It relies for sub-states on the state machine of the
66 // DhcpProxyScriptAdapterFetcher class. 124 // DhcpProxyScriptAdapterFetcher class.
67 // 125 //
68 // The goal of the implementation is to the following work in parallel 126 // The goal of the implementation is to the following work in parallel
69 // for all network adapters that are using DHCP: 127 // for all network adapters that are using DHCP:
70 // a) Try to get the PAC URL configured in DHCP; 128 // a) Try to get the PAC URL configured in DHCP;
71 // b) If one is configured, try to fetch the PAC URL. 129 // b) If one is configured, try to fetch the PAC URL.
72 // c) Once this is done for all adapters, or a timeout has passed after 130 // c) Once this is done for all adapters, or a timeout has passed after
73 // it has completed for the fastest adapter, return the PAC file 131 // it has completed for the fastest adapter, return the PAC file
74 // available for the most preferred network adapter, if any. 132 // available for the most preferred network adapter, if any.
75 // 133 //
76 // The state machine goes from START->NO_RESULTS when it creates 134 // The state machine goes from START->WAIT_ADAPTERS when it starts a
135 // worker thread to get the list of adapters with DHCP enabled.
136 // It then goes from WAIT_ADAPTERS->NO_RESULTS when it creates
77 // and starts an DhcpProxyScriptAdapterFetcher for each adapter. It goes 137 // and starts an DhcpProxyScriptAdapterFetcher for each adapter. It goes
78 // from NO_RESULTS->SOME_RESULTS when it gets the first result; at this 138 // from NO_RESULTS->SOME_RESULTS when it gets the first result; at this
79 // point a wait timer is started. It goes from SOME_RESULTS->DONE in 139 // point a wait timer is started. It goes from SOME_RESULTS->DONE in
80 // two cases: All results are known, or the wait timer expired. A call 140 // two cases: All results are known, or the wait timer expired. A call
81 // to Cancel() will also go straight to DONE from any state. Any 141 // to Cancel() will also go straight to DONE from any state. Any
82 // way the DONE state is entered, we will at that point cancel any 142 // way the DONE state is entered, we will at that point cancel any
83 // outstanding work and return the best known PAC script or the empty 143 // outstanding work and return the best known PAC script or the empty
84 // string. 144 // string.
85 // 145 //
86 // The state machine is reset for each Fetch(), a call to which is 146 // The state machine is reset for each Fetch(), a call to which is
87 // only valid in states START and DONE, as only one Fetch() is 147 // only valid in states START and DONE, as only one Fetch() is
88 // allowed to be outstanding at any given time. 148 // allowed to be outstanding at any given time.
89 enum State { 149 enum State {
90 STATE_START, 150 STATE_START,
151 STATE_WAIT_ADAPTERS,
91 STATE_NO_RESULTS, 152 STATE_NO_RESULTS,
92 STATE_SOME_RESULTS, 153 STATE_SOME_RESULTS,
93 STATE_DONE, 154 STATE_DONE,
94 }; 155 };
95 156
96 // Current state of this state machine. 157 // Current state of this state machine.
97 State state_; 158 State state_;
98 159
99 // Vector, in Windows' network adapter preference order, of 160 // Vector, in Windows' network adapter preference order, of
100 // DhcpProxyScriptAdapterFetcher objects that are or were attempting 161 // DhcpProxyScriptAdapterFetcher objects that are or were attempting
(...skipping 14 matching lines...) Expand all
115 // START and DONE. 176 // START and DONE.
116 string16* destination_string_; 177 string16* destination_string_;
117 178
118 // PAC URL retrieved from DHCP, if any. Valid only in state STATE_DONE. 179 // PAC URL retrieved from DHCP, if any. Valid only in state STATE_DONE.
119 GURL pac_url_; 180 GURL pac_url_;
120 181
121 base::OneShotTimer<DhcpProxyScriptFetcherWin> wait_timer_; 182 base::OneShotTimer<DhcpProxyScriptFetcherWin> wait_timer_;
122 183
123 scoped_refptr<URLRequestContext> url_request_context_; 184 scoped_refptr<URLRequestContext> url_request_context_;
124 185
186 scoped_refptr<WorkerThread> worker_thread_;
187
125 // Time |Fetch()| was last called, 0 if never. 188 // Time |Fetch()| was last called, 0 if never.
126 base::TimeTicks fetch_start_time_; 189 base::TimeTicks fetch_start_time_;
127 190
128 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptFetcherWin); 191 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptFetcherWin);
129 }; 192 };
130 193
131 } // namespace net 194 } // namespace net
132 195
133 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_ 196 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_
OLDNEW
« no previous file with comments | « net/proxy/dhcp_proxy_script_adapter_fetcher_win.h ('k') | net/proxy/dhcp_proxy_script_fetcher_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698