File: pac_file_fetcher_impl.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (161 lines) | stat: -rw-r--r-- 5,553 bytes parent folder | download | duplicates (11)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_
#define NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_

#include <stddef.h>

#include <memory>
#include <string>

#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_export.h"
#include "net/proxy_resolution/pac_file_fetcher.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request.h"

class GURL;

namespace net {

class URLRequestContext;

// Implementation of PacFileFetcher that downloads scripts using the
// specified request context.
class NET_EXPORT PacFileFetcherImpl : public PacFileFetcher,
                                      public URLRequest::Delegate {
 public:
  // Creates a PacFileFetcher that issues requests through
  // |url_request_context|. |url_request_context| must remain valid for the
  // lifetime of PacFileFetcherImpl.
  // 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().
  //
  // Fetch() supports the following URL schemes, provided the underlying
  // |url_request_context| also supports them:
  //
  //   * http://
  //   * https://
  //   * ftp://
  //   * data:
  static std::unique_ptr<PacFileFetcherImpl> Create(
      URLRequestContext* url_request_context);

  PacFileFetcherImpl(const PacFileFetcherImpl&) = delete;
  PacFileFetcherImpl& operator=(const PacFileFetcherImpl&) = delete;

  ~PacFileFetcherImpl() override;

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

  void OnResponseCompleted(URLRequest* request, int net_error);

  // PacFileFetcher methods:
  int Fetch(const GURL& url,
            std::u16string* text,
            CompletionOnceCallback callback,
            const NetworkTrafficAnnotationTag traffic_annotation) override;
  void Cancel() override;
  URLRequestContext* GetRequestContext() const override;
  void OnShutdown() override;

  // URLRequest::Delegate methods:
  void OnReceivedRedirect(URLRequest* request,
                          const RedirectInfo& redirect_info,
                          bool* defer_redirect) override;
  void OnAuthRequired(URLRequest* request,
                      const AuthChallengeInfo& auth_info) override;
  void OnSSLCertificateError(URLRequest* request,
                             int net_error,
                             const SSLInfo& ssl_info,
                             bool is_hsts_ok) override;
  void OnResponseStarted(URLRequest* request, int net_error) override;
  void OnReadCompleted(URLRequest* request, int num_bytes) override;

 private:
  enum { kBufSize = 4096 };

  explicit PacFileFetcherImpl(URLRequestContext* url_request_context);

  // Returns true if |url| has an acceptable URL scheme (i.e. http://, https://,
  // etc).
  bool IsUrlSchemeAllowed(const GURL& url) const;

  // Read more bytes from the response.
  void ReadBody(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(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);

  // The context used for making network requests.  Set to nullptr by
  // OnShutdown.
  raw_ptr<URLRequestContext> url_request_context_;

  // Buffer that URLRequest writes into.
  scoped_refptr<IOBuffer> buf_;

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

  // The current (in progress) request, or NULL.
  std::unique_ptr<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_ = 0;

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

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

  // 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.
  raw_ptr<std::u16string> result_text_ = nullptr;

  // 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_;

  // The time that the fetch started.
  base::TimeTicks fetch_start_time_;

  // The time that the first byte was received.
  base::TimeTicks fetch_time_to_first_byte_;

  // Factory for creating the time-out task. This takes care of revoking
  // outstanding tasks when |this| is deleted.
  base::WeakPtrFactory<PacFileFetcherImpl> weak_factory_{this};
};

}  // namespace net

#endif  // NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_