1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* base class for all rule types in a CSS style sheet */
11 #include "mozilla/css/GroupRule.h"
12 #include "mozilla/dom/CSSImportRule.h"
13 #include "mozilla/dom/DocumentOrShadowRoot.h"
14 #include "nsCCUncollectableMarker.h"
15 #include "mozilla/dom/Document.h"
16 #include "mozilla/HoldDropJSObjects.h"
17 #include "nsWrapperCacheInlines.h"
18 #include "mozilla/ServoBindings.h"
20 using namespace mozilla
;
21 using namespace mozilla::dom
;
23 namespace mozilla::css
{
25 NS_IMPL_CYCLE_COLLECTING_ADDREF(Rule
)
26 NS_IMPL_CYCLE_COLLECTING_RELEASE(Rule
)
28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Rule
)
29 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
30 NS_INTERFACE_MAP_ENTRY(nsISupports
)
33 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(Rule
)
35 bool Rule::IsCCLeaf() const { return !PreservingWrapper(); }
37 bool Rule::IsKnownLive() const {
38 if (HasKnownLiveWrapper()) {
42 StyleSheet
* sheet
= GetStyleSheet();
47 Document
* doc
= sheet
->GetKeptAliveByDocument();
49 nsCCUncollectableMarker::InGeneration(doc
->GetMarkedCCGeneration());
52 void Rule::UnlinkDeclarationWrapper(nsWrapperCache
& aDecl
) {
53 // We have to be a bit careful here. We have two separate nsWrapperCache
54 // instances, aDecl and this, that both correspond to the same CC participant:
55 // this. If we just used ReleaseWrapper() on one of them, that would
56 // unpreserve that one wrapper, then trace us with a tracer that clears JS
57 // things, and we would clear the wrapper on the cache that has not
58 // unpreserved the wrapper yet. That would violate the invariant that the
59 // cache keeps caching the wrapper until the wrapper dies.
61 // So we reimplement a modified version of nsWrapperCache::ReleaseWrapper here
62 // that unpreserves both wrappers before doing any clearing.
63 bool needDrop
= PreservingWrapper() || aDecl
.PreservingWrapper();
64 SetPreservingWrapper(false);
65 aDecl
.SetPreservingWrapper(false);
71 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(Rule
)
72 return tmp
->IsCCLeaf() || tmp
->IsKnownLive();
73 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
75 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(Rule
)
76 // Please see documentation for nsCycleCollectionParticipant::CanSkip* for why
77 // we need to check HasNothingToTrace here but not in the other two CanSkip
79 return tmp
->IsCCLeaf() || (tmp
->IsKnownLive() && tmp
->HasNothingToTrace(tmp
));
80 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
82 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(Rule
)
83 return tmp
->IsCCLeaf() || tmp
->IsKnownLive();
84 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
87 void Rule::DropSheetReference() { mSheet
= nullptr; }
89 void Rule::SetCssText(const nsACString
& aCssText
) {
90 // We used to throw for some rule types, but not all. Specifically, we did
91 // not throw for StyleRule. Let's just always not throw.
94 Rule
* Rule::GetParentRule() const { return mParentRule
; }
97 void Rule::AssertParentRuleType() {
98 // Would be nice to check that this->Type() is KEYFRAME_RULE when
99 // mParentRule->Tye() is KEYFRAMES_RULE, but we can't call
100 // this->Type() here since it's virtual.
102 auto type
= mParentRule
->Type();
103 MOZ_ASSERT(type
== StyleCssRuleType::Media
||
104 type
== StyleCssRuleType::Style
||
105 type
== StyleCssRuleType::Document
||
106 type
== StyleCssRuleType::Supports
||
107 type
== StyleCssRuleType::Keyframes
||
108 type
== StyleCssRuleType::LayerBlock
||
109 type
== StyleCssRuleType::Container
);
114 bool Rule::IsReadOnly() const {
115 MOZ_ASSERT(!mSheet
|| !mParentRule
||
116 mSheet
->IsReadOnly() == mParentRule
->IsReadOnly(),
117 "a parent rule should be read only iff the owning sheet is "
119 return mSheet
&& mSheet
->IsReadOnly();
122 bool Rule::IsIncompleteImportRule() const {
123 if (Type() != StyleCssRuleType::Import
) {
126 auto* sheet
= static_cast<const dom::CSSImportRule
*>(this)->GetStyleSheet();
127 return !sheet
|| !sheet
->IsComplete();
130 } // namespace mozilla::css