CachedInheritingStyles.cpp (2122B)
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 #include "mozilla/CachedInheritingStyles.h" 8 9 #include "mozilla/ComputedStyle.h" 10 #include "nsCOMPtr.h" 11 #include "nsWindowSizes.h" 12 13 namespace mozilla { 14 15 void CachedInheritingStyles::Insert(ComputedStyle* aStyle) { 16 MOZ_ASSERT(aStyle); 17 MOZ_ASSERT(aStyle->IsInheritingAnonBox() || 18 aStyle->IsLazilyCascadedPseudoElement()); 19 20 if (IsEmpty()) { 21 RefPtr<ComputedStyle> s = aStyle; 22 mBits = reinterpret_cast<uintptr_t>(s.forget().take()); 23 MOZ_ASSERT(!IsEmpty() && !IsIndirect()); 24 } else if (IsIndirect()) { 25 AsIndirect()->AppendElement(aStyle); 26 } else { 27 IndirectCache* cache = new IndirectCache(); 28 cache->AppendElement(dont_AddRef(AsDirect())); 29 cache->AppendElement(aStyle); 30 mBits = reinterpret_cast<uintptr_t>(cache) | 1; 31 MOZ_ASSERT(IsIndirect()); 32 } 33 } 34 35 ComputedStyle* CachedInheritingStyles::Lookup(PseudoStyleType aType) const { 36 MOZ_ASSERT(PseudoStyle::IsPseudoElement(aType) || 37 PseudoStyle::IsInheritingAnonBox(aType)); 38 if (IsIndirect()) { 39 for (auto& style : *AsIndirect()) { 40 if (style->GetPseudoType() == aType) { 41 return style; 42 } 43 } 44 45 return nullptr; 46 } 47 48 ComputedStyle* direct = AsDirect(); 49 return direct && direct->GetPseudoType() == aType ? direct : nullptr; 50 } 51 52 void CachedInheritingStyles::AddSizeOfIncludingThis(nsWindowSizes& aSizes, 53 size_t* aCVsSize) const { 54 if (IsIndirect()) { 55 for (auto& style : *AsIndirect()) { 56 if (!aSizes.mState.HaveSeenPtr(style)) { 57 style->AddSizeOfIncludingThis(aSizes, aCVsSize); 58 } 59 } 60 61 return; 62 } 63 64 ComputedStyle* direct = AsDirect(); 65 if (direct && !aSizes.mState.HaveSeenPtr(direct)) { 66 direct->AddSizeOfIncludingThis(aSizes, aCVsSize); 67 } 68 } 69 70 } // namespace mozilla