xpcom/glue/nsArrayEnumerator.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "mozilla/Attributes.h"
michael@0 7
michael@0 8 #include "nsArrayEnumerator.h"
michael@0 9
michael@0 10 #include "nsIArray.h"
michael@0 11 #include "nsISimpleEnumerator.h"
michael@0 12
michael@0 13 #include "nsCOMArray.h"
michael@0 14 #include "nsCOMPtr.h"
michael@0 15
michael@0 16 class nsSimpleArrayEnumerator MOZ_FINAL : public nsISimpleEnumerator
michael@0 17 {
michael@0 18 public:
michael@0 19 // nsISupports interface
michael@0 20 NS_DECL_ISUPPORTS
michael@0 21
michael@0 22 // nsISimpleEnumerator interface
michael@0 23 NS_DECL_NSISIMPLEENUMERATOR
michael@0 24
michael@0 25 // nsSimpleArrayEnumerator methods
michael@0 26 nsSimpleArrayEnumerator(nsIArray* aValueArray) :
michael@0 27 mValueArray(aValueArray), mIndex(0) {
michael@0 28 }
michael@0 29
michael@0 30 private:
michael@0 31 ~nsSimpleArrayEnumerator() {}
michael@0 32
michael@0 33 protected:
michael@0 34 nsCOMPtr<nsIArray> mValueArray;
michael@0 35 uint32_t mIndex;
michael@0 36 };
michael@0 37
michael@0 38 NS_IMPL_ISUPPORTS(nsSimpleArrayEnumerator, nsISimpleEnumerator)
michael@0 39
michael@0 40 NS_IMETHODIMP
michael@0 41 nsSimpleArrayEnumerator::HasMoreElements(bool* aResult)
michael@0 42 {
michael@0 43 NS_PRECONDITION(aResult != 0, "null ptr");
michael@0 44 if (! aResult)
michael@0 45 return NS_ERROR_NULL_POINTER;
michael@0 46
michael@0 47 if (!mValueArray) {
michael@0 48 *aResult = false;
michael@0 49 return NS_OK;
michael@0 50 }
michael@0 51
michael@0 52 uint32_t cnt;
michael@0 53 nsresult rv = mValueArray->GetLength(&cnt);
michael@0 54 if (NS_FAILED(rv)) return rv;
michael@0 55 *aResult = (mIndex < cnt);
michael@0 56 return NS_OK;
michael@0 57 }
michael@0 58
michael@0 59 NS_IMETHODIMP
michael@0 60 nsSimpleArrayEnumerator::GetNext(nsISupports** aResult)
michael@0 61 {
michael@0 62 NS_PRECONDITION(aResult != 0, "null ptr");
michael@0 63 if (! aResult)
michael@0 64 return NS_ERROR_NULL_POINTER;
michael@0 65
michael@0 66 if (!mValueArray) {
michael@0 67 *aResult = nullptr;
michael@0 68 return NS_OK;
michael@0 69 }
michael@0 70
michael@0 71 uint32_t cnt;
michael@0 72 nsresult rv = mValueArray->GetLength(&cnt);
michael@0 73 if (NS_FAILED(rv)) return rv;
michael@0 74 if (mIndex >= cnt)
michael@0 75 return NS_ERROR_UNEXPECTED;
michael@0 76
michael@0 77 return mValueArray->QueryElementAt(mIndex++, NS_GET_IID(nsISupports), (void**)aResult);
michael@0 78 }
michael@0 79
michael@0 80 nsresult
michael@0 81 NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
michael@0 82 nsIArray* array)
michael@0 83 {
michael@0 84 nsSimpleArrayEnumerator* enumer = new nsSimpleArrayEnumerator(array);
michael@0 85 if (enumer == nullptr)
michael@0 86 return NS_ERROR_OUT_OF_MEMORY;
michael@0 87
michael@0 88 NS_ADDREF(*result = enumer);
michael@0 89 return NS_OK;
michael@0 90 }
michael@0 91
michael@0 92 ////////////////////////////////////////////////////////////////////////////////
michael@0 93
michael@0 94 // enumerator implementation for nsCOMArray
michael@0 95 // creates a snapshot of the array in question
michael@0 96 // you MUST use NS_NewArrayEnumerator to create this, so that
michael@0 97 // allocation is done correctly
michael@0 98 class nsCOMArrayEnumerator MOZ_FINAL : public nsISimpleEnumerator
michael@0 99 {
michael@0 100 public:
michael@0 101 // nsISupports interface
michael@0 102 NS_DECL_ISUPPORTS
michael@0 103
michael@0 104 // nsISimpleEnumerator interface
michael@0 105 NS_DECL_NSISIMPLEENUMERATOR
michael@0 106
michael@0 107 // nsSimpleArrayEnumerator methods
michael@0 108 nsCOMArrayEnumerator() : mIndex(0) {
michael@0 109 }
michael@0 110
michael@0 111 // specialized operator to make sure we make room for mValues
michael@0 112 void* operator new (size_t size, const nsCOMArray_base& aArray) CPP_THROW_NEW;
michael@0 113 void operator delete(void* ptr) {
michael@0 114 ::operator delete(ptr);
michael@0 115 }
michael@0 116
michael@0 117 private:
michael@0 118 ~nsCOMArrayEnumerator(void);
michael@0 119
michael@0 120 protected:
michael@0 121 uint32_t mIndex; // current position
michael@0 122 uint32_t mArraySize; // size of the array
michael@0 123
michael@0 124 // this is actually bigger
michael@0 125 nsISupports* mValueArray[1];
michael@0 126 };
michael@0 127
michael@0 128 NS_IMPL_ISUPPORTS(nsCOMArrayEnumerator, nsISimpleEnumerator)
michael@0 129
michael@0 130 nsCOMArrayEnumerator::~nsCOMArrayEnumerator()
michael@0 131 {
michael@0 132 // only release the entries that we haven't visited yet
michael@0 133 for (; mIndex < mArraySize; ++mIndex) {
michael@0 134 NS_IF_RELEASE(mValueArray[mIndex]);
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138 NS_IMETHODIMP
michael@0 139 nsCOMArrayEnumerator::HasMoreElements(bool* aResult)
michael@0 140 {
michael@0 141 NS_PRECONDITION(aResult != 0, "null ptr");
michael@0 142 if (! aResult)
michael@0 143 return NS_ERROR_NULL_POINTER;
michael@0 144
michael@0 145 *aResult = (mIndex < mArraySize);
michael@0 146 return NS_OK;
michael@0 147 }
michael@0 148
michael@0 149 NS_IMETHODIMP
michael@0 150 nsCOMArrayEnumerator::GetNext(nsISupports** aResult)
michael@0 151 {
michael@0 152 NS_PRECONDITION(aResult != 0, "null ptr");
michael@0 153 if (! aResult)
michael@0 154 return NS_ERROR_NULL_POINTER;
michael@0 155
michael@0 156 if (mIndex >= mArraySize)
michael@0 157 return NS_ERROR_UNEXPECTED;
michael@0 158
michael@0 159 // pass the ownership of the reference to the caller. Since
michael@0 160 // we AddRef'ed during creation of |this|, there is no need
michael@0 161 // to AddRef here
michael@0 162 *aResult = mValueArray[mIndex++];
michael@0 163
michael@0 164 // this really isn't necessary. just pretend this happens, since
michael@0 165 // we'll never visit this value again!
michael@0 166 // mValueArray[(mIndex-1)] = nullptr;
michael@0 167
michael@0 168 return NS_OK;
michael@0 169 }
michael@0 170
michael@0 171 void*
michael@0 172 nsCOMArrayEnumerator::operator new (size_t size, const nsCOMArray_base& aArray)
michael@0 173 CPP_THROW_NEW
michael@0 174 {
michael@0 175 // create enough space such that mValueArray points to a large
michael@0 176 // enough value. Note that the initial value of size gives us
michael@0 177 // space for mValueArray[0], so we must subtract
michael@0 178 size += (aArray.Count() - 1) * sizeof(aArray[0]);
michael@0 179
michael@0 180 // do the actual allocation
michael@0 181 nsCOMArrayEnumerator * result =
michael@0 182 static_cast<nsCOMArrayEnumerator*>(::operator new(size));
michael@0 183
michael@0 184 // now need to copy over the values, and addref each one
michael@0 185 // now this might seem like a lot of work, but we're actually just
michael@0 186 // doing all our AddRef's ahead of time since GetNext() doesn't
michael@0 187 // need to AddRef() on the way out
michael@0 188 uint32_t i;
michael@0 189 uint32_t max = result->mArraySize = aArray.Count();
michael@0 190 for (i = 0; i<max; i++) {
michael@0 191 result->mValueArray[i] = aArray[i];
michael@0 192 NS_IF_ADDREF(result->mValueArray[i]);
michael@0 193 }
michael@0 194
michael@0 195 return result;
michael@0 196 }
michael@0 197
michael@0 198 nsresult
michael@0 199 NS_NewArrayEnumerator(nsISimpleEnumerator* *aResult,
michael@0 200 const nsCOMArray_base& aArray)
michael@0 201 {
michael@0 202 nsCOMArrayEnumerator *enumerator = new (aArray) nsCOMArrayEnumerator();
michael@0 203 if (!enumerator) return NS_ERROR_OUT_OF_MEMORY;
michael@0 204
michael@0 205 NS_ADDREF(*aResult = enumerator);
michael@0 206 return NS_OK;
michael@0 207 }

mercurial