1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/css/StreamLoader.h"
8 #include "mozilla/StaticPrefs_network.h"
9 #include "mozilla/Encoding.h"
10 #include "mozilla/glean/GleanMetrics.h"
11 #include "mozilla/TaskQueue.h"
12 #include "nsContentUtils.h"
13 #include "nsIChannel.h"
14 #include "nsIInputStream.h"
15 #include "nsIThreadRetargetableRequest.h"
16 #include "nsIStreamTransportService.h"
18 #include "nsNetUtil.h"
19 #include "nsProxyRelease.h"
20 #include "nsServiceManagerUtils.h"
22 namespace mozilla::css
{
24 StreamLoader::StreamLoader(SheetLoadData
& aSheetLoadData
)
25 : mSheetLoadData(&aSheetLoadData
),
27 mMainThreadSheetLoadData(new nsMainThreadPtrHolder
<SheetLoadData
>(
28 "StreamLoader::SheetLoadData", mSheetLoadData
, false)) {}
30 StreamLoader::~StreamLoader() {
32 MOZ_RELEASE_ASSERT(mOnStopProcessingDone
|| mChannelOpenFailed
);
36 NS_IMPL_ISUPPORTS(StreamLoader
, nsIStreamListener
,
37 nsIThreadRetargetableStreamListener
)
39 /* nsIRequestObserver implementation */
41 StreamLoader::OnStartRequest(nsIRequest
* aRequest
) {
44 mSheetLoadData
->NotifyStart(aRequest
);
46 // It's kinda bad to let Web content send a number that results
47 // in a potentially large allocation directly, but efficiency of
48 // compression bombs is so great that it doesn't make much sense
49 // to require a site to send one before going ahead and allocating.
50 if (nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(aRequest
)) {
52 nsresult rv
= channel
->GetContentLength(&length
);
53 if (NS_SUCCEEDED(rv
) && length
> 0) {
54 CheckedInt
<nsACString::size_type
> checkedLength(length
);
55 if (!checkedLength
.isValid()) {
56 return (mStatus
= NS_ERROR_OUT_OF_MEMORY
);
58 if (!mBytes
.SetCapacity(checkedLength
.value(), fallible
)) {
59 return (mStatus
= NS_ERROR_OUT_OF_MEMORY
);
62 NS_GetFinalChannelURI(channel
, getter_AddRefs(mFinalChannelURI
));
63 nsIScriptSecurityManager
* secMan
= nsContentUtils::GetSecurityManager();
64 // we dont return on error here as the error is handled in
65 // SheetLoadData::VerifySheetReadyToParse
66 Unused
<< secMan
->GetChannelResultPrincipal(
67 channel
, getter_AddRefs(mChannelResultPrincipal
));
69 if (nsCOMPtr
<nsIThreadRetargetableRequest
> rr
= do_QueryInterface(aRequest
)) {
70 nsCOMPtr
<nsIEventTarget
> sts
=
71 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID
);
73 TaskQueue::Create(sts
.forget(), "css::StreamLoader Delivery Queue");
74 rr
->RetargetDeliveryTo(queue
);
77 mSheetLoadData
->mExpirationTime
= [&] {
78 auto info
= nsContentUtils::GetSubresourceCacheValidationInfo(
79 aRequest
, mSheetLoadData
->mURI
);
81 // For now, we never cache entries that we have to revalidate, or whose
82 // channel don't support caching.
83 if (info
.mMustRevalidate
|| !info
.mExpirationTime
) {
84 return nsContentUtils::SecondsFromPRTime(PR_Now()) - 1;
86 return *info
.mExpirationTime
;
89 // We need to block block resolution of parse promise until we receive
90 // OnStopRequest on Main thread. This is necessary because parse promise
91 // resolution fires OnLoad event OnLoad event must not be dispatched until
92 // OnStopRequest in main thread is processed, for stuff like performance
94 mSheetLoadData
->mSheet
->BlockParsePromise();
100 StreamLoader::CheckListenerChain() { return NS_OK
; }
103 StreamLoader::OnStopRequest(nsIRequest
* aRequest
, nsresult aStatus
) {
104 MOZ_ASSERT_IF(!StaticPrefs::network_send_OnDataFinished_cssLoader(),
105 !mOnStopProcessingDone
);
107 // StreamLoader::OnStopRequest can get triggered twice for a request.
108 // Once from the path
109 // nsIThreadRetargetableStreamListener::OnDataFinished->StreamLoader::OnDataFinished
110 // (non-main thread) and
111 // once from nsIRequestObserver::OnStopRequest path (main thread). It is
112 // guaranteed that we will always get
113 // nsIThreadRetargetableStreamListener::OnDataFinished trigger first and this
114 // is always followed by nsIRequestObserver::OnStopRequest
116 // If we are executing OnStopRequest OMT, we need to block resolution of parse
117 // promise and unblock again if we are executing this in main thread.
118 // Resolution of parse promise fires onLoadEvent and this should not happen
119 // before main thread OnStopRequest is dispatched.
120 if (NS_IsMainThread()) {
121 if (mOnDataFinishedTime
) {
122 // collect telemetry for the delta between OnDataFinished and
124 TimeDuration delta
= (TimeStamp::Now() - mOnDataFinishedTime
);
125 glean::networking::http_content_cssloader_ondatafinished_to_onstop_delay
126 .AccumulateRawDuration(delta
);
128 mSheetLoadData
->mSheet
->UnblockParsePromise();
131 if (mOnStopProcessingDone
) {
134 mOnStopProcessingDone
= true;
136 nsresult rv
= mStatus
;
138 nsCString utf8String
;
140 nsCOMPtr
<nsIChannel
> channel
= do_QueryInterface(aRequest
);
142 if (NS_FAILED(mStatus
)) {
143 mSheetLoadData
->VerifySheetReadyToParse(mStatus
, ""_ns
, ""_ns
, channel
,
145 mChannelResultPrincipal
);
147 if (!NS_IsMainThread()) {
148 // When processing OMT, we have code paths in VerifySheetReadyToParse
149 // that are main-thread only. We bail on such scenarios and continue
150 // processing them on main thread OnStopRequest.
151 mOnStopProcessingDone
= false;
156 rv
= mSheetLoadData
->VerifySheetReadyToParse(aStatus
, mBOMBytes
, mBytes
,
157 channel
, mFinalChannelURI
,
158 mChannelResultPrincipal
);
159 if (rv
!= NS_OK_PARSE_SHEET
) {
160 if (!NS_IsMainThread()) {
161 mOnStopProcessingDone
= false;
166 // At this point all the conditions that requires us to run on main
167 // are checked in VerifySheetReadyToParse
169 // BOM detection generally happens during the write callback, but that
170 // won't have happened if fewer than three bytes were received.
171 if (mEncodingFromBOM
.isNothing()) {
173 MOZ_ASSERT(mEncodingFromBOM
.isSome());
175 // Hold the nsStringBuffer for the bytes from the stack to ensure release
176 // after its scope ends
177 nsCString bytes
= std::move(mBytes
);
178 // The BOM handling has happened, but we still may not have an encoding if
179 // there was no BOM. Ensure we have one.
180 const Encoding
* encoding
= mEncodingFromBOM
.value();
183 encoding
= mSheetLoadData
->DetermineNonBOMEncoding(bytes
, channel
);
185 mSheetLoadData
->mEncoding
= encoding
;
187 size_t validated
= 0;
188 if (encoding
== UTF_8_ENCODING
) {
189 validated
= Encoding::UTF8ValidUpTo(bytes
);
192 if (validated
== bytes
.Length()) {
193 // Either this is UTF-8 and all valid, or it's not UTF-8 but is an empty
194 // string. This assumes that an empty string in any encoding decodes to
195 // empty string, which seems like a plausible assumption.
196 utf8String
= std::move(bytes
);
198 rv
= encoding
->DecodeWithoutBOMHandling(bytes
, utf8String
, validated
);
199 NS_ENSURE_SUCCESS(rv
, rv
);
201 } // run destructor for `bytes`
203 // For reasons I don't understand, factoring the below lines into
204 // a method on SheetLoadData resulted in a linker error. Hence,
205 // accessing fields of mSheetLoadData from here.
206 mSheetLoadData
->mLoader
->ParseSheet(utf8String
, mMainThreadSheetLoadData
,
207 Loader::AllowAsyncParse::Yes
);
214 /* nsIStreamListener implementation */
216 StreamLoader::OnDataAvailable(nsIRequest
*, nsIInputStream
* aInputStream
,
217 uint64_t, uint32_t aCount
) {
218 if (NS_FAILED(mStatus
)) {
222 return aInputStream
->ReadSegments(WriteSegmentFun
, this, aCount
, &dummy
);
225 void StreamLoader::HandleBOM() {
226 MOZ_ASSERT(mEncodingFromBOM
.isNothing());
227 MOZ_ASSERT(mBytes
.IsEmpty());
229 auto [encoding
, bomLength
] = Encoding::ForBOM(mBOMBytes
);
230 mEncodingFromBOM
.emplace(encoding
); // Null means no BOM.
232 // BOMs are three bytes at most, but may be fewer. Copy over anything
233 // that wasn't part of the BOM to mBytes. Note that we need to track
234 // any BOM bytes as well for SRI handling.
235 mBytes
.Append(Substring(mBOMBytes
, bomLength
));
236 mBOMBytes
.Truncate(bomLength
);
240 StreamLoader::OnDataFinished(nsresult aResult
) {
241 if (StaticPrefs::network_send_OnDataFinished_cssLoader()) {
242 MOZ_ASSERT(mOnDataFinishedTime
.IsNull(),
243 "OnDataFinished should only be called once");
244 mOnDataFinishedTime
= TimeStamp::Now();
245 return OnStopRequest(mRequest
, aResult
);
251 nsresult
StreamLoader::WriteSegmentFun(nsIInputStream
*, void* aClosure
,
252 const char* aSegment
, uint32_t,
253 uint32_t aCount
, uint32_t* aWriteCount
) {
255 StreamLoader
* self
= static_cast<StreamLoader
*>(aClosure
);
256 if (NS_FAILED(self
->mStatus
)) {
257 return self
->mStatus
;
260 // If we haven't done BOM detection yet, divert bytes into the special buffer.
261 if (self
->mEncodingFromBOM
.isNothing()) {
262 size_t bytesToCopy
= std::min
<size_t>(3 - self
->mBOMBytes
.Length(), aCount
);
263 self
->mBOMBytes
.Append(aSegment
, bytesToCopy
);
264 aSegment
+= bytesToCopy
;
265 *aWriteCount
+= bytesToCopy
;
266 aCount
-= bytesToCopy
;
268 if (self
->mBOMBytes
.Length() == 3) {
275 if (!self
->mBytes
.Append(aSegment
, aCount
, fallible
)) {
276 self
->mBytes
.Truncate();
277 return (self
->mStatus
= NS_ERROR_OUT_OF_MEMORY
);
280 *aWriteCount
+= aCount
;
284 } // namespace mozilla::css