| 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_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_fetcher_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 8 #include "base/perftimer.h" | 9 #include "base/perftimer.h" |
| 9 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| 10 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 11 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" | 12 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" |
| 12 | 13 |
| 13 #include <winsock2.h> | 14 #include <winsock2.h> |
| 14 #include <iphlpapi.h> | 15 #include <iphlpapi.h> |
| 15 #pragma comment(lib, "iphlpapi.lib") | 16 #pragma comment(lib, "iphlpapi.lib") |
| 16 | 17 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 NOTREACHED(); | 61 NOTREACHED(); |
| 61 return ERR_UNEXPECTED; | 62 return ERR_UNEXPECTED; |
| 62 } | 63 } |
| 63 | 64 |
| 64 fetch_start_time_ = base::TimeTicks::Now(); | 65 fetch_start_time_ = base::TimeTicks::Now(); |
| 65 | 66 |
| 66 state_ = STATE_WAIT_ADAPTERS; | 67 state_ = STATE_WAIT_ADAPTERS; |
| 67 client_callback_ = callback; | 68 client_callback_ = callback; |
| 68 destination_string_ = utf16_text; | 69 destination_string_ = utf16_text; |
| 69 | 70 |
| 70 worker_thread_ = ImplCreateWorkerThread(AsWeakPtr()); | 71 scoped_refptr<AdapterQuery> adapter_query(ImplCreateAdapterQuery()); |
| 71 worker_thread_->Start(); | 72 base::WorkerPool::PostTaskAndReply( |
| 73 FROM_HERE, |
| 74 base::Bind( |
| 75 &DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames, |
| 76 adapter_query.get()), |
| 77 base::Bind( |
| 78 &DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone, |
| 79 AsWeakPtr(), |
| 80 adapter_query), |
| 81 true); |
| 72 | 82 |
| 73 return ERR_IO_PENDING; | 83 return ERR_IO_PENDING; |
| 74 } | 84 } |
| 75 | 85 |
| 76 void DhcpProxyScriptFetcherWin::Cancel() { | 86 void DhcpProxyScriptFetcherWin::Cancel() { |
| 77 DCHECK(CalledOnValidThread()); | 87 DCHECK(CalledOnValidThread()); |
| 78 | 88 |
| 79 if (state_ != STATE_DONE) { | 89 if (state_ != STATE_DONE) { |
| 80 // We only count this stat if the cancel was explicitly initiated by | 90 // We only count this stat if the cancel was explicitly initiated by |
| 81 // our client, and if we weren't already in STATE_DONE. | 91 // our client, and if we weren't already in STATE_DONE. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 98 it != fetchers_.end(); | 108 it != fetchers_.end(); |
| 99 ++it) { | 109 ++it) { |
| 100 (*it)->Cancel(); | 110 (*it)->Cancel(); |
| 101 } | 111 } |
| 102 | 112 |
| 103 fetchers_.reset(); | 113 fetchers_.reset(); |
| 104 } | 114 } |
| 105 } | 115 } |
| 106 | 116 |
| 107 void DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone( | 117 void DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone( |
| 108 const std::set<std::string>& adapter_names) { | 118 scoped_refptr<AdapterQuery> query) { |
| 109 DCHECK(CalledOnValidThread()); | 119 DCHECK(CalledOnValidThread()); |
| 110 | 120 |
| 121 // Enable unit tests to wait for this to happen; in production this function |
| 122 // call is a no-op. |
| 123 ImplOnGetCandidateAdapterNamesDone(); |
| 124 |
| 111 // We may have been cancelled. | 125 // We may have been cancelled. |
| 112 if (state_ != STATE_WAIT_ADAPTERS) | 126 if (state_ != STATE_WAIT_ADAPTERS) |
| 113 return; | 127 return; |
| 114 | 128 |
| 115 state_ = STATE_NO_RESULTS; | 129 state_ = STATE_NO_RESULTS; |
| 116 | 130 |
| 131 const std::set<std::string>& adapter_names = query->adapter_names(); |
| 132 |
| 117 if (adapter_names.empty()) { | 133 if (adapter_names.empty()) { |
| 118 TransitionToDone(); | 134 TransitionToDone(); |
| 119 return; | 135 return; |
| 120 } | 136 } |
| 121 | 137 |
| 122 for (std::set<std::string>::const_iterator it = adapter_names.begin(); | 138 for (std::set<std::string>::const_iterator it = adapter_names.begin(); |
| 123 it != adapter_names.end(); | 139 it != adapter_names.end(); |
| 124 ++it) { | 140 ++it) { |
| 125 DhcpProxyScriptAdapterFetcher* fetcher(ImplCreateAdapterFetcher()); | 141 DhcpProxyScriptAdapterFetcher* fetcher(ImplCreateAdapterFetcher()); |
| 126 fetcher->Fetch(*it, &fetcher_callback_); | 142 fetcher->Fetch(*it, &fetcher_callback_); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 263 |
| 248 URLRequestContext* DhcpProxyScriptFetcherWin::url_request_context() const { | 264 URLRequestContext* DhcpProxyScriptFetcherWin::url_request_context() const { |
| 249 return url_request_context_; | 265 return url_request_context_; |
| 250 } | 266 } |
| 251 | 267 |
| 252 DhcpProxyScriptAdapterFetcher* | 268 DhcpProxyScriptAdapterFetcher* |
| 253 DhcpProxyScriptFetcherWin::ImplCreateAdapterFetcher() { | 269 DhcpProxyScriptFetcherWin::ImplCreateAdapterFetcher() { |
| 254 return new DhcpProxyScriptAdapterFetcher(url_request_context_); | 270 return new DhcpProxyScriptAdapterFetcher(url_request_context_); |
| 255 } | 271 } |
| 256 | 272 |
| 257 DhcpProxyScriptFetcherWin::WorkerThread* | 273 DhcpProxyScriptFetcherWin::AdapterQuery* |
| 258 DhcpProxyScriptFetcherWin::ImplCreateWorkerThread( | 274 DhcpProxyScriptFetcherWin::ImplCreateAdapterQuery() { |
| 259 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) { | 275 return new AdapterQuery(); |
| 260 return new WorkerThread(owner); | |
| 261 } | 276 } |
| 262 | 277 |
| 263 int DhcpProxyScriptFetcherWin::ImplGetMaxWaitMs() { | 278 int DhcpProxyScriptFetcherWin::ImplGetMaxWaitMs() { |
| 264 return kMaxWaitAfterFirstResultMs; | 279 return kMaxWaitAfterFirstResultMs; |
| 265 } | 280 } |
| 266 | 281 |
| 267 bool DhcpProxyScriptFetcherWin::GetCandidateAdapterNames( | 282 bool DhcpProxyScriptFetcherWin::GetCandidateAdapterNames( |
| 268 std::set<std::string>* adapter_names) { | 283 std::set<std::string>* adapter_names) { |
| 269 DCHECK(adapter_names); | 284 DCHECK(adapter_names); |
| 270 adapter_names->clear(); | 285 adapter_names->clear(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 if ((adapter->Flags & IP_ADAPTER_DHCP_ENABLED) == 0) | 338 if ((adapter->Flags & IP_ADAPTER_DHCP_ENABLED) == 0) |
| 324 continue; | 339 continue; |
| 325 | 340 |
| 326 DCHECK(adapter->AdapterName); | 341 DCHECK(adapter->AdapterName); |
| 327 adapter_names->insert(adapter->AdapterName); | 342 adapter_names->insert(adapter->AdapterName); |
| 328 } | 343 } |
| 329 | 344 |
| 330 return true; | 345 return true; |
| 331 } | 346 } |
| 332 | 347 |
| 333 DhcpProxyScriptFetcherWin::WorkerThread::WorkerThread( | 348 DhcpProxyScriptFetcherWin::AdapterQuery::AdapterQuery() { |
| 334 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) { | |
| 335 Init(owner); | |
| 336 } | 349 } |
| 337 | 350 |
| 338 DhcpProxyScriptFetcherWin::WorkerThread::~WorkerThread() { | 351 DhcpProxyScriptFetcherWin::AdapterQuery::~AdapterQuery() { |
| 339 } | 352 } |
| 340 | 353 |
| 341 void DhcpProxyScriptFetcherWin::WorkerThread::Start() { | 354 void DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames() { |
| 342 bool succeeded = base::WorkerPool::PostTask( | 355 ImplGetCandidateAdapterNames(&adapter_names_); |
| 343 FROM_HERE, | |
| 344 NewRunnableMethod( | |
| 345 this, | |
| 346 &DhcpProxyScriptFetcherWin::WorkerThread::ThreadFunc), | |
| 347 true); | |
| 348 DCHECK(succeeded); | |
| 349 } | 356 } |
| 350 | 357 |
| 351 void DhcpProxyScriptFetcherWin::WorkerThread::ThreadFunc() { | 358 const std::set<std::string>& |
| 352 ImplGetCandidateAdapterNames(&adapter_names_); | 359 DhcpProxyScriptFetcherWin::AdapterQuery::adapter_names() const { |
| 353 | 360 return adapter_names_; |
| 354 bool succeeded = origin_loop_->PostTask( | |
| 355 FROM_HERE, | |
| 356 NewRunnableMethod( | |
| 357 this, | |
| 358 &DhcpProxyScriptFetcherWin::WorkerThread::OnThreadDone)); | |
| 359 DCHECK(succeeded); | |
| 360 } | 361 } |
| 361 | 362 |
| 362 void DhcpProxyScriptFetcherWin::WorkerThread::OnThreadDone() { | 363 bool DhcpProxyScriptFetcherWin::AdapterQuery::ImplGetCandidateAdapterNames( |
| 363 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 364 if (owner_) | |
| 365 owner_->OnGetCandidateAdapterNamesDone(adapter_names_); | |
| 366 } | |
| 367 | |
| 368 DhcpProxyScriptFetcherWin::WorkerThread::WorkerThread() { | |
| 369 } | |
| 370 | |
| 371 void DhcpProxyScriptFetcherWin::WorkerThread::Init( | |
| 372 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) { | |
| 373 owner_ = owner; | |
| 374 origin_loop_ = base::MessageLoopProxy::current(); | |
| 375 } | |
| 376 | |
| 377 bool DhcpProxyScriptFetcherWin::WorkerThread::ImplGetCandidateAdapterNames( | |
| 378 std::set<std::string>* adapter_names) { | 364 std::set<std::string>* adapter_names) { |
| 379 return DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(adapter_names); | 365 return DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(adapter_names); |
| 380 } | 366 } |
| 381 | 367 |
| 382 } // namespace net | 368 } // namespace net |
| OLD | NEW |