Skip to content

Commit

Permalink
Backed out changeset 80a164c1054e (bug 1730669) for causing bustages …
Browse files Browse the repository at this point in the history
…on css::SheetLoadData
  • Loading branch information
crisscozmuta committed Sep 15, 2021
1 parent 3aeb27e commit 2365892
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 59 deletions.
109 changes: 58 additions & 51 deletions layout/style/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,12 @@ static NotNull<const Encoding*> GetFallbackEncoding(
********************************/
NS_IMPL_ISUPPORTS(SheetLoadData, nsIRunnable, nsIThreadObserver)

SheetLoadData::SheetLoadData(Loader* aLoader, const nsAString& aTitle,
nsIURI* aURI, StyleSheet* aSheet, bool aSyncLoad,
nsINode* aOwningNode, IsAlternate aIsAlternate,
MediaMatched aMediaMatches,
StylePreloadKind aPreloadKind,
nsICSSLoaderObserver* aObserver,
nsIPrincipal* aTriggeringPrincipal,
nsIReferrerInfo* aReferrerInfo)
SheetLoadData::SheetLoadData(
Loader* aLoader, const nsAString& aTitle, nsIURI* aURI, StyleSheet* aSheet,
bool aSyncLoad, nsINode* aOwningNode, IsAlternate aIsAlternate,
MediaMatched aMediaMatches, StylePreloadKind aPreloadKind,
nsICSSLoaderObserver* aObserver, nsIPrincipal* aTriggeringPrincipal,
nsIReferrerInfo* aReferrerInfo, nsINode* aRequestingNode)
: mLoader(aLoader),
mTitle(aTitle),
mEncoding(nullptr),
Expand All @@ -297,6 +295,7 @@ SheetLoadData::SheetLoadData(Loader* aLoader, const nsAString& aTitle,
mLoadFailed(false),
mPreloadKind(aPreloadKind),
mOwningNodeBeforeLoadEvent(aOwningNode),
mRequestingNodeBeforeComplete(aRequestingNode),
mObserver(aObserver),
mTriggeringPrincipal(aTriggeringPrincipal),
mReferrerInfo(aReferrerInfo),
Expand All @@ -313,7 +312,8 @@ SheetLoadData::SheetLoadData(Loader* aLoader, nsIURI* aURI, StyleSheet* aSheet,
SheetLoadData* aParentData,
nsICSSLoaderObserver* aObserver,
nsIPrincipal* aTriggeringPrincipal,
nsIReferrerInfo* aReferrerInfo)
nsIReferrerInfo* aReferrerInfo,
nsINode* aRequestingNode)
: mLoader(aLoader),
mEncoding(nullptr),
mURI(aURI),
Expand All @@ -337,6 +337,7 @@ SheetLoadData::SheetLoadData(Loader* aLoader, nsIURI* aURI, StyleSheet* aSheet,
mBlockResourceTiming(false),
mLoadFailed(false),
mPreloadKind(StylePreloadKind::None),
mRequestingNodeBeforeComplete(aRequestingNode),
mObserver(aObserver),
mTriggeringPrincipal(aTriggeringPrincipal),
mReferrerInfo(aReferrerInfo),
Expand All @@ -354,7 +355,8 @@ SheetLoadData::SheetLoadData(
Loader* aLoader, nsIURI* aURI, StyleSheet* aSheet, bool aSyncLoad,
UseSystemPrincipal aUseSystemPrincipal, StylePreloadKind aPreloadKind,
const Encoding* aPreloadEncoding, nsICSSLoaderObserver* aObserver,
nsIPrincipal* aTriggeringPrincipal, nsIReferrerInfo* aReferrerInfo)
nsIPrincipal* aTriggeringPrincipal, nsIReferrerInfo* aReferrerInfo,
nsINode* aRequestingNode)
: mLoader(aLoader),
mEncoding(nullptr),
mURI(aURI),
Expand All @@ -377,6 +379,7 @@ SheetLoadData::SheetLoadData(
mBlockResourceTiming(false),
mLoadFailed(false),
mPreloadKind(aPreloadKind),
mRequestingNodeBeforeComplete(aRequestingNode),
mObserver(aObserver),
mTriggeringPrincipal(aTriggeringPrincipal),
mReferrerInfo(aReferrerInfo),
Expand Down Expand Up @@ -474,13 +477,6 @@ void SheetLoadData::ScheduleLoadEventIfNeeded() {
}
}

nsINode* SheetLoadData::GetRequestingNode() const {
if (nsINode* node = mSheet->GetOwnerNodeOfOutermostSheet()) {
return node;
}
return mLoader->GetDocument();
}

/*********************
* Style sheet reuse *
*********************/
Expand Down Expand Up @@ -1203,8 +1199,6 @@ nsresult Loader::LoadSheet(SheetLoadData& aLoadData, SheetState aSheetState,
SRIMetadata sriMetadata;
aLoadData.mSheet->GetIntegrity(sriMetadata);

nsINode* requestingNode = aLoadData.GetRequestingNode();

if (aLoadData.mSyncLoad) {
LOG((" Synchronous load"));
MOZ_ASSERT(!aLoadData.mObserver, "Observer for a sync load?");
Expand Down Expand Up @@ -1238,9 +1232,10 @@ nsresult Loader::LoadSheet(SheetLoadData& aLoadData, SheetState aSheetState,
// This is because of a case where the node is the document being styled and
// the principal is the stylesheet (perhaps from a different origin) that is
// applying the styles.
if (requestingNode) {
if (aLoadData.mRequestingNodeBeforeComplete) {
rv = NS_NewChannelWithTriggeringPrincipal(
getter_AddRefs(channel), aLoadData.mURI, requestingNode,
getter_AddRefs(channel), aLoadData.mURI,
aLoadData.mRequestingNodeBeforeComplete,
aLoadData.mTriggeringPrincipal, securityFlags, contentPolicyType);
} else {
MOZ_ASSERT(aLoadData.mTriggeringPrincipal->Equals(LoaderPrincipal()));
Expand Down Expand Up @@ -1268,10 +1263,11 @@ nsresult Loader::LoadSheet(SheetLoadData& aLoadData, SheetState aSheetState,

// snapshot the nonce at load start time for performing CSP checks
if (contentPolicyType == nsIContentPolicy::TYPE_INTERNAL_STYLESHEET) {
if (requestingNode) {
if (aLoadData.mRequestingNodeBeforeComplete) {
// TODO(bug 1607009) move to SheetLoadData
nsString* cspNonce = static_cast<nsString*>(
requestingNode->GetProperty(nsGkAtoms::nonce));
aLoadData.mRequestingNodeBeforeComplete->GetProperty(
nsGkAtoms::nonce));
if (cspNonce) {
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
loadInfo->SetCspNonce(*cspNonce);
Expand Down Expand Up @@ -1375,11 +1371,12 @@ nsresult Loader::LoadSheet(SheetLoadData& aLoadData, SheetState aSheetState,
// and a principal. This is because of a case where the node is the document
// being styled and the principal is the stylesheet (perhaps from a different
// origin) that is applying the styles.
if (requestingNode) {
if (aLoadData.mRequestingNodeBeforeComplete) {
rv = NS_NewChannelWithTriggeringPrincipal(
getter_AddRefs(channel), aLoadData.mURI, requestingNode,
aLoadData.mTriggeringPrincipal, securityFlags, contentPolicyType,
/* PerformanceStorage */ nullptr, loadGroup);
getter_AddRefs(channel), aLoadData.mURI,
aLoadData.mRequestingNodeBeforeComplete, aLoadData.mTriggeringPrincipal,
securityFlags, contentPolicyType, /* PerformanceStorage */ nullptr,
loadGroup);
} else {
MOZ_ASSERT(aLoadData.mTriggeringPrincipal->Equals(LoaderPrincipal()));
rv = NS_NewChannel(getter_AddRefs(channel), aLoadData.mURI,
Expand All @@ -1396,10 +1393,11 @@ nsresult Loader::LoadSheet(SheetLoadData& aLoadData, SheetState aSheetState,

// snapshot the nonce at load start time for performing CSP checks
if (contentPolicyType == nsIContentPolicy::TYPE_INTERNAL_STYLESHEET) {
if (requestingNode) {
if (aLoadData.mRequestingNodeBeforeComplete) {
// TODO(bug 1607009) move to SheetLoadData
nsString* cspNonce =
static_cast<nsString*>(requestingNode->GetProperty(nsGkAtoms::nonce));
nsString* cspNonce = static_cast<nsString*>(
aLoadData.mRequestingNodeBeforeComplete->GetProperty(
nsGkAtoms::nonce));
if (cspNonce) {
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
loadInfo->SetCspNonce(*cspNonce);
Expand Down Expand Up @@ -1744,8 +1742,7 @@ Result<Loader::LoadSheetResult, nsresult> Loader::LoadInlineStyle(
auto data = MakeRefPtr<SheetLoadData>(
this, aInfo.mTitle, nullptr, sheet, false, aInfo.mContent, isAlternate,
matched, StylePreloadKind::None, aObserver, principal,
aInfo.mReferrerInfo);
MOZ_ASSERT(data->GetRequestingNode() == aInfo.mContent);
aInfo.mReferrerInfo, aInfo.mContent);
data->mLineNumber = aLineNumber;

// Parse completion releases the load data.
Expand Down Expand Up @@ -1793,16 +1790,19 @@ Result<Loader::LoadSheetResult, nsresult> Loader::LoadStyleLink(
? aInfo.mTriggeringPrincipal.get()
: loadingPrincipal;

nsINode* requestingNode =
aInfo.mContent ? static_cast<nsINode*>(aInfo.mContent) : mDocument;
nsINode* context = aInfo.mContent;
if (!context) {
context = mDocument;
}

bool syncLoad = aInfo.mContent && aInfo.mContent->IsInUAWidget() &&
IsChromeURI(aInfo.mURI);
LOG((" Link sync load: '%s'", syncLoad ? "true" : "false"));
MOZ_ASSERT_IF(syncLoad, !aObserver);

nsresult rv =
CheckContentPolicy(loadingPrincipal, principal, aInfo.mURI,
requestingNode, aInfo.mNonce, StylePreloadKind::None);
CheckContentPolicy(loadingPrincipal, principal, aInfo.mURI, context,
aInfo.mNonce, StylePreloadKind::None);
if (NS_WARN_IF(NS_FAILED(rv))) {
// Don't fire the error event if our document is loaded as data. We're
// supposed to not even try to do loads in that case... Unfortunately, we
Expand Down Expand Up @@ -1845,9 +1845,7 @@ Result<Loader::LoadSheetResult, nsresult> Loader::LoadStyleLink(
auto data = MakeRefPtr<SheetLoadData>(
this, aInfo.mTitle, aInfo.mURI, sheet, syncLoad, aInfo.mContent,
isAlternate, matched, StylePreloadKind::None, aObserver, principal,
aInfo.mReferrerInfo);

MOZ_ASSERT(data->GetRequestingNode() == requestingNode);
aInfo.mReferrerInfo, context);

MaybeNotifyPreloadUsed(*data);

Expand Down Expand Up @@ -1932,17 +1930,27 @@ nsresult Loader::LoadChildSheet(StyleSheet& aParentSheet,

nsCOMPtr<nsINode> owningNode;

nsINode* requestingNode = aParentSheet.GetOwnerNodeOfOutermostSheet();
if (requestingNode) {
MOZ_ASSERT(LoaderPrincipal() == requestingNode->NodePrincipal());
} else {
requestingNode = mDocument;
// Check for an associated document or shadow root: if none, don't bother
// walking up the parent sheets.
if (aParentSheet.GetAssociatedDocumentOrShadowRoot()) {
StyleSheet* topSheet = &aParentSheet;
while (StyleSheet* parent = topSheet->GetParentSheet()) {
topSheet = parent;
}
owningNode = topSheet->GetOwnerNode();
}

nsINode* context = nullptr;
if (owningNode) {
context = owningNode;
MOZ_ASSERT(LoaderPrincipal() == owningNode->NodePrincipal());
} else if (mDocument) {
context = mDocument;
}

nsIPrincipal* principal = aParentSheet.Principal();
nsresult rv =
CheckContentPolicy(LoaderPrincipal(), principal, aURL, requestingNode,
u""_ns, StylePreloadKind::None);
nsresult rv = CheckContentPolicy(LoaderPrincipal(), principal, aURL, context,
u""_ns, StylePreloadKind::None);
if (NS_WARN_IF(NS_FAILED(rv))) {
if (aParentData) {
MarkLoadTreeFailed(*aParentData);
Expand Down Expand Up @@ -1991,10 +1999,9 @@ nsresult Loader::LoadChildSheet(StyleSheet& aParentSheet,
MOZ_ASSERT(sheet);
InsertChildSheet(*sheet, aParentSheet);

auto data =
MakeRefPtr<SheetLoadData>(this, aURL, sheet, aParentData, observer,
principal, aParentSheet.GetReferrerInfo());
MOZ_ASSERT(data->GetRequestingNode() == requestingNode);
auto data = MakeRefPtr<SheetLoadData>(
this, aURL, sheet, aParentData, observer, principal,
aParentSheet.GetReferrerInfo(), context);

MaybeNotifyPreloadUsed(*data);

Expand Down
3 changes: 3 additions & 0 deletions layout/style/SharedStyleSheetCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ void SharedStyleSheetCache::LoadCompletedInternal(
data->mSheetCompleteCalled = true;
#endif

// We no longer need this around.
data->mRequestingNodeBeforeComplete = nullptr;

if (!data->mSheetAlreadyComplete) {
// If mSheetAlreadyComplete, then the sheet could well be modified between
// when we posted the async call to SheetComplete and now, since the sheet
Expand Down
12 changes: 8 additions & 4 deletions layout/style/SheetLoadData.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ class SheetLoadData final : public PreloaderBase,
IsAlternate aIsAlternate, MediaMatched aMediaMatched,
StylePreloadKind aPreloadKind, nsICSSLoaderObserver* aObserver,
nsIPrincipal* aTriggeringPrincipal,
nsIReferrerInfo* aReferrerInfo);
nsIReferrerInfo* aReferrerInfo, nsINode* aRequestingNode);

// Data for loading a sheet linked from an @import rule
SheetLoadData(Loader* aLoader, nsIURI* aURI, StyleSheet* aSheet,
SheetLoadData* aParentData, nsICSSLoaderObserver* aObserver,
nsIPrincipal* aTriggeringPrincipal,
nsIReferrerInfo* aReferrerInfo);
nsIReferrerInfo* aReferrerInfo, nsINode* aRequestingNode);

// Data for loading a non-document sheet
SheetLoadData(Loader* aLoader, nsIURI* aURI, StyleSheet* aSheet,
bool aSyncLoad, UseSystemPrincipal, StylePreloadKind,
const Encoding* aPreloadEncoding,
nsICSSLoaderObserver* aObserver,
nsIPrincipal* aTriggeringPrincipal,
nsIReferrerInfo* aReferrerInfo);
nsIReferrerInfo* aReferrerInfo, nsINode* aRequestingNode);

nsIReferrerInfo* ReferrerInfo() const { return mReferrerInfo; }

Expand Down Expand Up @@ -209,7 +209,11 @@ class SheetLoadData final : public PreloaderBase,
// be null).
nsCOMPtr<nsINode> mOwningNodeBeforeLoadEvent;

nsINode* GetRequestingNode() const;
// The node that identifies who started loading us, before the load completes
// or errors.
//
// This is set for both top-level loads and @imports.
nsCOMPtr<nsINode> mRequestingNodeBeforeComplete;

// The observer that wishes to be notified of load completion
nsCOMPtr<nsICSSLoaderObserver> mObserver;
Expand Down
4 changes: 0 additions & 4 deletions layout/style/StyleSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,6 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache {

nsINode* GetOwnerNode() const { return mOwningNode; }

nsINode* GetOwnerNodeOfOutermostSheet() const {
return OutermostSheet().GetOwnerNode();
}

StyleSheet* GetParentSheet() const { return mParentSheet; }

void AddReferencingRule(dom::CSSImportRule& aRule) {
Expand Down

0 comments on commit 2365892

Please sign in to comment.