2001-09-25 23:31:24 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
2001-01-12 20:06:48 +00:00
|
|
|
*
|
2001-09-25 23:31:24 +00:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
2001-01-12 20:06:48 +00:00
|
|
|
*
|
2001-09-25 23:31:24 +00:00
|
|
|
* 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.
|
2001-01-12 20:06:48 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-25 23:31:24 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-01-12 20:06:48 +00:00
|
|
|
*
|
2001-09-25 23:31:24 +00:00
|
|
|
* Contributor(s):
|
2002-01-24 13:38:51 +00:00
|
|
|
* Peter Van der Beken <peterv@netscape.com>
|
2001-01-12 20:06:48 +00:00
|
|
|
*
|
2001-09-25 23:31:24 +00:00
|
|
|
*
|
|
|
|
* 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 NPL, 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 NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-01-12 20:06:48 +00:00
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIDOMLoadListener.h"
|
|
|
|
#include "nsIHttpEventSink.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsISyncLoadDOMService.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakReference.h"
|
2002-06-14 23:54:18 +00:00
|
|
|
#include "jsapi.h"
|
2001-08-02 20:29:20 +00:00
|
|
|
#include "nsIDocument.h"
|
2001-01-12 20:06:48 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
2002-01-24 13:38:51 +00:00
|
|
|
#include "nsIDOMElement.h"
|
2001-01-12 20:06:48 +00:00
|
|
|
#include "nsIDOMDOMImplementation.h"
|
|
|
|
#include "nsIDOMEventReceiver.h"
|
2001-08-02 20:29:20 +00:00
|
|
|
#include "nsIEventQueueService.h"
|
2002-06-14 23:54:18 +00:00
|
|
|
#include "nsIJSContextStack.h"
|
2001-08-03 13:48:30 +00:00
|
|
|
#include "nsIPrivateDOMImplementation.h"
|
2002-06-14 23:54:18 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
2001-08-02 20:29:20 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2002-06-14 23:54:18 +00:00
|
|
|
#include "nsContentCID.h"
|
2001-08-02 20:29:20 +00:00
|
|
|
#include "nsNetUtil.h"
|
2002-06-14 23:54:18 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
2002-10-09 07:03:15 +00:00
|
|
|
#include "nsIScriptLoader.h"
|
|
|
|
#include "nsIScriptLoaderObserver.h"
|
|
|
|
#include "nsIXMLContentSink.h"
|
|
|
|
#include "nsIContent.h"
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
static const char* kLoadAsData = "loadAsData";
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kIDOMDOMImplementationCID, NS_DOM_IMPLEMENTATION_CID);
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
2002-10-09 07:03:15 +00:00
|
|
|
static NS_DEFINE_CID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
|
|
|
|
|
|
|
|
// This is ugly, but nsXBLContentSink.h isn't exported
|
|
|
|
extern
|
|
|
|
nsresult
|
|
|
|
NS_NewXBLContentSink(nsIXMLContentSink** aResult,
|
|
|
|
nsIDocument* aDoc,
|
|
|
|
nsIURI* aURL,
|
|
|
|
nsIWebShell* aWebShell);
|
|
|
|
|
|
|
|
class nsSyncLoadService : public nsISyncLoadDOMService
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsSyncLoadService();
|
|
|
|
virtual ~nsSyncLoadService();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISYNCLOADDOMSERVICE
|
|
|
|
|
|
|
|
static
|
|
|
|
nsresult PushSyncStreamToListener(nsIInputStream* aIn,
|
|
|
|
nsIStreamListener* aListener,
|
|
|
|
nsIChannel* aChannel);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class manages loading a single XML document
|
|
|
|
*/
|
|
|
|
|
|
|
|
class nsSyncLoader : public nsIDOMLoadListener,
|
|
|
|
public nsIHttpEventSink,
|
|
|
|
public nsIInterfaceRequestor,
|
|
|
|
public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsSyncLoader();
|
|
|
|
virtual ~nsSyncLoader();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD LoadDocument(nsIChannel* aChannel, nsIURI *aLoaderURI,
|
|
|
|
PRBool aChannelIsSync,
|
|
|
|
nsIDOMDocument** _retval);
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent);
|
|
|
|
|
|
|
|
// nsIDOMLoadListener
|
|
|
|
NS_IMETHOD Load(nsIDOMEvent* aEvent);
|
|
|
|
NS_IMETHOD Unload(nsIDOMEvent* aEvent);
|
|
|
|
NS_IMETHOD Abort(nsIDOMEvent* aEvent);
|
|
|
|
NS_IMETHOD Error(nsIDOMEvent* aEvent);
|
|
|
|
|
|
|
|
NS_DECL_NSIHTTPEVENTSINK
|
|
|
|
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PushAsyncStream(nsIStreamListener* aListener);
|
|
|
|
PushSyncStream(nsIStreamListener* aListener);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
PRPackedBool mLoading;
|
|
|
|
PRPackedBool mLoadSuccess;
|
|
|
|
};
|
|
|
|
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This class exists to prevent a circular reference between
|
2002-10-09 07:03:15 +00:00
|
|
|
* the loaded document and the nsSyncLoader instance. The
|
2001-01-12 20:06:48 +00:00
|
|
|
* request owns the document. While the document is loading,
|
|
|
|
* the request is a load listener, held onto by the document.
|
|
|
|
* The proxy class breaks the circularity by filling in as the
|
|
|
|
* load listener and holding a weak reference to the request
|
|
|
|
* object.
|
|
|
|
*/
|
|
|
|
|
2001-06-08 07:14:19 +00:00
|
|
|
class txLoadListenerProxy : public nsIDOMLoadListener {
|
2001-01-12 20:06:48 +00:00
|
|
|
public:
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy(nsWeakPtr aParent);
|
|
|
|
virtual ~txLoadListenerProxy();
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent);
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
// nsIDOMLoadListener
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
NS_IMETHOD Load(nsIDOMEvent* aEvent);
|
|
|
|
NS_IMETHOD Unload(nsIDOMEvent* aEvent);
|
|
|
|
NS_IMETHOD Abort(nsIDOMEvent* aEvent);
|
|
|
|
NS_IMETHOD Error(nsIDOMEvent* aEvent);
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsWeakPtr mParent;
|
|
|
|
};
|
|
|
|
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy::txLoadListenerProxy(nsWeakPtr aParent)
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
|
|
|
mParent = aParent;
|
|
|
|
}
|
|
|
|
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy::~txLoadListenerProxy()
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-06-08 07:14:19 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(txLoadListenerProxy, nsIDOMLoadListener)
|
2001-01-12 20:06:48 +00:00
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
NS_IMETHODIMP
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy::HandleEvent(nsIDOMEvent* aEvent)
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMLoadListener> listener = do_QueryReferent(mParent);
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
return listener->HandleEvent(aEvent);
|
|
|
|
}
|
2001-08-02 20:29:20 +00:00
|
|
|
|
2001-01-12 20:06:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
NS_IMETHODIMP
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy::Load(nsIDOMEvent* aEvent)
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMLoadListener> listener = do_QueryReferent(mParent);
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
return listener->Load(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
NS_IMETHODIMP
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy::Unload(nsIDOMEvent* aEvent)
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMLoadListener> listener = do_QueryReferent(mParent);
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
return listener->Unload(aEvent);
|
|
|
|
}
|
2001-08-02 20:29:20 +00:00
|
|
|
|
2001-01-12 20:06:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
NS_IMETHODIMP
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy::Abort(nsIDOMEvent* aEvent)
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMLoadListener> listener = do_QueryReferent(mParent);
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
return listener->Abort(aEvent);
|
|
|
|
}
|
2001-08-02 20:29:20 +00:00
|
|
|
|
2001-01-12 20:06:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
NS_IMETHODIMP
|
2001-06-08 07:14:19 +00:00
|
|
|
txLoadListenerProxy::Error(nsIDOMEvent* aEvent)
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMLoadListener> listener = do_QueryReferent(mParent);
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
return listener->Error(aEvent);
|
|
|
|
}
|
2001-08-02 20:29:20 +00:00
|
|
|
|
2001-01-12 20:06:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSyncLoader::nsSyncLoader()
|
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSyncLoader::~nsSyncLoader()
|
|
|
|
{
|
2001-08-02 20:29:20 +00:00
|
|
|
if (mLoading && mChannel) {
|
|
|
|
mChannel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
}
|
2001-01-12 20:06:48 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
NS_IMPL_ISUPPORTS4(nsSyncLoader,
|
2002-06-14 23:54:18 +00:00
|
|
|
nsIDOMLoadListener,
|
|
|
|
nsIHttpEventSink,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsISupportsWeakReference)
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-10-09 07:03:15 +00:00
|
|
|
nsSyncLoader::LoadDocument(nsIChannel* aChannel,
|
|
|
|
nsIURI *aLoaderURI,
|
|
|
|
PRBool aChannelIsSync,
|
2002-06-14 23:54:18 +00:00
|
|
|
nsIDOMDocument **aResult)
|
2001-01-12 20:06:48 +00:00
|
|
|
{
|
2002-06-14 23:54:18 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
*aResult = nsnull;
|
|
|
|
nsresult rv = NS_OK;
|
2001-01-12 20:06:48 +00:00
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
mChannel = aChannel;
|
2001-01-12 20:06:48 +00:00
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
if (aLoaderURI) {
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> securityManager =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-06-14 23:54:18 +00:00
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
nsCOMPtr<nsIURI> docURI;
|
|
|
|
rv = aChannel->GetURI(getter_AddRefs(docURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-12 20:06:48 +00:00
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
rv = securityManager->CheckLoadURI(aLoaderURI, docURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-08-02 20:29:20 +00:00
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
rv = securityManager->CheckSameOriginURI(aLoaderURI, docURI);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-08-02 20:29:20 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
// Get the loadgroup of the channel
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = aChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
2001-08-02 20:29:20 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
// Create document
|
|
|
|
nsCOMPtr<nsIDocument> document = do_CreateInstance(kXMLDocumentCID, &rv);
|
2002-03-19 14:14:27 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
// Register as a load listener on the document
|
2002-10-09 07:03:15 +00:00
|
|
|
nsCOMPtr<nsIDOMEventReceiver> target = do_QueryInterface(document);
|
2002-03-19 14:14:27 +00:00
|
|
|
NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsWeakPtr requestWeak = getter_AddRefs(NS_GetWeakReference(NS_STATIC_CAST(nsIDOMLoadListener*, this)));
|
|
|
|
txLoadListenerProxy* proxy = new txLoadListenerProxy(requestWeak);
|
2002-10-09 07:03:15 +00:00
|
|
|
NS_ENSURE_TRUE(proxy, NS_ERROR_OUT_OF_MEMORY);
|
2002-03-19 14:14:27 +00:00
|
|
|
|
|
|
|
// This will addref the proxy
|
|
|
|
rv = target->AddEventListenerByIID(NS_STATIC_CAST(nsIDOMEventListener*,
|
|
|
|
proxy),
|
|
|
|
NS_GET_IID(nsIDOMLoadListener));
|
2002-10-09 07:03:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-08-02 20:29:20 +00:00
|
|
|
|
|
|
|
mLoadSuccess = PR_FALSE;
|
2001-01-12 20:06:48 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
2001-08-02 20:29:20 +00:00
|
|
|
rv = document->StartDocumentLoad(kLoadAsData, mChannel,
|
|
|
|
loadGroup, nsnull,
|
|
|
|
getter_AddRefs(listener),
|
|
|
|
PR_FALSE);
|
2001-01-12 20:06:48 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2001-08-02 20:29:20 +00:00
|
|
|
target->RemoveEventListenerByIID(NS_STATIC_CAST(nsIDOMEventListener*,
|
|
|
|
proxy),
|
|
|
|
NS_GET_IID(nsIDOMLoadListener));
|
|
|
|
return rv;
|
2001-01-12 20:06:48 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
if (aChannelIsSync) {
|
|
|
|
rv = PushSyncStream(listener);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = PushAsyncStream(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
mChannel = nsnull;
|
|
|
|
|
|
|
|
// This will release the proxy. Don't use the errorvalue from this since
|
|
|
|
// we're more interested in the errorvalue from the loading
|
|
|
|
target->RemoveEventListenerByIID(NS_STATIC_CAST(nsIDOMEventListener*,
|
|
|
|
proxy),
|
|
|
|
NS_GET_IID(nsIDOMLoadListener));
|
|
|
|
|
|
|
|
// check that the load succeeded
|
2002-06-14 23:54:18 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-12 20:06:48 +00:00
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
NS_ENSURE_TRUE(mLoadSuccess, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> rootContent;
|
|
|
|
document->GetRootContent(getter_AddRefs(rootContent));
|
|
|
|
NS_ENSURE_TRUE(rootContent, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return CallQueryInterface(document, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSyncLoader::PushAsyncStream(nsIStreamListener* aListener)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Set up a new eventqueue
|
|
|
|
nsCOMPtr<nsIEventQueueService> service =
|
|
|
|
do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventQueue> currentThreadQ;
|
|
|
|
rv = service->PushThreadEventQueue(getter_AddRefs(currentThreadQ));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Hook us up to listen to redirects and the like
|
|
|
|
mChannel->SetNotificationCallbacks(this);
|
|
|
|
|
|
|
|
// Start reading from the channel
|
|
|
|
rv = mChannel->AsyncOpen(aListener, nsnull);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mLoading = PR_TRUE;
|
|
|
|
|
|
|
|
// process events until we're finished.
|
|
|
|
PLEvent *event;
|
|
|
|
while (mLoading && NS_SUCCEEDED(rv)) {
|
|
|
|
rv = currentThreadQ->WaitForEvent(&event);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), ": currentThreadQ->WaitForEvent failed...\n");
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = currentThreadQ->HandleEvent(event);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), ": currentThreadQ->HandleEvent failed...\n");
|
|
|
|
}
|
|
|
|
}
|
2001-01-12 20:06:48 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 07:03:15 +00:00
|
|
|
service->PopThreadEventQueue(currentThreadQ);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSyncLoader::PushSyncStream(nsIStreamListener* aListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
nsresult rv = mChannel->Open(getter_AddRefs(in));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = nsSyncLoadService::PushSyncStreamToListener(in, aListener, mChannel);
|
2001-08-02 20:29:20 +00:00
|
|
|
|
2001-01-12 20:06:48 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDOMLoadListener
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::Load(nsIDOMEvent* aEvent)
|
|
|
|
{
|
2001-08-02 20:29:20 +00:00
|
|
|
if (mLoading) {
|
|
|
|
mLoading = PR_FALSE;
|
|
|
|
mLoadSuccess = PR_TRUE;
|
2001-01-12 20:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::Unload(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::Abort(nsIDOMEvent* aEvent)
|
|
|
|
{
|
2001-08-02 20:29:20 +00:00
|
|
|
if (mLoading) {
|
|
|
|
mLoading = PR_FALSE;
|
2001-01-12 20:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::Error(nsIDOMEvent* aEvent)
|
|
|
|
{
|
2001-08-02 20:29:20 +00:00
|
|
|
if (mLoading) {
|
|
|
|
mLoading = PR_FALSE;
|
2001-01-12 20:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-06-14 23:54:18 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSyncLoader::OnRedirect(nsIHttpChannel *aHttpChannel,
|
|
|
|
nsIChannel *aNewChannel)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aNewChannel);
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> oldURI;
|
|
|
|
rv = aHttpChannel->GetURI(getter_AddRefs(oldURI)); // The original URI
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
rv = aNewChannel->GetURI(getter_AddRefs(newURI)); // The new URI
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = secMan->CheckSameOriginURI(oldURI, newURI);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mChannel = aNewChannel;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSyncLoader::GetInterface(const nsIID & aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
2002-10-09 07:03:15 +00:00
|
|
|
|
|
|
|
nsSyncLoadService::nsSyncLoadService()
|
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSyncLoadService::~nsSyncLoadService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsSyncLoadService,
|
|
|
|
nsISyncLoadDOMService)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSyncLoadService::LoadDocument(nsIChannel* aChannel, nsIURI* aLoaderURI,
|
|
|
|
nsIDOMDocument** _retval)
|
|
|
|
{
|
|
|
|
nsSyncLoader* loader = new nsSyncLoader();
|
|
|
|
NS_ENSURE_TRUE(loader, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ADDREF(loader);
|
|
|
|
nsresult rv = loader->LoadDocument(aChannel, aLoaderURI, PR_FALSE,
|
|
|
|
_retval);
|
|
|
|
NS_RELEASE(loader);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoadService::LoadLocalDocument(nsIChannel* aChannel, nsIURI* aLoaderURI,
|
|
|
|
nsIDOMDocument** _retval)
|
|
|
|
{
|
|
|
|
nsSyncLoader* loader = new nsSyncLoader();
|
|
|
|
NS_ENSURE_TRUE(loader, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ADDREF(loader);
|
|
|
|
nsresult rv = loader->LoadDocument(aChannel, aLoaderURI, PR_TRUE, _retval);
|
|
|
|
NS_RELEASE(loader);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoadService::LoadLocalXBLDocument(nsIChannel* aChannel,
|
|
|
|
nsIDOMDocument** _retval)
|
|
|
|
{
|
|
|
|
*_retval = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
nsresult rv = aChannel->Open(getter_AddRefs(in));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get uri and loadgroup
|
|
|
|
nsCOMPtr<nsIURI> docURI;
|
|
|
|
rv = aChannel->GetURI(getter_AddRefs(docURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = aChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Create document and contentsink and set them up.
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_CreateInstance(kXMLDocumentCID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Do this after making sure the |channel->Open| succeeded (which it
|
|
|
|
// won't if the file doesn't exist) so that we don't have to go
|
|
|
|
// through the work of breaking the circular references between
|
|
|
|
// content sink, script loader, and document.
|
|
|
|
nsCOMPtr<nsIXMLContentSink> xblSink;
|
|
|
|
rv = NS_NewXBLContentSink(getter_AddRefs(xblSink), doc, docURI, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
rv = doc->StartDocumentLoad(kLoadAsData,
|
|
|
|
aChannel,
|
|
|
|
loadGroup,
|
|
|
|
nsnull,
|
|
|
|
getter_AddRefs(listener),
|
|
|
|
PR_TRUE,
|
|
|
|
xblSink);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = PushSyncStreamToListener(in, listener, aChannel);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return CallQueryInterface(doc, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsSyncLoadService::PushSyncStreamToListener(nsIInputStream* aIn,
|
|
|
|
nsIStreamListener* aListener,
|
|
|
|
nsIChannel* aChannel)
|
|
|
|
{
|
|
|
|
// Set up buffering stream
|
|
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
|
|
|
nsresult rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
|
|
|
|
aIn, 4096);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Load
|
|
|
|
aListener->OnStartRequest(aChannel, nsnull);
|
|
|
|
PRUint32 sourceOffset = 0;
|
|
|
|
while (1) {
|
|
|
|
PRUint32 readCount = 0;
|
|
|
|
rv = bufferedStream->Available(&readCount);
|
|
|
|
if (NS_FAILED(rv) || !readCount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = aListener->OnDataAvailable(aChannel, nsnull, bufferedStream,
|
|
|
|
sourceOffset, readCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceOffset += readCount;
|
|
|
|
}
|
|
|
|
aListener->OnStopRequest(aChannel, nsnull, rv);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewSyncLoadDOMService(nsISyncLoadDOMService** aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsSyncLoadService();
|
|
|
|
NS_ENSURE_TRUE(*aResult, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|