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

Unified 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: Merge to lkgr 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 side-by-side diff with in-line comments
Download patch
Index: net/proxy/dhcp_proxy_script_fetcher_win.cc
diff --git a/net/proxy/dhcp_proxy_script_fetcher_win.cc b/net/proxy/dhcp_proxy_script_fetcher_win.cc
index dced471f8f89948ff9ecf9a6de115784984f0847..ebbe86e7377a16f6b018477498940a82d212bdd6 100644
--- a/net/proxy/dhcp_proxy_script_fetcher_win.cc
+++ b/net/proxy/dhcp_proxy_script_fetcher_win.cc
@@ -4,6 +4,7 @@
#include "net/proxy/dhcp_proxy_script_fetcher_win.h"
+#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "base/perftimer.h"
#include "base/threading/worker_pool.h"
@@ -67,8 +68,17 @@ int DhcpProxyScriptFetcherWin::Fetch(string16* utf16_text,
client_callback_ = callback;
destination_string_ = utf16_text;
- worker_thread_ = ImplCreateWorkerThread(AsWeakPtr());
- worker_thread_->Start();
+ last_query_ = ImplCreateAdapterQuery();
+ base::WorkerPool::PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(
+ &DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames,
+ last_query_.get()),
+ base::Bind(
+ &DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone,
+ AsWeakPtr(),
+ last_query_),
+ true);
return ERR_IO_PENDING;
}
@@ -105,15 +115,27 @@ void DhcpProxyScriptFetcherWin::CancelImpl() {
}
void DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone(
- const std::set<std::string>& adapter_names) {
+ scoped_refptr<AdapterQuery> query) {
DCHECK(CalledOnValidThread());
+ // This can happen if this object is reused for multiple queries,
+ // and a previous query was cancelled before it completed.
+ if (query.get() != last_query_.get())
+ return;
+ last_query_.release();
willchan no longer on Chromium 2011/10/13 22:33:45 Did you really mean release()? Not reset()? Is the
Jói 2011/10/13 22:46:07 Argh, that's a bad method name, thanks for catchin
+
+ // Enable unit tests to wait for this to happen; in production this function
+ // call is a no-op.
+ ImplOnGetCandidateAdapterNamesDone();
+
// We may have been cancelled.
if (state_ != STATE_WAIT_ADAPTERS)
return;
state_ = STATE_NO_RESULTS;
+ const std::set<std::string>& adapter_names = query->adapter_names();
+
if (adapter_names.empty()) {
TransitionToDone();
return;
@@ -254,10 +276,9 @@ DhcpProxyScriptAdapterFetcher*
return new DhcpProxyScriptAdapterFetcher(url_request_context_);
}
-DhcpProxyScriptFetcherWin::WorkerThread*
- DhcpProxyScriptFetcherWin::ImplCreateWorkerThread(
- const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) {
- return new WorkerThread(owner);
+DhcpProxyScriptFetcherWin::AdapterQuery*
+ DhcpProxyScriptFetcherWin::ImplCreateAdapterQuery() {
+ return new AdapterQuery();
}
int DhcpProxyScriptFetcherWin::ImplGetMaxWaitMs() {
@@ -330,51 +351,22 @@ bool DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(
return true;
}
-DhcpProxyScriptFetcherWin::WorkerThread::WorkerThread(
- const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) {
- Init(owner);
-}
-
-DhcpProxyScriptFetcherWin::WorkerThread::~WorkerThread() {
+DhcpProxyScriptFetcherWin::AdapterQuery::AdapterQuery() {
}
-void DhcpProxyScriptFetcherWin::WorkerThread::Start() {
- bool succeeded = base::WorkerPool::PostTask(
- FROM_HERE,
- NewRunnableMethod(
- this,
- &DhcpProxyScriptFetcherWin::WorkerThread::ThreadFunc),
- true);
- DCHECK(succeeded);
+DhcpProxyScriptFetcherWin::AdapterQuery::~AdapterQuery() {
}
-void DhcpProxyScriptFetcherWin::WorkerThread::ThreadFunc() {
+void DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames() {
ImplGetCandidateAdapterNames(&adapter_names_);
-
- bool succeeded = origin_loop_->PostTask(
- FROM_HERE,
- NewRunnableMethod(
- this,
- &DhcpProxyScriptFetcherWin::WorkerThread::OnThreadDone));
- DCHECK(succeeded);
-}
-
-void DhcpProxyScriptFetcherWin::WorkerThread::OnThreadDone() {
- DCHECK(thread_checker_.CalledOnValidThread());
- if (owner_)
- owner_->OnGetCandidateAdapterNamesDone(adapter_names_);
-}
-
-DhcpProxyScriptFetcherWin::WorkerThread::WorkerThread() {
}
-void DhcpProxyScriptFetcherWin::WorkerThread::Init(
- const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) {
- owner_ = owner;
- origin_loop_ = base::MessageLoopProxy::current();
+const std::set<std::string>&
+ DhcpProxyScriptFetcherWin::AdapterQuery::adapter_names() const {
+ return adapter_names_;
}
-bool DhcpProxyScriptFetcherWin::WorkerThread::ImplGetCandidateAdapterNames(
+bool DhcpProxyScriptFetcherWin::AdapterQuery::ImplGetCandidateAdapterNames(
std::set<std::string>* adapter_names) {
return DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(adapter_names);
}

Powered by Google App Engine
This is Rietveld 408576698