/src/mozilla-central/netwerk/base/nsIOService.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
6 | | #ifndef nsIOService_h__ |
7 | | #define nsIOService_h__ |
8 | | |
9 | | #include "nsStringFwd.h" |
10 | | #include "nsIIOService.h" |
11 | | #include "nsTArray.h" |
12 | | #include "nsCOMPtr.h" |
13 | | #include "nsWeakPtr.h" |
14 | | #include "nsIObserver.h" |
15 | | #include "nsWeakReference.h" |
16 | | #include "nsINetUtil.h" |
17 | | #include "nsIChannelEventSink.h" |
18 | | #include "nsCategoryCache.h" |
19 | | #include "nsISpeculativeConnect.h" |
20 | | #include "nsDataHashtable.h" |
21 | | #include "mozilla/Atomics.h" |
22 | | #include "mozilla/Attributes.h" |
23 | | #include "prtime.h" |
24 | | #include "nsICaptivePortalService.h" |
25 | | |
26 | 49.9M | #define NS_N(x) (sizeof(x)/sizeof(*x)) |
27 | | |
28 | | // We don't want to expose this observer topic. |
29 | | // Intended internal use only for remoting offline/inline events. |
30 | | // See Bug 552829 |
31 | 3 | #define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline" |
32 | 0 | #define NS_IPC_IOSERVICE_SET_CONNECTIVITY_TOPIC "ipc:network:set-connectivity" |
33 | | |
34 | | static const char gScheme[][sizeof("moz-safe-about")] = |
35 | | {"chrome", "file", "http", "https", "jar", "data", "about", "moz-safe-about", "resource", |
36 | | "moz-extension", "page-icon", "blob"}; |
37 | | |
38 | | static const char gForcedExternalSchemes[][sizeof("moz-nullprincipal")] = |
39 | | {"place", "fake-favicon-uri", "favicon", "moz-nullprincipal"}; |
40 | | |
41 | | class nsINetworkLinkService; |
42 | | class nsIPrefBranch; |
43 | | class nsIProtocolProxyService2; |
44 | | class nsIProxyInfo; |
45 | | class nsPISocketTransportService; |
46 | | |
47 | | namespace mozilla { |
48 | | namespace net { |
49 | | class NeckoChild; |
50 | | class nsAsyncRedirectVerifyHelper; |
51 | | |
52 | | class nsIOService final : public nsIIOService |
53 | | , public nsIObserver |
54 | | , public nsINetUtil |
55 | | , public nsISpeculativeConnect |
56 | | , public nsSupportsWeakReference |
57 | | , public nsIIOServiceInternal |
58 | | { |
59 | | public: |
60 | | NS_DECL_THREADSAFE_ISUPPORTS |
61 | | NS_DECL_NSIIOSERVICE |
62 | | NS_DECL_NSIOBSERVER |
63 | | NS_DECL_NSINETUTIL |
64 | | NS_DECL_NSISPECULATIVECONNECT |
65 | | NS_DECL_NSIIOSERVICEINTERNAL |
66 | | |
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(); |
70 | | |
71 | | nsresult Init(); |
72 | | nsresult NewURI(const char* aSpec, nsIURI* aBaseURI, |
73 | | nsIURI* *result, |
74 | | nsIProtocolHandler* *hdlrResult); |
75 | | |
76 | | // Called by channels before a redirect happens. This notifies the global |
77 | | // redirect observers. |
78 | | nsresult AsyncOnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan, |
79 | | uint32_t flags, |
80 | | nsAsyncRedirectVerifyHelper *helper); |
81 | | |
82 | 0 | bool IsOffline() { return mOffline; } |
83 | 0 | PRIntervalTime LastOfflineStateChange() { return mLastOfflineStateChange; } |
84 | 0 | PRIntervalTime LastConnectivityChange() { return mLastConnectivityChange; } |
85 | 0 | PRIntervalTime LastNetworkLinkChange() { return mLastNetworkLinkChange; } |
86 | 7 | bool IsNetTearingDown() { return mShutdown || mOfflineForProfileChange || |
87 | 7 | mHttpHandlerAlreadyShutingDown; } |
88 | 0 | PRIntervalTime NetTearingDownStarted() { return mNetTearingDownStarted; } |
89 | | |
90 | | // nsHttpHandler is going to call this function to inform nsIOService that network |
91 | | // is in process of tearing down. Moving nsHttpConnectionMgr::Shutdown to nsIOService |
92 | | // caused problems (bug 1242755) so we doing it in this way. |
93 | | // As soon as nsIOService gets notification that it is shutdown it is going to |
94 | | // reset mHttpHandlerAlreadyShutingDown. |
95 | | void SetHttpHandlerAlreadyShutingDown(); |
96 | | |
97 | | bool IsLinkUp(); |
98 | | |
99 | | static bool IsDataURIUniqueOpaqueOrigin(); |
100 | | static bool BlockToplevelDataUriNavigations(); |
101 | | |
102 | | static bool BlockFTPSubresources(); |
103 | | |
104 | | // Used to count the total number of HTTP requests made |
105 | 0 | void IncrementRequestNumber() { mTotalRequests++; } |
106 | 0 | uint32_t GetTotalRequestNumber() { return mTotalRequests; } |
107 | | // Used to keep "race cache with network" stats |
108 | 0 | void IncrementCacheWonRequestNumber() { mCacheWon++; } |
109 | 0 | uint32_t GetCacheWonRequestNumber() { return mCacheWon; } |
110 | 0 | void IncrementNetWonRequestNumber() { mNetWon++; } |
111 | 0 | uint32_t GetNetWonRequestNumber() { return mNetWon; } |
112 | | |
113 | | // Used to trigger a recheck of the captive portal status |
114 | | nsresult RecheckCaptivePortal(); |
115 | | private: |
116 | | // These shouldn't be called directly: |
117 | | // - construct using GetInstance |
118 | | // - destroy using Release |
119 | | nsIOService(); |
120 | | ~nsIOService(); |
121 | | nsresult SetConnectivityInternal(bool aConnectivity); |
122 | | |
123 | | nsresult OnNetworkLinkEvent(const char *data); |
124 | | |
125 | | nsresult GetCachedProtocolHandler(const char *scheme, |
126 | | nsIProtocolHandler* *hdlrResult, |
127 | | uint32_t start=0, |
128 | | uint32_t end=0); |
129 | | nsresult CacheProtocolHandler(const char *scheme, |
130 | | nsIProtocolHandler* hdlr); |
131 | | |
132 | | nsresult InitializeCaptivePortalService(); |
133 | | nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan); |
134 | | |
135 | | // Prefs wrangling |
136 | | void PrefsChanged(const char *pref = nullptr); |
137 | | void ParsePortList(const char *pref, bool remove); |
138 | | |
139 | | nsresult InitializeSocketTransportService(); |
140 | | nsresult InitializeNetworkLinkService(); |
141 | | nsresult InitializeProtocolProxyService(); |
142 | | |
143 | | // consolidated helper function |
144 | | void LookupProxyInfo(nsIURI *aURI, nsIURI *aProxyURI, uint32_t aProxyFlags, |
145 | | nsCString *aScheme, nsIProxyInfo **outPI); |
146 | | |
147 | | nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI, |
148 | | nsIURI* aProxyURI, |
149 | | uint32_t aProxyFlags, |
150 | | nsINode* aLoadingNode, |
151 | | nsIPrincipal* aLoadingPrincipal, |
152 | | nsIPrincipal* aTriggeringPrincipal, |
153 | | const mozilla::Maybe<mozilla::dom::ClientInfo>& aLoadingClientInfo, |
154 | | const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController, |
155 | | uint32_t aSecurityFlags, |
156 | | uint32_t aContentPolicyType, |
157 | | nsIChannel** result); |
158 | | |
159 | | nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI, |
160 | | nsIURI* aProxyURI, |
161 | | uint32_t aProxyFlags, |
162 | | nsILoadInfo* aLoadInfo, |
163 | | nsIChannel** result); |
164 | | |
165 | | nsresult SpeculativeConnectInternal(nsIURI *aURI, |
166 | | nsIPrincipal *aPrincipal, |
167 | | nsIInterfaceRequestor *aCallbacks, |
168 | | bool aAnonymous); |
169 | | |
170 | | private: |
171 | | bool mOffline; |
172 | | mozilla::Atomic<bool, mozilla::Relaxed> mOfflineForProfileChange; |
173 | | bool mManageLinkStatus; |
174 | | bool mConnectivity; |
175 | | // If true, the connectivity state will be mirrored by IOService.offline |
176 | | // meaning if !mConnectivity, GetOffline() will return true |
177 | | bool mOfflineMirrorsConnectivity; |
178 | | |
179 | | // Used to handle SetOffline() reentrancy. See the comment in |
180 | | // SetOffline() for more details. |
181 | | bool mSettingOffline; |
182 | | bool mSetOfflineValue; |
183 | | |
184 | | mozilla::Atomic<bool, mozilla::Relaxed> mShutdown; |
185 | | mozilla::Atomic<bool, mozilla::Relaxed> mHttpHandlerAlreadyShutingDown; |
186 | | |
187 | | nsCOMPtr<nsPISocketTransportService> mSocketTransportService; |
188 | | nsCOMPtr<nsICaptivePortalService> mCaptivePortalService; |
189 | | nsCOMPtr<nsINetworkLinkService> mNetworkLinkService; |
190 | | bool mNetworkLinkServiceInitialized; |
191 | | |
192 | | // Cached protocol handlers, only accessed on the main thread |
193 | | nsWeakPtr mWeakHandler[NS_N(gScheme)]; |
194 | | |
195 | | // cached categories |
196 | | nsCategoryCache<nsIChannelEventSink> mChannelEventSinks; |
197 | | |
198 | | nsTArray<int32_t> mRestrictedPortList; |
199 | | |
200 | | bool mNetworkNotifyChanged; |
201 | | |
202 | | static bool sIsDataURIUniqueOpaqueOrigin; |
203 | | static bool sBlockToplevelDataUriNavigations; |
204 | | |
205 | | static bool sBlockFTPSubresources; |
206 | | |
207 | | uint32_t mTotalRequests; |
208 | | uint32_t mCacheWon; |
209 | | uint32_t mNetWon; |
210 | | |
211 | | // These timestamps are needed for collecting telemetry on PR_Connect, |
212 | | // PR_ConnectContinue and PR_Close blocking time. If we spend very long |
213 | | // time in any of these functions we want to know if and what network |
214 | | // change has happened shortly before. |
215 | | mozilla::Atomic<PRIntervalTime> mLastOfflineStateChange; |
216 | | mozilla::Atomic<PRIntervalTime> mLastConnectivityChange; |
217 | | mozilla::Atomic<PRIntervalTime> mLastNetworkLinkChange; |
218 | | |
219 | | // Time a network tearing down started. |
220 | | mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted; |
221 | | public: |
222 | | // Used for all default buffer sizes that necko allocates. |
223 | | static uint32_t gDefaultSegmentSize; |
224 | | static uint32_t gDefaultSegmentCount; |
225 | | }; |
226 | | |
227 | | /** |
228 | | * Reference to the IO service singleton. May be null. |
229 | | */ |
230 | | extern nsIOService* gIOService; |
231 | | |
232 | | } // namespace net |
233 | | } // namespace mozilla |
234 | | |
235 | | #endif // nsIOService_h__ |