Chromium Code Reviews| 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 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 8 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 9 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 10 #include "base/task.h" | |
| 11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/proxy/dhcpcsvc_init_win.h" | 14 #include "net/proxy/dhcpcsvc_init_win.h" |
| 15 #include "net/proxy/proxy_script_fetcher_impl.h" | 15 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 16 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
| 17 | 17 |
| 18 #include <windows.h> | 18 #include <windows.h> |
| 19 #include <winsock2.h> | 19 #include <winsock2.h> |
| 20 #include <dhcpcsdk.h> | 20 #include <dhcpcsdk.h> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 const std::string& adapter_name, OldCompletionCallback* callback) { | 53 const std::string& adapter_name, OldCompletionCallback* callback) { |
| 54 DCHECK(CalledOnValidThread()); | 54 DCHECK(CalledOnValidThread()); |
| 55 DCHECK_EQ(state_, STATE_START); | 55 DCHECK_EQ(state_, STATE_START); |
| 56 result_ = ERR_IO_PENDING; | 56 result_ = ERR_IO_PENDING; |
| 57 pac_script_ = string16(); | 57 pac_script_ = string16(); |
| 58 state_ = STATE_WAIT_DHCP; | 58 state_ = STATE_WAIT_DHCP; |
| 59 callback_ = callback; | 59 callback_ = callback; |
| 60 | 60 |
| 61 wait_timer_.Start(FROM_HERE, ImplGetTimeout(), | 61 wait_timer_.Start(FROM_HERE, ImplGetTimeout(), |
| 62 this, &DhcpProxyScriptAdapterFetcher::OnTimeout); | 62 this, &DhcpProxyScriptAdapterFetcher::OnTimeout); |
| 63 worker_thread_ = ImplCreateWorkerThread(AsWeakPtr()); | 63 scoped_refptr<DhcpQuery> dhcp_query(ImplCreateDhcpQuery()); |
|
awong
2011/10/07 17:53:22
I wonder if it would be possible to not bother wit
Jói
2011/10/11 19:51:02
I think if this way works, it seems more correct?
| |
| 64 worker_thread_->Start(adapter_name); | 64 base::WorkerPool::PostTaskAndReply( |
| 65 FROM_HERE, | |
| 66 base::Bind( | |
| 67 &DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter, | |
| 68 dhcp_query.get(), | |
|
awong
2011/10/07 17:53:22
FYI, You no longer need the .get(). Submitted CL
Jói
2011/10/11 19:51:02
Done.
| |
| 69 adapter_name), | |
| 70 base::Bind( | |
| 71 &DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone, | |
| 72 AsWeakPtr(), | |
| 73 dhcp_query), | |
| 74 true); | |
| 65 } | 75 } |
| 66 | 76 |
| 67 void DhcpProxyScriptAdapterFetcher::Cancel() { | 77 void DhcpProxyScriptAdapterFetcher::Cancel() { |
| 68 DCHECK(CalledOnValidThread()); | 78 DCHECK(CalledOnValidThread()); |
| 69 callback_ = NULL; | 79 callback_ = NULL; |
| 70 wait_timer_.Stop(); | 80 wait_timer_.Stop(); |
| 71 script_fetcher_.reset(); | 81 script_fetcher_.reset(); |
| 72 | 82 |
| 73 switch (state_) { | 83 switch (state_) { |
| 74 case STATE_WAIT_DHCP: | 84 case STATE_WAIT_DHCP: |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 102 string16 DhcpProxyScriptAdapterFetcher::GetPacScript() const { | 112 string16 DhcpProxyScriptAdapterFetcher::GetPacScript() const { |
| 103 DCHECK(CalledOnValidThread()); | 113 DCHECK(CalledOnValidThread()); |
| 104 return pac_script_; | 114 return pac_script_; |
| 105 } | 115 } |
| 106 | 116 |
| 107 GURL DhcpProxyScriptAdapterFetcher::GetPacURL() const { | 117 GURL DhcpProxyScriptAdapterFetcher::GetPacURL() const { |
| 108 DCHECK(CalledOnValidThread()); | 118 DCHECK(CalledOnValidThread()); |
| 109 return pac_url_; | 119 return pac_url_; |
| 110 } | 120 } |
| 111 | 121 |
| 112 DhcpProxyScriptAdapterFetcher::WorkerThread::WorkerThread( | 122 DhcpProxyScriptAdapterFetcher::DhcpQuery::DhcpQuery() { |
| 113 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner) | |
| 114 : owner_(owner), | |
| 115 origin_loop_(base::MessageLoopProxy::current()) { | |
| 116 } | 123 } |
| 117 | 124 |
| 118 DhcpProxyScriptAdapterFetcher::WorkerThread::~WorkerThread() { | 125 DhcpProxyScriptAdapterFetcher::DhcpQuery::~DhcpQuery() { |
| 119 } | 126 } |
| 120 | 127 |
| 121 void DhcpProxyScriptAdapterFetcher::WorkerThread::Start( | 128 void DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter( |
| 122 const std::string& adapter_name) { | 129 const std::string& adapter_name) { |
| 123 bool succeeded = base::WorkerPool::PostTask( | 130 url_ = ImplGetPacURLFromDhcp(adapter_name); |
| 124 FROM_HERE, | |
| 125 NewRunnableMethod( | |
| 126 this, | |
| 127 &DhcpProxyScriptAdapterFetcher::WorkerThread::ThreadFunc, | |
| 128 adapter_name), | |
| 129 true); | |
| 130 DCHECK(succeeded); | |
| 131 } | 131 } |
| 132 | 132 |
| 133 void DhcpProxyScriptAdapterFetcher::WorkerThread::ThreadFunc( | 133 std::string DhcpProxyScriptAdapterFetcher::DhcpQuery::url() const { |
| 134 const std::string& adapter_name) { | 134 return url_; |
| 135 std::string url = ImplGetPacURLFromDhcp(adapter_name); | |
| 136 | |
| 137 bool succeeded = origin_loop_->PostTask( | |
| 138 FROM_HERE, | |
| 139 NewRunnableMethod( | |
| 140 this, | |
| 141 &DhcpProxyScriptAdapterFetcher::WorkerThread::OnThreadDone, | |
| 142 url)); | |
| 143 DCHECK(succeeded); | |
| 144 } | |
| 145 | |
| 146 void DhcpProxyScriptAdapterFetcher::WorkerThread::OnThreadDone( | |
| 147 const std::string& url) { | |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 149 if (owner_) | |
| 150 owner_->OnQueryDhcpDone(url); | |
| 151 } | 135 } |
| 152 | 136 |
| 153 std::string | 137 std::string |
| 154 DhcpProxyScriptAdapterFetcher::WorkerThread::ImplGetPacURLFromDhcp( | 138 DhcpProxyScriptAdapterFetcher::DhcpQuery::ImplGetPacURLFromDhcp( |
| 155 const std::string& adapter_name) { | 139 const std::string& adapter_name) { |
| 156 return DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp(adapter_name); | 140 return DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp(adapter_name); |
| 157 } | 141 } |
| 158 | 142 |
| 159 void DhcpProxyScriptAdapterFetcher::OnQueryDhcpDone( | 143 void DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone( |
| 160 const std::string& url) { | 144 scoped_refptr<DhcpQuery> dhcp_query) { |
| 161 DCHECK(CalledOnValidThread()); | 145 DCHECK(CalledOnValidThread()); |
| 162 // Because we can't cancel the call to the Win32 API, we can expect | 146 // Because we can't cancel the call to the Win32 API, we can expect |
| 163 // it to finish while we are in a few different states. The expected | 147 // it to finish while we are in a few different states. The expected |
| 164 // one is WAIT_DHCP, but it could be in CANCEL if Cancel() was called, | 148 // one is WAIT_DHCP, but it could be in CANCEL if Cancel() was called, |
| 165 // or FINISH if timeout occurred. | 149 // or FINISH if timeout occurred. |
| 166 DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_CANCEL || | 150 DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_CANCEL || |
| 167 state_ == STATE_FINISH); | 151 state_ == STATE_FINISH); |
| 168 if (state_ != STATE_WAIT_DHCP) | 152 if (state_ != STATE_WAIT_DHCP) |
| 169 return; | 153 return; |
| 170 | 154 |
| 171 wait_timer_.Stop(); | 155 wait_timer_.Stop(); |
| 172 | 156 |
| 173 pac_url_ = GURL(url); | 157 pac_url_ = GURL(dhcp_query->url()); |
| 174 if (pac_url_.is_empty() || !pac_url_.is_valid()) { | 158 if (pac_url_.is_empty() || !pac_url_.is_valid()) { |
| 175 result_ = ERR_PAC_NOT_IN_DHCP; | 159 result_ = ERR_PAC_NOT_IN_DHCP; |
| 176 TransitionToFinish(); | 160 TransitionToFinish(); |
| 177 } else { | 161 } else { |
| 178 state_ = STATE_WAIT_URL; | 162 state_ = STATE_WAIT_URL; |
| 179 script_fetcher_.reset(ImplCreateScriptFetcher()); | 163 script_fetcher_.reset(ImplCreateScriptFetcher()); |
| 180 script_fetcher_->Fetch(pac_url_, &pac_script_, &script_fetcher_callback_); | 164 script_fetcher_->Fetch(pac_url_, &pac_script_, &script_fetcher_callback_); |
| 181 } | 165 } |
| 182 } | 166 } |
| 183 | 167 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 212 | 196 |
| 213 DhcpProxyScriptAdapterFetcher::State | 197 DhcpProxyScriptAdapterFetcher::State |
| 214 DhcpProxyScriptAdapterFetcher::state() const { | 198 DhcpProxyScriptAdapterFetcher::state() const { |
| 215 return state_; | 199 return state_; |
| 216 } | 200 } |
| 217 | 201 |
| 218 ProxyScriptFetcher* DhcpProxyScriptAdapterFetcher::ImplCreateScriptFetcher() { | 202 ProxyScriptFetcher* DhcpProxyScriptAdapterFetcher::ImplCreateScriptFetcher() { |
| 219 return new ProxyScriptFetcherImpl(url_request_context_); | 203 return new ProxyScriptFetcherImpl(url_request_context_); |
| 220 } | 204 } |
| 221 | 205 |
| 222 DhcpProxyScriptAdapterFetcher::WorkerThread* | 206 DhcpProxyScriptAdapterFetcher::DhcpQuery* |
| 223 DhcpProxyScriptAdapterFetcher::ImplCreateWorkerThread( | 207 DhcpProxyScriptAdapterFetcher::ImplCreateDhcpQuery() { |
| 224 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner) { | 208 return new DhcpQuery(); |
| 225 return new WorkerThread(owner); | |
| 226 } | 209 } |
| 227 | 210 |
| 228 base::TimeDelta DhcpProxyScriptAdapterFetcher::ImplGetTimeout() const { | 211 base::TimeDelta DhcpProxyScriptAdapterFetcher::ImplGetTimeout() const { |
| 229 return base::TimeDelta::FromMilliseconds(kTimeoutMs); | 212 return base::TimeDelta::FromMilliseconds(kTimeoutMs); |
| 230 } | 213 } |
| 231 | 214 |
| 232 // static | 215 // static |
| 233 std::string DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp( | 216 std::string DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp( |
| 234 const std::string& adapter_name) { | 217 const std::string& adapter_name) { |
| 235 EnsureDhcpcsvcInit(); | 218 EnsureDhcpcsvcInit(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 // Return only up to the first null in case of embedded NULLs; if the | 271 // Return only up to the first null in case of embedded NULLs; if the |
| 289 // server is giving us back a buffer with embedded NULLs, something is | 272 // server is giving us back a buffer with embedded NULLs, something is |
| 290 // broken anyway. | 273 // broken anyway. |
| 291 return std::string(reinterpret_cast<const char *>(wpad_params.Data)); | 274 return std::string(reinterpret_cast<const char *>(wpad_params.Data)); |
| 292 } | 275 } |
| 293 | 276 |
| 294 return ""; | 277 return ""; |
| 295 } | 278 } |
| 296 | 279 |
| 297 } // namespace net | 280 } // namespace net |
| OLD | NEW |