зеркало из https://github.com/mozilla/pjs.git
fixes bug 192284 "support nsIChannel::open for all protocols" r=dougt sr=bzbarsky
This commit is contained in:
Родитель
38b78f289c
Коммит
dfb4b8260b
|
@ -166,8 +166,7 @@ nsDateTimeChannel::GetURI(nsIURI* *aURI)
|
|||
NS_IMETHODIMP
|
||||
nsDateTimeChannel::Open(nsIInputStream **_retval)
|
||||
{
|
||||
NS_NOTREACHED("nsDateTimeChannel::Open");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
return NS_ImplementChannelOpen(this, _retval);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
|
|
@ -185,8 +185,7 @@ nsFingerChannel::GetURI(nsIURI* *aURI)
|
|||
NS_IMETHODIMP
|
||||
nsFingerChannel::Open(nsIInputStream **_retval)
|
||||
{
|
||||
NS_NOTREACHED("nsFingerChannel::Open");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
return NS_ImplementChannelOpen(this, _retval);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
|
|
@ -74,6 +74,7 @@ XPIDLSRCS = \
|
|||
nsISimpleStreamListener.idl \
|
||||
nsIStreamTransportService.idl \
|
||||
nsIStreamLoader.idl \
|
||||
nsISyncStreamListener.idl \
|
||||
nsIUnicharStreamLoader.idl \
|
||||
nsIStandardURL.idl \
|
||||
nsIURLParser.idl \
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
/* ***** 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 IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* IBM Corp.
|
||||
*
|
||||
* 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 "nsIStreamListener.idl"
|
||||
|
||||
[scriptable, uuid(7e1aa658-6e3f-4521-9946-9685a169f764)]
|
||||
interface nsISyncStreamListener : nsIStreamListener
|
||||
{
|
||||
/**
|
||||
* Returns an input stream that when read will fetch data delivered
|
||||
* to the sync stream listener. The nsIInputStream implementation
|
||||
* will wait for OnDataAvailable events before returning from Read.
|
||||
*/
|
||||
readonly attribute nsIInputStream inputStream;
|
||||
};
|
|
@ -80,6 +80,7 @@
|
|||
#include "nsIInputStreamPump.h"
|
||||
#include "nsIAsyncStreamCopier.h"
|
||||
#include "nsIPersistentProperties2.h"
|
||||
#include "nsISyncStreamListener.h"
|
||||
|
||||
// Helper, to simplify getting the I/O service.
|
||||
inline const nsGetServiceByCID
|
||||
|
@ -448,6 +449,47 @@ NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **aResult,
|
|||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_NewSyncStreamListener(nsIStreamListener **aResult,
|
||||
nsIInputStream **aStream)
|
||||
{
|
||||
nsresult rv;
|
||||
static NS_DEFINE_CID(kSyncStreamListenerCID, NS_SYNCSTREAMLISTENER_CID);
|
||||
nsCOMPtr<nsISyncStreamListener> listener =
|
||||
do_CreateInstance(kSyncStreamListenerCID, &rv);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = listener->GetInputStream(aStream);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
NS_ADDREF(*aResult = listener);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement the nsIChannel::Open(nsIInputStream**) method using the channel's
|
||||
* AsyncOpen method.
|
||||
*/
|
||||
inline nsresult
|
||||
NS_ImplementChannelOpen(nsIChannel *aChannel,
|
||||
nsIInputStream **aResult)
|
||||
{
|
||||
nsCOMPtr<nsIStreamListener> listener;
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
nsresult rv = NS_NewSyncStreamListener(getter_AddRefs(listener),
|
||||
getter_AddRefs(stream));
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = aChannel->AsyncOpen(listener, nsnull);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
PRUint32 n;
|
||||
// block until the initial response is received or an error occurs.
|
||||
rv = stream->Available(&n);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
NS_ADDREF(*aResult = stream);
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
NS_NewRequestObserverProxy(nsIRequestObserver **aResult,
|
||||
nsIRequestObserver *aObserver,
|
||||
|
|
|
@ -61,6 +61,7 @@ CPPSRCS = \
|
|||
nsSocketTransportService2.cpp \
|
||||
nsStreamListenerTee.cpp \
|
||||
nsStreamLoader.cpp \
|
||||
nsSyncStreamListener.cpp \
|
||||
nsUnicharStreamLoader.cpp \
|
||||
nsURIChecker.cpp \
|
||||
nsURLHelper.cpp \
|
||||
|
|
|
@ -0,0 +1,199 @@
|
|||
/* ***** 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 IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* IBM Corp.
|
||||
*
|
||||
* 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 "nsSyncStreamListener.h"
|
||||
#include "nsIPipe.h"
|
||||
#include "nsEventQueueUtils.h"
|
||||
#include "nsNetSegmentUtils.h"
|
||||
|
||||
nsresult
|
||||
nsSyncStreamListener::Init()
|
||||
{
|
||||
return NS_NewPipe(getter_AddRefs(mPipeIn),
|
||||
getter_AddRefs(mPipeOut),
|
||||
NET_DEFAULT_SEGMENT_SIZE,
|
||||
PR_UINT32_MAX, // no size limit
|
||||
PR_FALSE,
|
||||
PR_FALSE);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsSyncStreamListener::WaitForData()
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
if (!mEventQ) {
|
||||
rv = NS_GetCurrentEventQ(getter_AddRefs(mEventQ));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
}
|
||||
|
||||
mKeepWaiting = PR_TRUE;
|
||||
|
||||
PLEvent *ev;
|
||||
while (mKeepWaiting) {
|
||||
rv = mEventQ->WaitForEvent(&ev);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = mEventQ->HandleEvent(ev);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// nsSyncStreamListener::nsISupports
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NS_IMPL_ISUPPORTS4(nsSyncStreamListener,
|
||||
nsIStreamListener,
|
||||
nsIRequestObserver,
|
||||
nsIInputStream,
|
||||
nsISyncStreamListener)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// nsSyncStreamListener::nsISyncStreamListener
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::GetInputStream(nsIInputStream **result)
|
||||
{
|
||||
NS_ADDREF(*result = this);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// nsSyncStreamListener::nsIStreamListener
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::OnStartRequest(nsIRequest *request,
|
||||
nsISupports *context)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::OnDataAvailable(nsIRequest *request,
|
||||
nsISupports *context,
|
||||
nsIInputStream *stream,
|
||||
PRUint32 offset,
|
||||
PRUint32 count)
|
||||
{
|
||||
PRUint32 bytesWritten;
|
||||
|
||||
nsresult rv = mPipeOut->WriteFrom(stream, count, &bytesWritten);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
mKeepWaiting = PR_FALSE; // unblock Read
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::OnStopRequest(nsIRequest *request,
|
||||
nsISupports *context,
|
||||
nsresult status)
|
||||
{
|
||||
mStatus = status;
|
||||
mKeepWaiting = PR_FALSE; // unblock Read
|
||||
mDone = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// nsSyncStreamListener::nsIInputStream
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::Close()
|
||||
{
|
||||
mStatus = NS_BASE_STREAM_CLOSED;
|
||||
mDone = PR_TRUE;
|
||||
// XXX shouldn't we cancel the nsIRequest at this point?
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::Available(PRUint32 *result)
|
||||
{
|
||||
if (NS_FAILED(mStatus))
|
||||
return mStatus;
|
||||
|
||||
mStatus = mPipeIn->Available(result);
|
||||
if (NS_SUCCEEDED(mStatus) && (*result == 0) && !mDone) {
|
||||
mStatus = WaitForData();
|
||||
if (NS_SUCCEEDED(mStatus))
|
||||
mStatus = mPipeIn->Available(result);
|
||||
}
|
||||
return mStatus;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::Read(char *buf,
|
||||
PRUint32 bufLen,
|
||||
PRUint32 *result)
|
||||
{
|
||||
PRUint32 avail;
|
||||
if (NS_FAILED(Available(&avail)))
|
||||
return mStatus;
|
||||
|
||||
avail = PR_MIN(avail, bufLen);
|
||||
mStatus = mPipeIn->Read(buf, avail, result);
|
||||
return mStatus;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::ReadSegments(nsWriteSegmentFun writer,
|
||||
void *closure,
|
||||
PRUint32 count,
|
||||
PRUint32 *result)
|
||||
{
|
||||
PRUint32 avail;
|
||||
if (NS_FAILED(Available(&avail)))
|
||||
return mStatus;
|
||||
|
||||
avail = PR_MIN(avail, count);
|
||||
mStatus = mPipeIn->ReadSegments(writer, closure, avail, result);
|
||||
return mStatus;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSyncStreamListener::IsNonBlocking(PRBool *result)
|
||||
{
|
||||
*result = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
/* ***** 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 IBM Corporation.
|
||||
* Portions created by IBM Corporation are Copyright (C) 2003
|
||||
* IBM Corporation. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* IBM Corp.
|
||||
*
|
||||
* 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 ***** */
|
||||
|
||||
#ifndef nsSyncStreamListener_h__
|
||||
#define nsSyncStreamListener_h__
|
||||
|
||||
#include "nsISyncStreamListener.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIOutputStream.h"
|
||||
#include "nsIEventQueue.h"
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class nsSyncStreamListener : public nsISyncStreamListener
|
||||
, public nsIInputStream
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIREQUESTOBSERVER
|
||||
NS_DECL_NSISTREAMLISTENER
|
||||
NS_DECL_NSISYNCSTREAMLISTENER
|
||||
NS_DECL_NSIINPUTSTREAM
|
||||
|
||||
nsSyncStreamListener()
|
||||
: mStatus(NS_OK)
|
||||
, mKeepWaiting(PR_FALSE)
|
||||
, mDone(PR_FALSE) {}
|
||||
virtual ~nsSyncStreamListener() {}
|
||||
|
||||
nsresult Init();
|
||||
|
||||
private:
|
||||
|
||||
nsresult WaitForData();
|
||||
|
||||
nsCOMPtr<nsIInputStream> mPipeIn;
|
||||
nsCOMPtr<nsIOutputStream> mPipeOut;
|
||||
nsCOMPtr<nsIEventQueue> mEventQ;
|
||||
nsresult mStatus;
|
||||
PRPackedBool mKeepWaiting;
|
||||
PRPackedBool mDone;
|
||||
};
|
||||
|
||||
#endif // nsSyncStreamListener_h__
|
|
@ -309,6 +309,19 @@
|
|||
{0xbb, 0x4f, 0x96, 0x5c, 0xff, 0xd2, 0x3e, 0xce} \
|
||||
}
|
||||
|
||||
// component implementing nsISyncStreamListener.
|
||||
#define NS_SYNCSTREAMLISTENER_CLASSNAME \
|
||||
"nsSyncStreamListener"
|
||||
#define NS_SYNCSTREAMLISTENER_CONTRACTID \
|
||||
"@mozilla.org/network/sync-stream-listener;1"
|
||||
#define NS_SYNCSTREAMLISTENER_CID \
|
||||
{ /* 439400d3-6f23-43db-8b06-8aafe1869bd8 */ \
|
||||
0x439400d3, \
|
||||
0x6f23, \
|
||||
0x43db, \
|
||||
{0x8b, 0x06, 0x8a, 0xaf, 0xe1, 0x86, 0x9b, 0xd8} \
|
||||
}
|
||||
|
||||
// component implementing nsIURIChecker.
|
||||
#define NS_URICHECKER_CLASSNAME \
|
||||
"nsURIChecker"
|
||||
|
|
|
@ -98,6 +98,9 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsInputStreamChannel)
|
|||
#include "nsDownloader.h"
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDownloader)
|
||||
|
||||
#include "nsSyncStreamListener.h"
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsSyncStreamListener, Init)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "nsStreamConverterService.h"
|
||||
|
@ -620,6 +623,10 @@ static const nsModuleComponentInfo gNetModuleInfo[] = {
|
|||
NS_DOWNLOADER_CID,
|
||||
NS_DOWNLOADER_CONTRACTID,
|
||||
nsDownloaderConstructor },
|
||||
{ NS_SYNCSTREAMLISTENER_CLASSNAME,
|
||||
NS_SYNCSTREAMLISTENER_CID,
|
||||
NS_SYNCSTREAMLISTENER_CONTRACTID,
|
||||
nsSyncStreamListenerConstructor },
|
||||
{ NS_REQUESTOBSERVERPROXY_CLASSNAME,
|
||||
NS_REQUESTOBSERVERPROXY_CID,
|
||||
NS_REQUESTOBSERVERPROXY_CONTRACTID,
|
||||
|
|
|
@ -253,8 +253,7 @@ nsFTPChannel::GetURI(nsIURI* *aURL)
|
|||
NS_IMETHODIMP
|
||||
nsFTPChannel::Open(nsIInputStream **result)
|
||||
{
|
||||
NS_NOTREACHED("nsFTPChannel::Open");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
return NS_ImplementChannelOpen(this, result);
|
||||
}
|
||||
|
||||
nsresult
|
||||
|
|
|
@ -236,8 +236,7 @@ nsGopherChannel::GetURI(nsIURI* *aURI)
|
|||
NS_IMETHODIMP
|
||||
nsGopherChannel::Open(nsIInputStream **_retval)
|
||||
{
|
||||
NS_NOTREACHED("nsGopherChannel::Open");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
return NS_ImplementChannelOpen(this, _retval);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
|
|
@ -2709,8 +2709,7 @@ nsHttpChannel::SetContentLength(PRInt32 value)
|
|||
NS_IMETHODIMP
|
||||
nsHttpChannel::Open(nsIInputStream **_retval)
|
||||
{
|
||||
NS_NOTYETIMPLEMENTED("nsHttpChannel::Open");
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
return NS_ImplementChannelOpen(this, _retval);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
|
Загрузка…
Ссылка в новой задаче