xref: /aosp_15_r20/external/cronet/net/url_request/url_request_context_getter.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_URL_REQUEST_URL_REQUEST_CONTEXT_GETTER_H_
6 #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_GETTER_H_
7 
8 #include "base/memory/ref_counted.h"
9 #include "base/observer_list.h"
10 #include "base/task/sequenced_task_runner_helpers.h"
11 #include "build/build_config.h"
12 #include "net/base/net_export.h"
13 
14 namespace base {
15 class SingleThreadTaskRunner;
16 }  // namespace base
17 
18 #if BUILDFLAG(IS_IOS)
19 namespace web {
20 class NetworkContextOwner;
21 }
22 #endif  // BUILDFLAG(IS_IOS)
23 
24 namespace net {
25 class URLRequestContext;
26 class URLRequestContextGetterObserver;
27 
28 struct URLRequestContextGetterTraits;
29 
30 // Interface for retrieving an URLRequestContext.
31 class NET_EXPORT URLRequestContextGetter
32     : public base::RefCountedThreadSafe<URLRequestContextGetter,
33                                         URLRequestContextGetterTraits> {
34  public:
35   URLRequestContextGetter(const URLRequestContextGetter&) = delete;
36   URLRequestContextGetter& operator=(const URLRequestContextGetter&) = delete;
37 
38   // Returns the URLRequestContextGetter's URLRequestContext. Must only be
39   // called on the network task runner. Once NotifyContextShuttingDown() is
40   // invoked, must always return nullptr. Does not transfer ownership of
41   // the URLRequestContext.
42   virtual URLRequestContext* GetURLRequestContext() = 0;
43 
44   // Returns a SingleThreadTaskRunner corresponding to the thread on
45   // which the network IO happens (the thread on which the returned
46   // URLRequestContext may be used).
47   virtual scoped_refptr<base::SingleThreadTaskRunner>
48       GetNetworkTaskRunner() const = 0;
49 
50  protected:
51   friend class base::RefCountedThreadSafe<URLRequestContextGetter,
52                                           URLRequestContextGetterTraits>;
53   friend class base::DeleteHelper<URLRequestContextGetter>;
54   friend struct URLRequestContextGetterTraits;
55 
56   URLRequestContextGetter();
57   virtual ~URLRequestContextGetter();
58 
59   // Called to indicate the URLRequestContext is about to be shutdown, so
60   // observers need to abort any URLRequests they own.  The implementation of
61   // this class is responsible for making sure this gets called.
62   //
63   // Must be called once and only once *before* context tear down begins, so any
64   // pending requests can be torn down safely. Right before calling this method,
65   // subclasses must ensure GetURLRequestContext returns nullptr, to protect
66   // against reentrancy.
67   void NotifyContextShuttingDown();
68 
69  private:
70   // AddObserver and RemoveObserver are deprecated. Friend URLFetcherCore and
71   // web::NetworkContextOwner to restrict visibility.
72   friend class URLFetcherCore;
73 
74 #if BUILDFLAG(IS_IOS)
75   friend class web::NetworkContextOwner;
76 #endif  // BUILDFLAG(IS_IOS)
77 
78   // Adds / removes an observer to watch for shutdown of |this|'s context. Must
79   // only be called on network thread. May not be called once
80   // GetURLRequestContext() starts returning nullptr.
81   void AddObserver(URLRequestContextGetterObserver* observer);
82   void RemoveObserver(URLRequestContextGetterObserver* observer);
83 
84   // OnDestruct is used to ensure deletion on the thread on which the request
85   // IO happens.
86   void OnDestruct() const;
87 
88   base::ObserverList<URLRequestContextGetterObserver>::Unchecked observer_list_;
89 };
90 
91 struct URLRequestContextGetterTraits {
DestructURLRequestContextGetterTraits92   static void Destruct(const URLRequestContextGetter* context_getter) {
93     context_getter->OnDestruct();
94   }
95 };
96 
97 }  // namespace net
98 
99 #endif  // NET_URL_REQUEST_URL_REQUEST_CONTEXT_GETTER_H_
100