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