1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/css/StreamLoader.h"
#include "mozilla/StaticPrefs_network.h"
#include "mozilla/Encoding.h"
#include "mozilla/glean/NetwerkMetrics.h"
#include "mozilla/TaskQueue.h"
#include "mozilla/net/UrlClassifierFeatureFactory.h"
#include "mozilla/dom/CacheExpirationTime.h"
#include "nsContentUtils.h"
#include "nsIAsyncVerifyRedirectCallback.h"
#include "nsIChannel.h"
#include "nsIInputStream.h"
#include "nsIThreadRetargetableRequest.h"
#include "nsIStreamTransportService.h"
#include "nsNetCID.h"
#include "nsNetUtil.h"
#include "nsProxyRelease.h"
#include "nsServiceManagerUtils.h"
namespace mozilla::css {
StreamLoader::StreamLoader(SheetLoadData& aSheetLoadData)
: mSheetLoadData(&aSheetLoadData),
mStatus(NS_OK),
mMainThreadSheetLoadData(new nsMainThreadPtrHolder<SheetLoadData>(
"StreamLoader::SheetLoadData", mSheetLoadData, false)) {}
StreamLoader::~StreamLoader() {
#ifdef NIGHTLY_BUILD
MOZ_RELEASE_ASSERT(mOnStopProcessingDone || mChannelOpenFailed);
#endif
}
NS_IMPL_ISUPPORTS(StreamLoader, nsIStreamListener,
nsIThreadRetargetableStreamListener, nsIChannelEventSink,
nsIInterfaceRequestor)
/* nsIRequestObserver implementation */
NS_IMETHODIMP
StreamLoader::OnStartRequest(nsIRequest* aRequest) {
MOZ_ASSERT(aRequest);
mRequest = aRequest;
mSheetLoadData->OnStartRequest(aRequest);
// It's kinda bad to let Web content send a number that results
// in a potentially large allocation directly, but efficiency of
// compression bombs is so great that it doesn't make much sense
// to require a site to send one before going ahead and allocating.
if (nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest)) {
int64_t length;
nsresult rv = channel->GetContentLength(&length);
if (NS_SUCCEEDED(rv) && length > 0) {
CheckedInt<nsACString::size_type> checkedLength(length);
if (!checkedLength.isValid()) {
return (mStatus = NS_ERROR_OUT_OF_MEMORY);
}
if (!mBytes.SetCapacity(checkedLength.value(), fallible)) {
return (mStatus = NS_ERROR_OUT_OF_MEMORY);
}
}
}
if (nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(aRequest)) {
nsCOMPtr<nsIEventTarget> sts =
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
RefPtr queue =
TaskQueue::Create(sts.forget(), "css::StreamLoader Delivery Queue");
rr->RetargetDeliveryTo(queue);
}
return NS_OK;
}
NS_IMETHODIMP
StreamLoader::CheckListenerChain() { return NS_OK; }
NS_IMETHODIMP
StreamLoader::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
MOZ_ASSERT_IF(!StaticPrefs::network_send_OnDataFinished_cssLoader(),
!mOnStopProcessingDone);
mRequest = nullptr;
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
// StreamLoader::OnStopRequest can get triggered twice for a request.
// Once from the path
// nsIThreadRetargetableStreamListener::OnDataFinished->StreamLoader::OnDataFinished
// (non-main thread) and
// once from nsIRequestObserver::OnStopRequest path (main thread). It is
// guaranteed that we will always get
// nsIThreadRetargetableStreamListener::OnDataFinished trigger first and this
// is always followed by nsIRequestObserver::OnStopRequest
// If we are executing OnStopRequest OMT, we need to block resolution of parse
// promise and unblock again if we are executing this in main thread.
// Resolution of parse promise fires onLoadEvent and this should not happen
// before main thread OnStopRequest is dispatched.
if (NS_IsMainThread()) {
channel->SetNotificationCallbacks(nullptr);
mSheetLoadData->mNetworkMetadata =
new SubResourceNetworkMetadataHolder(aRequest);
if (mOnDataFinishedTime) {
// collect telemetry for the delta between OnDataFinished and
// OnStopRequest
TimeDuration delta = (TimeStamp::Now() - mOnDataFinishedTime);
glean::networking::http_content_cssloader_ondatafinished_to_onstop_delay
.AccumulateRawDuration(delta);
}
mSheetLoadData->mSheet->UnblockParsePromise();
} else {
if (mSheetLoadData->mRecordErrors) {
// We can't report errors off main thread right now.
return NS_OK;
}
}
auto HandleErrorInMainThread = [&] {
MOZ_ASSERT(mStatus != NS_OK_PARSE_SHEET);
MOZ_ASSERT(NS_IsMainThread());
if (net::UrlClassifierFeatureFactory::IsClassifierBlockingErrorCode(
mStatus)) {
// Handle sheet not loading error because source was a tracking URL (or
// fingerprinting, cryptomining, etc). We make a note of this sheet node
// by including it in a dedicated array of blocked tracking nodes under
// its parent document.
//
// Multiple sheet load instances might be tied to this request, we
// annotate each one linked to a valid owning element (node).
//
// TODO(emilio): Maybe this should be done in Loader::NotifyObservers?
// Feels pretty random here.
for (SheetLoadData* data = mSheetLoadData; data; data = data->mNext) {
if (nsINode* node = data->mSheet->GetOwnerNode()) {
node->OwnerDoc()->AddBlockedNodeByClassifier(node);
}
}
}
mSheetLoadData->mLoader->SheetComplete(*mSheetLoadData, mStatus);
};
if (mOnStopProcessingDone) {
MOZ_ASSERT(NS_IsMainThread());
if (mStatus != NS_OK_PARSE_SHEET) {
HandleErrorInMainThread();
}
return NS_OK;
}
mOnStopProcessingDone = true;
// Decoded data
nsCString utf8String;
{
nsresult status = NS_FAILED(mStatus) ? mStatus : aStatus;
mStatus = mSheetLoadData->VerifySheetReadyToParse(status, mBOMBytes, mBytes,
channel);
if (mStatus != NS_OK_PARSE_SHEET) {
if (NS_IsMainThread()) {
HandleErrorInMainThread();
}
return mStatus;
}
// At this point all the conditions that requires us to run on main
// are checked in VerifySheetReadyToParse
// BOM detection generally happens during the write callback, but that
// won't have happened if fewer than three bytes were received.
if (mEncodingFromBOM.isNothing()) {
HandleBOM();
MOZ_ASSERT(mEncodingFromBOM.isSome());
}
// Hold the nsStringBuffer for the bytes from the stack to ensure release
// after its scope ends
nsCString bytes = std::move(mBytes);
// The BOM handling has happened, but we still may not have an encoding if
// there was no BOM. Ensure we have one.
const Encoding* encoding = mEncodingFromBOM.value();
if (!encoding) {
// No BOM
encoding = mSheetLoadData->DetermineNonBOMEncoding(bytes, channel);
}
mSheetLoadData->mEncoding = encoding;
size_t validated = 0;
if (encoding == UTF_8_ENCODING) {
validated = Encoding::UTF8ValidUpTo(bytes);
}
if (validated == bytes.Length()) {
// Either this is UTF-8 and all valid, or it's not UTF-8 but is an empty
// string. This assumes that an empty string in any encoding decodes to
// empty string, which seems like a plausible assumption.
utf8String = std::move(bytes);
} else {
// FIXME: Seems early returning here is wrong, what completes the sheet?
MOZ_TRY(encoding->DecodeWithoutBOMHandling(bytes, utf8String, validated));
}
} // run destructor for `bytes`
// For reasons I don't understand, factoring the below lines into
// a method on SheetLoadData resulted in a linker error. Hence,
// accessing fields of mSheetLoadData from here.
mSheetLoadData->mLoader->ParseSheet(utf8String, mMainThreadSheetLoadData,
Loader::AllowAsyncParse::Yes);
return NS_OK;
}
/* nsIStreamListener implementation */
NS_IMETHODIMP
StreamLoader::OnDataAvailable(nsIRequest*, nsIInputStream* aInputStream,
uint64_t, uint32_t aCount) {
if (NS_FAILED(mStatus)) {
return mStatus;
}
uint32_t dummy;
return aInputStream->ReadSegments(WriteSegmentFun, this, aCount, &dummy);
}
void StreamLoader::HandleBOM() {
MOZ_ASSERT(mEncodingFromBOM.isNothing());
MOZ_ASSERT(mBytes.IsEmpty());
auto [encoding, bomLength] = Encoding::ForBOM(mBOMBytes);
mEncodingFromBOM.emplace(encoding); // Null means no BOM.
// BOMs are three bytes at most, but may be fewer. Copy over anything
// that wasn't part of the BOM to mBytes. Note that we need to track
// any BOM bytes as well for SRI handling.
mBytes.Append(Substring(mBOMBytes, bomLength));
mBOMBytes.Truncate(bomLength);
}
NS_IMETHODIMP
StreamLoader::OnDataFinished(nsresult aResult) {
nsCOMPtr<nsIRequest> request = mRequest.forget();
if (StaticPrefs::network_send_OnDataFinished_cssLoader()) {
MOZ_ASSERT(mOnDataFinishedTime.IsNull(),
"OnDataFinished should only be called once");
mOnDataFinishedTime = TimeStamp::Now();
return OnStopRequest(request, aResult);
}
return NS_OK;
}
NS_IMETHODIMP
StreamLoader::GetInterface(const nsIID& aIID, void** aResult) {
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
return QueryInterface(aIID, aResult);
}
return NS_NOINTERFACE;
}
nsresult StreamLoader::AsyncOnChannelRedirect(
nsIChannel* aOld, nsIChannel* aNew, uint32_t aFlags,
nsIAsyncVerifyRedirectCallback* aCallback) {
mSheetLoadData->SetMinimumExpirationTime(
nsContentUtils::GetSubresourceCacheExpirationTime(aOld,
mSheetLoadData->mURI));
aCallback->OnRedirectVerifyCallback(NS_OK);
return NS_OK;
}
nsresult StreamLoader::WriteSegmentFun(nsIInputStream*, void* aClosure,
const char* aSegment, uint32_t,
uint32_t aCount, uint32_t* aWriteCount) {
*aWriteCount = 0;
StreamLoader* self = static_cast<StreamLoader*>(aClosure);
if (NS_FAILED(self->mStatus)) {
return self->mStatus;
}
// If we haven't done BOM detection yet, divert bytes into the special buffer.
if (self->mEncodingFromBOM.isNothing()) {
size_t bytesToCopy = std::min<size_t>(3 - self->mBOMBytes.Length(), aCount);
self->mBOMBytes.Append(aSegment, bytesToCopy);
aSegment += bytesToCopy;
*aWriteCount += bytesToCopy;
aCount -= bytesToCopy;
if (self->mBOMBytes.Length() == 3) {
self->HandleBOM();
} else {
return NS_OK;
}
}
if (!self->mBytes.Append(aSegment, aCount, fallible)) {
self->mBytes.Truncate();
return (self->mStatus = NS_ERROR_OUT_OF_MEMORY);
}
*aWriteCount += aCount;
return NS_OK;
}
} // namespace mozilla::css
|