2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-04-20 03:36:57 +00:00
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
#include "mozilla/dom/DOMParser.h"
|
2013-08-22 06:30:55 +00:00
|
|
|
|
|
|
|
#include "nsIDOMDocument.h"
|
2006-04-20 03:36:57 +00:00
|
|
|
#include "nsNetUtil.h"
|
2015-09-29 04:31:56 +00:00
|
|
|
#include "nsIStreamListener.h"
|
2006-04-20 03:39:49 +00:00
|
|
|
#include "nsStringStream.h"
|
2016-06-08 01:57:45 +00:00
|
|
|
#include "nsIScriptError.h"
|
2006-04-20 03:36:57 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2006-04-20 03:38:00 +00:00
|
|
|
#include "nsCRT.h"
|
2006-04-20 03:39:03 +00:00
|
|
|
#include "nsStreamUtils.h"
|
2006-04-26 09:19:48 +00:00
|
|
|
#include "nsContentUtils.h"
|
2006-06-13 03:07:47 +00:00
|
|
|
#include "nsDOMJSUtils.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2007-12-12 08:33:32 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
2015-04-13 18:47:41 +00:00
|
|
|
#include "nsNullPrincipal.h"
|
2014-07-10 06:56:37 +00:00
|
|
|
#include "mozilla/LoadInfo.h"
|
2012-12-25 23:06:15 +00:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-08-21 00:45:04 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2010-10-05 01:25:44 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2012-12-04 01:26:16 +00:00
|
|
|
using namespace mozilla::dom;
|
2006-04-20 03:39:03 +00:00
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::DOMParser()
|
2011-10-17 14:59:28 +00:00
|
|
|
: mAttemptedInit(false)
|
2006-04-20 03:36:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::~DOMParser()
|
2006-04-20 03:36:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
// QueryInterface implementation for DOMParser
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMParser)
|
2012-12-04 01:26:16 +00:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2006-04-20 03:37:35 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMParser)
|
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.
2006-04-20 03:37:17 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMParser)
|
2006-04-20 03:37:35 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
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.
2006-04-20 03:37:17 +00:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 08:57:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMParser, mOwner)
|
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.
2006-04-20 03:37:17 +00:00
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMParser)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMParser)
|
2012-12-04 01:26:16 +00:00
|
|
|
|
2013-05-06 19:28:13 +00:00
|
|
|
static const char*
|
|
|
|
StringFromSupportedType(SupportedType aType)
|
|
|
|
{
|
|
|
|
return SupportedTypeValues::strings[static_cast<int>(aType)].value;
|
|
|
|
}
|
|
|
|
|
2012-12-04 01:26:16 +00:00
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::ParseFromString(const nsAString& aStr, SupportedType aType,
|
|
|
|
ErrorResult& rv)
|
2012-12-04 01:26:16 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2012-12-05 04:15:47 +00:00
|
|
|
rv = ParseFromString(aStr,
|
2013-05-06 19:28:13 +00:00
|
|
|
StringFromSupportedType(aType),
|
2012-12-05 04:15:47 +00:00
|
|
|
getter_AddRefs(domDocument));
|
2012-12-04 01:26:16 +00:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
2006-04-20 03:36:57 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 15:02:17 +00:00
|
|
|
DOMParser::ParseFromString(const char16_t *str,
|
2013-08-30 17:16:32 +00:00
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2006-04-20 03:36:57 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(str);
|
2012-12-05 04:15:47 +00:00
|
|
|
// Converting a string to an enum value manually is a bit of a pain,
|
|
|
|
// so let's just use a helper that takes a content-type string.
|
|
|
|
return ParseFromString(nsDependentString(str), contentType, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::ParseFromString(const nsAString& str,
|
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2012-12-05 04:15:47 +00:00
|
|
|
{
|
2006-04-26 09:19:48 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2006-04-20 03:36:57 +00:00
|
|
|
|
2012-01-20 12:03:49 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!nsCRT::strcmp(contentType, "text/html")) {
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
|
|
|
rv = SetUpDocument(DocumentFlavorHTML, getter_AddRefs(domDocument));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIDocument> document = do_QueryInterface(domDocument);
|
|
|
|
|
|
|
|
// Keep the XULXBL state, base URL and principal setting in sync with the
|
|
|
|
// XML case
|
|
|
|
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(mOriginalPrincipal)) {
|
|
|
|
document->ForceEnableXULXBL();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to give this document the right base URI
|
|
|
|
document->SetBaseURI(mBaseURI);
|
|
|
|
// And the right principal
|
|
|
|
document->SetPrincipal(mPrincipal);
|
|
|
|
|
2012-12-05 04:15:47 +00:00
|
|
|
rv = nsContentUtils::ParseDocumentHTML(str, document, false);
|
2012-07-09 18:24:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-01-20 12:03:49 +00:00
|
|
|
domDocument.forget(aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-07 15:18:30 +00:00
|
|
|
nsAutoCString utf8str;
|
|
|
|
// Convert from UTF16 to UTF8 using fallible allocations
|
2015-01-28 09:00:40 +00:00
|
|
|
if (!AppendUTF16toUTF8(str, utf8str, mozilla::fallible)) {
|
2014-01-07 15:18:30 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2006-04-20 03:36:57 +00:00
|
|
|
|
2006-04-20 03:39:49 +00:00
|
|
|
// The new stream holds a reference to the buffer
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2012-01-20 12:03:49 +00:00
|
|
|
rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
2014-01-07 15:18:30 +00:00
|
|
|
utf8str.get(), utf8str.Length(),
|
2012-01-20 12:03:49 +00:00
|
|
|
NS_ASSIGNMENT_DEPEND);
|
2006-04-20 03:39:49 +00:00
|
|
|
if (NS_FAILED(rv))
|
2006-04-20 03:37:03 +00:00
|
|
|
return rv;
|
2006-04-20 03:36:57 +00:00
|
|
|
|
2014-01-07 15:18:30 +00:00
|
|
|
return ParseFromStream(stream, "UTF-8", utf8str.Length(), contentType, aResult);
|
2006-04-20 03:39:00 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 01:26:16 +00:00
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::ParseFromBuffer(const Sequence<uint8_t>& aBuf, uint32_t aBufLen,
|
|
|
|
SupportedType aType, ErrorResult& rv)
|
2012-12-04 01:26:16 +00:00
|
|
|
{
|
|
|
|
if (aBufLen > aBuf.Length()) {
|
|
|
|
rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2013-08-30 17:16:32 +00:00
|
|
|
rv = DOMParser::ParseFromBuffer(aBuf.Elements(), aBufLen,
|
|
|
|
StringFromSupportedType(aType),
|
|
|
|
getter_AddRefs(domDocument));
|
2012-12-04 01:26:16 +00:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::ParseFromBuffer(const Uint8Array& aBuf, uint32_t aBufLen,
|
|
|
|
SupportedType aType, ErrorResult& rv)
|
2012-12-04 01:26:16 +00:00
|
|
|
{
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 21:32:41 +00:00
|
|
|
aBuf.ComputeLengthAndData();
|
|
|
|
|
2012-12-04 01:26:16 +00:00
|
|
|
if (aBufLen > aBuf.Length()) {
|
|
|
|
rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2013-08-30 17:16:32 +00:00
|
|
|
rv = DOMParser::ParseFromBuffer(aBuf.Data(), aBufLen,
|
2013-05-06 19:28:13 +00:00
|
|
|
StringFromSupportedType(aType),
|
2012-12-04 01:26:16 +00:00
|
|
|
getter_AddRefs(domDocument));
|
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
|
|
|
|
2006-04-20 03:39:00 +00:00
|
|
|
NS_IMETHODIMP
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::ParseFromBuffer(const uint8_t *buf,
|
|
|
|
uint32_t bufLen,
|
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2006-04-20 03:39:00 +00:00
|
|
|
{
|
2006-04-20 03:39:02 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(buf);
|
2006-04-26 09:19:48 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2006-04-20 03:39:00 +00:00
|
|
|
|
2006-04-20 03:39:49 +00:00
|
|
|
// The new stream holds a reference to the buffer
|
2006-04-20 03:39:00 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2006-04-20 03:39:49 +00:00
|
|
|
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
2007-07-08 07:08:04 +00:00
|
|
|
reinterpret_cast<const char *>(buf),
|
2006-04-20 03:39:49 +00:00
|
|
|
bufLen, NS_ASSIGNMENT_DEPEND);
|
|
|
|
if (NS_FAILED(rv))
|
2006-04-20 03:39:00 +00:00
|
|
|
return rv;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return ParseFromStream(stream, nullptr, bufLen, contentType, aResult);
|
2006-04-20 03:36:57 +00:00
|
|
|
}
|
|
|
|
|
2006-04-20 03:37:01 +00:00
|
|
|
|
2012-12-04 01:26:16 +00:00
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::ParseFromStream(nsIInputStream* aStream,
|
|
|
|
const nsAString& aCharset,
|
|
|
|
int32_t aContentLength,
|
|
|
|
SupportedType aType,
|
|
|
|
ErrorResult& rv)
|
2012-12-04 01:26:16 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2013-08-30 17:16:32 +00:00
|
|
|
rv = DOMParser::ParseFromStream(aStream,
|
|
|
|
NS_ConvertUTF16toUTF8(aCharset).get(),
|
|
|
|
aContentLength,
|
|
|
|
StringFromSupportedType(aType),
|
|
|
|
getter_AddRefs(domDocument));
|
2012-12-04 01:26:16 +00:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
|
|
|
|
2006-04-20 03:36:57 +00:00
|
|
|
NS_IMETHODIMP
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::ParseFromStream(nsIInputStream *stream,
|
|
|
|
const char *charset,
|
|
|
|
int32_t contentLength,
|
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2006-04-20 03:36:57 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(stream);
|
2006-04-20 03:39:02 +00:00
|
|
|
NS_ENSURE_ARG(contentType);
|
2006-04-26 09:19:48 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
2006-04-20 03:36:57 +00:00
|
|
|
|
2011-10-13 10:50:05 +00:00
|
|
|
bool svg = nsCRT::strcmp(contentType, "image/svg+xml") == 0;
|
|
|
|
|
2006-04-20 03:36:57 +00:00
|
|
|
// For now, we can only create XML documents.
|
2011-10-13 10:50:05 +00:00
|
|
|
//XXXsmaug Should we create an HTMLDocument (in XHTML mode)
|
|
|
|
// for "application/xhtml+xml"?
|
2006-04-20 03:39:02 +00:00
|
|
|
if ((nsCRT::strcmp(contentType, "text/xml") != 0) &&
|
|
|
|
(nsCRT::strcmp(contentType, "application/xml") != 0) &&
|
2011-10-13 10:50:05 +00:00
|
|
|
(nsCRT::strcmp(contentType, "application/xhtml+xml") != 0) &&
|
|
|
|
!svg)
|
2006-04-20 03:36:57 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
2006-04-20 03:38:27 +00:00
|
|
|
nsresult rv;
|
2006-09-09 04:54:03 +00:00
|
|
|
|
|
|
|
// Put the nsCOMPtr out here so we hold a ref to the stream as needed
|
2006-06-15 20:30:44 +00:00
|
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
2006-04-20 03:39:03 +00:00
|
|
|
if (!NS_InputStreamIsBuffered(stream)) {
|
2006-06-15 20:30:44 +00:00
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream,
|
|
|
|
4096);
|
2006-04-20 03:39:03 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-06-15 20:30:44 +00:00
|
|
|
|
2006-04-20 03:39:03 +00:00
|
|
|
stream = bufferedStream;
|
|
|
|
}
|
2007-10-01 10:02:32 +00:00
|
|
|
|
2006-04-20 03:36:57 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2012-01-20 12:03:49 +00:00
|
|
|
rv = SetUpDocument(svg ? DocumentFlavorSVG : DocumentFlavorLegacyGuess,
|
|
|
|
getter_AddRefs(domDocument));
|
2006-06-15 20:30:44 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-04-20 03:36:57 +00:00
|
|
|
|
|
|
|
// Create a fake channel
|
2006-04-20 03:39:49 +00:00
|
|
|
nsCOMPtr<nsIChannel> parserChannel;
|
2014-10-16 18:14:42 +00:00
|
|
|
NS_NewInputStreamChannel(getter_AddRefs(parserChannel),
|
|
|
|
mDocumentURI,
|
|
|
|
nullptr, // aStream
|
|
|
|
mOriginalPrincipal,
|
|
|
|
nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
nsDependentCString(contentType));
|
2006-04-20 03:39:49 +00:00
|
|
|
NS_ENSURE_STATE(parserChannel);
|
2006-04-20 03:36:57 +00:00
|
|
|
|
2006-04-20 03:39:02 +00:00
|
|
|
if (charset) {
|
|
|
|
parserChannel->SetContentCharset(nsDependentCString(charset));
|
|
|
|
}
|
|
|
|
|
2006-04-20 03:36:57 +00:00
|
|
|
// Tell the document to start loading
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
2006-04-20 03:37:35 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
// Have to pass false for reset here, else the reset will remove
|
2006-04-20 03:39:42 +00:00
|
|
|
// our event listener. Should that listener addition move to later
|
2006-09-09 04:54:03 +00:00
|
|
|
// than this call? Then we wouldn't need to mess around with
|
|
|
|
// SetPrincipal, etc, probably!
|
2008-04-24 21:54:42 +00:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
if (!document) return NS_ERROR_FAILURE;
|
|
|
|
|
2012-01-20 12:03:49 +00:00
|
|
|
// Keep the XULXBL state, base URL and principal setting in sync with the
|
|
|
|
// HTML case
|
|
|
|
|
2010-08-19 23:12:46 +00:00
|
|
|
if (nsContentUtils::IsSystemPrincipal(mOriginalPrincipal)) {
|
|
|
|
document->ForceEnableXULXBL();
|
|
|
|
}
|
|
|
|
|
2006-04-20 03:39:49 +00:00
|
|
|
rv = document->StartDocumentLoad(kLoadAsData, parserChannel,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, nullptr,
|
2006-04-20 03:36:57 +00:00
|
|
|
getter_AddRefs(listener),
|
2011-10-17 14:59:28 +00:00
|
|
|
false);
|
2006-04-20 03:36:57 +00:00
|
|
|
|
2008-04-24 21:54:42 +00:00
|
|
|
// Make sure to give this document the right base URI
|
2006-09-09 04:54:03 +00:00
|
|
|
document->SetBaseURI(mBaseURI);
|
2006-04-20 03:39:42 +00:00
|
|
|
|
2008-04-24 21:54:42 +00:00
|
|
|
// And the right principal
|
|
|
|
document->SetPrincipal(mPrincipal);
|
|
|
|
|
2006-04-20 03:37:35 +00:00
|
|
|
if (NS_FAILED(rv) || !listener) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2006-04-20 03:36:57 +00:00
|
|
|
|
|
|
|
// Now start pumping data to the listener
|
|
|
|
nsresult status;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = listener->OnStartRequest(parserChannel, nullptr);
|
2006-04-20 03:39:49 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
parserChannel->Cancel(rv);
|
|
|
|
parserChannel->GetStatus(&status);
|
2006-04-20 03:36:57 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = listener->OnDataAvailable(parserChannel, nullptr, stream, 0,
|
2006-04-20 03:39:49 +00:00
|
|
|
contentLength);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
parserChannel->Cancel(rv);
|
|
|
|
parserChannel->GetStatus(&status);
|
2006-04-20 03:36:57 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = listener->OnStopRequest(parserChannel, nullptr, status);
|
2006-04-20 03:39:49 +00:00
|
|
|
// Failure returned from OnStopRequest does not affect the final status of
|
|
|
|
// the channel, so we do not need to call Cancel(rv) as we do above.
|
2006-04-20 03:38:20 +00:00
|
|
|
|
2006-04-20 03:37:35 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-04-26 09:19:48 +00:00
|
|
|
domDocument.swap(*aResult);
|
2006-04-20 03:36:57 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-09-09 04:54:03 +00:00
|
|
|
NS_IMETHODIMP
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::Init(nsIPrincipal* principal, nsIURI* documentURI,
|
2014-08-21 00:45:04 +00:00
|
|
|
nsIURI* baseURI, nsIGlobalObject* aScriptObject)
|
2006-04-20 03:37:01 +00:00
|
|
|
{
|
2006-09-09 04:54:03 +00:00
|
|
|
NS_ENSURE_STATE(!mAttemptedInit);
|
2011-10-17 14:59:28 +00:00
|
|
|
mAttemptedInit = true;
|
2006-09-09 04:54:03 +00:00
|
|
|
NS_ENSURE_ARG(principal || documentURI);
|
|
|
|
mDocumentURI = documentURI;
|
2010-08-19 23:06:06 +00:00
|
|
|
|
2006-09-09 04:54:03 +00:00
|
|
|
if (!mDocumentURI) {
|
|
|
|
principal->GetURI(getter_AddRefs(mDocumentURI));
|
2010-08-19 23:06:06 +00:00
|
|
|
// If we have the system principal, then we'll just use the null principals
|
|
|
|
// uri.
|
|
|
|
if (!mDocumentURI && !nsContentUtils::IsSystemPrincipal(principal)) {
|
2006-09-09 04:54:03 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-01 10:02:32 +00:00
|
|
|
mScriptHandlingObject = do_GetWeakReference(aScriptObject);
|
2006-09-09 04:54:03 +00:00
|
|
|
mPrincipal = principal;
|
2008-03-28 03:46:15 +00:00
|
|
|
nsresult rv;
|
2006-09-09 04:54:03 +00:00
|
|
|
if (!mPrincipal) {
|
2016-06-08 01:57:45 +00:00
|
|
|
// BUG 1237080 -- in this case we're getting a chrome privilege scripted
|
|
|
|
// DOMParser object creation without an explicit principal set. This is
|
|
|
|
// now deprecated.
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
|
|
NS_LITERAL_CSTRING("DOM"),
|
|
|
|
nullptr,
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"ChromeScriptedDOMParserWithoutPrincipal",
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
documentURI);
|
|
|
|
|
2016-05-24 10:01:34 +00:00
|
|
|
PrincipalOriginAttributes attrs;
|
|
|
|
mPrincipal = BasePrincipal::CreateCodebasePrincipal(mDocumentURI, attrs);
|
|
|
|
NS_ENSURE_TRUE(mPrincipal, NS_ERROR_FAILURE);
|
2008-03-28 03:46:15 +00:00
|
|
|
mOriginalPrincipal = mPrincipal;
|
|
|
|
} else {
|
2010-08-19 23:06:06 +00:00
|
|
|
mOriginalPrincipal = mPrincipal;
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(mPrincipal)) {
|
2008-03-28 03:46:15 +00:00
|
|
|
// Don't give DOMParsers the system principal. Use a null
|
|
|
|
// principal instead.
|
2015-04-13 18:47:41 +00:00
|
|
|
mPrincipal = nsNullPrincipal::Create();
|
2010-08-19 23:06:06 +00:00
|
|
|
|
|
|
|
if (!mDocumentURI) {
|
|
|
|
rv = mPrincipal->GetURI(getter_AddRefs(mDocumentURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-03-28 03:46:15 +00:00
|
|
|
}
|
2006-09-09 04:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mBaseURI = baseURI;
|
|
|
|
// Note: if mBaseURI is null, fine. Leave it like that; that will use the
|
|
|
|
// documentURI as the base. Otherwise for null principals we'll get
|
|
|
|
// nsDocument::SetBaseURI giving errors.
|
2006-04-26 09:19:48 +00:00
|
|
|
|
2006-09-09 04:54:03 +00:00
|
|
|
NS_POSTCONDITION(mPrincipal, "Must have principal");
|
2008-03-28 03:46:15 +00:00
|
|
|
NS_POSTCONDITION(mOriginalPrincipal, "Must have original principal");
|
2006-09-09 04:54:03 +00:00
|
|
|
NS_POSTCONDITION(mDocumentURI, "Must have document URI");
|
2006-04-20 03:37:01 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-09-09 04:54:03 +00:00
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
/*static */already_AddRefed<DOMParser>
|
|
|
|
DOMParser::Constructor(const GlobalObject& aOwner,
|
|
|
|
nsIPrincipal* aPrincipal, nsIURI* aDocumentURI,
|
|
|
|
nsIURI* aBaseURI, ErrorResult& rv)
|
2012-12-04 01:26:16 +00:00
|
|
|
{
|
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
|
|
|
rv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DOMParser> domParser = new DOMParser(aOwner.GetAsSupports());
|
2013-08-23 05:17:08 +00:00
|
|
|
rv = domParser->InitInternal(aOwner.GetAsSupports(), aPrincipal, aDocumentURI,
|
2012-12-03 16:07:49 +00:00
|
|
|
aBaseURI);
|
2012-12-04 01:26:16 +00:00
|
|
|
if (rv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return domParser.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-30 17:16:32 +00:00
|
|
|
/*static */already_AddRefed<DOMParser>
|
|
|
|
DOMParser::Constructor(const GlobalObject& aOwner,
|
|
|
|
ErrorResult& rv)
|
2006-09-09 04:54:03 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DOMParser> domParser = new DOMParser(aOwner.GetAsSupports());
|
2014-05-06 22:43:03 +00:00
|
|
|
rv = domParser->InitInternal(aOwner.GetAsSupports(),
|
2014-05-13 09:58:00 +00:00
|
|
|
nsContentUtils::SubjectPrincipal(),
|
2014-05-06 22:43:03 +00:00
|
|
|
nullptr, nullptr);
|
2012-12-04 01:26:16 +00:00
|
|
|
if (rv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return domParser.forget();
|
2012-12-04 01:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::InitInternal(nsISupports* aOwner, nsIPrincipal* prin,
|
|
|
|
nsIURI* documentURI, nsIURI* baseURI)
|
2012-12-04 01:26:16 +00:00
|
|
|
{
|
|
|
|
AttemptedInitMarker marker(&mAttemptedInit);
|
2006-09-09 04:54:03 +00:00
|
|
|
if (!documentURI) {
|
|
|
|
// No explicit documentURI; grab document and base URIs off the window our
|
|
|
|
// constructor was called on. Error out if anything untoward happens.
|
|
|
|
|
|
|
|
// Note that this is a behavior change as far as I can tell -- we're now
|
|
|
|
// using the base URI and document URI of the window off of which the
|
|
|
|
// DOMParser is created, not the window in which parse*() is called.
|
|
|
|
// Does that matter?
|
|
|
|
|
|
|
|
// Also note that |cx| matches what GetDocumentFromContext() would return,
|
|
|
|
// while GetDocumentFromCaller() gives us the window that the DOMParser()
|
|
|
|
// call was made on.
|
2007-12-12 08:33:32 +00:00
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aOwner);
|
2012-12-26 11:00:05 +00:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-12-12 08:33:32 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 11:00:05 +00:00
|
|
|
baseURI = window->GetDocBaseURI();
|
|
|
|
documentURI = window->GetDocumentURI();
|
|
|
|
if (!documentURI) {
|
2006-09-09 04:54:03 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 00:45:04 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> scriptglobal = do_QueryInterface(aOwner);
|
2007-12-12 08:33:32 +00:00
|
|
|
return Init(prin, documentURI, baseURI, scriptglobal);
|
2006-09-09 04:54:03 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 01:26:16 +00:00
|
|
|
void
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::Init(nsIPrincipal* aPrincipal, nsIURI* aDocumentURI,
|
|
|
|
nsIURI* aBaseURI, mozilla::ErrorResult& rv)
|
2006-09-09 04:54:03 +00:00
|
|
|
{
|
|
|
|
AttemptedInitMarker marker(&mAttemptedInit);
|
|
|
|
|
2010-08-19 23:06:06 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = aPrincipal;
|
|
|
|
if (!principal && !aDocumentURI) {
|
2014-05-13 09:58:00 +00:00
|
|
|
principal = nsContentUtils::SubjectPrincipal();
|
2010-08-19 23:06:06 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 00:45:04 +00:00
|
|
|
rv = Init(principal, aDocumentURI, aBaseURI, GetEntryGlobal());
|
2006-09-09 04:54:03 +00:00
|
|
|
}
|
2012-01-20 12:03:49 +00:00
|
|
|
|
|
|
|
nsresult
|
2013-08-30 17:16:32 +00:00
|
|
|
DOMParser::SetUpDocument(DocumentFlavor aFlavor, nsIDOMDocument** aResult)
|
2012-01-20 12:03:49 +00:00
|
|
|
{
|
2014-08-21 00:45:04 +00:00
|
|
|
// We should really QI to nsIGlobalObject here, but nsDocument gets confused
|
|
|
|
// if we pass it a scriptHandlingObject that doesn't QI to
|
|
|
|
// nsIScriptGlobalObject, and test_isequalnode.js (an xpcshell test without
|
|
|
|
// a window global) breaks. The correct solution is just to wean nsDocument
|
|
|
|
// off of nsIScriptGlobalObject, but that's a yak to shave another day.
|
2012-01-20 12:03:49 +00:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptHandlingObject =
|
|
|
|
do_QueryReferent(mScriptHandlingObject);
|
|
|
|
nsresult rv;
|
|
|
|
if (!mPrincipal) {
|
|
|
|
NS_ENSURE_TRUE(!mAttemptedInit, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
AttemptedInitMarker marker(&mAttemptedInit);
|
|
|
|
|
2015-04-13 18:47:41 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> prin = nsNullPrincipal::Create();
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = Init(prin, nullptr, nullptr, scriptHandlingObject);
|
2012-01-20 12:03:49 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mPrincipal, "Must have principal by now");
|
|
|
|
NS_ASSERTION(mDocumentURI, "Must have document URI by now");
|
|
|
|
|
|
|
|
// Here we have to cheat a little bit... Setting the base URI won't
|
|
|
|
// work if the document has a null principal, so use
|
|
|
|
// mOriginalPrincipal when creating the document, then reset the
|
|
|
|
// principal.
|
2012-12-10 14:05:33 +00:00
|
|
|
return NS_NewDOMDocument(aResult, EmptyString(), EmptyString(), nullptr,
|
|
|
|
mDocumentURI, mBaseURI,
|
|
|
|
mOriginalPrincipal,
|
|
|
|
true,
|
|
|
|
scriptHandlingObject,
|
|
|
|
aFlavor);
|
2012-01-20 12:03:49 +00:00
|
|
|
}
|