Bug 1307357 part 3 - Implement CSSRuleList interface for stylo. r=heycam,manishearth

MozReview-Commit-ID: Ecnbj66yKOE

--HG--
extra : source : e2e51877116f4dde6bd054e9b6e681f3e7f4250b
This commit is contained in:
Xidorn Quan 2016-11-23 10:26:20 +11:00
Родитель dc820f67ee
Коммит e887e33696
9 изменённых файлов: 181 добавлений и 2 удалений

Просмотреть файл

@ -37,6 +37,8 @@ SERVO_BINDING_FUNC(Servo_StyleSheet_Release, void,
RawServoStyleSheetBorrowed sheet)
SERVO_BINDING_FUNC(Servo_StyleSheet_HasRules, bool,
RawServoStyleSheetBorrowed sheet)
SERVO_BINDING_FUNC(Servo_StyleSheet_GetRules, ServoCssRulesStrong,
RawServoStyleSheetBorrowed sheet)
SERVO_BINDING_FUNC(Servo_StyleSet_Init, RawServoStyleSetOwned)
SERVO_BINDING_FUNC(Servo_StyleSet_Drop, void, RawServoStyleSetOwned set)
SERVO_BINDING_FUNC(Servo_StyleSet_AppendStyleSheet, void,
@ -49,6 +51,11 @@ SERVO_BINDING_FUNC(Servo_StyleSet_InsertStyleSheetBefore, void,
RawServoStyleSetBorrowed set, RawServoStyleSheetBorrowed sheet,
RawServoStyleSheetBorrowed reference)
// CSSRuleList
SERVO_BINDING_FUNC(Servo_CssRules_ListTypes, void,
ServoCssRulesBorrowed rules,
nsTArrayBorrowed_uintptr_t result)
// Animations API
SERVO_BINDING_FUNC(Servo_ParseProperty,
RawServoDeclarationBlockStrong,

Просмотреть файл

@ -11,6 +11,7 @@
#include "mozilla/UniquePtr.h"
struct ServoComputedValues;
struct ServoCssRules;
struct RawServoStyleSheet;
struct RawServoStyleSet;
struct RawServoDeclarationBlock;
@ -72,6 +73,7 @@ typedef nsIDocument RawGeckoDocument;
DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR(type_)
DECL_ARC_REF_TYPE_FOR(ServoComputedValues)
DECL_ARC_REF_TYPE_FOR(ServoCssRules)
DECL_ARC_REF_TYPE_FOR(RawServoStyleSheet)
DECL_ARC_REF_TYPE_FOR(RawServoDeclarationBlock)
// This is a reference to a reference of RawServoDeclarationBlock, which
@ -121,6 +123,7 @@ DECL_BORROWED_MUT_REF_TYPE_FOR(nsCSSValue)
}; \
}
DEFINE_REFPTR_TRAITS(CssRules, ServoCssRules)
DEFINE_REFPTR_TRAITS(StyleSheet, RawServoStyleSheet)
DEFINE_REFPTR_TRAITS(ComputedValues, ServoComputedValues)
DEFINE_REFPTR_TRAITS(DeclarationBlock, RawServoDeclarationBlock)

Просмотреть файл

@ -45,6 +45,7 @@ using namespace mozilla::dom;
}
IMPL_STRONG_REF_TYPE_FOR(ServoComputedValues)
IMPL_STRONG_REF_TYPE_FOR(ServoCssRules)
IMPL_STRONG_REF_TYPE_FOR(RawServoStyleSheet)
IMPL_STRONG_REF_TYPE_FOR(RawServoDeclarationBlock)

Просмотреть файл

@ -64,6 +64,16 @@ struct nsStyleDisplay;
void Gecko_Release##name_##ArbitraryThread(ThreadSafe##name_##Holder* aPtr) \
{ NS_RELEASE(aPtr); } \
#define DEFINE_ARRAY_TYPE_FOR(type_) \
struct nsTArrayBorrowed_##type_ { \
nsTArray<type_>* mArray; \
MOZ_IMPLICIT nsTArrayBorrowed_##type_(nsTArray<type_>* aArray) \
: mArray(aArray) {} \
}
DEFINE_ARRAY_TYPE_FOR(uintptr_t);
#undef DEFINE_ARRAY_TYPE_FOR
extern "C" {
// Object refcounting.

Просмотреть файл

@ -0,0 +1,79 @@
/* -*- 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"
namespace mozilla {
ServoCSSRuleList::ServoCSSRuleList(ServoStyleSheet* aStyleSheet,
already_AddRefed<ServoCssRules> aRawRules)
: mStyleSheet(aStyleSheet)
, mRawRules(aRawRules)
{
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.
}
nsIDOMCSSRule*
ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound)
{
if (aIndex >= mRules.Length()) {
aFound = false;
return nullptr;
}
aFound = true;
uintptr_t rule = mRules[aIndex];
if (rule <= kMaxRuleType) {
RefPtr<css::Rule> ruleObj = nullptr;
switch (rule) {
case nsIDOMCSSRule::STYLE_RULE:
case nsIDOMCSSRule::MEDIA_RULE:
case nsIDOMCSSRule::FONT_FACE_RULE:
case nsIDOMCSSRule::KEYFRAMES_RULE:
case nsIDOMCSSRule::NAMESPACE_RULE:
// XXX create corresponding rules
default:
MOZ_CRASH("stylo: not implemented yet");
}
ruleObj->SetStyleSheet(mStyleSheet);
rule = CastToUint(ruleObj.forget().take());
mRules[aIndex] = rule;
}
return CastToPtr(rule)->GetDOMRule();
}
template<typename Func>
void
ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback)
{
for (uintptr_t rule : mRules) {
if (rule > kMaxRuleType) {
aCallback(CastToPtr(rule));
}
}
}
void
ServoCSSRuleList::DropReference()
{
mStyleSheet = nullptr;
EnumerateInstantiatedRules([](css::Rule* rule) {
rule->SetStyleSheet(nullptr);
});
}
ServoCSSRuleList::~ServoCSSRuleList()
{
EnumerateInstantiatedRules([](css::Rule* rule) { rule->Release(); });
}
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,65 @@
/* -*- 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 */
#ifndef mozilla_ServoCSSRuleList_h
#define mozilla_ServoCSSRuleList_h
#include "mozilla/ServoBindingTypes.h"
#include "mozilla/dom/CSSRuleList.h"
namespace mozilla {
class ServoStyleSheet;
namespace css {
class Rule;
} // namespace css
class ServoCSSRuleList final : public CSSRuleList
{
public:
ServoCSSRuleList(ServoStyleSheet* aStyleSheet,
already_AddRefed<ServoCssRules> aRawRules);
ServoStyleSheet* GetParentObject() final { return mStyleSheet; }
nsIDOMCSSRule* IndexedGetter(uint32_t aIndex, bool& aFound) final;
uint32_t Length() final { return mRules.Length(); }
void DropReference();
private:
virtual ~ServoCSSRuleList();
// XXX Is it possible to have an address lower than or equal to 255?
// Is it possible to have more than 255 CSS rule types?
static const uintptr_t kMaxRuleType = UINT8_MAX;
static uintptr_t CastToUint(css::Rule* aPtr) {
return reinterpret_cast<uintptr_t>(aPtr);
}
static css::Rule* CastToPtr(uintptr_t aInt) {
MOZ_ASSERT(aInt > kMaxRuleType);
return reinterpret_cast<css::Rule*>(aInt);
}
template<typename Func>
void EnumerateInstantiatedRules(Func aCallback);
// mStyleSheet may be nullptr when it drops the reference to us.
ServoStyleSheet* mStyleSheet;
RefPtr<ServoCssRules> mRawRules;
// Array stores either a number indicating rule type, or a pointer to
// css::Rule. If the value is less than kMaxRuleType, the given rule
// instance has not been constructed, and the value means the type
// of the rule. Otherwise, it is a pointer.
nsTArray<uintptr_t> mRules;
};
} // namespace mozilla
#endif // mozilla_ServoCSSRuleList_h

Просмотреть файл

@ -6,6 +6,9 @@
#include "mozilla/ServoStyleSheet.h"
#include "mozilla/StyleBackendType.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoCSSRuleList.h"
#include "mozilla/dom/CSSRuleList.h"
namespace mozilla {
@ -21,6 +24,9 @@ ServoStyleSheet::ServoStyleSheet(css::SheetParsingMode aParsingMode,
ServoStyleSheet::~ServoStyleSheet()
{
DropSheet();
if (mRuleList) {
mRuleList->DropReference();
}
}
bool
@ -122,8 +128,11 @@ ServoStyleSheet::GetDOMOwnerRule() const
CSSRuleList*
ServoStyleSheet::GetCssRulesInternal(ErrorResult& aRv)
{
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
return nullptr;
if (!mRuleList) {
RefPtr<ServoCssRules> rawRules = Servo_StyleSheet_GetRules(mSheet).Consume();
mRuleList = new ServoCSSRuleList(this, rawRules.forget());
}
return mRuleList;
}
uint32_t

Просмотреть файл

@ -16,6 +16,8 @@
namespace mozilla {
class ServoCSSRuleList;
/**
* CSS style sheet object that is a wrapper for a Servo Stylesheet.
*/
@ -80,6 +82,7 @@ private:
void DropSheet();
RefPtr<RawServoStyleSheet> mSheet;
RefPtr<ServoCSSRuleList> mRuleList;
StyleSheetInfo mSheetInfo;
friend class StyleSheet;

Просмотреть файл

@ -96,6 +96,7 @@ EXPORTS.mozilla += [
'ServoBindingList.h',
'ServoBindings.h',
'ServoBindingTypes.h',
'ServoCSSRuleList.h',
'ServoDeclarationBlock.h',
'ServoElementSnapshot.h',
'ServoStyleSet.h',
@ -196,6 +197,7 @@ UNIFIED_SOURCES += [
'RuleNodeCacheConditions.cpp',
'RuleProcessorCache.cpp',
'ServoBindings.cpp',
'ServoCSSRuleList.cpp',
'ServoDeclarationBlock.cpp',
'ServoElementSnapshot.cpp',
'ServoStyleSet.cpp',