fixes bug 143311 "nsStreamListenerTee should completely drop mSink during
OnStopRequest" r=gordon,dougt sr=rpotts git-svn-id: svn://10.0.0.236/trunk@121271 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
c77048cb3b
commit
b529cd37d3
@ -1,3 +1,40 @@
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Netscape Communications Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2002
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Darin Fisher <darin@netscape.com> (original author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsStreamListenerTee.h"
|
||||
|
||||
NS_IMPL_ISUPPORTS3(nsStreamListenerTee,
|
||||
@ -19,7 +56,11 @@ nsStreamListenerTee::OnStopRequest(nsIRequest *request,
|
||||
nsresult status)
|
||||
{
|
||||
NS_ENSURE_TRUE(mListener, NS_ERROR_NOT_INITIALIZED);
|
||||
mInputTee = 0;
|
||||
// it is critical that we close out the input stream tee
|
||||
if (mInputTee) {
|
||||
mInputTee->SetSink(nsnull);
|
||||
mInputTee = 0;
|
||||
}
|
||||
mSink = 0;
|
||||
return mListener->OnStopRequest(request, context, status);
|
||||
}
|
||||
@ -46,7 +87,7 @@ nsStreamListenerTee::OnDataAvailable(nsIRequest *request,
|
||||
}
|
||||
else {
|
||||
// re-initialize the input tee since the input stream may have changed.
|
||||
rv = mInputTee->Init(input, mSink);
|
||||
rv = mInputTee->SetSource(input);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
tee = do_QueryInterface(mInputTee, &rv);
|
||||
|
||||
@ -48,8 +48,8 @@ interface nsIOutputStream;
|
||||
[scriptable, uuid(44e8b2c8-1ecb-4a63-8b23-3e3500c34f32)]
|
||||
interface nsIInputStreamTee : nsIInputStream
|
||||
{
|
||||
void init(in nsIInputStream source,
|
||||
in nsIOutputStream sink);
|
||||
attribute nsIInputStream source;
|
||||
attribute nsIOutputStream sink;
|
||||
};
|
||||
|
||||
%{C++
|
||||
|
||||
@ -76,15 +76,23 @@ nsInputStreamTee::~nsInputStreamTee()
|
||||
nsresult
|
||||
nsInputStreamTee::TeeSegment(const char *buf, PRUint32 count)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
if (!mSink)
|
||||
return NS_OK; // nothing to do
|
||||
nsresult rv;
|
||||
PRUint32 bytesWritten = 0;
|
||||
while (count) {
|
||||
rv = mSink->Write(buf + bytesWritten, count, &bytesWritten);
|
||||
if (NS_FAILED(rv)) break;
|
||||
if (NS_FAILED(rv)) {
|
||||
// ok, this is not a fatal error... just drop our reference to mSink
|
||||
// and continue on as if nothing happened.
|
||||
NS_WARNING("Write failed (non-fatal)");
|
||||
mSink = 0;
|
||||
break;
|
||||
}
|
||||
NS_ASSERTION(bytesWritten <= count, "wrote too much");
|
||||
count -= bytesWritten;
|
||||
}
|
||||
return rv;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_METHOD
|
||||
@ -128,7 +136,6 @@ NS_IMETHODIMP
|
||||
nsInputStreamTee::Read(char *buf, PRUint32 count, PRUint32 *bytesRead)
|
||||
{
|
||||
NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
|
||||
NS_ENSURE_TRUE(mSink, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
nsresult rv = mSource->Read(buf, count, bytesRead);
|
||||
if (NS_FAILED(rv) || (*bytesRead == 0))
|
||||
@ -144,7 +151,6 @@ nsInputStreamTee::ReadSegments(nsWriteSegmentFun writer,
|
||||
PRUint32 *bytesRead)
|
||||
{
|
||||
NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
|
||||
NS_ENSURE_TRUE(mSink, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
mWriter = writer;
|
||||
mClosure = closure;
|
||||
@ -159,25 +165,44 @@ nsInputStreamTee::IsNonBlocking(PRBool *result)
|
||||
return mSource->IsNonBlocking(result);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsInputStreamTee::Init(nsIInputStream *source, nsIOutputStream *sink)
|
||||
NS_IMETHODIMP
|
||||
nsInputStreamTee::SetSource(nsIInputStream *source)
|
||||
{
|
||||
NS_ENSURE_TRUE(source, NS_ERROR_NOT_INITIALIZED);
|
||||
NS_ENSURE_TRUE(sink, NS_ERROR_NOT_INITIALIZED);
|
||||
|
||||
PRBool nonBlocking = PR_FALSE;
|
||||
nsresult rv = sink->IsNonBlocking(&nonBlocking);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
if (nonBlocking) {
|
||||
NS_NOTREACHED("Can only tee data to a blocking output stream");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
mSource = source;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsInputStreamTee::GetSource(nsIInputStream **source)
|
||||
{
|
||||
NS_IF_ADDREF(*source = mSource);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsInputStreamTee::SetSink(nsIOutputStream *sink)
|
||||
{
|
||||
if (sink) {
|
||||
PRBool nonBlocking = PR_FALSE;
|
||||
nsresult rv = sink->IsNonBlocking(&nonBlocking);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
if (nonBlocking) {
|
||||
NS_NOTREACHED("Can only tee data to a blocking output stream");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
}
|
||||
mSink = sink;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsInputStreamTee::GetSink(nsIOutputStream **sink)
|
||||
{
|
||||
NS_IF_ADDREF(*sink = mSink);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// factory method
|
||||
|
||||
NS_COM nsresult
|
||||
@ -185,13 +210,19 @@ NS_NewInputStreamTee(nsIInputStream **result,
|
||||
nsIInputStream *source,
|
||||
nsIOutputStream *sink)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsIInputStreamTee> tee;
|
||||
NS_NEWXPCOM(tee, nsInputStreamTee);
|
||||
if (!tee)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv = tee->Init(source, sink);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
NS_ADDREF(*result = tee);
|
||||
rv = tee->SetSource(source);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = tee->SetSink(sink);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
NS_ADDREF(*result = tee);
|
||||
return rv;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user