Bug 1100398 P2 Make nsStringInputStream cloneable. r=froydnj

This commit is contained in:
Ben Kelly 2015-02-10 23:55:43 -05:00
parent a4ec611b4f
commit d38f4641c3
3 changed files with 100 additions and 2 deletions

View File

@ -13,6 +13,7 @@
#include "nsStringStream.h"
#include "nsStreamUtils.h"
#include "nsReadableUtils.h"
#include "nsICloneableInputStream.h"
#include "nsISeekableStream.h"
#include "nsISupportsPrimitives.h"
#include "nsCRT.h"
@ -34,6 +35,7 @@ class nsStringInputStream MOZ_FINAL
, public nsISeekableStream
, public nsISupportsCString
, public nsIIPCSerializableInputStream
, public nsICloneableInputStream
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
@ -43,12 +45,21 @@ public:
NS_DECL_NSISUPPORTSPRIMITIVE
NS_DECL_NSISUPPORTSCSTRING
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
NS_DECL_NSICLONEABLEINPUTSTREAM
nsStringInputStream()
{
Clear();
}
explicit nsStringInputStream(const nsStringInputStream& aOther)
: mOffset(aOther.mOffset)
{
// Use Assign() here because we don't want the life of the clone to be
// dependent on the life of the original stream.
mData.Assign(aOther.mData);
}
private:
~nsStringInputStream()
{
@ -90,12 +101,14 @@ NS_IMPL_QUERY_INTERFACE_CI(nsStringInputStream,
nsIInputStream,
nsISupportsCString,
nsISeekableStream,
nsIIPCSerializableInputStream)
nsIIPCSerializableInputStream,
nsICloneableInputStream)
NS_IMPL_CI_INTERFACE_GETTER(nsStringInputStream,
nsIStringInputStream,
nsIInputStream,
nsISupportsCString,
nsISeekableStream)
nsISeekableStream,
nsICloneableInputStream)
/////////
// nsISupportsCString implementation
@ -308,6 +321,10 @@ nsStringInputStream::SetEOF()
return NS_OK;
}
/////////
// nsIIPCSerializableInputStream implementation
/////////
void
nsStringInputStream::Serialize(InputStreamParams& aParams,
FileDescriptorArray& /* aFDs */)
@ -337,6 +354,25 @@ nsStringInputStream::Deserialize(const InputStreamParams& aParams,
return true;
}
/////////
// nsICloneableInputStream implementation
/////////
NS_IMETHODIMP
nsStringInputStream::GetCloneable(bool* aCloneableOut)
{
*aCloneableOut = true;
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::Clone(nsIInputStream** aCloneOut)
{
nsRefPtr<nsIInputStream> ref = new nsStringInputStream(*this);
ref.forget(aCloneOut);
return NS_OK;
}
nsresult
NS_NewByteInputStream(nsIInputStream** aStreamResult,
const char* aStringToRead, int32_t aLength,

View File

@ -0,0 +1,61 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "gtest/gtest.h"
#include "Helpers.h"
#include "nsICloneableInputStream.h"
#include "nsStringStream.h"
namespace {
static void TestStringStream(uint32_t aNumBytes)
{
nsTArray<char> inputData;
testing::CreateData(aNumBytes, inputData);
nsDependentCSubstring inputString(inputData.Elements(), inputData.Length());
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream), inputString);
ASSERT_TRUE(NS_SUCCEEDED(rv));
testing::ConsumeAndValidateStream(stream, inputString);
}
static void TestStringStreamClone(uint32_t aNumBytes)
{
nsTArray<char> inputData;
testing::CreateData(aNumBytes, inputData);
nsDependentCSubstring inputString(inputData.Elements(), inputData.Length());
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream), inputString);
ASSERT_TRUE(NS_SUCCEEDED(rv));
nsCOMPtr<nsICloneableInputStream> cloneable = do_QueryInterface(stream);
ASSERT_TRUE(cloneable != nullptr);
ASSERT_TRUE(cloneable->GetCloneable());
nsCOMPtr<nsIInputStream> clone;
rv = cloneable->Clone(getter_AddRefs(clone));
testing::ConsumeAndValidateStream(stream, inputString);
// Release the stream to verify that the clone's string survives correctly.
stream = nullptr;
testing::ConsumeAndValidateStream(clone, inputString);
}
} // anonymous namespace
TEST(StringStream, Simple_4k)
{
TestStringStream(1024 * 4);
}
TEST(StringStream, Clone_4k)
{
TestStringStreamClone(1024 * 4);
}

View File

@ -14,6 +14,7 @@ UNIFIED_SOURCES += [
'TestSnappyStreams.cpp',
'TestStorageStream.cpp',
'TestStrings.cpp',
'TestStringStream.cpp',
'TestSynchronization.cpp',
'TestThreadPool.cpp',
'TestTimeStamp.cpp',