OLD | NEW |
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 private: |
| 79 // This is the method that runs on the worker thread. |
| 80 void ThreadFunc(); |
| 81 |
| 82 // Callback for the above; this executes back on the main thread, |
| 83 // not the worker thread. |
| 84 void OnThreadDone(); |
| 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: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // START and DONE. | 173 // START and DONE. |
116 string16* destination_string_; | 174 string16* destination_string_; |
117 | 175 |
118 // PAC URL retrieved from DHCP, if any. Valid only in state STATE_DONE. | 176 // PAC URL retrieved from DHCP, if any. Valid only in state STATE_DONE. |
119 GURL pac_url_; | 177 GURL pac_url_; |
120 | 178 |
121 base::OneShotTimer<DhcpProxyScriptFetcherWin> wait_timer_; | 179 base::OneShotTimer<DhcpProxyScriptFetcherWin> wait_timer_; |
122 | 180 |
123 scoped_refptr<URLRequestContext> url_request_context_; | 181 scoped_refptr<URLRequestContext> url_request_context_; |
124 | 182 |
| 183 scoped_refptr<WorkerThread> worker_thread_; |
| 184 |
125 // Time |Fetch()| was last called, 0 if never. | 185 // Time |Fetch()| was last called, 0 if never. |
126 base::TimeTicks fetch_start_time_; | 186 base::TimeTicks fetch_start_time_; |
127 | 187 |
128 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptFetcherWin); | 188 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptFetcherWin); |
129 }; | 189 }; |
130 | 190 |
131 } // namespace net | 191 } // namespace net |
132 | 192 |
133 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_ | 193 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_ |
OLD | NEW |