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" |
| 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 { |
| 27 public: |
| 28 // |url_request_context| must outlive DhcpProxyScriptAdapterFetcher. |
| 29 explicit DhcpProxyScriptAdapterFetcher( |
| 30 URLRequestContext* url_request_context); |
| 31 ~DhcpProxyScriptAdapterFetcher(); |
| 32 |
| 33 // Starts a fetch. DhcpProxyScriptAdapterFetcher takes ownership of |
| 34 // |adapter_name|. On completion (but not cancellation), |callback| |
| 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; |
| 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; |
| 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 state WAIT_DHCP, our timeout occurring can push us to FINISH. |
| 103 // |
| 104 // In any state except FINISH, a call to Cancel() will move to state |
| 105 // CANCEL and cause all outstanding work to be cancelled or its |
| 106 // results ignored when available. |
| 107 enum State { |
| 108 STATE_START, |
| 109 STATE_WAIT_DHCP, |
| 110 STATE_WAIT_URL, |
| 111 STATE_FINISH, |
| 112 STATE_CANCEL, |
| 113 }; |
| 114 |
| 115 // Current state of this state machine. |
| 116 State state_; |
| 117 |
| 118 // A network error indicating result of operation. |
| 119 int result_; |
| 120 |
| 121 // Empty string or the PAC script downloaded. |
| 122 string16 pac_script_; |
| 123 |
| 124 // Empty URL or the PAC URL configured in DHCP. |
| 125 GURL pac_url_; |
| 126 |
| 127 // Callback to let our client know we're done. Invalid in states |
| 128 // START, FINISH and CANCEL. |
| 129 CompletionCallback* callback_; |
| 130 |
| 131 // Fetcher to retrieve PAC files once URL is known. |
| 132 scoped_ptr<ProxyScriptFetcher> script_fetcher_; |
| 133 |
| 134 // Callback from the script fetcher. |
| 135 CompletionCallbackImpl<DhcpProxyScriptAdapterFetcher> |
| 136 script_fetcher_callback_; |
| 137 |
| 138 // Implements a timeout on the call to the Win32 DHCP API. |
| 139 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_; |
| 140 |
| 141 // Used by worker thread to post a message back to the original |
| 142 // thread. Fine to use a proxy since in the case where the original |
| 143 // thread has gone away, that would mean this object is in state |
| 144 // CANCEL and no further processing should be done. |
| 145 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
| 146 |
| 147 URLRequestContext* url_request_context_; |
| 148 |
| 149 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher); |
| 150 }; |
| 151 |
| 152 } // namespace net |
| 153 |
| 154 #endif // NET_PROXY_DHCP_SCRIPT_ADAPTER_FETCHER_WIN_H_ |
OLD | NEW |