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