2016-02-26 04:51:01 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2016-03-02 01:38:13 +03:00
|
|
|
#include "mozilla/StyleSheet.h"
|
|
|
|
|
2018-03-22 21:20:41 +03:00
|
|
|
#include "mozilla/ComputedStyleInlines.h"
|
2018-04-30 20:23:53 +03:00
|
|
|
#include "mozilla/css/ErrorReporter.h"
|
2018-02-01 07:04:04 +03:00
|
|
|
#include "mozilla/css/GroupRule.h"
|
2017-05-30 04:10:25 +03:00
|
|
|
#include "mozilla/dom/CSSImportRule.h"
|
2016-10-14 14:25:38 +03:00
|
|
|
#include "mozilla/dom/CSSRuleList.h"
|
2018-02-13 03:53:44 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2016-11-09 09:28:24 +03:00
|
|
|
#include "mozilla/dom/MediaList.h"
|
2016-03-02 02:10:45 +03:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2018-02-13 03:53:44 +03:00
|
|
|
#include "mozilla/dom/ShadowRootBinding.h"
|
2018-07-17 22:37:48 +03:00
|
|
|
#include "mozilla/NullPrincipal.h"
|
2018-06-04 15:14:47 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2017-06-19 08:45:43 +03:00
|
|
|
#include "mozilla/ServoCSSRuleList.h"
|
2018-02-13 03:53:44 +03:00
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2018-04-30 20:23:53 +03:00
|
|
|
#include "mozilla/StaticPrefs.h"
|
2016-03-02 01:38:13 +03:00
|
|
|
#include "mozilla/StyleSheetInlines.h"
|
2016-02-26 04:51:01 +03:00
|
|
|
|
2016-11-23 02:26:20 +03:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2019-03-30 03:23:49 +03:00
|
|
|
#include "nsLayoutStylesheetCache.h"
|
2019-05-22 09:01:51 +03:00
|
|
|
#include "SheetLoadData.h"
|
2016-09-29 09:19:06 +03:00
|
|
|
|
2016-02-26 04:51:01 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2018-06-04 13:09:11 +03:00
|
|
|
using namespace dom;
|
|
|
|
|
2018-04-30 18:50:03 +03:00
|
|
|
StyleSheet::StyleSheet(css::SheetParsingMode aParsingMode, CORSMode aCORSMode,
|
|
|
|
net::ReferrerPolicy aReferrerPolicy,
|
|
|
|
const dom::SRIMetadata& aIntegrity)
|
2017-01-25 00:09:33 +03:00
|
|
|
: mParent(nullptr),
|
2018-05-11 13:57:38 +03:00
|
|
|
mDocumentOrShadowRoot(nullptr),
|
2016-03-01 23:39:29 +03:00
|
|
|
mOwningNode(nullptr),
|
2017-05-30 04:10:25 +03:00
|
|
|
mOwnerRule(nullptr),
|
2016-08-02 23:12:27 +03:00
|
|
|
mParsingMode(aParsingMode),
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
mState(static_cast<State>(0)),
|
2018-05-11 13:57:38 +03:00
|
|
|
mAssociationMode(NotOwnedByDocumentOrShadowRoot),
|
2018-04-30 18:50:03 +03:00
|
|
|
mInner(new StyleSheetInfo(aCORSMode, aReferrerPolicy, aIntegrity,
|
|
|
|
aParsingMode)) {
|
|
|
|
mInner->AddSheet(this);
|
2016-02-26 04:51:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
StyleSheet::StyleSheet(const StyleSheet& aCopy, StyleSheet* aParentToUse,
|
2017-05-30 04:10:25 +03:00
|
|
|
dom::CSSImportRule* aOwnerRuleToUse,
|
2018-05-11 13:57:38 +03:00
|
|
|
dom::DocumentOrShadowRoot* aDocumentOrShadowRoot,
|
2016-02-26 04:51:01 +03:00
|
|
|
nsINode* aOwningNodeToUse)
|
2017-05-31 14:12:33 +03:00
|
|
|
: mParent(aParentToUse),
|
2017-01-25 00:09:33 +03:00
|
|
|
mTitle(aCopy.mTitle),
|
2018-05-11 13:57:38 +03:00
|
|
|
mDocumentOrShadowRoot(aDocumentOrShadowRoot),
|
2016-03-01 23:39:29 +03:00
|
|
|
mOwningNode(aOwningNodeToUse),
|
2017-05-30 04:10:25 +03:00
|
|
|
mOwnerRule(aOwnerRuleToUse),
|
2016-02-26 04:51:01 +03:00
|
|
|
mParsingMode(aCopy.mParsingMode),
|
2018-12-05 21:44:05 +03:00
|
|
|
mState(aCopy.mState),
|
2017-05-31 14:12:33 +03:00
|
|
|
// 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.
|
2018-05-11 13:57:38 +03:00
|
|
|
mAssociationMode(NotOwnedByDocumentOrShadowRoot),
|
2018-12-05 21:44:05 +03:00
|
|
|
// Shallow copy, but concrete subclasses will fix up.
|
|
|
|
mInner(aCopy.mInner) {
|
2017-02-18 02:48:35 +03:00
|
|
|
MOZ_ASSERT(mInner, "Should only copy StyleSheets with an mInner.");
|
|
|
|
mInner->AddSheet(this);
|
|
|
|
|
2018-04-30 18:50:03 +03:00
|
|
|
if (HasForcedUniqueInner()) { // CSSOM's been there, force full copy now
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
MOZ_ASSERT(IsComplete(),
|
|
|
|
"Why have rules been accessed on an incomplete sheet?");
|
2018-04-30 18:50:03 +03:00
|
|
|
// FIXME: handle failure?
|
|
|
|
EnsureUniqueInner();
|
|
|
|
}
|
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
if (aCopy.mMedia) {
|
|
|
|
// XXX This is wrong; we should be keeping @import rules and
|
|
|
|
// sheets in sync!
|
|
|
|
mMedia = aCopy.mMedia->Clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleSheet::~StyleSheet() {
|
2017-06-19 03:15:49 +03:00
|
|
|
MOZ_ASSERT(!mInner, "Inner should have been dropped in LastRelease");
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:50:03 +03:00
|
|
|
bool StyleSheet::HasRules() const {
|
2018-04-30 20:47:12 +03:00
|
|
|
return Servo_StyleSheet_HasRules(Inner().mContents);
|
2018-04-30 18:50:03 +03:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* StyleSheet::GetAssociatedDocument() const {
|
2018-05-11 13:57:38 +03:00
|
|
|
return mDocumentOrShadowRoot ? mDocumentOrShadowRoot->AsNode().OwnerDoc()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* StyleSheet::GetComposedDoc() const {
|
2018-05-11 13:57:38 +03:00
|
|
|
return mDocumentOrShadowRoot
|
|
|
|
? mDocumentOrShadowRoot->AsNode().GetComposedDoc()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StyleSheet::IsKeptAliveByDocument() const {
|
|
|
|
if (mAssociationMode != OwnedByDocumentOrShadowRoot) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!GetComposedDoc();
|
|
|
|
}
|
|
|
|
|
2017-06-19 03:15:49 +03:00
|
|
|
void StyleSheet::LastRelease() {
|
2017-02-18 02:48:35 +03:00
|
|
|
MOZ_ASSERT(mInner, "Should have an mInner at time of destruction.");
|
|
|
|
MOZ_ASSERT(mInner->mSheets.Contains(this), "Our mInner should include us.");
|
2017-06-19 03:15:49 +03:00
|
|
|
|
|
|
|
UnparentChildren();
|
|
|
|
|
2017-02-18 02:48:35 +03:00
|
|
|
mInner->RemoveSheet(this);
|
|
|
|
mInner = nullptr;
|
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
DropMedia();
|
2018-04-30 18:50:03 +03:00
|
|
|
DropRuleList();
|
2016-02-26 04:51:01 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 03:19:33 +03:00
|
|
|
void StyleSheet::UnlinkInner() {
|
|
|
|
// We can only have a cycle through our inner if we have a unique inner,
|
|
|
|
// because otherwise there are no JS wrappers for anything in the inner.
|
|
|
|
if (mInner->mSheets.Length() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have to be a bit careful with child sheets, because we want to
|
|
|
|
// drop their mNext pointers and null out their mParent and
|
|
|
|
// mDocument, but don't want to work with deleted objects. And we
|
|
|
|
// don't want to do any addrefing in the process, just to make sure
|
|
|
|
// we don't confuse the cycle collector (though on the face of it,
|
|
|
|
// addref/release pairs during unlink should probably be ok).
|
|
|
|
RefPtr<StyleSheet> child;
|
2018-04-30 20:47:12 +03:00
|
|
|
child.swap(Inner().mFirstChild);
|
2017-02-16 03:19:33 +03:00
|
|
|
while (child) {
|
|
|
|
MOZ_ASSERT(child->mParent == this, "We have a unique inner!");
|
|
|
|
child->mParent = nullptr;
|
2017-04-24 22:44:19 +03:00
|
|
|
// We (and child) might still think we're owned by a document, because
|
|
|
|
// unlink order is non-deterministic, so the document's unlink, which would
|
|
|
|
// tell us it does't own us anymore, may not have happened yet. But if
|
|
|
|
// we're being unlinked, clearly we're not owned by a document anymore
|
|
|
|
// conceptually!
|
2018-05-11 13:57:38 +03:00
|
|
|
child->ClearAssociatedDocumentOrShadowRoot();
|
2017-02-16 03:19:33 +03:00
|
|
|
|
|
|
|
RefPtr<StyleSheet> next;
|
|
|
|
// Null out child->mNext, but don't let it die yet
|
|
|
|
next.swap(child->mNext);
|
|
|
|
// Switch to looking at the old value of child->mNext next iteration
|
|
|
|
child.swap(next);
|
|
|
|
// "next" is now our previous value of child; it'll get released
|
|
|
|
// as we loop around.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::TraverseInner(nsCycleCollectionTraversalCallback& cb) {
|
|
|
|
// We can only have a cycle through our inner if we have a unique inner,
|
|
|
|
// because otherwise there are no JS wrappers for anything in the inner.
|
|
|
|
if (mInner->mSheets.Length() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleSheet* childSheet = GetFirstChild();
|
|
|
|
while (childSheet) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "child sheet");
|
2018-01-11 11:17:57 +03:00
|
|
|
cb.NoteXPCOMChild(childSheet);
|
2017-02-16 03:19:33 +03:00
|
|
|
childSheet = childSheet->mNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// QueryInterface implementation for StyleSheet
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StyleSheet)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2017-04-03 12:55:06 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)
|
2018-01-11 11:17:57 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2016-10-14 14:25:38 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(StyleSheet)
|
2017-06-19 03:15:49 +03:00
|
|
|
// We want to disconnect from our inner as soon as our refcount drops to zero,
|
|
|
|
// without waiting for async deletion by the cycle collector. Otherwise we
|
|
|
|
// might end up cloning the inner if someone mutates another sheet that shares
|
|
|
|
// it with us, even though there is only one such sheet and we're about to go
|
|
|
|
// away. This situation arises easily with sheet preloading.
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(StyleSheet, LastRelease())
|
2016-10-14 14:25:38 +03:00
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(StyleSheet)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(StyleSheet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMedia)
|
2018-04-30 18:50:03 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRuleList)
|
2017-02-16 03:19:33 +03:00
|
|
|
tmp->TraverseInner(cb);
|
2017-01-06 10:05:24 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(StyleSheet)
|
|
|
|
tmp->DropMedia();
|
2017-02-16 03:19:33 +03:00
|
|
|
tmp->UnlinkInner();
|
2018-04-30 18:50:03 +03:00
|
|
|
tmp->DropRuleList();
|
2017-01-06 10:05:24 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(StyleSheet)
|
2016-10-14 14:25:38 +03:00
|
|
|
|
2016-08-02 23:17:06 +03:00
|
|
|
mozilla::dom::CSSStyleSheetParsingMode StyleSheet::ParsingModeDOM() {
|
2018-12-24 04:34:49 +03:00
|
|
|
#define CHECK_MODE(X, Y) \
|
2016-08-02 23:17:06 +03:00
|
|
|
static_assert( \
|
|
|
|
static_cast<int>(X) == static_cast<int>(Y), \
|
|
|
|
"mozilla::dom::CSSStyleSheetParsingMode and " \
|
|
|
|
"mozilla::css::SheetParsingMode should have identical values");
|
|
|
|
|
2018-12-24 04:34:49 +03:00
|
|
|
CHECK_MODE(mozilla::dom::CSSStyleSheetParsingMode::Agent,
|
|
|
|
css::eAgentSheetFeatures);
|
|
|
|
CHECK_MODE(mozilla::dom::CSSStyleSheetParsingMode::User,
|
|
|
|
css::eUserSheetFeatures);
|
|
|
|
CHECK_MODE(mozilla::dom::CSSStyleSheetParsingMode::Author,
|
|
|
|
css::eAuthorSheetFeatures);
|
2016-08-02 23:17:06 +03:00
|
|
|
|
2018-12-24 04:34:49 +03:00
|
|
|
#undef CHECK_MODE
|
2016-08-02 23:17:06 +03:00
|
|
|
|
|
|
|
return static_cast<mozilla::dom::CSSStyleSheetParsingMode>(mParsingMode);
|
|
|
|
}
|
|
|
|
|
2016-03-02 02:10:45 +03:00
|
|
|
void StyleSheet::SetComplete() {
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
MOZ_ASSERT(!HasForcedUniqueInner(),
|
|
|
|
"Can't complete a sheet that's already been forced unique.");
|
|
|
|
MOZ_ASSERT(!IsComplete(), "Already complete?");
|
|
|
|
mState |= State::Complete;
|
|
|
|
if (!Disabled()) {
|
2018-03-17 00:56:05 +03:00
|
|
|
ApplicableStateChanged(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::ApplicableStateChanged(bool aApplicable) {
|
2018-05-11 13:57:38 +03:00
|
|
|
if (!mDocumentOrShadowRoot) {
|
|
|
|
return;
|
2016-03-02 02:10:45 +03:00
|
|
|
}
|
|
|
|
|
2018-05-11 13:57:38 +03:00
|
|
|
nsINode& node = mDocumentOrShadowRoot->AsNode();
|
|
|
|
if (auto* shadow = ShadowRoot::FromNode(node)) {
|
2018-03-17 00:56:05 +03:00
|
|
|
shadow->StyleSheetApplicableStateChanged(*this, aApplicable);
|
2018-05-11 13:57:38 +03:00
|
|
|
} else {
|
2018-05-11 19:37:47 +03:00
|
|
|
node.AsDocument()->SetStyleSheetApplicableState(this, aApplicable);
|
2016-03-02 02:10:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
void StyleSheet::SetDisabled(bool aDisabled) {
|
2019-04-29 08:34:06 +03:00
|
|
|
if (IsReadOnly()) {
|
2019-03-15 15:54:17 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
if (aDisabled == Disabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-06 10:05:24 +03:00
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
if (aDisabled) {
|
|
|
|
mState |= State::Disabled;
|
|
|
|
} else {
|
|
|
|
mState &= ~State::Disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsComplete()) {
|
|
|
|
ApplicableStateChanged(!aDisabled);
|
2017-01-06 10:05:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 03:15:43 +03:00
|
|
|
void StyleSheet::SetURLExtraData() {
|
|
|
|
Inner().mURLData = new URLExtraData(GetBaseURI(), GetSheetURI(), Principal(),
|
|
|
|
GetReferrerPolicy());
|
2018-09-17 08:36:45 +03:00
|
|
|
}
|
2019-03-30 03:15:43 +03:00
|
|
|
|
2016-09-29 09:19:06 +03:00
|
|
|
StyleSheetInfo::StyleSheetInfo(CORSMode aCORSMode,
|
|
|
|
ReferrerPolicy aReferrerPolicy,
|
2018-04-30 18:50:03 +03:00
|
|
|
const SRIMetadata& aIntegrity,
|
|
|
|
css::SheetParsingMode aParsingMode)
|
2018-03-22 21:36:20 +03:00
|
|
|
: mPrincipal(NullPrincipal::CreateWithoutOriginAttributes()),
|
2016-09-29 09:19:06 +03:00
|
|
|
mCORSMode(aCORSMode),
|
|
|
|
mReferrerPolicy(aReferrerPolicy),
|
|
|
|
mIntegrity(aIntegrity),
|
2018-04-30 18:50:03 +03:00
|
|
|
mContents(Servo_StyleSheet_Empty(aParsingMode).Consume()),
|
|
|
|
mURLData(URLExtraData::Dummy())
|
2016-09-29 09:19:06 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mPrincipalSet(false)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (!mPrincipal) {
|
2017-10-25 09:30:31 +03:00
|
|
|
MOZ_CRASH("NullPrincipal::Init failed");
|
2016-09-29 09:19:06 +03:00
|
|
|
}
|
2018-04-30 18:50:03 +03:00
|
|
|
MOZ_COUNT_CTOR(StyleSheetInfo);
|
2016-09-29 09:19:06 +03:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:50:03 +03:00
|
|
|
StyleSheetInfo::StyleSheetInfo(StyleSheetInfo& aCopy, StyleSheet* aPrimarySheet)
|
2017-02-18 02:48:35 +03:00
|
|
|
: mSheetURI(aCopy.mSheetURI),
|
|
|
|
mOriginalSheetURI(aCopy.mOriginalSheetURI),
|
|
|
|
mBaseURI(aCopy.mBaseURI),
|
|
|
|
mPrincipal(aCopy.mPrincipal),
|
|
|
|
mCORSMode(aCopy.mCORSMode),
|
|
|
|
mReferrerPolicy(aCopy.mReferrerPolicy),
|
|
|
|
mIntegrity(aCopy.mIntegrity),
|
2019-04-29 08:34:06 +03:00
|
|
|
mFirstChild(), // We don't rebuild the child because we're making a copy
|
|
|
|
// without children.
|
2017-06-29 02:51:46 +03:00
|
|
|
mSourceMapURL(aCopy.mSourceMapURL),
|
2017-08-09 22:33:24 +03:00
|
|
|
mSourceMapURLFromComment(aCopy.mSourceMapURLFromComment),
|
2017-09-14 23:59:32 +03:00
|
|
|
mSourceURL(aCopy.mSourceURL),
|
2018-04-30 18:50:03 +03:00
|
|
|
mContents(Servo_StyleSheet_Clone(aCopy.mContents.get(), aPrimarySheet)
|
|
|
|
.Consume()),
|
2019-03-30 03:23:49 +03:00
|
|
|
// Cloning aCopy.mContents will still leave us with some references to
|
|
|
|
// data in shared memory (for example, any SelectorList objects will still
|
|
|
|
// be shared), so continue to keep it alive.
|
|
|
|
mSharedMemory(aCopy.mSharedMemory),
|
2018-04-30 18:50:03 +03:00
|
|
|
mURLData(aCopy.mURLData)
|
2017-02-18 02:48:35 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mPrincipalSet(aCopy.mPrincipalSet)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
AddSheet(aPrimarySheet);
|
2018-04-30 18:50:03 +03:00
|
|
|
|
|
|
|
// Our child list is fixed up by our parent.
|
|
|
|
MOZ_COUNT_CTOR(StyleSheetInfo);
|
2017-02-18 02:48:35 +03:00
|
|
|
}
|
|
|
|
|
2019-03-30 03:23:49 +03:00
|
|
|
StyleSheetInfo::~StyleSheetInfo() {
|
|
|
|
MOZ_COUNT_DTOR(StyleSheetInfo);
|
|
|
|
|
|
|
|
// Drop the sheet contents before the shared memory.
|
|
|
|
mContents = nullptr;
|
|
|
|
}
|
2018-04-30 18:50:03 +03:00
|
|
|
|
|
|
|
StyleSheetInfo* StyleSheetInfo::CloneFor(StyleSheet* aPrimarySheet) {
|
|
|
|
return new StyleSheetInfo(*this, aPrimarySheet);
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:09:30 +03:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(ServoStyleSheetMallocSizeOf)
|
|
|
|
MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(ServoStyleSheetMallocEnclosingSizeOf)
|
|
|
|
|
2018-04-30 18:50:03 +03:00
|
|
|
size_t StyleSheetInfo::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = aMallocSizeOf(this);
|
2019-03-30 03:23:49 +03:00
|
|
|
|
|
|
|
// If this sheet came from shared memory, then it will be counted by
|
|
|
|
// nsLayoutStylesheetCache in the parent process.
|
|
|
|
if (!mSharedMemory) {
|
|
|
|
n += Servo_StyleSheet_SizeOfIncludingThis(
|
|
|
|
ServoStyleSheetMallocSizeOf, ServoStyleSheetMallocEnclosingSizeOf,
|
|
|
|
mContents);
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:50:03 +03:00
|
|
|
return n;
|
2017-05-16 02:19:17 +03:00
|
|
|
}
|
|
|
|
|
2017-02-18 02:48:35 +03:00
|
|
|
void StyleSheetInfo::AddSheet(StyleSheet* aSheet) {
|
|
|
|
mSheets.AppendElement(aSheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheetInfo::RemoveSheet(StyleSheet* aSheet) {
|
2017-03-07 03:27:32 +03:00
|
|
|
if ((aSheet == mSheets.ElementAt(0)) && (mSheets.Length() > 1)) {
|
|
|
|
StyleSheet::ChildSheetListBuilder::ReparentChildList(mSheets[1],
|
|
|
|
mFirstChild);
|
|
|
|
}
|
|
|
|
|
2017-02-18 02:48:35 +03:00
|
|
|
if (1 == mSheets.Length()) {
|
|
|
|
NS_ASSERTION(aSheet == mSheets.ElementAt(0), "bad parent");
|
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSheets.RemoveElement(aSheet);
|
|
|
|
}
|
|
|
|
|
2017-03-07 03:27:32 +03:00
|
|
|
void StyleSheet::ChildSheetListBuilder::SetParentLinks(StyleSheet* aSheet) {
|
|
|
|
aSheet->mParent = parent;
|
2018-05-11 13:57:38 +03:00
|
|
|
aSheet->SetAssociatedDocumentOrShadowRoot(parent->mDocumentOrShadowRoot,
|
|
|
|
parent->mAssociationMode);
|
2017-03-07 03:27:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::ChildSheetListBuilder::ReparentChildList(
|
|
|
|
StyleSheet* aPrimarySheet, StyleSheet* aFirstChild) {
|
|
|
|
for (StyleSheet* child = aFirstChild; child; child = child->mNext) {
|
|
|
|
child->mParent = aPrimarySheet;
|
2018-05-11 13:57:38 +03:00
|
|
|
child->SetAssociatedDocumentOrShadowRoot(
|
|
|
|
aPrimarySheet->mDocumentOrShadowRoot, aPrimarySheet->mAssociationMode);
|
2017-03-07 03:27:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
void StyleSheet::GetType(nsAString& aType) { aType.AssignLiteral("text/css"); }
|
|
|
|
|
2018-01-11 11:17:57 +03:00
|
|
|
void StyleSheet::GetHref(nsAString& aHref, ErrorResult& aRv) {
|
2018-04-30 20:47:12 +03:00
|
|
|
if (nsIURI* sheetURI = Inner().mOriginalSheetURI) {
|
2016-10-14 14:25:38 +03:00
|
|
|
nsAutoCString str;
|
|
|
|
nsresult rv = sheetURI->GetSpec(str);
|
2018-01-11 11:17:57 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-14 14:25:38 +03:00
|
|
|
CopyUTF8toUTF16(str, aHref);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aHref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::GetTitle(nsAString& aTitle) {
|
2018-05-08 19:53:51 +03:00
|
|
|
// From https://drafts.csswg.org/cssom/#dom-stylesheet-title:
|
|
|
|
//
|
|
|
|
// The title attribute must return the title or null if title is the empty
|
|
|
|
// string.
|
|
|
|
//
|
|
|
|
if (!mTitle.IsEmpty()) {
|
|
|
|
aTitle.Assign(mTitle);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aTitle);
|
|
|
|
}
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 02:19:17 +03:00
|
|
|
void StyleSheet::WillDirty() {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
if (IsComplete()) {
|
2017-05-16 02:19:17 +03:00
|
|
|
EnsureUniqueInner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 14:15:46 +03:00
|
|
|
void StyleSheet::AddStyleSet(ServoStyleSet* aStyleSet) {
|
2019-04-19 07:20:31 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mStyleSets.Contains(aStyleSet),
|
|
|
|
"style set already registered");
|
2017-05-16 02:27:15 +03:00
|
|
|
mStyleSets.AppendElement(aStyleSet);
|
|
|
|
}
|
|
|
|
|
2018-03-29 14:15:46 +03:00
|
|
|
void StyleSheet::DropStyleSet(ServoStyleSet* aStyleSet) {
|
2019-04-19 07:20:31 +03:00
|
|
|
bool found = mStyleSets.RemoveElement(aStyleSet);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(found, "didn't find style set");
|
2019-04-19 16:59:53 +03:00
|
|
|
#ifndef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
Unused << found;
|
|
|
|
#endif
|
2017-05-16 02:27:15 +03:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:01:51 +03:00
|
|
|
#define NOTIFY(function_, args_) \
|
|
|
|
do { \
|
|
|
|
StyleSheet* current = this; \
|
|
|
|
do { \
|
|
|
|
for (ServoStyleSet * handle : current->mStyleSets) { \
|
|
|
|
handle->function_ args_; \
|
|
|
|
} \
|
|
|
|
if (auto* shadow = current->GetContainingShadow()) { \
|
|
|
|
shadow->function_ args_; \
|
|
|
|
} \
|
|
|
|
current = current->mParent; \
|
|
|
|
} while (current); \
|
|
|
|
} while (0)
|
|
|
|
|
2017-05-16 02:19:17 +03:00
|
|
|
void StyleSheet::EnsureUniqueInner() {
|
|
|
|
MOZ_ASSERT(mInner->mSheets.Length() != 0, "unexpected number of outers");
|
2019-04-29 08:34:06 +03:00
|
|
|
|
|
|
|
if (IsReadOnly()) {
|
|
|
|
// Sheets that can't be modified don't need a unique inner.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
mState |= State::ForcedUniqueInner;
|
2017-05-16 02:19:17 +03:00
|
|
|
|
2017-10-12 10:56:02 +03:00
|
|
|
if (HasUniqueInner()) {
|
2017-05-16 02:19:17 +03:00
|
|
|
// already unique
|
|
|
|
return;
|
|
|
|
}
|
2017-11-28 03:50:45 +03:00
|
|
|
|
2017-05-16 02:19:17 +03:00
|
|
|
StyleSheetInfo* clone = mInner->CloneFor(this);
|
|
|
|
MOZ_ASSERT(clone);
|
|
|
|
mInner->RemoveSheet(this);
|
|
|
|
mInner = clone;
|
2017-05-19 00:21:11 +03:00
|
|
|
|
2018-03-23 19:01:34 +03:00
|
|
|
// Fixup the child lists and parent links in the Servo sheet. This is done
|
|
|
|
// here instead of in StyleSheetInner::CloneFor, because it's just more
|
|
|
|
// convenient to do so instead.
|
2018-04-30 18:50:03 +03:00
|
|
|
BuildChildListAfterInnerClone();
|
2017-05-16 02:30:10 +03:00
|
|
|
|
|
|
|
// let our containing style sets know that if we call
|
|
|
|
// nsPresContext::EnsureSafeToHandOutCSSRules we will need to restyle the
|
|
|
|
// document
|
2019-05-22 09:01:51 +03:00
|
|
|
NOTIFY(StyleSheetCloned, (*this));
|
2017-05-16 02:19:17 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 03:11:08 +03:00
|
|
|
void StyleSheet::AppendAllChildSheets(nsTArray<StyleSheet*>& aArray) {
|
|
|
|
for (StyleSheet* child = GetFirstChild(); child; child = child->mNext) {
|
|
|
|
aArray.AppendElement(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// WebIDL CSSStyleSheet API
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
dom::CSSRuleList* StyleSheet::GetCssRules(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2016-10-14 14:25:38 +03:00
|
|
|
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-04-30 19:08:40 +03:00
|
|
|
return GetCssRulesInternal();
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
2017-06-29 02:51:46 +03:00
|
|
|
void StyleSheet::GetSourceMapURL(nsAString& aSourceMapURL) {
|
2017-08-09 22:33:24 +03:00
|
|
|
if (mInner->mSourceMapURL.IsEmpty()) {
|
|
|
|
aSourceMapURL = mInner->mSourceMapURLFromComment;
|
|
|
|
} else {
|
|
|
|
aSourceMapURL = mInner->mSourceMapURL;
|
|
|
|
}
|
2017-06-29 02:51:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::SetSourceMapURL(const nsAString& aSourceMapURL) {
|
|
|
|
mInner->mSourceMapURL = aSourceMapURL;
|
|
|
|
}
|
|
|
|
|
2017-08-09 22:33:24 +03:00
|
|
|
void StyleSheet::SetSourceMapURLFromComment(
|
|
|
|
const nsAString& aSourceMapURLFromComment) {
|
|
|
|
mInner->mSourceMapURLFromComment = aSourceMapURLFromComment;
|
|
|
|
}
|
|
|
|
|
2017-09-14 23:59:32 +03:00
|
|
|
void StyleSheet::GetSourceURL(nsAString& aSourceURL) {
|
|
|
|
aSourceURL = mInner->mSourceURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::SetSourceURL(const nsAString& aSourceURL) {
|
|
|
|
mInner->mSourceURL = aSourceURL;
|
|
|
|
}
|
|
|
|
|
2017-05-30 04:10:25 +03:00
|
|
|
css::Rule* StyleSheet::GetDOMOwnerRule() const { return mOwnerRule; }
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
uint32_t StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2019-05-09 15:32:52 +03:00
|
|
|
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
2016-10-14 14:25:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2018-04-30 19:08:40 +03:00
|
|
|
return InsertRuleInternal(aRule, aIndex, aRv);
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::DeleteRule(uint32_t aIndex, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2019-05-09 15:32:52 +03:00
|
|
|
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
2016-10-14 14:25:38 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-04-30 19:08:40 +03:00
|
|
|
return DeleteRuleInternal(aIndex, aRv);
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 15:32:52 +03:00
|
|
|
int32_t StyleSheet::AddRule(const nsAString& aSelector, const nsAString& aBlock,
|
|
|
|
const Optional<uint32_t>& aIndex,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString rule;
|
|
|
|
rule.Append(aSelector);
|
|
|
|
rule.AppendLiteral(" { ");
|
|
|
|
if (!aBlock.IsEmpty()) {
|
|
|
|
rule.Append(aBlock);
|
|
|
|
rule.Append(' ');
|
|
|
|
}
|
|
|
|
rule.Append('}');
|
|
|
|
|
|
|
|
auto index =
|
|
|
|
aIndex.WasPassed() ? aIndex.Value() : GetCssRulesInternal()->Length();
|
|
|
|
|
|
|
|
InsertRuleInternal(rule, index, aRv);
|
|
|
|
// Always return -1.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-08 10:04:15 +03:00
|
|
|
nsresult StyleSheet::DeleteRuleFromGroup(css::GroupRule* aGroup,
|
|
|
|
uint32_t aIndex) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aGroup);
|
|
|
|
NS_ASSERTION(IsComplete(), "No deleting from an incomplete sheet!");
|
|
|
|
RefPtr<css::Rule> rule = aGroup->GetStyleRuleAt(aIndex);
|
|
|
|
NS_ENSURE_TRUE(rule, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
|
|
|
|
// check that the rule actually belongs to this sheet!
|
|
|
|
if (this != rule->GetStyleSheet()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:34:06 +03:00
|
|
|
if (IsReadOnly()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-08 10:04:15 +03:00
|
|
|
WillDirty();
|
|
|
|
|
|
|
|
nsresult result = aGroup->DeleteStyleRuleAt(aIndex);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
2018-06-29 05:56:09 +03:00
|
|
|
rule->DropReferences();
|
|
|
|
|
2017-11-28 03:50:45 +03:00
|
|
|
RuleRemoved(*rule);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-17 00:56:05 +03:00
|
|
|
dom::ShadowRoot* StyleSheet::GetContainingShadow() const {
|
|
|
|
if (!mOwningNode || !mOwningNode->IsContent()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mOwningNode->AsContent()->GetContainingShadow();
|
|
|
|
}
|
|
|
|
|
2017-11-28 03:50:45 +03:00
|
|
|
void StyleSheet::RuleAdded(css::Rule& aRule) {
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
mState |= State::ModifiedRules;
|
2018-04-30 19:19:44 +03:00
|
|
|
NOTIFY(RuleAdded, (*this, aRule));
|
2017-03-08 10:04:15 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = GetComposedDoc()) {
|
2018-05-11 13:57:38 +03:00
|
|
|
doc->StyleRuleAdded(this, &aRule);
|
2017-03-08 10:04:15 +03:00
|
|
|
}
|
2017-11-28 03:50:45 +03:00
|
|
|
}
|
2017-03-08 10:04:15 +03:00
|
|
|
|
2017-11-28 03:50:45 +03:00
|
|
|
void StyleSheet::RuleRemoved(css::Rule& aRule) {
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
mState |= State::ModifiedRules;
|
2018-04-30 19:19:44 +03:00
|
|
|
NOTIFY(RuleRemoved, (*this, aRule));
|
2017-11-28 03:50:45 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = GetComposedDoc()) {
|
2018-05-11 13:57:38 +03:00
|
|
|
doc->StyleRuleRemoved(this, &aRule);
|
2017-11-28 03:50:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::RuleChanged(css::Rule* aRule) {
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
mState |= State::ModifiedRules;
|
2018-04-30 19:19:44 +03:00
|
|
|
NOTIFY(RuleChanged, (*this, aRule));
|
2017-11-28 03:50:45 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = GetComposedDoc()) {
|
2018-05-11 13:57:38 +03:00
|
|
|
doc->StyleRuleChanged(this, aRule);
|
2017-11-28 03:50:45 +03:00
|
|
|
}
|
2017-03-08 10:04:15 +03:00
|
|
|
}
|
|
|
|
|
2018-03-21 08:31:23 +03:00
|
|
|
#undef NOTIFY
|
|
|
|
|
2017-03-08 10:04:15 +03:00
|
|
|
nsresult StyleSheet::InsertRuleIntoGroup(const nsAString& aRule,
|
|
|
|
css::GroupRule* aGroup,
|
|
|
|
uint32_t aIndex) {
|
|
|
|
NS_ASSERTION(IsComplete(), "No inserting into an incomplete sheet!");
|
|
|
|
// check that the group actually belongs to this sheet!
|
|
|
|
if (this != aGroup->GetStyleSheet()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:34:06 +03:00
|
|
|
if (IsReadOnly()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-08 10:04:15 +03:00
|
|
|
WillDirty();
|
|
|
|
|
2018-04-30 19:19:44 +03:00
|
|
|
nsresult result = InsertRuleIntoGroupInternal(aRule, aGroup, aIndex);
|
2017-03-08 10:04:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2017-11-28 03:50:45 +03:00
|
|
|
RuleAdded(*aGroup->GetStyleRuleAt(aIndex));
|
2017-03-08 10:04:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-11 00:52:00 +03:00
|
|
|
uint64_t StyleSheet::FindOwningWindowInnerID() const {
|
|
|
|
uint64_t windowID = 0;
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = GetAssociatedDocument()) {
|
2018-05-11 13:57:38 +03:00
|
|
|
windowID = doc->InnerWindowID();
|
2017-07-11 00:52:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (windowID == 0 && mOwningNode) {
|
|
|
|
windowID = mOwningNode->OwnerDoc()->InnerWindowID();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<css::Rule> ownerRule;
|
|
|
|
if (windowID == 0 && (ownerRule = GetDOMOwnerRule())) {
|
|
|
|
RefPtr<StyleSheet> sheet = ownerRule->GetStyleSheet();
|
|
|
|
if (sheet) {
|
|
|
|
windowID = sheet->FindOwningWindowInnerID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (windowID == 0 && mParent) {
|
|
|
|
windowID = mParent->FindOwningWindowInnerID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return windowID;
|
|
|
|
}
|
|
|
|
|
2017-01-24 03:37:59 +03:00
|
|
|
void StyleSheet::UnparentChildren() {
|
|
|
|
// XXXbz this is a little bogus; see the XXX comment where we
|
|
|
|
// declare mFirstChild in StyleSheetInfo.
|
|
|
|
for (StyleSheet* child = GetFirstChild(); child; child = child->mNext) {
|
|
|
|
if (child->mParent == this) {
|
|
|
|
child->mParent = nullptr;
|
2018-05-11 13:57:38 +03:00
|
|
|
MOZ_ASSERT(child->mAssociationMode == NotOwnedByDocumentOrShadowRoot,
|
2017-04-24 22:44:19 +03:00
|
|
|
"How did we get to the destructor, exactly, if we're owned "
|
|
|
|
"by a document?");
|
2018-05-11 13:57:38 +03:00
|
|
|
child->mDocumentOrShadowRoot = nullptr;
|
2017-01-24 03:37:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
void StyleSheet::SubjectSubsumesInnerPrincipal(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2018-04-30 20:47:12 +03:00
|
|
|
StyleSheetInfo& info = Inner();
|
2016-10-14 14:25:38 +03:00
|
|
|
|
|
|
|
if (aSubjectPrincipal.Subsumes(info.mPrincipal)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-29 10:03:01 +03:00
|
|
|
// Allow access only if CORS mode is not NONE and the security flag
|
|
|
|
// is not turned off.
|
|
|
|
if (GetCORSMode() == CORS_NONE && !nsContentUtils::BypassCSSOMOriginCheck()) {
|
2016-10-14 14:25:38 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we set the principal of our inner to the subjectPrincipal.
|
|
|
|
// We do this because we're in a situation where the caller would not normally
|
|
|
|
// be able to access the sheet, but the sheet has opted in to being read.
|
|
|
|
// Unfortunately, that means it's also opted in to being _edited_, and if the
|
|
|
|
// caller now makes edits to the sheet we want the resulting resource loads,
|
|
|
|
// if any, to look as if they are coming from the caller's principal, not the
|
|
|
|
// original sheet principal.
|
|
|
|
//
|
|
|
|
// That means we need a unique inner, of course. But we don't want to do that
|
|
|
|
// if we're not complete yet. Luckily, all the callers of this method throw
|
|
|
|
// anyway if not complete, so we can just do that here too.
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
if (!IsComplete()) {
|
2016-10-14 14:25:38 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WillDirty();
|
|
|
|
|
|
|
|
info.mPrincipal = &aSubjectPrincipal;
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
bool StyleSheet::AreRulesAvailable(nsIPrincipal& aSubjectPrincipal,
|
2016-10-14 14:25:38 +03:00
|
|
|
ErrorResult& aRv) {
|
|
|
|
// Rules are not available on incomplete sheets.
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
if (!IsComplete()) {
|
2016-10-14 14:25:38 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//-- Security check: Only scripts whose principal subsumes that of the
|
|
|
|
// style sheet can access rule collections.
|
|
|
|
SubjectSubsumesInnerPrincipal(aSubjectPrincipal, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:52:56 +03:00
|
|
|
StyleSheet* StyleSheet::GetFirstChild() const { return Inner().mFirstChild; }
|
|
|
|
|
2018-05-11 13:57:38 +03:00
|
|
|
void StyleSheet::SetAssociatedDocumentOrShadowRoot(
|
|
|
|
DocumentOrShadowRoot* aDocOrShadowRoot, AssociationMode aAssociationMode) {
|
|
|
|
MOZ_ASSERT(aDocOrShadowRoot ||
|
|
|
|
aAssociationMode == NotOwnedByDocumentOrShadowRoot);
|
2017-01-25 00:12:40 +03:00
|
|
|
|
|
|
|
// not ref counted
|
2018-05-11 13:57:38 +03:00
|
|
|
mDocumentOrShadowRoot = aDocOrShadowRoot;
|
|
|
|
mAssociationMode = aAssociationMode;
|
2017-01-25 00:12:40 +03:00
|
|
|
|
|
|
|
// 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 (StyleSheet* child = GetFirstChild(); child; child = child->mNext) {
|
|
|
|
if (child->mParent == this) {
|
2018-05-11 13:57:38 +03:00
|
|
|
child->SetAssociatedDocumentOrShadowRoot(aDocOrShadowRoot,
|
|
|
|
aAssociationMode);
|
2017-01-25 00:12:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 04:10:25 +03:00
|
|
|
void StyleSheet::PrependStyleSheet(StyleSheet* aSheet) {
|
2017-01-24 03:23:40 +03:00
|
|
|
WillDirty();
|
2017-06-29 20:09:56 +03:00
|
|
|
PrependStyleSheetSilently(aSheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::PrependStyleSheetSilently(StyleSheet* aSheet) {
|
|
|
|
MOZ_ASSERT(aSheet);
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
2017-06-29 20:09:56 +03:00
|
|
|
|
2018-04-30 20:47:12 +03:00
|
|
|
aSheet->mNext = Inner().mFirstChild;
|
|
|
|
Inner().mFirstChild = aSheet;
|
2017-01-24 03:23:40 +03:00
|
|
|
|
|
|
|
// This is not reference counted. Our parent tells us when
|
|
|
|
// it's going away.
|
|
|
|
aSheet->mParent = this;
|
2018-05-11 13:57:38 +03:00
|
|
|
aSheet->SetAssociatedDocumentOrShadowRoot(mDocumentOrShadowRoot,
|
|
|
|
mAssociationMode);
|
2017-01-24 03:23:40 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 03:42:31 +03:00
|
|
|
size_t StyleSheet::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = 0;
|
|
|
|
const StyleSheet* s = this;
|
|
|
|
while (s) {
|
|
|
|
n += aMallocSizeOf(s);
|
|
|
|
|
2018-04-30 18:50:03 +03:00
|
|
|
// See the comment in CSSStyleSheet::SizeOfIncludingThis() for an
|
|
|
|
// explanation of this.
|
|
|
|
//
|
|
|
|
// FIXME(emilio): This comment is gone, someone should go find it.
|
2018-04-30 20:47:12 +03:00
|
|
|
if (s->Inner().mSheets.LastElement() == s) {
|
|
|
|
n += s->Inner().SizeOfIncludingThis(aMallocSizeOf);
|
2018-04-30 18:50:03 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 03:42:31 +03:00
|
|
|
// Measurement of the following members may be added later if DMD finds it
|
|
|
|
// is worthwhile:
|
|
|
|
// - s->mTitle
|
|
|
|
// - s->mMedia
|
2017-05-16 02:27:15 +03:00
|
|
|
// - s->mStyleSets
|
2018-04-30 18:50:03 +03:00
|
|
|
// - s->mRuleList
|
2017-01-24 03:42:31 +03:00
|
|
|
|
|
|
|
s = s->mNext;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-01-24 03:40:28 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void StyleSheet::List(FILE* out, int32_t aIndent) const {
|
|
|
|
int32_t index;
|
|
|
|
|
|
|
|
// Indent
|
|
|
|
nsAutoCString str;
|
|
|
|
for (index = aIndent; --index >= 0;) {
|
|
|
|
str.AppendLiteral(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
str.AppendLiteral("CSS Style Sheet: ");
|
|
|
|
nsAutoCString urlSpec;
|
|
|
|
nsresult rv = GetSheetURI()->GetSpec(urlSpec);
|
|
|
|
if (NS_SUCCEEDED(rv) && !urlSpec.IsEmpty()) {
|
|
|
|
str.Append(urlSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMedia) {
|
|
|
|
str.AppendLiteral(" media: ");
|
|
|
|
nsAutoString buffer;
|
|
|
|
mMedia->GetText(buffer);
|
|
|
|
AppendUTF16toUTF8(buffer, str);
|
|
|
|
}
|
|
|
|
str.Append('\n');
|
|
|
|
fprintf_stderr(out, "%s", str.get());
|
|
|
|
|
|
|
|
for (const StyleSheet* child = GetFirstChild(); child; child = child->mNext) {
|
|
|
|
child->List(out, aIndent + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-09 09:28:24 +03:00
|
|
|
void StyleSheet::SetMedia(dom::MediaList* aMedia) {
|
2017-11-26 23:07:14 +03:00
|
|
|
if (aMedia) {
|
|
|
|
aMedia->SetStyleSheet(this);
|
|
|
|
}
|
2017-01-06 10:05:24 +03:00
|
|
|
mMedia = aMedia;
|
|
|
|
}
|
|
|
|
|
2018-09-17 08:35:26 +03:00
|
|
|
void StyleSheet::SetReferrerPolicy(net::ReferrerPolicy aReferrerPolicy) {
|
|
|
|
Inner().mReferrerPolicy = aReferrerPolicy;
|
|
|
|
}
|
|
|
|
|
2017-01-06 10:05:24 +03:00
|
|
|
void StyleSheet::DropMedia() {
|
|
|
|
if (mMedia) {
|
|
|
|
mMedia->SetStyleSheet(nullptr);
|
|
|
|
mMedia = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::MediaList* StyleSheet::Media() {
|
|
|
|
if (!mMedia) {
|
2018-03-28 18:31:46 +03:00
|
|
|
mMedia = dom::MediaList::Create(nsString());
|
2017-01-06 10:05:24 +03:00
|
|
|
mMedia->SetStyleSheet(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mMedia;
|
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
// nsWrapperCache
|
|
|
|
|
|
|
|
JSObject* StyleSheet::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return dom::CSSStyleSheet_Binding::Wrap(aCx, this, aGivenProto);
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
bool StyleSheet::RuleHasPendingChildSheet(css::Rule* aRule) {
|
2018-06-26 00:20:54 +03:00
|
|
|
MOZ_ASSERT(aRule->Type() == dom::CSSRule_Binding::IMPORT_RULE);
|
2017-05-30 04:10:25 +03:00
|
|
|
auto rule = static_cast<dom::CSSImportRule*>(aRule);
|
|
|
|
if (StyleSheet* childSheet = rule->GetStyleSheet()) {
|
|
|
|
return !childSheet->IsComplete();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:09:30 +03:00
|
|
|
void StyleSheet::BuildChildListAfterInnerClone() {
|
2018-04-30 20:47:12 +03:00
|
|
|
MOZ_ASSERT(Inner().mSheets.Length() == 1, "Should've just cloned");
|
|
|
|
MOZ_ASSERT(Inner().mSheets[0] == this);
|
|
|
|
MOZ_ASSERT(!Inner().mFirstChild);
|
2018-04-30 20:09:30 +03:00
|
|
|
|
2018-04-30 20:47:12 +03:00
|
|
|
auto* contents = Inner().mContents.get();
|
2018-04-30 20:09:30 +03:00
|
|
|
RefPtr<ServoCssRules> rules = Servo_StyleSheet_GetRules(contents).Consume();
|
|
|
|
|
|
|
|
uint32_t index = 0;
|
|
|
|
while (true) {
|
|
|
|
uint32_t line, column; // Actually unused.
|
|
|
|
RefPtr<RawServoImportRule> import =
|
|
|
|
Servo_CssRules_GetImportRuleAt(rules, index, &line, &column).Consume();
|
|
|
|
if (!import) {
|
|
|
|
// Note that only @charset rules come before @import rules, and @charset
|
|
|
|
// rules are parsed but skipped, so we can stop iterating as soon as we
|
|
|
|
// find something that isn't an @import rule.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto* sheet = const_cast<StyleSheet*>(Servo_ImportRule_GetSheet(import));
|
|
|
|
MOZ_ASSERT(sheet);
|
|
|
|
PrependStyleSheetSilently(sheet);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<StyleSheet> StyleSheet::CreateEmptyChildSheet(
|
|
|
|
already_AddRefed<dom::MediaList> aMediaList) const {
|
|
|
|
RefPtr<StyleSheet> child = new StyleSheet(ParsingMode(), CORSMode::CORS_NONE,
|
|
|
|
GetReferrerPolicy(), SRIMetadata());
|
|
|
|
|
|
|
|
child->mMedia = aMediaList;
|
|
|
|
return child.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We disable parallel stylesheet parsing if any of the following three
|
|
|
|
// conditions hold:
|
|
|
|
//
|
|
|
|
// (1) The pref is off.
|
2018-11-28 01:08:11 +03:00
|
|
|
// (2) The browser is recording CSS errors (which parallel parsing can't
|
2018-12-05 21:44:05 +03:00
|
|
|
// handle).
|
|
|
|
// (3) The stylesheet is a chrome stylesheet, since those can use
|
|
|
|
// -moz-bool-pref, which needs to access the pref service, which is not
|
|
|
|
// threadsafe.
|
2018-04-30 20:09:30 +03:00
|
|
|
static bool AllowParallelParse(css::Loader* aLoader, nsIURI* aSheetURI) {
|
|
|
|
// Check the pref.
|
|
|
|
if (!StaticPrefs::layout_css_parsing_parallel()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the browser is recording CSS errors, we need to use the sequential path
|
|
|
|
// because the parallel path doesn't support that.
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aLoader->GetDocument();
|
2018-04-30 20:09:30 +03:00
|
|
|
if (doc && css::ErrorReporter::ShouldReportErrors(*doc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a chrome stylesheet, it might use -moz-bool-pref, which needs to
|
|
|
|
// access the pref service, which is not thread-safe. We could probably expose
|
|
|
|
// the relevant booleans as thread-safe var caches if we needed to, but
|
|
|
|
// parsing chrome stylesheets in parallel is unlikely to be a win anyway.
|
|
|
|
//
|
|
|
|
// Note that UA stylesheets can also use -moz-bool-pref, but those are always
|
|
|
|
// parsed sync.
|
|
|
|
if (dom::IsChromeURI(aSheetURI)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<StyleSheetParsePromise> StyleSheet::ParseSheet(
|
2018-08-20 20:03:11 +03:00
|
|
|
css::Loader* aLoader, const nsACString& aBytes,
|
|
|
|
css::SheetLoadData* aLoadData) {
|
2018-04-30 20:09:30 +03:00
|
|
|
MOZ_ASSERT(aLoader);
|
|
|
|
MOZ_ASSERT(aLoadData);
|
|
|
|
MOZ_ASSERT(mParsePromise.IsEmpty());
|
|
|
|
RefPtr<StyleSheetParsePromise> p = mParsePromise.Ensure(__func__);
|
2019-03-30 03:15:43 +03:00
|
|
|
SetURLExtraData();
|
2018-04-30 20:09:30 +03:00
|
|
|
|
2018-08-20 20:03:11 +03:00
|
|
|
const StyleUseCounters* useCounters =
|
|
|
|
aLoader->GetDocument() ? aLoader->GetDocument()->GetStyleUseCounters()
|
|
|
|
: nullptr;
|
|
|
|
|
2018-04-30 20:09:30 +03:00
|
|
|
if (!AllowParallelParse(aLoader, GetSheetURI())) {
|
|
|
|
RefPtr<RawServoStyleSheetContents> contents =
|
|
|
|
Servo_StyleSheet_FromUTF8Bytes(
|
2018-04-30 20:47:12 +03:00
|
|
|
aLoader, this, aLoadData, &aBytes, mParsingMode, Inner().mURLData,
|
2018-04-30 20:09:30 +03:00
|
|
|
aLoadData->mLineNumber, aLoader->GetCompatibilityMode(),
|
2018-08-20 20:03:11 +03:00
|
|
|
/* reusable_sheets = */ nullptr, useCounters)
|
2018-04-30 20:09:30 +03:00
|
|
|
.Consume();
|
|
|
|
FinishAsyncParse(contents.forget());
|
|
|
|
} else {
|
|
|
|
RefPtr<css::SheetLoadDataHolder> loadDataHolder =
|
|
|
|
new css::SheetLoadDataHolder(__func__, aLoadData);
|
|
|
|
Servo_StyleSheet_FromUTF8BytesAsync(
|
2018-04-30 20:47:12 +03:00
|
|
|
loadDataHolder, Inner().mURLData, &aBytes, mParsingMode,
|
2018-08-20 20:03:11 +03:00
|
|
|
aLoadData->mLineNumber, aLoader->GetCompatibilityMode(),
|
2018-08-21 13:31:11 +03:00
|
|
|
/* should_record_counters = */ !!useCounters);
|
2018-04-30 20:09:30 +03:00
|
|
|
}
|
|
|
|
|
2018-06-01 18:59:07 +03:00
|
|
|
return p;
|
2018-04-30 20:09:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::FinishAsyncParse(
|
|
|
|
already_AddRefed<RawServoStyleSheetContents> aSheetContents) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mParsePromise.IsEmpty());
|
2018-04-30 20:47:12 +03:00
|
|
|
Inner().mContents = aSheetContents;
|
2018-04-30 20:09:30 +03:00
|
|
|
FinishParse();
|
|
|
|
mParsePromise.Resolve(true, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::ParseSheetSync(
|
|
|
|
css::Loader* aLoader, const nsACString& aBytes,
|
|
|
|
css::SheetLoadData* aLoadData, uint32_t aLineNumber,
|
|
|
|
css::LoaderReusableStyleSheets* aReusableSheets) {
|
|
|
|
nsCompatibility compatMode =
|
|
|
|
aLoader ? aLoader->GetCompatibilityMode() : eCompatibility_FullStandards;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-20 20:03:11 +03:00
|
|
|
const StyleUseCounters* useCounters =
|
|
|
|
aLoader && aLoader->GetDocument()
|
|
|
|
? aLoader->GetDocument()->GetStyleUseCounters()
|
|
|
|
: nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-30 03:15:43 +03:00
|
|
|
SetURLExtraData();
|
2018-04-30 20:47:12 +03:00
|
|
|
Inner().mContents =
|
|
|
|
Servo_StyleSheet_FromUTF8Bytes(
|
|
|
|
aLoader, this, aLoadData, &aBytes, mParsingMode, Inner().mURLData,
|
2018-08-20 20:03:11 +03:00
|
|
|
aLineNumber, compatMode, aReusableSheets, useCounters)
|
2018-04-30 20:09:30 +03:00
|
|
|
.Consume();
|
|
|
|
|
|
|
|
FinishParse();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::FinishParse() {
|
|
|
|
nsString sourceMapURL;
|
2018-04-30 20:47:12 +03:00
|
|
|
Servo_StyleSheet_GetSourceMapURL(Inner().mContents, &sourceMapURL);
|
2018-04-30 20:09:30 +03:00
|
|
|
SetSourceMapURLFromComment(sourceMapURL);
|
|
|
|
|
|
|
|
nsString sourceURL;
|
2018-04-30 20:47:12 +03:00
|
|
|
Servo_StyleSheet_GetSourceURL(Inner().mContents, &sourceURL);
|
2018-04-30 20:09:30 +03:00
|
|
|
SetSourceURL(sourceURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult StyleSheet::ReparseSheet(const nsAString& aInput) {
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 11:55:10 +03:00
|
|
|
if (!IsComplete()) {
|
2018-04-30 20:09:30 +03:00
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
|
2019-03-12 18:17:54 +03:00
|
|
|
// Allowing to modify UA sheets is dangerous (in the sense that C++ code
|
|
|
|
// relies on rules in those sheets), plus they're probably going to be shared
|
|
|
|
// across processes in which case this is directly a no-go.
|
2019-04-29 08:34:06 +03:00
|
|
|
if (IsReadOnly()) {
|
|
|
|
return NS_OK;
|
2019-03-12 18:17:54 +03:00
|
|
|
}
|
|
|
|
|
2018-04-30 20:09:30 +03:00
|
|
|
// Hold strong ref to the CSSLoader in case the document update
|
|
|
|
// kills the document
|
|
|
|
RefPtr<css::Loader> loader;
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = GetAssociatedDocument()) {
|
2018-05-11 13:57:38 +03:00
|
|
|
loader = doc->CSSLoader();
|
2018-04-30 20:09:30 +03:00
|
|
|
NS_ASSERTION(loader, "Document with no CSS loader!");
|
|
|
|
} else {
|
|
|
|
loader = new css::Loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
WillDirty();
|
|
|
|
|
|
|
|
// cache child sheets to reuse
|
|
|
|
css::LoaderReusableStyleSheets reusableSheets;
|
|
|
|
for (StyleSheet* child = GetFirstChild(); child; child = child->mNext) {
|
|
|
|
if (child->GetOriginalURI()) {
|
|
|
|
reusableSheets.AddReusableSheet(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up child sheets list
|
|
|
|
for (StyleSheet* child = GetFirstChild(); child;) {
|
|
|
|
StyleSheet* next = child->mNext;
|
|
|
|
child->mParent = nullptr;
|
2018-05-11 13:57:38 +03:00
|
|
|
child->ClearAssociatedDocumentOrShadowRoot();
|
2018-04-30 20:09:30 +03:00
|
|
|
child->mNext = nullptr;
|
|
|
|
child = next;
|
|
|
|
}
|
2018-04-30 20:47:12 +03:00
|
|
|
Inner().mFirstChild = nullptr;
|
2018-04-30 20:09:30 +03:00
|
|
|
|
|
|
|
uint32_t lineNumber = 1;
|
|
|
|
if (mOwningNode) {
|
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement> link = do_QueryInterface(mOwningNode);
|
|
|
|
if (link) {
|
|
|
|
lineNumber = link->GetLineNumber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify to the stylesets about the old rules going away.
|
|
|
|
{
|
|
|
|
ServoCSSRuleList* ruleList = GetCssRulesInternal();
|
|
|
|
MOZ_ASSERT(ruleList);
|
|
|
|
|
|
|
|
uint32_t ruleCount = ruleList->Length();
|
|
|
|
for (uint32_t i = 0; i < ruleCount; ++i) {
|
|
|
|
css::Rule* rule = ruleList->GetRule(i);
|
|
|
|
MOZ_ASSERT(rule);
|
2018-06-26 00:20:54 +03:00
|
|
|
if (rule->Type() == dom::CSSRule_Binding::IMPORT_RULE &&
|
2018-04-30 20:09:30 +03:00
|
|
|
RuleHasPendingChildSheet(rule)) {
|
|
|
|
continue; // notify when loaded (see StyleSheetLoaded)
|
|
|
|
}
|
|
|
|
RuleRemoved(*rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DropRuleList();
|
|
|
|
|
|
|
|
ParseSheetSync(loader, NS_ConvertUTF16toUTF8(aInput),
|
|
|
|
/* aLoadData = */ nullptr, lineNumber, &reusableSheets);
|
|
|
|
|
|
|
|
// Notify the stylesets about the new rules.
|
|
|
|
{
|
|
|
|
// Get the rule list (which will need to be regenerated after ParseSheet).
|
|
|
|
ServoCSSRuleList* ruleList = GetCssRulesInternal();
|
|
|
|
MOZ_ASSERT(ruleList);
|
|
|
|
|
|
|
|
uint32_t ruleCount = ruleList->Length();
|
|
|
|
for (uint32_t i = 0; i < ruleCount; ++i) {
|
|
|
|
css::Rule* rule = ruleList->GetRule(i);
|
|
|
|
MOZ_ASSERT(rule);
|
2018-06-26 00:20:54 +03:00
|
|
|
if (rule->Type() == CSSRule_Binding::IMPORT_RULE &&
|
2018-04-30 20:09:30 +03:00
|
|
|
RuleHasPendingChildSheet(rule)) {
|
|
|
|
continue; // notify when loaded (see StyleSheetLoaded)
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleAdded(*rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our rules are no longer considered modified.
|
|
|
|
ClearModifiedRules();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsICSSLoaderObserver implementation
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::StyleSheetLoaded(StyleSheet* aSheet, bool aWasAlternate,
|
|
|
|
nsresult aStatus) {
|
|
|
|
if (!aSheet->GetParentSheet()) {
|
|
|
|
return NS_OK; // ignore if sheet has been detached already
|
|
|
|
}
|
|
|
|
NS_ASSERTION(this == aSheet->GetParentSheet(),
|
|
|
|
"We are being notified of a sheet load for a sheet that is not "
|
|
|
|
"our child!");
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
RuleAdded(*aSheet->GetOwnerRule());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::DropRuleList() {
|
|
|
|
if (mRuleList) {
|
2018-06-29 05:56:09 +03:00
|
|
|
mRuleList->DropReferences();
|
2018-04-30 20:09:30 +03:00
|
|
|
mRuleList = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<StyleSheet> StyleSheet::Clone(
|
|
|
|
StyleSheet* aCloneParent, dom::CSSImportRule* aCloneOwnerRule,
|
2018-05-11 13:57:38 +03:00
|
|
|
dom::DocumentOrShadowRoot* aCloneDocumentOrShadowRoot,
|
2018-04-30 20:09:30 +03:00
|
|
|
nsINode* aCloneOwningNode) const {
|
|
|
|
RefPtr<StyleSheet> clone =
|
|
|
|
new StyleSheet(*this, aCloneParent, aCloneOwnerRule,
|
2018-05-11 13:57:38 +03:00
|
|
|
aCloneDocumentOrShadowRoot, aCloneOwningNode);
|
2018-04-30 20:09:30 +03:00
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
ServoCSSRuleList* StyleSheet::GetCssRulesInternal() {
|
|
|
|
if (!mRuleList) {
|
|
|
|
EnsureUniqueInner();
|
|
|
|
|
|
|
|
RefPtr<ServoCssRules> rawRules =
|
2018-04-30 20:47:12 +03:00
|
|
|
Servo_StyleSheet_GetRules(Inner().mContents).Consume();
|
2018-04-30 20:09:30 +03:00
|
|
|
MOZ_ASSERT(rawRules);
|
2018-06-29 05:56:09 +03:00
|
|
|
mRuleList = new ServoCSSRuleList(rawRules.forget(), this, nullptr);
|
2018-04-30 20:09:30 +03:00
|
|
|
}
|
|
|
|
return mRuleList;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t StyleSheet::InsertRuleInternal(const nsAString& aRule, uint32_t aIndex,
|
|
|
|
ErrorResult& aRv) {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
|
|
|
|
2018-04-30 20:09:30 +03:00
|
|
|
// Ensure mRuleList is constructed.
|
|
|
|
GetCssRulesInternal();
|
|
|
|
|
|
|
|
aRv = mRuleList->InsertRule(aRule, aIndex);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX We may not want to get the rule when stylesheet change event
|
|
|
|
// is not enabled.
|
|
|
|
css::Rule* rule = mRuleList->GetRule(aIndex);
|
2018-06-26 00:20:54 +03:00
|
|
|
if (rule->Type() != CSSRule_Binding::IMPORT_RULE ||
|
2018-04-30 20:09:30 +03:00
|
|
|
!RuleHasPendingChildSheet(rule)) {
|
|
|
|
RuleAdded(*rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::DeleteRuleInternal(uint32_t aIndex, ErrorResult& aRv) {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
|
|
|
|
2018-04-30 20:09:30 +03:00
|
|
|
// Ensure mRuleList is constructed.
|
|
|
|
GetCssRulesInternal();
|
|
|
|
if (aIndex >= mRuleList->Length()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold a strong ref to the rule so it doesn't die when we remove it
|
|
|
|
// from the list. XXX We may not want to hold it if stylesheet change
|
|
|
|
// event is not enabled.
|
|
|
|
RefPtr<css::Rule> rule = mRuleList->GetRule(aIndex);
|
|
|
|
aRv = mRuleList->DeleteRule(aIndex);
|
|
|
|
MOZ_ASSERT(!aRv.ErrorCodeIs(NS_ERROR_DOM_INDEX_SIZE_ERR),
|
|
|
|
"IndexSizeError should have been handled earlier");
|
|
|
|
if (!aRv.Failed()) {
|
|
|
|
RuleRemoved(*rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult StyleSheet::InsertRuleIntoGroupInternal(const nsAString& aRule,
|
|
|
|
css::GroupRule* aGroup,
|
|
|
|
uint32_t aIndex) {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
|
|
|
|
2018-04-30 20:09:30 +03:00
|
|
|
auto rules = static_cast<ServoCSSRuleList*>(aGroup->CssRules());
|
|
|
|
MOZ_ASSERT(rules->GetParentRule() == aGroup);
|
|
|
|
return rules->InsertRule(aRule, aIndex);
|
|
|
|
}
|
|
|
|
|
2019-04-19 07:20:31 +03:00
|
|
|
StyleOrigin StyleSheet::GetOrigin() const {
|
|
|
|
return Servo_StyleSheet_GetOrigin(Inner().mContents);
|
2018-04-30 20:09:30 +03:00
|
|
|
}
|
|
|
|
|
2019-03-30 03:23:49 +03:00
|
|
|
void StyleSheet::SetSharedContents(nsLayoutStylesheetCache::Shm* aSharedMemory,
|
|
|
|
const ServoCssRules* aSharedRules) {
|
|
|
|
MOZ_ASSERT(aSharedMemory);
|
|
|
|
MOZ_ASSERT(!IsComplete());
|
|
|
|
|
|
|
|
SetURLExtraData();
|
|
|
|
|
|
|
|
// Hold a strong reference to the shared memory that aSharedRules comes
|
|
|
|
// from, so that we don't end up releasing the shared memory before the
|
|
|
|
// StyleSheetInner.
|
|
|
|
Inner().mSharedMemory = aSharedMemory;
|
|
|
|
|
|
|
|
Inner().mContents =
|
|
|
|
Servo_StyleSheet_FromSharedData(Inner().mURLData, aSharedRules).Consume();
|
|
|
|
|
|
|
|
// Don't call FinishParse(), since that tries to set source map URLs,
|
|
|
|
// which we don't have.
|
|
|
|
}
|
|
|
|
|
|
|
|
const ServoCssRules* StyleSheet::ToShared(
|
|
|
|
RawServoSharedMemoryBuilder* aBuilder) {
|
|
|
|
// Assert some things we assume when creating a StyleSheet using shared
|
|
|
|
// memory.
|
|
|
|
MOZ_ASSERT(GetReferrerPolicy() == net::RP_Unset);
|
|
|
|
MOZ_ASSERT(GetCORSMode() == CORS_NONE);
|
|
|
|
MOZ_ASSERT(Inner().mIntegrity.IsEmpty());
|
|
|
|
MOZ_ASSERT(nsContentUtils::IsSystemPrincipal(Principal()));
|
|
|
|
|
|
|
|
return Servo_SharedMemoryBuilder_AddStylesheet(aBuilder, Inner().mContents);
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:34:06 +03:00
|
|
|
bool StyleSheet::IsReadOnly() const {
|
|
|
|
return IsComplete() && GetOrigin() == StyleOrigin::UserAgent;
|
|
|
|
}
|
|
|
|
|
2016-02-26 04:51:01 +03:00
|
|
|
} // namespace mozilla
|