Files
palemoon27/layout/base/FramePropertyTable.h
T
roytam1 279b2f1b52 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1136897 - Don't evict expired entries from disk. r=michal (6af5d87e8)
- Bug 1188745 - Rename nsTArray::SizeOfExcludingThis() as ShallowSizeOfExcludingThis(). r=froydnj. (3dbb2875a)
- Bug 1150810 part 1 - Move DocumentTimeline methods up to AnimationTimeline; r=jwatt (3d8c679af)
- Bug 1171817 part 1 - Cancel animations when destroying the property holding them; r=dbaron (6f28fbf46)
- Bug 1171817 part 2 - Add CSSAnimation::GetOwningElement; r=dbaron (700decca6)
- Bug 1171817 part 3 - Add CSSTransition::GetOwningElement; r=dbaron (e6da3c726)
- Bug 1171817 part 4 - Add const version of AsCSSAnimation/AsCSSTransition methods; r=dbaron (f264ebad7)
- Bug 1171817 part 5 - Add a sequence number member to Animations; r=dbaron (b041eeb6b)
- Bug 1171817 part 6 - Add Animation::HasLowerCompositeOrderThan; r=dbaron (df97cf295)
- Bug 1171817 part 7 - Add Animation::IsUsingCustomCompositeOrder; r=dbaron (5735f03a2)
- Bug 1171817 part 8 - Override sequence numbers for CSS animations; r=dbaron (50671de0f)
- Bug 1171817 part 9 - Add override of HasLowerCompositeOrderThan for CSS animations; r=dbaron (b467ffd3c)
- Bug 1171817 part 10 - Override sequence numbers for transitions; r=dbron (3703102f6)
- Bug 1171817 part 11 - Add CSSTransition::TransitionProperty(); r=dbaron (c889aa8ec)
- Bug 1171817 part 13 - Add override of HasLowerCompositeOrderThan for CSS transitions; r=dbaron (8c628cac4)
- Bug 1171817 part 14 - Add AnimationPtrComparator class; r=dbaron (c522180c9)
- Bug 1171817 part 15 - Factor out common code for comparing owning elements into a separate class; r=dbaron (132998a2c)
- Bug 1171817 part 16 - Always cancel transitions before removing them; r=dbaron (01113a650)
- Bug 1150810 part 2 - Replace references to DocumentTimeline with AnimationTimeline; r=jwatt (d377ba63f)
- Bug 1150810 part 3 - Make IsPossiblyOrphanedPendingAnimation return true when there is no rendered doc; r=jwatt (63b2b6760)
- Bug 1150810 part 4 - Store global on Animation; r=smaug, jwatt (fd099d639)
- Bug 1150810 part 5 - Handle Timeline() returning null; r=jwatt (641bd865d)
2021-03-26 17:01:09 +08:00

267 lines
8.8 KiB
C++

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* 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/. */
#ifndef FRAMEPROPERTYTABLE_H_
#define FRAMEPROPERTYTABLE_H_
#include "mozilla/MemoryReporting.h"
#include "nsTArray.h"
#include "nsTHashtable.h"
#include "nsHashKeys.h"
class nsIFrame;
namespace mozilla {
struct FramePropertyDescriptor;
typedef void (*FramePropertyDestructor)(void* aPropertyValue);
typedef void (*FramePropertyDestructorWithFrame)(nsIFrame* aFrame,
void* aPropertyValue);
/**
* A pointer to a FramePropertyDescriptor serves as a unique property ID.
* The FramePropertyDescriptor stores metadata about the property.
* Currently the only metadata is a destructor function. The destructor
* function is called on property values when they are overwritten or
* deleted.
*
* To use this class, declare a global (i.e., file, class or function-scope
* static member) FramePropertyDescriptor and pass its address as
* aProperty in the FramePropertyTable methods.
*/
struct FramePropertyDescriptor {
/**
* mDestructor will be called if it's non-null.
*/
FramePropertyDestructor mDestructor;
/**
* mDestructorWithFrame will be called if it's non-null and mDestructor
* is null. WARNING: The frame passed to mDestructorWithFrame may
* be a dangling frame pointer, if this is being called during
* presshell teardown. Do not use it except to compare against
* other frame pointers. No frame will have been allocated with
* the same address yet.
*/
FramePropertyDestructorWithFrame mDestructorWithFrame;
/**
* mDestructor and mDestructorWithFrame may both be null, in which case
* no value destruction is a no-op.
*/
};
/**
* The FramePropertyTable is optimized for storing 0 or 1 properties on
* a given frame. Storing very large numbers of properties on a single
* frame will not be efficient.
*
* Property values are passed as void* but do not actually have to be
* valid pointers. You can use NS_INT32_TO_PTR/NS_PTR_TO_INT32 to
* store int32_t values. Null/zero values can be stored and retrieved.
* Of course, the destructor function (if any) must handle such values
* correctly.
*/
class FramePropertyTable {
public:
FramePropertyTable() : mLastFrame(nullptr), mLastEntry(nullptr)
{
}
~FramePropertyTable()
{
DeleteAll();
}
/**
* Set a property value on a frame. This requires one hashtable
* lookup (using the frame as the key) and a linear search through
* the properties of that frame. Any existing value for the property
* is destroyed.
*/
void Set(nsIFrame* aFrame, const FramePropertyDescriptor* aProperty,
void* aValue);
/**
* Get a property value for a frame. This requires one hashtable
* lookup (using the frame as the key) and a linear search through
* the properties of that frame. If the frame has no such property,
* returns null.
* @param aFoundResult if non-null, receives a value 'true' iff
* the frame has a value for the property. This lets callers
* disambiguate a null result, which can mean 'no such property' or
* 'property value is null'.
*/
void* Get(const nsIFrame* aFrame, const FramePropertyDescriptor* aProperty,
bool* aFoundResult = nullptr);
/**
* Remove a property value for a frame. This requires one hashtable
* lookup (using the frame as the key) and a linear search through
* the properties of that frame. The old property value is returned
* (and not destroyed). If the frame has no such property,
* returns null.
* @param aFoundResult if non-null, receives a value 'true' iff
* the frame had a value for the property. This lets callers
* disambiguate a null result, which can mean 'no such property' or
* 'property value is null'.
*/
void* Remove(nsIFrame* aFrame, const FramePropertyDescriptor* aProperty,
bool* aFoundResult = nullptr);
/**
* Remove and destroy a property value for a frame. This requires one
* hashtable lookup (using the frame as the key) and a linear search
* through the properties of that frame. If the frame has no such
* property, nothing happens.
*/
void Delete(nsIFrame* aFrame, const FramePropertyDescriptor* aProperty);
/**
* Remove and destroy all property values for a frame. This requires one
* hashtable lookup (using the frame as the key).
*/
void DeleteAllFor(nsIFrame* aFrame);
/**
* Remove and destroy all property values for all frames.
*/
void DeleteAll();
/**
* Check if a property exists (added for TenFourFox issue 493).
*/
bool Has(const nsIFrame* aFrame, const FramePropertyDescriptor* aProperty)
{
bool foundResult = false;
(void)Get(aFrame, aProperty, &foundResult);
return foundResult;
}
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
protected:
/**
* Stores a property descriptor/value pair. It can also be used to
* store an nsTArray of PropertyValues.
*/
struct PropertyValue {
PropertyValue() : mProperty(nullptr), mValue(nullptr) {}
PropertyValue(const FramePropertyDescriptor* aProperty, void* aValue)
: mProperty(aProperty), mValue(aValue) {}
bool IsArray() { return !mProperty && mValue; }
nsTArray<PropertyValue>* ToArray()
{
NS_ASSERTION(IsArray(), "Must be array");
return reinterpret_cast<nsTArray<PropertyValue>*>(&mValue);
}
void DestroyValueFor(nsIFrame* aFrame) {
if (mProperty->mDestructor) {
mProperty->mDestructor(mValue);
} else if (mProperty->mDestructorWithFrame) {
mProperty->mDestructorWithFrame(aFrame, mValue);
}
}
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) {
size_t n = 0;
// We don't need to measure mProperty because it always points to static
// memory. As for mValue: if it's a single value we can't measure it,
// because the type is opaque; if it's an array, we measure the array
// storage, but we can't measure the individual values, again because
// their types are opaque.
if (IsArray()) {
nsTArray<PropertyValue>* array = ToArray();
n += array->ShallowSizeOfExcludingThis(aMallocSizeOf);
}
return n;
}
const FramePropertyDescriptor* mProperty;
void* mValue;
};
/**
* Used with an array of PropertyValues to allow lookups that compare
* only on the FramePropertyDescriptor.
*/
class PropertyComparator {
public:
bool Equals(const PropertyValue& a, const PropertyValue& b) const {
return a.mProperty == b.mProperty;
}
bool Equals(const FramePropertyDescriptor* a, const PropertyValue& b) const {
return a == b.mProperty;
}
bool Equals(const PropertyValue& a, const FramePropertyDescriptor* b) const {
return a.mProperty == b;
}
};
/**
* Our hashtable entry. The key is an nsIFrame*, the value is a
* PropertyValue representing one or more property/value pairs.
*/
class Entry : public nsPtrHashKey<nsIFrame>
{
public:
explicit Entry(KeyTypePointer aKey) : nsPtrHashKey<nsIFrame>(aKey) {}
Entry(const Entry &toCopy) :
nsPtrHashKey<nsIFrame>(toCopy), mProp(toCopy.mProp) {}
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) {
return mProp.SizeOfExcludingThis(aMallocSizeOf);
}
PropertyValue mProp;
};
static void DeleteAllForEntry(Entry* aEntry);
nsTHashtable<Entry> mEntries;
nsIFrame* mLastFrame;
Entry* mLastEntry;
};
/**
* This class encapsulates the properties of a frame.
*/
class FrameProperties {
public:
FrameProperties(FramePropertyTable* aTable, nsIFrame* aFrame)
: mTable(aTable), mFrame(aFrame) {}
FrameProperties(FramePropertyTable* aTable, const nsIFrame* aFrame)
: mTable(aTable), mFrame(const_cast<nsIFrame*>(aFrame)) {}
void Set(const FramePropertyDescriptor* aProperty, void* aValue) const
{
mTable->Set(mFrame, aProperty, aValue);
}
void* Get(const FramePropertyDescriptor* aProperty,
bool* aFoundResult = nullptr) const
{
return mTable->Get(mFrame, aProperty, aFoundResult);
}
void* Remove(const FramePropertyDescriptor* aProperty,
bool* aFoundResult = nullptr) const
{
return mTable->Remove(mFrame, aProperty, aFoundResult);
}
void Delete(const FramePropertyDescriptor* aProperty)
{
mTable->Delete(mFrame, aProperty);
}
// TenFourFox issue 493
bool Has(const FramePropertyDescriptor* aProperty)
{
bool foundResult;
(void)mTable->Get(mFrame, aProperty, &foundResult);
return foundResult;
}
private:
FramePropertyTable* mTable;
nsIFrame* mFrame;
};
} // namespace mozilla
#endif /* FRAMEPROPERTYTABLE_H_ */