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

Side by Side Diff: net/proxy/dhcp_proxy_script_adapter_fetcher_win.h

Issue 6831025: Adds support for the DHCP portion of the WPAD (proxy auto-discovery) protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Isolate worker thread to separate object. Refcount context. Fix mandatory PAC. Created 9 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_
6 #define NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_
7 #pragma once
8
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/string16.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/timer.h"
15 #include "net/base/completion_callback.h"
16 #include "googleurl/src/gurl.h"
17
18 namespace base {
19 class MessageLoopProxy;
20 }
21
22 namespace net {
23
24 class ProxyScriptFetcher;
25 class URLRequestContext;
26
27 // For a given adapter, this class takes care of first doing a DHCP lookup
28 // to get the PAC URL, then if there is one, trying to fetch it.
29 //
30 // Non thread-safe for clients, but does some internal multi-threading.
31 class DhcpProxyScriptAdapterFetcher
32 : public base::SupportsWeakPtr<DhcpProxyScriptAdapterFetcher>,
33 public base::NonThreadSafe {
34 public:
35 // |url_request_context| must outlive DhcpProxyScriptAdapterFetcher.
36 explicit DhcpProxyScriptAdapterFetcher(
37 URLRequestContext* url_request_context);
38 virtual ~DhcpProxyScriptAdapterFetcher();
39
40 // Starts a fetch. On completion (but not cancellation), |callback|
41 // will be invoked with the network error indicating success or failure
42 // of fetching a DHCP-configured PAC file on this adapter.
43 //
44 // You may only call Fetch() once on a given instance of
45 // DhcpProxyScriptAdapterFetcher.
eroman 2011/05/17 04:39:39 could also mention that on completion result is ob
Jói 2011/05/17 15:38:35 Done.
46 virtual void Fetch(const std::string& adapter_name,
47 CompletionCallback* callback);
48
49 // Cancels the fetch on this adapter.
50 virtual void Cancel();
51
52 // Returns true if in the FINISH state (not CANCEL).
53 virtual bool DidFinish() const;
54
55 // Returns the network error indicating the result of the fetch. Will
56 // return IO_PENDING until the fetch is complete or cancelled.
57 virtual int GetResult() const;
eroman 2011/05/17 04:39:39 Is this not the same as the integer passed to the
Jói 2011/05/17 15:38:35 It is. The implementation of going through the fet
58
59 // Returns the contents of the PAC file retrieved. Only valid if
60 // |IsComplete()| is true. Returns the empty string if |GetResult()|
61 // returns anything other than OK.
62 virtual string16 GetPacScript() const;
63
64 // Returns the PAC URL retrieved from DHCP. Only guaranteed to be
65 // valid if |IsComplete()| is true. Returns an empty URL if no URL was
66 // configured in DHCP. May return a valid URL even if |result()| does
67 // not return OK (this would indicate that we found a URL configured in
68 // DHCP but failed to download it).
69 virtual GURL GetPacURL() const;
70
71 // Returns the PAC URL configured in DHCP for the given |adapter_name|, or
72 // the empty string if none is configured.
73 //
74 // This function executes synchronously due to limitations of the Windows
75 // DHCP client API.
76 static std::string GetPacURLFromDhcp(const std::string& adapter_name);
77
78 protected:
79 // This inner class is used to encapsulate the worker thread, which has
80 // only a weak reference back to the main object, so that the main object
81 // can be destroyed before the thread ends. This also keeps the main
82 // object completely thread safe and allows it to be non-refcounted.
83 class WorkerThread : public base::RefCountedThreadSafe<WorkerThread> {
eroman 2011/05/17 04:39:39 (optional) WorkerThread sounds like it should be a
Jói 2011/05/17 15:38:35 I'll leave it as is since it encapsulates our use
84 public:
85 // Creates and initializes (but does not start) the worker thread.
86 explicit WorkerThread(
eroman 2011/05/17 04:39:39 Please also add a virtual destructor to prevent ac
Jói 2011/05/17 15:38:35 Doh, I have to stop forgetting this. Thanks for c
87 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner);
88
89 // Starts the worker thread, fetching information for |adapter_name| using
90 // |get_pac_from_url_func|.
91 void Start(const std::string& adapter_name);
92
93 // This is the method that runs on the worker thread.
94 void ThreadFunc(const std::string& adapter_name);
eroman 2011/05/17 04:39:39 can these two methods be private?
Jói 2011/05/17 15:38:35 Done.
95
96 // Callback for the above; this executes back on the main thread,
97 // not the worker thread.
98 void OnThreadDone(const std::string& url);
99
100 protected:
101 // Unit testing seam.
eroman 2011/05/17 04:39:39 what is a "seam"?
Jói 2011/05/17 15:38:35 Sorry, I thought it was common terminology; it's u
102 virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name);
103
104 private:
105 // All work except QueryDHcpOnWorkerThread and destruction should
eroman 2011/05/17 04:39:39 ThreadFunc?
Jói 2011/05/17 15:38:35 Done.
106 // occur on the thread that constructs the object.
107 base::ThreadChecker thread_checker_;
108
109 // May only be accessed on the thread that constructs the object.
110 base::WeakPtr<DhcpProxyScriptAdapterFetcher> owner_;
eroman 2011/05/17 04:39:39 I like your general approach of keeping the parent
Jói 2011/05/17 15:38:35 Thanks. The weak_ptr.h header file has a comment
111
112 // Used by worker thread to post a message back to the original
113 // thread. Fine to use a proxy since in the case where the original
114 // thread has gone away, that would mean this object is in state
eroman 2011/05/17 04:39:39 nit: not sure I understand this comment. CANCEL is
Jói 2011/05/17 15:38:35 Copy/paste error. Updated the comment to: //
115 // CANCEL and no further processing should be done.
116 scoped_refptr<base::MessageLoopProxy> origin_loop_;
117
118 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
119 };
120
121 // Event/state transition handlers
122 void OnQueryDhcpDone(const std::string& url);
123 void OnTimeout();
124 void OnFetcherDone(int result);
125 void TransitionToFinish();
126
127 // Virtual methods introduced to allow unit testing.
128 virtual ProxyScriptFetcher* ImplCreateScriptFetcher();
129 virtual WorkerThread* ImplCreateWorkerThread(
130 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner);
131 virtual base::TimeDelta ImplGetTimeout() const;
132
133 // This is the state machine for fetching from a given adapter.
134 //
135 // The state machine goes from START->WAIT_DHCP when it starts
136 // a worker thread to fetch the PAC URL from DHCP.
137 //
138 // In state WAIT_DHCP, if the DHCP query finishes and has no URL, it
139 // moves to state FINISH. If there is a URL, it starts a
140 // ProxyScriptFetcher to fetch it and moves to state WAIT_URL.
141 //
142 // It goes from WAIT_URL->FINISH when the ProxyScriptFetcher completes.
143 //
144 // In state FINISH, completion is indicated to the outer class, with
145 // the results of the fetch if a PAC script was successfully fetched.
146 //
147 // In state WAIT_DHCP, our timeout occurring can push us to FINISH.
148 //
149 // In any state except FINISH, a call to Cancel() will move to state
150 // CANCEL and cause all outstanding work to be cancelled or its
151 // results ignored when available.
152 enum State {
153 STATE_START,
154 STATE_WAIT_DHCP,
155 STATE_WAIT_URL,
156 STATE_FINISH,
157 STATE_CANCEL,
158 };
159
160 // Current state of this state machine.
161 State state_;
162
163 // A network error indicating result of operation.
164 int result_;
165
166 // Empty string or the PAC script downloaded.
167 string16 pac_script_;
168
169 // Empty URL or the PAC URL configured in DHCP.
170 GURL pac_url_;
171
172 // Callback to let our client know we're done. Invalid in states
173 // START, FINISH and CANCEL.
174 CompletionCallback* callback_;
175
176 // Container for our worker thread. NULL if not currently running.
177 scoped_refptr<WorkerThread> worker_thread_;
178
179 // Fetcher to retrieve PAC files once URL is known.
180 scoped_ptr<ProxyScriptFetcher> script_fetcher_;
181
182 // Callback from the script fetcher.
183 CompletionCallbackImpl<DhcpProxyScriptAdapterFetcher>
184 script_fetcher_callback_;
185
186 // Implements a timeout on the call to the Win32 DHCP API.
187 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_;
188
189 scoped_refptr<URLRequestContext> url_request_context_;
190
191 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher);
192 };
193
194 } // namespace net
195
196 #endif // NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698