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/CSSMediaRule.h"
8 
9 #include "mozilla/dom/CSSMediaRuleBinding.h"
10 #include "mozilla/dom/MediaList.h"
11 #include "mozilla/ServoBindings.h"
12 
13 namespace mozilla::dom {
14 
CSSMediaRule(RefPtr<RawServoMediaRule> aRawRule,StyleSheet * aSheet,css::Rule * aParentRule,uint32_t aLine,uint32_t aColumn)15 CSSMediaRule::CSSMediaRule(RefPtr<RawServoMediaRule> aRawRule,
16                            StyleSheet* aSheet, css::Rule* aParentRule,
17                            uint32_t aLine, uint32_t aColumn)
18     : ConditionRule(Servo_MediaRule_GetRules(aRawRule).Consume(), aSheet,
19                     aParentRule, aLine, aColumn),
20       mRawRule(std::move(aRawRule)) {}
21 
~CSSMediaRule()22 CSSMediaRule::~CSSMediaRule() {
23   if (mMediaList) {
24     mMediaList->SetStyleSheet(nullptr);
25   }
26 }
27 
28 NS_IMPL_ADDREF_INHERITED(CSSMediaRule, css::ConditionRule)
29 NS_IMPL_RELEASE_INHERITED(CSSMediaRule, css::ConditionRule)
30 
31 // QueryInterface implementation for MediaRule
32 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSMediaRule)
33 NS_INTERFACE_MAP_END_INHERITING(css::ConditionRule)
34 
35 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSMediaRule)
36 
37 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(CSSMediaRule,
38                                                 css::ConditionRule)
39   if (tmp->mMediaList) {
40     tmp->mMediaList->SetStyleSheet(nullptr);
41     tmp->mMediaList = nullptr;
42   }
43 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
44 
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSMediaRule,css::ConditionRule)45 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSMediaRule,
46                                                   css::ConditionRule)
47   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMediaList)
48 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
49 
50 /* virtual */
51 void CSSMediaRule::DropSheetReference() {
52   if (mMediaList) {
53     mMediaList->SetStyleSheet(nullptr);
54   }
55   ConditionRule::DropSheetReference();
56 }
57 
SetRawAfterClone(RefPtr<RawServoMediaRule> aRaw)58 void CSSMediaRule::SetRawAfterClone(RefPtr<RawServoMediaRule> aRaw) {
59   mRawRule = std::move(aRaw);
60   if (mMediaList) {
61     mMediaList->SetRawAfterClone(Servo_MediaRule_GetMedia(mRawRule).Consume());
62     mMediaList->SetStyleSheet(nullptr);
63     mMediaList->SetStyleSheet(GetStyleSheet());
64   }
65   css::ConditionRule::SetRawAfterClone(
66       Servo_MediaRule_GetRules(mRawRule).Consume());
67 }
68 
Type() const69 StyleCssRuleType CSSMediaRule::Type() const { return StyleCssRuleType::Media; }
70 
71 #ifdef DEBUG
72 /* virtual */
List(FILE * out,int32_t aIndent) const73 void CSSMediaRule::List(FILE* out, int32_t aIndent) const {
74   nsAutoCString str;
75   for (int32_t i = 0; i < aIndent; i++) {
76     str.AppendLiteral("  ");
77   }
78   Servo_MediaRule_Debug(mRawRule, &str);
79   fprintf_stderr(out, "%s\n", str.get());
80 }
81 #endif
82 
GetConditionText(nsACString & aConditionText)83 void CSSMediaRule::GetConditionText(nsACString& aConditionText) {
84   Media()->GetMediaText(aConditionText);
85 }
86 
SetConditionText(const nsACString & aConditionText,ErrorResult & aRv)87 void CSSMediaRule::SetConditionText(const nsACString& aConditionText,
88                                     ErrorResult& aRv) {
89   if (IsReadOnly()) {
90     return;
91   }
92   Media()->SetMediaText(aConditionText);
93 }
94 
95 /* virtual */
GetCssText(nsACString & aCssText) const96 void CSSMediaRule::GetCssText(nsACString& aCssText) const {
97   Servo_MediaRule_GetCssText(mRawRule, &aCssText);
98 }
99 
Media()100 /* virtual */ dom::MediaList* CSSMediaRule::Media() {
101   if (!mMediaList) {
102     mMediaList = new MediaList(Servo_MediaRule_GetMedia(mRawRule).Consume());
103     mMediaList->SetStyleSheet(GetStyleSheet());
104   }
105   return mMediaList;
106 }
107 
108 /* virtual */
SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const109 size_t CSSMediaRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
110   // TODO Implement this!
111   return aMallocSizeOf(this);
112 }
113 
114 /* virtual */
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)115 JSObject* CSSMediaRule::WrapObject(JSContext* aCx,
116                                    JS::Handle<JSObject*> aGivenProto) {
117   return CSSMediaRule_Binding::Wrap(aCx, this, aGivenProto);
118 }
119 
120 }  // namespace mozilla::dom
121