Source code

Revision control

Other Tools

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/. */
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 "nsIObserver.h"
14
#include "nsIWeakReferenceUtils.h"
15
#include "nsINetUtil.h"
16
#include "nsIChannelEventSink.h"
17
#include "nsCategoryCache.h"
18
#include "nsISpeculativeConnect.h"
19
#include "nsDataHashtable.h"
20
#include "mozilla/Atomics.h"
21
#include "mozilla/Attributes.h"
22
#include "prtime.h"
23
#include "nsICaptivePortalService.h"
24
25
#define NS_N(x) (sizeof(x) / sizeof(*x))
26
27
// We don't want to expose this observer topic.
28
// Intended internal use only for remoting offline/inline events.
29
// See Bug 552829
30
#define NS_IPC_IOSERVICE_SET_OFFLINE_TOPIC "ipc:network:set-offline"
31
#define NS_IPC_IOSERVICE_SET_CONNECTIVITY_TOPIC "ipc:network:set-connectivity"
32
33
static const char gScheme[][sizeof("moz-safe-about")] = {
34
"chrome", "file", "http", "https",
35
"jar", "data", "about", "moz-safe-about",
36
"resource", "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
class MemoryReportingProcess;
49
namespace net {
50
class NeckoChild;
51
class nsAsyncRedirectVerifyHelper;
52
class SocketProcessHost;
53
class SocketProcessMemoryReporter;
54
55
class nsIOService final : public nsIIOService,
56
public nsIObserver,
57
public nsINetUtil,
58
public nsISpeculativeConnect,
59
public nsSupportsWeakReference,
60
public nsIIOServiceInternal {
61
public:
62
NS_DECL_THREADSAFE_ISUPPORTS
63
NS_DECL_NSIIOSERVICE
64
NS_DECL_NSIOBSERVER
65
NS_DECL_NSINETUTIL
66
NS_DECL_NSISPECULATIVECONNECT
67
NS_DECL_NSIIOSERVICEINTERNAL
68
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();
72
73
nsresult Init();
74
nsresult NewURI(const char* aSpec, nsIURI* aBaseURI, nsIURI** result,
75
nsIProtocolHandler** hdlrResult);
76
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);
82
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;
90
}
91
PRIntervalTime NetTearingDownStarted() { return mNetTearingDownStarted; }
92
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();
99
100
bool IsLinkUp();
101
102
static bool IsDataURIUniqueOpaqueOrigin();
103
static bool BlockToplevelDataUriNavigations();
104
105
static bool BlockFTPSubresources();
106
107
// Used to count the total number of HTTP requests made
108
void IncrementRequestNumber() { mTotalRequests++; }
109
uint32_t GetTotalRequestNumber() { return mTotalRequests; }
110
// Used to keep "race cache with network" stats
111
void IncrementCacheWonRequestNumber() { mCacheWon++; }
112
uint32_t GetCacheWonRequestNumber() { return mCacheWon; }
113
void IncrementNetWonRequestNumber() { mNetWon++; }
114
uint32_t GetNetWonRequestNumber() { return mNetWon; }
115
116
// Used to trigger a recheck of the captive portal status
117
nsresult RecheckCaptivePortal();
118
119
void OnProcessLaunchComplete(SocketProcessHost* aHost, bool aSucceeded);
120
void OnProcessUnexpectedShutdown(SocketProcessHost* aHost);
121
bool SocketProcessReady();
122
void NotifySocketProcessPrefsChanged(const char* aName);
123
124
bool IsSocketProcessLaunchComplete();
125
126
// Call func immediately if socket process is launched completely. Otherwise,
127
// |func| will be queued and then executed in the *main thread* once socket
128
// process is launced.
129
void CallOrWaitForSocketProcess(const std::function<void()>& aFunc);
130
131
int32_t SocketProcessPid();
132
SocketProcessHost* SocketProcess() { return mSocketProcess; }
133
134
friend SocketProcessMemoryReporter;
135
RefPtr<MemoryReportingProcess> GetSocketProcessMemoryReporter();
136
137
private:
138
// These shouldn't be called directly:
139
// - construct using GetInstance
140
// - destroy using Release
141
nsIOService();
142
~nsIOService();
143
nsresult SetConnectivityInternal(bool aConnectivity);
144
145
nsresult OnNetworkLinkEvent(const char* data);
146
147
nsresult GetCachedProtocolHandler(const char* scheme,
148
nsIProtocolHandler** hdlrResult,
149
uint32_t start = 0, uint32_t end = 0);
150
nsresult CacheProtocolHandler(const char* scheme, nsIProtocolHandler* hdlr);
151
152
nsresult InitializeCaptivePortalService();
153
nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan);
154
155
// Prefs wrangling
156
void PrefsChanged(const char* pref = nullptr);
157
void ParsePortList(const char* pref, bool remove);
158
159
nsresult InitializeSocketTransportService();
160
nsresult InitializeNetworkLinkService();
161
nsresult InitializeProtocolProxyService();
162
163
// consolidated helper function
164
void LookupProxyInfo(nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
165
nsCString* aScheme, nsIProxyInfo** outPI);
166
167
nsresult NewChannelFromURIWithProxyFlagsInternal(
168
nsIURI* aURI, nsIURI* aProxyURI, uint32_t aProxyFlags,
169
nsINode* aLoadingNode, nsIPrincipal* aLoadingPrincipal,
170
nsIPrincipal* aTriggeringPrincipal,
171
const mozilla::Maybe<mozilla::dom::ClientInfo>& aLoadingClientInfo,
172
const mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController,
173
uint32_t aSecurityFlags, uint32_t aContentPolicyType,
174
nsIChannel** result);
175
176
nsresult NewChannelFromURIWithProxyFlagsInternal(nsIURI* aURI,
177
nsIURI* aProxyURI,
178
uint32_t aProxyFlags,
179
nsILoadInfo* aLoadInfo,
180
nsIChannel** result);
181
182
nsresult SpeculativeConnectInternal(nsIURI* aURI, nsIPrincipal* aPrincipal,
183
nsIInterfaceRequestor* aCallbacks,
184
bool aAnonymous);
185
186
nsresult LaunchSocketProcess();
187
void DestroySocketProcess();
188
189
private:
190
bool mOffline;
191
mozilla::Atomic<bool, mozilla::Relaxed> mOfflineForProfileChange;
192
bool mManageLinkStatus;
193
bool mConnectivity;
194
// If true, the connectivity state will be mirrored by IOService.offline
195
// meaning if !mConnectivity, GetOffline() will return true
196
bool mOfflineMirrorsConnectivity;
197
198
// Used to handle SetOffline() reentrancy. See the comment in
199
// SetOffline() for more details.
200
bool mSettingOffline;
201
bool mSetOfflineValue;
202
203
bool mSocketProcessLaunchComplete;
204
205
mozilla::Atomic<bool, mozilla::Relaxed> mShutdown;
206
mozilla::Atomic<bool, mozilla::Relaxed> mHttpHandlerAlreadyShutingDown;
207
208
nsCOMPtr<nsPISocketTransportService> mSocketTransportService;
209
nsCOMPtr<nsICaptivePortalService> mCaptivePortalService;
210
nsCOMPtr<nsINetworkLinkService> mNetworkLinkService;
211
bool mNetworkLinkServiceInitialized;
212
213
// Cached protocol handlers, only accessed on the main thread
214
nsWeakPtr mWeakHandler[NS_N(gScheme)];
215
216
// cached categories
217
nsCategoryCache<nsIChannelEventSink> mChannelEventSinks;
218
219
nsTArray<int32_t> mRestrictedPortList;
220
221
bool mNetworkNotifyChanged;
222
223
static bool sIsDataURIUniqueOpaqueOrigin;
224
static bool sBlockToplevelDataUriNavigations;
225
226
static bool sBlockFTPSubresources;
227
228
uint32_t mTotalRequests;
229
uint32_t mCacheWon;
230
uint32_t mNetWon;
231
232
// These timestamps are needed for collecting telemetry on PR_Connect,
233
// PR_ConnectContinue and PR_Close blocking time. If we spend very long
234
// time in any of these functions we want to know if and what network
235
// change has happened shortly before.
236
mozilla::Atomic<PRIntervalTime> mLastOfflineStateChange;
237
mozilla::Atomic<PRIntervalTime> mLastConnectivityChange;
238
mozilla::Atomic<PRIntervalTime> mLastNetworkLinkChange;
239
240
// Time a network tearing down started.
241
mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted;
242
243
SocketProcessHost* mSocketProcess;
244
245
// Events should be executed after the socket process is launched. Will
246
// dispatch these events while socket process fires OnProcessLaunchComplete.
247
// Note: this array is accessed only on the main thread.
248
nsTArray<std::function<void()>> mPendingEvents;
249
250
public:
251
// Used for all default buffer sizes that necko allocates.
252
static uint32_t gDefaultSegmentSize;
253
static uint32_t gDefaultSegmentCount;
254
};
255
256
/**
257
* Reference to the IO service singleton. May be null.
258
*/
259
extern nsIOService* gIOService;
260
261
} // namespace net
262
} // namespace mozilla
263
264
#endif // nsIOService_h__