OLD | NEW |
---|---|
(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/threading/non_thread_safe.h" | |
11 #include "base/timer.h" | |
12 #include "net/proxy/proxy_script_fetcher_impl.h" | |
eroman
2011/04/21 05:22:48
Is this header necessary? Could we just include pr
Jói
2011/05/03 21:20:59
Moved to .cc file.
| |
13 | |
14 namespace base { | |
15 class MessageLoopProxy; | |
16 } | |
17 | |
18 namespace net { | |
19 | |
20 // For a given adapter, this class takes care of first doing a DHCP lookup | |
21 // to get the PAC URL, then if there is one, trying to fetch it. | |
22 // | |
23 // Non thread-safe for clients, but does some internal multi-threading. | |
24 class DhcpProxyScriptAdapterFetcher | |
25 : public base::RefCountedThreadSafe<DhcpProxyScriptAdapterFetcher>, | |
26 public base::NonThreadSafe { | |
eroman
2011/04/21 05:22:48
Feels a bit weird to call this nonthreadsafe, sinc
Jói
2011/04/21 15:57:38
The public interface is non-threadsafe, so I think
| |
27 public: | |
28 // |url_request_context| must outlive DhcpProxyScriptAdapterFetcher. | |
29 explicit DhcpProxyScriptAdapterFetcher( | |
30 URLRequestContext* url_request_context); | |
31 ~DhcpProxyScriptAdapterFetcher(); | |
eroman
2011/04/21 05:22:48
I always recommend making destructors virtual any
Jói
2011/05/03 21:20:59
Now virtual, thanks for catching that.
| |
32 | |
33 // Starts a fetch. DhcpProxyScriptAdapterFetcher takes ownership of | |
34 // |adapter_name|. On completion (but not cancellation), |callback| | |
eroman
2011/04/21 05:22:48
what does it mean to take ownership of |adapter_na
Jói
2011/04/21 15:57:38
Sorry, will fix the comment, that is a holdover fr
| |
35 // will be invoked with the network error indicating success or failure | |
36 // of fetching a DHCP-configured PAC file on this adapter. | |
37 // | |
38 // You may only call Fetch() once on a given instance of | |
39 // DhcpProxyScriptAdapterFetcher. | |
40 virtual void Fetch(const std::string& adapter_name, | |
41 CompletionCallback* callback); | |
42 | |
43 // Cancels the fetch on this adapter. | |
44 virtual void Cancel(); | |
45 | |
46 // Returns true if in the FINISH state (not CANCEL). | |
47 virtual bool DidFinish() const; | |
48 | |
49 // Returns the network error indicating the result of the fetch. Will | |
50 // return IO_PENDING until the fetch is complete or cancelled. | |
51 virtual int result() const; | |
52 | |
53 // Returns the contents of the PAC file retrieved. Only valid if | |
54 // |IsComplete()| is true. Returns the empty string if |GetResult()| | |
55 // returns anything other than OK. | |
56 virtual string16 pac_script() const; | |
eroman
2011/04/21 05:22:48
Don't use hacker_notation() for virtuals. (I know
Jói
2011/05/03 21:20:59
Done.
| |
57 | |
58 // Returns the PAC URL retrieved from DHCP. Only guaranteed to be | |
59 // valid if |IsComplete()| is true. Returns an empty URL if no URL was | |
60 // configured in DHCP. May return a valid URL even if |result()| does | |
61 // not return OK (this would indicate that we found a URL configured in | |
62 // DHCP but failed to download it). | |
63 virtual GURL pac_url() const; | |
eroman
2011/04/21 05:22:48
ditto.
Jói
2011/05/03 21:20:59
Done.
| |
64 | |
65 // Returns the PAC URL configured in DHCP for the given |adapter_name|, or | |
66 // the empty string if none is configured. | |
67 // | |
68 // This function executes synchronously due to limitations of the Windows | |
69 // DHCP client API. | |
70 static std::string GetPacURLFromDhcp(const std::string& adapter_name); | |
71 | |
72 protected: | |
73 // This queries the synchronous Win32 API we need to use on the worker | |
74 // thread. | |
75 void QueryDhcpOnWorkerThread(const std::string& adapter_name); | |
76 | |
77 // Event/state transition handlers | |
78 void OnQueryDhcpDone(std::string url); | |
79 void OnTimeout(); | |
80 void OnFetcherDone(int result); | |
81 void TransitionToFinish(); | |
82 | |
83 // Virtual methods introduced to allow unit testing. | |
84 virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name); | |
85 virtual ProxyScriptFetcher* ImplCreateScriptFetcher(const GURL& url); | |
86 virtual int ImplGetTimeoutMs() const; | |
87 | |
88 // This is the state machine for fetching from a given adapter. | |
89 // | |
90 // The state machine goes from START->WAIT_DHCP when it starts | |
91 // a worker thread to fetch the PAC URL from DHCP. | |
92 // | |
93 // In state WAIT_DHCP, if the DHCP query finishes and has no URL, it | |
94 // moves to state FINISH. If there is a URL, it starts a | |
95 // ProxyScriptFetcher to fetch it and moves to state WAIT_URL. | |
96 // | |
97 // It goes from WAIT_URL->FINISH when the ProxyScriptFetcher completes. | |
98 // | |
99 // In state FINISH, completion is indicated to the outer class, with | |
100 // the results of the fetch if a PAC script was successfully fetched. | |
101 // | |
102 // In any state except FINISH, a call to Cancel() will move to state | |
103 // CANCEL and cause all outstanding work to be cancelled or its | |
104 // results ignored when available. | |
105 enum State { | |
106 STATE_START, | |
107 STATE_WAIT_DHCP, | |
108 STATE_WAIT_URL, | |
109 STATE_FINISH, | |
110 STATE_CANCEL, | |
eroman
2011/04/21 05:22:48
nit: how about STATE_CANCELLED
Jói
2011/05/03 21:20:59
I considered that but then the others should chang
| |
111 }; | |
112 | |
113 // Current state of this state machine. | |
114 State state_; | |
115 | |
116 // A network error indicating result of operation. | |
117 int result_; | |
118 | |
119 // Empty string or the PAC script downloaded. | |
120 string16 pac_script_; | |
121 | |
122 // Empty URL or the PAC URL configured in DHCP. | |
123 GURL pac_url_; | |
124 | |
125 // Callback to let our client know we're done. Invalid in states | |
126 // START, FINISH and CANCEL. | |
127 CompletionCallback* callback_; | |
128 | |
129 // Fetcher to retrieve PAC files once URL is known. | |
130 scoped_ptr<ProxyScriptFetcher> script_fetcher_; | |
131 | |
132 // Callback from the script fetcher. | |
133 CompletionCallbackImpl<DhcpProxyScriptAdapterFetcher> | |
134 script_fetcher_callback_; | |
135 | |
136 // Implements a timeout on the call to the Win32 DHCP API. | |
137 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_; | |
138 | |
139 // Used by worker thread to post a message back to the original | |
140 // thread. Fine to use a proxy since in the case where the original | |
141 // thread has gone away, that would mean this object is in state | |
142 // CANCEL and no further processing should be done. | |
143 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
144 | |
145 URLRequestContext* url_request_context_; | |
146 | |
147 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher); | |
148 }; | |
149 | |
150 } // namespace net | |
151 | |
152 #endif // NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ | |
OLD | NEW |