2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-03-11 00:09:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsStringEnumerator.h"
|
2018-08-19 00:22:47 +03:00
|
|
|
#include "nsSimpleEnumerator.h"
|
2003-03-11 00:09:31 +03:00
|
|
|
#include "nsSupportsPrimitives.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2018-08-25 02:21:33 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
|
|
|
#include "mozilla/dom/IteratorResultBinding.h"
|
|
|
|
#include "mozilla/dom/RootedDictionary.h"
|
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2013-09-19 23:31:21 +04:00
|
|
|
#include "nsTArray.h"
|
2003-03-11 00:09:31 +03:00
|
|
|
|
2018-08-25 02:21:33 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class JSStringEnumerator final : public nsIJSEnumerator
|
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIJSENUMERATOR
|
|
|
|
|
|
|
|
explicit JSStringEnumerator(nsIStringEnumerator* aEnumerator)
|
|
|
|
: mEnumerator(do_QueryInterface(aEnumerator))
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~JSStringEnumerator() = default;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringEnumerator> mEnumerator;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
JSStringEnumerator::Iterator(nsIJSEnumerator** aResult)
|
|
|
|
{
|
|
|
|
RefPtr<JSStringEnumerator> result(this);
|
|
|
|
result.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
JSStringEnumerator::Next(JSContext* aCx, JS::MutableHandleValue aResult)
|
|
|
|
{
|
|
|
|
RootedDictionary<IteratorResult> result(aCx);
|
|
|
|
|
|
|
|
nsAutoString elem;
|
|
|
|
if (NS_FAILED(mEnumerator->GetNext(elem))) {
|
|
|
|
result.mDone = true;
|
|
|
|
} else {
|
|
|
|
result.mDone = false;
|
|
|
|
|
|
|
|
if (!ToJSValue(aCx, elem,
|
|
|
|
JS::MutableHandleValue::fromMarkedLocation(&result.mValue))) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ToJSValue(aCx, result, aResult)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(JSStringEnumerator, nsIJSEnumerator)
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsStringEnumeratorBase
|
|
|
|
//
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStringEnumeratorBase::GetNext(nsAString& aResult)
|
|
|
|
{
|
|
|
|
nsAutoCString str;
|
|
|
|
MOZ_TRY(GetNext(str));
|
|
|
|
|
|
|
|
CopyUTF8toUTF16(str, aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumeratorBase::StringIterator(nsIJSEnumerator** aRetVal)
|
|
|
|
{
|
|
|
|
auto result = MakeRefPtr<JSStringEnumerator>(this);
|
|
|
|
result.forget(aRetVal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-11 00:09:31 +03:00
|
|
|
//
|
|
|
|
// nsStringEnumerator
|
|
|
|
//
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsStringEnumerator final
|
2018-08-19 00:22:47 +03:00
|
|
|
: public nsSimpleEnumerator
|
|
|
|
, public nsIStringEnumerator
|
2014-07-09 19:15:21 +04:00
|
|
|
, public nsIUTF8StringEnumerator
|
2008-02-21 23:39:20 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-07-09 19:15:21 +04:00
|
|
|
nsStringEnumerator(const nsTArray<nsString>* aArray, bool aOwnsArray)
|
|
|
|
: mArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwnsArray(aOwnsArray)
|
|
|
|
, mIsUnicode(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsStringEnumerator(const nsTArray<nsCString>* aArray, bool aOwnsArray)
|
|
|
|
: mCArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwnsArray(aOwnsArray)
|
|
|
|
, mIsUnicode(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsStringEnumerator(const nsTArray<nsString>* aArray, nsISupports* aOwner)
|
|
|
|
: mArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwner(aOwner)
|
|
|
|
, mOwnsArray(false)
|
|
|
|
, mIsUnicode(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsStringEnumerator(const nsTArray<nsCString>* aArray, nsISupports* aOwner)
|
|
|
|
: mCArray(aArray)
|
|
|
|
, mIndex(0)
|
|
|
|
, mOwner(aOwner)
|
|
|
|
, mOwnsArray(false)
|
|
|
|
, mIsUnicode(false)
|
|
|
|
{}
|
|
|
|
|
2018-08-19 00:22:47 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_DECL_NSIUTF8STRINGENUMERATOR
|
2018-08-25 02:21:33 +03:00
|
|
|
NS_DECL_NSISTRINGENUMERATORBASE
|
2014-07-09 19:15:21 +04:00
|
|
|
|
|
|
|
// have to declare nsIStringEnumerator manually, because of
|
|
|
|
// overlapping method names
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD GetNext(nsAString& aResult) override;
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsID& DefaultInterface() override
|
|
|
|
{
|
|
|
|
if (mIsUnicode) {
|
|
|
|
return NS_GET_IID(nsISupportsString);
|
|
|
|
}
|
|
|
|
return NS_GET_IID(nsISupportsCString);
|
|
|
|
}
|
|
|
|
|
2008-02-21 23:39:20 +03:00
|
|
|
private:
|
2014-07-09 19:15:21 +04:00
|
|
|
~nsStringEnumerator()
|
|
|
|
{
|
|
|
|
if (mOwnsArray) {
|
|
|
|
// const-casting is safe here, because the NS_New*
|
|
|
|
// constructors make sure mOwnsArray is consistent with
|
|
|
|
// the constness of the objects
|
|
|
|
if (mIsUnicode) {
|
|
|
|
delete const_cast<nsTArray<nsString>*>(mArray);
|
|
|
|
} else {
|
|
|
|
delete const_cast<nsTArray<nsCString>*>(mCArray);
|
|
|
|
}
|
2008-02-21 23:39:20 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const nsTArray<nsString>* mArray;
|
|
|
|
const nsTArray<nsCString>* mCArray;
|
|
|
|
};
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
inline uint32_t Count()
|
|
|
|
{
|
|
|
|
return mIsUnicode ? mArray->Length() : mCArray->Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mIndex;
|
|
|
|
|
|
|
|
// the owner allows us to hold a strong reference to the object
|
|
|
|
// that owns the array. Having a non-null value in mOwner implies
|
|
|
|
// that mOwnsArray is false, because we rely on the real owner
|
|
|
|
// to release the array
|
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
bool mOwnsArray;
|
|
|
|
bool mIsUnicode;
|
2008-02-21 23:39:20 +03:00
|
|
|
};
|
|
|
|
|
2018-08-19 00:22:47 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsStringEnumerator,
|
|
|
|
nsSimpleEnumerator,
|
|
|
|
nsIStringEnumerator,
|
|
|
|
nsIUTF8StringEnumerator)
|
2008-02-21 23:39:20 +03:00
|
|
|
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsStringEnumerator::HasMore(bool* aResult)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
*aResult = mIndex < Count();
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsStringEnumerator::HasMoreElements(bool* aResult)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
return HasMore(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
2018-08-19 04:28:10 +03:00
|
|
|
if (mIndex >= mArray->Length()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
if (mIsUnicode) {
|
2016-05-03 23:25:41 +03:00
|
|
|
nsSupportsString* stringImpl = new nsSupportsString();
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!stringImpl) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
stringImpl->SetData(mArray->ElementAt(mIndex++));
|
|
|
|
*aResult = stringImpl;
|
|
|
|
} else {
|
2016-05-03 23:25:41 +03:00
|
|
|
nsSupportsCString* cstringImpl = new nsSupportsCString();
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!cstringImpl) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
|
|
|
|
cstringImpl->SetData(mCArray->ElementAt(mIndex++));
|
|
|
|
*aResult = cstringImpl;
|
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsAString& aResult)
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(mIndex >= Count())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsUnicode) {
|
|
|
|
aResult = mArray->ElementAt(mIndex++);
|
|
|
|
} else {
|
|
|
|
CopyUTF8toUTF16(mCArray->ElementAt(mIndex++), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::GetNext(nsACString& aResult)
|
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(mIndex >= Count())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsUnicode) {
|
|
|
|
CopyUTF16toUTF8(mArray->ElementAt(mIndex++), aResult);
|
|
|
|
} else {
|
|
|
|
aResult = mCArray->ElementAt(mIndex++);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2018-08-25 02:21:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringEnumerator::StringIterator(nsIJSEnumerator** aRetVal)
|
|
|
|
{
|
|
|
|
auto result = MakeRefPtr<JSStringEnumerator>(this);
|
|
|
|
result.forget(aRetVal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-11 00:09:31 +03:00
|
|
|
template<class T>
|
|
|
|
static inline nsresult
|
2012-07-07 00:14:07 +04:00
|
|
|
StringEnumeratorTail(T** aResult)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!*aResult) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// constructors
|
|
|
|
//
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 23:14:14 +03:00
|
|
|
const nsTArray<nsString>* aArray, nsISupports* aOwner)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, aOwner);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2014-07-09 19:15:21 +04:00
|
|
|
const nsTArray<nsCString>* aArray,
|
|
|
|
nsISupports* aOwner)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, aOwner);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, true);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2009-01-22 07:15:34 +03:00
|
|
|
nsTArray<nsCString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, true);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// const ones internally just forward to the non-const equivalents
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewStringEnumerator(nsIStringEnumerator** aResult,
|
2009-01-18 23:14:14 +03:00
|
|
|
const nsTArray<nsString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, false);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2003-03-11 00:09:31 +03:00
|
|
|
NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
|
2009-01-22 07:15:34 +03:00
|
|
|
const nsTArray<nsCString>* aArray)
|
2003-03-11 00:09:31 +03:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aArray)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = new nsStringEnumerator(aArray, false);
|
|
|
|
return StringEnumeratorTail(aResult);
|
2003-03-11 00:09:31 +03:00
|
|
|
}
|
|
|
|
|