Bug 1826564 [wpt PR 39394] - Update mypy, a=testonly
[gecko.git] / layout / style / Rule.cpp
bloba107dc3f01b4fb29397fce40d692cf7e3b639310
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 */
9 #include "Rule.h"
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)
31 NS_INTERFACE_MAP_END
33 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(Rule)
35 bool Rule::IsCCLeaf() const { return !PreservingWrapper(); }
37 bool Rule::IsKnownLive() const {
38 if (HasKnownLiveWrapper()) {
39 return true;
42 StyleSheet* sheet = GetStyleSheet();
43 if (!sheet) {
44 return false;
47 Document* doc = sheet->GetKeptAliveByDocument();
48 return doc &&
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);
66 if (needDrop) {
67 DropJSObjects(this);
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
78 // methods.
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
86 /* virtual */
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; }
96 #ifdef DEBUG
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.
101 if (mParentRule) {
102 auto type = mParentRule->Type();
103 MOZ_ASSERT(type == StyleCssRuleType::Media ||
104 type == StyleCssRuleType::Document ||
105 type == StyleCssRuleType::Supports ||
106 type == StyleCssRuleType::Keyframes ||
107 type == StyleCssRuleType::LayerBlock ||
108 type == StyleCssRuleType::Container);
111 #endif
113 bool Rule::IsReadOnly() const {
114 MOZ_ASSERT(!mSheet || !mParentRule ||
115 mSheet->IsReadOnly() == mParentRule->IsReadOnly(),
116 "a parent rule should be read only iff the owning sheet is "
117 "read only");
118 return mSheet && mSheet->IsReadOnly();
121 bool Rule::IsIncompleteImportRule() const {
122 if (Type() != StyleCssRuleType::Import) {
123 return false;
125 auto* sheet = static_cast<const dom::CSSImportRule*>(this)->GetStyleSheet();
126 return !sheet || !sheet->IsComplete();
129 } // namespace mozilla::css