nsIOService.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsIOService_h__
  6. #define nsIOService_h__
  7. #include "nsStringFwd.h"
  8. #include "nsIIOService2.h"
  9. #include "nsTArray.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsWeakPtr.h"
  12. #include "nsIObserver.h"
  13. #include "nsWeakReference.h"
  14. #include "nsINetUtil.h"
  15. #include "nsIChannelEventSink.h"
  16. #include "nsCategoryCache.h"
  17. #include "nsISpeculativeConnect.h"
  18. #include "nsDataHashtable.h"
  19. #include "mozilla/Atomics.h"
  20. #include "mozilla/Attributes.h"
  21. #include "prtime.h"
  22. #include "nsICaptivePortalService.h"
  23. #define NS_N(x) (sizeof(x)/sizeof(*x))
  24. // We don't want to expose this observer topic.
  25. // Intended internal use only for remoting offline/inline events.
  26. // See Bug 552829
  27. #define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline"
  28. #define NS_IPC_IOSERVICE_SET_CONNECTIVITY_TOPIC "ipc:network:set-connectivity"
  29. static const char gScheme[][sizeof("moz-safe-about")] =
  30. {"chrome", "file", "http", "https", "jar", "data", "about", "moz-safe-about", "resource"};
  31. class nsINetworkLinkService;
  32. class nsIPrefBranch;
  33. class nsIProtocolProxyService2;
  34. class nsIProxyInfo;
  35. class nsPIDNSService;
  36. class nsPISocketTransportService;
  37. namespace mozilla {
  38. namespace net {
  39. class NeckoChild;
  40. class nsAsyncRedirectVerifyHelper;
  41. class nsIOService final : public nsIIOService2
  42. , public nsIObserver
  43. , public nsINetUtil
  44. , public nsISpeculativeConnect
  45. , public nsSupportsWeakReference
  46. , public nsIIOServiceInternal
  47. {
  48. public:
  49. NS_DECL_THREADSAFE_ISUPPORTS
  50. NS_DECL_NSIIOSERVICE
  51. NS_DECL_NSIIOSERVICE2
  52. NS_DECL_NSIOBSERVER
  53. NS_DECL_NSINETUTIL
  54. NS_DECL_NSISPECULATIVECONNECT
  55. NS_DECL_NSIIOSERVICEINTERNAL
  56. // Gets the singleton instance of the IO Service, creating it as needed
  57. // Returns nullptr on out of memory or failure to initialize.
  58. // Returns an addrefed pointer.
  59. static nsIOService* GetInstance();
  60. nsresult Init();
  61. nsresult NewURI(const char* aSpec, nsIURI* aBaseURI,
  62. nsIURI* *result,
  63. nsIProtocolHandler* *hdlrResult);
  64. // Called by channels before a redirect happens. This notifies the global
  65. // redirect observers.
  66. nsresult AsyncOnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan,
  67. uint32_t flags,
  68. nsAsyncRedirectVerifyHelper *helper);
  69. bool IsOffline() { return mOffline; }
  70. bool IsNetTearingDown() { return mShutdown || mOfflineForProfileChange ||
  71. mHttpHandlerAlreadyShutingDown; }
  72. PRIntervalTime NetTearingDownStarted() { return mNetTearingDownStarted; }
  73. // nsHttpHandler is going to call this function to inform nsIOService that network
  74. // is in process of tearing down. Moving nsHttpConnectionMgr::Shutdown to nsIOService
  75. // caused problems (bug 1242755) so we doing it in this way.
  76. // As soon as nsIOService gets notification that it is shutdown it is going to
  77. // reset mHttpHandlerAlreadyShutingDown.
  78. void SetHttpHandlerAlreadyShutingDown();
  79. bool IsLinkUp();
  80. static bool BlockToplevelDataUriNavigations();
  81. static bool BlockFTPSubresources();
  82. // Used to trigger a recheck of the captive portal status
  83. nsresult RecheckCaptivePortal();
  84. private:
  85. // These shouldn't be called directly:
  86. // - construct using GetInstance
  87. // - destroy using Release
  88. nsIOService();
  89. ~nsIOService();
  90. nsresult SetConnectivityInternal(bool aConnectivity);
  91. nsresult OnNetworkLinkEvent(const char *data);
  92. nsresult GetCachedProtocolHandler(const char *scheme,
  93. nsIProtocolHandler* *hdlrResult,
  94. uint32_t start=0,
  95. uint32_t end=0);
  96. nsresult CacheProtocolHandler(const char *scheme,
  97. nsIProtocolHandler* hdlr);
  98. nsresult InitializeCaptivePortalService();
  99. nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan);
  100. // Prefs wrangling
  101. void PrefsChanged(nsIPrefBranch *prefs, const char *pref = nullptr);
  102. void GetPrefBranch(nsIPrefBranch **);
  103. void ParsePortList(nsIPrefBranch *prefBranch, const char *pref, bool remove);
  104. nsresult InitializeSocketTransportService();
  105. nsresult InitializeNetworkLinkService();
  106. // consolidated helper function
  107. void LookupProxyInfo(nsIURI *aURI, nsIURI *aProxyURI, uint32_t aProxyFlags,
  108. nsCString *aScheme, nsIProxyInfo **outPI);
  109. nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI,
  110. nsIURI* aProxyURI,
  111. uint32_t aProxyFlags,
  112. nsILoadInfo* aLoadInfo,
  113. nsIChannel** result);
  114. nsresult SpeculativeConnectInternal(nsIURI *aURI,
  115. nsIPrincipal *aPrincipal,
  116. nsIInterfaceRequestor *aCallbacks,
  117. bool aAnonymous);
  118. private:
  119. bool mOffline;
  120. mozilla::Atomic<bool, mozilla::Relaxed> mOfflineForProfileChange;
  121. bool mManageLinkStatus;
  122. bool mConnectivity;
  123. // If true, the connectivity state will be mirrored by IOService.offline
  124. // meaning if !mConnectivity, GetOffline() will return true
  125. bool mOfflineMirrorsConnectivity;
  126. // Used to handle SetOffline() reentrancy. See the comment in
  127. // SetOffline() for more details.
  128. bool mSettingOffline;
  129. bool mSetOfflineValue;
  130. mozilla::Atomic<bool, mozilla::Relaxed> mShutdown;
  131. mozilla::Atomic<bool, mozilla::Relaxed> mHttpHandlerAlreadyShutingDown;
  132. nsCOMPtr<nsPISocketTransportService> mSocketTransportService;
  133. nsCOMPtr<nsPIDNSService> mDNSService;
  134. nsCOMPtr<nsIProtocolProxyService2> mProxyService;
  135. nsCOMPtr<nsICaptivePortalService> mCaptivePortalService;
  136. nsCOMPtr<nsINetworkLinkService> mNetworkLinkService;
  137. bool mNetworkLinkServiceInitialized;
  138. // Cached protocol handlers, only accessed on the main thread
  139. nsWeakPtr mWeakHandler[NS_N(gScheme)];
  140. // cached categories
  141. nsCategoryCache<nsIChannelEventSink> mChannelEventSinks;
  142. nsTArray<int32_t> mRestrictedPortList;
  143. bool mNetworkNotifyChanged;
  144. static bool sBlockToplevelDataUriNavigations;
  145. static bool sBlockFTPSubresources;
  146. // Time a network tearing down started.
  147. mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted;
  148. public:
  149. // Used for all default buffer sizes that necko allocates.
  150. static uint32_t gDefaultSegmentSize;
  151. static uint32_t gDefaultSegmentCount;
  152. };
  153. /**
  154. * Reference to the IO service singleton. May be null.
  155. */
  156. extern nsIOService* gIOService;
  157. } // namespace net
  158. } // namespace mozilla
  159. #endif // nsIOService_h__