Skip to content

Commit

Permalink
Bug 1846745 - Miscellaneous clean-ups to nsCSSProps. r=boris
Browse files Browse the repository at this point in the history
Fixes various clang-tidy warnings and makes initialization a bit more
straight-forward.

Depends on D185154

Differential Revision: https://phabricator.services.mozilla.com/D185155
  • Loading branch information
emilio committed Aug 3, 2023
1 parent 597d6d7 commit 5502bf7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 66 deletions.
3 changes: 1 addition & 2 deletions layout/build/nsLayoutStatics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ nsresult nsLayoutStatics::Initialize() {

ContentParent::StartUp();

nsCSSProps::AddRefTable();
nsCSSProps::Init();
nsColorNames::AddRefTable();

#ifdef DEBUG
Expand Down Expand Up @@ -342,7 +342,6 @@ void nsLayoutStatics::Shutdown() {

// Release all of our atoms
nsColorNames::ReleaseTable();
nsCSSProps::ReleaseTable();
nsRepeatService::Shutdown();

nsXULContentUtils::Finish();
Expand Down
77 changes: 33 additions & 44 deletions layout/style/nsCSSProps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/ClearOnShutdown.h"

using namespace mozilla;

static int32_t gPropertyTableRefCount;
static StaticAutoPtr<nsStaticCaseInsensitiveNameTable> gFontDescTable;
static StaticAutoPtr<nsStaticCaseInsensitiveNameTable> gCounterDescTable;
static StaticAutoPtr<nsTHashMap<nsCStringHashKey, nsCSSPropertyID>>
Expand Down Expand Up @@ -64,7 +64,7 @@ static constexpr CSSPropFlags kFlagsTable[eCSSProperty_COUNT_with_aliases] = {

static nsStaticCaseInsensitiveNameTable* CreateStaticTable(
const char* const aRawTable[], int32_t aLength) {
auto table = new nsStaticCaseInsensitiveNameTable(aRawTable, aLength);
auto* table = new nsStaticCaseInsensitiveNameTable(aRawTable, aLength);
#ifdef DEBUG
// Partially verify the entries.
for (int32_t index = 0; index < aLength; ++index) {
Expand Down Expand Up @@ -97,50 +97,39 @@ void nsCSSProps::RecomputeEnabledState(const char* aPref, void*) {
MOZ_ASSERT(foundPref);
}

void nsCSSProps::AddRefTable(void) {
if (0 == gPropertyTableRefCount++) {
MOZ_ASSERT(!gFontDescTable, "pre existing array!");
MOZ_ASSERT(!gCounterDescTable, "pre existing array!");
MOZ_ASSERT(!gPropertyIDLNameTable, "pre existing array!");

gFontDescTable = CreateStaticTable(kCSSRawFontDescs, eCSSFontDesc_COUNT);
gCounterDescTable =
CreateStaticTable(kCSSRawCounterDescs, eCSSCounterDesc_COUNT);

gPropertyIDLNameTable = new nsTHashMap<nsCStringHashKey, nsCSSPropertyID>;
for (nsCSSPropertyID p = nsCSSPropertyID(0);
size_t(p) < ArrayLength(kIDLNameTable); p = nsCSSPropertyID(p + 1)) {
if (kIDLNameTable[p]) {
gPropertyIDLNameTable->InsertOrUpdate(
nsDependentCString(kIDLNameTable[p]), p);
}
}

static bool prefObserversInited = false;
if (!prefObserversInited) {
prefObserversInited = true;
for (const PropertyPref* pref = kPropertyPrefTable;
pref->mPropID != eCSSProperty_UNKNOWN; pref++) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1472523
// We need to use nsCString instead of substring because the preference
// callback code stores them. Using AssignLiteral prevents any
// unnecessary allocations.
nsCString prefName;
prefName.AssignLiteral(pref->mPref, strlen(pref->mPref));
Preferences::RegisterCallback(nsCSSProps::RecomputeEnabledState,
prefName);
}
RecomputeEnabledState(/* aPrefName = */ nullptr);
void nsCSSProps::Init() {
MOZ_ASSERT(!gFontDescTable, "pre existing array!");
MOZ_ASSERT(!gCounterDescTable, "pre existing array!");
MOZ_ASSERT(!gPropertyIDLNameTable, "pre existing array!");

gFontDescTable = CreateStaticTable(kCSSRawFontDescs, eCSSFontDesc_COUNT);
gCounterDescTable =
CreateStaticTable(kCSSRawCounterDescs, eCSSCounterDesc_COUNT);

gPropertyIDLNameTable = new nsTHashMap<nsCStringHashKey, nsCSSPropertyID>;
for (nsCSSPropertyID p = nsCSSPropertyID(0);
size_t(p) < ArrayLength(kIDLNameTable); p = nsCSSPropertyID(p + 1)) {
if (kIDLNameTable[p]) {
gPropertyIDLNameTable->InsertOrUpdate(
nsDependentCString(kIDLNameTable[p]), p);
}
}
}

void nsCSSProps::ReleaseTable(void) {
if (0 == --gPropertyTableRefCount) {
gFontDescTable = nullptr;
gCounterDescTable = nullptr;
gPropertyIDLNameTable = nullptr;
ClearOnShutdown(&gFontDescTable);
ClearOnShutdown(&gCounterDescTable);
ClearOnShutdown(&gPropertyIDLNameTable);

for (const PropertyPref* pref = kPropertyPrefTable;
pref->mPropID != eCSSProperty_UNKNOWN; pref++) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1472523
// We need to use nsCString instead of substring because the preference
// callback code stores them. Using AssignLiteral prevents any
// unnecessary allocations.
nsCString prefName;
prefName.AssignLiteral(pref->mPref, strlen(pref->mPref));
Preferences::RegisterCallback(nsCSSProps::RecomputeEnabledState, prefName);
}
RecomputeEnabledState(/* aPrefName = */ nullptr);
}

/* static */
Expand Down Expand Up @@ -192,10 +181,10 @@ const nsCString& nsCSSProps::GetStringValue(nsCSSCounterDesc aCounterDescID) {
return sDescNullStr;
}

bool nsCSSProps::PropHasFlags(nsCSSPropertyID aProperty, Flags aFlags) {
CSSPropFlags nsCSSProps::PropFlags(nsCSSPropertyID aProperty) {
MOZ_ASSERT(0 <= aProperty && aProperty < eCSSProperty_COUNT_with_aliases,
"out of range");
return (kFlagsTable[aProperty] & aFlags) == aFlags;
return kFlagsTable[aProperty];
}

/* static */
Expand Down
33 changes: 13 additions & 20 deletions layout/style/nsCSSProps.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@
#ifndef nsCSSProps_h___
#define nsCSSProps_h___

#include <limits>
#include <ostream>
#include <type_traits>

#include "nsString.h"
#include "nsCSSPropertyID.h"
#include "nsStyleStructFwd.h"
#include "mozilla/UseCounter.h"
#include "mozilla/CSSEnabledState.h"
#include "mozilla/CSSPropFlags.h"
#include "mozilla/EnumTypeTraits.h"
#include "mozilla/Preferences.h"
#include "mozilla/gfx/gfxVarReceiver.h"
#include "nsXULAppAPI.h"

// Length of the "--" prefix on custom names (such as custom property names,
// and, in the future, custom media query names).
#define CSS_CUSTOM_NAME_PREFIX_LENGTH 2

namespace mozilla {
class ComputedStyle;
namespace gfx {
class gfxVarReceiver;
}
} // namespace mozilla

extern "C" {
nsCSSPropertyID Servo_ResolveLogicalProperty(nsCSSPropertyID,
Expand All @@ -44,11 +42,10 @@ const uint8_t* Servo_Property_GetName(nsCSSPropertyID, uint32_t* aLength);

class nsCSSProps {
public:
typedef mozilla::CSSEnabledState EnabledState;
typedef mozilla::CSSPropFlags Flags;
using EnabledState = mozilla::CSSEnabledState;
using Flags = mozilla::CSSPropFlags;

static void AddRefTable(void);
static void ReleaseTable(void);
static void Init();

// Looks up the property with name aProperty and returns its corresponding
// nsCSSPropertyID value. If aProperty is the name of a custom property,
Expand Down Expand Up @@ -76,7 +73,7 @@ class nsCSSProps {
}

// Same but for @font-face descriptors
static nsCSSFontDesc LookupFontDesc(const nsACString& aProperty);
static nsCSSFontDesc LookupFontDesc(const nsACString&);

// The relevant invariants are asserted in Document.cpp
static mozilla::UseCounter UseCounterFor(nsCSSPropertyID aProperty) {
Expand All @@ -89,7 +86,7 @@ class nsCSSProps {
// Given a property enum, get the string value
//
// This string is static.
static const nsDependentCSubstring GetStringValue(nsCSSPropertyID aProperty) {
static nsDependentCSubstring GetStringValue(nsCSSPropertyID aProperty) {
uint32_t len;
const uint8_t* chars = Servo_Property_GetName(aProperty, &len);
return nsDependentCSubstring(reinterpret_cast<const char*>(chars), len);
Expand All @@ -98,7 +95,10 @@ class nsCSSProps {
static const nsCString& GetStringValue(nsCSSFontDesc aFontDesc);
static const nsCString& GetStringValue(nsCSSCounterDesc aCounterDesc);

static bool PropHasFlags(nsCSSPropertyID aProperty, Flags aFlags);
static Flags PropFlags(nsCSSPropertyID);
static bool PropHasFlags(nsCSSPropertyID aProperty, Flags aFlags) {
return (PropFlags(aProperty) & aFlags) == aFlags;
}

static nsCSSPropertyID Physicalize(nsCSSPropertyID aProperty,
const mozilla::ComputedStyle& aStyle) {
Expand Down Expand Up @@ -145,10 +145,9 @@ class nsCSSProps {

private:
static bool gPropertyEnabled[eCSSProperty_COUNT_with_aliases];

private:
// Defined in the generated nsCSSPropsGenerated.inc.
static const char* const kIDLNameTable[eCSSProperty_COUNT];
static const int32_t kIDLNameSortPositionTable[eCSSProperty_COUNT];

public:
/**
Expand All @@ -167,10 +166,6 @@ class nsCSSProps {
return kIDLNameTable[aProperty];
}

private:
static const int32_t kIDLNameSortPositionTable[eCSSProperty_COUNT];

public:
/**
* Returns the position of the specified property in a list of all
* properties sorted by their IDL name.
Expand All @@ -181,7 +176,6 @@ class nsCSSProps {
return kIDLNameSortPositionTable[aProperty];
}

public:
static bool IsEnabled(nsCSSPropertyID aProperty, EnabledState aEnabled) {
MOZ_ASSERT(0 <= aProperty && aProperty < eCSSProperty_COUNT_with_aliases,
"out of range");
Expand All @@ -206,7 +200,6 @@ class nsCSSProps {
return false;
}

public:
struct PropertyPref {
nsCSSPropertyID mPropID;
const char* mPref;
Expand Down

0 comments on commit 5502bf7

Please sign in to comment.