CoalescedInputData.h (2977B)
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 #ifndef mozilla_dom_CoalescedInputData_h 8 #define mozilla_dom_CoalescedInputData_h 9 10 #include "mozilla/UniquePtr.h" 11 #include "mozilla/layers/ScrollableLayerGuid.h" 12 #include "nsRefreshObservers.h" 13 14 class nsRefreshDriver; 15 16 namespace mozilla::dom { 17 18 class BrowserChild; 19 20 template <class InputEventType> 21 class CoalescedInputData { 22 protected: 23 using ScrollableLayerGuid = mozilla::layers::ScrollableLayerGuid; 24 25 UniquePtr<InputEventType> mCoalescedInputEvent; 26 ScrollableLayerGuid mGuid; 27 uint64_t mInputBlockId = 0; 28 uint32_t mGeneration = 0; 29 30 void AdvanceGeneration() { 31 if (!IsEmpty()) { 32 mGeneration++; 33 } 34 } 35 36 public: 37 CoalescedInputData() = default; 38 39 void RetrieveDataFrom(CoalescedInputData& aSource) { 40 aSource.AdvanceGeneration(); 41 AdvanceGeneration(); 42 mCoalescedInputEvent = std::move(aSource.mCoalescedInputEvent); 43 mGuid = aSource.mGuid; 44 mInputBlockId = aSource.mInputBlockId; 45 } 46 47 bool IsEmpty() { return !mCoalescedInputEvent; } 48 49 bool CanCoalesce(const InputEventType& aEvent, 50 const ScrollableLayerGuid& aGuid, 51 const uint64_t& aInputBlockId); 52 53 UniquePtr<InputEventType> TakeCoalescedEvent() { 54 AdvanceGeneration(); 55 return std::move(mCoalescedInputEvent); 56 } 57 58 ScrollableLayerGuid GetScrollableLayerGuid() { return mGuid; } 59 60 uint64_t GetInputBlockId() { return mInputBlockId; } 61 62 /** 63 * The generation number of the latest state stored by the instance. 64 * It'll be incremented when the coalesced event data is retrieved or taken. 65 * So, this is useful to avoid handling same coalesced events twice when 66 * a nested event loop may handle this. 67 * NOTE: You should compare the value only with `==` or `!=`. Do not use 68 * `<` nor `>` because the value may circulate to 0 from UINT32_MAX. 69 */ 70 [[nodiscard]] uint32_t Generation() const { return mGeneration; } 71 }; 72 73 class CoalescedInputFlusher : public nsARefreshObserver { 74 public: 75 explicit CoalescedInputFlusher(BrowserChild* aBrowserChild); 76 77 virtual void WillRefresh(mozilla::TimeStamp aTime) override = 0; 78 79 NS_INLINE_DECL_REFCOUNTING(CoalescedInputFlusher, override) 80 81 void StartObserver(); 82 void RemoveObserver(); 83 84 /** 85 * Return a refresh driver which is proper one for BrowserChild. 86 * Note that this is not a getter of mRefreshDriver. 87 */ 88 [[nodiscard]] nsRefreshDriver* GetRefreshDriver(); 89 90 protected: 91 virtual ~CoalescedInputFlusher(); 92 93 BrowserChild* mBrowserChild; 94 // A refresh driver which this instance waits for the next refresh of. 95 RefPtr<nsRefreshDriver> mRefreshDriver; 96 }; 97 } // namespace mozilla::dom 98 99 #endif // mozilla_dom_CoalescedInputData_h