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