2016-11-23 02:26:20 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
/* representation of CSSRuleList for stylo */
|
|
|
|
|
|
|
|
#include "mozilla/ServoCSSRuleList.h"
|
|
|
|
|
|
|
|
#include "mozilla/ServoBindings.h"
|
2016-11-23 02:26:20 +03:00
|
|
|
#include "mozilla/ServoStyleRule.h"
|
2017-03-09 09:37:45 +03:00
|
|
|
#include "mozilla/ServoMediaRule.h"
|
2017-03-14 10:51:59 +03:00
|
|
|
#include "mozilla/ServoNamespaceRule.h"
|
2017-03-31 05:25:22 +03:00
|
|
|
#include "mozilla/ServoPageRule.h"
|
2017-04-26 10:59:29 +03:00
|
|
|
#include "mozilla/ServoSupportsRule.h"
|
2016-11-23 02:26:20 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-03-09 12:53:34 +03:00
|
|
|
ServoCSSRuleList::ServoCSSRuleList(already_AddRefed<ServoCssRules> aRawRules)
|
|
|
|
: mRawRules(aRawRules)
|
2016-11-23 02:26:20 +03:00
|
|
|
{
|
|
|
|
Servo_CssRules_ListTypes(mRawRules, &mRules);
|
|
|
|
// XXX We may want to eagerly create object for import rule, so that
|
|
|
|
// we don't lose the reference to child stylesheet when our own
|
|
|
|
// stylesheet goes away.
|
|
|
|
}
|
|
|
|
|
2017-01-06 07:14:28 +03:00
|
|
|
// QueryInterface implementation for ServoCSSRuleList
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServoCSSRuleList)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList)
|
2017-03-09 12:53:34 +03:00
|
|
|
tmp->DropAllRules();
|
2017-01-06 07:14:28 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
|
|
|
|
dom::CSSRuleList)
|
|
|
|
tmp->EnumerateInstantiatedRules([&](css::Rule* aRule) {
|
2016-12-01 02:18:14 +03:00
|
|
|
if (!aRule->IsCCLeaf()) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
|
|
|
|
cb.NoteXPCOMChild(aRule);
|
Bug 1345696 part 6 - Fix cycle collection for font-face rule. r=heycam,mccr8
Having Servo's FontFaceRule owning Gecko's nsCSSFontFaceRule object opens an
untracked edge, because we generally don't track the reference through Servo
objects, so there is a chance that nsCSSFontFaceRule can form a undroppable
reference cycle.
This patch adds a workaround that we track @font-face rule twice in CSS rule
list. This is not perfect, but the idea is that, if someone wants to put some
reference on the @font-face rule, the rule itself and its parent list should
have been constructed in the CSSOM tree. If they are not there, we are
probably safe from cycle reference.
For @font-face rule, that assumption isn't strictly true. Script can still
get a font-face rule via layout inspector's nsIDOMFontFace::rule. If some
script puts an object which references the stylesheet of the rule or any of
its parent rule / rule list into font-face rule's expando, we would leak
again... But as far as that is an internal interface, we are probably safe?
MozReview-Commit-ID: DDMJh3mxDCH
--HG--
extra : rebase_source : 646fcf076195151e6890ebffb7597e58c8a7d522
2017-03-28 08:11:44 +03:00
|
|
|
// Note about @font-face rule again, since there is an indirect
|
|
|
|
// owning edge through Servo's struct that FontFaceRule in Servo
|
|
|
|
// owns a Gecko nsCSSFontFaceRule object.
|
|
|
|
if (aRule->Type() == nsIDOMCSSRule::FONT_FACE_RULE) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRawRules[i]");
|
|
|
|
cb.NoteXPCOMChild(aRule);
|
|
|
|
}
|
2016-12-01 02:18:14 +03:00
|
|
|
}
|
2017-01-06 07:14:28 +03:00
|
|
|
});
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2017-03-09 12:53:34 +03:00
|
|
|
void
|
|
|
|
ServoCSSRuleList::SetParentRule(css::GroupRule* aParentRule)
|
|
|
|
{
|
|
|
|
mParentRule = aParentRule;
|
|
|
|
EnumerateInstantiatedRules([aParentRule](css::Rule* rule) {
|
|
|
|
rule->SetParentRule(aParentRule);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoCSSRuleList::SetStyleSheet(StyleSheet* aStyleSheet)
|
|
|
|
{
|
|
|
|
mStyleSheet = aStyleSheet ? aStyleSheet->AsServo() : nullptr;
|
|
|
|
EnumerateInstantiatedRules([aStyleSheet](css::Rule* rule) {
|
|
|
|
rule->SetStyleSheet(aStyleSheet);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:59:55 +03:00
|
|
|
css::Rule*
|
|
|
|
ServoCSSRuleList::GetRule(uint32_t aIndex)
|
2016-11-23 02:26:20 +03:00
|
|
|
{
|
|
|
|
uintptr_t rule = mRules[aIndex];
|
|
|
|
if (rule <= kMaxRuleType) {
|
|
|
|
RefPtr<css::Rule> ruleObj = nullptr;
|
|
|
|
switch (rule) {
|
2017-03-14 09:52:37 +03:00
|
|
|
#define CASE_RULE(const_, name_) \
|
|
|
|
case nsIDOMCSSRule::const_##_RULE: { \
|
|
|
|
ruleObj = new Servo##name_##Rule( \
|
|
|
|
Servo_CssRules_Get##name_##RuleAt(mRawRules, aIndex).Consume()); \
|
|
|
|
break; \
|
2017-03-09 09:37:45 +03:00
|
|
|
}
|
2017-03-14 09:52:37 +03:00
|
|
|
CASE_RULE(STYLE, Style)
|
|
|
|
CASE_RULE(MEDIA, Media)
|
2017-03-14 10:51:59 +03:00
|
|
|
CASE_RULE(NAMESPACE, Namespace)
|
2017-03-31 05:25:22 +03:00
|
|
|
CASE_RULE(PAGE, Page)
|
2017-04-26 10:59:29 +03:00
|
|
|
CASE_RULE(SUPPORTS, Supports)
|
2017-03-14 09:52:37 +03:00
|
|
|
#undef CASE_RULE
|
2017-03-27 06:49:26 +03:00
|
|
|
case nsIDOMCSSRule::FONT_FACE_RULE: {
|
|
|
|
// Returns a borrowed nsCSSFontFaceRule object directly, so we
|
|
|
|
// don't need to create anything here, but we still need to have
|
|
|
|
// the style sheet and parent rule set properly.
|
|
|
|
ruleObj = Servo_CssRules_GetFontFaceRuleAt(mRawRules, aIndex);
|
|
|
|
break;
|
|
|
|
}
|
2016-11-23 02:26:20 +03:00
|
|
|
case nsIDOMCSSRule::KEYFRAMES_RULE:
|
|
|
|
// XXX create corresponding rules
|
|
|
|
default:
|
2017-02-14 04:19:54 +03:00
|
|
|
NS_WARNING("stylo: not implemented yet");
|
2016-11-24 08:28:28 +03:00
|
|
|
return nullptr;
|
2016-11-23 02:26:20 +03:00
|
|
|
}
|
|
|
|
ruleObj->SetStyleSheet(mStyleSheet);
|
2017-03-09 12:53:34 +03:00
|
|
|
ruleObj->SetParentRule(mParentRule);
|
2016-11-23 02:26:20 +03:00
|
|
|
rule = CastToUint(ruleObj.forget().take());
|
|
|
|
mRules[aIndex] = rule;
|
|
|
|
}
|
2016-11-29 14:59:55 +03:00
|
|
|
return CastToPtr(rule);
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:41:03 +03:00
|
|
|
css::Rule*
|
2016-11-29 14:59:55 +03:00
|
|
|
ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound)
|
|
|
|
{
|
|
|
|
if (aIndex >= mRules.Length()) {
|
|
|
|
aFound = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
aFound = true;
|
2017-01-13 18:41:03 +03:00
|
|
|
return GetRule(aIndex);
|
2016-11-23 02:26:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Func>
|
|
|
|
void
|
|
|
|
ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback)
|
|
|
|
{
|
|
|
|
for (uintptr_t rule : mRules) {
|
|
|
|
if (rule > kMaxRuleType) {
|
|
|
|
aCallback(CastToPtr(rule));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 12:53:34 +03:00
|
|
|
static void
|
|
|
|
DropRule(already_AddRefed<css::Rule> aRule)
|
|
|
|
{
|
|
|
|
RefPtr<css::Rule> rule = aRule;
|
|
|
|
rule->SetStyleSheet(nullptr);
|
|
|
|
rule->SetParentRule(nullptr);
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:26:20 +03:00
|
|
|
void
|
2017-03-09 12:53:34 +03:00
|
|
|
ServoCSSRuleList::DropAllRules()
|
2016-11-23 02:26:20 +03:00
|
|
|
{
|
|
|
|
EnumerateInstantiatedRules([](css::Rule* rule) {
|
2017-03-09 12:53:34 +03:00
|
|
|
DropRule(already_AddRefed<css::Rule>(rule));
|
2016-11-23 02:26:20 +03:00
|
|
|
});
|
2017-03-09 12:53:34 +03:00
|
|
|
mRules.Clear();
|
Bug 1345696 part 6 - Fix cycle collection for font-face rule. r=heycam,mccr8
Having Servo's FontFaceRule owning Gecko's nsCSSFontFaceRule object opens an
untracked edge, because we generally don't track the reference through Servo
objects, so there is a chance that nsCSSFontFaceRule can form a undroppable
reference cycle.
This patch adds a workaround that we track @font-face rule twice in CSS rule
list. This is not perfect, but the idea is that, if someone wants to put some
reference on the @font-face rule, the rule itself and its parent list should
have been constructed in the CSSOM tree. If they are not there, we are
probably safe from cycle reference.
For @font-face rule, that assumption isn't strictly true. Script can still
get a font-face rule via layout inspector's nsIDOMFontFace::rule. If some
script puts an object which references the stylesheet of the rule or any of
its parent rule / rule list into font-face rule's expando, we would leak
again... But as far as that is an internal interface, we are probably safe?
MozReview-Commit-ID: DDMJh3mxDCH
--HG--
extra : rebase_source : 646fcf076195151e6890ebffb7597e58c8a7d522
2017-03-28 08:11:44 +03:00
|
|
|
mRawRules = nullptr;
|
2017-03-09 12:53:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoCSSRuleList::DropReference()
|
|
|
|
{
|
|
|
|
mStyleSheet = nullptr;
|
|
|
|
mParentRule = nullptr;
|
|
|
|
DropAllRules();
|
2016-11-23 02:26:20 +03:00
|
|
|
}
|
|
|
|
|
2016-11-29 14:59:55 +03:00
|
|
|
nsresult
|
|
|
|
ServoCSSRuleList::InsertRule(const nsAString& aRule, uint32_t aIndex)
|
|
|
|
{
|
2017-04-03 13:04:32 +03:00
|
|
|
MOZ_ASSERT(mStyleSheet, "Caller must ensure that "
|
|
|
|
"the list is not unlinked from stylesheet");
|
2016-11-29 14:59:55 +03:00
|
|
|
NS_ConvertUTF16toUTF8 rule(aRule);
|
2017-03-09 12:53:34 +03:00
|
|
|
bool nested = !!mParentRule;
|
2017-04-03 13:04:32 +03:00
|
|
|
css::Loader* loader = nullptr;
|
|
|
|
if (nsIDocument* doc = mStyleSheet->GetAssociatedDocument()) {
|
|
|
|
loader = doc->CSSLoader();
|
|
|
|
}
|
2016-11-29 14:59:55 +03:00
|
|
|
uint16_t type;
|
|
|
|
nsresult rv = Servo_CssRules_InsertRule(mRawRules, mStyleSheet->RawSheet(),
|
2017-04-03 13:04:32 +03:00
|
|
|
&rule, aIndex, nested,
|
|
|
|
loader, mStyleSheet, &type);
|
2016-11-29 14:59:55 +03:00
|
|
|
if (!NS_FAILED(rv)) {
|
2016-11-30 06:36:00 +03:00
|
|
|
mRules.InsertElementAt(aIndex, type);
|
2016-11-29 14:59:55 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ServoCSSRuleList::DeleteRule(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
|
|
|
|
if (!NS_FAILED(rv)) {
|
2017-02-08 06:19:08 +03:00
|
|
|
uintptr_t rule = mRules[aIndex];
|
|
|
|
if (rule > kMaxRuleType) {
|
2017-03-09 12:53:34 +03:00
|
|
|
DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
|
2017-02-08 06:19:08 +03:00
|
|
|
}
|
2016-11-29 14:59:55 +03:00
|
|
|
mRules.RemoveElementAt(aIndex);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-04-03 12:55:06 +03:00
|
|
|
uint16_t
|
|
|
|
ServoCSSRuleList::GetRuleType(uint32_t aIndex) const
|
|
|
|
{
|
|
|
|
uintptr_t rule = mRules[aIndex];
|
|
|
|
if (rule <= kMaxRuleType) {
|
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
return CastToPtr(rule)->Type();
|
|
|
|
}
|
|
|
|
|
2017-04-07 18:07:37 +03:00
|
|
|
void
|
|
|
|
ServoCSSRuleList::FillStyleRuleHashtable(StyleRuleHashtable& aTable)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mRules.Length(); i++) {
|
|
|
|
uint16_t type = GetRuleType(i);
|
|
|
|
if (type == nsIDOMCSSRule::STYLE_RULE) {
|
|
|
|
ServoStyleRule* castedRule = static_cast<ServoStyleRule*>(GetRule(i));
|
|
|
|
RawServoStyleRule* rawRule = castedRule->Raw();
|
|
|
|
aTable.Put(rawRule, castedRule);
|
2017-04-26 10:59:29 +03:00
|
|
|
} else if (type == nsIDOMCSSRule::MEDIA_RULE ||
|
|
|
|
type == nsIDOMCSSRule::SUPPORTS_RULE) {
|
|
|
|
GroupRule* castedRule = static_cast<GroupRule*>(GetRule(i));
|
2017-04-07 18:07:37 +03:00
|
|
|
|
|
|
|
// Call this method recursively on the ServoCSSRuleList in the rule.
|
|
|
|
ServoCSSRuleList* castedRuleList = static_cast<ServoCSSRuleList*>(
|
|
|
|
castedRule->CssRules());
|
|
|
|
castedRuleList->FillStyleRuleHashtable(aTable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:26:20 +03:00
|
|
|
ServoCSSRuleList::~ServoCSSRuleList()
|
|
|
|
{
|
2017-03-09 12:53:34 +03:00
|
|
|
DropAllRules();
|
2016-11-23 02:26:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|