2001-05-17 05:20:51 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2004-05-13 18:34:18 +00:00
|
|
|
// vim: ft=cpp tw=78 sw=2 et ts=2
|
2004-04-17 21:52:36 +00:00
|
|
|
/* ***** 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.
|
|
|
|
*
|
2001-05-17 05:20:51 +00:00
|
|
|
* The Original Code is Mozilla.
|
2004-04-17 21:52:36 +00:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2001-05-17 05:20:51 +00:00
|
|
|
* Vidur Apparao <vidur@netscape.com> (original author)
|
2004-04-17 21:52:36 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of 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 ***** */
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsParserUtils.h"
|
2005-01-05 07:25:29 +00:00
|
|
|
#include "nsIMIMEHeaderParam.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsHTMLAtoms.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsIScriptContext.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsINodeInfo.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
2001-05-21 22:40:10 +00:00
|
|
|
#include "nsIDOMWindow.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIScriptElement.h"
|
2004-07-06 22:52:03 +00:00
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
2001-05-17 05:20:51 +00:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "jsapi.h"
|
2003-03-04 12:12:48 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
2003-07-11 22:47:17 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2003-02-28 05:47:36 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2003-10-21 22:11:49 +00:00
|
|
|
static already_AddRefed<nsIPrincipal>
|
|
|
|
IntersectPrincipalCerts(nsIPrincipal *aOld, nsIPrincipal *aNew)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aOld, "Null old principal!");
|
|
|
|
NS_PRECONDITION(aNew, "Null new principal!");
|
|
|
|
|
|
|
|
nsIPrincipal *principal = aOld;
|
|
|
|
|
|
|
|
PRBool hasCert;
|
|
|
|
aOld->GetHasCertificate(&hasCert);
|
|
|
|
if (hasCert) {
|
|
|
|
PRBool equal;
|
|
|
|
aOld->Equals(aNew, &equal);
|
|
|
|
if (!equal) {
|
|
|
|
nsCOMPtr<nsIURI> uri, domain;
|
|
|
|
aOld->GetURI(getter_AddRefs(uri));
|
|
|
|
aOld->GetDomain(getter_AddRefs(domain));
|
|
|
|
|
|
|
|
nsContentUtils::GetSecurityManager()->GetCodebasePrincipal(uri, &principal);
|
|
|
|
if (principal && domain) {
|
|
|
|
principal->SetDomain(domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(principal);
|
|
|
|
|
|
|
|
return principal;
|
|
|
|
}
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// Per-request data structure
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsScriptLoadRequest : public nsISupports {
|
|
|
|
public:
|
2004-07-06 22:52:03 +00:00
|
|
|
nsScriptLoadRequest(nsIScriptElement* aElement,
|
2001-05-17 05:20:51 +00:00
|
|
|
nsIScriptLoaderObserver* aObserver,
|
2005-01-05 07:25:29 +00:00
|
|
|
const char* aVersionString,
|
|
|
|
PRBool aHasE4XOption);
|
2001-05-17 05:54:16 +00:00
|
|
|
virtual ~nsScriptLoadRequest();
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
void FireScriptAvailable(nsresult aResult,
|
|
|
|
const nsAFlatString& aScript);
|
|
|
|
void FireScriptEvaluated(nsresult aResult);
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
nsCOMPtr<nsIScriptElement> mElement;
|
2001-05-17 05:20:51 +00:00
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> mObserver;
|
2002-10-11 00:38:29 +00:00
|
|
|
PRPackedBool mLoading; // Are we still waiting for a load to complete?
|
|
|
|
PRPackedBool mWasPending; // Processed immediately or pending
|
|
|
|
PRPackedBool mIsInline; // Is the script inline or loaded?
|
2005-01-05 07:25:29 +00:00
|
|
|
PRPackedBool mHasE4XOption; // Has E4X=1 script type parameter
|
2004-02-19 02:44:03 +00:00
|
|
|
nsString mScriptText; // Holds script for loaded scripts
|
|
|
|
const char* mJSVersion; // We don't own this string
|
2001-05-17 05:20:51 +00:00
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
PRInt32 mLineNo;
|
|
|
|
};
|
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
nsScriptLoadRequest::nsScriptLoadRequest(nsIScriptElement* aElement,
|
2001-05-17 05:20:51 +00:00
|
|
|
nsIScriptLoaderObserver* aObserver,
|
2005-01-05 07:25:29 +00:00
|
|
|
const char* aVersionString,
|
|
|
|
PRBool aHasE4XOption) :
|
|
|
|
mElement(aElement), mObserver(aObserver),
|
2001-05-17 05:54:16 +00:00
|
|
|
mLoading(PR_TRUE), mWasPending(PR_FALSE),
|
2005-01-05 07:25:29 +00:00
|
|
|
mIsInline(PR_TRUE), mHasE4XOption(aHasE4XOption),
|
|
|
|
mJSVersion(aVersionString), mLineNo(1)
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptLoadRequest::~nsScriptLoadRequest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-01-08 06:32:36 +00:00
|
|
|
|
|
|
|
// The nsScriptLoadRequest is passed as the context to necko, and thus
|
|
|
|
// it needs to be threadsafe. Necko won't do anything with this
|
|
|
|
// context, but it will AddRef and Release it on other threads.
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsScriptLoadRequest)
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoadRequest::FireScriptAvailable(nsresult aResult,
|
|
|
|
const nsAFlatString& aScript)
|
|
|
|
{
|
|
|
|
if (mObserver) {
|
|
|
|
mObserver->ScriptAvailable(aResult, mElement, mIsInline, mWasPending,
|
|
|
|
mURI, mLineNo,
|
|
|
|
aScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoadRequest::FireScriptEvaluated(nsresult aResult)
|
|
|
|
{
|
|
|
|
if (mObserver) {
|
|
|
|
mObserver->ScriptEvaluated(aResult, mElement, mIsInline, mWasPending);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
nsScriptLoader::nsScriptLoader()
|
2002-10-11 00:38:29 +00:00
|
|
|
: mDocument(nsnull), mEnabled(PR_TRUE)
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptLoader::~nsScriptLoader()
|
|
|
|
{
|
|
|
|
mObservers.Clear();
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2003-07-11 22:47:17 +00:00
|
|
|
PRInt32 count = mPendingRequests.Count();
|
|
|
|
for (PRInt32 i = 0; i < count; i++) {
|
|
|
|
nsScriptLoadRequest* req = mPendingRequests[i];
|
|
|
|
if (req) {
|
2004-01-29 22:04:45 +00:00
|
|
|
req->FireScriptAvailable(NS_ERROR_ABORT, EmptyString());
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mPendingRequests.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsScriptLoader)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIScriptLoader)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIScriptLoader)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamLoaderObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsScriptLoader)
|
|
|
|
NS_IMPL_RELEASE(nsScriptLoader)
|
|
|
|
|
|
|
|
/* void init (in nsIDocument aDocument); */
|
2005-01-05 07:25:29 +00:00
|
|
|
NS_IMETHODIMP
|
2001-05-17 05:20:51 +00:00
|
|
|
nsScriptLoader::Init(nsIDocument *aDocument)
|
|
|
|
{
|
|
|
|
mDocument = aDocument;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void dropDocumentReference (); */
|
2005-01-05 07:25:29 +00:00
|
|
|
NS_IMETHODIMP
|
2001-05-17 05:20:51 +00:00
|
|
|
nsScriptLoader::DropDocumentReference()
|
|
|
|
{
|
|
|
|
mDocument = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void addObserver (in nsIScriptLoaderObserver aObserver); */
|
2005-01-05 07:25:29 +00:00
|
|
|
NS_IMETHODIMP
|
2001-05-17 05:20:51 +00:00
|
|
|
nsScriptLoader::AddObserver(nsIScriptLoaderObserver *aObserver)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aObserver);
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2003-07-11 22:47:17 +00:00
|
|
|
mObservers.AppendObject(aObserver);
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void removeObserver (in nsIScriptLoaderObserver aObserver); */
|
2005-01-05 07:25:29 +00:00
|
|
|
NS_IMETHODIMP
|
2001-05-17 05:20:51 +00:00
|
|
|
nsScriptLoader::RemoveObserver(nsIScriptLoaderObserver *aObserver)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aObserver);
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2003-07-11 22:47:17 +00:00
|
|
|
mObservers.RemoveObject(aObserver);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2004-07-06 22:52:03 +00:00
|
|
|
nsScriptLoader::InNonScriptingContainer(nsIScriptElement* aScriptElement)
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(aScriptElement));
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
|
|
|
|
node->GetParentNode(getter_AddRefs(parent));
|
2005-01-05 07:25:29 +00:00
|
|
|
while (parent) {
|
2001-05-17 05:20:51 +00:00
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(parent));
|
|
|
|
if (!content) {
|
|
|
|
break;
|
|
|
|
}
|
2003-09-27 04:18:26 +00:00
|
|
|
|
|
|
|
nsINodeInfo *nodeInfo = content->GetNodeInfo();
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_ASSERTION(nodeInfo, "element without node info");
|
|
|
|
|
|
|
|
if (nodeInfo) {
|
2003-11-15 05:27:37 +00:00
|
|
|
nsIAtom *localName = nodeInfo->NameAtom();
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
// XXX noframes and noembed are currently unconditionally not
|
|
|
|
// displayed and processed. This might change if we support either
|
|
|
|
// prefs or per-document container settings for not allowing
|
|
|
|
// frames or plugins.
|
|
|
|
if (content->IsContentOfType(nsIContent::eHTML) &&
|
2003-11-15 05:27:37 +00:00
|
|
|
((localName == nsHTMLAtoms::iframe) ||
|
|
|
|
(localName == nsHTMLAtoms::noframes) ||
|
|
|
|
(localName == nsHTMLAtoms::noembed))) {
|
2001-05-17 05:20:51 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = parent;
|
|
|
|
node->GetParentNode(getter_AddRefs(parent));
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-03-04 12:12:48 +00:00
|
|
|
// Helper method for checking if the script element is an event-handler
|
|
|
|
// This means that it has both a for-attribute and a event-attribute.
|
|
|
|
// Also, if the for-attribute has a value that matches "\s*window\s*",
|
|
|
|
// and the event-attribute matches "\s*onload([ \(].*)?" then it isn't an
|
|
|
|
// eventhandler. (both matches are case insensitive).
|
|
|
|
// This is how IE seems to filter out a window's onload handler from a
|
|
|
|
// <script for=... event=...> element.
|
|
|
|
|
|
|
|
PRBool
|
2004-07-06 22:52:03 +00:00
|
|
|
nsScriptLoader::IsScriptEventHandler(nsIScriptElement *aScriptElement)
|
2003-03-04 12:12:48 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> contElement = do_QueryInterface(aScriptElement);
|
|
|
|
if (!contElement ||
|
|
|
|
!contElement->HasAttr(kNameSpaceID_None, nsHTMLAtoms::_event) ||
|
|
|
|
!contElement->HasAttr(kNameSpaceID_None, nsHTMLAtoms::_for)) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString str;
|
|
|
|
nsresult rv = contElement->GetAttr(kNameSpaceID_None, nsHTMLAtoms::_for,
|
|
|
|
str);
|
|
|
|
NS_ENSURE_SUCCESS(rv, PR_FALSE);
|
|
|
|
|
|
|
|
const nsAString& for_str = nsContentUtils::TrimWhitespace(str);
|
|
|
|
|
2004-06-17 00:13:25 +00:00
|
|
|
if (!for_str.LowerCaseEqualsLiteral("window")) {
|
2003-03-04 12:12:48 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found for="window", now check for event="onload".
|
|
|
|
|
|
|
|
rv = contElement->GetAttr(kNameSpaceID_None, nsHTMLAtoms::_event, str);
|
|
|
|
NS_ENSURE_SUCCESS(rv, PR_FALSE);
|
|
|
|
|
|
|
|
const nsAString& event_str = nsContentUtils::TrimWhitespace(str, PR_FALSE);
|
|
|
|
|
|
|
|
if (event_str.Length() < 6) {
|
|
|
|
// String too short, can't be "onload".
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2003-07-14 07:37:39 +00:00
|
|
|
if (!StringBeginsWith(event_str, NS_LITERAL_STRING("onload"),
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
2003-03-04 12:12:48 +00:00
|
|
|
// It ain't "onload.*".
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString::const_iterator start, end;
|
|
|
|
event_str.BeginReading(start);
|
|
|
|
event_str.EndReading(end);
|
|
|
|
|
|
|
|
start.advance(6); // advance past "onload"
|
|
|
|
|
|
|
|
if (start != end && *start != '(' && *start != ' ') {
|
|
|
|
// We got onload followed by something other than space or
|
|
|
|
// '('. Not good enough.
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
/* void processScriptElement (in nsIScriptElement aElement, in nsIScriptLoaderObserver aObserver); */
|
2005-01-05 07:25:29 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement,
|
2001-05-17 05:20:51 +00:00
|
|
|
nsIScriptLoaderObserver *aObserver)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aElement);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// We need a document to evaluate scripts.
|
|
|
|
if (!mDocument) {
|
|
|
|
return FireErrorNotification(NS_ERROR_FAILURE, aElement, aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see that the element is not in a container that
|
|
|
|
// suppresses script evaluation within it.
|
2002-10-11 00:38:29 +00:00
|
|
|
if (!mEnabled || InNonScriptingContainer(aElement)) {
|
2001-05-17 05:20:51 +00:00
|
|
|
return FireErrorNotification(NS_ERROR_NOT_AVAILABLE, aElement, aObserver);
|
|
|
|
}
|
|
|
|
|
2003-03-04 12:12:48 +00:00
|
|
|
// Check that the script is not an eventhandler
|
|
|
|
if (IsScriptEventHandler(aElement)) {
|
|
|
|
return FireErrorNotification(NS_CONTENT_SCRIPT_IS_EVENTHANDLER, aElement,
|
|
|
|
aObserver);
|
|
|
|
}
|
|
|
|
|
2003-08-11 18:13:16 +00:00
|
|
|
// Check whether we should be executing scripts at all for this document
|
2003-07-31 01:28:16 +00:00
|
|
|
if (!mDocument->IsScriptEnabled()) {
|
2001-05-17 05:20:51 +00:00
|
|
|
return FireErrorNotification(NS_ERROR_NOT_AVAILABLE, aElement, aObserver);
|
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2003-08-11 18:13:16 +00:00
|
|
|
// Script evaluation can also be disabled in the current script
|
|
|
|
// context even though it's enabled in the document.
|
2003-10-22 06:09:48 +00:00
|
|
|
nsIScriptGlobalObject *globalObject = mDocument->GetScriptGlobalObject();
|
2003-08-11 18:13:16 +00:00
|
|
|
if (globalObject)
|
|
|
|
{
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIScriptContext *context = globalObject->GetContext();
|
|
|
|
|
|
|
|
// If scripts aren't enabled in the current context, there's no
|
|
|
|
// point in going on.
|
|
|
|
if (context && !context->GetScriptsEnabled()) {
|
|
|
|
return FireErrorNotification(NS_ERROR_NOT_AVAILABLE, aElement,
|
|
|
|
aObserver);
|
2003-08-11 18:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
PRBool isJavaScript = PR_TRUE;
|
2005-01-05 07:25:29 +00:00
|
|
|
PRBool hasE4XOption = PR_FALSE;
|
2001-05-17 05:20:51 +00:00
|
|
|
const char* jsVersionString = nsnull;
|
2002-05-06 22:59:42 +00:00
|
|
|
nsAutoString language, type, src;
|
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
// "language" is a deprecated attribute of HTML, so we check it only for
|
|
|
|
// HTML script elements.
|
|
|
|
nsCOMPtr<nsIDOMHTMLScriptElement> htmlScriptElement =
|
|
|
|
do_QueryInterface(aElement);
|
2004-07-06 22:52:03 +00:00
|
|
|
if (htmlScriptElement) {
|
|
|
|
// Check the language attribute first, so type can trump language.
|
|
|
|
htmlScriptElement->GetAttribute(NS_LITERAL_STRING("language"), language);
|
|
|
|
if (!language.IsEmpty()) {
|
2005-01-05 07:25:29 +00:00
|
|
|
isJavaScript = nsParserUtils::IsJavaScriptLanguage(language,
|
|
|
|
&jsVersionString);
|
2005-02-20 03:35:21 +00:00
|
|
|
|
|
|
|
// IE, Opera, etc. do not respect language version, so neither should
|
|
|
|
// we at this late date in the browser wars saga. Note that this change
|
|
|
|
// affects HTML but not XUL or SVG (but note also that XUL has its own
|
|
|
|
// code to check nsParserUtils::IsJavaScriptLanguage -- that's probably
|
|
|
|
// a separate bug, one we may not be able to fix short of XUL2). See
|
|
|
|
// bug 255895 (https://bugzilla.mozilla.org/show_bug.cgi?id=255895).
|
|
|
|
jsVersionString = ::JS_VersionToString(JSVERSION_DEFAULT);
|
2004-07-06 22:52:03 +00:00
|
|
|
}
|
2002-05-06 22:59:42 +00:00
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
// Check the type attribute to determine language and version.
|
2004-07-06 22:52:03 +00:00
|
|
|
aElement->GetScriptType(type);
|
2001-05-17 05:20:51 +00:00
|
|
|
if (!type.IsEmpty()) {
|
2005-01-05 07:25:29 +00:00
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimeHdrParser =
|
|
|
|
do_GetService("@mozilla.org/network/mime-hdrparam;1");
|
|
|
|
NS_ENSURE_TRUE(mimeHdrParser, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 typeAndParams(type);
|
|
|
|
|
|
|
|
nsAutoString mimeType;
|
|
|
|
rv = mimeHdrParser->GetParameter(typeAndParams, nsnull,
|
|
|
|
EmptyCString(), PR_FALSE, nsnull,
|
|
|
|
mimeType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
isJavaScript = mimeType.LowerCaseEqualsLiteral("text/javascript") ||
|
|
|
|
mimeType.LowerCaseEqualsLiteral("application/x-javascript");
|
2001-05-17 05:20:51 +00:00
|
|
|
if (isJavaScript) {
|
|
|
|
JSVersion jsVersion = JSVERSION_DEFAULT;
|
2005-01-05 07:25:29 +00:00
|
|
|
nsAutoString value;
|
|
|
|
rv = mimeHdrParser->GetParameter(typeAndParams, "version",
|
|
|
|
EmptyCString(), PR_FALSE, nsnull,
|
|
|
|
value);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv != NS_ERROR_INVALID_ARG)
|
|
|
|
return rv;
|
|
|
|
} else {
|
|
|
|
if (value.Length() != 3 || value[0] != '1' || value[1] != '.')
|
2001-05-17 05:20:51 +00:00
|
|
|
jsVersion = JSVERSION_UNKNOWN;
|
2005-01-05 07:25:29 +00:00
|
|
|
else switch (value[2]) {
|
|
|
|
case '0': jsVersion = JSVERSION_1_0; break;
|
|
|
|
case '1': jsVersion = JSVERSION_1_1; break;
|
|
|
|
case '2': jsVersion = JSVERSION_1_2; break;
|
|
|
|
case '3': jsVersion = JSVERSION_1_3; break;
|
|
|
|
case '4': jsVersion = JSVERSION_1_4; break;
|
|
|
|
case '5': jsVersion = JSVERSION_1_5; break;
|
|
|
|
default: jsVersion = JSVERSION_UNKNOWN;
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
jsVersionString = ::JS_VersionToString(jsVersion);
|
|
|
|
|
|
|
|
rv = mimeHdrParser->GetParameter(typeAndParams, "e4x",
|
|
|
|
EmptyCString(), PR_FALSE, nsnull,
|
|
|
|
value);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv != NS_ERROR_INVALID_ARG)
|
|
|
|
return rv;
|
|
|
|
} else {
|
|
|
|
if (value.Length() == 1 && value[0] == '1')
|
|
|
|
hasE4XOption = PR_TRUE;
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't JavaScript, we don't know how to evaluate.
|
2005-01-05 07:25:29 +00:00
|
|
|
// XXX How and where should we deal with other scripting languages?
|
|
|
|
// See bug 255942 (https://bugzilla.mozilla.org/show_bug.cgi?id=255942).
|
2001-05-17 05:20:51 +00:00
|
|
|
if (!isJavaScript) {
|
|
|
|
return FireErrorNotification(NS_ERROR_NOT_AVAILABLE, aElement, aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a request object for this script
|
2005-01-05 07:25:29 +00:00
|
|
|
nsRefPtr<nsScriptLoadRequest> request = new nsScriptLoadRequest(aElement, aObserver, jsVersionString, hasE4XOption);
|
2001-05-17 05:20:51 +00:00
|
|
|
if (!request) {
|
|
|
|
return FireErrorNotification(NS_ERROR_OUT_OF_MEMORY, aElement, aObserver);
|
|
|
|
}
|
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
// First check to see if this is an external script
|
|
|
|
nsCOMPtr<nsIURI> scriptURI = aElement->GetScriptURI();
|
|
|
|
if (scriptURI) {
|
2001-05-17 05:20:51 +00:00
|
|
|
// Check that the containing page is allowed to load this URI.
|
2004-04-25 16:55:27 +00:00
|
|
|
nsIPrincipal *docPrincipal = mDocument->GetPrincipal();
|
|
|
|
if (!docPrincipal) {
|
2002-11-05 03:45:28 +00:00
|
|
|
return FireErrorNotification(NS_ERROR_UNEXPECTED, aElement, aObserver);
|
|
|
|
}
|
2003-10-21 22:11:49 +00:00
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
2004-04-25 16:55:27 +00:00
|
|
|
CheckLoadURIWithPrincipal(docPrincipal, scriptURI,
|
|
|
|
nsIScriptSecurityManager::ALLOW_CHROME);
|
2003-10-21 22:11:49 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return FireErrorNotification(rv, aElement, aObserver);
|
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
// After the security manager, the content-policy stuff gets a veto
|
2001-05-21 22:40:10 +00:00
|
|
|
if (globalObject) {
|
2004-05-13 18:34:18 +00:00
|
|
|
PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
|
|
|
|
nsIURI *docURI = mDocument->GetDocumentURI();
|
|
|
|
rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_SCRIPT,
|
|
|
|
scriptURI,
|
|
|
|
docURI,
|
2004-07-27 17:15:53 +00:00
|
|
|
aElement,
|
2004-05-13 18:34:18 +00:00
|
|
|
NS_LossyConvertUCS2toASCII(type),
|
|
|
|
nsnull, //extra
|
|
|
|
&shouldLoad);
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
if (NS_FAILED(rv) || shouldLoad != nsIContentPolicy::REJECT_TYPE) {
|
|
|
|
return FireErrorNotification(NS_ERROR_CONTENT_BLOCKED, aElement,
|
|
|
|
aObserver);
|
|
|
|
}
|
|
|
|
return FireErrorNotification(NS_ERROR_CONTENT_BLOCKED_SHOW_ALT,
|
|
|
|
aElement, aObserver);
|
2001-05-21 22:40:10 +00:00
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2001-08-10 01:46:41 +00:00
|
|
|
request->mURI = scriptURI;
|
|
|
|
request->mIsInline = PR_FALSE;
|
|
|
|
request->mWasPending = PR_TRUE;
|
|
|
|
request->mLoading = PR_TRUE;
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2001-08-10 01:46:41 +00:00
|
|
|
// Add the request to our pending requests list
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.AppendObject(request);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = mDocument->GetDocumentLoadGroup();
|
2001-08-10 01:46:41 +00:00
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
nsIDocShell *docshell = globalObject->GetDocShell();
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2001-08-10 01:46:41 +00:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> prompter(do_QueryInterface(docshell));
|
|
|
|
|
2002-01-08 06:32:36 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2002-09-27 04:22:13 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
scriptURI, nsnull, loadGroup,
|
|
|
|
prompter, nsIRequest::LOAD_NORMAL);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel) {
|
|
|
|
// HTTP content negotation has little value in this context.
|
|
|
|
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
2002-11-22 07:40:16 +00:00
|
|
|
NS_LITERAL_CSTRING("*/*"),
|
|
|
|
PR_FALSE);
|
2004-01-09 23:54:21 +00:00
|
|
|
httpChannel->SetReferrer(mDocument->GetDocumentURI());
|
2002-09-27 04:22:13 +00:00
|
|
|
}
|
2003-07-11 22:47:17 +00:00
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), channel, this, request);
|
2002-09-27 04:22:13 +00:00
|
|
|
}
|
2001-08-10 01:46:41 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObject(request);
|
2001-08-10 01:46:41 +00:00
|
|
|
return FireErrorNotification(rv, aElement, aObserver);
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
2001-08-10 01:46:41 +00:00
|
|
|
} else {
|
2001-05-17 05:20:51 +00:00
|
|
|
request->mLoading = PR_FALSE;
|
|
|
|
request->mIsInline = PR_TRUE;
|
2004-01-09 23:54:21 +00:00
|
|
|
request->mURI = mDocument->GetDocumentURI();
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2004-07-06 22:52:03 +00:00
|
|
|
request->mLineNo = aElement->GetScriptLineNumber();
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
// If we've got existing pending requests, add ourselves
|
|
|
|
// to this list.
|
2003-07-11 22:47:17 +00:00
|
|
|
if (mPendingRequests.Count() > 0) {
|
2001-05-17 05:20:51 +00:00
|
|
|
request->mWasPending = PR_TRUE;
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.AppendObject(request);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
request->mWasPending = PR_FALSE;
|
|
|
|
rv = ProcessRequest(request);
|
|
|
|
}
|
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2005-06-07 19:21:05 +00:00
|
|
|
nsresult
|
|
|
|
nsScriptLoader::GetCurrentScript(nsIScriptElement **aElement)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aElement);
|
|
|
|
*aElement = mCurrentScript;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aElement);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
nsresult
|
|
|
|
nsScriptLoader::FireErrorNotification(nsresult aResult,
|
2004-07-06 22:52:03 +00:00
|
|
|
nsIScriptElement* aElement,
|
2001-05-17 05:20:51 +00:00
|
|
|
nsIScriptLoaderObserver* aObserver)
|
|
|
|
{
|
2003-07-11 22:47:17 +00:00
|
|
|
PRInt32 count = mObservers.Count();
|
|
|
|
for (PRInt32 i = 0; i < count; i++) {
|
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> observer = mObservers[i];
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
if (observer) {
|
2005-01-05 07:25:29 +00:00
|
|
|
observer->ScriptAvailable(aResult, aElement,
|
2001-05-17 05:20:51 +00:00
|
|
|
PR_TRUE, PR_FALSE,
|
|
|
|
nsnull, 0,
|
2004-01-29 22:04:45 +00:00
|
|
|
EmptyString());
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aObserver) {
|
2005-01-05 07:25:29 +00:00
|
|
|
aObserver->ScriptAvailable(aResult, aElement,
|
2001-05-17 05:20:51 +00:00
|
|
|
PR_TRUE, PR_FALSE,
|
|
|
|
nsnull, 0,
|
2004-01-29 22:04:45 +00:00
|
|
|
EmptyString());
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return aResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::ProcessRequest(nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aRequest);
|
|
|
|
nsAFlatString* script;
|
|
|
|
nsAutoString textData;
|
|
|
|
|
|
|
|
// If there's no script text, we try to get it from the element
|
|
|
|
if (aRequest->mIsInline) {
|
|
|
|
// XXX This is inefficient - GetText makes multiple
|
|
|
|
// copies.
|
2004-07-06 22:52:03 +00:00
|
|
|
aRequest->mElement->GetScriptText(textData);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
script = &textData;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
script = &aRequest->mScriptText;
|
|
|
|
}
|
|
|
|
|
|
|
|
FireScriptAvailable(NS_OK, aRequest, *script);
|
|
|
|
nsresult rv = EvaluateScript(aRequest, *script);
|
|
|
|
FireScriptEvaluated(rv, aRequest);
|
2003-11-13 04:15:25 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptAvailable(nsresult aResult,
|
|
|
|
nsScriptLoadRequest* aRequest,
|
|
|
|
const nsAFlatString& aScript)
|
|
|
|
{
|
2003-07-11 22:47:17 +00:00
|
|
|
PRInt32 count = mObservers.Count();
|
|
|
|
for (PRInt32 i = 0; i < count; i++) {
|
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> observer = mObservers[i];
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
if (observer) {
|
2005-01-05 07:25:29 +00:00
|
|
|
observer->ScriptAvailable(aResult, aRequest->mElement,
|
2001-05-17 05:20:51 +00:00
|
|
|
aRequest->mIsInline, aRequest->mWasPending,
|
|
|
|
aRequest->mURI, aRequest->mLineNo,
|
|
|
|
aScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aRequest->FireScriptAvailable(aResult, aScript);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::FireScriptEvaluated(nsresult aResult,
|
|
|
|
nsScriptLoadRequest* aRequest)
|
|
|
|
{
|
2003-07-11 22:47:17 +00:00
|
|
|
PRInt32 count = mObservers.Count();
|
|
|
|
for (PRInt32 i = 0; i < count; i++) {
|
|
|
|
nsCOMPtr<nsIScriptLoaderObserver> observer = mObservers[i];
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
if (observer) {
|
|
|
|
observer->ScriptEvaluated(aResult, aRequest->mElement,
|
|
|
|
aRequest->mIsInline, aRequest->mWasPending);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
aRequest->FireScriptEvaluated(aResult);
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptLoader::EvaluateScript(nsScriptLoadRequest* aRequest,
|
|
|
|
const nsAFlatString& aScript)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-09-25 22:16:41 +00:00
|
|
|
// We need a document to evaluate scripts.
|
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
nsIScriptGlobalObject *globalObject = mDocument->GetScriptGlobalObject();
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_ENSURE_TRUE(globalObject, NS_ERROR_FAILURE);
|
|
|
|
|
2004-02-26 02:35:30 +00:00
|
|
|
// Make sure context is a strong reference since we access it after
|
|
|
|
// we've executed a script, which may cause all other references to
|
|
|
|
// the context to go away.
|
|
|
|
nsCOMPtr<nsIScriptContext> context = globalObject->GetContext();
|
2004-02-09 22:48:53 +00:00
|
|
|
if (!context) {
|
2001-05-17 05:20:51 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
nsIPrincipal *principal = mDocument->GetPrincipal();
|
2001-05-17 05:20:51 +00:00
|
|
|
// We can survive without a principal, but we really should
|
|
|
|
// have one.
|
|
|
|
NS_ASSERTION(principal, "principal required for document");
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCAutoString url;
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
if (aRequest->mURI) {
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = aRequest->mURI->GetSpec(url);
|
2001-05-17 05:20:51 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2004-02-26 02:35:30 +00:00
|
|
|
|
2004-09-01 16:50:12 +00:00
|
|
|
PRBool oldProcessingScriptTag = context->GetProcessingScriptTag();
|
2001-05-17 05:20:51 +00:00
|
|
|
context->SetProcessingScriptTag(PR_TRUE);
|
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
JSContext *cx = (JSContext *)context->GetNativeContext();
|
|
|
|
uint32 options = ::JS_GetOptions(cx);
|
|
|
|
PRBool needE4X = aRequest->mHasE4XOption && !(options & JSOPTION_XML);
|
|
|
|
if (needE4X) {
|
|
|
|
::JS_SetOptions(cx, options | JSOPTION_XML);
|
|
|
|
}
|
|
|
|
|
2005-06-07 19:21:05 +00:00
|
|
|
// Update our current script.
|
|
|
|
nsCOMPtr<nsIScriptElement> oldCurrent = mCurrentScript;
|
|
|
|
mCurrentScript = aRequest->mElement;
|
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
PRBool isUndefined;
|
2002-03-06 07:48:55 +00:00
|
|
|
context->EvaluateString(aScript, nsnull, principal, url.get(),
|
2004-06-11 00:48:00 +00:00
|
|
|
aRequest->mLineNo, aRequest->mJSVersion, nsnull,
|
2004-09-01 16:50:12 +00:00
|
|
|
&isUndefined);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
2005-06-07 19:21:05 +00:00
|
|
|
// Put the old script back in case it wants to do anything else.
|
|
|
|
mCurrentScript = oldCurrent;
|
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
::JS_ReportPendingException(cx);
|
|
|
|
if (needE4X) {
|
|
|
|
::JS_SetOptions(cx, options);
|
|
|
|
}
|
2004-09-01 16:50:12 +00:00
|
|
|
|
|
|
|
context->SetProcessingScriptTag(oldProcessingScriptTag);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPCNativeCallContext> ncc;
|
|
|
|
nsContentUtils::XPConnect()->
|
|
|
|
GetCurrentNativeCallContext(getter_AddRefs(ncc));
|
|
|
|
|
|
|
|
if (ncc) {
|
|
|
|
ncc->SetExceptionWasThrown(PR_FALSE);
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScriptLoader::ProcessPendingReqests()
|
|
|
|
{
|
2003-07-11 22:47:17 +00:00
|
|
|
if (mPendingRequests.Count() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2005-01-05 07:25:29 +00:00
|
|
|
|
2003-07-11 22:47:17 +00:00
|
|
|
nsRefPtr<nsScriptLoadRequest> request = mPendingRequests[0];
|
2001-05-17 05:20:51 +00:00
|
|
|
while (request && !request->mLoading) {
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObjectAt(0);
|
2001-05-17 05:20:51 +00:00
|
|
|
ProcessRequest(request);
|
2003-07-11 22:47:17 +00:00
|
|
|
if (mPendingRequests.Count() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request = mPendingRequests[0];
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-18 19:44:19 +00:00
|
|
|
|
2005-01-05 07:25:29 +00:00
|
|
|
// This function was copied from nsParser.cpp. It was simplified a bit.
|
|
|
|
static PRBool
|
|
|
|
DetectByteOrderMark(const unsigned char* aBytes, PRInt32 aLen, nsCString& oCharset)
|
2002-09-18 19:44:19 +00:00
|
|
|
{
|
|
|
|
if (aLen < 2)
|
2002-09-18 22:05:09 +00:00
|
|
|
return PR_FALSE;
|
2002-09-18 19:44:19 +00:00
|
|
|
|
|
|
|
switch(aBytes[0]) {
|
2005-01-05 07:25:29 +00:00
|
|
|
case 0xEF:
|
|
|
|
if (aLen >= 3 && 0xBB == aBytes[1] && 0xBF == aBytes[2]) {
|
|
|
|
// EF BB BF
|
|
|
|
// Win2K UTF-8 BOM
|
|
|
|
oCharset.Assign("UTF-8");
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFE:
|
2005-01-05 07:25:29 +00:00
|
|
|
if (0xFF == aBytes[1]) {
|
2002-09-18 19:44:19 +00:00
|
|
|
// FE FF
|
2005-01-05 07:25:29 +00:00
|
|
|
// UTF-16, big-endian
|
|
|
|
oCharset.Assign("UTF-16BE");
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xFF:
|
2005-01-05 07:25:29 +00:00
|
|
|
if (0xFE == aBytes[1]) {
|
2002-09-18 19:44:19 +00:00
|
|
|
// FF FE
|
2005-01-05 07:25:29 +00:00
|
|
|
// UTF-16, little-endian
|
|
|
|
oCharset.Assign("UTF-16LE");
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-01-05 07:25:29 +00:00
|
|
|
}
|
2003-05-23 21:34:47 +00:00
|
|
|
return !oCharset.IsEmpty();
|
2002-09-18 19:44:19 +00:00
|
|
|
}
|
|
|
|
|
2005-06-12 12:32:05 +00:00
|
|
|
/* static */ nsresult
|
|
|
|
nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData,
|
|
|
|
PRUint32 aLength, const nsString& aHintCharset,
|
|
|
|
nsIDocument* aDocument, nsString& aString)
|
|
|
|
{
|
|
|
|
if (!aLength) {
|
|
|
|
aString.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString characterSet;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (aChannel) {
|
|
|
|
rv = aChannel->GetContentCharset(characterSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aHintCharset.IsEmpty() && (NS_FAILED(rv) || characterSet.IsEmpty())) {
|
|
|
|
// charset name is always ASCII.
|
|
|
|
LossyCopyUTF16toASCII(aHintCharset, characterSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || characterSet.IsEmpty()) {
|
|
|
|
DetectByteOrderMark(aData, aLength, characterSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (characterSet.IsEmpty()) {
|
|
|
|
// charset from document default
|
|
|
|
characterSet = aDocument->GetDocumentCharacterSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (characterSet.IsEmpty()) {
|
|
|
|
// fall back to ISO-8859-1, see bug 118404
|
|
|
|
characterSet.AssignLiteral("ISO-8859-1");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> charsetConv =
|
|
|
|
do_GetService(kCharsetConverterManagerCID, &rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && charsetConv) {
|
|
|
|
rv = charsetConv->GetUnicodeDecoder(characterSet.get(),
|
|
|
|
getter_AddRefs(unicodeDecoder));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// fall back to ISO-8859-1 if charset is not supported. (bug 230104)
|
|
|
|
rv = charsetConv->GetUnicodeDecoderRaw("ISO-8859-1",
|
|
|
|
getter_AddRefs(unicodeDecoder));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// converts from the charset to unicode
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRInt32 unicodeLength = 0;
|
|
|
|
|
|
|
|
rv = unicodeDecoder->GetMaxLength(NS_REINTERPRET_CAST(const char*, aData),
|
|
|
|
aLength, &unicodeLength);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aString.SetLength(unicodeLength);
|
|
|
|
PRUnichar *ustr = aString.BeginWriting();
|
|
|
|
|
|
|
|
PRInt32 consumedLength = 0;
|
|
|
|
PRInt32 originalLength = aLength;
|
|
|
|
PRInt32 convertedLength = 0;
|
|
|
|
PRInt32 bufferLength = unicodeLength;
|
|
|
|
do {
|
|
|
|
rv = unicodeDecoder->Convert(NS_REINTERPRET_CAST(const char*, aData),
|
|
|
|
(PRInt32 *) &aLength, ustr,
|
|
|
|
&unicodeLength);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// if we failed, we consume one byte, replace it with U+FFFD
|
|
|
|
// and try the conversion again.
|
|
|
|
ustr[unicodeLength++] = (PRUnichar)0xFFFD;
|
|
|
|
ustr += unicodeLength;
|
|
|
|
|
|
|
|
unicodeDecoder->Reset();
|
|
|
|
}
|
|
|
|
aData += ++aLength;
|
|
|
|
consumedLength += aLength;
|
|
|
|
aLength = originalLength - consumedLength;
|
|
|
|
convertedLength += unicodeLength;
|
|
|
|
unicodeLength = bufferLength - convertedLength;
|
|
|
|
} while (NS_FAILED(rv) && (originalLength > consumedLength) && (bufferLength > convertedLength));
|
|
|
|
aString.SetLength(convertedLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2002-09-18 19:44:19 +00:00
|
|
|
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
PRUint32 stringLen,
|
2004-04-27 16:06:00 +00:00
|
|
|
const PRUint8* string)
|
2001-05-17 05:20:51 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-07-11 22:47:17 +00:00
|
|
|
nsScriptLoadRequest* request = NS_STATIC_CAST(nsScriptLoadRequest*, aContext);
|
2001-05-17 05:20:51 +00:00
|
|
|
NS_ASSERTION(request, "null request in stream complete handler");
|
|
|
|
if (!request) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObject(request);
|
2004-01-29 22:04:45 +00:00
|
|
|
FireScriptAvailable(aStatus, request, EmptyString());
|
2001-05-17 05:20:51 +00:00
|
|
|
ProcessPendingReqests();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a document, then we need to abort further
|
|
|
|
// evaluation.
|
|
|
|
if (!mDocument) {
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObject(request);
|
2005-01-05 07:25:29 +00:00
|
|
|
FireScriptAvailable(NS_ERROR_NOT_AVAILABLE, request,
|
2004-01-29 22:04:45 +00:00
|
|
|
EmptyString());
|
2001-05-17 05:20:51 +00:00
|
|
|
ProcessPendingReqests();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-07-01 23:12:13 +00:00
|
|
|
// If the load returned an error page, then we need to abort
|
|
|
|
nsCOMPtr<nsIRequest> req;
|
2005-01-05 07:25:29 +00:00
|
|
|
rv = aLoader->GetRequest(getter_AddRefs(req));
|
2002-07-01 23:12:13 +00:00
|
|
|
NS_ASSERTION(req, "StreamLoader's request went away prematurely");
|
|
|
|
if (NS_FAILED(rv)) return rv; // XXX Should this remove the pending request?
|
2003-11-10 05:53:48 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(req));
|
2002-07-01 23:12:13 +00:00
|
|
|
if (httpChannel) {
|
|
|
|
PRBool requestSucceeded;
|
|
|
|
rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
if (NS_SUCCEEDED(rv) && !requestSucceeded) {
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObject(request);
|
2005-01-05 07:25:29 +00:00
|
|
|
FireScriptAvailable(NS_ERROR_NOT_AVAILABLE, request,
|
2004-01-29 22:04:45 +00:00
|
|
|
EmptyString());
|
2002-07-01 23:12:13 +00:00
|
|
|
ProcessPendingReqests();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-12 12:32:05 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(req);
|
2001-05-17 05:20:51 +00:00
|
|
|
if (stringLen) {
|
2005-06-12 12:32:05 +00:00
|
|
|
// Check the charset attribute to determine script charset.
|
|
|
|
nsAutoString hintCharset;
|
|
|
|
request->mElement->GetScriptCharset(hintCharset);
|
|
|
|
rv = ConvertToUTF16(channel, string, stringLen, hintCharset, mDocument,
|
|
|
|
request->mScriptText);
|
2001-05-17 05:20:51 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"Could not convert external JavaScript to Unicode!");
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObject(request);
|
2004-01-29 22:04:45 +00:00
|
|
|
FireScriptAvailable(rv, request, EmptyString());
|
2001-05-17 05:20:51 +00:00
|
|
|
ProcessPendingReqests();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- Merge the principal of the script file with that of the document
|
|
|
|
if (channel) {
|
|
|
|
nsCOMPtr<nsISupports> owner;
|
|
|
|
channel->GetOwner(getter_AddRefs(owner));
|
2003-10-21 22:11:49 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(owner);
|
|
|
|
|
|
|
|
if (principal) {
|
2003-10-22 06:09:48 +00:00
|
|
|
nsIPrincipal *docPrincipal = mDocument->GetPrincipal();
|
|
|
|
if (docPrincipal) {
|
2003-10-21 22:11:49 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> newPrincipal =
|
|
|
|
IntersectPrincipalCerts(docPrincipal, principal);
|
|
|
|
|
|
|
|
mDocument->SetPrincipal(newPrincipal);
|
|
|
|
} else {
|
|
|
|
mPendingRequests.RemoveObject(request);
|
2004-01-29 22:04:45 +00:00
|
|
|
FireScriptAvailable(rv, request, EmptyString());
|
2003-10-21 22:11:49 +00:00
|
|
|
ProcessPendingReqests();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-05-17 05:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we're not the first in the pending list, we mark ourselves
|
|
|
|
// as loaded and just stay on the list.
|
2003-07-11 22:47:17 +00:00
|
|
|
NS_ASSERTION(mPendingRequests.Count() > 0, "aContext is a pending request!");
|
|
|
|
if (mPendingRequests[0] != request) {
|
2001-05-17 05:20:51 +00:00
|
|
|
request->mLoading = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-11 22:47:17 +00:00
|
|
|
mPendingRequests.RemoveObject(request);
|
2001-05-17 05:20:51 +00:00
|
|
|
ProcessRequest(request);
|
|
|
|
|
|
|
|
// Process any pending requests
|
|
|
|
ProcessPendingReqests();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-05-25 07:03:04 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-10-11 00:38:29 +00:00
|
|
|
nsScriptLoader::GetEnabled(PRBool *aEnabled)
|
2001-05-25 07:03:04 +00:00
|
|
|
{
|
2002-10-11 00:38:29 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aEnabled);
|
|
|
|
*aEnabled = mEnabled;
|
2001-05-25 07:03:04 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-10-11 00:38:29 +00:00
|
|
|
nsScriptLoader::SetEnabled(PRBool aEnabled)
|
2001-05-25 07:03:04 +00:00
|
|
|
{
|
2002-10-11 00:38:29 +00:00
|
|
|
mEnabled = aEnabled;
|
2001-05-25 07:03:04 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|