blob: bc1e1928fb09130c370857dbe777079c2b06f183 [file] [log] [blame]
[email protected]ef2bf422012-05-11 03:27:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]86933612010-10-16 23:10:332// 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_PROXY_SCRIPT_FETCHER_IMPL_H_
6#define NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_
[email protected]86933612010-10-16 23:10:337
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9
danakj8a98ca22016-04-16 02:47:3610#include <memory>
[email protected]6a60c8c2011-11-11 05:27:2611#include <string>
12
[email protected]c4c1b482011-07-22 17:24:2613#include "base/compiler_specific.h"
Avi Drissman13fc8932015-12-20 04:40:4614#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
[email protected]235786812011-12-20 02:15:3116#include "base/memory/weak_ptr.h"
[email protected]fc9be5802013-06-11 10:56:5117#include "base/strings/string16.h"
[email protected]66e96c42013-06-28 15:20:3118#include "base/time/time.h"
bnc81c46c1f2016-10-04 16:25:5919#include "net/base/net_export.h"
[email protected]86933612010-10-16 23:10:3320#include "net/proxy/proxy_script_fetcher.h"
21#include "net/url_request/url_request.h"
22
23class GURL;
[email protected]86933612010-10-16 23:10:3324
25namespace net {
26
[email protected]27a112c2011-01-06 04:19:3027class URLRequestContext;
28
[email protected]86933612010-10-16 23:10:3329// Implementation of ProxyScriptFetcher that downloads scripts using the
30// specified request context.
[email protected]172da1b2011-08-12 15:52:2631class NET_EXPORT ProxyScriptFetcherImpl : public ProxyScriptFetcher,
32 public URLRequest::Delegate {
[email protected]86933612010-10-16 23:10:3333 public:
34 // Creates a ProxyScriptFetcher that issues requests through
35 // |url_request_context|. |url_request_context| must remain valid for the
36 // lifetime of ProxyScriptFetcherImpl.
37 // Note that while a request is in progress, we will be holding a reference
38 // to |url_request_context|. Be careful not to create cycles between the
39 // fetcher and the context; you can break such cycles by calling Cancel().
[email protected]da968bc2011-01-19 11:48:1940 explicit ProxyScriptFetcherImpl(URLRequestContext* url_request_context);
[email protected]86933612010-10-16 23:10:3341
dchengb03027d2014-10-21 12:00:2042 ~ProxyScriptFetcherImpl() override;
[email protected]86933612010-10-16 23:10:3343
[email protected]7aefb152011-01-21 23:46:4944 // Used by unit-tests to modify the default limits.
45 base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout);
46 size_t SetSizeConstraint(size_t size_bytes);
[email protected]86933612010-10-16 23:10:3347
maksim.sisovbf794e22016-09-13 09:20:3448 void OnResponseCompleted(URLRequest* request, int net_error);
[email protected]7aefb152011-01-21 23:46:4949
50 // ProxyScriptFetcher methods:
dchengb03027d2014-10-21 12:00:2051 int Fetch(const GURL& url,
52 base::string16* text,
ttuttle859dc7a2015-04-23 19:42:2953 const CompletionCallback& callback) override;
dchengb03027d2014-10-21 12:00:2054 void Cancel() override;
55 URLRequestContext* GetRequestContext() const override;
mmenkeed8d7e432017-05-03 16:48:3356 void OnShutdown() override;
[email protected]86933612010-10-16 23:10:3357
[email protected]da968bc2011-01-19 11:48:1958 // URLRequest::Delegate methods:
dchengb03027d2014-10-21 12:00:2059 void OnAuthRequired(URLRequest* request,
60 AuthChallengeInfo* auth_info) override;
61 void OnSSLCertificateError(URLRequest* request,
62 const SSLInfo& ssl_info,
63 bool is_hsts_ok) override;
maksim.sisovbf794e22016-09-13 09:20:3464 void OnResponseStarted(URLRequest* request, int net_error) override;
dchengb03027d2014-10-21 12:00:2065 void OnReadCompleted(URLRequest* request, int num_bytes) override;
[email protected]86933612010-10-16 23:10:3366
67 private:
[email protected]7aefb152011-01-21 23:46:4968 enum { kBufSize = 4096 };
69
[email protected]86933612010-10-16 23:10:3370 // Read more bytes from the response.
[email protected]da968bc2011-01-19 11:48:1971 void ReadBody(URLRequest* request);
[email protected]86933612010-10-16 23:10:3372
[email protected]0967249162010-11-20 00:13:3273 // Handles a response from Read(). Returns true if we should continue trying
74 // to read. |num_bytes| is 0 for EOF, and < 0 on errors.
[email protected]da968bc2011-01-19 11:48:1975 bool ConsumeBytesRead(URLRequest* request, int num_bytes);
[email protected]0967249162010-11-20 00:13:3276
[email protected]86933612010-10-16 23:10:3377 // Called once the request has completed to notify the caller of
78 // |response_code_| and |response_text_|.
79 void FetchCompleted();
80
81 // Clear out the state for the current request.
82 void ResetCurRequestState();
83
84 // Callback for time-out task of request with id |id|.
85 void OnTimeout(int id);
86
mmenkeed8d7e432017-05-03 16:48:3387 // The context used for making network requests. Set to nullptr by
88 // OnShutdown.
89 URLRequestContext* url_request_context_;
[email protected]86933612010-10-16 23:10:3390
[email protected]da968bc2011-01-19 11:48:1991 // Buffer that URLRequest writes into.
[email protected]da968bc2011-01-19 11:48:1992 scoped_refptr<IOBuffer> buf_;
[email protected]86933612010-10-16 23:10:3393
94 // The next ID to use for |cur_request_| (monotonically increasing).
95 int next_id_;
96
97 // The current (in progress) request, or NULL.
danakj8a98ca22016-04-16 02:47:3698 std::unique_ptr<URLRequest> cur_request_;
[email protected]86933612010-10-16 23:10:3399
100 // State for current request (only valid when |cur_request_| is not NULL):
101
102 // Unique ID for the current request.
103 int cur_request_id_;
104
105 // Callback to invoke on completion of the fetch.
ttuttle859dc7a2015-04-23 19:42:29106 CompletionCallback callback_;
[email protected]86933612010-10-16 23:10:33107
108 // Holds the error condition that was hit on the current request, or OK.
109 int result_code_;
110
111 // Holds the bytes read so far. Will not exceed |max_response_bytes|.
112 std::string bytes_read_so_far_;
113
114 // This buffer is owned by the owner of |callback|, and will be filled with
115 // UTF16 response on completion.
[email protected]42cba2fb2013-03-29 19:58:57116 base::string16* result_text_;
[email protected]86933612010-10-16 23:10:33117
118 // The maximum number of bytes to allow in responses.
119 size_t max_response_bytes_;
120
121 // The maximum amount of time to wait for download to complete.
122 base::TimeDelta max_duration_;
123
cbentzel27d2e5e52015-07-10 17:39:56124 // The time that the fetch started.
cbentzele45ccba2015-08-21 18:23:14125 base::TimeTicks fetch_start_time_;
126
127 // The time that the first byte was received.
128 base::TimeTicks fetch_time_to_first_byte_;
cbentzel27d2e5e52015-07-10 17:39:56129
kulkarni.acd7b4462014-08-28 07:41:34130 // Factory for creating the time-out task. This takes care of revoking
131 // outstanding tasks when |this| is deleted.
132 base::WeakPtrFactory<ProxyScriptFetcherImpl> weak_factory_;
133
[email protected]86933612010-10-16 23:10:33134 DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl);
135};
136
137} // namespace net
138
139#endif // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_