Fix nullptr crash in OnEmbed
[chromium-blink-merge.git] / net / proxy / dhcp_proxy_script_adapter_fetcher_win.h
blob30dcda2bde6f872fcb7859187fd6006866312db9
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.
5 #ifndef NET_PROXY_DHCP_PROXY_SCRIPT_ADAPTER_FETCHER_WIN_H_
6 #define NET_PROXY_DHCP_PROXY_SCRIPT_ADAPTER_FETCHER_WIN_H_
8 #include <string>
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"
20 namespace base {
21 class TaskRunner;
24 namespace net {
26 class ProxyScriptFetcher;
27 class URLRequestContext;
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.
31 class 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();
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.
45 // On completion, results can be obtained via |GetPacScript()|, |GetPacURL()|.
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 CompletionCallback& callback);
52 // Cancels the fetch on this adapter.
53 virtual void Cancel();
55 // Returns true if in the FINISH state (not CANCEL).
56 virtual bool DidFinish() const;
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;
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;
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;
75 // Returns the PAC URL configured in DHCP for the given |adapter_name|, or
76 // the empty string if none is configured.
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);
82 // Sanitizes a string returned via the DHCP API.
83 static std::string SanitizeDhcpApiString(const char* data,
84 size_t count_bytes);
86 protected:
87 // This is the state machine for fetching from a given adapter.
89 // The state machine goes from START->WAIT_DHCP when it starts
90 // a worker thread to fetch the PAC URL from DHCP.
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.
96 // It goes from WAIT_URL->FINISH when the ProxyScriptFetcher completes.
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.
101 // In state WAIT_DHCP, our timeout occurring can push us to FINISH.
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,
114 State state() const;
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();
124 // This method should run on a worker pool thread, via PostTaskAndReply.
125 // After it has run, the |url()| method on this object will return the
126 // URL retrieved.
127 void GetPacURLForAdapter(const std::string& adapter_name);
129 // Returns the URL retrieved for the given adapter, once the task has run.
130 const std::string& url() const;
132 protected:
133 // Virtual method introduced to allow unit testing.
134 virtual std::string ImplGetPacURLFromDhcp(const std::string& adapter_name);
136 friend class base::RefCountedThreadSafe<DhcpQuery>;
137 virtual ~DhcpQuery();
139 private:
140 // The URL retrieved for the given adapter.
141 std::string url_;
143 DISALLOW_COPY_AND_ASSIGN(DhcpQuery);
146 // Virtual methods introduced to allow unit testing.
147 virtual ProxyScriptFetcher* ImplCreateScriptFetcher();
148 virtual DhcpQuery* ImplCreateDhcpQuery();
149 virtual base::TimeDelta ImplGetTimeout() const;
151 private:
152 // Event/state transition handlers
153 void OnDhcpQueryDone(scoped_refptr<DhcpQuery> dhcp_query);
154 void OnTimeout();
155 void OnFetcherDone(int result);
156 void TransitionToFinish();
158 // TaskRunner for posting tasks to a worker thread.
159 scoped_refptr<base::TaskRunner> task_runner_;
161 // Current state of this state machine.
162 State state_;
164 // A network error indicating result of operation.
165 int result_;
167 // Empty string or the PAC script downloaded.
168 base::string16 pac_script_;
170 // Empty URL or the PAC URL configured in DHCP.
171 GURL pac_url_;
173 // Callback to let our client know we're done. Invalid in states
174 // START, FINISH and CANCEL.
175 CompletionCallback callback_;
177 // Fetcher to retrieve PAC files once URL is known.
178 scoped_ptr<ProxyScriptFetcher> script_fetcher_;
180 // Implements a timeout on the call to the Win32 DHCP API.
181 base::OneShotTimer<DhcpProxyScriptAdapterFetcher> wait_timer_;
183 URLRequestContext* const url_request_context_;
185 DISALLOW_IMPLICIT_CONSTRUCTORS(DhcpProxyScriptAdapterFetcher);
188 } // namespace net
190 #endif // NET_PROXY_DHCP_PROXY_SCRIPT_ADAPTER_FETCHER_WIN_H_