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/. */
6 
7 #include "mozilla/dom/CSSPageRule.h"
8 #include "mozilla/dom/CSSPageRuleBinding.h"
9 
10 #include "mozilla/DeclarationBlock.h"
11 #include "mozilla/ServoBindings.h"
12 
13 namespace mozilla {
14 namespace dom {
15 
16 // -- CSSPageRuleDeclaration ---------------------------------------
17 
CSSPageRuleDeclaration(already_AddRefed<RawServoDeclarationBlock> aDecls)18 CSSPageRuleDeclaration::CSSPageRuleDeclaration(
19     already_AddRefed<RawServoDeclarationBlock> aDecls)
20     : mDecls(new DeclarationBlock(std::move(aDecls))) {}
21 
~CSSPageRuleDeclaration()22 CSSPageRuleDeclaration::~CSSPageRuleDeclaration() {
23   mDecls->SetOwningRule(nullptr);
24 }
25 
26 // QueryInterface implementation for CSSPageRuleDeclaration
27 NS_INTERFACE_MAP_BEGIN(CSSPageRuleDeclaration)
28   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
29   // We forward the cycle collection interfaces to Rule(), which is
30   // never null (in fact, we're part of that object!)
31   if (aIID.Equals(NS_GET_IID(nsCycleCollectionISupports)) ||
32       aIID.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant))) {
33     return Rule()->QueryInterface(aIID, aInstancePtr);
34   }
35 NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration)
36 
NS_IMPL_ADDREF_USING_AGGREGATOR(CSSPageRuleDeclaration,Rule ())37 NS_IMPL_ADDREF_USING_AGGREGATOR(CSSPageRuleDeclaration, Rule())
38 NS_IMPL_RELEASE_USING_AGGREGATOR(CSSPageRuleDeclaration, Rule())
39 
40 /* nsDOMCSSDeclaration implementation */
41 
42 css::Rule* CSSPageRuleDeclaration::GetParentRule() { return Rule(); }
43 
GetAssociatedNode() const44 nsINode* CSSPageRuleDeclaration::GetAssociatedNode() const {
45   return Rule()->GetAssociatedDocumentOrShadowRoot();
46 }
47 
GetParentObject() const48 nsISupports* CSSPageRuleDeclaration::GetParentObject() const {
49   return Rule()->GetParentObject();
50 }
51 
GetOrCreateCSSDeclaration(Operation aOperation,DeclarationBlock ** aCreated)52 DeclarationBlock* CSSPageRuleDeclaration::GetOrCreateCSSDeclaration(
53     Operation aOperation, DeclarationBlock** aCreated) {
54   return mDecls;
55 }
56 
SetCSSDeclaration(DeclarationBlock * aDecl,MutationClosureData * aClosureData)57 nsresult CSSPageRuleDeclaration::SetCSSDeclaration(
58     DeclarationBlock* aDecl, MutationClosureData* aClosureData) {
59   MOZ_ASSERT(aDecl, "must be non-null");
60   CSSPageRule* rule = Rule();
61 
62   if (rule->IsReadOnly()) {
63     return NS_OK;
64   }
65 
66   if (aDecl != mDecls) {
67     mDecls->SetOwningRule(nullptr);
68     RefPtr<DeclarationBlock> decls = aDecl;
69     Servo_PageRule_SetStyle(rule->Raw(), decls->Raw());
70     mDecls = std::move(decls);
71     mDecls->SetOwningRule(rule);
72   }
73 
74   return NS_OK;
75 }
76 
77 nsDOMCSSDeclaration::ParsingEnvironment
GetParsingEnvironment(nsIPrincipal * aSubjectPrincipal) const78 CSSPageRuleDeclaration::GetParsingEnvironment(
79     nsIPrincipal* aSubjectPrincipal) const {
80   return GetParsingEnvironmentForRule(Rule(), CSSRule_Binding::PAGE_RULE);
81 }
82 
83 // -- CSSPageRule --------------------------------------------------
84 
CSSPageRule(RefPtr<RawServoPageRule> aRawRule,StyleSheet * aSheet,css::Rule * aParentRule,uint32_t aLine,uint32_t aColumn)85 CSSPageRule::CSSPageRule(RefPtr<RawServoPageRule> aRawRule, StyleSheet* aSheet,
86                          css::Rule* aParentRule, uint32_t aLine,
87                          uint32_t aColumn)
88     : css::Rule(aSheet, aParentRule, aLine, aColumn),
89       mRawRule(std::move(aRawRule)),
90       mDecls(Servo_PageRule_GetStyle(mRawRule).Consume()) {}
91 
92 NS_IMPL_ADDREF_INHERITED(CSSPageRule, css::Rule)
93 NS_IMPL_RELEASE_INHERITED(CSSPageRule, css::Rule)
94 
95 // QueryInterface implementation for PageRule
96 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSPageRule)
97 NS_INTERFACE_MAP_END_INHERITING(css::Rule)
98 
99 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSPageRule)
100 
101 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(CSSPageRule, css::Rule)
102   // Keep this in sync with IsCCLeaf.
103 
104   // Trace the wrapper for our declaration.  This just expands out
105   // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
106   // directly because the wrapper is on the declaration, not on us.
107   tmp->mDecls.TraceWrapper(aCallbacks, aClosure);
108 NS_IMPL_CYCLE_COLLECTION_TRACE_END
109 
110 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSPageRule)
111   // Keep this in sync with IsCCLeaf.
112 
113   // Unlink the wrapper for our declaration.
114   //
115   // Note that this has to happen before unlinking css::Rule.
116   tmp->UnlinkDeclarationWrapper(tmp->mDecls);
117   tmp->mDecls.mDecls->SetOwningRule(nullptr);
118 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::Rule)
119 
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSPageRule,css::Rule)120 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSPageRule, css::Rule)
121   // Keep this in sync with IsCCLeaf.
122 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
123 
124 bool CSSPageRule::IsCCLeaf() const {
125   if (!Rule::IsCCLeaf()) {
126     return false;
127   }
128 
129   return !mDecls.PreservingWrapper();
130 }
131 
SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const132 size_t CSSPageRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
133   // TODO Implement this!
134   return aMallocSizeOf(this);
135 }
136 
137 #ifdef DEBUG
List(FILE * out,int32_t aIndent) const138 void CSSPageRule::List(FILE* out, int32_t aIndent) const {
139   nsAutoCString str;
140   for (int32_t i = 0; i < aIndent; i++) {
141     str.AppendLiteral("  ");
142   }
143   Servo_PageRule_Debug(mRawRule, &str);
144   fprintf_stderr(out, "%s\n", str.get());
145 }
146 #endif
147 
148 /* CSSRule implementation */
149 
GetCssText(nsACString & aCssText) const150 void CSSPageRule::GetCssText(nsACString& aCssText) const {
151   Servo_PageRule_GetCssText(mRawRule, &aCssText);
152 }
153 
154 /* CSSPageRule implementation */
155 
Style()156 nsICSSDeclaration* CSSPageRule::Style() { return &mDecls; }
157 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)158 JSObject* CSSPageRule::WrapObject(JSContext* aCx,
159                                   JS::Handle<JSObject*> aGivenProto) {
160   return CSSPageRule_Binding::Wrap(aCx, this, aGivenProto);
161 }
162 
163 }  // namespace dom
164 }  // namespace mozilla
165