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_PROXY_SCRIPT_FETCHER_WIN_H_ |
| 6 #define NET_PROXY_DHCP_PROXY_SCRIPT_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/dhcp_proxy_script_fetcher.h" |
| 13 |
| 14 #include <list> |
| 15 #include <set> |
| 16 #include <string> |
| 17 |
| 18 namespace net { |
| 19 |
| 20 class DhcpProxyScriptAdapterFetcher; |
| 21 class URLRequestContext; |
| 22 |
| 23 // Windows-specific implementation. |
| 24 class WindowsDhcpProxyScriptFetcher |
| 25 : public DhcpProxyScriptFetcher, |
| 26 public base::NonThreadSafe { |
| 27 public: |
| 28 // Creates a WindowsDhcpProxyScriptFetcher that issues requests through |
| 29 // |url_request_context|. |url_request_context| must remain valid for |
| 30 // the lifetime of WindowsDhcpProxyScriptFetcher. |
| 31 explicit WindowsDhcpProxyScriptFetcher( |
| 32 URLRequestContext* url_request_context); |
| 33 virtual ~WindowsDhcpProxyScriptFetcher(); |
| 34 |
| 35 // DhcpProxyScriptFetcher implementation. |
| 36 int Fetch(string16* utf16_text, CompletionCallback* callback) OVERRIDE; |
| 37 void Cancel() OVERRIDE; |
| 38 URLRequestContext* GetRequestContext() const OVERRIDE; |
| 39 GURL GetPacURL() const OVERRIDE; |
| 40 std::string GetFetcherName() const OVERRIDE; |
| 41 |
| 42 // Adds to |adapter_names| the name of each network adapter on this machine |
| 43 // that has DHCP enabled and is not a loop-back adapter. Returns false |
| 44 // on error. |
| 45 static bool GetCandidateAdapterNames(std::set<std::string>* adapter_names); |
| 46 |
| 47 protected: |
| 48 // Event/state transition handlers |
| 49 void CancelImpl(bool clear_fetchers); |
| 50 void OnFetcherDone(int result); |
| 51 void OnWaitTimer(); |
| 52 void TransitionToDone(); |
| 53 |
| 54 // Virtual methods introduced to allow unit testing. |
| 55 virtual DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher(); |
| 56 virtual bool ImplGetCandidateAdapterNames( |
| 57 std::set<std::string>* adapter_names); |
| 58 virtual int ImplGetMaxWaitMs(); |
| 59 |
| 60 // This is the outer state machine for fetching PAC configuration from |
| 61 // DHCP. It relies for sub-states on the state machine of the |
| 62 // DhcpProxyScriptAdapterFetcher class. |
| 63 // |
| 64 // The goal of the implementation is to the following work in parallel |
| 65 // for all network adapters that are using DHCP: |
| 66 // a) Try to get the PAC URL configured in DHCP; |
| 67 // b) If one is configured, try to fetch the PAC URL. |
| 68 // c) Once this is done for all adapters, or a timeout has passed after |
| 69 // it has completed for the fastest adapter, return the PAC file |
| 70 // available for the most preferred network adapter, if any. |
| 71 // |
| 72 // The state machine goes from START->NO_RESULTS when it creates |
| 73 // and starts an DhcpProxyScriptAdapterFetcher for each adapter. It goes |
| 74 // from NO_RESULTS->SOME_RESULTS when it gets the first result; at this |
| 75 // point a wait timer is started. It goes from SOME_RESULTS->DONE in |
| 76 // two cases: All results are known, or the wait timer expired. A call |
| 77 // to Cancel() will also go straight to DONE from any state. Any |
| 78 // way the DONE state is entered, we will at that point cancel any |
| 79 // outstanding work and return the best known PAC script or the empty |
| 80 // string. |
| 81 // |
| 82 // The state machine is reset for each Fetch(), a call to which is |
| 83 // only valid in states START and DONE, as only one Fetch() is |
| 84 // allowed to be outstanding at any given time. |
| 85 enum State { |
| 86 STATE_START, |
| 87 STATE_NO_RESULTS, |
| 88 STATE_SOME_RESULTS, |
| 89 STATE_DONE, |
| 90 }; |
| 91 |
| 92 // Current state of this state machine. |
| 93 State state_; |
| 94 |
| 95 // List, in Windows' network adapter preference order, of |
| 96 // DhcpProxyScriptAdapterFetcher objects that are or were attempting |
| 97 // to fetch a PAC file based on DHCP configuration. |
| 98 typedef std::list<scoped_refptr<DhcpProxyScriptAdapterFetcher>> FetcherList; |
| 99 FetcherList fetchers_; |
| 100 |
| 101 // Callback invoked when any fetcher completes. |
| 102 CompletionCallbackImpl<WindowsDhcpProxyScriptFetcher> fetcher_callback_; |
| 103 |
| 104 // Number of fetchers we are waiting for. |
| 105 int num_pending_fetchers_; |
| 106 |
| 107 // Lets our client know we're done. Not valid in states START or DONE. |
| 108 CompletionCallback* client_callback_; |
| 109 |
| 110 // Pointer to string we will write results to. Not valid in states |
| 111 // START and DONE. |
| 112 string16* destination_string_; |
| 113 |
| 114 // PAC URL retrieved from DHCP, if any. Valid only in state STATE_DONE. |
| 115 GURL pac_url_; |
| 116 |
| 117 base::OneShotTimer<WindowsDhcpProxyScriptFetcher> wait_timer_; |
| 118 |
| 119 URLRequestContext* url_request_context_; |
| 120 |
| 121 DISALLOW_IMPLICIT_CONSTRUCTORS(WindowsDhcpProxyScriptFetcher); |
| 122 }; |
| 123 |
| 124 } // namespace net |
| 125 |
| 126 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_FETCHER_WIN_H_ |
OLD | NEW |