summaryrefslogtreecommitdiffstats
path: root/net/proxy/proxy_script_fetcher_impl.h
blob: 4461650456ece28ac4304adf12c236a11db9ccfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_
#define NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_
#pragma once

#include "base/basictypes.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/string16.h"
#include "base/task.h"
#include "base/time.h"
#include "net/proxy/proxy_script_fetcher.h"
#include "net/url_request/url_request.h"

class GURL;
class URLRequestContext;
class X509Certificate;

namespace net {

// Implementation of ProxyScriptFetcher that downloads scripts using the
// specified request context.
class ProxyScriptFetcherImpl : public ProxyScriptFetcher,
                               public net::URLRequest::Delegate {
 public:
  // Creates a ProxyScriptFetcher that issues requests through
  // |url_request_context|. |url_request_context| must remain valid for the
  // lifetime of ProxyScriptFetcherImpl.
  // Note that while a request is in progress, we will be holding a reference
  // to |url_request_context|. Be careful not to create cycles between the
  // fetcher and the context; you can break such cycles by calling Cancel().
  explicit ProxyScriptFetcherImpl(URLRequestContext* url_request_context);

  virtual ~ProxyScriptFetcherImpl();

  // ProxyScriptFetcher methods:

  virtual int Fetch(const GURL& url, string16* text,
                    CompletionCallback* callback);
  virtual void Cancel();
  virtual URLRequestContext* GetRequestContext();

  // net::URLRequest::Delegate methods:
  virtual void OnAuthRequired(net::URLRequest* request,
                              AuthChallengeInfo* auth_info);
  virtual void OnSSLCertificateError(net::URLRequest* request, int cert_error,
                                     X509Certificate* cert);
  virtual void OnResponseStarted(net::URLRequest* request);
  virtual void OnReadCompleted(net::URLRequest* request, int num_bytes);
  virtual void OnResponseCompleted(net::URLRequest* request);

  // Used by unit-tests to modify the default limits.
  base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout);
  size_t SetSizeConstraint(size_t size_bytes);

 private:
  // Read more bytes from the response.
  void ReadBody(net::URLRequest* request);

  // Handles a response from Read(). Returns true if we should continue trying
  // to read. |num_bytes| is 0 for EOF, and < 0 on errors.
  bool ConsumeBytesRead(net::URLRequest* request, int num_bytes);

  // Called once the request has completed to notify the caller of
  // |response_code_| and |response_text_|.
  void FetchCompleted();

  // Clear out the state for the current request.
  void ResetCurRequestState();

  // Callback for time-out task of request with id |id|.
  void OnTimeout(int id);

  // Factory for creating the time-out task. This takes care of revoking
  // outstanding tasks when |this| is deleted.
  ScopedRunnableMethodFactory<ProxyScriptFetcherImpl> task_factory_;

  // The context used for making network requests.
  URLRequestContext* url_request_context_;

  // Buffer that net::URLRequest writes into.
  enum { kBufSize = 4096 };
  scoped_refptr<net::IOBuffer> buf_;

  // The next ID to use for |cur_request_| (monotonically increasing).
  int next_id_;

  // The current (in progress) request, or NULL.
  scoped_ptr<net::URLRequest> cur_request_;

  // State for current request (only valid when |cur_request_| is not NULL):

  // Unique ID for the current request.
  int cur_request_id_;

  // Callback to invoke on completion of the fetch.
  CompletionCallback* callback_;

  // Holds the error condition that was hit on the current request, or OK.
  int result_code_;

  // Holds the bytes read so far. Will not exceed |max_response_bytes|.
  std::string bytes_read_so_far_;

  // This buffer is owned by the owner of |callback|, and will be filled with
  // UTF16 response on completion.
  string16* result_text_;

  // The maximum number of bytes to allow in responses.
  size_t max_response_bytes_;

  // The maximum amount of time to wait for download to complete.
  base::TimeDelta max_duration_;

  DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl);
};

}  // namespace net

#endif  // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_