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.
4
5#ifndef NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_
6#define NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_
7
8#include <string>
9
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"
19
20class GURL;
21
22namespace net {
23
24class URLRequestContext;
25
26// Implementation of ProxyScriptFetcher that downloads scripts using the
27// specified request context.
28class 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);
38
39  virtual ~ProxyScriptFetcherImpl();
40
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);
44
45  void OnResponseCompleted(URLRequest* request);
46
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;
52
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;
61
62 private:
63  enum { kBufSize = 4096 };
64
65  // Read more bytes from the response.
66  void ReadBody(URLRequest* request);
67
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);
71
72  // Called once the request has completed to notify the caller of
73  // |response_code_| and |response_text_|.
74  void FetchCompleted();
75
76  // Clear out the state for the current request.
77  void ResetCurRequestState();
78
79  // Callback for time-out task of request with id |id|.
80  void OnTimeout(int id);
81
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_;
85
86  // The context used for making network requests.
87  URLRequestContext* const url_request_context_;
88
89  // Buffer that URLRequest writes into.
90  scoped_refptr<IOBuffer> buf_;
91
92  // The next ID to use for |cur_request_| (monotonically increasing).
93  int next_id_;
94
95  // The current (in progress) request, or NULL.
96  scoped_ptr<URLRequest> cur_request_;
97
98  // State for current request (only valid when |cur_request_| is not NULL):
99
100  // Unique ID for the current request.
101  int cur_request_id_;
102
103  // Callback to invoke on completion of the fetch.
104  net::CompletionCallback callback_;
105
106  // Holds the error condition that was hit on the current request, or OK.
107  int result_code_;
108
109  // Holds the bytes read so far. Will not exceed |max_response_bytes|.
110  std::string bytes_read_so_far_;
111
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_;
115
116  // The maximum number of bytes to allow in responses.
117  size_t max_response_bytes_;
118
119  // The maximum amount of time to wait for download to complete.
120  base::TimeDelta max_duration_;
121
122  DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl);
123};
124
125}  // namespace net
126
127#endif  // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_
128