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
// Used to count the total number of HTTP requests made
106
void IncrementRequestNumber() { mTotalRequests++; }
107
uint32_t GetTotalRequestNumber() { return mTotalRequests; }
108
// Used to keep "race cache with network" stats
109
void IncrementCacheWonRequestNumber() { mCacheWon++; }
110
uint32_t GetCacheWonRequestNumber() { return mCacheWon; }
111
void IncrementNetWonRequestNumber() { mNetWon++; }
112
uint32_t GetNetWonRequestNumber() { return mNetWon; }
113
114
// Used to trigger a recheck of the captive portal status
115
nsresult RecheckCaptivePortal();
116
117
void OnProcessLaunchComplete(SocketProcessHost* aHost, bool aSucceeded);
118
void OnProcessUnexpectedShutdown(SocketProcessHost* aHost);
119
bool SocketProcessReady();
120
static void NotifySocketProcessPrefsChanged(const char* aName, void* aSelf);
121
void NotifySocketProcessPrefsChanged(const char* aName);
122
123
bool IsSocketProcessLaunchComplete();
124
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);
129
130
int32_t SocketProcessPid();
131
SocketProcessHost* SocketProcess() { return mSocketProcess; }
132
133
friend SocketProcessMemoryReporter;
134
RefPtr<MemoryReportingProcess> GetSocketProcessMemoryReporter();
135
136
private:
137
// These shouldn't be called directly:
138
// - construct using GetInstance
139
// - destroy using Release
140
nsIOService();
141
~nsIOService();
142
nsresult SetConnectivityInternal(bool aConnectivity);
143
144
nsresult OnNetworkLinkEvent(const char* data);
145
146
nsresult GetCachedProtocolHandler(const char* scheme,
147
nsIProtocolHandler** hdlrResult,
148
uint32_t start = 0, uint32_t end = 0);
149
nsresult CacheProtocolHandler(const char* scheme, nsIProtocolHandler* hdlr);
150
151
nsresult InitializeCaptivePortalService();
152
nsresult RecheckCaptivePortalIfLocalRedirect(nsIChannel* newChan);
153
154
// Prefs wrangling
155
static void PrefsChanged(const char* pref, void* self);
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
static bool sIsDataURIUniqueOpaqueOrigin;
222
static bool sBlockToplevelDataUriNavigations;
223
224
uint32_t mTotalRequests;
225
uint32_t mCacheWon;
226
uint32_t mNetWon;
227
228
// These timestamps are needed for collecting telemetry on PR_Connect,
229
// PR_ConnectContinue and PR_Close blocking time. If we spend very long
230
// time in any of these functions we want to know if and what network
231
// change has happened shortly before.
232
mozilla::Atomic<PRIntervalTime> mLastOfflineStateChange;
233
mozilla::Atomic<PRIntervalTime> mLastConnectivityChange;
234
mozilla::Atomic<PRIntervalTime> mLastNetworkLinkChange;
235
236
// Time a network tearing down started.
237
mozilla::Atomic<PRIntervalTime> mNetTearingDownStarted;
238
239
SocketProcessHost* mSocketProcess;
240
241
// Events should be executed after the socket process is launched. Will
242
// dispatch these events while socket process fires OnProcessLaunchComplete.
243
// Note: this array is accessed only on the main thread.
244
nsTArray<std::function<void()>> mPendingEvents;
245
246
public:
247
// Used for all default buffer sizes that necko allocates.
248
static uint32_t gDefaultSegmentSize;
249
static uint32_t gDefaultSegmentCount;
250
};
251
252
/**
253
* Reference to the IO service singleton. May be null.
254
*/
255
extern nsIOService* gIOService;
256
257
} // namespace net
258
} // namespace mozilla
259
260
#endif // nsIOService_h__