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_
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"
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
{
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
;
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 // Factory for creating the time-out task. This takes care of revoking
83 // outstanding tasks when |this| is deleted.
84 base::WeakPtrFactory
<ProxyScriptFetcherImpl
> weak_factory_
;
86 // The context used for making network requests.
87 URLRequestContext
* const url_request_context_
;
89 // Buffer that URLRequest writes into.
90 scoped_refptr
<IOBuffer
> buf_
;
92 // The next ID to use for |cur_request_| (monotonically increasing).
95 // The current (in progress) request, or NULL.
96 scoped_ptr
<URLRequest
> cur_request_
;
98 // State for current request (only valid when |cur_request_| is not NULL):
100 // Unique ID for the current request.
103 // Callback to invoke on completion of the fetch.
104 net::CompletionCallback callback_
;
106 // Holds the error condition that was hit on the current request, or OK.
109 // Holds the bytes read so far. Will not exceed |max_response_bytes|.
110 std::string bytes_read_so_far_
;
112 // This buffer is owned by the owner of |callback|, and will be filled with
113 // UTF16 response on completion.
114 base::string16
* result_text_
;
116 // The maximum number of bytes to allow in responses.
117 size_t max_response_bytes_
;
119 // The maximum amount of time to wait for download to complete.
120 base::TimeDelta max_duration_
;
122 DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl
);
127 #endif // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_