TimelineCollection.h (2213B)
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_TimelineCollection_h 8 #define mozilla_TimelineCollection_h 9 10 #include "mozilla/LinkedList.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/PseudoStyleType.h" 13 #include "mozilla/RefPtr.h" 14 #include "nsAtomHashKeys.h" 15 #include "nsTHashMap.h" 16 17 class nsAtom; 18 19 namespace mozilla { 20 namespace dom { 21 class Element; 22 } 23 24 // The collection of ScrollTimeline or ViewTimeline. We use the template class 25 // to share the implementation for these two timeline types. 26 template <class TimelineType> 27 class TimelineCollection final 28 : public LinkedListElement<TimelineCollection<TimelineType>> { 29 public: 30 using SelfType = TimelineCollection<TimelineType>; 31 using TimelineMap = nsTHashMap<RefPtr<nsAtom>, RefPtr<TimelineType>>; 32 33 TimelineCollection(dom::Element& aElement, 34 const PseudoStyleRequest& aPseudoRequest) 35 : mElement(aElement), mPseudo(aPseudoRequest) { 36 MOZ_COUNT_CTOR(TimelineCollection); 37 } 38 39 ~TimelineCollection(); 40 41 already_AddRefed<TimelineType> Lookup(nsAtom* aName) const { 42 return mTimelines.Get(aName).forget(); 43 } 44 45 already_AddRefed<TimelineType> Extract(nsAtom* aName) { 46 Maybe<RefPtr<TimelineType>> timeline = mTimelines.Extract(aName); 47 return timeline ? timeline->forget() : nullptr; 48 } 49 50 void Swap(TimelineMap& aValue) { mTimelines.SwapElements(aValue); } 51 52 void Destroy(); 53 54 // Get the collection of timelines for the given |aElement| and or create it 55 // if it does not already exist. 56 static TimelineCollection* Get(const dom::Element* aElement, 57 const PseudoStyleRequest& aPseudoRequest); 58 const TimelineMap& Timelines() const { return mTimelines; } 59 60 private: 61 // The element. Weak reference is fine since it owns us. 62 dom::Element& mElement; 63 const PseudoStyleRequest mPseudo; 64 65 TimelineMap mTimelines; 66 }; 67 68 } // namespace mozilla 69 70 #endif // mozilla_TimelineCollection_h