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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Christopher Blizzard <blizzard@mozilla.org>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsIAsyncInputStream.h"
40 : #include "nsIDocShell.h"
41 : #include "nsIInterfaceRequestorUtils.h"
42 : #include "nsIPipe.h"
43 :
44 : #include "nsEmbedStream.h"
45 : #include "nsNetError.h"
46 : #include "nsString.h"
47 :
48 0 : NS_IMPL_ISUPPORTS0(nsEmbedStream)
49 :
50 0 : nsEmbedStream::nsEmbedStream()
51 : {
52 0 : mOwner = nsnull;
53 0 : }
54 :
55 0 : nsEmbedStream::~nsEmbedStream()
56 : {
57 0 : }
58 :
59 : void
60 0 : nsEmbedStream::InitOwner(nsIWebBrowser *aOwner)
61 : {
62 0 : mOwner = aOwner;
63 0 : }
64 :
65 : NS_METHOD
66 0 : nsEmbedStream::Init(void)
67 : {
68 0 : return NS_OK;
69 : }
70 :
71 : NS_METHOD
72 0 : nsEmbedStream::OpenStream(nsIURI *aBaseURI, const nsACString& aContentType)
73 : {
74 : nsresult rv;
75 0 : NS_ENSURE_ARG_POINTER(aBaseURI);
76 0 : NS_ENSURE_TRUE(IsASCII(aContentType), NS_ERROR_INVALID_ARG);
77 :
78 : // if we're already doing a stream, return an error
79 0 : if (mOutputStream)
80 0 : return NS_ERROR_IN_PROGRESS;
81 :
82 0 : nsCOMPtr<nsIAsyncInputStream> inputStream;
83 0 : nsCOMPtr<nsIAsyncOutputStream> outputStream;
84 0 : rv = NS_NewPipe2(getter_AddRefs(inputStream),
85 0 : getter_AddRefs(outputStream),
86 0 : true, false, 0, PR_UINT32_MAX);
87 0 : if (NS_FAILED(rv))
88 0 : return rv;
89 :
90 0 : nsCOMPtr<nsIDocShell> docShell = do_GetInterface(mOwner);
91 0 : rv = docShell->LoadStream(inputStream, aBaseURI, aContentType,
92 0 : EmptyCString(), nsnull);
93 0 : if (NS_FAILED(rv))
94 0 : return rv;
95 :
96 0 : mOutputStream = outputStream;
97 0 : return rv;
98 : }
99 :
100 : NS_METHOD
101 0 : nsEmbedStream::AppendToStream(const PRUint8 *aData, PRUint32 aLen)
102 : {
103 : nsresult rv;
104 0 : NS_ENSURE_STATE(mOutputStream);
105 :
106 0 : PRUint32 bytesWritten = 0;
107 0 : rv = mOutputStream->Write(reinterpret_cast<const char*>(aData),
108 0 : aLen, &bytesWritten);
109 0 : if (NS_FAILED(rv))
110 0 : return rv;
111 :
112 0 : NS_ASSERTION(bytesWritten == aLen,
113 : "underlying buffer couldn't handle the write");
114 0 : return rv;
115 : }
116 :
117 : NS_METHOD
118 0 : nsEmbedStream::CloseStream(void)
119 : {
120 0 : nsresult rv = NS_OK;
121 :
122 : // NS_ENSURE_STATE returns NS_ERROR_UNEXPECTED if the condition isn't
123 : // satisfied; this is exactly what we want to return.
124 0 : NS_ENSURE_STATE(mOutputStream);
125 0 : mOutputStream->Close();
126 0 : mOutputStream = 0;
127 :
128 0 : return rv;
129 : }
|