1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Christopher Blizzard.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
23 * Christopher Blizzard <blizzard@mozilla.org>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsIAsyncInputStream.h"
40 #include "nsIDocShell.h"
41 #include "nsIInterfaceRequestorUtils.h"
44 #include "nsEmbedStream.h"
45 #include "nsNetError.h"
48 NS_IMPL_ISUPPORTS0(nsEmbedStream
)
50 nsEmbedStream::nsEmbedStream()
55 nsEmbedStream::~nsEmbedStream()
60 nsEmbedStream::InitOwner(nsIWebBrowser
*aOwner
)
66 nsEmbedStream::Init(void)
72 nsEmbedStream::OpenStream(nsIURI
*aBaseURI
, const nsACString
& aContentType
)
75 NS_ENSURE_ARG_POINTER(aBaseURI
);
76 NS_ENSURE_TRUE(IsASCII(aContentType
), NS_ERROR_INVALID_ARG
);
78 // if we're already doing a stream, return an error
80 return NS_ERROR_IN_PROGRESS
;
82 nsCOMPtr
<nsIAsyncInputStream
> inputStream
;
83 nsCOMPtr
<nsIAsyncOutputStream
> outputStream
;
84 rv
= NS_NewPipe2(getter_AddRefs(inputStream
),
85 getter_AddRefs(outputStream
),
86 PR_TRUE
, PR_FALSE
, 0, PR_UINT32_MAX
);
90 nsCOMPtr
<nsIDocShell
> docShell
= do_GetInterface(mOwner
);
91 rv
= docShell
->LoadStream(inputStream
, aBaseURI
, aContentType
,
92 EmptyCString(), nsnull
);
96 mOutputStream
= outputStream
;
101 nsEmbedStream::AppendToStream(const PRUint8
*aData
, PRUint32 aLen
)
104 NS_ENSURE_STATE(mOutputStream
);
106 PRUint32 bytesWritten
= 0;
107 rv
= mOutputStream
->Write(reinterpret_cast<const char*>(aData
),
108 aLen
, &bytesWritten
);
112 NS_ASSERTION(bytesWritten
== aLen
,
113 "underlying buffer couldn't handle the write");
118 nsEmbedStream::CloseStream(void)
122 // NS_ENSURE_STATE returns NS_ERROR_UNEXPECTED if the condition isn't
123 // satisfied; this is exactly what we want to return.
124 NS_ENSURE_STATE(mOutputStream
);
125 mOutputStream
->Close();