2015-05-03 22:32:37 +03: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 15:12:37 +04: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 07:36:57 +04:00
|
|
|
|
2013-08-30 21:16:32 +04:00
|
|
|
#include "mozilla/dom/DOMParser.h"
|
2013-08-22 10:30:55 +04:00
|
|
|
|
|
|
|
#include "nsIDOMDocument.h"
|
2006-04-20 07:36:57 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-09-29 07:31:56 +03:00
|
|
|
#include "nsIStreamListener.h"
|
2006-04-20 07:39:49 +04:00
|
|
|
#include "nsStringStream.h"
|
2006-04-20 07:36:57 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2006-04-20 07:38:00 +04:00
|
|
|
#include "nsCRT.h"
|
2006-04-20 07:39:03 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2006-04-26 13:19:48 +04:00
|
|
|
#include "nsContentUtils.h"
|
2006-06-13 07:07:47 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2007-12-12 11:33:32 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2015-04-13 21:47:41 +03:00
|
|
|
#include "nsNullPrincipal.h"
|
2014-07-10 10:56:37 +04:00
|
|
|
#include "mozilla/LoadInfo.h"
|
2012-12-26 03:06:15 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-08-21 04:45:04 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2010-10-05 05:25:44 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2012-12-04 05:26:16 +04:00
|
|
|
using namespace mozilla::dom;
|
2006-04-20 07:39:03 +04:00
|
|
|
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::DOMParser()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mAttemptedInit(false)
|
2006-04-20 07:36:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::~DOMParser()
|
2006-04-20 07:36:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-30 21:16:32 +04:00
|
|
|
// QueryInterface implementation for DOMParser
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMParser)
|
2012-12-04 05:26:16 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2006-04-20 07:37:35 +04: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 07:37:17 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMParser)
|
2006-04-20 07:37:35 +04: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 07:37:17 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 12:57:00 +04: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 07:37:17 +04:00
|
|
|
|
2013-08-30 21:16:32 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMParser)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMParser)
|
2012-12-04 05:26:16 +04:00
|
|
|
|
2013-05-06 23:28:13 +04:00
|
|
|
static const char*
|
|
|
|
StringFromSupportedType(SupportedType aType)
|
|
|
|
{
|
|
|
|
return SupportedTypeValues::strings[static_cast<int>(aType)].value;
|
|
|
|
}
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::ParseFromString(const nsAString& aStr, SupportedType aType,
|
|
|
|
ErrorResult& rv)
|
2012-12-04 05:26:16 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2012-12-05 08:15:47 +04:00
|
|
|
rv = ParseFromString(aStr,
|
2013-05-06 23:28:13 +04:00
|
|
|
StringFromSupportedType(aType),
|
2012-12-05 08:15:47 +04:00
|
|
|
getter_AddRefs(domDocument));
|
2012-12-04 05:26:16 +04:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
2006-04-20 07:36:57 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
DOMParser::ParseFromString(const char16_t *str,
|
2013-08-30 21:16:32 +04:00
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2006-04-20 07:36:57 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(str);
|
2012-12-05 08:15:47 +04: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 21:16:32 +04:00
|
|
|
DOMParser::ParseFromString(const nsAString& str,
|
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2012-12-05 08:15:47 +04:00
|
|
|
{
|
2006-04-26 13:19:48 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2006-04-20 07:36:57 +04:00
|
|
|
|
2012-01-20 16:03:49 +04: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 08:15:47 +04:00
|
|
|
rv = nsContentUtils::ParseDocumentHTML(str, document, false);
|
2012-07-09 22:24:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-01-20 16:03:49 +04:00
|
|
|
domDocument.forget(aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-07 19:18:30 +04:00
|
|
|
nsAutoCString utf8str;
|
|
|
|
// Convert from UTF16 to UTF8 using fallible allocations
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!AppendUTF16toUTF8(str, utf8str, mozilla::fallible)) {
|
2014-01-07 19:18:30 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2006-04-20 07:36:57 +04:00
|
|
|
|
2006-04-20 07:39:49 +04:00
|
|
|
// The new stream holds a reference to the buffer
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2012-01-20 16:03:49 +04:00
|
|
|
rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
2014-01-07 19:18:30 +04:00
|
|
|
utf8str.get(), utf8str.Length(),
|
2012-01-20 16:03:49 +04:00
|
|
|
NS_ASSIGNMENT_DEPEND);
|
2006-04-20 07:39:49 +04:00
|
|
|
if (NS_FAILED(rv))
|
2006-04-20 07:37:03 +04:00
|
|
|
return rv;
|
2006-04-20 07:36:57 +04:00
|
|
|
|
2014-01-07 19:18:30 +04:00
|
|
|
return ParseFromStream(stream, "UTF-8", utf8str.Length(), contentType, aResult);
|
2006-04-20 07:39:00 +04:00
|
|
|
}
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::ParseFromBuffer(const Sequence<uint8_t>& aBuf, uint32_t aBufLen,
|
|
|
|
SupportedType aType, ErrorResult& rv)
|
2012-12-04 05:26:16 +04:00
|
|
|
{
|
|
|
|
if (aBufLen > aBuf.Length()) {
|
|
|
|
rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2013-08-30 21:16:32 +04:00
|
|
|
rv = DOMParser::ParseFromBuffer(aBuf.Elements(), aBufLen,
|
|
|
|
StringFromSupportedType(aType),
|
|
|
|
getter_AddRefs(domDocument));
|
2012-12-04 05:26:16 +04:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::ParseFromBuffer(const Uint8Array& aBuf, uint32_t aBufLen,
|
|
|
|
SupportedType aType, ErrorResult& rv)
|
2012-12-04 05:26:16 +04: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-28 01:32:41 +04:00
|
|
|
aBuf.ComputeLengthAndData();
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
if (aBufLen > aBuf.Length()) {
|
|
|
|
rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2013-08-30 21:16:32 +04:00
|
|
|
rv = DOMParser::ParseFromBuffer(aBuf.Data(), aBufLen,
|
2013-05-06 23:28:13 +04:00
|
|
|
StringFromSupportedType(aType),
|
2012-12-04 05:26:16 +04:00
|
|
|
getter_AddRefs(domDocument));
|
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
|
|
|
|
2006-04-20 07:39:00 +04:00
|
|
|
NS_IMETHODIMP
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::ParseFromBuffer(const uint8_t *buf,
|
|
|
|
uint32_t bufLen,
|
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2006-04-20 07:39:00 +04:00
|
|
|
{
|
2006-04-20 07:39:02 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(buf);
|
2006-04-26 13:19:48 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2006-04-20 07:39:00 +04:00
|
|
|
|
2006-04-20 07:39:49 +04:00
|
|
|
// The new stream holds a reference to the buffer
|
2006-04-20 07:39:00 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2006-04-20 07:39:49 +04:00
|
|
|
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
2007-07-08 11:08:04 +04:00
|
|
|
reinterpret_cast<const char *>(buf),
|
2006-04-20 07:39:49 +04:00
|
|
|
bufLen, NS_ASSIGNMENT_DEPEND);
|
|
|
|
if (NS_FAILED(rv))
|
2006-04-20 07:39:00 +04:00
|
|
|
return rv;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return ParseFromStream(stream, nullptr, bufLen, contentType, aResult);
|
2006-04-20 07:36:57 +04:00
|
|
|
}
|
|
|
|
|
2006-04-20 07:37:01 +04:00
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
already_AddRefed<nsIDocument>
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::ParseFromStream(nsIInputStream* aStream,
|
|
|
|
const nsAString& aCharset,
|
|
|
|
int32_t aContentLength,
|
|
|
|
SupportedType aType,
|
|
|
|
ErrorResult& rv)
|
2012-12-04 05:26:16 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2013-08-30 21:16:32 +04:00
|
|
|
rv = DOMParser::ParseFromStream(aStream,
|
|
|
|
NS_ConvertUTF16toUTF8(aCharset).get(),
|
|
|
|
aContentLength,
|
|
|
|
StringFromSupportedType(aType),
|
|
|
|
getter_AddRefs(domDocument));
|
2012-12-04 05:26:16 +04:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
return document.forget();
|
|
|
|
}
|
|
|
|
|
2006-04-20 07:36:57 +04:00
|
|
|
NS_IMETHODIMP
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::ParseFromStream(nsIInputStream *stream,
|
|
|
|
const char *charset,
|
|
|
|
int32_t contentLength,
|
|
|
|
const char *contentType,
|
|
|
|
nsIDOMDocument **aResult)
|
2006-04-20 07:36:57 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(stream);
|
2006-04-20 07:39:02 +04:00
|
|
|
NS_ENSURE_ARG(contentType);
|
2006-04-26 13:19:48 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2006-04-20 07:36:57 +04:00
|
|
|
|
2011-10-13 14:50:05 +04:00
|
|
|
bool svg = nsCRT::strcmp(contentType, "image/svg+xml") == 0;
|
|
|
|
|
2006-04-20 07:36:57 +04:00
|
|
|
// For now, we can only create XML documents.
|
2011-10-13 14:50:05 +04:00
|
|
|
//XXXsmaug Should we create an HTMLDocument (in XHTML mode)
|
|
|
|
// for "application/xhtml+xml"?
|
2006-04-20 07:39:02 +04:00
|
|
|
if ((nsCRT::strcmp(contentType, "text/xml") != 0) &&
|
|
|
|
(nsCRT::strcmp(contentType, "application/xml") != 0) &&
|
2011-10-13 14:50:05 +04:00
|
|
|
(nsCRT::strcmp(contentType, "application/xhtml+xml") != 0) &&
|
|
|
|
!svg)
|
2006-04-20 07:36:57 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
2006-04-20 07:38:27 +04:00
|
|
|
nsresult rv;
|
2006-09-09 08:54:03 +04:00
|
|
|
|
|
|
|
// Put the nsCOMPtr out here so we hold a ref to the stream as needed
|
2006-06-16 00:30:44 +04:00
|
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
2006-04-20 07:39:03 +04:00
|
|
|
if (!NS_InputStreamIsBuffered(stream)) {
|
2006-06-16 00:30:44 +04:00
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream,
|
|
|
|
4096);
|
2006-04-20 07:39:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-06-16 00:30:44 +04:00
|
|
|
|
2006-04-20 07:39:03 +04:00
|
|
|
stream = bufferedStream;
|
|
|
|
}
|
2007-10-01 14:02:32 +04:00
|
|
|
|
2006-04-20 07:36:57 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
2012-01-20 16:03:49 +04:00
|
|
|
rv = SetUpDocument(svg ? DocumentFlavorSVG : DocumentFlavorLegacyGuess,
|
|
|
|
getter_AddRefs(domDocument));
|
2006-06-16 00:30:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-04-20 07:36:57 +04:00
|
|
|
|
|
|
|
// Create a fake channel
|
2006-04-20 07:39:49 +04:00
|
|
|
nsCOMPtr<nsIChannel> parserChannel;
|
2014-10-16 22:14:42 +04:00
|
|
|
NS_NewInputStreamChannel(getter_AddRefs(parserChannel),
|
|
|
|
mDocumentURI,
|
|
|
|
nullptr, // aStream
|
|
|
|
mOriginalPrincipal,
|
|
|
|
nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
nsDependentCString(contentType));
|
2006-04-20 07:39:49 +04:00
|
|
|
NS_ENSURE_STATE(parserChannel);
|
2006-04-20 07:36:57 +04:00
|
|
|
|
2006-04-20 07:39:02 +04:00
|
|
|
if (charset) {
|
|
|
|
parserChannel->SetContentCharset(nsDependentCString(charset));
|
|
|
|
}
|
|
|
|
|
2006-04-20 07:36:57 +04:00
|
|
|
// Tell the document to start loading
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
2006-04-20 07:37:35 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
// Have to pass false for reset here, else the reset will remove
|
2006-04-20 07:39:42 +04:00
|
|
|
// our event listener. Should that listener addition move to later
|
2006-09-09 08:54:03 +04:00
|
|
|
// than this call? Then we wouldn't need to mess around with
|
|
|
|
// SetPrincipal, etc, probably!
|
2008-04-25 01:54:42 +04:00
|
|
|
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
|
|
|
|
if (!document) return NS_ERROR_FAILURE;
|
|
|
|
|
2012-01-20 16:03:49 +04:00
|
|
|
// Keep the XULXBL state, base URL and principal setting in sync with the
|
|
|
|
// HTML case
|
|
|
|
|
2010-08-20 03:12:46 +04:00
|
|
|
if (nsContentUtils::IsSystemPrincipal(mOriginalPrincipal)) {
|
|
|
|
document->ForceEnableXULXBL();
|
|
|
|
}
|
|
|
|
|
2006-04-20 07:39:49 +04:00
|
|
|
rv = document->StartDocumentLoad(kLoadAsData, parserChannel,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, nullptr,
|
2006-04-20 07:36:57 +04:00
|
|
|
getter_AddRefs(listener),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2006-04-20 07:36:57 +04:00
|
|
|
|
2008-04-25 01:54:42 +04:00
|
|
|
// Make sure to give this document the right base URI
|
2006-09-09 08:54:03 +04:00
|
|
|
document->SetBaseURI(mBaseURI);
|
2006-04-20 07:39:42 +04:00
|
|
|
|
2008-04-25 01:54:42 +04:00
|
|
|
// And the right principal
|
|
|
|
document->SetPrincipal(mPrincipal);
|
|
|
|
|
2006-04-20 07:37:35 +04:00
|
|
|
if (NS_FAILED(rv) || !listener) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2006-04-20 07:36:57 +04:00
|
|
|
|
|
|
|
// Now start pumping data to the listener
|
|
|
|
nsresult status;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = listener->OnStartRequest(parserChannel, nullptr);
|
2006-04-20 07:39:49 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
parserChannel->Cancel(rv);
|
|
|
|
parserChannel->GetStatus(&status);
|
2006-04-20 07:36:57 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = listener->OnDataAvailable(parserChannel, nullptr, stream, 0,
|
2006-04-20 07:39:49 +04:00
|
|
|
contentLength);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
parserChannel->Cancel(rv);
|
|
|
|
parserChannel->GetStatus(&status);
|
2006-04-20 07:36:57 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = listener->OnStopRequest(parserChannel, nullptr, status);
|
2006-04-20 07:39:49 +04: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 07:38:20 +04:00
|
|
|
|
2006-04-20 07:37:35 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-04-26 13:19:48 +04:00
|
|
|
domDocument.swap(*aResult);
|
2006-04-20 07:36:57 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-09-09 08:54:03 +04:00
|
|
|
NS_IMETHODIMP
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::Init(nsIPrincipal* principal, nsIURI* documentURI,
|
2014-08-21 04:45:04 +04:00
|
|
|
nsIURI* baseURI, nsIGlobalObject* aScriptObject)
|
2006-04-20 07:37:01 +04:00
|
|
|
{
|
2006-09-09 08:54:03 +04:00
|
|
|
NS_ENSURE_STATE(!mAttemptedInit);
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttemptedInit = true;
|
2006-09-09 08:54:03 +04:00
|
|
|
|
|
|
|
NS_ENSURE_ARG(principal || documentURI);
|
2006-04-26 13:19:48 +04:00
|
|
|
|
2006-09-09 08:54:03 +04:00
|
|
|
mDocumentURI = documentURI;
|
2010-08-20 03:06:06 +04:00
|
|
|
|
2006-09-09 08:54:03 +04:00
|
|
|
if (!mDocumentURI) {
|
|
|
|
principal->GetURI(getter_AddRefs(mDocumentURI));
|
2010-08-20 03:06:06 +04:00
|
|
|
// If we have the system principal, then we'll just use the null principals
|
|
|
|
// uri.
|
|
|
|
if (!mDocumentURI && !nsContentUtils::IsSystemPrincipal(principal)) {
|
2006-09-09 08:54:03 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-01 14:02:32 +04:00
|
|
|
mScriptHandlingObject = do_GetWeakReference(aScriptObject);
|
2006-09-09 08:54:03 +04:00
|
|
|
mPrincipal = principal;
|
2008-03-28 06:46:15 +03:00
|
|
|
nsresult rv;
|
2006-09-09 08:54:03 +04:00
|
|
|
if (!mPrincipal) {
|
2010-08-20 03:06:06 +04:00
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ENSURE_TRUE(secMan, NS_ERROR_NOT_AVAILABLE);
|
2008-03-28 06:46:15 +03:00
|
|
|
rv =
|
2012-07-21 11:29:40 +04:00
|
|
|
secMan->GetSimpleCodebasePrincipal(mDocumentURI,
|
|
|
|
getter_AddRefs(mPrincipal));
|
2010-08-20 03:06:06 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-03-28 06:46:15 +03:00
|
|
|
mOriginalPrincipal = mPrincipal;
|
|
|
|
} else {
|
2010-08-20 03:06:06 +04:00
|
|
|
mOriginalPrincipal = mPrincipal;
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(mPrincipal)) {
|
2008-03-28 06:46:15 +03:00
|
|
|
// Don't give DOMParsers the system principal. Use a null
|
|
|
|
// principal instead.
|
2015-04-13 21:47:41 +03:00
|
|
|
mPrincipal = nsNullPrincipal::Create();
|
|
|
|
NS_ENSURE_TRUE(mPrincipal, NS_ERROR_FAILURE);
|
2010-08-20 03:06:06 +04:00
|
|
|
|
|
|
|
if (!mDocumentURI) {
|
|
|
|
rv = mPrincipal->GetURI(getter_AddRefs(mDocumentURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-03-28 06:46:15 +03:00
|
|
|
}
|
2006-09-09 08:54:03 +04: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 13:19:48 +04:00
|
|
|
|
2006-09-09 08:54:03 +04:00
|
|
|
NS_POSTCONDITION(mPrincipal, "Must have principal");
|
2008-03-28 06:46:15 +03:00
|
|
|
NS_POSTCONDITION(mOriginalPrincipal, "Must have original principal");
|
2006-09-09 08:54:03 +04:00
|
|
|
NS_POSTCONDITION(mDocumentURI, "Must have document URI");
|
2006-04-20 07:37:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-09-09 08:54:03 +04:00
|
|
|
|
2013-08-30 21:16:32 +04:00
|
|
|
/*static */already_AddRefed<DOMParser>
|
|
|
|
DOMParser::Constructor(const GlobalObject& aOwner,
|
|
|
|
nsIPrincipal* aPrincipal, nsIURI* aDocumentURI,
|
|
|
|
nsIURI* aBaseURI, ErrorResult& rv)
|
2012-12-04 05:26:16 +04:00
|
|
|
{
|
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
|
|
|
rv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMParser> domParser = new DOMParser(aOwner.GetAsSupports());
|
2013-08-23 09:17:08 +04:00
|
|
|
rv = domParser->InitInternal(aOwner.GetAsSupports(), aPrincipal, aDocumentURI,
|
2012-12-03 20:07:49 +04:00
|
|
|
aBaseURI);
|
2012-12-04 05:26:16 +04:00
|
|
|
if (rv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return domParser.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-30 21:16:32 +04:00
|
|
|
/*static */already_AddRefed<DOMParser>
|
|
|
|
DOMParser::Constructor(const GlobalObject& aOwner,
|
|
|
|
ErrorResult& rv)
|
2006-09-09 08:54:03 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMParser> domParser = new DOMParser(aOwner.GetAsSupports());
|
2014-05-07 02:43:03 +04:00
|
|
|
rv = domParser->InitInternal(aOwner.GetAsSupports(),
|
2014-05-13 13:58:00 +04:00
|
|
|
nsContentUtils::SubjectPrincipal(),
|
2014-05-07 02:43:03 +04:00
|
|
|
nullptr, nullptr);
|
2012-12-04 05:26:16 +04:00
|
|
|
if (rv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return domParser.forget();
|
2012-12-04 05:26:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::InitInternal(nsISupports* aOwner, nsIPrincipal* prin,
|
|
|
|
nsIURI* documentURI, nsIURI* baseURI)
|
2012-12-04 05:26:16 +04:00
|
|
|
{
|
|
|
|
AttemptedInitMarker marker(&mAttemptedInit);
|
2006-09-09 08:54:03 +04: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 11:33:32 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aOwner);
|
2012-12-26 15:00:05 +04:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-12-12 11:33:32 +03:00
|
|
|
}
|
|
|
|
|
2012-12-26 15:00:05 +04:00
|
|
|
baseURI = window->GetDocBaseURI();
|
|
|
|
documentURI = window->GetDocumentURI();
|
|
|
|
if (!documentURI) {
|
2006-09-09 08:54:03 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 04:45:04 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> scriptglobal = do_QueryInterface(aOwner);
|
2007-12-12 11:33:32 +03:00
|
|
|
return Init(prin, documentURI, baseURI, scriptglobal);
|
2006-09-09 08:54:03 +04:00
|
|
|
}
|
|
|
|
|
2012-12-04 05:26:16 +04:00
|
|
|
void
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::Init(nsIPrincipal* aPrincipal, nsIURI* aDocumentURI,
|
|
|
|
nsIURI* aBaseURI, mozilla::ErrorResult& rv)
|
2006-09-09 08:54:03 +04:00
|
|
|
{
|
|
|
|
AttemptedInitMarker marker(&mAttemptedInit);
|
|
|
|
|
2010-08-20 03:06:06 +04:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = aPrincipal;
|
|
|
|
if (!principal && !aDocumentURI) {
|
2014-05-13 13:58:00 +04:00
|
|
|
principal = nsContentUtils::SubjectPrincipal();
|
2010-08-20 03:06:06 +04:00
|
|
|
}
|
|
|
|
|
2014-08-21 04:45:04 +04:00
|
|
|
rv = Init(principal, aDocumentURI, aBaseURI, GetEntryGlobal());
|
2006-09-09 08:54:03 +04:00
|
|
|
}
|
2012-01-20 16:03:49 +04:00
|
|
|
|
|
|
|
nsresult
|
2013-08-30 21:16:32 +04:00
|
|
|
DOMParser::SetUpDocument(DocumentFlavor aFlavor, nsIDOMDocument** aResult)
|
2012-01-20 16:03:49 +04:00
|
|
|
{
|
2014-08-21 04:45:04 +04: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 16:03:49 +04: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 21:47:41 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> prin = nsNullPrincipal::Create();
|
|
|
|
NS_ENSURE_TRUE(prin, NS_ERROR_FAILURE);
|
2012-01-20 16:03:49 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = Init(prin, nullptr, nullptr, scriptHandlingObject);
|
2012-01-20 16:03:49 +04: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 18:05:33 +04:00
|
|
|
return NS_NewDOMDocument(aResult, EmptyString(), EmptyString(), nullptr,
|
|
|
|
mDocumentURI, mBaseURI,
|
|
|
|
mOriginalPrincipal,
|
|
|
|
true,
|
|
|
|
scriptHandlingObject,
|
|
|
|
aFlavor);
|
2012-01-20 16:03:49 +04:00
|
|
|
}
|