michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIAsyncInputStream.h" michael@0: #include "nsIAsyncOutputStream.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsIPipe.h" michael@0: michael@0: #include "nsEmbedStream.h" michael@0: #include "nsError.h" michael@0: #include "nsString.h" michael@0: michael@0: NS_IMPL_ISUPPORTS0(nsEmbedStream) michael@0: michael@0: nsEmbedStream::nsEmbedStream() michael@0: { michael@0: mOwner = nullptr; michael@0: } michael@0: michael@0: nsEmbedStream::~nsEmbedStream() michael@0: { michael@0: } michael@0: michael@0: void michael@0: nsEmbedStream::InitOwner(nsIWebBrowser *aOwner) michael@0: { michael@0: mOwner = aOwner; michael@0: } michael@0: michael@0: NS_METHOD michael@0: nsEmbedStream::Init(void) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_METHOD michael@0: nsEmbedStream::OpenStream(nsIURI *aBaseURI, const nsACString& aContentType) michael@0: { michael@0: nsresult rv; michael@0: NS_ENSURE_ARG_POINTER(aBaseURI); michael@0: NS_ENSURE_TRUE(IsASCII(aContentType), NS_ERROR_INVALID_ARG); michael@0: michael@0: // if we're already doing a stream, return an error michael@0: if (mOutputStream) michael@0: return NS_ERROR_IN_PROGRESS; michael@0: michael@0: nsCOMPtr inputStream; michael@0: nsCOMPtr outputStream; michael@0: rv = NS_NewPipe2(getter_AddRefs(inputStream), michael@0: getter_AddRefs(outputStream), michael@0: true, false, 0, UINT32_MAX); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCOMPtr docShell = do_GetInterface(mOwner); michael@0: rv = docShell->LoadStream(inputStream, aBaseURI, aContentType, michael@0: EmptyCString(), nullptr); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: mOutputStream = outputStream; michael@0: return rv; michael@0: } michael@0: michael@0: NS_METHOD michael@0: nsEmbedStream::AppendToStream(const uint8_t *aData, uint32_t aLen) michael@0: { michael@0: nsresult rv; michael@0: NS_ENSURE_STATE(mOutputStream); michael@0: michael@0: uint32_t bytesWritten = 0; michael@0: rv = mOutputStream->Write(reinterpret_cast(aData), michael@0: aLen, &bytesWritten); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: NS_ASSERTION(bytesWritten == aLen, michael@0: "underlying buffer couldn't handle the write"); michael@0: return rv; michael@0: } michael@0: michael@0: NS_METHOD michael@0: nsEmbedStream::CloseStream(void) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: // NS_ENSURE_STATE returns NS_ERROR_UNEXPECTED if the condition isn't michael@0: // satisfied; this is exactly what we want to return. michael@0: NS_ENSURE_STATE(mOutputStream); michael@0: mOutputStream->Close(); michael@0: mOutputStream = 0; michael@0: michael@0: return rv; michael@0: }