Home | History | Annotate | Download | only in proxy
      1 // Copyright (c) 2012 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_ADAPTER_FETCHER_WIN_H_
      6 #define NET_PROXY_DHCP_PROXY_SCRIPT_ADAPTER_FETCHER_WIN_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/strings/string16.h"
     14 #include "base/threading/non_thread_safe.h"
     15 #include "base/timer/timer.h"
     16 #include "net/base/completion_callback.h"
     17 #include "net/base/net_export.h"
     18 #include "url/gurl.h"
     19 
     20 namespace net {
     21 
     22 class ProxyScriptFetcher;
     23 class URLRequestContext;
     24 
     25 // For a given adapter, this class takes care of first doing a DHCP lookup
     26 // to get the PAC URL, then if there is one, trying to fetch it.
     27 class NET_EXPORT_PRIVATE DhcpProxyScriptAdapterFetcher
     28     : public base::SupportsWeakPtr<DhcpProxyScriptAdapterFetcher>,
     29       NON_EXPORTED_BASE(public base::NonThreadSafe) {
     30  public:
     31   // |url_request_context| must outlive DhcpProxyScriptAdapterFetcher.
     32   explicit DhcpProxyScriptAdapterFetcher(
     33       URLRequestContext* url_request_context);
     34   virtual ~DhcpProxyScriptAdapterFetcher();
     35 
     36   // Starts a fetch.  On completion (but not cancellation), |callback|
     37   // will be invoked with the network error indicating success or failure
     38   // of fetching a DHCP-configured PAC file on this adapter.
     39   //
     40   // On completion, results can be obtained via |GetPacScript()|, |GetPacURL()|.
     41   //
     42   // You may only call Fetch() once on a given instance of
     43   // DhcpProxyScriptAdapterFetcher.
     44   virtual void Fetch(const std::string& adapter_name,
     45                      const net::CompletionCallback& callback);
     46 
     47   // Cancels the fetch on this adapter.
     48   virtual void Cancel();
     49 
     50   // Returns true if in the FINISH state (not CANCEL).
     51   virtual bool DidFinish() const;
     52 
     53   // Returns the network error indicating the result of the fetch. Will
     54   // return IO_PENDING until the fetch is complete or cancelled. This is
     55   // the same network error passed to the |callback| provided to |Fetch()|.
     56   virtual int GetResult() const;
     57 
     58   // Returns the contents of the PAC file retrieved.  Only valid if
     59   // |IsComplete()| is true.  Returns the empty string if |GetResult()|
     60   // returns anything other than OK.
     61   virtual base::string16 GetPacScript() const;
     62 
     63   // Returns the PAC URL retrieved from DHCP.  Only guaranteed to be
     64   // valid if |IsComplete()| is true.  Returns an empty URL if no URL was
     65   // configured in DHCP.  May return a valid URL even if |result()| does
     66   // not return OK (this would indicate that we found a URL configured in
     67   // DHCP but failed to download it).
     68   virtual GURL GetPacURL() const;
     69 
     70   // Returns the PAC URL configured in DHCP for the given |adapter_name|, or
     71   // the empty string if none is configured.
     72   //
     73   // This function executes synchronously due to limitations of the Windows
     74   // DHCP client API.
     75   static std::string GetPacURLFromDhcp(const std::string& adapter_name);
     76 
     77  protected:
     78   // This is the state machine for fetching from a given adapter.
     79   //
     80   // The state machine goes from START->WAIT_DHCP when it starts
     81   // a worker thread to fetch the PAC URL from DHCP.
     82   //
     83   // In state WAIT_DHCP, if the DHCP query finishes and has no URL, it
     84   // moves to state FINISH.  If there is a URL, it starts a
     85   // ProxyScriptFetcher to fetch it and moves to state WAIT_URL.
     86   //
     87   // It goes from WAIT_URL->FINISH when the ProxyScriptFetcher completes.
     88   //
     89   // In state FINISH, completion is indicated to the outer class, with
     90   // the results of the fetch if a PAC script was successfully fetched.
     91   //
     92   // In state WAIT_DHCP, our timeout occurring can push us to FINISH.
     93   //
     94   // In any state except FINISH, a call to Cancel() will move to state
     95   // CANCEL and cause all outstanding work to be cancelled or its
     96   // results ignored when available.
     97   enum State {
     98     STATE_START,
     99     STATE_WAIT_DHCP,
    100     STATE_WAIT_URL,
    101     STATE_FINISH,
    102     STATE_CANCEL,
    103   };
    104 
    105   State state() const;
    106 
    107   // This inner class encapsulates work done on a worker pool thread.
    108   // By using a separate object, we can keep the main object completely
    109   // thread safe and let it be non-refcounted.
    110   class NET_EXPORT_PRIVATE DhcpQuery
    111       : public base::RefCountedThreadSafe<DhcpQuery> {
    112    public:
    113     DhcpQuery();
    114     virtual ~DhcpQuery();
    115 
    116     // This method should run on a worker pool thread, via PostTaskAndReply.
    117     // After it has run, the |url()| method on this object will return the
    118     // URL retrieved.
    119     void GetPacURLForAdapter(const std::string& adapter_name);
    120 
    121     // Returns the URL retrieved for the given adapter, once the task has run.
    122     const std::string& url() const;
    123 
    124    protected:
    125     // Virtual method introduced to allow unit testing.
    126     virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name);
    127 
    128    private:
    129     // The URL retrieved for the given adapter.
    130     std::string url_;
    131 
    132     DISALLOW_COPY_AND_ASSIGN(DhcpQuery);
    133   };
    134 
    135   // Virtual methods introduced to allow unit testing.
    136   virtual ProxyScriptFetcher* ImplCreateScriptFetcher();
    137   virtual DhcpQuery* ImplCreateDhcpQuery();
    138   virtual base::TimeDelta ImplGetTimeout() const;
    139 
    140  private:
    141   // Event/state transition handlers
    142   void OnDhcpQueryDone(scoped_refptr<DhcpQuery> dhcp_query);
    143   void OnTimeout();
    144   void OnFetcherDone(int result);
    145   void TransitionToFinish();
    146 
    147   // Current state of this state machine.
    148   State state_;
    149 
    150   // A network error indicating result of operation.
    151   int result_;
    152 
    153   // Empty string or the PAC script downloaded.
    154   base::string16 pac_script_;
    155 
    156   // Empty URL or the PAC URL configured in DHCP.
    157   GURL pac_url_;
    158 
    159   // Callback to let our client know we're done. Invalid in states
    160   // START, FINISH and CANCEL.
    161   net::CompletionCallback callback_;
    162 
    163   // Fetcher to retrieve PAC files once URL is known.
    164   scoped_ptr<ProxyScriptFetcher> script_fetcher_;
    165 
    166   // Implements a timeout on the call to the Win32 DHCP API.
    167   base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_;
    168 
    169   URLRequestContext* const url_request_context_;
    170 
    171   DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher);
    172 };
    173 
    174 }  // namespace net
    175 
    176 #endif  // NET_PROXY_DHCP_PROXY_SCRIPT_ADAPTER_FETCHER_WIN_H_
    177