summaryrefslogtreecommitdiffstats
path: root/net/proxy/dhcp_proxy_script_fetcher_win.h
diff options
context:
space:
mode:
authorjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-23 20:34:58 +0000
committerjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-23 20:34:58 +0000
commitc24ebc31b665f800a3d51eea012d91bc2728364d (patch)
treedc717b354436761c3de6ab1fe30e1925293a4b8f /net/proxy/dhcp_proxy_script_fetcher_win.h
parentff04c53708ec68d1f6b1aefa2d44df1725cdf7eb (diff)
downloadchromium_src-c24ebc31b665f800a3d51eea012d91bc2728364d.zip
chromium_src-c24ebc31b665f800a3d51eea012d91bc2728364d.tar.gz
chromium_src-c24ebc31b665f800a3d51eea012d91bc2728364d.tar.bz2
Do GetAdaptersAddresses on a worker thread.
BUG=84047 TEST=net_unittests Review URL: http://codereview.chromium.org/7189016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90258 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/proxy/dhcp_proxy_script_fetcher_win.h')
-rw-r--r--net/proxy/dhcp_proxy_script_fetcher_win.h69
1 files changed, 66 insertions, 3 deletions
diff --git a/net/proxy/dhcp_proxy_script_fetcher_win.h b/net/proxy/dhcp_proxy_script_fetcher_win.h
index a220f56..ae9548a 100644
--- a/net/proxy/dhcp_proxy_script_fetcher_win.h
+++ b/net/proxy/dhcp_proxy_script_fetcher_win.h
@@ -11,6 +11,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
+#include "base/message_loop_proxy.h"
#include "base/threading/non_thread_safe.h"
#include "base/time.h"
#include "base/timer.h"
@@ -24,6 +25,7 @@ class URLRequestContext;
// Windows-specific implementation.
class NET_TEST DhcpProxyScriptFetcherWin
: public DhcpProxyScriptFetcher,
+ public base::SupportsWeakPtr<DhcpProxyScriptFetcherWin>,
NON_EXPORTED_BASE(public base::NonThreadSafe) {
public:
// Creates a DhcpProxyScriptFetcherWin that issues requests through
@@ -48,15 +50,71 @@ class NET_TEST DhcpProxyScriptFetcherWin
URLRequestContext* url_request_context() const;
+ // This inner class is used to encapsulate a worker thread that calls
+ // GetCandidateAdapterNames as it can take a couple of hundred
+ // milliseconds.
+ //
+ // TODO(joi): Replace with PostTaskAndReply once http://crbug.com/86301
+ // has been implemented.
+ class NET_TEST WorkerThread
+ : public base::RefCountedThreadSafe<WorkerThread> {
+ public:
+ // Creates and initializes (but does not start) the worker thread.
+ explicit WorkerThread(
+ const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner);
+ virtual ~WorkerThread();
+
+ // Starts the worker thread.
+ void Start();
+
+ protected:
+ WorkerThread(); // To override in unit tests only.
+ void Init(const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner);
+
+ // Virtual method introduced to allow unit testing.
+ virtual bool ImplGetCandidateAdapterNames(
+ std::set<std::string>* adapter_names);
+
+ // Callback for ThreadFunc; this executes back on the main thread,
+ // not the worker thread. May be overridden by unit tests.
+ virtual void OnThreadDone();
+
+ private:
+ // This is the method that runs on the worker thread.
+ void ThreadFunc();
+
+ // All work except ThreadFunc and (sometimes) destruction should occur
+ // on the thread that constructs the object.
+ base::ThreadChecker thread_checker_;
+
+ // May only be accessed on the thread that constructs the object.
+ base::WeakPtr<DhcpProxyScriptFetcherWin> owner_;
+
+ // Used by worker thread to post a message back to the original
+ // thread. Fine to use a proxy since in the case where the original
+ // thread has gone away, that would mean the |owner_| object is gone
+ // anyway, so there is nobody to receive the result.
+ scoped_refptr<base::MessageLoopProxy> origin_loop_;
+
+ // This is constructed on the originating thread, then used on the
+ // worker thread, then used again on the originating thread only when
+ // the task has completed on the worker thread. No locking required.
+ std::set<std::string> adapter_names_;
+
+ DISALLOW_COPY_AND_ASSIGN(WorkerThread);
+ };
+
// Virtual methods introduced to allow unit testing.
virtual DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher();
- virtual bool ImplGetCandidateAdapterNames(
- std::set<std::string>* adapter_names);
+ virtual WorkerThread* ImplCreateWorkerThread(
+ const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner);
virtual int ImplGetMaxWaitMs();
private:
// Event/state transition handlers
void CancelImpl();
+ void OnGetCandidateAdapterNamesDone(
+ const std::set<std::string>& adapter_names);
void OnFetcherDone(int result);
void OnWaitTimer();
void TransitionToDone();
@@ -73,7 +131,9 @@ class NET_TEST DhcpProxyScriptFetcherWin
// it has completed for the fastest adapter, return the PAC file
// available for the most preferred network adapter, if any.
//
- // The state machine goes from START->NO_RESULTS when it creates
+ // The state machine goes from START->WAIT_ADAPTERS when it starts a
+ // worker thread to get the list of adapters with DHCP enabled.
+ // It then goes from WAIT_ADAPTERS->NO_RESULTS when it creates
// and starts an DhcpProxyScriptAdapterFetcher for each adapter. It goes
// from NO_RESULTS->SOME_RESULTS when it gets the first result; at this
// point a wait timer is started. It goes from SOME_RESULTS->DONE in
@@ -88,6 +148,7 @@ class NET_TEST DhcpProxyScriptFetcherWin
// allowed to be outstanding at any given time.
enum State {
STATE_START,
+ STATE_WAIT_ADAPTERS,
STATE_NO_RESULTS,
STATE_SOME_RESULTS,
STATE_DONE,
@@ -122,6 +183,8 @@ class NET_TEST DhcpProxyScriptFetcherWin
scoped_refptr<URLRequestContext> url_request_context_;
+ scoped_refptr<WorkerThread> worker_thread_;
+
// Time |Fetch()| was last called, 0 if never.
base::TimeTicks fetch_start_time_;