| 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/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 class DhcpProxyScriptAdapterFetcher | |
| 30 : public base::SupportsWeakPtr<DhcpProxyScriptAdapterFetcher>, | |
| 31 public base::NonThreadSafe { | |
| 32 public: | |
| 33 // |url_request_context| must outlive DhcpProxyScriptAdapterFetcher. | |
| 34 explicit DhcpProxyScriptAdapterFetcher( | |
| 35 URLRequestContext* url_request_context); | |
| 36 virtual ~DhcpProxyScriptAdapterFetcher(); | |
| 37 | |
| 38 // Starts a fetch. On completion (but not cancellation), |callback| | |
| 39 // will be invoked with the network error indicating success or failure | |
| 40 // of fetching a DHCP-configured PAC file on this adapter. | |
| 41 // | |
| 42 // On completion, results can be obtained via |GetPacScript()|, |GetPacURL()|. | |
| 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. This is | |
| 57 // the same network error passed to the |callback| provided to |Fetch()|. | |
| 58 virtual int GetResult() const; | |
| 59 | |
| 60 // Returns the contents of the PAC file retrieved. Only valid if | |
| 61 // |IsComplete()| is true. Returns the empty string if |GetResult()| | |
| 62 // returns anything other than OK. | |
| 63 virtual string16 GetPacScript() const; | |
| 64 | |
| 65 // Returns the PAC URL retrieved from DHCP. Only guaranteed to be | |
| 66 // valid if |IsComplete()| is true. Returns an empty URL if no URL was | |
| 67 // configured in DHCP. May return a valid URL even if |result()| does | |
| 68 // not return OK (this would indicate that we found a URL configured in | |
| 69 // DHCP but failed to download it). | |
| 70 virtual GURL GetPacURL() const; | |
| 71 | |
| 72 // Returns the PAC URL configured in DHCP for the given |adapter_name|, or | |
| 73 // the empty string if none is configured. | |
| 74 // | |
| 75 // This function executes synchronously due to limitations of the Windows | |
| 76 // DHCP client API. | |
| 77 static std::string GetPacURLFromDhcp(const std::string& adapter_name); | |
| 78 | |
| 79 protected: | |
| 80 // This inner class is used to encapsulate the worker thread, which has | |
| 81 // only a weak reference back to the main object, so that the main object | |
| 82 // can be destroyed before the thread ends. This also keeps the main | |
| 83 // object completely thread safe and allows it to be non-refcounted. | |
| 84 class WorkerThread : public base::RefCountedThreadSafe<WorkerThread> { | |
| 85 public: | |
| 86 // Creates and initializes (but does not start) the worker thread. | |
| 87 explicit WorkerThread( | |
| 88 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner); | |
| 89 virtual ~WorkerThread(); | |
| 90 | |
| 91 // Starts the worker thread, fetching information for |adapter_name| using | |
| 92 // |get_pac_from_url_func|. | |
| 93 void Start(const std::string& adapter_name); | |
| 94 | |
| 95 protected: | |
| 96 // Virtual method introduced to allow unit testing. | |
| 97 virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name); | |
| 98 | |
| 99 private: | |
| 100 // This is the method that runs on the worker thread. | |
| 101 void ThreadFunc(const std::string& adapter_name); | |
| 102 | |
| 103 // Callback for the above; this executes back on the main thread, | |
| 104 // not the worker thread. | |
| 105 void OnThreadDone(const std::string& url); | |
| 106 | |
| 107 // All work except ThreadFunc and (sometimes) destruction should occur | |
| 108 // on the thread that constructs the object. | |
| 109 base::ThreadChecker thread_checker_; | |
| 110 | |
| 111 // May only be accessed on the thread that constructs the object. | |
| 112 base::WeakPtr<DhcpProxyScriptAdapterFetcher> owner_; | |
| 113 | |
| 114 // Used by worker thread to post a message back to the original | |
| 115 // thread. Fine to use a proxy since in the case where the original | |
| 116 // thread has gone away, that would mean the |owner_| object is gone | |
| 117 // anyway, so there is nobody to receive the result. | |
| 118 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(WorkerThread); | |
| 121 }; | |
| 122 | |
| 123 // Event/state transition handlers | |
| 124 void OnQueryDhcpDone(const std::string& url); | |
| 125 void OnTimeout(); | |
| 126 void OnFetcherDone(int result); | |
| 127 void TransitionToFinish(); | |
| 128 | |
| 129 // Virtual methods introduced to allow unit testing. | |
| 130 virtual ProxyScriptFetcher* ImplCreateScriptFetcher(); | |
| 131 virtual WorkerThread* ImplCreateWorkerThread( | |
| 132 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner); | |
| 133 virtual base::TimeDelta ImplGetTimeout() const; | |
| 134 | |
| 135 // This is the state machine for fetching from a given adapter. | |
| 136 // | |
| 137 // The state machine goes from START->WAIT_DHCP when it starts | |
| 138 // a worker thread to fetch the PAC URL from DHCP. | |
| 139 // | |
| 140 // In state WAIT_DHCP, if the DHCP query finishes and has no URL, it | |
| 141 // moves to state FINISH. If there is a URL, it starts a | |
| 142 // ProxyScriptFetcher to fetch it and moves to state WAIT_URL. | |
| 143 // | |
| 144 // It goes from WAIT_URL->FINISH when the ProxyScriptFetcher completes. | |
| 145 // | |
| 146 // In state FINISH, completion is indicated to the outer class, with | |
| 147 // the results of the fetch if a PAC script was successfully fetched. | |
| 148 // | |
| 149 // In state WAIT_DHCP, our timeout occurring can push us to FINISH. | |
| 150 // | |
| 151 // In any state except FINISH, a call to Cancel() will move to state | |
| 152 // CANCEL and cause all outstanding work to be cancelled or its | |
| 153 // results ignored when available. | |
| 154 enum State { | |
| 155 STATE_START, | |
| 156 STATE_WAIT_DHCP, | |
| 157 STATE_WAIT_URL, | |
| 158 STATE_FINISH, | |
| 159 STATE_CANCEL, | |
| 160 }; | |
| 161 | |
| 162 // Current state of this state machine. | |
| 163 State state_; | |
| 164 | |
| 165 // A network error indicating result of operation. | |
| 166 int result_; | |
| 167 | |
| 168 // Empty string or the PAC script downloaded. | |
| 169 string16 pac_script_; | |
| 170 | |
| 171 // Empty URL or the PAC URL configured in DHCP. | |
| 172 GURL pac_url_; | |
| 173 | |
| 174 // Callback to let our client know we're done. Invalid in states | |
| 175 // START, FINISH and CANCEL. | |
| 176 CompletionCallback* callback_; | |
| 177 | |
| 178 // Container for our worker thread. NULL if not currently running. | |
| 179 scoped_refptr<WorkerThread> worker_thread_; | |
| 180 | |
| 181 // Fetcher to retrieve PAC files once URL is known. | |
| 182 scoped_ptr<ProxyScriptFetcher> script_fetcher_; | |
| 183 | |
| 184 // Callback from the script fetcher. | |
| 185 CompletionCallbackImpl<DhcpProxyScriptAdapterFetcher> | |
| 186 script_fetcher_callback_; | |
| 187 | |
| 188 // Implements a timeout on the call to the Win32 DHCP API. | |
| 189 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_; | |
| 190 | |
| 191 scoped_refptr<URLRequestContext> url_request_context_; | |
| 192 | |
| 193 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher); | |
| 194 }; | |
| 195 | |
| 196 } // namespace net | |
| 197 | |
| 198 #endif // NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ | |
| OLD | NEW |