1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_ResizeObserverController_h
8 #define mozilla_dom_ResizeObserverController_h
10 #include "mozilla/dom/ResizeObserver.h"
11 #include "mozilla/TimeStamp.h"
12 #include "nsRefreshDriver.h"
13 #include "nsTObserverArray.h"
22 class ResizeObserverController
;
25 * ResizeObserverNotificationHelper will trigger ResizeObserver notifications
26 * by registering with the Refresh Driver.
28 class ResizeObserverNotificationHelper final
: public nsARefreshObserver
{
30 NS_INLINE_DECL_REFCOUNTING(ResizeObserverNotificationHelper
, override
)
32 explicit ResizeObserverNotificationHelper(ResizeObserverController
* aOwner
)
33 : mOwner(aOwner
), mRegistered(false) {
34 MOZ_ASSERT(mOwner
, "Need a non-null owner");
37 MOZ_CAN_RUN_SCRIPT
void WillRefresh(TimeStamp aTime
) override
;
39 nsRefreshDriver
* GetRefreshDriver() const;
45 bool IsRegistered() const { return mRegistered
; }
47 void DetachFromOwner() { mOwner
= nullptr; }
50 virtual ~ResizeObserverNotificationHelper();
52 ResizeObserverController
* mOwner
;
57 * ResizeObserverController contains the list of ResizeObservers and controls
58 * the flow of notification.
60 class ResizeObserverController final
{
62 explicit ResizeObserverController(Document
* aDocument
)
63 : mDocument(aDocument
),
64 mResizeObserverNotificationHelper(
65 new ResizeObserverNotificationHelper(this)) {
66 MOZ_ASSERT(mDocument
, "Need a non-null document");
69 // Methods for supporting cycle-collection
70 void Traverse(nsCycleCollectionTraversalCallback
& aCb
);
73 void ShellDetachedFromDocument();
74 void AddResizeObserver(ResizeObserver
* aObserver
);
77 * Schedule the notification via ResizeObserverNotificationHelper refresh
80 void ScheduleNotification();
83 * Notify all ResizeObservers by gathering and broadcasting all active
86 MOZ_CAN_RUN_SCRIPT
void Notify();
88 PresShell
* GetPresShell() const { return mDocument
->GetPresShell(); }
90 ~ResizeObserverController();
94 * Calls GatherActiveObservations(aDepth) for all ResizeObservers in this
95 * controller. All observations in each ResizeObserver with element's depth
96 * more than aDepth will be gathered.
98 void GatherAllActiveObservations(uint32_t aDepth
);
101 * Calls BroadcastActiveObservations() for all ResizeObservers in this
102 * controller. It also returns the shallowest depth of observed target
103 * elements with active observations from all ResizeObservers or
104 * numeric_limits<uint32_t>::max() if there aren't any active observations
107 MOZ_CAN_RUN_SCRIPT
uint32_t BroadcastAllActiveObservations();
110 * Returns whether there is any ResizeObserver that has active observations.
112 bool HasAnyActiveObservations() const;
115 * Returns whether there is any ResizeObserver that has skipped observations.
117 bool HasAnySkippedObservations() const;
119 // Raw pointer is OK because mDocument strongly owns us & hence must outlive
121 Document
* const mDocument
;
123 RefPtr
<ResizeObserverNotificationHelper
> mResizeObserverNotificationHelper
;
124 nsTObserverArray
<RefPtr
<ResizeObserver
>> mResizeObservers
;
128 } // namespace mozilla
130 #endif // mozilla_dom_ResizeObserverController_h