import changes from `dev' branch of rmottola/Arctic-Fox:

- Bug 1154563 - Drop the unused argument of WorkerPrivate::Close(); r=baku (366f353f9f)
- Bug 1178721 - Implement SuspendWorkersForWindow;r=khuey (a49bc9a306)
- Bug 1207490 - Part 6: Remove use of expression closure from browser/devtools/. r=vporof (1c4d153319)
- Bug 1198982: Don't fail the SW load for an importScripts failure. r=bkelly (948f5dca94)
- Bug 1160890 - Part 1: Remove unneeded code from ImportScripts(). r=smaug (e31f7c82eb)
- Bug 1160890 - Part 2: ImportScripts() should return muted errors with 3rd party scripts. r=smaug (347e54a605)
- Bug 1188141: Make Worker error events not bubble. r=baku (509fd46933)
- Bug 1160890, r=smaug (0ce3ee09d6)
- Bug 1208687: Only discard events from the outermost queue. r=ehsan (bce722c16b)
- Bug 949376 - MessageEvent::initMessageEvent, r=smaug (8878e51c91)
- Bug 1214772 - Part 2: Make FetchEvent inherit from ExtendableEvent; r=bzbarsky (edff91f7fb)
- Bug 1218131 - Mark FetchEvent.request as SameObject; r=bzbarsky (1f79b94838)
- Bug 1188545 - Add tests for service workers' lifetime management. r=nsm (9bac3b9f2d)
- Bug 1218135 - Remove FetchEvent.client; r=bzbarsky (e95e4e5326)
- Bug 1218151 - Make FetchEventInit.isReload default to false; r=bzbarsky (795d597a3b)
- Bug 1218621 - Keep the service worker alive while the promise passed to FetchEvent.respondWith() settles; r=catalinb (653633c136)
- Bug 1212636 - Add a better error message for the content corrupcted error caused by the Promise being passed to FetchEvent.respondWith; r=bkelly (d6ebabc2f2)
- Bug 1215140 P4 Make service worker respondWith() use channel ConsoleReportCollector. r=bz (0e07f364d4)
- Bug 1218499 - Make FetchEvent.request nullable; r=bzbarsky (850630ea6d)
- Bug 1179397 - Disallow FetchEvent.respondWith() when the dispatch flag is unset; r=jdm (2e57abf1c6)
- Bug 1215140 P5 Report the line number where respondWith() was called. r=bz (484e385ce3)
- Bug 1161239 - Emit a warning if the respondWith handler is resolved with a non-Object value; r=baku (cf2779f827)
- Bug 1181054 - Part 1: Move FormFillIterator and FormDataParser to FetchUtil.cpp; r=bkelly (9f62174f72)
- Bug 1181054 - Part 2: Refactor the code to extract an HTTP header from a buffer from FormDataParser; r=bkelly (7f57f76ce1)
- Bug 1181054 - Part 3: Correctly handle upload channels that have embedded body headers when dispatching a FetchEvent; r=bkelly (ebbfac4419)
-  ug 1181054 - Part 4: Make fetch-event.https.html pass; r=bkelly (b2d88a3f5b)
- Bug 1215140 P6 Update service worker interception error strings to include detailed parameters. r=bz (7918278f09)
- Bug 1219852 P1 Extract common JS values for rejected respondWith() promises. r=bz (12a6beed6c)
- Bug 1219852 P2 Report non-response values passed to FetchEvent.respondWith(). r=bz (69fdad9d5f)
- Bug 1207068 - Implement ExtendableMessageEvent interface. r=baku (29fd7c1c59)
- Bug 1224061: Make Event::InitEvent infallible. r=smaug Bug 1224061 followup to fix bustage. r=me on a CLOSED TREE IGNORE IDL (b4fc91b14b)
- Bug 1205109 - Make pushsubscriptionchange extendable. r=mt (abb45ac864)
- Bug 1207491 - Part 8: Remove use of expression closure from browser/omponents/nsBrowserContentHandler.js. r=Gijs (98dcb2cbee)
- Bug 1182571: Followup bustage fix from merge fail. CLOSED TREE (5062c88996)
- Bug 1213646: Allow URI_IS_UI_RESOURCE and safe about: URIs when SEC_ALLOW_CHROME is set. r=bz (6ca4e2322f)
- Bug 1191645 - Use channel->asycnOpen2 in dom/base/nsSyncLoadService.cpp. r=sicking (3fbd471f6b)
- Bug 1194526 - Use channel->asycnOpen2 in dom/base/nsScriptLoader.cpp (r=sicking) (7207efa45b)
- Bug 1084009 - Part 1/3 - Parse sync scripts off the main thread. r=smaug (72f4d5c749)
- Bug 1084009 - Part 2/3 - Only parse scripts off-main-thread on multicore systems. r=luke (ffb7e2270e)
- Bug 1209193 - Cache PR_GetNumberOfProcessors when checking to do off-main-thread script compilation. r=luke (c514373ad7)
- Bug 663570 - MetaCSP Part 7: CSP preload validation (r=bz) (5398116f85)
- Bug 1207863 - Fix ScopeIter iterating a strict eval frame that errored out before its CallObject was allocated. (r=jorendorff) (ae38882b7c)
- Bug 1223006 - Fix some typo in spidermonkey's comments. r=nbp (7d49536a0f)
- Bug 1223490 - Use stable hashing for InnerViewTable; r=jonco (12b4329982)
This commit is contained in:
2022-11-30 11:46:18 +08:00
parent 4f15bc133e
commit fb9b106168
162 changed files with 2859 additions and 1208 deletions
+136 -52
View File
@@ -10,6 +10,7 @@
#include "nsScriptLoader.h"
#include "prsystem.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "xpcpublic.h"
@@ -113,6 +114,7 @@ nsScriptLoadRequestList::Contains(nsScriptLoadRequest* aElem)
nsScriptLoader::nsScriptLoader(nsIDocument *aDocument)
: mDocument(aDocument),
mBlockerCount(0),
mNumberOfProcessors(0),
mEnabled(true),
mDeferEnabled(false),
mDocumentParsingDone(false),
@@ -273,30 +275,41 @@ nsScriptLoader::ShouldLoadScript(nsIDocument* aDocument,
return NS_OK;
}
class ContextMediator : public nsIStreamLoaderObserver
{
public:
explicit ContextMediator(nsScriptLoader *aScriptLoader, nsISupports *aContext)
: mScriptLoader(aScriptLoader)
, mContext(aContext) {}
NS_DECL_ISUPPORTS
NS_DECL_NSISTREAMLOADEROBSERVER
private:
virtual ~ContextMediator() {}
RefPtr<nsScriptLoader> mScriptLoader;
nsCOMPtr<nsISupports> mContext;
};
NS_IMPL_ISUPPORTS(ContextMediator, nsIStreamLoaderObserver)
NS_IMETHODIMP
ContextMediator::OnStreamComplete(nsIStreamLoader* aLoader,
nsISupports* aContext,
nsresult aStatus,
uint32_t aStringLen,
const uint8_t* aString)
{
// pass arguments through except for the aContext,
// we have to mediate and use mContext instead.
return mScriptLoader->OnStreamComplete(aLoader, mContext, aStatus,
aStringLen, aString);
}
nsresult
nsScriptLoader::StartLoad(nsScriptLoadRequest *aRequest, const nsAString &aType,
bool aScriptFromHead)
{
nsISupports *context = aRequest->mElement.get()
? static_cast<nsISupports *>(aRequest->mElement.get())
: static_cast<nsISupports *>(mDocument);
nsresult rv = ShouldLoadScript(mDocument, context, aRequest->mURI, aType, aRequest->IsPreload());
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsILoadGroup> loadGroup = mDocument->GetDocumentLoadGroup();
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mDocument->MasterDocument()->GetWindow()));
if (!window) {
return NS_ERROR_NULL_POINTER;
}
nsIDocShell *docshell = window->GetDocShell();
nsCOMPtr<nsIInterfaceRequestor> prompter(do_QueryInterface(docshell));
// If this document is sandboxed without 'allow-scripts', abort.
if (mDocument->HasScriptsBlockedBySandbox()) {
return NS_OK;
@@ -305,17 +318,39 @@ nsScriptLoader::StartLoad(nsScriptLoadRequest *aRequest, const nsAString &aType,
nsContentPolicyType contentPolicyType = aRequest->IsPreload()
? nsIContentPolicy::TYPE_INTERNAL_SCRIPT_PRELOAD
: nsIContentPolicy::TYPE_INTERNAL_SCRIPT;
nsCOMPtr<nsINode> context;
if (aRequest->mElement) {
context = do_QueryInterface(aRequest->mElement);
}
else {
context = mDocument;
}
nsCOMPtr<nsILoadGroup> loadGroup = mDocument->GetDocumentLoadGroup();
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mDocument->MasterDocument()->GetWindow()));
NS_ENSURE_TRUE(window, NS_ERROR_NULL_POINTER);
nsIDocShell *docshell = window->GetDocShell();
nsCOMPtr<nsIInterfaceRequestor> prompter(do_QueryInterface(docshell));
nsSecurityFlags securityFlags =
aRequest->mCORSMode == CORS_NONE
? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL
: nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
if (aRequest->mCORSMode == CORS_USE_CREDENTIALS) {
securityFlags |= nsILoadInfo::SEC_REQUIRE_CORS_WITH_CREDENTIALS;
}
securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel),
aRequest->mURI,
mDocument,
nsILoadInfo::SEC_NORMAL,
contentPolicyType,
loadGroup,
prompter,
nsIRequest::LOAD_NORMAL |
nsIChannel::LOAD_CLASSIFY_URI);
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
aRequest->mURI,
context,
securityFlags,
contentPolicyType,
loadGroup,
prompter,
nsIRequest::LOAD_NORMAL |
nsIChannel::LOAD_CLASSIFY_URI);
NS_ENSURE_SUCCESS(rv, rv);
@@ -354,26 +389,13 @@ nsScriptLoader::StartLoad(nsScriptLoadRequest *aRequest, const nsAString &aType,
timedChannel->SetInitiatorType(NS_LITERAL_STRING("script"));
}
RefPtr<ContextMediator> mediator = new ContextMediator(this, aRequest);
nsCOMPtr<nsIStreamLoader> loader;
rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
rv = NS_NewStreamLoader(getter_AddRefs(loader), mediator);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStreamListener> listener = loader.get();
if (aRequest->mCORSMode != CORS_NONE) {
bool withCredentials = (aRequest->mCORSMode == CORS_USE_CREDENTIALS);
RefPtr<nsCORSListenerProxy> corsListener =
new nsCORSListenerProxy(listener, mDocument->NodePrincipal(),
withCredentials);
rv = corsListener->Init(channel, DataURIHandling::Allow);
NS_ENSURE_SUCCESS(rv, rv);
listener = corsListener;
}
rv = channel->AsyncOpen(listener, aRequest);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
return channel->AsyncOpen2(loader);
}
bool
@@ -561,7 +583,12 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
ourCORSMode == request->mCORSMode &&
ourRefPolicy == request->mReferrerPolicy) {
rv = CheckContentPolicy(mDocument, aElement, request->mURI, type, false);
NS_ENSURE_SUCCESS(rv, false);
if (NS_FAILED(rv)) {
// probably plans have changed; even though the preload was allowed seems
// like the actual load is not; let's cancel the preload request.
request->Cancel();
return false;
}
} else {
// Drop the preload
request = nullptr;
@@ -617,7 +644,7 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
// loop gets a chance to spin.
// KVKV TODO: Instead of processing immediately, try off-thread-parsing
// it and only schedule a ProcessRequest if that fails.
// it and only schedule a pending ProcessRequest if that fails.
ProcessPendingRequestsAsync();
} else {
mLoadingAsyncRequests.AppendElement(request);
@@ -666,6 +693,7 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
}
return true;
}
if (request->IsDoneLoading() && ReadyToExecuteScripts()) {
// The request has already been loaded and there are no pending style
// sheets. If the script comes from the network stream, cheat for
@@ -684,6 +712,7 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement)
ProcessPendingRequestsAsync();
return true;
}
// The script hasn't loaded yet or there's a style sheet blocking it.
// The script will be run when it loads or the style sheet loads.
NS_ASSERTION(!mParserBlockingRequest,
@@ -782,6 +811,23 @@ nsScriptLoader::ProcessOffThreadRequest(nsScriptLoadRequest* aRequest)
{
MOZ_ASSERT(aRequest->mProgress == nsScriptLoadRequest::Progress_Compiling);
aRequest->mProgress = nsScriptLoadRequest::Progress_DoneCompiling;
if (aRequest == mParserBlockingRequest) {
if (!ReadyToExecuteScripts()) {
// If not ready to execute scripts, schedule an async call to
// ProcessPendingRequests to handle it.
ProcessPendingRequestsAsync();
return NS_OK;
}
// Same logic as in top of ProcessPendingRequests.
mParserBlockingRequest = nullptr;
UnblockParser(aRequest);
ProcessRequest(aRequest);
mDocument->UnblockOnload(false);
ContinueParserAsync(aRequest);
return NS_OK;
}
nsresult rv = ProcessRequest(aRequest);
mDocument->UnblockOnload(false);
return rv;
@@ -830,9 +876,10 @@ OffThreadScriptLoaderCallback(void *aToken, void *aCallbackData)
}
nsresult
nsScriptLoader::AttemptAsyncScriptParse(nsScriptLoadRequest* aRequest)
nsScriptLoader::AttemptAsyncScriptCompile(nsScriptLoadRequest* aRequest)
{
if (!aRequest->mElement->GetScriptAsync() || aRequest->mIsInline) {
// Don't off-thread compile inline scripts.
if (aRequest->mIsInline) {
return NS_ERROR_FAILURE;
}
@@ -873,7 +920,8 @@ nsScriptLoader::AttemptAsyncScriptParse(nsScriptLoadRequest* aRequest)
}
nsresult
nsScriptLoader::CompileOffThreadOrProcessRequest(nsScriptLoadRequest* aRequest)
nsScriptLoader::CompileOffThreadOrProcessRequest(nsScriptLoadRequest* aRequest,
bool* oCompiledOffThread)
{
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(),
"Processing requests when running scripts is unsafe.");
@@ -882,8 +930,11 @@ nsScriptLoader::CompileOffThreadOrProcessRequest(nsScriptLoadRequest* aRequest)
NS_ASSERTION(!aRequest->InCompilingStage(),
"Candidate for off-thread compile is already in compiling stage.");
nsresult rv = AttemptAsyncScriptParse(aRequest);
nsresult rv = AttemptAsyncScriptCompile(aRequest);
if (rv != NS_ERROR_FAILURE) {
if (oCompiledOffThread && rv == NS_OK) {
*oCompiledOffThread = true;
}
return rv;
}
@@ -1174,8 +1225,12 @@ nsScriptLoader::ProcessPendingRequests()
mParserBlockingRequest->IsReadyToRun() &&
ReadyToExecuteScripts()) {
request.swap(mParserBlockingRequest);
bool offThreadCompiled = request->mProgress == nsScriptLoadRequest::Progress_DoneCompiling;
UnblockParser(request);
ProcessRequest(request);
if (offThreadCompiled) {
mDocument->UnblockOnload(false);
}
ContinueParserAsync(request);
}
@@ -1487,6 +1542,18 @@ nsScriptLoader::ContinueParserAsync(nsScriptLoadRequest* aParserBlockingRequest)
aParserBlockingRequest->mElement->ContinueParserAsync();
}
uint32_t
nsScriptLoader::NumberOfProcessors()
{
if (mNumberOfProcessors > 0)
return mNumberOfProcessors;
int32_t numProcs = PR_GetNumberOfProcessors();
if (numProcs > 0)
mNumberOfProcessors = numProcs;
return mNumberOfProcessors;
}
nsresult
nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
nsIStreamLoader* aLoader,
@@ -1575,6 +1642,23 @@ nsScriptLoader::PrepareLoadedRequest(nsScriptLoadRequest* aRequest,
// Mark this as loaded
aRequest->mProgress = nsScriptLoadRequest::Progress_DoneLoading;
// If this is currently blocking the parser, attempt to compile it off-main-thread.
if (aRequest == mParserBlockingRequest && (NumberOfProcessors() > 1)) {
nsresult rv = AttemptAsyncScriptCompile(aRequest);
if (rv == NS_OK) {
NS_ASSERTION(aRequest->mProgress == nsScriptLoadRequest::Progress_Compiling,
"Request should be off-thread compiling now.");
return NS_OK;
}
// If off-thread compile errored, return the error.
if (rv != NS_ERROR_FAILURE) {
return rv;
}
// If off-thread compile was rejected, continue with regular processing.
}
// And if it's async, move it to the loaded list. aRequest->mIsAsync really
// _should_ be in a list, but the consequences if it's not are bad enough we
// want to avoid trying to move it if it's not.