nsRefreshObservers.h (3281B)
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/. */ 6 7 /* 8 * Code to notify things that animate before a refresh, at an appropriate 9 * refresh rate. (Perhaps temporary, until replaced by compositor.) 10 */ 11 12 #ifndef LAYOUT_BASE_NSREFRESHOBSERVERS_H_ 13 #define LAYOUT_BASE_NSREFRESHOBSERVERS_H_ 14 15 #include <functional> 16 17 #include "mozilla/Attributes.h" 18 #include "mozilla/TimeStamp.h" 19 #include "nsISupports.h" 20 21 class nsPresContext; 22 23 namespace mozilla { 24 class AnimationEventDispatcher; 25 class PendingFullscreenEvent; 26 class PresShell; 27 class RefreshDriverTimer; 28 } // namespace mozilla 29 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. 34 */ 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. 40 // 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 44 45 MOZ_CAN_RUN_SCRIPT virtual void WillRefresh(mozilla::TimeStamp aTime) = 0; 46 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; 53 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"); 59 } 60 #endif // DEBUG 61 }; 62 63 /** 64 * An abstract base class to be implemented by callers wanting to be notified 65 * that a refresh has occurred. Callers must ensure an observer is removed 66 * before it is destroyed. 67 */ 68 class nsAPostRefreshObserver { 69 public: 70 virtual void DidRefresh() = 0; 71 }; 72 73 namespace mozilla { 74 75 /** 76 * A wrapper for nsAPostRefreshObserver that's refcounted and might unregister 77 * itself after firing. 78 * 79 * Note, while the observer unregisters itself, the registering still needs to 80 * be done by the caller. 81 */ 82 class ManagedPostRefreshObserver : public nsAPostRefreshObserver { 83 public: 84 // Whether the post-refresh observer should be unregistered after it has 85 // fired. 86 enum class Unregister : bool { No, Yes }; 87 using Action = std::function<Unregister(bool aWasCanceled)>; 88 NS_INLINE_DECL_REFCOUNTING(ManagedPostRefreshObserver) 89 ManagedPostRefreshObserver(nsPresContext*, Action&&); 90 explicit ManagedPostRefreshObserver(nsPresContext*); 91 void DidRefresh() override; 92 void Cancel(); 93 94 protected: 95 virtual ~ManagedPostRefreshObserver(); 96 RefPtr<nsPresContext> mPresContext; 97 Action mAction; 98 }; 99 100 } // namespace mozilla 101 102 #endif