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/CSSImportRule.h"
8 
9 #include "mozilla/dom/CSSImportRuleBinding.h"
10 #include "mozilla/dom/MediaList.h"
11 #include "mozilla/ServoBindings.h"
12 #include "mozilla/StyleSheet.h"
13 
14 namespace mozilla {
15 namespace dom {
16 
CSSImportRule(RefPtr<RawServoImportRule> aRawRule,StyleSheet * aSheet,css::Rule * aParentRule,uint32_t aLine,uint32_t aColumn)17 CSSImportRule::CSSImportRule(RefPtr<RawServoImportRule> aRawRule,
18                              StyleSheet* aSheet, css::Rule* aParentRule,
19                              uint32_t aLine, uint32_t aColumn)
20     : css::Rule(aSheet, aParentRule, aLine, aColumn),
21       mRawRule(std::move(aRawRule)) {
22   const auto* sheet = Servo_ImportRule_GetSheet(mRawRule.get());
23   MOZ_ASSERT(sheet);
24   mChildSheet = const_cast<StyleSheet*>(sheet);
25   mChildSheet->SetOwnerRule(this);
26 }
27 
~CSSImportRule()28 CSSImportRule::~CSSImportRule() {
29   if (mChildSheet) {
30     mChildSheet->SetOwnerRule(nullptr);
31   }
32 }
33 
34 // QueryInterface implementation for CSSImportRule
35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSImportRule)
36 NS_INTERFACE_MAP_END_INHERITING(css::Rule)
37 
38 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSImportRule)
39 
40 NS_IMPL_ADDREF_INHERITED(CSSImportRule, css::Rule)
41 NS_IMPL_RELEASE_INHERITED(CSSImportRule, css::Rule)
42 
43 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSImportRule, css::Rule)
44   // Note the child sheet twice, since the Servo rule also holds a strong
45   // reference to it.
46   NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mChildSheet");
47   cb.NoteXPCOMChild(tmp->mChildSheet);
48   MOZ_ASSERT_IF(tmp->mRawRule,
49                 Servo_ImportRule_GetSheet(tmp->mRawRule) == tmp->mChildSheet);
50   cb.NoteXPCOMChild(tmp->mChildSheet);
51   NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRawRule.stylesheet");
52 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
53 
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSImportRule)
55   if (tmp->mChildSheet) {
56     tmp->mChildSheet->SetOwnerRule(nullptr);
57     tmp->mChildSheet = nullptr;
58   }
59   tmp->mRawRule = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::Rule)60 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::Rule)
61 
62 #ifdef DEBUG
63 /* virtual */
64 void CSSImportRule::List(FILE* out, int32_t aIndent) const {
65   nsAutoCString str;
66   for (int32_t i = 0; i < aIndent; i++) {
67     str.AppendLiteral("  ");
68   }
69   Servo_ImportRule_Debug(mRawRule, &str);
70   fprintf_stderr(out, "%s\n", str.get());
71 }
72 #endif
73 
GetMedia() const74 dom::MediaList* CSSImportRule::GetMedia() const {
75   // When Bug 1326509 is fixed, we can assert mChildSheet instead.
76   return mChildSheet ? mChildSheet->Media() : nullptr;
77 }
78 
DropSheetReference()79 void CSSImportRule::DropSheetReference() {
80   if (mChildSheet) {
81     mChildSheet->RemoveFromParent();
82   }
83   Rule::DropSheetReference();
84 }
85 
GetHref(nsAString & aHref) const86 void CSSImportRule::GetHref(nsAString& aHref) const {
87   Servo_ImportRule_GetHref(mRawRule, &aHref);
88 }
89 
90 /* virtual */
GetCssText(nsAString & aCssText) const91 void CSSImportRule::GetCssText(nsAString& aCssText) const {
92   Servo_ImportRule_GetCssText(mRawRule, &aCssText);
93 }
94 
95 /* virtual */
SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const96 size_t CSSImportRule::SizeOfIncludingThis(
97     mozilla::MallocSizeOf aMallocSizeOf) const {
98   // TODO Implement this!
99   return aMallocSizeOf(this);
100 }
101 
IsCCLeaf() const102 bool CSSImportRule::IsCCLeaf() const {
103   // We're not a leaf.
104   return false;
105 }
106 
107 /* virtual */
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)108 JSObject* CSSImportRule::WrapObject(JSContext* aCx,
109                                     JS::Handle<JSObject*> aGivenProto) {
110   return CSSImportRule_Binding::Wrap(aCx, this, aGivenProto);
111 }
112 
113 }  // namespace dom
114 }  // namespace mozilla
115