summaryrefslogtreecommitdiffstats
path: root/layout/style/CSSKeyframeRule.cpp
blob: b70c6543b0cf20e0574844cba212062d7fc8cb0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/CSSKeyframeRule.h"

#include "mozilla/DeclarationBlock.h"
#include "mozilla/dom/CSSKeyframeRuleBinding.h"
#include "nsDOMCSSDeclaration.h"

namespace mozilla::dom {

// -------------------------------------------
// CSSKeyframeDeclaration
//

class CSSKeyframeDeclaration final : public nsDOMCSSDeclaration {
 public:
  explicit CSSKeyframeDeclaration(CSSKeyframeRule* aRule) : mRule(aRule) {
    mDecls =
        new DeclarationBlock(Servo_Keyframe_GetStyle(aRule->Raw()).Consume());
    mDecls->SetOwningRule(aRule);
  }

  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS_AMBIGUOUS(CSSKeyframeDeclaration,
                                                        nsICSSDeclaration)

  css::Rule* GetParentRule() final { return mRule; }

  void DropReference() {
    mRule = nullptr;
    mDecls->SetOwningRule(nullptr);
  }

  DeclarationBlock* GetOrCreateCSSDeclaration(
      Operation aOperation, DeclarationBlock** aCreated) final {
    if (aOperation != Operation::Read && mRule) {
      if (StyleSheet* sheet = mRule->GetStyleSheet()) {
        sheet->WillDirty();
      }
    }
    return mDecls;
  }
  nsresult SetCSSDeclaration(DeclarationBlock* aDecls,
                             MutationClosureData* aClosureData) final {
    if (!mRule) {
      return NS_OK;
    }
    mRule->UpdateRule([this, aDecls]() {
      if (mDecls != aDecls) {
        mDecls->SetOwningRule(nullptr);
        mDecls = aDecls;
        mDecls->SetOwningRule(mRule);
        Servo_Keyframe_SetStyle(mRule->Raw(), mDecls->Raw());
      }
    });
    return NS_OK;
  }
  ParsingEnvironment GetParsingEnvironment(
      nsIPrincipal* aSubjectPrincipal) const final {
    return GetParsingEnvironmentForRule(mRule, StyleCssRuleType::Keyframe);
  }

  nsINode* GetAssociatedNode() const final {
    return mRule ? mRule->GetAssociatedDocumentOrShadowRoot() : nullptr;
  }

  nsISupports* GetParentObject() const final {
    return mRule ? mRule->GetParentObject() : nullptr;
  }

  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
    size_t n = aMallocSizeOf(this);
    // TODO we may want to add size of mDecls as well
    return n;
  }

  void SetRawAfterClone(StyleLockedKeyframe* aKeyframe) {
    mDecls->SetOwningRule(nullptr);
    mDecls = new DeclarationBlock(Servo_Keyframe_GetStyle(aKeyframe).Consume());
    mDecls->SetOwningRule(mRule);
  }

 private:
  virtual ~CSSKeyframeDeclaration() {
    MOZ_ASSERT(!mRule, "Backpointer should have been cleared");
  }

  CSSKeyframeRule* mRule;
  RefPtr<DeclarationBlock> mDecls;
};

NS_IMPL_CYCLE_COLLECTING_ADDREF(CSSKeyframeDeclaration)
NS_IMPL_CYCLE_COLLECTING_RELEASE(CSSKeyframeDeclaration)

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(CSSKeyframeDeclaration)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSKeyframeDeclaration)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration)

// -------------------------------------------
// CSSKeyframeRule
//

CSSKeyframeRule::CSSKeyframeRule(already_AddRefed<StyleLockedKeyframe> aRaw,
                                 StyleSheet* aSheet, css::Rule* aParentRule,
                                 uint32_t aLine, uint32_t aColumn)
    : css::Rule(aSheet, aParentRule, aLine, aColumn), mRaw(aRaw) {}

CSSKeyframeRule::~CSSKeyframeRule() {
  if (mDeclaration) {
    mDeclaration->DropReference();
  }
}

NS_IMPL_ADDREF_INHERITED(CSSKeyframeRule, css::Rule)
NS_IMPL_RELEASE_INHERITED(CSSKeyframeRule, css::Rule)

// QueryInterface implementation for nsCSSKeyframeRule
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSKeyframeRule)
NS_INTERFACE_MAP_END_INHERITING(css::Rule)

NS_IMPL_CYCLE_COLLECTION_CLASS(CSSKeyframeRule)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(CSSKeyframeRule, css::Rule)
  if (tmp->mDeclaration) {
    tmp->mDeclaration->DropReference();
    tmp->mDeclaration = nullptr;
  }
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSKeyframeRule, css::Rule)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDeclaration)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

bool CSSKeyframeRule::IsCCLeaf() const {
  return Rule::IsCCLeaf() && !mDeclaration;
}

StyleCssRuleType CSSKeyframeRule::Type() const {
  return StyleCssRuleType::Keyframe;
}

void CSSKeyframeRule::SetRawAfterClone(RefPtr<StyleLockedKeyframe> aRaw) {
  mRaw = std::move(aRaw);

  if (mDeclaration) {
    mDeclaration->SetRawAfterClone(mRaw);
  }
}

#ifdef DEBUG
/* virtual */
void CSSKeyframeRule::List(FILE* out, int32_t aIndent) const {
  nsAutoCString str;
  for (int32_t i = 0; i < aIndent; i++) {
    str.AppendLiteral("  ");
  }
  Servo_Keyframe_Debug(mRaw, &str);
  fprintf_stderr(out, "%s\n", str.get());
}
#endif

template <typename Func>
void CSSKeyframeRule::UpdateRule(Func aCallback) {
  if (IsReadOnly()) {
    return;
  }

  StyleSheet* sheet = GetStyleSheet();
  if (sheet) {
    sheet->WillDirty();
  }

  aCallback();

  if (sheet) {
    sheet->RuleChanged(this, StyleRuleChangeKind::Generic);
  }
}

void CSSKeyframeRule::GetKeyText(nsACString& aKeyText) {
  Servo_Keyframe_GetKeyText(mRaw, &aKeyText);
}

void CSSKeyframeRule::SetKeyText(const nsACString& aKeyText) {
  UpdateRule(
      [this, &aKeyText]() { Servo_Keyframe_SetKeyText(mRaw, &aKeyText); });
}

void CSSKeyframeRule::GetCssText(nsACString& aCssText) const {
  Servo_Keyframe_GetCssText(mRaw, &aCssText);
}

nsICSSDeclaration* CSSKeyframeRule::Style() {
  if (!mDeclaration) {
    mDeclaration = new CSSKeyframeDeclaration(this);
  }
  return mDeclaration;
}

size_t CSSKeyframeRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
  size_t n = aMallocSizeOf(this);
  if (mDeclaration) {
    n += mDeclaration->SizeOfIncludingThis(aMallocSizeOf);
  }
  return n;
}

/* virtual */
JSObject* CSSKeyframeRule::WrapObject(JSContext* aCx,
                                      JS::Handle<JSObject*> aGivenProto) {
  return CSSKeyframeRule_Binding::Wrap(aCx, this, aGivenProto);
}

}  // namespace mozilla::dom