Bug 545431, fix versioning of mac sdks, r=ted.mielczarek
[gecko.git] / embedding / browser / webBrowser / nsEmbedStream.cpp
blobc1d9f72354092439284d15ba4c9369f3ec3df7eb
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
13 * License.
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.
22 * Contributor(s):
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"
42 #include "nsIPipe.h"
44 #include "nsEmbedStream.h"
45 #include "nsNetError.h"
46 #include "nsString.h"
48 NS_IMPL_ISUPPORTS0(nsEmbedStream)
50 nsEmbedStream::nsEmbedStream()
52 mOwner = nsnull;
55 nsEmbedStream::~nsEmbedStream()
59 void
60 nsEmbedStream::InitOwner(nsIWebBrowser *aOwner)
62 mOwner = aOwner;
65 NS_METHOD
66 nsEmbedStream::Init(void)
68 return NS_OK;
71 NS_METHOD
72 nsEmbedStream::OpenStream(nsIURI *aBaseURI, const nsACString& aContentType)
74 nsresult rv;
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
79 if (mOutputStream)
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);
87 if (NS_FAILED(rv))
88 return rv;
90 nsCOMPtr<nsIDocShell> docShell = do_GetInterface(mOwner);
91 rv = docShell->LoadStream(inputStream, aBaseURI, aContentType,
92 EmptyCString(), nsnull);
93 if (NS_FAILED(rv))
94 return rv;
96 mOutputStream = outputStream;
97 return rv;
100 NS_METHOD
101 nsEmbedStream::AppendToStream(const PRUint8 *aData, PRUint32 aLen)
103 nsresult rv;
104 NS_ENSURE_STATE(mOutputStream);
106 PRUint32 bytesWritten = 0;
107 rv = mOutputStream->Write(reinterpret_cast<const char*>(aData),
108 aLen, &bytesWritten);
109 if (NS_FAILED(rv))
110 return rv;
112 NS_ASSERTION(bytesWritten == aLen,
113 "underlying buffer couldn't handle the write");
114 return rv;
117 NS_METHOD
118 nsEmbedStream::CloseStream(void)
120 nsresult rv = NS_OK;
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();
126 mOutputStream = 0;
128 return rv;