Home | History | Annotate | Download | only in proxy
      1 // Copyright (c) 2009 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_INIT_PROXY_RESOLVER_H_
      6 #define NET_PROXY_INIT_PROXY_RESOLVER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "googleurl/src/gurl.h"
     12 #include "net/base/completion_callback.h"
     13 
     14 namespace net {
     15 
     16 class LoadLog;
     17 class ProxyConfig;
     18 class ProxyResolver;
     19 class ProxyScriptFetcher;
     20 
     21 // InitProxyResolver is a helper class used by ProxyService to
     22 // initialize a ProxyResolver with the PAC script data specified
     23 // by a particular ProxyConfig.
     24 //
     25 // This involves trying to use PAC scripts in this order:
     26 //
     27 //   (1) WPAD if auto-detect is on.
     28 //   (2) Custom PAC script if a URL was given.
     29 //
     30 // If no PAC script was successfully downloaded + parsed, then it fails with
     31 // a network error. Otherwise the proxy resolver is left initialized with
     32 // the PAC script.
     33 //
     34 // Deleting InitProxyResolver while Init() is in progress, will
     35 // cancel the request.
     36 //
     37 class InitProxyResolver {
     38  public:
     39   // |resolver| and |proxy_script_fetcher| must remain valid for
     40   // the lifespan of InitProxyResolver.
     41   InitProxyResolver(ProxyResolver* resolver,
     42                     ProxyScriptFetcher* proxy_script_fetcher);
     43 
     44   // Aborts any in-progress request.
     45   ~InitProxyResolver();
     46 
     47   // Apply the PAC settings of |config| to |resolver_|.
     48   int Init(const ProxyConfig& config,
     49            CompletionCallback* callback,
     50            LoadLog* load_log);
     51 
     52  private:
     53   enum State {
     54     STATE_NONE,
     55     STATE_FETCH_PAC_SCRIPT,
     56     STATE_FETCH_PAC_SCRIPT_COMPLETE,
     57     STATE_SET_PAC_SCRIPT,
     58     STATE_SET_PAC_SCRIPT_COMPLETE,
     59   };
     60   typedef std::vector<GURL> UrlList;
     61 
     62   // Returns ordered list of PAC urls to try for |config|.
     63   UrlList BuildPacUrlsFallbackList(const ProxyConfig& config) const;
     64 
     65   void OnIOCompletion(int result);
     66   int DoLoop(int result);
     67   void DoCallback(int result);
     68 
     69   int DoFetchPacScript();
     70   int DoFetchPacScriptComplete(int result);
     71 
     72   int DoSetPacScript();
     73   int DoSetPacScriptComplete(int result);
     74 
     75   // Tries restarting using the next fallback PAC URL:
     76   // |pac_urls_[++current_pac_url_index]|.
     77   // Returns OK and rewinds the state machine when there
     78   // is something to try, otherwise returns |error|.
     79   int TryToFallbackPacUrl(int error);
     80 
     81   // Gets the initial state (we skip fetching when the
     82   // ProxyResolver doesn't |expect_pac_bytes()|.
     83   State GetStartState() const;
     84 
     85   // Returns the current PAC URL we are fetching/testing.
     86   const GURL& current_pac_url() const;
     87 
     88   void DidCompleteInit();
     89   void Cancel();
     90 
     91   ProxyResolver* resolver_;
     92   ProxyScriptFetcher* proxy_script_fetcher_;
     93 
     94   CompletionCallbackImpl<InitProxyResolver> io_callback_;
     95   CompletionCallback* user_callback_;
     96 
     97   size_t current_pac_url_index_;
     98 
     99   // Filled when the PAC script fetch completes.
    100   std::string pac_bytes_;
    101 
    102   UrlList pac_urls_;
    103   State next_state_;
    104 
    105   scoped_refptr<LoadLog> load_log_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(InitProxyResolver);
    108 };
    109 
    110 }  // namespace net
    111 
    112 #endif  // NET_PROXY_INIT_PROXY_RESOLVER_H_
    113