Use scoped_ptr::Pass instead of scoped_ptr::PassAs<T>.
[chromium-blink-merge.git] / net / proxy / proxy_script_fetcher_impl.h
blob8cdcf81310118f341e66713fd66f840f05f06771
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 virtual ~ProxyScriptFetcherImpl();
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 virtual int Fetch(const GURL& url, base::string16* text,
49 const net::CompletionCallback& callback) override;
50 virtual void Cancel() override;
51 virtual URLRequestContext* GetRequestContext() const override;
53 // URLRequest::Delegate methods:
54 virtual void OnAuthRequired(URLRequest* request,
55 AuthChallengeInfo* auth_info) override;
56 virtual void OnSSLCertificateError(URLRequest* request,
57 const SSLInfo& ssl_info,
58 bool is_hsts_ok) override;
59 virtual void OnResponseStarted(URLRequest* request) override;
60 virtual void OnReadCompleted(URLRequest* request, int num_bytes) override;
62 private:
63 enum { kBufSize = 4096 };
65 // Read more bytes from the response.
66 void ReadBody(URLRequest* request);
68 // Handles a response from Read(). Returns true if we should continue trying
69 // to read. |num_bytes| is 0 for EOF, and < 0 on errors.
70 bool ConsumeBytesRead(URLRequest* request, int num_bytes);
72 // Called once the request has completed to notify the caller of
73 // |response_code_| and |response_text_|.
74 void FetchCompleted();
76 // Clear out the state for the current request.
77 void ResetCurRequestState();
79 // Callback for time-out task of request with id |id|.
80 void OnTimeout(int id);
82 // The context used for making network requests.
83 URLRequestContext* const url_request_context_;
85 // Buffer that URLRequest writes into.
86 scoped_refptr<IOBuffer> buf_;
88 // The next ID to use for |cur_request_| (monotonically increasing).
89 int next_id_;
91 // The current (in progress) request, or NULL.
92 scoped_ptr<URLRequest> cur_request_;
94 // State for current request (only valid when |cur_request_| is not NULL):
96 // Unique ID for the current request.
97 int cur_request_id_;
99 // Callback to invoke on completion of the fetch.
100 net::CompletionCallback callback_;
102 // Holds the error condition that was hit on the current request, or OK.
103 int result_code_;
105 // Holds the bytes read so far. Will not exceed |max_response_bytes|.
106 std::string bytes_read_so_far_;
108 // This buffer is owned by the owner of |callback|, and will be filled with
109 // UTF16 response on completion.
110 base::string16* result_text_;
112 // The maximum number of bytes to allow in responses.
113 size_t max_response_bytes_;
115 // The maximum amount of time to wait for download to complete.
116 base::TimeDelta max_duration_;
118 // Factory for creating the time-out task. This takes care of revoking
119 // outstanding tasks when |this| is deleted.
120 base::WeakPtrFactory<ProxyScriptFetcherImpl> weak_factory_;
122 DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl);
125 } // namespace net
127 #endif // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_