1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsIAsyncInputStream.h"
7 #include "nsIAsyncOutputStream.h"
8 #include "nsIDocShell.h"
9 #include "nsIInterfaceRequestorUtils.h"
12 #include "nsEmbedStream.h"
16 NS_IMPL_ISUPPORTS0(nsEmbedStream
)
18 nsEmbedStream::nsEmbedStream()
23 nsEmbedStream::~nsEmbedStream()
28 nsEmbedStream::InitOwner(nsIWebBrowser
*aOwner
)
34 nsEmbedStream::Init(void)
40 nsEmbedStream::OpenStream(nsIURI
*aBaseURI
, const nsACString
& aContentType
)
43 NS_ENSURE_ARG_POINTER(aBaseURI
);
44 NS_ENSURE_TRUE(IsASCII(aContentType
), NS_ERROR_INVALID_ARG
);
46 // if we're already doing a stream, return an error
48 return NS_ERROR_IN_PROGRESS
;
50 nsCOMPtr
<nsIAsyncInputStream
> inputStream
;
51 nsCOMPtr
<nsIAsyncOutputStream
> outputStream
;
52 rv
= NS_NewPipe2(getter_AddRefs(inputStream
),
53 getter_AddRefs(outputStream
),
54 true, false, 0, UINT32_MAX
);
58 nsCOMPtr
<nsIDocShell
> docShell
= do_GetInterface(mOwner
);
59 rv
= docShell
->LoadStream(inputStream
, aBaseURI
, aContentType
,
60 EmptyCString(), nullptr);
64 mOutputStream
= outputStream
;
69 nsEmbedStream::AppendToStream(const uint8_t *aData
, uint32_t aLen
)
72 NS_ENSURE_STATE(mOutputStream
);
74 uint32_t bytesWritten
= 0;
75 rv
= mOutputStream
->Write(reinterpret_cast<const char*>(aData
),
80 NS_ASSERTION(bytesWritten
== aLen
,
81 "underlying buffer couldn't handle the write");
86 nsEmbedStream::CloseStream(void)
90 // NS_ENSURE_STATE returns NS_ERROR_UNEXPECTED if the condition isn't
91 // satisfied; this is exactly what we want to return.
92 NS_ENSURE_STATE(mOutputStream
);
93 mOutputStream
->Close();