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