Bug 1837643 [wpt PR 40475] - [RemoveLegacy] GridTrackList::legacy_track_list_, a...
[gecko.git] / netwerk / base / nsIOService.h
blob55dbea6c555fa3ef43f74b2c8f988b7eec1089aa
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
6 #ifndef nsIOService_h__
7 #define nsIOService_h__
9 #include "nsStringFwd.h"
10 #include "nsIIOService.h"
11 #include "nsTArray.h"
12 #include "nsCOMPtr.h"
13 #include "nsIObserver.h"
14 #include "nsIWeakReferenceUtils.h"
15 #include "nsINetUtil.h"
16 #include "nsIChannelEventSink.h"
17 #include "nsCategoryCache.h"
18 #include "nsISpeculativeConnect.h"
19 #include "nsWeakReference.h"
20 #include "mozilla/Atomics.h"
21 #include "mozilla/Attributes.h"
22 #include "mozilla/RWLock.h"
23 #include "mozilla/net/ProtocolHandlerInfo.h"
24 #include "prtime.h"
25 #include "nsICaptivePortalService.h"
26 #include "nsIObserverService.h"
27 #include "nsTHashSet.h"
28 #include "nsWeakReference.h"
29 #include "nsNetCID.h"
31 #define NS_N(x) (sizeof(x) / sizeof(*(x)))
33 // We don't want to expose this observer topic.
34 // Intended internal use only for remoting offline/inline events.
35 // See Bug 552829
36 #define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline"
37 #define NS_IPC_IOSERVICE_SET_CONNECTIVITY_TOPIC "ipc:network:set-connectivity"
39 class nsINetworkLinkService;
40 class nsIPrefBranch;
41 class nsIProtocolProxyService2;
42 class nsIProxyInfo;
43 class nsPISocketTransportService;
45 namespace mozilla {
46 class MemoryReportingProcess;
47 namespace net {
48 class NeckoChild;
49 class nsAsyncRedirectVerifyHelper;
50 class SocketProcessHost;
51 class SocketProcessMemoryReporter;
53 class nsIOService final : public nsIIOService,
54 public nsIObserver,
55 public nsINetUtil,
56 public nsISpeculativeConnect,
57 public nsSupportsWeakReference,
58 public nsIIOServiceInternal,
59 public nsIObserverService {
60 public:
61 NS_DECL_THREADSAFE_ISUPPORTS
62 NS_DECL_NSIIOSERVICE
63 NS_DECL_NSIOBSERVER
64 NS_DECL_NSINETUTIL
65 NS_DECL_NSISPECULATIVECONNECT
66 NS_DECL_NSIIOSERVICEINTERNAL
67 NS_DECL_NSIOBSERVERSERVICE
69 // Gets the singleton instance of the IO Service, creating it as needed
70 // Returns nullptr on out of memory or failure to initialize.
71 static already_AddRefed<nsIOService> GetInstance();
73 nsresult Init();
74 nsresult NewURI(const char* aSpec, nsIURI* aBaseURI, nsIURI** result,
75 nsIProtocolHandler** hdlrResult);
77 // Called by channels before a redirect happens. This notifies the global
78 // redirect observers.
79 nsresult AsyncOnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan,
80 uint32_t flags,
81 nsAsyncRedirectVerifyHelper* helper);
83 bool IsOffline() { return mOffline; }
84 PRIntervalTime LastOfflineStateChange() { return mLastOfflineStateChange; }
85 PRIntervalTime LastConnectivityChange() { return mLastConnectivityChange; }
86 PRIntervalTime LastNetworkLinkChange() { return mLastNetworkLinkChange; }
87 bool IsNetTearingDown() {
88 return mShutdown || mOfflineForProfileChange ||
89 mHttpHandlerAlreadyShutingDown;
91 PRIntervalTime NetTearingDownStarted() { return mNetTearingDownStarted; }
93 // nsHttpHandler is going to call this function to inform nsIOService that
94 // network is in process of tearing down. Moving nsHttpConnectionMgr::Shutdown
95 // to nsIOService caused problems (bug 1242755) so we doing it in this way. As
96 // soon as nsIOService gets notification that it is shutdown it is going to
97 // reset mHttpHandlerAlreadyShutingDown.
98 void SetHttpHandlerAlreadyShutingDown();
100 bool IsLinkUp();
102 // Converts an internal URI (e.g. one that has a username and password in
103 // it) into one which we can expose to the user, for example on the URL bar.
104 static already_AddRefed<nsIURI> CreateExposableURI(nsIURI*);
106 // Used to count the total number of HTTP requests made
107 void IncrementRequestNumber() { mTotalRequests++; }
108 uint32_t GetTotalRequestNumber() { return mTotalRequests; }
109 // Used to keep "race cache with network" stats
110 void IncrementCacheWonRequestNumber() { mCacheWon++; }
111 uint32_t GetCacheWonRequestNumber() { return mCacheWon; }
112 void IncrementNetWonRequestNumber() { mNetWon++; }
113 uint32_t GetNetWonRequestNumber() { return mNetWon; }
115 // Used to trigger a recheck of the captive portal status
116 nsresult RecheckCaptivePortal();
118 void OnProcessLaunchComplete(SocketProcessHost* aHost, bool aSucceeded);
119 void OnProcessUnexpectedShutdown(SocketProcessHost* aHost);
120 bool SocketProcessReady();
121 static void NotifySocketProcessPrefsChanged(const char* aName, void* aSelf);
122 void NotifySocketProcessPrefsChanged(const char* aName);
123 static bool UseSocketProcess(bool aCheckAgain = false);
125 bool IsSocketProcessLaunchComplete();
127 // Call func immediately if socket process is launched completely. Otherwise,
128 // |func| will be queued and then executed in the *main thread* once socket
129 // process is launced.
130 void CallOrWaitForSocketProcess(const std::function<void()>& aFunc);
132 int32_t SocketProcessPid();
133 SocketProcessHost* SocketProcess() { return mSocketProcess; }
135 friend SocketProcessMemoryReporter;
136 RefPtr<MemoryReportingProcess> GetSocketProcessMemoryReporter();
138 // Lookup the ProtocolHandlerInfo based on a given scheme.
139 // Safe to call from any thread.
140 ProtocolHandlerInfo LookupProtocolHandler(const nsACString& aScheme);
142 static void OnTLSPrefChange(const char* aPref, void* aSelf);
144 nsresult LaunchSocketProcess();
146 static bool TooManySocketProcessCrash();
147 static void IncreaseSocketProcessCrashCount();
149 private:
150 // These shouldn't be called directly:
151 // - construct using GetInstance
152 // - destroy using Release
153 nsIOService();
154 ~nsIOService();
155 nsresult SetConnectivityInternal(bool aConnectivity);
157 nsresult OnNetworkLinkEvent(const char* data);
159 nsresult InitializeCaptivePortalService();
160 nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan);
162 // Prefs wrangling
163 static void PrefsChanged(const char* pref, void* self);
164 void PrefsChanged(const char* pref = nullptr);
165 void ParsePortList(const char* pref, bool remove);
167 nsresult InitializeSocketTransportService();
168 nsresult InitializeNetworkLinkService();
169 nsresult InitializeProtocolProxyService();
171 // consolidated helper function
172 void LookupProxyInfo(nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
173 nsCString* aScheme, nsIProxyInfo** outPI);
175 nsresult NewChannelFromURIWithProxyFlagsInternal(
176 nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
177 nsINode* aLoadingNode, nsIPrincipal* aLoadingPrincipal,
178 nsIPrincipal* aTriggeringPrincipal,
179 const mozilla::Maybe<mozilla::dom::ClientInfo>& aLoadingClientInfo,
180 const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController,
181 uint32_t aSecurityFlags, nsContentPolicyType aContentPolicyType,
182 uint32_t aSandboxFlags, bool aSkipCheckForBrokenURLOrZeroSized,
183 nsIChannel** result);
185 nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI,
186 nsIURI* aProxyURI,
187 uint32_t aProxyFlags,
188 nsILoadInfo* aLoadInfo,
189 nsIChannel** result);
191 nsresult SpeculativeConnectInternal(
192 nsIURI* aURI, nsIPrincipal* aPrincipal,
193 Maybe<OriginAttributes>&& aOriginAttributes,
194 nsIInterfaceRequestor* aCallbacks, bool aAnonymous);
196 void DestroySocketProcess();
198 nsresult SetOfflineInternal(bool offline, bool notifySocketProcess = true);
200 bool UsesExternalProtocolHandler(const nsACString& aScheme)
201 MOZ_REQUIRES_SHARED(mLock);
203 private:
204 mozilla::Atomic<bool, mozilla::Relaxed> mOffline{true};
205 mozilla::Atomic<bool, mozilla::Relaxed> mOfflineForProfileChange{false};
206 bool mManageLinkStatus{false};
207 mozilla::Atomic<bool, mozilla::Relaxed> mConnectivity{true};
209 // Used to handle SetOffline() reentrancy. See the comment in
210 // SetOffline() for more details.
211 bool mSettingOffline{false};
212 bool mSetOfflineValue{false};
214 bool mSocketProcessLaunchComplete{false};
216 mozilla::Atomic<bool, mozilla::Relaxed> mShutdown{false};
217 mozilla::Atomic<bool, mozilla::Relaxed> mHttpHandlerAlreadyShutingDown{false};
219 nsCOMPtr<nsPISocketTransportService> mSocketTransportService;
220 nsCOMPtr<nsICaptivePortalService> mCaptivePortalService;
221 nsCOMPtr<nsINetworkLinkService> mNetworkLinkService;
222 bool mNetworkLinkServiceInitialized{false};
224 // cached categories
225 nsCategoryCache<nsIChannelEventSink> mChannelEventSinks{
226 NS_CHANNEL_EVENT_SINK_CATEGORY};
228 RWLock mLock{"nsIOService::mLock"};
229 nsTArray<int32_t> mRestrictedPortList MOZ_GUARDED_BY(mLock);
230 nsTArray<nsCString> mForceExternalSchemes MOZ_GUARDED_BY(mLock);
231 nsTHashMap<nsCString, RuntimeProtocolHandler> mRuntimeProtocolHandlers
232 MOZ_GUARDED_BY(mLock);
234 uint32_t mTotalRequests{0};
235 uint32_t mCacheWon{0};
236 uint32_t mNetWon{0};
237 static uint32_t sSocketProcessCrashedCount;
239 // These timestamps are needed for collecting telemetry on PR_Connect,
240 // PR_ConnectContinue and PR_Close blocking time. If we spend very long
241 // time in any of these functions we want to know if and what network
242 // change has happened shortly before.
243 mozilla::Atomic<PRIntervalTime> mLastOfflineStateChange;
244 mozilla::Atomic<PRIntervalTime> mLastConnectivityChange;
245 mozilla::Atomic<PRIntervalTime> mLastNetworkLinkChange;
247 // Time a network tearing down started.
248 mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted{0};
250 SocketProcessHost* mSocketProcess{nullptr};
252 // Events should be executed after the socket process is launched. Will
253 // dispatch these events while socket process fires OnProcessLaunchComplete.
254 // Note: this array is accessed only on the main thread.
255 nsTArray<std::function<void()>> mPendingEvents;
257 // The observer notifications need to be forwarded to socket process.
258 nsTHashSet<nsCString> mObserverTopicForSocketProcess;
259 // Some noticications (e.g., NS_XPCOM_SHUTDOWN_OBSERVER_ID) are triggered in
260 // socket process, so we should not send the notifications again.
261 nsTHashSet<nsCString> mSocketProcessTopicBlockedList;
262 // Used to store the topics that are already observed by IOService.
263 nsTHashSet<nsCString> mIOServiceTopicList;
265 nsCOMPtr<nsIObserverService> mObserverService;
267 public:
268 // Used for all default buffer sizes that necko allocates.
269 static uint32_t gDefaultSegmentSize;
270 static uint32_t gDefaultSegmentCount;
274 * Reference to the IO service singleton. May be null.
276 extern nsIOService* gIOService;
278 } // namespace net
279 } // namespace mozilla
281 #endif // nsIOService_h__