Bug 1875768 - Call the appropriate postfork handler on MacOS r=glandium
[gecko.git] / netwerk / base / nsIOService.h
blob70ffbc1e7f6724ec67a161ddd3cea509e518f518
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 // We don't want to expose this observer topic.
32 // Intended internal use only for remoting offline/inline events.
33 // See Bug 552829
34 #define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline"
35 #define NS_IPC_IOSERVICE_SET_CONNECTIVITY_TOPIC "ipc:network:set-connectivity"
37 class nsINetworkLinkService;
38 class nsIPrefBranch;
39 class nsIProtocolProxyService2;
40 class nsIProxyInfo;
41 class nsPISocketTransportService;
43 namespace mozilla {
44 class MemoryReportingProcess;
45 namespace net {
46 class NeckoChild;
47 class nsAsyncRedirectVerifyHelper;
48 class SocketProcessHost;
49 class SocketProcessMemoryReporter;
51 class nsIOService final : public nsIIOService,
52 public nsIObserver,
53 public nsINetUtil,
54 public nsISpeculativeConnect,
55 public nsSupportsWeakReference,
56 public nsIIOServiceInternal,
57 public nsIObserverService {
58 public:
59 NS_DECL_THREADSAFE_ISUPPORTS
60 NS_DECL_NSIIOSERVICE
61 NS_DECL_NSIOBSERVER
62 NS_DECL_NSINETUTIL
63 NS_DECL_NSISPECULATIVECONNECT
64 NS_DECL_NSIIOSERVICEINTERNAL
65 NS_DECL_NSIOBSERVERSERVICE
67 // Gets the singleton instance of the IO Service, creating it as needed
68 // Returns nullptr on out of memory or failure to initialize.
69 static already_AddRefed<nsIOService> GetInstance();
71 nsresult Init();
72 nsresult NewURI(const char* aSpec, nsIURI* aBaseURI, nsIURI** result,
73 nsIProtocolHandler** hdlrResult);
75 // Called by channels before a redirect happens. This notifies the global
76 // redirect observers.
77 nsresult AsyncOnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan,
78 uint32_t flags,
79 nsAsyncRedirectVerifyHelper* helper);
81 bool IsOffline() { return mOffline; }
82 PRIntervalTime LastOfflineStateChange() { return mLastOfflineStateChange; }
83 PRIntervalTime LastConnectivityChange() { return mLastConnectivityChange; }
84 PRIntervalTime LastNetworkLinkChange() { return mLastNetworkLinkChange; }
85 bool IsNetTearingDown() {
86 return mShutdown || mOfflineForProfileChange ||
87 mHttpHandlerAlreadyShutingDown;
89 PRIntervalTime NetTearingDownStarted() { return mNetTearingDownStarted; }
91 // nsHttpHandler is going to call this function to inform nsIOService that
92 // network is in process of tearing down. Moving nsHttpConnectionMgr::Shutdown
93 // to nsIOService caused problems (bug 1242755) so we doing it in this way. As
94 // soon as nsIOService gets notification that it is shutdown it is going to
95 // reset mHttpHandlerAlreadyShutingDown.
96 void SetHttpHandlerAlreadyShutingDown();
98 bool IsLinkUp();
100 // Converts an internal URI (e.g. one that has a username and password in
101 // it) into one which we can expose to the user, for example on the URL bar.
102 static already_AddRefed<nsIURI> CreateExposableURI(nsIURI*);
104 // Used to count the total number of HTTP requests made
105 void IncrementRequestNumber() { mTotalRequests++; }
106 uint32_t GetTotalRequestNumber() { return mTotalRequests; }
107 // Used to keep "race cache with network" stats
108 void IncrementCacheWonRequestNumber() { mCacheWon++; }
109 uint32_t GetCacheWonRequestNumber() { return mCacheWon; }
110 void IncrementNetWonRequestNumber() { mNetWon++; }
111 uint32_t GetNetWonRequestNumber() { return mNetWon; }
113 // Used to trigger a recheck of the captive portal status
114 nsresult RecheckCaptivePortal();
116 void OnProcessLaunchComplete(SocketProcessHost* aHost, bool aSucceeded);
117 void OnProcessUnexpectedShutdown(SocketProcessHost* aHost);
118 bool SocketProcessReady();
119 static void NotifySocketProcessPrefsChanged(const char* aName, void* aSelf);
120 void NotifySocketProcessPrefsChanged(const char* aName);
121 static bool UseSocketProcess(bool aCheckAgain = false);
123 bool IsSocketProcessLaunchComplete();
125 // Call func immediately if socket process is launched completely. Otherwise,
126 // |func| will be queued and then executed in the *main thread* once socket
127 // process is launced.
128 void CallOrWaitForSocketProcess(const std::function<void()>& aFunc);
130 int32_t SocketProcessPid();
131 SocketProcessHost* SocketProcess() { return mSocketProcess; }
133 friend SocketProcessMemoryReporter;
134 RefPtr<MemoryReportingProcess> GetSocketProcessMemoryReporter();
136 // Lookup the ProtocolHandlerInfo based on a given scheme.
137 // Safe to call from any thread.
138 ProtocolHandlerInfo LookupProtocolHandler(const nsACString& aScheme);
140 static void OnTLSPrefChange(const char* aPref, void* aSelf);
142 nsresult LaunchSocketProcess();
144 static bool TooManySocketProcessCrash();
145 static void IncreaseSocketProcessCrashCount();
146 #ifdef MOZ_WIDGET_ANDROID
147 static bool ShouldAddAdditionalSearchHeaders(nsIURI* aURI, bool* val);
148 #endif
150 private:
151 // These shouldn't be called directly:
152 // - construct using GetInstance
153 // - destroy using Release
154 nsIOService();
155 ~nsIOService();
156 nsresult SetConnectivityInternal(bool aConnectivity);
158 nsresult OnNetworkLinkEvent(const char* data);
160 nsresult InitializeCaptivePortalService();
161 nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan);
163 // Prefs wrangling
164 static void PrefsChanged(const char* pref, void* self);
165 void PrefsChanged(const char* pref = nullptr);
166 void ParsePortList(const char* pref, bool remove);
168 nsresult InitializeSocketTransportService();
169 nsresult InitializeNetworkLinkService();
170 nsresult InitializeProtocolProxyService();
172 // consolidated helper function
173 void LookupProxyInfo(nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
174 nsCString* aScheme, nsIProxyInfo** outPI);
176 nsresult NewChannelFromURIWithProxyFlagsInternal(
177 nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
178 nsINode* aLoadingNode, nsIPrincipal* aLoadingPrincipal,
179 nsIPrincipal* aTriggeringPrincipal,
180 const mozilla::Maybe<mozilla::dom::ClientInfo>& aLoadingClientInfo,
181 const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController,
182 uint32_t aSecurityFlags, nsContentPolicyType aContentPolicyType,
183 uint32_t aSandboxFlags, bool aSkipCheckForBrokenURLOrZeroSized,
184 nsIChannel** result);
186 nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI,
187 nsIURI* aProxyURI,
188 uint32_t aProxyFlags,
189 nsILoadInfo* aLoadInfo,
190 nsIChannel** result);
192 nsresult SpeculativeConnectInternal(
193 nsIURI* aURI, nsIPrincipal* aPrincipal,
194 Maybe<OriginAttributes>&& aOriginAttributes,
195 nsIInterfaceRequestor* aCallbacks, bool aAnonymous);
197 void DestroySocketProcess();
199 nsresult SetOfflineInternal(bool offline, bool notifySocketProcess = true);
201 bool UsesExternalProtocolHandler(const nsACString& aScheme)
202 MOZ_REQUIRES_SHARED(mLock);
204 private:
205 mozilla::Atomic<bool, mozilla::Relaxed> mOffline{true};
206 mozilla::Atomic<bool, mozilla::Relaxed> mOfflineForProfileChange{false};
207 bool mManageLinkStatus{false};
208 mozilla::Atomic<bool, mozilla::Relaxed> mConnectivity{true};
210 // Used to handle SetOffline() reentrancy. See the comment in
211 // SetOffline() for more details.
212 bool mSettingOffline{false};
213 bool mSetOfflineValue{false};
215 bool mSocketProcessLaunchComplete{false};
217 mozilla::Atomic<bool, mozilla::Relaxed> mShutdown{false};
218 mozilla::Atomic<bool, mozilla::Relaxed> mHttpHandlerAlreadyShutingDown{false};
220 nsCOMPtr<nsPISocketTransportService> mSocketTransportService;
221 nsCOMPtr<nsICaptivePortalService> mCaptivePortalService;
222 nsCOMPtr<nsINetworkLinkService> mNetworkLinkService;
223 bool mNetworkLinkServiceInitialized{false};
225 // cached categories
226 nsCategoryCache<nsIChannelEventSink> mChannelEventSinks{
227 NS_CHANNEL_EVENT_SINK_CATEGORY};
229 RWLock mLock{"nsIOService::mLock"};
230 nsTArray<int32_t> mRestrictedPortList MOZ_GUARDED_BY(mLock);
231 nsTArray<nsCString> mForceExternalSchemes MOZ_GUARDED_BY(mLock);
232 nsTHashMap<nsCString, RuntimeProtocolHandler> mRuntimeProtocolHandlers
233 MOZ_GUARDED_BY(mLock);
235 uint32_t mTotalRequests{0};
236 uint32_t mCacheWon{0};
237 uint32_t mNetWon{0};
238 static uint32_t sSocketProcessCrashedCount;
240 // These timestamps are needed for collecting telemetry on PR_Connect,
241 // PR_ConnectContinue and PR_Close blocking time. If we spend very long
242 // time in any of these functions we want to know if and what network
243 // change has happened shortly before.
244 mozilla::Atomic<PRIntervalTime> mLastOfflineStateChange;
245 mozilla::Atomic<PRIntervalTime> mLastConnectivityChange;
246 mozilla::Atomic<PRIntervalTime> mLastNetworkLinkChange;
248 // Time a network tearing down started.
249 mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted{0};
251 SocketProcessHost* mSocketProcess{nullptr};
253 // Events should be executed after the socket process is launched. Will
254 // dispatch these events while socket process fires OnProcessLaunchComplete.
255 // Note: this array is accessed only on the main thread.
256 nsTArray<std::function<void()>> mPendingEvents;
258 // The observer notifications need to be forwarded to socket process.
259 nsTHashSet<nsCString> mObserverTopicForSocketProcess;
260 // Some noticications (e.g., NS_XPCOM_SHUTDOWN_OBSERVER_ID) are triggered in
261 // socket process, so we should not send the notifications again.
262 nsTHashSet<nsCString> mSocketProcessTopicBlockedList;
263 // Used to store the topics that are already observed by IOService.
264 nsTHashSet<nsCString> mIOServiceTopicList;
266 nsCOMPtr<nsIObserverService> mObserverService;
268 public:
269 // Used for all default buffer sizes that necko allocates.
270 static uint32_t gDefaultSegmentSize;
271 static uint32_t gDefaultSegmentCount;
275 * Reference to the IO service singleton. May be null.
277 extern nsIOService* gIOService;
279 } // namespace net
280 } // namespace mozilla
282 #endif // nsIOService_h__