SelectionChangeEventDispatcher.h (2422B)
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_SelectionChangeEventDispatcher_h 8 #define mozilla_SelectionChangeEventDispatcher_h 9 10 #include "mozilla/Attributes.h" 11 #include "nsCOMPtr.h" 12 #include "nsCycleCollectionParticipant.h" 13 #include "nsDirection.h" 14 #include "nsTArray.h" 15 16 class nsINode; 17 class nsRange; 18 19 namespace mozilla { 20 21 namespace dom { 22 class Document; 23 class Selection; 24 } // namespace dom 25 26 class SelectionChangeEventDispatcher final { 27 public: 28 // SelectionChangeEventDispatcher has to participate in cycle collection 29 // because it holds strong references to nsINodes in its mOldRanges array. 30 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING( 31 SelectionChangeEventDispatcher) 32 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(SelectionChangeEventDispatcher) 33 34 MOZ_CAN_RUN_SCRIPT 35 void OnSelectionChange(dom::Document* aDocument, dom::Selection* aSelection, 36 int16_t aReason); 37 38 // This field is used to keep track of the ranges which were present in the 39 // selection when the selectionchange event was previously fired. This allows 40 // for the selectionchange event to only be fired when a selection is actually 41 // changed. 42 struct RawRangeData { 43 // These properties are not void*s to avoid the potential situation where 44 // the nsINode is freed, and a new nsINode is allocated with the same 45 // address, which could potentially break the comparison logic. In reality, 46 // this is extremely unlikely to occur (potentially impossible), but these 47 // nsCOMPtrs are safer. They are never dereferenced. 48 nsCOMPtr<nsINode> mStartContainer; 49 nsCOMPtr<nsINode> mEndContainer; 50 51 // XXX These are int32_ts on nsRange, but uint32_ts in the return value 52 // of GetStart_, so I use uint32_ts here. See bug 1194256. 53 uint32_t mStartOffset; 54 uint32_t mEndOffset; 55 56 explicit RawRangeData(const nsRange* aRange); 57 bool Equals(const nsRange* aRange); 58 }; 59 60 private: 61 nsTArray<RawRangeData> mOldRanges; 62 nsDirection mOldDirection; 63 64 ~SelectionChangeEventDispatcher() = default; 65 }; 66 67 } // namespace mozilla 68 69 #endif // mozilla_SelectionChangeEventDispatcher_h