Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 4; 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
/*
7
* A service that provides methods for synchronously loading a DOM in various
8
* ways.
9
*/
10
11
#include "nsSyncLoadService.h"
12
#include "nsCOMPtr.h"
13
#include "nsIChannel.h"
14
#include "nsIChannelEventSink.h"
15
#include "nsIAsyncVerifyRedirectCallback.h"
16
#include "nsIInterfaceRequestor.h"
17
#include "nsIStreamListener.h"
18
#include "nsIURI.h"
19
#include "nsString.h"
20
#include "nsWeakReference.h"
21
#include "mozilla/dom/Document.h"
22
#include "nsIPrincipal.h"
23
#include "nsContentUtils.h" // for kLoadAsData
24
#include "nsThreadUtils.h"
25
#include "nsNetUtil.h"
26
#include "nsStreamUtils.h"
27
#include "ReferrerInfo.h"
28
#include <algorithm>
29
30
using namespace mozilla;
31
using namespace mozilla::dom;
32
33
using mozilla::dom::ReferrerPolicy;
34
35
/**
36
* This class manages loading a single XML document
37
*/
38
39
class nsSyncLoader : public nsIStreamListener,
40
public nsIChannelEventSink,
41
public nsIInterfaceRequestor,
42
public nsSupportsWeakReference {
43
public:
44
nsSyncLoader()
45
: mLoading(false), mAsyncLoadStatus(NS_ERROR_NOT_INITIALIZED) {}
46
47
NS_DECL_ISUPPORTS
48
49
nsresult LoadDocument(nsIChannel* aChannel, bool aChannelIsSync,
50
bool aForceToXML, ReferrerPolicy aReferrerPolicy,
51
Document** aResult);
52
53
NS_FORWARD_NSISTREAMLISTENER(mListener->)
54
NS_DECL_NSIREQUESTOBSERVER
55
56
NS_DECL_NSICHANNELEVENTSINK
57
58
NS_DECL_NSIINTERFACEREQUESTOR
59
60
private:
61
virtual ~nsSyncLoader();
62
63
nsresult PushAsyncStream(nsIStreamListener* aListener);
64
nsresult PushSyncStream(nsIStreamListener* aListener);
65
66
nsCOMPtr<nsIChannel> mChannel;
67
nsCOMPtr<nsIStreamListener> mListener;
68
bool mLoading;
69
nsresult mAsyncLoadStatus;
70
};
71
72
class nsForceXMLListener : public nsIStreamListener {
73
virtual ~nsForceXMLListener();
74
75
public:
76
explicit nsForceXMLListener(nsIStreamListener* aListener);
77
78
NS_DECL_ISUPPORTS
79
NS_FORWARD_NSISTREAMLISTENER(mListener->)
80
NS_DECL_NSIREQUESTOBSERVER
81
82
private:
83
nsCOMPtr<nsIStreamListener> mListener;
84
};
85
86
nsForceXMLListener::nsForceXMLListener(nsIStreamListener* aListener)
87
: mListener(aListener) {}
88
89
nsForceXMLListener::~nsForceXMLListener() {}
90
91
NS_IMPL_ISUPPORTS(nsForceXMLListener, nsIStreamListener, nsIRequestObserver)
92
93
NS_IMETHODIMP
94
nsForceXMLListener::OnStartRequest(nsIRequest* aRequest) {
95
nsresult status;
96
aRequest->GetStatus(&status);
97
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
98
if (channel && NS_SUCCEEDED(status)) {
99
channel->SetContentType(NS_LITERAL_CSTRING("text/xml"));
100
}
101
102
return mListener->OnStartRequest(aRequest);
103
}
104
105
NS_IMETHODIMP
106
nsForceXMLListener::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) {
107
return mListener->OnStopRequest(aRequest, aStatusCode);
108
}
109
110
nsSyncLoader::~nsSyncLoader() {
111
if (mLoading && mChannel) {
112
mChannel->Cancel(NS_BINDING_ABORTED);
113
}
114
}
115
116
NS_IMPL_ISUPPORTS(nsSyncLoader, nsIStreamListener, nsIRequestObserver,
117
nsIChannelEventSink, nsIInterfaceRequestor,
118
nsISupportsWeakReference)
119
120
nsresult nsSyncLoader::LoadDocument(nsIChannel* aChannel, bool aChannelIsSync,
121
bool aForceToXML,
122
ReferrerPolicy aReferrerPolicy,
123
Document** aResult) {
124
NS_ENSURE_ARG(aChannel);
125
NS_ENSURE_ARG_POINTER(aResult);
126
*aResult = nullptr;
127
nsresult rv = NS_OK;
128
129
mChannel = aChannel;
130
nsCOMPtr<nsIHttpChannel> http = do_QueryInterface(mChannel);
131
if (http) {
132
rv = http->SetRequestHeader(
133
NS_LITERAL_CSTRING("Accept"),
134
NS_LITERAL_CSTRING(
135
"text/xml,application/xml,application/xhtml+xml,*/*;q=0.1"),
136
false);
137
MOZ_ASSERT(NS_SUCCEEDED(rv));
138
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
139
nsCOMPtr<nsIURI> loaderUri;
140
loadInfo->TriggeringPrincipal()->GetURI(getter_AddRefs(loaderUri));
141
if (loaderUri) {
142
nsCOMPtr<nsIReferrerInfo> referrerInfo =
143
new ReferrerInfo(loaderUri, aReferrerPolicy);
144
rv = http->SetReferrerInfoWithoutClone(referrerInfo);
145
MOZ_ASSERT(NS_SUCCEEDED(rv));
146
}
147
}
148
149
// Hook us up to listen to redirects and the like.
150
// Do this before setting up the cross-site proxy since
151
// that installs its own proxies.
152
mChannel->SetNotificationCallbacks(this);
153
154
// Get the loadgroup of the channel
155
nsCOMPtr<nsILoadGroup> loadGroup;
156
rv = aChannel->GetLoadGroup(getter_AddRefs(loadGroup));
157
NS_ENSURE_SUCCESS(rv, rv);
158
159
// Create document
160
nsCOMPtr<Document> document;
161
rv = NS_NewXMLDocument(getter_AddRefs(document));
162
NS_ENSURE_SUCCESS(rv, rv);
163
164
// Start the document load. Do this before we attach the load listener
165
// since we reset the document which drops all observers.
166
nsCOMPtr<nsIStreamListener> listener;
167
rv = document->StartDocumentLoad(kLoadAsData, mChannel, loadGroup, nullptr,
168
getter_AddRefs(listener), true);
169
NS_ENSURE_SUCCESS(rv, rv);
170
171
if (aForceToXML) {
172
nsCOMPtr<nsIStreamListener> forceListener =
173
new nsForceXMLListener(listener);
174
listener.swap(forceListener);
175
}
176
177
if (aChannelIsSync) {
178
rv = PushSyncStream(listener);
179
} else {
180
rv = PushAsyncStream(listener);
181
}
182
183
http = do_QueryInterface(mChannel);
184
if (NS_SUCCEEDED(rv) && http) {
185
bool succeeded;
186
if (NS_FAILED(http->GetRequestSucceeded(&succeeded)) || !succeeded) {
187
rv = NS_ERROR_FAILURE;
188
}
189
}
190
mChannel = nullptr;
191
192
// check that the load succeeded
193
NS_ENSURE_SUCCESS(rv, rv);
194
195
NS_ENSURE_TRUE(document->GetRootElement(), NS_ERROR_FAILURE);
196
197
document.forget(aResult);
198
199
return NS_OK;
200
}
201
202
nsresult nsSyncLoader::PushAsyncStream(nsIStreamListener* aListener) {
203
mListener = aListener;
204
205
mAsyncLoadStatus = NS_OK;
206
207
// Start reading from the channel
208
nsresult rv = mChannel->AsyncOpen(this);
209
210
if (NS_SUCCEEDED(rv)) {
211
// process events until we're finished.
212
mLoading = true;
213
nsIThread* thread = NS_GetCurrentThread();
214
while (mLoading && NS_SUCCEEDED(rv)) {
215
bool processedEvent;
216
rv = thread->ProcessNextEvent(true, &processedEvent);
217
if (NS_SUCCEEDED(rv) && !processedEvent) rv = NS_ERROR_UNEXPECTED;
218
}
219
}
220
221
mListener = nullptr;
222
223
NS_ENSURE_SUCCESS(rv, rv);
224
225
// Note that if AsyncOpen failed that's ok -- the only caller of
226
// this method nulls out mChannel immediately after we return.
227
228
return mAsyncLoadStatus;
229
}
230
231
nsresult nsSyncLoader::PushSyncStream(nsIStreamListener* aListener) {
232
nsCOMPtr<nsIInputStream> in;
233
nsresult rv = mChannel->Open(getter_AddRefs(in));
234
NS_ENSURE_SUCCESS(rv, rv);
235
236
mLoading = true;
237
rv = nsSyncLoadService::PushSyncStreamToListener(in.forget(), aListener,
238
mChannel);
239
mLoading = false;
240
241
return rv;
242
}
243
244
NS_IMETHODIMP
245
nsSyncLoader::OnStartRequest(nsIRequest* aRequest) {
246
return mListener->OnStartRequest(aRequest);
247
}
248
249
NS_IMETHODIMP
250
nsSyncLoader::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) {
251
if (NS_SUCCEEDED(mAsyncLoadStatus) && NS_FAILED(aStatusCode)) {
252
mAsyncLoadStatus = aStatusCode;
253
}
254
nsresult rv = mListener->OnStopRequest(aRequest, aStatusCode);
255
if (NS_SUCCEEDED(mAsyncLoadStatus) && NS_FAILED(rv)) {
256
mAsyncLoadStatus = rv;
257
}
258
mLoading = false;
259
260
return rv;
261
}
262
263
NS_IMETHODIMP
264
nsSyncLoader::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
265
nsIChannel* aNewChannel, uint32_t aFlags,
266
nsIAsyncVerifyRedirectCallback* callback) {
267
MOZ_ASSERT(aNewChannel, "Redirecting to null channel?");
268
269
mChannel = aNewChannel;
270
271
callback->OnRedirectVerifyCallback(NS_OK);
272
return NS_OK;
273
}
274
275
NS_IMETHODIMP
276
nsSyncLoader::GetInterface(const nsIID& aIID, void** aResult) {
277
return QueryInterface(aIID, aResult);
278
}
279
280
/* static */
281
nsresult nsSyncLoadService::LoadDocument(
282
nsIURI* aURI, nsContentPolicyType aContentPolicyType,
283
nsIPrincipal* aLoaderPrincipal, nsSecurityFlags aSecurityFlags,
284
nsILoadGroup* aLoadGroup, nsICookieSettings* aCookieSettings,
285
bool aForceToXML, ReferrerPolicy aReferrerPolicy, Document** aResult) {
286
nsCOMPtr<nsIChannel> channel;
287
nsresult rv =
288
NS_NewChannel(getter_AddRefs(channel), aURI, aLoaderPrincipal,
289
aSecurityFlags, aContentPolicyType, aCookieSettings,
290
nullptr, // PerformanceStorage
291
aLoadGroup);
292
NS_ENSURE_SUCCESS(rv, rv);
293
294
if (!aForceToXML) {
295
channel->SetContentType(NS_LITERAL_CSTRING("text/xml"));
296
}
297
298
// if the load needs to enforce CORS, then force the load to be async
299
bool isSync =
300
!(aSecurityFlags & nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) &&
301
(aURI->SchemeIs("chrome") || aURI->SchemeIs("resource"));
302
RefPtr<nsSyncLoader> loader = new nsSyncLoader();
303
return loader->LoadDocument(channel, isSync, aForceToXML, aReferrerPolicy,
304
aResult);
305
}
306
307
/* static */
308
nsresult nsSyncLoadService::PushSyncStreamToListener(
309
already_AddRefed<nsIInputStream> aIn, nsIStreamListener* aListener,
310
nsIChannel* aChannel) {
311
nsCOMPtr<nsIInputStream> in = std::move(aIn);
312
313
// Set up buffering stream
314
nsresult rv;
315
nsCOMPtr<nsIInputStream> bufferedStream;
316
if (!NS_InputStreamIsBuffered(in)) {
317
int64_t chunkSize;
318
rv = aChannel->GetContentLength(&chunkSize);
319
if (NS_FAILED(rv) || chunkSize < 1) {
320
chunkSize = 4096;
321
}
322
chunkSize = std::min(int64_t(UINT16_MAX), chunkSize);
323
324
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), in.forget(),
325
chunkSize);
326
NS_ENSURE_SUCCESS(rv, rv);
327
328
in = bufferedStream;
329
}
330
331
// Load
332
rv = aListener->OnStartRequest(aChannel);
333
if (NS_SUCCEEDED(rv)) {
334
uint64_t sourceOffset = 0;
335
while (1) {
336
uint64_t readCount = 0;
337
rv = in->Available(&readCount);
338
if (NS_FAILED(rv) || !readCount) {
339
if (rv == NS_BASE_STREAM_CLOSED) {
340
// End of file, but not an error
341
rv = NS_OK;
342
}
343
break;
344
}
345
346
if (readCount > UINT32_MAX) readCount = UINT32_MAX;
347
348
rv = aListener->OnDataAvailable(
349
aChannel, in, (uint32_t)std::min(sourceOffset, (uint64_t)UINT32_MAX),
350
(uint32_t)readCount);
351
if (NS_FAILED(rv)) {
352
break;
353
}
354
sourceOffset += readCount;
355
}
356
}
357
if (NS_FAILED(rv)) {
358
aChannel->Cancel(rv);
359
}
360
aListener->OnStopRequest(aChannel, rv);
361
362
return rv;
363
}