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

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

Issue 8139028: Add WorkerPool::PostTaskAndReply and use in DHCP code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Mark private Created 9 years, 2 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 #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
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 last_query_ = ImplCreateAdapterQuery();
71 worker_thread_->Start(); 72 base::WorkerPool::PostTaskAndReply(
73 FROM_HERE,
74 base::Bind(
75 &DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames,
76 last_query_.get()),
77 base::Bind(
78 &DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone,
79 AsWeakPtr(),
80 last_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
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 // This can happen if this object is reused for multiple queries,
122 // and a previous query was cancelled before it completed.
123 if (query.get() != last_query_.get())
124 return;
125 last_query_ = NULL;
126
127 // Enable unit tests to wait for this to happen; in production this function
128 // call is a no-op.
129 ImplOnGetCandidateAdapterNamesDone();
130
111 // We may have been cancelled. 131 // We may have been cancelled.
112 if (state_ != STATE_WAIT_ADAPTERS) 132 if (state_ != STATE_WAIT_ADAPTERS)
113 return; 133 return;
114 134
115 state_ = STATE_NO_RESULTS; 135 state_ = STATE_NO_RESULTS;
116 136
137 const std::set<std::string>& adapter_names = query->adapter_names();
138
117 if (adapter_names.empty()) { 139 if (adapter_names.empty()) {
118 TransitionToDone(); 140 TransitionToDone();
119 return; 141 return;
120 } 142 }
121 143
122 for (std::set<std::string>::const_iterator it = adapter_names.begin(); 144 for (std::set<std::string>::const_iterator it = adapter_names.begin();
123 it != adapter_names.end(); 145 it != adapter_names.end();
124 ++it) { 146 ++it) {
125 DhcpProxyScriptAdapterFetcher* fetcher(ImplCreateAdapterFetcher()); 147 DhcpProxyScriptAdapterFetcher* fetcher(ImplCreateAdapterFetcher());
126 fetcher->Fetch(*it, &fetcher_callback_); 148 fetcher->Fetch(*it, &fetcher_callback_);
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 269
248 URLRequestContext* DhcpProxyScriptFetcherWin::url_request_context() const { 270 URLRequestContext* DhcpProxyScriptFetcherWin::url_request_context() const {
249 return url_request_context_; 271 return url_request_context_;
250 } 272 }
251 273
252 DhcpProxyScriptAdapterFetcher* 274 DhcpProxyScriptAdapterFetcher*
253 DhcpProxyScriptFetcherWin::ImplCreateAdapterFetcher() { 275 DhcpProxyScriptFetcherWin::ImplCreateAdapterFetcher() {
254 return new DhcpProxyScriptAdapterFetcher(url_request_context_); 276 return new DhcpProxyScriptAdapterFetcher(url_request_context_);
255 } 277 }
256 278
257 DhcpProxyScriptFetcherWin::WorkerThread* 279 DhcpProxyScriptFetcherWin::AdapterQuery*
258 DhcpProxyScriptFetcherWin::ImplCreateWorkerThread( 280 DhcpProxyScriptFetcherWin::ImplCreateAdapterQuery() {
259 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) { 281 return new AdapterQuery();
260 return new WorkerThread(owner);
261 } 282 }
262 283
263 int DhcpProxyScriptFetcherWin::ImplGetMaxWaitMs() { 284 int DhcpProxyScriptFetcherWin::ImplGetMaxWaitMs() {
264 return kMaxWaitAfterFirstResultMs; 285 return kMaxWaitAfterFirstResultMs;
265 } 286 }
266 287
267 bool DhcpProxyScriptFetcherWin::GetCandidateAdapterNames( 288 bool DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(
268 std::set<std::string>* adapter_names) { 289 std::set<std::string>* adapter_names) {
269 DCHECK(adapter_names); 290 DCHECK(adapter_names);
270 adapter_names->clear(); 291 adapter_names->clear();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 if ((adapter->Flags & IP_ADAPTER_DHCP_ENABLED) == 0) 344 if ((adapter->Flags & IP_ADAPTER_DHCP_ENABLED) == 0)
324 continue; 345 continue;
325 346
326 DCHECK(adapter->AdapterName); 347 DCHECK(adapter->AdapterName);
327 adapter_names->insert(adapter->AdapterName); 348 adapter_names->insert(adapter->AdapterName);
328 } 349 }
329 350
330 return true; 351 return true;
331 } 352 }
332 353
333 DhcpProxyScriptFetcherWin::WorkerThread::WorkerThread( 354 DhcpProxyScriptFetcherWin::AdapterQuery::AdapterQuery() {
334 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) {
335 Init(owner);
336 } 355 }
337 356
338 DhcpProxyScriptFetcherWin::WorkerThread::~WorkerThread() { 357 DhcpProxyScriptFetcherWin::AdapterQuery::~AdapterQuery() {
339 } 358 }
340 359
341 void DhcpProxyScriptFetcherWin::WorkerThread::Start() { 360 void DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames() {
342 bool succeeded = base::WorkerPool::PostTask( 361 ImplGetCandidateAdapterNames(&adapter_names_);
343 FROM_HERE,
344 NewRunnableMethod(
345 this,
346 &DhcpProxyScriptFetcherWin::WorkerThread::ThreadFunc),
347 true);
348 DCHECK(succeeded);
349 } 362 }
350 363
351 void DhcpProxyScriptFetcherWin::WorkerThread::ThreadFunc() { 364 const std::set<std::string>&
352 ImplGetCandidateAdapterNames(&adapter_names_); 365 DhcpProxyScriptFetcherWin::AdapterQuery::adapter_names() const {
353 366 return adapter_names_;
354 bool succeeded = origin_loop_->PostTask(
355 FROM_HERE,
356 NewRunnableMethod(
357 this,
358 &DhcpProxyScriptFetcherWin::WorkerThread::OnThreadDone));
359 DCHECK(succeeded);
360 } 367 }
361 368
362 void DhcpProxyScriptFetcherWin::WorkerThread::OnThreadDone() { 369 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) { 370 std::set<std::string>* adapter_names) {
379 return DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(adapter_names); 371 return DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(adapter_names);
380 } 372 }
381 373
382 } // namespace net 374 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/dhcp_proxy_script_fetcher_win.h ('k') | net/proxy/dhcp_proxy_script_fetcher_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698