xref: /aosp_15_r20/external/cronet/net/proxy_resolution/pac_file_fetcher_impl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_
6 #define NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "base/compiler_specific.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "net/base/completion_once_callback.h"
19 #include "net/base/net_export.h"
20 #include "net/proxy_resolution/pac_file_fetcher.h"
21 #include "net/traffic_annotation/network_traffic_annotation.h"
22 #include "net/url_request/url_request.h"
23 
24 class GURL;
25 
26 namespace net {
27 
28 class URLRequestContext;
29 
30 // Implementation of PacFileFetcher that downloads scripts using the
31 // specified request context.
32 class NET_EXPORT PacFileFetcherImpl : public PacFileFetcher,
33                                       public URLRequest::Delegate {
34  public:
35   // Creates a PacFileFetcher that issues requests through
36   // |url_request_context|. |url_request_context| must remain valid for the
37   // lifetime of PacFileFetcherImpl.
38   // Note that while a request is in progress, we will be holding a reference
39   // to |url_request_context|. Be careful not to create cycles between the
40   // fetcher and the context; you can break such cycles by calling Cancel().
41   //
42   // Fetch() supports the following URL schemes, provided the underlying
43   // |url_request_context| also supports them:
44   //
45   //   * http://
46   //   * https://
47   //   * ftp://
48   //   * data:
49   static std::unique_ptr<PacFileFetcherImpl> Create(
50       URLRequestContext* url_request_context);
51 
52   PacFileFetcherImpl(const PacFileFetcherImpl&) = delete;
53   PacFileFetcherImpl& operator=(const PacFileFetcherImpl&) = delete;
54 
55   ~PacFileFetcherImpl() override;
56 
57   // Used by unit-tests to modify the default limits.
58   base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout);
59   size_t SetSizeConstraint(size_t size_bytes);
60 
61   void OnResponseCompleted(URLRequest* request, int net_error);
62 
63   // PacFileFetcher methods:
64   int Fetch(const GURL& url,
65             std::u16string* text,
66             CompletionOnceCallback callback,
67             const NetworkTrafficAnnotationTag traffic_annotation) override;
68   void Cancel() override;
69   URLRequestContext* GetRequestContext() const override;
70   void OnShutdown() override;
71 
72   // URLRequest::Delegate methods:
73   void OnReceivedRedirect(URLRequest* request,
74                           const RedirectInfo& redirect_info,
75                           bool* defer_redirect) override;
76   void OnAuthRequired(URLRequest* request,
77                       const AuthChallengeInfo& auth_info) override;
78   void OnSSLCertificateError(URLRequest* request,
79                              int net_error,
80                              const SSLInfo& ssl_info,
81                              bool is_hsts_ok) override;
82   void OnResponseStarted(URLRequest* request, int net_error) override;
83   void OnReadCompleted(URLRequest* request, int num_bytes) override;
84 
85  private:
86   enum { kBufSize = 4096 };
87 
88   explicit PacFileFetcherImpl(URLRequestContext* url_request_context);
89 
90   // Returns true if |url| has an acceptable URL scheme (i.e. http://, https://,
91   // etc).
92   bool IsUrlSchemeAllowed(const GURL& url) const;
93 
94   // Read more bytes from the response.
95   void ReadBody(URLRequest* request);
96 
97   // Handles a response from Read(). Returns true if we should continue trying
98   // to read. |num_bytes| is 0 for EOF, and < 0 on errors.
99   bool ConsumeBytesRead(URLRequest* request, int num_bytes);
100 
101   // Called once the request has completed to notify the caller of
102   // |response_code_| and |response_text_|.
103   void FetchCompleted();
104 
105   // Clear out the state for the current request.
106   void ResetCurRequestState();
107 
108   // Callback for time-out task of request with id |id|.
109   void OnTimeout(int id);
110 
111   // The context used for making network requests.  Set to nullptr by
112   // OnShutdown.
113   raw_ptr<URLRequestContext> url_request_context_;
114 
115   // Buffer that URLRequest writes into.
116   scoped_refptr<IOBuffer> buf_;
117 
118   // The next ID to use for |cur_request_| (monotonically increasing).
119   int next_id_ = 0;
120 
121   // The current (in progress) request, or NULL.
122   std::unique_ptr<URLRequest> cur_request_;
123 
124   // State for current request (only valid when |cur_request_| is not NULL):
125 
126   // Unique ID for the current request.
127   int cur_request_id_ = 0;
128 
129   // Callback to invoke on completion of the fetch.
130   CompletionOnceCallback callback_;
131 
132   // Holds the error condition that was hit on the current request, or OK.
133   int result_code_ = OK;
134 
135   // Holds the bytes read so far. Will not exceed |max_response_bytes|.
136   std::string bytes_read_so_far_;
137 
138   // This buffer is owned by the owner of |callback|, and will be filled with
139   // UTF16 response on completion.
140   raw_ptr<std::u16string> result_text_ = nullptr;
141 
142   // The maximum number of bytes to allow in responses.
143   size_t max_response_bytes_;
144 
145   // The maximum amount of time to wait for download to complete.
146   base::TimeDelta max_duration_;
147 
148   // The time that the fetch started.
149   base::TimeTicks fetch_start_time_;
150 
151   // The time that the first byte was received.
152   base::TimeTicks fetch_time_to_first_byte_;
153 
154   // Factory for creating the time-out task. This takes care of revoking
155   // outstanding tasks when |this| is deleted.
156   base::WeakPtrFactory<PacFileFetcherImpl> weak_factory_{this};
157 };
158 
159 }  // namespace net
160 
161 #endif  // NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_IMPL_H_
162