gecko-dev/layout/style/ServoCSSRuleList.cpp

225 строки
6.5 KiB
C++
Исходник Обычный вид История

/* -*- 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/IntegerRange.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoCounterStyleRule.h"
#include "mozilla/ServoDocumentRule.h"
#include "mozilla/ServoImportRule.h"
Bug 1449087 part 2 - Use Servo data to back @font-face rule. r=emilio This patch does the following things: * Create a new class ServoFontFaceRule for CSSOM of @font-face rule which mostly follows how nsCSSFontFaceRule was implemented. * Remove the old nsCSSFontFaceRule and binding code to create it. * Have FontFace backed by Servo data via making mRule and mDescriptors of the class hold RawServoFontFaceRule like ServoFontFaceRule. To keep this patch small, it effectively just delays the conversion from Servo data to nsCSSValue from parsing to using. This may cause worse performance if the font set is flushed repeatedly. Supposing we don't flush font set very frequently, it may not be a big deal. We may still want to remove the intermediate nsCSSValue conversion at some point, and have everything converted to their final form directly when used, but that can happen in followups. There are some unfortunate bits from this change: * We lose style sheet for logging in FontFaceSet. This is probably not all that worse, because we wouldn't have that before either if the page doesn't use CSSOM to visit it. But we should figure out some approach to fix it anyway. * InspectorFontFace no longer shares the same rule object as CSSOM. This isn't really a problem if the @font-face rule isn't very mutable. Unless we want to make the rule returned from InspectorFontFace to be mutable (i.e. via inspector), not using the same object probably isn't too bad. This patch switches the code we use to serialize stuff in FontFace and CSSFontFaceRule, which leads to some failures in tests. Specifically, the expected changes including: * Value of font-family now can be serialized to identifier sequence like font-family property. The old code always serializes it to string, but it doesn't seem to have different requirement than the property. Blink can serialize to identifier as well. * Family name inside local() is also changed to use the same way as family names elsewhere (i.e. can be identifier sequence). Blink has the same behavior as the old code, but I don't think it's a big deal. * The order of descriptors serialized gets changed. I don't think it matters at all. * Empty string as font-family via using string syntax is no longer considered invalid for FontFace. I don't find it is mentioned anywhere that it should be specifically treated invalid. MozReview-Commit-ID: 32Fk3Fi9uTs --HG-- extra : rebase_source : 6221ec8fc56de357b06dd27e770fb175348a2f77
2018-04-04 01:42:10 +03:00
#include "mozilla/ServoFontFaceRule.h"
#include "mozilla/ServoFontFeatureValuesRule.h"
#include "mozilla/ServoKeyframesRule.h"
#include "mozilla/ServoMediaRule.h"
#include "mozilla/ServoNamespaceRule.h"
#include "mozilla/ServoPageRule.h"
#include "mozilla/ServoStyleRule.h"
#include "mozilla/ServoSupportsRule.h"
#include "mozilla/StyleSheet.h"
using namespace mozilla::dom;
namespace mozilla {
ServoCSSRuleList::ServoCSSRuleList(already_AddRefed<ServoCssRules> aRawRules,
StyleSheet* aDirectOwnerStyleSheet)
: mStyleSheet(aDirectOwnerStyleSheet)
, mRawRules(aRawRules)
{
Servo_CssRules_ListTypes(mRawRules, &mRules);
}
// QueryInterface implementation for ServoCSSRuleList
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(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)
tmp->DropAllRules();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
dom::CSSRuleList)
tmp->EnumerateInstantiatedRules([&](css::Rule* aRule) {
if (!aRule->IsCCLeaf()) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
cb.NoteXPCOMChild(aRule);
}
});
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
void
ServoCSSRuleList::SetParentRule(css::GroupRule* aParentRule)
{
mParentRule = aParentRule;
EnumerateInstantiatedRules([aParentRule](css::Rule* rule) {
rule->SetParentRule(aParentRule);
});
}
void
ServoCSSRuleList::SetStyleSheet(StyleSheet* aStyleSheet)
{
mStyleSheet = aStyleSheet;
EnumerateInstantiatedRules([this](css::Rule* rule) {
rule->SetStyleSheet(mStyleSheet);
});
}
css::Rule*
ServoCSSRuleList::GetRule(uint32_t aIndex)
{
uintptr_t rule = mRules[aIndex];
if (rule <= kMaxRuleType) {
RefPtr<css::Rule> ruleObj = nullptr;
switch (rule) {
#define CASE_RULE(const_, name_) \
case CSSRuleBinding::const_##_RULE: { \
uint32_t line = 0, column = 0; \
RefPtr<RawServo##name_##Rule> rule = \
Servo_CssRules_Get##name_##RuleAt( \
mRawRules, aIndex, &line, &column \
).Consume(); \
MOZ_ASSERT(rule); \
ruleObj = new Servo##name_##Rule(rule.forget(), line, column); \
break; \
}
CASE_RULE(STYLE, Style)
CASE_RULE(KEYFRAMES, Keyframes)
CASE_RULE(MEDIA, Media)
CASE_RULE(NAMESPACE, Namespace)
CASE_RULE(PAGE, Page)
CASE_RULE(SUPPORTS, Supports)
CASE_RULE(DOCUMENT, Document)
CASE_RULE(IMPORT, Import)
CASE_RULE(FONT_FEATURE_VALUES, FontFeatureValues)
Bug 1449087 part 2 - Use Servo data to back @font-face rule. r=emilio This patch does the following things: * Create a new class ServoFontFaceRule for CSSOM of @font-face rule which mostly follows how nsCSSFontFaceRule was implemented. * Remove the old nsCSSFontFaceRule and binding code to create it. * Have FontFace backed by Servo data via making mRule and mDescriptors of the class hold RawServoFontFaceRule like ServoFontFaceRule. To keep this patch small, it effectively just delays the conversion from Servo data to nsCSSValue from parsing to using. This may cause worse performance if the font set is flushed repeatedly. Supposing we don't flush font set very frequently, it may not be a big deal. We may still want to remove the intermediate nsCSSValue conversion at some point, and have everything converted to their final form directly when used, but that can happen in followups. There are some unfortunate bits from this change: * We lose style sheet for logging in FontFaceSet. This is probably not all that worse, because we wouldn't have that before either if the page doesn't use CSSOM to visit it. But we should figure out some approach to fix it anyway. * InspectorFontFace no longer shares the same rule object as CSSOM. This isn't really a problem if the @font-face rule isn't very mutable. Unless we want to make the rule returned from InspectorFontFace to be mutable (i.e. via inspector), not using the same object probably isn't too bad. This patch switches the code we use to serialize stuff in FontFace and CSSFontFaceRule, which leads to some failures in tests. Specifically, the expected changes including: * Value of font-family now can be serialized to identifier sequence like font-family property. The old code always serializes it to string, but it doesn't seem to have different requirement than the property. Blink can serialize to identifier as well. * Family name inside local() is also changed to use the same way as family names elsewhere (i.e. can be identifier sequence). Blink has the same behavior as the old code, but I don't think it's a big deal. * The order of descriptors serialized gets changed. I don't think it matters at all. * Empty string as font-family via using string syntax is no longer considered invalid for FontFace. I don't find it is mentioned anywhere that it should be specifically treated invalid. MozReview-Commit-ID: 32Fk3Fi9uTs --HG-- extra : rebase_source : 6221ec8fc56de357b06dd27e770fb175348a2f77
2018-04-04 01:42:10 +03:00
CASE_RULE(FONT_FACE, FontFace)
CASE_RULE(COUNTER_STYLE, CounterStyle)
#undef CASE_RULE
case CSSRuleBinding::KEYFRAME_RULE:
MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
return nullptr;
default:
NS_WARNING("stylo: not implemented yet");
return nullptr;
}
ruleObj->SetStyleSheet(mStyleSheet);
ruleObj->SetParentRule(mParentRule);
rule = CastToUint(ruleObj.forget().take());
mRules[aIndex] = rule;
}
return CastToPtr(rule);
}
css::Rule*
ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound)
{
if (aIndex >= mRules.Length()) {
aFound = false;
return nullptr;
}
aFound = true;
return GetRule(aIndex);
}
template<typename Func>
void
ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback)
{
for (uintptr_t rule : mRules) {
if (rule > kMaxRuleType) {
aCallback(CastToPtr(rule));
}
}
}
static void
DropRule(already_AddRefed<css::Rule> aRule)
{
RefPtr<css::Rule> rule = aRule;
rule->SetStyleSheet(nullptr);
rule->SetParentRule(nullptr);
}
void
ServoCSSRuleList::DropAllRules()
{
EnumerateInstantiatedRules([](css::Rule* rule) {
DropRule(already_AddRefed<css::Rule>(rule));
});
mRules.Clear();
mRawRules = nullptr;
}
void
ServoCSSRuleList::DropReference()
{
mStyleSheet = nullptr;
mParentRule = nullptr;
DropAllRules();
}
nsresult
ServoCSSRuleList::InsertRule(const nsAString& aRule, uint32_t aIndex)
{
MOZ_ASSERT(mStyleSheet, "Caller must ensure that "
"the list is not unlinked from stylesheet");
NS_ConvertUTF16toUTF8 rule(aRule);
bool nested = !!mParentRule;
css::Loader* loader = nullptr;
if (nsIDocument* doc = mStyleSheet->GetAssociatedDocument()) {
loader = doc->CSSLoader();
}
uint16_t type;
nsresult rv = Servo_CssRules_InsertRule(mRawRules, mStyleSheet->RawContents(),
&rule, aIndex, nested,
loader, mStyleSheet, &type);
if (NS_FAILED(rv)) {
return rv;
}
mRules.InsertElementAt(aIndex, type);
return rv;
}
nsresult
ServoCSSRuleList::DeleteRule(uint32_t aIndex)
{
nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
if (!NS_FAILED(rv)) {
uintptr_t rule = mRules[aIndex];
if (rule > kMaxRuleType) {
DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
}
mRules.RemoveElementAt(aIndex);
}
return rv;
}
uint16_t
ServoCSSRuleList::GetDOMCSSRuleType(uint32_t aIndex) const
{
uintptr_t rule = mRules[aIndex];
if (rule <= kMaxRuleType) {
return rule;
}
return CastToPtr(rule)->Type();
}
ServoCSSRuleList::~ServoCSSRuleList()
{
MOZ_ASSERT(!mStyleSheet, "Backpointer should have been cleared");
MOZ_ASSERT(!mParentRule, "Backpointer should have been cleared");
DropAllRules();
}
} // namespace mozilla