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: 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 | |
michael@0 | 7 | #include "nsStringEnumerator.h" |
michael@0 | 8 | #include "nsISimpleEnumerator.h" |
michael@0 | 9 | #include "nsSupportsPrimitives.h" |
michael@0 | 10 | #include "mozilla/Attributes.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | |
michael@0 | 13 | // |
michael@0 | 14 | // nsStringEnumerator |
michael@0 | 15 | // |
michael@0 | 16 | |
michael@0 | 17 | class nsStringEnumerator MOZ_FINAL : public nsIStringEnumerator, |
michael@0 | 18 | public nsIUTF8StringEnumerator, |
michael@0 | 19 | public nsISimpleEnumerator |
michael@0 | 20 | { |
michael@0 | 21 | public: |
michael@0 | 22 | nsStringEnumerator(const nsTArray<nsString>* aArray, bool aOwnsArray) : |
michael@0 | 23 | mArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(true) |
michael@0 | 24 | {} |
michael@0 | 25 | |
michael@0 | 26 | nsStringEnumerator(const nsTArray<nsCString>* aArray, bool aOwnsArray) : |
michael@0 | 27 | mCArray(aArray), mIndex(0), mOwnsArray(aOwnsArray), mIsUnicode(false) |
michael@0 | 28 | {} |
michael@0 | 29 | |
michael@0 | 30 | nsStringEnumerator(const nsTArray<nsString>* aArray, nsISupports* aOwner) : |
michael@0 | 31 | mArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(false), mIsUnicode(true) |
michael@0 | 32 | {} |
michael@0 | 33 | |
michael@0 | 34 | nsStringEnumerator(const nsTArray<nsCString>* aArray, nsISupports* aOwner) : |
michael@0 | 35 | mCArray(aArray), mIndex(0), mOwner(aOwner), mOwnsArray(false), mIsUnicode(false) |
michael@0 | 36 | {} |
michael@0 | 37 | |
michael@0 | 38 | NS_DECL_ISUPPORTS |
michael@0 | 39 | NS_DECL_NSIUTF8STRINGENUMERATOR |
michael@0 | 40 | |
michael@0 | 41 | // have to declare nsIStringEnumerator manually, because of |
michael@0 | 42 | // overlapping method names |
michael@0 | 43 | NS_IMETHOD GetNext(nsAString& aResult); |
michael@0 | 44 | NS_DECL_NSISIMPLEENUMERATOR |
michael@0 | 45 | |
michael@0 | 46 | private: |
michael@0 | 47 | ~nsStringEnumerator() { |
michael@0 | 48 | if (mOwnsArray) { |
michael@0 | 49 | // const-casting is safe here, because the NS_New* |
michael@0 | 50 | // constructors make sure mOwnsArray is consistent with |
michael@0 | 51 | // the constness of the objects |
michael@0 | 52 | if (mIsUnicode) |
michael@0 | 53 | delete const_cast<nsTArray<nsString>*>(mArray); |
michael@0 | 54 | else |
michael@0 | 55 | delete const_cast<nsTArray<nsCString>*>(mCArray); |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | union { |
michael@0 | 60 | const nsTArray<nsString>* mArray; |
michael@0 | 61 | const nsTArray<nsCString>* mCArray; |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | inline uint32_t Count() { |
michael@0 | 65 | return mIsUnicode ? mArray->Length() : mCArray->Length(); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | uint32_t mIndex; |
michael@0 | 69 | |
michael@0 | 70 | // the owner allows us to hold a strong reference to the object |
michael@0 | 71 | // that owns the array. Having a non-null value in mOwner implies |
michael@0 | 72 | // that mOwnsArray is false, because we rely on the real owner |
michael@0 | 73 | // to release the array |
michael@0 | 74 | nsCOMPtr<nsISupports> mOwner; |
michael@0 | 75 | bool mOwnsArray; |
michael@0 | 76 | bool mIsUnicode; |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | NS_IMPL_ISUPPORTS(nsStringEnumerator, |
michael@0 | 80 | nsIStringEnumerator, |
michael@0 | 81 | nsIUTF8StringEnumerator, |
michael@0 | 82 | nsISimpleEnumerator) |
michael@0 | 83 | |
michael@0 | 84 | NS_IMETHODIMP |
michael@0 | 85 | nsStringEnumerator::HasMore(bool* aResult) |
michael@0 | 86 | { |
michael@0 | 87 | *aResult = mIndex < Count(); |
michael@0 | 88 | return NS_OK; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | NS_IMETHODIMP |
michael@0 | 92 | nsStringEnumerator::HasMoreElements(bool* aResult) |
michael@0 | 93 | { |
michael@0 | 94 | return HasMore(aResult); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | NS_IMETHODIMP |
michael@0 | 98 | nsStringEnumerator::GetNext(nsISupports** aResult) |
michael@0 | 99 | { |
michael@0 | 100 | if (mIsUnicode) { |
michael@0 | 101 | nsSupportsStringImpl* stringImpl = new nsSupportsStringImpl(); |
michael@0 | 102 | if (!stringImpl) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 103 | |
michael@0 | 104 | stringImpl->SetData(mArray->ElementAt(mIndex++)); |
michael@0 | 105 | *aResult = stringImpl; |
michael@0 | 106 | } |
michael@0 | 107 | else { |
michael@0 | 108 | nsSupportsCStringImpl* cstringImpl = new nsSupportsCStringImpl(); |
michael@0 | 109 | if (!cstringImpl) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 110 | |
michael@0 | 111 | cstringImpl->SetData(mCArray->ElementAt(mIndex++)); |
michael@0 | 112 | *aResult = cstringImpl; |
michael@0 | 113 | } |
michael@0 | 114 | NS_ADDREF(*aResult); |
michael@0 | 115 | return NS_OK; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | NS_IMETHODIMP |
michael@0 | 119 | nsStringEnumerator::GetNext(nsAString& aResult) |
michael@0 | 120 | { |
michael@0 | 121 | if (NS_WARN_IF(mIndex >= Count())) |
michael@0 | 122 | return NS_ERROR_UNEXPECTED; |
michael@0 | 123 | |
michael@0 | 124 | if (mIsUnicode) |
michael@0 | 125 | aResult = mArray->ElementAt(mIndex++); |
michael@0 | 126 | else |
michael@0 | 127 | CopyUTF8toUTF16(mCArray->ElementAt(mIndex++), aResult); |
michael@0 | 128 | |
michael@0 | 129 | return NS_OK; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | NS_IMETHODIMP |
michael@0 | 133 | nsStringEnumerator::GetNext(nsACString& aResult) |
michael@0 | 134 | { |
michael@0 | 135 | if (NS_WARN_IF(mIndex >= Count())) |
michael@0 | 136 | return NS_ERROR_UNEXPECTED; |
michael@0 | 137 | |
michael@0 | 138 | if (mIsUnicode) |
michael@0 | 139 | CopyUTF16toUTF8(mArray->ElementAt(mIndex++), aResult); |
michael@0 | 140 | else |
michael@0 | 141 | aResult = mCArray->ElementAt(mIndex++); |
michael@0 | 142 | |
michael@0 | 143 | return NS_OK; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | template<class T> |
michael@0 | 147 | static inline nsresult |
michael@0 | 148 | StringEnumeratorTail(T** aResult) |
michael@0 | 149 | { |
michael@0 | 150 | if (!*aResult) |
michael@0 | 151 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 152 | NS_ADDREF(*aResult); |
michael@0 | 153 | return NS_OK; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | // |
michael@0 | 157 | // constructors |
michael@0 | 158 | // |
michael@0 | 159 | |
michael@0 | 160 | nsresult |
michael@0 | 161 | NS_NewStringEnumerator(nsIStringEnumerator** aResult, |
michael@0 | 162 | const nsTArray<nsString>* aArray, nsISupports* aOwner) |
michael@0 | 163 | { |
michael@0 | 164 | if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) |
michael@0 | 165 | return NS_ERROR_INVALID_ARG; |
michael@0 | 166 | |
michael@0 | 167 | *aResult = new nsStringEnumerator(aArray, aOwner); |
michael@0 | 168 | return StringEnumeratorTail(aResult); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | |
michael@0 | 172 | nsresult |
michael@0 | 173 | NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, |
michael@0 | 174 | const nsTArray<nsCString>* aArray, nsISupports* aOwner) |
michael@0 | 175 | { |
michael@0 | 176 | if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) |
michael@0 | 177 | return NS_ERROR_INVALID_ARG; |
michael@0 | 178 | |
michael@0 | 179 | *aResult = new nsStringEnumerator(aArray, aOwner); |
michael@0 | 180 | return StringEnumeratorTail(aResult); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | nsresult |
michael@0 | 184 | NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult, |
michael@0 | 185 | nsTArray<nsString>* aArray) |
michael@0 | 186 | { |
michael@0 | 187 | if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) |
michael@0 | 188 | return NS_ERROR_INVALID_ARG; |
michael@0 | 189 | |
michael@0 | 190 | *aResult = new nsStringEnumerator(aArray, true); |
michael@0 | 191 | return StringEnumeratorTail(aResult); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | nsresult |
michael@0 | 195 | NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, |
michael@0 | 196 | nsTArray<nsCString>* aArray) |
michael@0 | 197 | { |
michael@0 | 198 | if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) |
michael@0 | 199 | return NS_ERROR_INVALID_ARG; |
michael@0 | 200 | |
michael@0 | 201 | *aResult = new nsStringEnumerator(aArray, true); |
michael@0 | 202 | return StringEnumeratorTail(aResult); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | // const ones internally just forward to the non-const equivalents |
michael@0 | 206 | nsresult |
michael@0 | 207 | NS_NewStringEnumerator(nsIStringEnumerator** aResult, |
michael@0 | 208 | const nsTArray<nsString>* aArray) |
michael@0 | 209 | { |
michael@0 | 210 | if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) |
michael@0 | 211 | return NS_ERROR_INVALID_ARG; |
michael@0 | 212 | |
michael@0 | 213 | *aResult = new nsStringEnumerator(aArray, false); |
michael@0 | 214 | return StringEnumeratorTail(aResult); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | nsresult |
michael@0 | 218 | NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, |
michael@0 | 219 | const nsTArray<nsCString>* aArray) |
michael@0 | 220 | { |
michael@0 | 221 | if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) |
michael@0 | 222 | return NS_ERROR_INVALID_ARG; |
michael@0 | 223 | |
michael@0 | 224 | *aResult = new nsStringEnumerator(aArray, false); |
michael@0 | 225 | return StringEnumeratorTail(aResult); |
michael@0 | 226 | } |
michael@0 | 227 |