Bumping manifests a=b2g-bump
[gecko.git] / embedding / browser / nsEmbedStream.cpp
blobdf44a0d4b6b0caa679e8d2bcb7cdde816743f967
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"
10 #include "nsIPipe.h"
12 #include "nsEmbedStream.h"
13 #include "nsError.h"
14 #include "nsString.h"
16 NS_IMPL_ISUPPORTS0(nsEmbedStream)
18 nsEmbedStream::nsEmbedStream()
20 mOwner = nullptr;
23 nsEmbedStream::~nsEmbedStream()
27 void
28 nsEmbedStream::InitOwner(nsIWebBrowser *aOwner)
30 mOwner = aOwner;
33 NS_METHOD
34 nsEmbedStream::Init(void)
36 return NS_OK;
39 NS_METHOD
40 nsEmbedStream::OpenStream(nsIURI *aBaseURI, const nsACString& aContentType)
42 nsresult rv;
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
47 if (mOutputStream)
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);
55 if (NS_FAILED(rv))
56 return rv;
58 nsCOMPtr<nsIDocShell> docShell = do_GetInterface(mOwner);
59 rv = docShell->LoadStream(inputStream, aBaseURI, aContentType,
60 EmptyCString(), nullptr);
61 if (NS_FAILED(rv))
62 return rv;
64 mOutputStream = outputStream;
65 return rv;
68 NS_METHOD
69 nsEmbedStream::AppendToStream(const uint8_t *aData, uint32_t aLen)
71 nsresult rv;
72 NS_ENSURE_STATE(mOutputStream);
74 uint32_t bytesWritten = 0;
75 rv = mOutputStream->Write(reinterpret_cast<const char*>(aData),
76 aLen, &bytesWritten);
77 if (NS_FAILED(rv))
78 return rv;
80 NS_ASSERTION(bytesWritten == aLen,
81 "underlying buffer couldn't handle the write");
82 return rv;
85 NS_METHOD
86 nsEmbedStream::CloseStream(void)
88 nsresult rv = NS_OK;
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();
94 mOutputStream = 0;
96 return rv;