Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "FramePropertyTable.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/MemoryReporting.h" |
michael@0 | 9 | |
michael@0 | 10 | namespace mozilla { |
michael@0 | 11 | |
michael@0 | 12 | void |
michael@0 | 13 | FramePropertyTable::Set(nsIFrame* aFrame, const FramePropertyDescriptor* aProperty, |
michael@0 | 14 | void* aValue) |
michael@0 | 15 | { |
michael@0 | 16 | NS_ASSERTION(aFrame, "Null frame?"); |
michael@0 | 17 | NS_ASSERTION(aProperty, "Null property?"); |
michael@0 | 18 | |
michael@0 | 19 | if (mLastFrame != aFrame || !mLastEntry) { |
michael@0 | 20 | mLastFrame = aFrame; |
michael@0 | 21 | mLastEntry = mEntries.PutEntry(aFrame); |
michael@0 | 22 | } |
michael@0 | 23 | Entry* entry = mLastEntry; |
michael@0 | 24 | |
michael@0 | 25 | if (!entry->mProp.IsArray()) { |
michael@0 | 26 | if (!entry->mProp.mProperty) { |
michael@0 | 27 | // Empty entry, so we can just store our property in the empty slot |
michael@0 | 28 | entry->mProp.mProperty = aProperty; |
michael@0 | 29 | entry->mProp.mValue = aValue; |
michael@0 | 30 | return; |
michael@0 | 31 | } |
michael@0 | 32 | if (entry->mProp.mProperty == aProperty) { |
michael@0 | 33 | // Just overwrite the current value |
michael@0 | 34 | entry->mProp.DestroyValueFor(aFrame); |
michael@0 | 35 | entry->mProp.mValue = aValue; |
michael@0 | 36 | return; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | // We need to expand the single current entry to an array |
michael@0 | 40 | PropertyValue current = entry->mProp; |
michael@0 | 41 | entry->mProp.mProperty = nullptr; |
michael@0 | 42 | static_assert(sizeof(nsTArray<PropertyValue>) <= sizeof(void *), |
michael@0 | 43 | "Property array must fit entirely within entry->mProp.mValue"); |
michael@0 | 44 | new (&entry->mProp.mValue) nsTArray<PropertyValue>(4); |
michael@0 | 45 | entry->mProp.ToArray()->AppendElement(current); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | nsTArray<PropertyValue>* array = entry->mProp.ToArray(); |
michael@0 | 49 | nsTArray<PropertyValue>::index_type index = |
michael@0 | 50 | array->IndexOf(aProperty, 0, PropertyComparator()); |
michael@0 | 51 | if (index != nsTArray<PropertyValue>::NoIndex) { |
michael@0 | 52 | PropertyValue* pv = &array->ElementAt(index); |
michael@0 | 53 | pv->DestroyValueFor(aFrame); |
michael@0 | 54 | pv->mValue = aValue; |
michael@0 | 55 | return; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | array->AppendElement(PropertyValue(aProperty, aValue)); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | void* |
michael@0 | 62 | FramePropertyTable::Get(const nsIFrame* aFrame, |
michael@0 | 63 | const FramePropertyDescriptor* aProperty, |
michael@0 | 64 | bool* aFoundResult) |
michael@0 | 65 | { |
michael@0 | 66 | NS_ASSERTION(aFrame, "Null frame?"); |
michael@0 | 67 | NS_ASSERTION(aProperty, "Null property?"); |
michael@0 | 68 | |
michael@0 | 69 | if (aFoundResult) { |
michael@0 | 70 | *aFoundResult = false; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | if (mLastFrame != aFrame) { |
michael@0 | 74 | mLastFrame = const_cast<nsIFrame*>(aFrame); |
michael@0 | 75 | mLastEntry = mEntries.GetEntry(mLastFrame); |
michael@0 | 76 | } |
michael@0 | 77 | Entry* entry = mLastEntry; |
michael@0 | 78 | if (!entry) |
michael@0 | 79 | return nullptr; |
michael@0 | 80 | |
michael@0 | 81 | if (entry->mProp.mProperty == aProperty) { |
michael@0 | 82 | if (aFoundResult) { |
michael@0 | 83 | *aFoundResult = true; |
michael@0 | 84 | } |
michael@0 | 85 | return entry->mProp.mValue; |
michael@0 | 86 | } |
michael@0 | 87 | if (!entry->mProp.IsArray()) { |
michael@0 | 88 | // There's just one property and it's not the one we want, bail |
michael@0 | 89 | return nullptr; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | nsTArray<PropertyValue>* array = entry->mProp.ToArray(); |
michael@0 | 93 | nsTArray<PropertyValue>::index_type index = |
michael@0 | 94 | array->IndexOf(aProperty, 0, PropertyComparator()); |
michael@0 | 95 | if (index == nsTArray<PropertyValue>::NoIndex) |
michael@0 | 96 | return nullptr; |
michael@0 | 97 | |
michael@0 | 98 | if (aFoundResult) { |
michael@0 | 99 | *aFoundResult = true; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | return array->ElementAt(index).mValue; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void* |
michael@0 | 106 | FramePropertyTable::Remove(nsIFrame* aFrame, const FramePropertyDescriptor* aProperty, |
michael@0 | 107 | bool* aFoundResult) |
michael@0 | 108 | { |
michael@0 | 109 | NS_ASSERTION(aFrame, "Null frame?"); |
michael@0 | 110 | NS_ASSERTION(aProperty, "Null property?"); |
michael@0 | 111 | |
michael@0 | 112 | if (aFoundResult) { |
michael@0 | 113 | *aFoundResult = false; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | if (mLastFrame != aFrame) { |
michael@0 | 117 | mLastFrame = aFrame; |
michael@0 | 118 | mLastEntry = mEntries.GetEntry(aFrame); |
michael@0 | 119 | } |
michael@0 | 120 | Entry* entry = mLastEntry; |
michael@0 | 121 | if (!entry) |
michael@0 | 122 | return nullptr; |
michael@0 | 123 | |
michael@0 | 124 | if (entry->mProp.mProperty == aProperty) { |
michael@0 | 125 | // There's only one entry and it's the one we want |
michael@0 | 126 | void* value = entry->mProp.mValue; |
michael@0 | 127 | mEntries.RawRemoveEntry(entry); |
michael@0 | 128 | mLastEntry = nullptr; |
michael@0 | 129 | if (aFoundResult) { |
michael@0 | 130 | *aFoundResult = true; |
michael@0 | 131 | } |
michael@0 | 132 | return value; |
michael@0 | 133 | } |
michael@0 | 134 | if (!entry->mProp.IsArray()) { |
michael@0 | 135 | // There's just one property and it's not the one we want, bail |
michael@0 | 136 | return nullptr; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | nsTArray<PropertyValue>* array = entry->mProp.ToArray(); |
michael@0 | 140 | nsTArray<PropertyValue>::index_type index = |
michael@0 | 141 | array->IndexOf(aProperty, 0, PropertyComparator()); |
michael@0 | 142 | if (index == nsTArray<PropertyValue>::NoIndex) { |
michael@0 | 143 | // No such property, bail |
michael@0 | 144 | return nullptr; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | if (aFoundResult) { |
michael@0 | 148 | *aFoundResult = true; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | void* result = array->ElementAt(index).mValue; |
michael@0 | 152 | |
michael@0 | 153 | uint32_t last = array->Length() - 1; |
michael@0 | 154 | array->ElementAt(index) = array->ElementAt(last); |
michael@0 | 155 | array->RemoveElementAt(last); |
michael@0 | 156 | |
michael@0 | 157 | if (last == 1) { |
michael@0 | 158 | PropertyValue pv = array->ElementAt(0); |
michael@0 | 159 | array->~nsTArray<PropertyValue>(); |
michael@0 | 160 | entry->mProp = pv; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | return result; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | void |
michael@0 | 167 | FramePropertyTable::Delete(nsIFrame* aFrame, const FramePropertyDescriptor* aProperty) |
michael@0 | 168 | { |
michael@0 | 169 | NS_ASSERTION(aFrame, "Null frame?"); |
michael@0 | 170 | NS_ASSERTION(aProperty, "Null property?"); |
michael@0 | 171 | |
michael@0 | 172 | bool found; |
michael@0 | 173 | void* v = Remove(aFrame, aProperty, &found); |
michael@0 | 174 | if (found) { |
michael@0 | 175 | PropertyValue pv(aProperty, v); |
michael@0 | 176 | pv.DestroyValueFor(aFrame); |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | /* static */ void |
michael@0 | 181 | FramePropertyTable::DeleteAllForEntry(Entry* aEntry) |
michael@0 | 182 | { |
michael@0 | 183 | if (!aEntry->mProp.IsArray()) { |
michael@0 | 184 | aEntry->mProp.DestroyValueFor(aEntry->GetKey()); |
michael@0 | 185 | return; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | nsTArray<PropertyValue>* array = aEntry->mProp.ToArray(); |
michael@0 | 189 | for (uint32_t i = 0; i < array->Length(); ++i) { |
michael@0 | 190 | array->ElementAt(i).DestroyValueFor(aEntry->GetKey()); |
michael@0 | 191 | } |
michael@0 | 192 | array->~nsTArray<PropertyValue>(); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | void |
michael@0 | 196 | FramePropertyTable::DeleteAllFor(nsIFrame* aFrame) |
michael@0 | 197 | { |
michael@0 | 198 | NS_ASSERTION(aFrame, "Null frame?"); |
michael@0 | 199 | |
michael@0 | 200 | Entry* entry = mEntries.GetEntry(aFrame); |
michael@0 | 201 | if (!entry) |
michael@0 | 202 | return; |
michael@0 | 203 | |
michael@0 | 204 | if (mLastFrame == aFrame) { |
michael@0 | 205 | // Flush cache. We assume DeleteAllForEntry will be called before |
michael@0 | 206 | // a frame is destroyed. |
michael@0 | 207 | mLastFrame = nullptr; |
michael@0 | 208 | mLastEntry = nullptr; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | DeleteAllForEntry(entry); |
michael@0 | 212 | mEntries.RawRemoveEntry(entry); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | /* static */ PLDHashOperator |
michael@0 | 216 | FramePropertyTable::DeleteEnumerator(Entry* aEntry, void* aArg) |
michael@0 | 217 | { |
michael@0 | 218 | DeleteAllForEntry(aEntry); |
michael@0 | 219 | return PL_DHASH_REMOVE; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | void |
michael@0 | 223 | FramePropertyTable::DeleteAll() |
michael@0 | 224 | { |
michael@0 | 225 | mLastFrame = nullptr; |
michael@0 | 226 | mLastEntry = nullptr; |
michael@0 | 227 | |
michael@0 | 228 | mEntries.EnumerateEntries(DeleteEnumerator, nullptr); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | size_t |
michael@0 | 232 | FramePropertyTable::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
michael@0 | 233 | { |
michael@0 | 234 | return mEntries.SizeOfExcludingThis(SizeOfPropertyTableEntryExcludingThis, |
michael@0 | 235 | aMallocSizeOf); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | /* static */ size_t |
michael@0 | 239 | FramePropertyTable::SizeOfPropertyTableEntryExcludingThis(Entry* aEntry, |
michael@0 | 240 | mozilla::MallocSizeOf aMallocSizeOf, void *) |
michael@0 | 241 | { |
michael@0 | 242 | return aEntry->mProp.SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | } |