Chromium Code Reviews| 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/memory/scoped_ptr.h" | |
| 11 #include "base/string16.h" | |
| 12 #include "base/threading/non_thread_safe.h" | |
| 13 #include "base/timer.h" | |
| 14 #include "net/base/completion_callback.h" | |
| 15 #include "googleurl/src/gurl.h" | |
| 16 | |
| 17 namespace base { | |
| 18 class MessageLoopProxy; | |
| 19 } | |
| 20 | |
| 21 namespace net { | |
| 22 | |
| 23 class ProxyScriptFetcher; | |
| 24 class URLRequestContext; | |
| 25 | |
| 26 // For a given adapter, this class takes care of first doing a DHCP lookup | |
| 27 // to get the PAC URL, then if there is one, trying to fetch it. | |
| 28 // | |
| 29 // Non thread-safe for clients, but does some internal multi-threading. | |
| 30 class DhcpProxyScriptAdapterFetcher | |
| 31 : public base::RefCountedThreadSafe<DhcpProxyScriptAdapterFetcher>, | |
| 32 public base::NonThreadSafe { | |
| 33 public: | |
| 34 // |url_request_context| must outlive DhcpProxyScriptAdapterFetcher. | |
| 35 explicit DhcpProxyScriptAdapterFetcher( | |
| 36 URLRequestContext* url_request_context); | |
| 37 virtual ~DhcpProxyScriptAdapterFetcher(); | |
| 38 | |
| 39 // Starts a fetch. DhcpProxyScriptAdapterFetcher takes ownership of | |
|
eroman
2011/05/13 05:03:32
Same comment as before: please update this text, t
Jói
2011/05/13 20:19:09
Done.
| |
| 40 // |adapter_name|. 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. | |
| 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 result() const; | |
|
eroman
2011/05/13 05:03:32
style nit: please don't use hacker_style() for vir
Jói
2011/05/13 20:19:09
Done.
| |
| 58 | |
| 59 // Returns the contents of the PAC file retrieved. Only valid if | |
| 60 // |IsComplete()| is true. Returns the empty string if |GetResult()| | |
|
eroman
2011/05/13 05:03:32
What is GetResult()?
Jói
2011/05/13 20:19:09
Hmm, I guess it was result() above before I change
| |
| 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 queries the synchronous Win32 API we need to use on the worker | |
| 80 // thread. | |
| 81 void QueryDhcpOnWorkerThread(const std::string& adapter_name); | |
| 82 | |
| 83 // Event/state transition handlers | |
| 84 void OnQueryDhcpDone(std::string url); | |
|
eroman
2011/05/13 05:03:32
can this use a const string&?
Jói
2011/05/13 20:19:09
Done.
| |
| 85 void OnTimeout(); | |
| 86 void OnFetcherDone(int result); | |
| 87 void TransitionToFinish(); | |
| 88 | |
| 89 // Virtual methods introduced to allow unit testing. | |
| 90 virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name); | |
| 91 virtual ProxyScriptFetcher* ImplCreateScriptFetcher(); | |
| 92 virtual int ImplGetTimeoutMs() const; | |
| 93 | |
| 94 // This is the state machine for fetching from a given adapter. | |
| 95 // | |
| 96 // The state machine goes from START->WAIT_DHCP when it starts | |
| 97 // a worker thread to fetch the PAC URL from DHCP. | |
| 98 // | |
| 99 // In state WAIT_DHCP, if the DHCP query finishes and has no URL, it | |
| 100 // moves to state FINISH. If there is a URL, it starts a | |
| 101 // ProxyScriptFetcher to fetch it and moves to state WAIT_URL. | |
| 102 // | |
| 103 // It goes from WAIT_URL->FINISH when the ProxyScriptFetcher completes. | |
| 104 // | |
| 105 // In state FINISH, completion is indicated to the outer class, with | |
| 106 // the results of the fetch if a PAC script was successfully fetched. | |
| 107 // | |
| 108 // In state WAIT_DHCP, our timeout occurring can push us to FINISH. | |
| 109 // | |
| 110 // In any state except FINISH, a call to Cancel() will move to state | |
| 111 // CANCEL and cause all outstanding work to be cancelled or its | |
| 112 // results ignored when available. | |
| 113 enum State { | |
| 114 STATE_START, | |
| 115 STATE_WAIT_DHCP, | |
| 116 STATE_WAIT_URL, | |
| 117 STATE_FINISH, | |
| 118 STATE_CANCEL, | |
| 119 }; | |
| 120 | |
| 121 // Current state of this state machine. | |
| 122 State state_; | |
| 123 | |
| 124 // A network error indicating result of operation. | |
| 125 int result_; | |
| 126 | |
| 127 // Empty string or the PAC script downloaded. | |
| 128 string16 pac_script_; | |
| 129 | |
| 130 // Empty URL or the PAC URL configured in DHCP. | |
| 131 GURL pac_url_; | |
| 132 | |
| 133 // Callback to let our client know we're done. Invalid in states | |
| 134 // START, FINISH and CANCEL. | |
| 135 CompletionCallback* callback_; | |
| 136 | |
| 137 // Fetcher to retrieve PAC files once URL is known. | |
| 138 scoped_ptr<ProxyScriptFetcher> script_fetcher_; | |
| 139 | |
| 140 // Callback from the script fetcher. | |
| 141 CompletionCallbackImpl<DhcpProxyScriptAdapterFetcher> | |
| 142 script_fetcher_callback_; | |
| 143 | |
| 144 // Implements a timeout on the call to the Win32 DHCP API. | |
| 145 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_; | |
| 146 | |
| 147 // Used by worker thread to post a message back to the original | |
| 148 // thread. Fine to use a proxy since in the case where the original | |
| 149 // thread has gone away, that would mean this object is in state | |
| 150 // CANCEL and no further processing should be done. | |
| 151 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
| 152 | |
| 153 URLRequestContext* url_request_context_; | |
|
eroman
2011/05/13 05:03:32
Can you contain this in a scoped_refptr<> ?
Jói
2011/05/13 20:19:09
I attempted to do so but it would result in a prob
| |
| 154 | |
| 155 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher); | |
| 156 }; | |
| 157 | |
| 158 } // namespace net | |
| 159 | |
| 160 #endif // NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ | |
| OLD | NEW |