Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsIAsyncInputStream.h"
8 : #include "nsIAsyncOutputStream.h"
9 : #include "nsIDocShell.h"
10 : #include "nsIInterfaceRequestorUtils.h"
11 : #include "nsIPipe.h"
12 :
13 : #include "nsEmbedStream.h"
14 : #include "nsError.h"
15 : #include "nsString.h"
16 :
17 0 : NS_IMPL_ISUPPORTS0(nsEmbedStream)
18 :
19 0 : nsEmbedStream::nsEmbedStream()
20 : {
21 0 : mOwner = nullptr;
22 0 : }
23 :
24 0 : nsEmbedStream::~nsEmbedStream()
25 : {
26 0 : }
27 :
28 : void
29 0 : nsEmbedStream::InitOwner(nsIWebBrowser* aOwner)
30 : {
31 0 : mOwner = aOwner;
32 0 : }
33 :
34 : nsresult
35 0 : nsEmbedStream::Init(void)
36 : {
37 0 : return NS_OK;
38 : }
39 :
40 : nsresult
41 0 : nsEmbedStream::OpenStream(nsIURI* aBaseURI, const nsACString& aContentType)
42 : {
43 : nsresult rv;
44 0 : NS_ENSURE_ARG_POINTER(aBaseURI);
45 0 : NS_ENSURE_TRUE(IsASCII(aContentType), NS_ERROR_INVALID_ARG);
46 :
47 : // if we're already doing a stream, return an error
48 0 : if (mOutputStream) {
49 0 : return NS_ERROR_IN_PROGRESS;
50 : }
51 :
52 0 : nsCOMPtr<nsIAsyncInputStream> inputStream;
53 0 : nsCOMPtr<nsIAsyncOutputStream> outputStream;
54 0 : rv = NS_NewPipe2(getter_AddRefs(inputStream), getter_AddRefs(outputStream),
55 0 : true, false, 0, UINT32_MAX);
56 0 : if (NS_FAILED(rv)) {
57 0 : return rv;
58 : }
59 :
60 0 : nsCOMPtr<nsIDocShell> docShell = do_GetInterface(mOwner);
61 0 : rv = docShell->LoadStream(inputStream, aBaseURI, aContentType,
62 0 : EmptyCString(), nullptr);
63 0 : if (NS_FAILED(rv)) {
64 0 : return rv;
65 : }
66 :
67 0 : mOutputStream = outputStream;
68 0 : return rv;
69 : }
70 :
71 : nsresult
72 0 : nsEmbedStream::AppendToStream(const uint8_t* aData, uint32_t aLen)
73 : {
74 : nsresult rv;
75 0 : NS_ENSURE_STATE(mOutputStream);
76 :
77 0 : uint32_t bytesWritten = 0;
78 0 : rv = mOutputStream->Write(reinterpret_cast<const char*>(aData),
79 0 : aLen, &bytesWritten);
80 0 : if (NS_FAILED(rv)) {
81 0 : return rv;
82 : }
83 :
84 0 : NS_ASSERTION(bytesWritten == aLen,
85 : "underlying buffer couldn't handle the write");
86 0 : return rv;
87 : }
88 :
89 : nsresult
90 0 : nsEmbedStream::CloseStream(void)
91 : {
92 0 : nsresult rv = NS_OK;
93 :
94 : // NS_ENSURE_STATE returns NS_ERROR_UNEXPECTED if the condition isn't
95 : // satisfied; this is exactly what we want to return.
96 0 : NS_ENSURE_STATE(mOutputStream);
97 0 : mOutputStream->Close();
98 0 : mOutputStream = nullptr;
99 :
100 0 : return rv;
101 : }
|