Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / layout / base / nsRefreshObservers.h
blobf1516cc4c1a094f1c248ac885fe23a1a1090680a
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * Code to notify things that animate before a refresh, at an appropriate
9 * refresh rate. (Perhaps temporary, until replaced by compositor.)
12 #ifndef LAYOUT_BASE_NSREFRESHOBSERVERS_H_
13 #define LAYOUT_BASE_NSREFRESHOBSERVERS_H_
15 #include <functional>
17 #include "mozilla/Attributes.h"
18 #include "mozilla/TimeStamp.h"
19 #include "nsISupports.h"
21 class nsPresContext;
23 namespace mozilla {
24 class AnimationEventDispatcher;
25 class PendingFullscreenEvent;
26 class PresShell;
27 class RefreshDriverTimer;
28 } // namespace mozilla
30 /**
31 * An abstract base class to be implemented by callers wanting to be
32 * notified at refresh times. When nothing needs to be painted, callers
33 * may not be notified.
35 class nsARefreshObserver {
36 public:
37 // AddRef and Release signatures that match nsISupports. Implementors
38 // must implement reference counting, and those that do implement
39 // nsISupports will already have methods with the correct signature.
41 // The refresh driver does NOT hold references to refresh observers
42 // except while it is notifying them.
43 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
45 MOZ_CAN_RUN_SCRIPT virtual void WillRefresh(mozilla::TimeStamp aTime) = 0;
47 #ifdef DEBUG
48 // Count of the active registrations we currently have with nsRefreshDrivers.
49 // This is incremented in nsRefreshDriver::AddRefreshObserver, decremented in
50 // nsRefreshDriver::RemoveRefreshObserver. No other code should modify this.
51 // We expect this to be 0 when we're destructed.
52 int32_t mRegistrationCount = 0;
54 // Virtual destructor to enforce the mRegistrationCount invariant.
55 virtual ~nsARefreshObserver() {
56 MOZ_ASSERT(mRegistrationCount == 0,
57 "Refresh observer AddRefreshObserver/RemoveRefreshObserver "
58 "calls should have balanced out to zero");
60 #endif // DEBUG
63 /**
64 * An abstract base class to be implemented by callers wanting to be notified
65 * when the observing refresh driver updated mMostRecentRefresh due to active
66 * timer changes. Callers must ensure an observer is removed before it is
67 * destroyed.
69 class nsATimerAdjustmentObserver {
70 public:
71 virtual void NotifyTimerAdjusted(mozilla::TimeStamp aTime) = 0;
74 /**
75 * An abstract base class to be implemented by callers wanting to be notified
76 * that a refresh has occurred. Callers must ensure an observer is removed
77 * before it is destroyed.
79 class nsAPostRefreshObserver {
80 public:
81 virtual void DidRefresh() = 0;
84 namespace mozilla {
86 /**
87 * A wrapper for nsAPostRefreshObserver that's refcounted and might unregister
88 * itself after firing.
90 * Note, while the observer unregisters itself, the registering still needs to
91 * be done by the caller.
93 class ManagedPostRefreshObserver : public nsAPostRefreshObserver {
94 public:
95 // Whether the post-refresh observer should be unregistered after it has
96 // fired.
97 enum class Unregister : bool { No, Yes };
98 using Action = std::function<Unregister(bool aWasCanceled)>;
99 NS_INLINE_DECL_REFCOUNTING(ManagedPostRefreshObserver)
100 ManagedPostRefreshObserver(nsPresContext*, Action&&);
101 explicit ManagedPostRefreshObserver(nsPresContext*);
102 void DidRefresh() override;
103 void Cancel();
105 protected:
106 virtual ~ManagedPostRefreshObserver();
107 RefPtr<nsPresContext> mPresContext;
108 Action mAction;
111 } // namespace mozilla
113 #endif