1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_GETTER_H_
6 #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_GETTER_H_
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/observer_list.h"
11 #include "base/sequenced_task_runner_helpers.h"
12 #include "net/base/net_export.h"
15 class SingleThreadTaskRunner
;
20 class URLRequestContext
;
21 class URLRequestContextGetterObserver
;
23 struct URLRequestContextGetterTraits
;
25 // Interface for retrieving an URLRequestContext.
26 class NET_EXPORT URLRequestContextGetter
27 : public base::RefCountedThreadSafe
<URLRequestContextGetter
,
28 URLRequestContextGetterTraits
> {
30 // Returns the URLRequestContextGetter's URLRequestContext. Must only be
31 // called on the network task runner. Once NotifyContextShuttingDown() is
32 // invoked, must always return nullptr.
33 virtual URLRequestContext
* GetURLRequestContext() = 0;
35 // Returns a SingleThreadTaskRunner corresponding to the thread on
36 // which the network IO happens (the thread on which the returned
37 // URLRequestContext may be used).
38 virtual scoped_refptr
<base::SingleThreadTaskRunner
>
39 GetNetworkTaskRunner() const = 0;
41 // Adds / removes an observer to watch for shutdown of |this|'s context. Must
42 // only be called on network thread. May not be called once
43 // GetURLRequestContext() starts returning nullptr.
44 void AddObserver(URLRequestContextGetterObserver
* observer
);
45 void RemoveObserver(URLRequestContextGetterObserver
* observer
);
48 friend class base::RefCountedThreadSafe
<URLRequestContextGetter
,
49 URLRequestContextGetterTraits
>;
50 friend class base::DeleteHelper
<URLRequestContextGetter
>;
51 friend struct URLRequestContextGetterTraits
;
53 URLRequestContextGetter();
54 virtual ~URLRequestContextGetter();
56 // Called to indicate the URLRequestContext is about to be shutdown, so
57 // observers need to abort any URLRequests they own. The implementation of
58 // this class is responsible for making sure this gets called.
60 // Must be called once and only once *before* context tear down begins, so any
61 // pending requests can be torn down safely. Right before calling this method,
62 // subclasses must ensure GetURLRequestContext returns nullptr, to protect
63 // against reentrancy.
64 void NotifyContextShuttingDown();
67 // OnDestruct is used to ensure deletion on the thread on which the request
69 void OnDestruct() const;
71 base::ObserverList
<URLRequestContextGetterObserver
> observer_list_
;
73 DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter
);
76 struct URLRequestContextGetterTraits
{
77 static void Destruct(const URLRequestContextGetter
* context_getter
) {
78 context_getter
->OnDestruct();
82 // For use in shimming a URLRequestContext into a URLRequestContextGetter.
83 class NET_EXPORT TrivialURLRequestContextGetter
84 : public URLRequestContextGetter
{
86 TrivialURLRequestContextGetter(
87 URLRequestContext
* context
,
88 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_task_runner
);
90 // URLRequestContextGetter implementation:
91 URLRequestContext
* GetURLRequestContext() override
;
93 scoped_refptr
<base::SingleThreadTaskRunner
> GetNetworkTaskRunner()
97 ~TrivialURLRequestContextGetter() override
;
99 URLRequestContext
* context_
;
100 const scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
102 DISALLOW_COPY_AND_ASSIGN(TrivialURLRequestContextGetter
);
107 #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_GETTER_H_