Tue, 06 Jan 2015 21:39:09 +0100
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 "nsEnumeratorUtils.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "nsISimpleEnumerator.h" |
michael@0 | 11 | #include "nsIStringEnumerator.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsCOMPtr.h" |
michael@0 | 14 | |
michael@0 | 15 | class EmptyEnumeratorImpl : public nsISimpleEnumerator, |
michael@0 | 16 | public nsIUTF8StringEnumerator, |
michael@0 | 17 | public nsIStringEnumerator |
michael@0 | 18 | { |
michael@0 | 19 | public: |
michael@0 | 20 | EmptyEnumeratorImpl() {} |
michael@0 | 21 | // nsISupports interface |
michael@0 | 22 | NS_DECL_ISUPPORTS_INHERITED // not really inherited, but no mRefCnt |
michael@0 | 23 | |
michael@0 | 24 | // nsISimpleEnumerator |
michael@0 | 25 | NS_DECL_NSISIMPLEENUMERATOR |
michael@0 | 26 | NS_DECL_NSIUTF8STRINGENUMERATOR |
michael@0 | 27 | // can't use NS_DECL_NSISTRINGENUMERATOR because they share the |
michael@0 | 28 | // HasMore() signature |
michael@0 | 29 | NS_IMETHOD GetNext(nsAString& aResult); |
michael@0 | 30 | |
michael@0 | 31 | static EmptyEnumeratorImpl* GetInstance() { |
michael@0 | 32 | static const EmptyEnumeratorImpl kInstance; |
michael@0 | 33 | return const_cast<EmptyEnumeratorImpl*>(&kInstance); |
michael@0 | 34 | } |
michael@0 | 35 | }; |
michael@0 | 36 | |
michael@0 | 37 | // nsISupports interface |
michael@0 | 38 | NS_IMETHODIMP_(MozExternalRefCountType) EmptyEnumeratorImpl::AddRef(void) |
michael@0 | 39 | { |
michael@0 | 40 | return 2; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | NS_IMETHODIMP_(MozExternalRefCountType) EmptyEnumeratorImpl::Release(void) |
michael@0 | 44 | { |
michael@0 | 45 | return 1; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | NS_IMPL_QUERY_INTERFACE(EmptyEnumeratorImpl, nsISimpleEnumerator, |
michael@0 | 49 | nsIUTF8StringEnumerator, nsIStringEnumerator) |
michael@0 | 50 | |
michael@0 | 51 | // nsISimpleEnumerator interface |
michael@0 | 52 | NS_IMETHODIMP EmptyEnumeratorImpl::HasMoreElements(bool* aResult) |
michael@0 | 53 | { |
michael@0 | 54 | *aResult = false; |
michael@0 | 55 | return NS_OK; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | NS_IMETHODIMP EmptyEnumeratorImpl::HasMore(bool* aResult) |
michael@0 | 59 | { |
michael@0 | 60 | *aResult = false; |
michael@0 | 61 | return NS_OK; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsISupports** aResult) |
michael@0 | 65 | { |
michael@0 | 66 | return NS_ERROR_UNEXPECTED; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsACString& aResult) |
michael@0 | 70 | { |
michael@0 | 71 | return NS_ERROR_UNEXPECTED; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsAString& aResult) |
michael@0 | 75 | { |
michael@0 | 76 | return NS_ERROR_UNEXPECTED; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | nsresult |
michael@0 | 80 | NS_NewEmptyEnumerator(nsISimpleEnumerator** aResult) |
michael@0 | 81 | { |
michael@0 | 82 | *aResult = EmptyEnumeratorImpl::GetInstance(); |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 87 | |
michael@0 | 88 | class nsSingletonEnumerator MOZ_FINAL : public nsISimpleEnumerator |
michael@0 | 89 | { |
michael@0 | 90 | public: |
michael@0 | 91 | NS_DECL_ISUPPORTS |
michael@0 | 92 | |
michael@0 | 93 | // nsISimpleEnumerator methods |
michael@0 | 94 | NS_IMETHOD HasMoreElements(bool* aResult); |
michael@0 | 95 | NS_IMETHOD GetNext(nsISupports** aResult); |
michael@0 | 96 | |
michael@0 | 97 | nsSingletonEnumerator(nsISupports* aValue); |
michael@0 | 98 | |
michael@0 | 99 | private: |
michael@0 | 100 | ~nsSingletonEnumerator(); |
michael@0 | 101 | |
michael@0 | 102 | protected: |
michael@0 | 103 | nsISupports* mValue; |
michael@0 | 104 | bool mConsumed; |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue) |
michael@0 | 108 | : mValue(aValue) |
michael@0 | 109 | { |
michael@0 | 110 | NS_IF_ADDREF(mValue); |
michael@0 | 111 | mConsumed = (mValue ? false : true); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | nsSingletonEnumerator::~nsSingletonEnumerator() |
michael@0 | 115 | { |
michael@0 | 116 | NS_IF_RELEASE(mValue); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | NS_IMPL_ISUPPORTS(nsSingletonEnumerator, nsISimpleEnumerator) |
michael@0 | 120 | |
michael@0 | 121 | NS_IMETHODIMP |
michael@0 | 122 | nsSingletonEnumerator::HasMoreElements(bool* aResult) |
michael@0 | 123 | { |
michael@0 | 124 | NS_PRECONDITION(aResult != 0, "null ptr"); |
michael@0 | 125 | if (! aResult) |
michael@0 | 126 | return NS_ERROR_NULL_POINTER; |
michael@0 | 127 | |
michael@0 | 128 | *aResult = !mConsumed; |
michael@0 | 129 | return NS_OK; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | |
michael@0 | 133 | NS_IMETHODIMP |
michael@0 | 134 | nsSingletonEnumerator::GetNext(nsISupports** aResult) |
michael@0 | 135 | { |
michael@0 | 136 | NS_PRECONDITION(aResult != 0, "null ptr"); |
michael@0 | 137 | if (! aResult) |
michael@0 | 138 | return NS_ERROR_NULL_POINTER; |
michael@0 | 139 | |
michael@0 | 140 | if (mConsumed) |
michael@0 | 141 | return NS_ERROR_UNEXPECTED; |
michael@0 | 142 | |
michael@0 | 143 | mConsumed = true; |
michael@0 | 144 | |
michael@0 | 145 | *aResult = mValue; |
michael@0 | 146 | NS_ADDREF(*aResult); |
michael@0 | 147 | return NS_OK; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | nsresult |
michael@0 | 151 | NS_NewSingletonEnumerator(nsISimpleEnumerator* *result, |
michael@0 | 152 | nsISupports* singleton) |
michael@0 | 153 | { |
michael@0 | 154 | nsSingletonEnumerator* enumer = new nsSingletonEnumerator(singleton); |
michael@0 | 155 | if (enumer == nullptr) |
michael@0 | 156 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 157 | *result = enumer; |
michael@0 | 158 | NS_ADDREF(*result); |
michael@0 | 159 | return NS_OK; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 163 | |
michael@0 | 164 | class nsUnionEnumerator MOZ_FINAL : public nsISimpleEnumerator |
michael@0 | 165 | { |
michael@0 | 166 | public: |
michael@0 | 167 | NS_DECL_ISUPPORTS |
michael@0 | 168 | |
michael@0 | 169 | // nsISimpleEnumerator methods |
michael@0 | 170 | NS_IMETHOD HasMoreElements(bool* aResult); |
michael@0 | 171 | NS_IMETHOD GetNext(nsISupports** aResult); |
michael@0 | 172 | |
michael@0 | 173 | nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator, |
michael@0 | 174 | nsISimpleEnumerator* secondEnumerator); |
michael@0 | 175 | |
michael@0 | 176 | private: |
michael@0 | 177 | ~nsUnionEnumerator(); |
michael@0 | 178 | |
michael@0 | 179 | protected: |
michael@0 | 180 | nsCOMPtr<nsISimpleEnumerator> mFirstEnumerator, mSecondEnumerator; |
michael@0 | 181 | bool mConsumed; |
michael@0 | 182 | bool mAtSecond; |
michael@0 | 183 | }; |
michael@0 | 184 | |
michael@0 | 185 | nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator, |
michael@0 | 186 | nsISimpleEnumerator* secondEnumerator) |
michael@0 | 187 | : mFirstEnumerator(firstEnumerator), |
michael@0 | 188 | mSecondEnumerator(secondEnumerator), |
michael@0 | 189 | mConsumed(false), mAtSecond(false) |
michael@0 | 190 | { |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | nsUnionEnumerator::~nsUnionEnumerator() |
michael@0 | 194 | { |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | NS_IMPL_ISUPPORTS(nsUnionEnumerator, nsISimpleEnumerator) |
michael@0 | 198 | |
michael@0 | 199 | NS_IMETHODIMP |
michael@0 | 200 | nsUnionEnumerator::HasMoreElements(bool* aResult) |
michael@0 | 201 | { |
michael@0 | 202 | NS_PRECONDITION(aResult != 0, "null ptr"); |
michael@0 | 203 | if (! aResult) |
michael@0 | 204 | return NS_ERROR_NULL_POINTER; |
michael@0 | 205 | |
michael@0 | 206 | nsresult rv; |
michael@0 | 207 | |
michael@0 | 208 | if (mConsumed) { |
michael@0 | 209 | *aResult = false; |
michael@0 | 210 | return NS_OK; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | if (! mAtSecond) { |
michael@0 | 214 | rv = mFirstEnumerator->HasMoreElements(aResult); |
michael@0 | 215 | if (NS_FAILED(rv)) return rv; |
michael@0 | 216 | |
michael@0 | 217 | if (*aResult) |
michael@0 | 218 | return NS_OK; |
michael@0 | 219 | |
michael@0 | 220 | mAtSecond = true; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | rv = mSecondEnumerator->HasMoreElements(aResult); |
michael@0 | 224 | if (NS_FAILED(rv)) return rv; |
michael@0 | 225 | |
michael@0 | 226 | if (*aResult) |
michael@0 | 227 | return NS_OK; |
michael@0 | 228 | |
michael@0 | 229 | *aResult = false; |
michael@0 | 230 | mConsumed = true; |
michael@0 | 231 | return NS_OK; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | NS_IMETHODIMP |
michael@0 | 235 | nsUnionEnumerator::GetNext(nsISupports** aResult) |
michael@0 | 236 | { |
michael@0 | 237 | NS_PRECONDITION(aResult != 0, "null ptr"); |
michael@0 | 238 | if (! aResult) |
michael@0 | 239 | return NS_ERROR_NULL_POINTER; |
michael@0 | 240 | |
michael@0 | 241 | if (mConsumed) |
michael@0 | 242 | return NS_ERROR_UNEXPECTED; |
michael@0 | 243 | |
michael@0 | 244 | if (! mAtSecond) |
michael@0 | 245 | return mFirstEnumerator->GetNext(aResult); |
michael@0 | 246 | |
michael@0 | 247 | return mSecondEnumerator->GetNext(aResult); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | nsresult |
michael@0 | 251 | NS_NewUnionEnumerator(nsISimpleEnumerator* *result, |
michael@0 | 252 | nsISimpleEnumerator* firstEnumerator, |
michael@0 | 253 | nsISimpleEnumerator* secondEnumerator) |
michael@0 | 254 | { |
michael@0 | 255 | *result = nullptr; |
michael@0 | 256 | if (! firstEnumerator) { |
michael@0 | 257 | *result = secondEnumerator; |
michael@0 | 258 | } else if (! secondEnumerator) { |
michael@0 | 259 | *result = firstEnumerator; |
michael@0 | 260 | } else { |
michael@0 | 261 | nsUnionEnumerator* enumer = new nsUnionEnumerator(firstEnumerator, secondEnumerator); |
michael@0 | 262 | if (enumer == nullptr) |
michael@0 | 263 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 264 | *result = enumer; |
michael@0 | 265 | } |
michael@0 | 266 | NS_ADDREF(*result); |
michael@0 | 267 | return NS_OK; |
michael@0 | 268 | } |