Bug 1332353. Make it clearer when a stylesheet is really owned by its mDocument. r=heycam

This commit is contained in:
Boris Zbarsky 2017-01-19 23:49:44 -05:00
parent b1bdc1c66d
commit 89948607fd
15 changed files with 86 additions and 41 deletions

View File

@ -3467,7 +3467,7 @@ nsDOMWindowUtils::AddSheet(nsIPreloadedStyleSheet* aSheet, uint32_t aSheetType)
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(sheet, NS_ERROR_FAILURE);
if (sheet->GetOwningDocument()) {
if (sheet->GetAssociatedDocument()) {
return NS_ERROR_INVALID_ARG;
}

View File

@ -1485,7 +1485,7 @@ nsDocument::~nsDocument()
// Let the stylesheets know we're going away
for (StyleSheet* sheet : mStyleSheets) {
sheet->SetOwningDocument(nullptr);
sheet->ClearAssociatedDocument();
}
if (mAttrStyleSheet) {
mAttrStyleSheet->SetOwningDocument(nullptr);
@ -2126,7 +2126,7 @@ nsDocument::RemoveDocStyleSheetsFromStyleSets()
{
// The stylesheets should forget us
for (StyleSheet* sheet : Reversed(mStyleSheets)) {
sheet->SetOwningDocument(nullptr);
sheet->ClearAssociatedDocument();
if (sheet->IsApplicable()) {
nsCOMPtr<nsIPresShell> shell = GetShell();
@ -2145,7 +2145,7 @@ nsDocument::RemoveStyleSheetsFromStyleSets(
{
// The stylesheets should forget us
for (StyleSheet* sheet : Reversed(aSheets)) {
sheet->SetOwningDocument(nullptr);
sheet->ClearAssociatedDocument();
if (sheet->IsApplicable()) {
nsCOMPtr<nsIPresShell> shell = GetShell();
@ -4082,7 +4082,7 @@ nsDocument::AddStyleSheet(StyleSheet* aSheet)
{
NS_PRECONDITION(aSheet, "null arg");
mStyleSheets.AppendElement(aSheet);
aSheet->SetOwningDocument(this);
aSheet->SetAssociatedDocument(this, StyleSheet::OwnedByDocument);
if (aSheet->IsApplicable()) {
AddStyleSheetToStyleSets(aSheet);
@ -4119,7 +4119,7 @@ nsDocument::RemoveStyleSheet(StyleSheet* aSheet)
NotifyStyleSheetRemoved(aSheet, true);
}
aSheet->SetOwningDocument(nullptr);
aSheet->ClearAssociatedDocument();
}
void
@ -4147,7 +4147,7 @@ nsDocument::UpdateStyleSheets(nsTArray<RefPtr<StyleSheet>>& aOldSheets,
StyleSheet* newSheet = aNewSheets[i];
if (newSheet) {
mStyleSheets.InsertElementAt(oldIndex, newSheet);
newSheet->SetOwningDocument(this);
newSheet->SetAssociatedDocument(this, StyleSheet::OwnedByDocument);
if (newSheet->IsApplicable()) {
AddStyleSheetToStyleSets(newSheet);
}
@ -4166,7 +4166,7 @@ nsDocument::InsertStyleSheetAt(StyleSheet* aSheet, int32_t aIndex)
mStyleSheets.InsertElementAt(aIndex, aSheet);
aSheet->SetOwningDocument(this);
aSheet->SetAssociatedDocument(this, StyleSheet::OwnedByDocument);
if (aSheet->IsApplicable()) {
AddStyleSheetToStyleSets(aSheet);
@ -4294,7 +4294,7 @@ nsDocument::LoadAdditionalStyleSheet(additionalSheetType aType,
nsresult rv = loader->LoadSheetSync(aSheetURI, parsingMode, true, &sheet);
NS_ENSURE_SUCCESS(rv, rv);
sheet->SetOwningDocument(this);
sheet->SetAssociatedDocument(this, StyleSheet::OwnedByDocument);
MOZ_ASSERT(sheet->IsApplicable());
return AddAdditionalStyleSheet(aType, sheet);
@ -4352,7 +4352,7 @@ nsDocument::RemoveAdditionalStyleSheet(additionalSheetType aType, nsIURI* aSheet
NotifyStyleSheetRemoved(sheetRef, false);
EndUpdate(UPDATE_STYLE);
sheetRef->SetOwningDocument(nullptr);
sheetRef->ClearAssociatedDocument();
}
}
@ -12022,7 +12022,7 @@ SizeOfOwnedSheetArrayExcludingThis(const nsTArray<RefPtr<StyleSheet>>& aSheets,
size_t n = 0;
n += aSheets.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (StyleSheet* sheet : aSheets) {
if (!sheet->GetOwningDocument()) {
if (!sheet->GetAssociatedDocument()) {
// Avoid over-reporting shared sheets.
continue;
}

View File

@ -2952,7 +2952,7 @@ HTMLEditor::EnableStyleSheet(const nsAString& aURL,
// Ensure the style sheet is owned by our document.
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
sheet->SetOwningDocument(doc);
sheet->SetAssociatedDocument(doc, StyleSheet::NotOwnedByDocument);
if (sheet->IsServo()) {
// XXXheycam ServoStyleSheets don't support being enabled/disabled yet.
@ -2974,7 +2974,7 @@ HTMLEditor::EnableExistingStyleSheet(const nsAString& aURL)
// Ensure the style sheet is owned by our document.
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
sheet->SetOwningDocument(doc);
sheet->SetAssociatedDocument(doc, StyleSheet::NotOwnedByDocument);
if (sheet->IsServo()) {
// XXXheycam ServoStyleSheets don't support being enabled/disabled yet.

View File

@ -151,7 +151,8 @@ struct ChildSheetListBuilder {
void SetParentLinks(CSSStyleSheet* aSheet) {
aSheet->mParent = parent;
aSheet->SetOwningDocument(parent->mDocument);
aSheet->SetAssociatedDocument(parent->mDocument,
parent->mDocumentAssociationMode);
}
static void ReparentChildList(CSSStyleSheet* aPrimarySheet,
@ -159,7 +160,8 @@ struct ChildSheetListBuilder {
{
for (CSSStyleSheet *child = aFirstChild; child; child = child->mNext) {
child->mParent = aPrimarySheet;
child->SetOwningDocument(aPrimarySheet->mDocument);
child->SetAssociatedDocument(aPrimarySheet->mDocument,
aPrimarySheet->mDocumentAssociationMode);
}
}
};
@ -612,16 +614,22 @@ CSSStyleSheet::GetParentSheet() const
}
void
CSSStyleSheet::SetOwningDocument(nsIDocument* aDocument)
{ // not ref counted
CSSStyleSheet::SetAssociatedDocument(nsIDocument* aDocument,
DocumentAssociationMode aAssociationMode)
{
MOZ_ASSERT_IF(!aDocument, aAssociationMode == NotOwnedByDocument);
// not ref counted
mDocument = aDocument;
mDocumentAssociationMode = aAssociationMode;
// Now set the same document on all our child sheets....
// XXXbz this is a little bogus; see the XXX comment where we
// declare mFirstChild.
for (CSSStyleSheet* child = mInner->mFirstChild;
child; child = child->mNext) {
if (child->mParent == this) {
child->SetOwningDocument(aDocument);
child->SetAssociatedDocument(aDocument, aAssociationMode);
}
}
}

View File

@ -117,7 +117,8 @@ public:
// style sheet owner info
CSSStyleSheet* GetParentSheet() const; // may be null
void SetOwningDocument(nsIDocument* aDocument);
void SetAssociatedDocument(nsIDocument* aDocument,
DocumentAssociationMode aAssociationMode);
// Find the ID of the owner inner window.
uint64_t FindOwningWindowInnerID() const;

View File

@ -2218,13 +2218,13 @@ Loader::LoadChildSheet(StyleSheet* aParentSheet,
nsCOMPtr<nsINode> owningNode;
// check for an owning document: if none, don't bother walking up the parent
// sheets
// check for an associated document: if none, don't bother walking up the
// parent sheets
//
// FIXME(emilio): Figure out whether this walk up is necessary (try seems
// green without it), and fix the parenting of stylesheets in the servo case
// if that's the case.
if (aParentSheet->GetOwningDocument() && aParentSheet->IsGecko()) {
if (aParentSheet->GetAssociatedDocument() && aParentSheet->IsGecko()) {
StyleSheet* topSheet = aParentSheet;
while (StyleSheet* parent = topSheet->GetParentSheet()) {
topSheet = parent;

View File

@ -70,15 +70,15 @@ ServoStyleRuleDeclaration::SetCSSDeclaration(DeclarationBlock* aDecl)
{
ServoStyleRule* rule = Rule();
if (RefPtr<ServoStyleSheet> sheet = rule->GetStyleSheet()->AsServo()) {
nsCOMPtr<nsIDocument> owningDoc = sheet->GetOwningDocument();
mozAutoDocUpdate updateBatch(owningDoc, UPDATE_STYLE, true);
nsCOMPtr<nsIDocument> doc = sheet->GetAssociatedDocument();
mozAutoDocUpdate updateBatch(doc, UPDATE_STYLE, true);
if (aDecl != mDecls) {
RefPtr<ServoDeclarationBlock> decls = aDecl->AsServo();
Servo_StyleRule_SetStyle(rule->Raw(), decls->Raw());
mDecls = decls.forget();
}
if (owningDoc) {
owningDoc->StyleRuleChanged(sheet, rule);
if (doc) {
doc->StyleRuleChanged(sheet, rule);
}
}
return NS_OK;

View File

@ -55,19 +55,23 @@ ServoStyleSheet::HasRules() const
}
void
ServoStyleSheet::SetOwningDocument(nsIDocument* aDocument)
ServoStyleSheet::SetAssociatedDocument(nsIDocument* aDocument,
DocumentAssociationMode aAssociationMode)
{
MOZ_ASSERT_IF(!aDocument, aAssociationMode == NotOwnedByDocument);
// XXXheycam: Traverse to child ServoStyleSheets to set this, like
// CSSStyleSheet::SetOwningDocument does.
// CSSStyleSheet::SetAssociatedDocument does.
mDocument = aDocument;
mDocumentAssociationMode = aAssociationMode;
}
ServoStyleSheet*
ServoStyleSheet::GetParentSheet() const
{
// XXXheycam: When we implement support for child sheets, we'll have
// to fix SetOwningDocument to propagate the owning document down
// to fix SetAssociatedDocument to propagate the associated document down
// to the children.
MOZ_CRASH("stylo: not implemented");
}

View File

@ -38,7 +38,8 @@ public:
bool HasRules() const;
void SetOwningDocument(nsIDocument* aDocument);
void SetAssociatedDocument(nsIDocument* aDocument,
DocumentAssociationMode aAssociationMode);
ServoStyleSheet* GetParentSheet() const;
void AppendStyleSheet(ServoStyleSheet* aSheet);

View File

@ -1206,13 +1206,13 @@ DOMCSSDeclarationImpl::SetCSSDeclaration(DeclarationBlock* aDecl)
NS_PRECONDITION(mRule,
"can only be called when |GetCSSDeclaration| returned a declaration");
nsCOMPtr<nsIDocument> owningDoc;
nsCOMPtr<nsIDocument> doc;
RefPtr<CSSStyleSheet> sheet = mRule->GetStyleSheet();
if (sheet) {
owningDoc = sheet->GetOwningDocument();
doc = sheet->GetAssociatedDocument();
}
mozAutoDocUpdate updateBatch(owningDoc, UPDATE_STYLE, true);
mozAutoDocUpdate updateBatch(doc, UPDATE_STYLE, true);
mRule->SetDeclaration(aDecl->AsGecko());
@ -1220,8 +1220,8 @@ DOMCSSDeclarationImpl::SetCSSDeclaration(DeclarationBlock* aDecl)
sheet->DidDirty();
}
if (owningDoc) {
owningDoc->StyleRuleChanged(sheet, mRule);
if (doc) {
doc->StyleRuleChanged(sheet, mRule);
}
return NS_OK;
}

View File

@ -24,6 +24,7 @@ StyleSheet::StyleSheet(StyleBackendType aType, css::SheetParsingMode aParsingMod
, mParsingMode(aParsingMode)
, mType(aType)
, mDisabled(false)
, mDocumentAssociationMode(NotOwnedByDocument)
{
}
@ -36,6 +37,9 @@ StyleSheet::StyleSheet(const StyleSheet& aCopy,
, mParsingMode(aCopy.mParsingMode)
, mType(aCopy.mType)
, mDisabled(aCopy.mDisabled)
// We only use this constructor during cloning. It's the cloner's
// responsibility to notify us if we end up being owned by a document.
, mDocumentAssociationMode(NotOwnedByDocument)
{
if (aCopy.mMedia) {
// XXX This is wrong; we should be keeping @import rules and

View File

@ -106,8 +106,22 @@ public:
inline bool HasRules() const;
// style sheet owner info
nsIDocument* GetOwningDocument() const { return mDocument; }
inline void SetOwningDocument(nsIDocument* aDocument);
enum DocumentAssociationMode {
// OwnedByDocument means mDocument owns us (possibly via a chain of other
// stylesheets).
OwnedByDocument,
// NotOwnedByDocument means we're owned by something that might have a
// different lifetime than mDocument.
NotOwnedByDocument
};
nsIDocument* GetAssociatedDocument() const { return mDocument; }
bool IsOwnedByDocument() const {
return mDocumentAssociationMode == OwnedByDocument;
}
// aDocument must not be null.
inline void SetAssociatedDocument(nsIDocument* aDocument,
DocumentAssociationMode aMode);
inline void ClearAssociatedDocument();
nsINode* GetOwnerNode() const { return mOwningNode; }
inline StyleSheet* GetParentSheet() const;
@ -225,6 +239,11 @@ protected:
const StyleBackendType mType;
bool mDisabled;
// mDocumentAssociationMode determines whether mDocument directly owns us (in
// the sense that if it's known-live then we're known-live). Always
// NotOwnedByDocument when mDocument is null.
DocumentAssociationMode mDocumentAssociationMode;
};
} // namespace mozilla

View File

@ -83,9 +83,17 @@ StyleSheet::HasRules() const
}
void
StyleSheet::SetOwningDocument(nsIDocument* aDocument)
StyleSheet::SetAssociatedDocument(nsIDocument* aDocument,
DocumentAssociationMode aAssociationMode)
{
MOZ_STYLO_FORWARD(SetOwningDocument, (aDocument))
MOZ_ASSERT(aDocument);
MOZ_STYLO_FORWARD(SetAssociatedDocument, (aDocument, aAssociationMode))
}
void
StyleSheet::ClearAssociatedDocument()
{
MOZ_STYLO_FORWARD(SetAssociatedDocument, (nullptr, NotOwnedByDocument));
}
StyleSheet*

View File

@ -267,7 +267,7 @@ nsDOMCSSDeclaration::GetCSSParsingEnvironmentForRule(css::Rule* aRule,
return;
}
nsIDocument* document = sheet->GetOwningDocument();
nsIDocument* document = sheet->GetAssociatedDocument();
aCSSParseEnv.mSheetURI = sheet->GetSheetURI();
aCSSParseEnv.mBaseURI = sheet->GetBaseURI();
aCSSParseEnv.mPrincipal = sheet->Principal();

View File

@ -583,7 +583,7 @@ nsMediaList::GetMediaText(nsAString& aMediaText)
// nsCOMPtr<nsIDocument>
#define BEGIN_MEDIA_CHANGE(sheet, doc) \
if (sheet) { \
doc = sheet->GetOwningDocument(); \
doc = sheet->GetAssociatedDocument(); \
} \
mozAutoDocUpdate updateBatch(doc, UPDATE_STYLE, true); \
if (sheet) { \