2009-07-03 18:00:02 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-03 18:00:02 -07:00
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIPipe.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsEmbedStream.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2009-07-03 18:00:02 -07:00
|
|
|
#include "nsString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-03 18:00:02 -07:00
|
|
|
NS_IMPL_ISUPPORTS0(nsEmbedStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsEmbedStream::nsEmbedStream()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwner = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsEmbedStream::~nsEmbedStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsEmbedStream::InitOwner(nsIWebBrowser *aOwner)
|
|
|
|
{
|
|
|
|
mOwner = aOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsEmbedStream::Init(void)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsEmbedStream::OpenStream(nsIURI *aBaseURI, const nsACString& aContentType)
|
|
|
|
{
|
2009-07-03 18:00:02 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aBaseURI);
|
|
|
|
NS_ENSURE_TRUE(IsASCII(aContentType), NS_ERROR_INVALID_ARG);
|
|
|
|
|
|
|
|
// if we're already doing a stream, return an error
|
2009-07-03 18:00:02 -07:00
|
|
|
if (mOutputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
|
2009-07-03 18:00:02 -07:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> inputStream;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> outputStream;
|
|
|
|
rv = NS_NewPipe2(getter_AddRefs(inputStream),
|
|
|
|
getter_AddRefs(outputStream),
|
2012-09-27 23:57:33 -07:00
|
|
|
true, false, 0, UINT32_MAX);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-07-03 18:00:02 -07:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = do_GetInterface(mOwner);
|
|
|
|
rv = docShell->LoadStream(inputStream, aBaseURI, aContentType,
|
2012-07-30 07:20:58 -07:00
|
|
|
EmptyCString(), nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-07-03 18:00:02 -07:00
|
|
|
mOutputStream = outputStream;
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEmbedStream::AppendToStream(const uint8_t *aData, uint32_t aLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2009-07-03 18:00:02 -07:00
|
|
|
NS_ENSURE_STATE(mOutputStream);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesWritten = 0;
|
2009-07-03 18:00:02 -07:00
|
|
|
rv = mOutputStream->Write(reinterpret_cast<const char*>(aData),
|
|
|
|
aLen, &bytesWritten);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-07-03 18:00:02 -07:00
|
|
|
NS_ASSERTION(bytesWritten == aLen,
|
|
|
|
"underlying buffer couldn't handle the write");
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsEmbedStream::CloseStream(void)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// NS_ENSURE_STATE returns NS_ERROR_UNEXPECTED if the condition isn't
|
|
|
|
// satisfied; this is exactly what we want to return.
|
2009-07-03 18:00:02 -07:00
|
|
|
NS_ENSURE_STATE(mOutputStream);
|
|
|
|
mOutputStream->Close();
|
|
|
|
mOutputStream = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|