|
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/. */ |
|
5 |
|
6 #include "nsIAsyncInputStream.h" |
|
7 #include "nsIAsyncOutputStream.h" |
|
8 #include "nsIDocShell.h" |
|
9 #include "nsIInterfaceRequestorUtils.h" |
|
10 #include "nsIPipe.h" |
|
11 |
|
12 #include "nsEmbedStream.h" |
|
13 #include "nsError.h" |
|
14 #include "nsString.h" |
|
15 |
|
16 NS_IMPL_ISUPPORTS0(nsEmbedStream) |
|
17 |
|
18 nsEmbedStream::nsEmbedStream() |
|
19 { |
|
20 mOwner = nullptr; |
|
21 } |
|
22 |
|
23 nsEmbedStream::~nsEmbedStream() |
|
24 { |
|
25 } |
|
26 |
|
27 void |
|
28 nsEmbedStream::InitOwner(nsIWebBrowser *aOwner) |
|
29 { |
|
30 mOwner = aOwner; |
|
31 } |
|
32 |
|
33 NS_METHOD |
|
34 nsEmbedStream::Init(void) |
|
35 { |
|
36 return NS_OK; |
|
37 } |
|
38 |
|
39 NS_METHOD |
|
40 nsEmbedStream::OpenStream(nsIURI *aBaseURI, const nsACString& aContentType) |
|
41 { |
|
42 nsresult rv; |
|
43 NS_ENSURE_ARG_POINTER(aBaseURI); |
|
44 NS_ENSURE_TRUE(IsASCII(aContentType), NS_ERROR_INVALID_ARG); |
|
45 |
|
46 // if we're already doing a stream, return an error |
|
47 if (mOutputStream) |
|
48 return NS_ERROR_IN_PROGRESS; |
|
49 |
|
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; |
|
57 |
|
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; |
|
63 |
|
64 mOutputStream = outputStream; |
|
65 return rv; |
|
66 } |
|
67 |
|
68 NS_METHOD |
|
69 nsEmbedStream::AppendToStream(const uint8_t *aData, uint32_t aLen) |
|
70 { |
|
71 nsresult rv; |
|
72 NS_ENSURE_STATE(mOutputStream); |
|
73 |
|
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; |
|
79 |
|
80 NS_ASSERTION(bytesWritten == aLen, |
|
81 "underlying buffer couldn't handle the write"); |
|
82 return rv; |
|
83 } |
|
84 |
|
85 NS_METHOD |
|
86 nsEmbedStream::CloseStream(void) |
|
87 { |
|
88 nsresult rv = NS_OK; |
|
89 |
|
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; |
|
95 |
|
96 return rv; |
|
97 } |