Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / netwerk / base / nsIOService.h
blob52c8b48e7f81562f9c7077642c8100ee559aa5c5
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();
147 private:
148 // These shouldn't be called directly:
149 // - construct using GetInstance
150 // - destroy using Release
151 nsIOService();
152 ~nsIOService();
153 nsresult SetConnectivityInternal(bool aConnectivity);
155 nsresult OnNetworkLinkEvent(const char* data);
157 nsresult InitializeCaptivePortalService();
158 nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan);
160 // Prefs wrangling
161 static void PrefsChanged(const char* pref, void* self);
162 void PrefsChanged(const char* pref = nullptr);
163 void ParsePortList(const char* pref, bool remove);
165 nsresult InitializeSocketTransportService();
166 nsresult InitializeNetworkLinkService();
167 nsresult InitializeProtocolProxyService();
169 // consolidated helper function
170 void LookupProxyInfo(nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
171 nsCString* aScheme, nsIProxyInfo** outPI);
173 nsresult NewChannelFromURIWithProxyFlagsInternal(
174 nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
175 nsINode* aLoadingNode, nsIPrincipal* aLoadingPrincipal,
176 nsIPrincipal* aTriggeringPrincipal,
177 const mozilla::Maybe<mozilla::dom::ClientInfo>& aLoadingClientInfo,
178 const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController,
179 uint32_t aSecurityFlags, nsContentPolicyType aContentPolicyType,
180 uint32_t aSandboxFlags, bool aSkipCheckForBrokenURLOrZeroSized,
181 nsIChannel** result);
183 nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI,
184 nsIURI* aProxyURI,
185 uint32_t aProxyFlags,
186 nsILoadInfo* aLoadInfo,
187 nsIChannel** result);
189 nsresult SpeculativeConnectInternal(
190 nsIURI* aURI, nsIPrincipal* aPrincipal,
191 Maybe<OriginAttributes>&& aOriginAttributes,
192 nsIInterfaceRequestor* aCallbacks, bool aAnonymous);
194 void DestroySocketProcess();
196 nsresult SetOfflineInternal(bool offline, bool notifySocketProcess = true);
198 bool UsesExternalProtocolHandler(const nsACString& aScheme)
199 MOZ_REQUIRES_SHARED(mLock);
201 private:
202 mozilla::Atomic<bool, mozilla::Relaxed> mOffline{true};
203 mozilla::Atomic<bool, mozilla::Relaxed> mOfflineForProfileChange{false};
204 bool mManageLinkStatus{false};
205 mozilla::Atomic<bool, mozilla::Relaxed> mConnectivity{true};
207 // Used to handle SetOffline() reentrancy. See the comment in
208 // SetOffline() for more details.
209 bool mSettingOffline{false};
210 bool mSetOfflineValue{false};
212 bool mSocketProcessLaunchComplete{false};
214 mozilla::Atomic<bool, mozilla::Relaxed> mShutdown{false};
215 mozilla::Atomic<bool, mozilla::Relaxed> mHttpHandlerAlreadyShutingDown{false};
217 nsCOMPtr<nsPISocketTransportService> mSocketTransportService;
218 nsCOMPtr<nsICaptivePortalService> mCaptivePortalService;
219 nsCOMPtr<nsINetworkLinkService> mNetworkLinkService;
220 bool mNetworkLinkServiceInitialized{false};
222 // cached categories
223 nsCategoryCache<nsIChannelEventSink> mChannelEventSinks{
224 NS_CHANNEL_EVENT_SINK_CATEGORY};
226 RWLock mLock{"nsIOService::mLock"};
227 nsTArray<int32_t> mRestrictedPortList MOZ_GUARDED_BY(mLock);
228 nsTArray<nsCString> mForceExternalSchemes MOZ_GUARDED_BY(mLock);
229 nsTHashMap<nsCString, RuntimeProtocolHandler> mRuntimeProtocolHandlers
230 MOZ_GUARDED_BY(mLock);
232 uint32_t mTotalRequests{0};
233 uint32_t mCacheWon{0};
234 uint32_t mNetWon{0};
235 static uint32_t sSocketProcessCrashedCount;
237 // These timestamps are needed for collecting telemetry on PR_Connect,
238 // PR_ConnectContinue and PR_Close blocking time. If we spend very long
239 // time in any of these functions we want to know if and what network
240 // change has happened shortly before.
241 mozilla::Atomic<PRIntervalTime> mLastOfflineStateChange;
242 mozilla::Atomic<PRIntervalTime> mLastConnectivityChange;
243 mozilla::Atomic<PRIntervalTime> mLastNetworkLinkChange;
245 // Time a network tearing down started.
246 mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted{0};
248 SocketProcessHost* mSocketProcess{nullptr};
250 // Events should be executed after the socket process is launched. Will
251 // dispatch these events while socket process fires OnProcessLaunchComplete.
252 // Note: this array is accessed only on the main thread.
253 nsTArray<std::function<void()>> mPendingEvents;
255 // The observer notifications need to be forwarded to socket process.
256 nsTHashSet<nsCString> mObserverTopicForSocketProcess;
257 // Some noticications (e.g., NS_XPCOM_SHUTDOWN_OBSERVER_ID) are triggered in
258 // socket process, so we should not send the notifications again.
259 nsTHashSet<nsCString> mSocketProcessTopicBlockedList;
260 // Used to store the topics that are already observed by IOService.
261 nsTHashSet<nsCString> mIOServiceTopicList;
263 nsCOMPtr<nsIObserverService> mObserverService;
265 public:
266 // Used for all default buffer sizes that necko allocates.
267 static uint32_t gDefaultSegmentSize;
268 static uint32_t gDefaultSegmentCount;
272 * Reference to the IO service singleton. May be null.
274 extern nsIOService* gIOService;
276 } // namespace net
277 } // namespace mozilla
279 #endif // nsIOService_h__