| 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_SCRIPT_ADAPTER_FETCHER_WIN_H_ | 5 #ifndef NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ |
| 6 #define NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ | 6 #define NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 enum State { | 100 enum State { |
| 101 STATE_START, | 101 STATE_START, |
| 102 STATE_WAIT_DHCP, | 102 STATE_WAIT_DHCP, |
| 103 STATE_WAIT_URL, | 103 STATE_WAIT_URL, |
| 104 STATE_FINISH, | 104 STATE_FINISH, |
| 105 STATE_CANCEL, | 105 STATE_CANCEL, |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 State state() const; | 108 State state() const; |
| 109 | 109 |
| 110 // This inner class is used to encapsulate the worker thread, which has | 110 // This inner class encapsulates work done on a worker pool thread. |
| 111 // only a weak reference back to the main object, so that the main object | 111 // By using a separate object, we can keep the main object completely |
| 112 // can be destroyed before the thread ends. This also keeps the main | 112 // thread safe and let it be non-refcounted. |
| 113 // object completely thread safe and allows it to be non-refcounted. | 113 class NET_EXPORT_PRIVATE DhcpQuery |
| 114 // | 114 : public base::RefCountedThreadSafe<DhcpQuery> { |
| 115 // TODO(joi): Replace with PostTaskAndReply once http://crbug.com/86301 | |
| 116 // has been implemented. | |
| 117 class NET_EXPORT_PRIVATE WorkerThread | |
| 118 : public base::RefCountedThreadSafe<WorkerThread> { | |
| 119 public: | 115 public: |
| 120 // Creates and initializes (but does not start) the worker thread. | 116 DhcpQuery(); |
| 121 explicit WorkerThread( | 117 virtual ~DhcpQuery(); |
| 122 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner); | |
| 123 virtual ~WorkerThread(); | |
| 124 | 118 |
| 125 // Starts the worker thread, fetching information for |adapter_name| using | 119 // This method should run on a worker pool thread, via PostTaskAndReply. |
| 126 // |get_pac_from_url_func|. | 120 // After it has run, the |url()| method on this object will return the |
| 127 void Start(const std::string& adapter_name); | 121 // URL retrieved. |
| 122 void GetPacURLForAdapter(const std::string& adapter_name); |
| 123 |
| 124 // Returns the URL retrieved for the given adapter, once the task has run. |
| 125 std::string url() const; |
| 128 | 126 |
| 129 protected: | 127 protected: |
| 130 // Virtual method introduced to allow unit testing. | 128 // Virtual method introduced to allow unit testing. |
| 131 virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name); | 129 virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name); |
| 132 | 130 |
| 133 private: | 131 private: |
| 134 // This is the method that runs on the worker thread. | 132 // The URL retrieved for the given adapter. |
| 135 void ThreadFunc(const std::string& adapter_name); | 133 std::string url_; |
| 136 | 134 |
| 137 // Callback for the above; this executes back on the main thread, | 135 DISALLOW_COPY_AND_ASSIGN(DhcpQuery); |
| 138 // not the worker thread. | |
| 139 void OnThreadDone(const std::string& url); | |
| 140 | |
| 141 // All work except ThreadFunc and (sometimes) destruction should occur | |
| 142 // on the thread that constructs the object. | |
| 143 base::ThreadChecker thread_checker_; | |
| 144 | |
| 145 // May only be accessed on the thread that constructs the object. | |
| 146 base::WeakPtr<DhcpProxyScriptAdapterFetcher> owner_; | |
| 147 | |
| 148 // Used by worker thread to post a message back to the original | |
| 149 // thread. Fine to use a proxy since in the case where the original | |
| 150 // thread has gone away, that would mean the |owner_| object is gone | |
| 151 // anyway, so there is nobody to receive the result. | |
| 152 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(WorkerThread); | |
| 155 }; | 136 }; |
| 156 | 137 |
| 157 // Virtual methods introduced to allow unit testing. | 138 // Virtual methods introduced to allow unit testing. |
| 158 virtual ProxyScriptFetcher* ImplCreateScriptFetcher(); | 139 virtual ProxyScriptFetcher* ImplCreateScriptFetcher(); |
| 159 virtual WorkerThread* ImplCreateWorkerThread( | 140 virtual DhcpQuery* ImplCreateDhcpQuery(); |
| 160 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner); | |
| 161 virtual base::TimeDelta ImplGetTimeout() const; | 141 virtual base::TimeDelta ImplGetTimeout() const; |
| 162 | 142 |
| 163 private: | 143 private: |
| 164 // Event/state transition handlers | 144 // Event/state transition handlers |
| 165 void OnQueryDhcpDone(const std::string& url); | 145 void OnDhcpQueryDone(scoped_refptr<DhcpQuery> dhcp_query); |
| 166 void OnTimeout(); | 146 void OnTimeout(); |
| 167 void OnFetcherDone(int result); | 147 void OnFetcherDone(int result); |
| 168 void TransitionToFinish(); | 148 void TransitionToFinish(); |
| 169 | 149 |
| 170 // Current state of this state machine. | 150 // Current state of this state machine. |
| 171 State state_; | 151 State state_; |
| 172 | 152 |
| 173 // A network error indicating result of operation. | 153 // A network error indicating result of operation. |
| 174 int result_; | 154 int result_; |
| 175 | 155 |
| 176 // Empty string or the PAC script downloaded. | 156 // Empty string or the PAC script downloaded. |
| 177 string16 pac_script_; | 157 string16 pac_script_; |
| 178 | 158 |
| 179 // Empty URL or the PAC URL configured in DHCP. | 159 // Empty URL or the PAC URL configured in DHCP. |
| 180 GURL pac_url_; | 160 GURL pac_url_; |
| 181 | 161 |
| 182 // Callback to let our client know we're done. Invalid in states | 162 // Callback to let our client know we're done. Invalid in states |
| 183 // START, FINISH and CANCEL. | 163 // START, FINISH and CANCEL. |
| 184 OldCompletionCallback* callback_; | 164 OldCompletionCallback* callback_; |
| 185 | 165 |
| 186 // Container for our worker thread. NULL if not currently running. | |
| 187 scoped_refptr<WorkerThread> worker_thread_; | |
| 188 | |
| 189 // Fetcher to retrieve PAC files once URL is known. | 166 // Fetcher to retrieve PAC files once URL is known. |
| 190 scoped_ptr<ProxyScriptFetcher> script_fetcher_; | 167 scoped_ptr<ProxyScriptFetcher> script_fetcher_; |
| 191 | 168 |
| 192 // Callback from the script fetcher. | 169 // Callback from the script fetcher. |
| 193 OldCompletionCallbackImpl<DhcpProxyScriptAdapterFetcher> | 170 OldCompletionCallbackImpl<DhcpProxyScriptAdapterFetcher> |
| 194 script_fetcher_callback_; | 171 script_fetcher_callback_; |
| 195 | 172 |
| 196 // Implements a timeout on the call to the Win32 DHCP API. | 173 // Implements a timeout on the call to the Win32 DHCP API. |
| 197 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_; | 174 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_; |
| 198 | 175 |
| 199 scoped_refptr<URLRequestContext> url_request_context_; | 176 scoped_refptr<URLRequestContext> url_request_context_; |
| 200 | 177 |
| 201 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher); | 178 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher); |
| 202 }; | 179 }; |
| 203 | 180 |
| 204 } // namespace net | 181 } // namespace net |
| 205 | 182 |
| 206 #endif // NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ | 183 #endif // NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ |
| OLD | NEW |