url_request_context_getter.h 3.5 KB

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