xref: /aosp_15_r20/external/cronet/net/proxy_resolution/pac_file_fetcher.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 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 // PacFileFetcher is an async interface for fetching a proxy auto config
6 // script. It is specific to fetching a PAC script; enforces timeout, max-size,
7 // status code.
8 
9 #ifndef NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_H_
10 #define NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_H_
11 
12 #include <string>
13 
14 #include "net/base/completion_once_callback.h"
15 #include "net/base/isolation_info.h"
16 #include "net/base/net_export.h"
17 #include "net/traffic_annotation/network_traffic_annotation.h"
18 
19 class GURL;
20 
21 namespace net {
22 
23 class URLRequestContext;
24 
25 // Interface for downloading a PAC script. Implementations can enforce
26 // timeouts, maximum size constraints, content encoding, etc..
27 class NET_EXPORT_PRIVATE PacFileFetcher {
28  public:
29   PacFileFetcher();
30   PacFileFetcher(const PacFileFetcher&) = delete;
31   PacFileFetcher& operator=(const PacFileFetcher&) = delete;
32 
33   // Destruction should cancel any outstanding requests.
34   virtual ~PacFileFetcher();
35 
36   // Downloads the given PAC URL, and invokes |callback| on completion.
37   // Returns OK on success, otherwise the error code. If the return code is
38   // ERR_IO_PENDING, then the request completes asynchronously, and |callback|
39   // will be invoked later with the final error code.
40   // After synchronous or asynchronous completion with a result code of OK,
41   // |*utf16_text| is filled with the response. On failure, the result text is
42   // an empty string, and the result code is a network error. Some special
43   // network errors that may occur are:
44   //
45   //    ERR_TIMED_OUT         -- the fetch took too long to complete.
46   //    ERR_FILE_TOO_BIG      -- the response's body was too large.
47   //    ERR_HTTP_RESPONSE_CODE_FAILURE -- non-200 HTTP status code.
48   //    ERR_NOT_IMPLEMENTED   -- the response required authentication.
49   //
50   // If the request is cancelled (either using the "Cancel()" method or by
51   // deleting |this|), then no callback is invoked.
52   //
53   // Only one fetch is allowed to be outstanding at a time.
54   virtual int Fetch(const GURL& url,
55                     std::u16string* utf16_text,
56                     CompletionOnceCallback callback,
57                     const NetworkTrafficAnnotationTag traffic_annotation) = 0;
58 
59   // Aborts the in-progress fetch (if any).
60   virtual void Cancel() = 0;
61 
62   // Returns the request context that this fetcher uses to issue downloads,
63   // or NULL.
64   virtual URLRequestContext* GetRequestContext() const = 0;
65 
66   // Fails the in-progress fetch (if any) and future requests will fail
67   // immediately. GetRequestContext() will always return nullptr after this is
68   // called.  Must be called before the URLRequestContext the fetcher was
69   // created with is torn down.
70   virtual void OnShutdown() = 0;
71 
isolation_info()72   const IsolationInfo& isolation_info() const { return isolation_info_; }
73 
74  private:
75   // Transient IsolationInfo used to fetch PAC scripts and resolve hostnames.
76   // Safe to reuse because delays for WPAD fetches don't provide information
77   // to the web platform useful to attackers, and WPAD fetches uniformly
78   // block all network requests.
79   const IsolationInfo isolation_info_ = IsolationInfo::CreateTransient();
80 };
81 
82 }  // namespace net
83 
84 #endif  // NET_PROXY_RESOLUTION_PAC_FILE_FETCHER_H_
85