Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / proxy / proxy_script_fetcher_impl.h
blobb02c9fc5db7c9679ac78018486c9584e37587dca
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_PROXY_SCRIPT_FETCHER_IMPL_H_
6 #define NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/time/time.h"
17 #include "net/proxy/proxy_script_fetcher.h"
18 #include "net/url_request/url_request.h"
20 class GURL;
22 namespace net {
24 class URLRequestContext;
26 // Implementation of ProxyScriptFetcher that downloads scripts using the
27 // specified request context.
28 class NET_EXPORT ProxyScriptFetcherImpl : public ProxyScriptFetcher,
29 public URLRequest::Delegate {
30 public:
31 // Creates a ProxyScriptFetcher that issues requests through
32 // |url_request_context|. |url_request_context| must remain valid for the
33 // lifetime of ProxyScriptFetcherImpl.
34 // Note that while a request is in progress, we will be holding a reference
35 // to |url_request_context|. Be careful not to create cycles between the
36 // fetcher and the context; you can break such cycles by calling Cancel().
37 explicit ProxyScriptFetcherImpl(URLRequestContext* url_request_context);
39 ~ProxyScriptFetcherImpl() override;
41 // Used by unit-tests to modify the default limits.
42 base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout);
43 size_t SetSizeConstraint(size_t size_bytes);
45 void OnResponseCompleted(URLRequest* request);
47 // ProxyScriptFetcher methods:
48 int Fetch(const GURL& url,
49 base::string16* text,
50 const CompletionCallback& callback) override;
51 void Cancel() override;
52 URLRequestContext* GetRequestContext() const override;
54 // URLRequest::Delegate methods:
55 void OnAuthRequired(URLRequest* request,
56 AuthChallengeInfo* auth_info) override;
57 void OnSSLCertificateError(URLRequest* request,
58 const SSLInfo& ssl_info,
59 bool is_hsts_ok) override;
60 void OnResponseStarted(URLRequest* request) override;
61 void OnReadCompleted(URLRequest* request, int num_bytes) override;
63 private:
64 enum { kBufSize = 4096 };
66 // Read more bytes from the response.
67 void ReadBody(URLRequest* request);
69 // Handles a response from Read(). Returns true if we should continue trying
70 // to read. |num_bytes| is 0 for EOF, and < 0 on errors.
71 bool ConsumeBytesRead(URLRequest* request, int num_bytes);
73 // Called once the request has completed to notify the caller of
74 // |response_code_| and |response_text_|.
75 void FetchCompleted();
77 // Clear out the state for the current request.
78 void ResetCurRequestState();
80 // Callback for time-out task of request with id |id|.
81 void OnTimeout(int id);
83 // The context used for making network requests.
84 URLRequestContext* const url_request_context_;
86 // Buffer that URLRequest writes into.
87 scoped_refptr<IOBuffer> buf_;
89 // The next ID to use for |cur_request_| (monotonically increasing).
90 int next_id_;
92 // The current (in progress) request, or NULL.
93 scoped_ptr<URLRequest> cur_request_;
95 // State for current request (only valid when |cur_request_| is not NULL):
97 // Unique ID for the current request.
98 int cur_request_id_;
100 // Callback to invoke on completion of the fetch.
101 CompletionCallback callback_;
103 // Holds the error condition that was hit on the current request, or OK.
104 int result_code_;
106 // Holds the bytes read so far. Will not exceed |max_response_bytes|.
107 std::string bytes_read_so_far_;
109 // This buffer is owned by the owner of |callback|, and will be filled with
110 // UTF16 response on completion.
111 base::string16* result_text_;
113 // The maximum number of bytes to allow in responses.
114 size_t max_response_bytes_;
116 // The maximum amount of time to wait for download to complete.
117 base::TimeDelta max_duration_;
119 // The time that the fetch started.
120 base::TimeTicks fetch_start_time_;
122 // The time that the first byte was received.
123 base::TimeTicks fetch_time_to_first_byte_;
125 // Factory for creating the time-out task. This takes care of revoking
126 // outstanding tasks when |this| is deleted.
127 base::WeakPtrFactory<ProxyScriptFetcherImpl> weak_factory_;
129 DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl);
132 } // namespace net
134 #endif // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_