Bug 1880928 - Make ResizeObserver::mCallback a ResizeObserverCallback again. r=Oriol

Essentially reverting D154324 since we no longer need it to accept
internal callback functions.

Differential Revision: https://phabricator.services.mozilla.com/D202555
This commit is contained in:
Frédéric Wang 2024-02-29 08:50:57 +00:00
parent c9e3e2d833
commit e31798198b
3 changed files with 9 additions and 52 deletions

View file

@ -17171,14 +17171,8 @@ bool Document::IsExtensionPage() const {
void Document::AddResizeObserver(ResizeObserver& aObserver) { void Document::AddResizeObserver(ResizeObserver& aObserver) {
MOZ_ASSERT(!mResizeObservers.Contains(&aObserver)); MOZ_ASSERT(!mResizeObservers.Contains(&aObserver));
// Insert internal ResizeObservers before scripted ones, since they may have
// observable side-effects and we don't want to expose the insertion time.
if (aObserver.HasNativeCallback()) {
mResizeObservers.InsertElementAt(0, &aObserver);
} else {
mResizeObservers.AppendElement(&aObserver); mResizeObservers.AppendElement(&aObserver);
} }
}
void Document::RemoveResizeObserver(ResizeObserver& aObserver) { void Document::RemoveResizeObserver(ResizeObserver& aObserver) {
MOZ_ASSERT(mResizeObservers.Contains(&aObserver)); MOZ_ASSERT(mResizeObservers.Contains(&aObserver));

View file

@ -214,23 +214,14 @@ void ResizeObservation::UpdateLastReportedSize(
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ResizeObserver) NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ResizeObserver)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ResizeObserver) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ResizeObserver)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner, mDocument, mActiveTargets, NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner, mDocument, mCallback,
mObservationMap); mActiveTargets, mObservationMap);
if (tmp->mCallback.is<RefPtr<ResizeObserverCallback>>()) {
ImplCycleCollectionTraverse(
cb, tmp->mCallback.as<RefPtr<ResizeObserverCallback>>(), "mCallback",
0);
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ResizeObserver) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ResizeObserver)
tmp->Disconnect(); tmp->Disconnect();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner, mDocument, mActiveTargets, NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner, mDocument, mCallback, mActiveTargets,
mObservationMap); mObservationMap);
if (tmp->mCallback.is<RefPtr<ResizeObserverCallback>>()) {
ImplCycleCollectionUnlink(
tmp->mCallback.as<RefPtr<ResizeObserverCallback>>());
}
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_UNLINK_END
@ -241,14 +232,6 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ResizeObserver)
NS_INTERFACE_MAP_ENTRY(nsISupports) NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END NS_INTERFACE_MAP_END
ResizeObserver::ResizeObserver(Document& aDocument, NativeCallback aCallback)
: mOwner(aDocument.GetInnerWindow()),
mDocument(&aDocument),
mCallback(aCallback) {
MOZ_ASSERT(mOwner, "Need a non-null owner window");
MOZ_ASSERT(mDocument == mOwner->GetExtantDoc());
}
already_AddRefed<ResizeObserver> ResizeObserver::Constructor( already_AddRefed<ResizeObserver> ResizeObserver::Constructor(
const GlobalObject& aGlobal, ResizeObserverCallback& aCb, const GlobalObject& aGlobal, ResizeObserverCallback& aCb,
ErrorResult& aRv) { ErrorResult& aRv) {
@ -354,15 +337,10 @@ void ResizeObserver::GatherActiveObservations(uint32_t aDepth) {
if (targetDepth > aDepth) { if (targetDepth > aDepth) {
mActiveTargets.AppendElement(observation); mActiveTargets.AppendElement(observation);
} else { } else {
// This boolean is only used to indicate we will deliver resize loop error
// notification later on. However, we don't want to do that for our
// internal observers.
if (!HasNativeCallback()) {
mHasSkippedTargets = true; mHasSkippedTargets = true;
} }
} }
} }
}
uint32_t ResizeObserver::BroadcastActiveObservations() { uint32_t ResizeObserver::BroadcastActiveObservations() {
uint32_t shallowestTargetDepth = std::numeric_limits<uint32_t>::max(); uint32_t shallowestTargetDepth = std::numeric_limits<uint32_t>::max();
@ -412,13 +390,8 @@ uint32_t ResizeObserver::BroadcastActiveObservations() {
} }
} }
if (mCallback.is<RefPtr<ResizeObserverCallback>>()) { RefPtr<ResizeObserverCallback> callback(mCallback);
RefPtr<ResizeObserverCallback> callback(
mCallback.as<RefPtr<ResizeObserverCallback>>());
callback->Call(this, entries, *this); callback->Call(this, entries, *this);
} else {
mCallback.as<NativeCallback>()(entries, *this);
}
mActiveTargets.Clear(); mActiveTargets.Clear();
mHasSkippedTargets = false; mHasSkippedTargets = false;

View file

@ -121,9 +121,6 @@ class ResizeObservation final : public LinkedListElement<ResizeObservation> {
* https://drafts.csswg.org/resize-observer/#api * https://drafts.csswg.org/resize-observer/#api
*/ */
class ResizeObserver final : public nsISupports, public nsWrapperCache { class ResizeObserver final : public nsISupports, public nsWrapperCache {
using NativeCallback = void (*)(
const Sequence<OwningNonNull<ResizeObserverEntry>>&, ResizeObserver&);
ResizeObserver(Document& aDocument, NativeCallback aCallback);
public: public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@ -131,9 +128,7 @@ class ResizeObserver final : public nsISupports, public nsWrapperCache {
ResizeObserver(nsCOMPtr<nsPIDOMWindowInner>&& aOwner, Document* aDocument, ResizeObserver(nsCOMPtr<nsPIDOMWindowInner>&& aOwner, Document* aDocument,
ResizeObserverCallback& aCb) ResizeObserverCallback& aCb)
: mOwner(std::move(aOwner)), : mOwner(std::move(aOwner)), mDocument(aDocument), mCallback(&aCb) {
mDocument(aDocument),
mCallback(RefPtr<ResizeObserverCallback>(&aCb)) {
MOZ_ASSERT(mOwner, "Need a non-null owner window"); MOZ_ASSERT(mOwner, "Need a non-null owner window");
MOZ_ASSERT(mDocument, "Need a non-null doc"); MOZ_ASSERT(mDocument, "Need a non-null doc");
MOZ_ASSERT(mDocument == mOwner->GetExtantDoc()); MOZ_ASSERT(mDocument == mOwner->GetExtantDoc());
@ -176,11 +171,6 @@ class ResizeObserver final : public nsISupports, public nsWrapperCache {
*/ */
bool HasSkippedObservations() const { return mHasSkippedTargets; } bool HasSkippedObservations() const { return mHasSkippedTargets; }
/**
* Returns whether this is an internal ResizeObserver with a native callback.
*/
bool HasNativeCallback() const { return mCallback.is<NativeCallback>(); }
/** /**
* Invoke the callback function in JavaScript for all active observations * Invoke the callback function in JavaScript for all active observations
* and pass the sequence of ResizeObserverEntry so JavaScript can access them. * and pass the sequence of ResizeObserverEntry so JavaScript can access them.
@ -213,7 +203,7 @@ class ResizeObserver final : public nsISupports, public nsWrapperCache {
nsCOMPtr<nsPIDOMWindowInner> mOwner; nsCOMPtr<nsPIDOMWindowInner> mOwner;
// The window's document at the time of ResizeObserver creation. // The window's document at the time of ResizeObserver creation.
RefPtr<Document> mDocument; RefPtr<Document> mDocument;
Variant<RefPtr<ResizeObserverCallback>, NativeCallback> mCallback; RefPtr<ResizeObserverCallback> mCallback;
nsTArray<RefPtr<ResizeObservation>> mActiveTargets; nsTArray<RefPtr<ResizeObservation>> mActiveTargets;
// The spec uses a list to store the skipped targets. However, it seems what // The spec uses a list to store the skipped targets. However, it seems what
// we want is to check if there are any skipped targets (i.e. existence). // we want is to check if there are any skipped targets (i.e. existence).