Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/dom/DOMParser.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIDOMDocument.h" |
michael@0 | 9 | #include "nsNetUtil.h" |
michael@0 | 10 | #include "nsStringStream.h" |
michael@0 | 11 | #include "nsIScriptSecurityManager.h" |
michael@0 | 12 | #include "nsCRT.h" |
michael@0 | 13 | #include "nsStreamUtils.h" |
michael@0 | 14 | #include "nsContentUtils.h" |
michael@0 | 15 | #include "nsDOMJSUtils.h" |
michael@0 | 16 | #include "nsError.h" |
michael@0 | 17 | #include "nsPIDOMWindow.h" |
michael@0 | 18 | #include "mozilla/dom/BindingUtils.h" |
michael@0 | 19 | |
michael@0 | 20 | using namespace mozilla; |
michael@0 | 21 | using namespace mozilla::dom; |
michael@0 | 22 | |
michael@0 | 23 | DOMParser::DOMParser() |
michael@0 | 24 | : mAttemptedInit(false) |
michael@0 | 25 | { |
michael@0 | 26 | SetIsDOMBinding(); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | DOMParser::~DOMParser() |
michael@0 | 30 | { |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | // QueryInterface implementation for DOMParser |
michael@0 | 34 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMParser) |
michael@0 | 35 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
michael@0 | 36 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMParser) |
michael@0 | 37 | NS_INTERFACE_MAP_ENTRY(nsIDOMParser) |
michael@0 | 38 | NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) |
michael@0 | 39 | NS_INTERFACE_MAP_END |
michael@0 | 40 | |
michael@0 | 41 | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DOMParser, mOwner) |
michael@0 | 42 | |
michael@0 | 43 | NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMParser) |
michael@0 | 44 | NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMParser) |
michael@0 | 45 | |
michael@0 | 46 | static const char* |
michael@0 | 47 | StringFromSupportedType(SupportedType aType) |
michael@0 | 48 | { |
michael@0 | 49 | return SupportedTypeValues::strings[static_cast<int>(aType)].value; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | already_AddRefed<nsIDocument> |
michael@0 | 53 | DOMParser::ParseFromString(const nsAString& aStr, SupportedType aType, |
michael@0 | 54 | ErrorResult& rv) |
michael@0 | 55 | { |
michael@0 | 56 | nsCOMPtr<nsIDOMDocument> domDocument; |
michael@0 | 57 | rv = ParseFromString(aStr, |
michael@0 | 58 | StringFromSupportedType(aType), |
michael@0 | 59 | getter_AddRefs(domDocument)); |
michael@0 | 60 | nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument)); |
michael@0 | 61 | return document.forget(); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | NS_IMETHODIMP |
michael@0 | 65 | DOMParser::ParseFromString(const char16_t *str, |
michael@0 | 66 | const char *contentType, |
michael@0 | 67 | nsIDOMDocument **aResult) |
michael@0 | 68 | { |
michael@0 | 69 | NS_ENSURE_ARG(str); |
michael@0 | 70 | // Converting a string to an enum value manually is a bit of a pain, |
michael@0 | 71 | // so let's just use a helper that takes a content-type string. |
michael@0 | 72 | return ParseFromString(nsDependentString(str), contentType, aResult); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | nsresult |
michael@0 | 76 | DOMParser::ParseFromString(const nsAString& str, |
michael@0 | 77 | const char *contentType, |
michael@0 | 78 | nsIDOMDocument **aResult) |
michael@0 | 79 | { |
michael@0 | 80 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 81 | |
michael@0 | 82 | nsresult rv; |
michael@0 | 83 | |
michael@0 | 84 | if (!nsCRT::strcmp(contentType, "text/html")) { |
michael@0 | 85 | nsCOMPtr<nsIDOMDocument> domDocument; |
michael@0 | 86 | rv = SetUpDocument(DocumentFlavorHTML, getter_AddRefs(domDocument)); |
michael@0 | 87 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 88 | nsCOMPtr<nsIDocument> document = do_QueryInterface(domDocument); |
michael@0 | 89 | |
michael@0 | 90 | // Keep the XULXBL state, base URL and principal setting in sync with the |
michael@0 | 91 | // XML case |
michael@0 | 92 | |
michael@0 | 93 | if (nsContentUtils::IsSystemPrincipal(mOriginalPrincipal)) { |
michael@0 | 94 | document->ForceEnableXULXBL(); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | // Make sure to give this document the right base URI |
michael@0 | 98 | document->SetBaseURI(mBaseURI); |
michael@0 | 99 | // And the right principal |
michael@0 | 100 | document->SetPrincipal(mPrincipal); |
michael@0 | 101 | |
michael@0 | 102 | rv = nsContentUtils::ParseDocumentHTML(str, document, false); |
michael@0 | 103 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 104 | |
michael@0 | 105 | domDocument.forget(aResult); |
michael@0 | 106 | return rv; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | nsAutoCString utf8str; |
michael@0 | 110 | // Convert from UTF16 to UTF8 using fallible allocations |
michael@0 | 111 | if (!AppendUTF16toUTF8(str, utf8str, mozilla::fallible_t())) { |
michael@0 | 112 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | // The new stream holds a reference to the buffer |
michael@0 | 116 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 117 | rv = NS_NewByteInputStream(getter_AddRefs(stream), |
michael@0 | 118 | utf8str.get(), utf8str.Length(), |
michael@0 | 119 | NS_ASSIGNMENT_DEPEND); |
michael@0 | 120 | if (NS_FAILED(rv)) |
michael@0 | 121 | return rv; |
michael@0 | 122 | |
michael@0 | 123 | return ParseFromStream(stream, "UTF-8", utf8str.Length(), contentType, aResult); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | already_AddRefed<nsIDocument> |
michael@0 | 127 | DOMParser::ParseFromBuffer(const Sequence<uint8_t>& aBuf, uint32_t aBufLen, |
michael@0 | 128 | SupportedType aType, ErrorResult& rv) |
michael@0 | 129 | { |
michael@0 | 130 | if (aBufLen > aBuf.Length()) { |
michael@0 | 131 | rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY); |
michael@0 | 132 | return nullptr; |
michael@0 | 133 | } |
michael@0 | 134 | nsCOMPtr<nsIDOMDocument> domDocument; |
michael@0 | 135 | rv = DOMParser::ParseFromBuffer(aBuf.Elements(), aBufLen, |
michael@0 | 136 | StringFromSupportedType(aType), |
michael@0 | 137 | getter_AddRefs(domDocument)); |
michael@0 | 138 | nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument)); |
michael@0 | 139 | return document.forget(); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | already_AddRefed<nsIDocument> |
michael@0 | 143 | DOMParser::ParseFromBuffer(const Uint8Array& aBuf, uint32_t aBufLen, |
michael@0 | 144 | SupportedType aType, ErrorResult& rv) |
michael@0 | 145 | { |
michael@0 | 146 | aBuf.ComputeLengthAndData(); |
michael@0 | 147 | |
michael@0 | 148 | if (aBufLen > aBuf.Length()) { |
michael@0 | 149 | rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY); |
michael@0 | 150 | return nullptr; |
michael@0 | 151 | } |
michael@0 | 152 | nsCOMPtr<nsIDOMDocument> domDocument; |
michael@0 | 153 | rv = DOMParser::ParseFromBuffer(aBuf.Data(), aBufLen, |
michael@0 | 154 | StringFromSupportedType(aType), |
michael@0 | 155 | getter_AddRefs(domDocument)); |
michael@0 | 156 | nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument)); |
michael@0 | 157 | return document.forget(); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | NS_IMETHODIMP |
michael@0 | 161 | DOMParser::ParseFromBuffer(const uint8_t *buf, |
michael@0 | 162 | uint32_t bufLen, |
michael@0 | 163 | const char *contentType, |
michael@0 | 164 | nsIDOMDocument **aResult) |
michael@0 | 165 | { |
michael@0 | 166 | NS_ENSURE_ARG_POINTER(buf); |
michael@0 | 167 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 168 | |
michael@0 | 169 | // The new stream holds a reference to the buffer |
michael@0 | 170 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 171 | nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), |
michael@0 | 172 | reinterpret_cast<const char *>(buf), |
michael@0 | 173 | bufLen, NS_ASSIGNMENT_DEPEND); |
michael@0 | 174 | if (NS_FAILED(rv)) |
michael@0 | 175 | return rv; |
michael@0 | 176 | |
michael@0 | 177 | return ParseFromStream(stream, nullptr, bufLen, contentType, aResult); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | |
michael@0 | 181 | already_AddRefed<nsIDocument> |
michael@0 | 182 | DOMParser::ParseFromStream(nsIInputStream* aStream, |
michael@0 | 183 | const nsAString& aCharset, |
michael@0 | 184 | int32_t aContentLength, |
michael@0 | 185 | SupportedType aType, |
michael@0 | 186 | ErrorResult& rv) |
michael@0 | 187 | { |
michael@0 | 188 | nsCOMPtr<nsIDOMDocument> domDocument; |
michael@0 | 189 | rv = DOMParser::ParseFromStream(aStream, |
michael@0 | 190 | NS_ConvertUTF16toUTF8(aCharset).get(), |
michael@0 | 191 | aContentLength, |
michael@0 | 192 | StringFromSupportedType(aType), |
michael@0 | 193 | getter_AddRefs(domDocument)); |
michael@0 | 194 | nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument)); |
michael@0 | 195 | return document.forget(); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | NS_IMETHODIMP |
michael@0 | 199 | DOMParser::ParseFromStream(nsIInputStream *stream, |
michael@0 | 200 | const char *charset, |
michael@0 | 201 | int32_t contentLength, |
michael@0 | 202 | const char *contentType, |
michael@0 | 203 | nsIDOMDocument **aResult) |
michael@0 | 204 | { |
michael@0 | 205 | NS_ENSURE_ARG(stream); |
michael@0 | 206 | NS_ENSURE_ARG(contentType); |
michael@0 | 207 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 208 | *aResult = nullptr; |
michael@0 | 209 | |
michael@0 | 210 | bool svg = nsCRT::strcmp(contentType, "image/svg+xml") == 0; |
michael@0 | 211 | |
michael@0 | 212 | // For now, we can only create XML documents. |
michael@0 | 213 | //XXXsmaug Should we create an HTMLDocument (in XHTML mode) |
michael@0 | 214 | // for "application/xhtml+xml"? |
michael@0 | 215 | if ((nsCRT::strcmp(contentType, "text/xml") != 0) && |
michael@0 | 216 | (nsCRT::strcmp(contentType, "application/xml") != 0) && |
michael@0 | 217 | (nsCRT::strcmp(contentType, "application/xhtml+xml") != 0) && |
michael@0 | 218 | !svg) |
michael@0 | 219 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 220 | |
michael@0 | 221 | nsresult rv; |
michael@0 | 222 | |
michael@0 | 223 | // Put the nsCOMPtr out here so we hold a ref to the stream as needed |
michael@0 | 224 | nsCOMPtr<nsIInputStream> bufferedStream; |
michael@0 | 225 | if (!NS_InputStreamIsBuffered(stream)) { |
michael@0 | 226 | rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream, |
michael@0 | 227 | 4096); |
michael@0 | 228 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 229 | |
michael@0 | 230 | stream = bufferedStream; |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | nsCOMPtr<nsIDOMDocument> domDocument; |
michael@0 | 234 | rv = SetUpDocument(svg ? DocumentFlavorSVG : DocumentFlavorLegacyGuess, |
michael@0 | 235 | getter_AddRefs(domDocument)); |
michael@0 | 236 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 237 | |
michael@0 | 238 | // Create a fake channel |
michael@0 | 239 | nsCOMPtr<nsIChannel> parserChannel; |
michael@0 | 240 | NS_NewInputStreamChannel(getter_AddRefs(parserChannel), mDocumentURI, nullptr, |
michael@0 | 241 | nsDependentCString(contentType), nullptr); |
michael@0 | 242 | NS_ENSURE_STATE(parserChannel); |
michael@0 | 243 | |
michael@0 | 244 | // More principal-faking here |
michael@0 | 245 | parserChannel->SetOwner(mOriginalPrincipal); |
michael@0 | 246 | |
michael@0 | 247 | if (charset) { |
michael@0 | 248 | parserChannel->SetContentCharset(nsDependentCString(charset)); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | // Tell the document to start loading |
michael@0 | 252 | nsCOMPtr<nsIStreamListener> listener; |
michael@0 | 253 | |
michael@0 | 254 | // Have to pass false for reset here, else the reset will remove |
michael@0 | 255 | // our event listener. Should that listener addition move to later |
michael@0 | 256 | // than this call? Then we wouldn't need to mess around with |
michael@0 | 257 | // SetPrincipal, etc, probably! |
michael@0 | 258 | nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument)); |
michael@0 | 259 | if (!document) return NS_ERROR_FAILURE; |
michael@0 | 260 | |
michael@0 | 261 | // Keep the XULXBL state, base URL and principal setting in sync with the |
michael@0 | 262 | // HTML case |
michael@0 | 263 | |
michael@0 | 264 | if (nsContentUtils::IsSystemPrincipal(mOriginalPrincipal)) { |
michael@0 | 265 | document->ForceEnableXULXBL(); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | rv = document->StartDocumentLoad(kLoadAsData, parserChannel, |
michael@0 | 269 | nullptr, nullptr, |
michael@0 | 270 | getter_AddRefs(listener), |
michael@0 | 271 | false); |
michael@0 | 272 | |
michael@0 | 273 | // Make sure to give this document the right base URI |
michael@0 | 274 | document->SetBaseURI(mBaseURI); |
michael@0 | 275 | |
michael@0 | 276 | // And the right principal |
michael@0 | 277 | document->SetPrincipal(mPrincipal); |
michael@0 | 278 | |
michael@0 | 279 | if (NS_FAILED(rv) || !listener) { |
michael@0 | 280 | return NS_ERROR_FAILURE; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | // Now start pumping data to the listener |
michael@0 | 284 | nsresult status; |
michael@0 | 285 | |
michael@0 | 286 | rv = listener->OnStartRequest(parserChannel, nullptr); |
michael@0 | 287 | if (NS_FAILED(rv)) |
michael@0 | 288 | parserChannel->Cancel(rv); |
michael@0 | 289 | parserChannel->GetStatus(&status); |
michael@0 | 290 | |
michael@0 | 291 | if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) { |
michael@0 | 292 | rv = listener->OnDataAvailable(parserChannel, nullptr, stream, 0, |
michael@0 | 293 | contentLength); |
michael@0 | 294 | if (NS_FAILED(rv)) |
michael@0 | 295 | parserChannel->Cancel(rv); |
michael@0 | 296 | parserChannel->GetStatus(&status); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | rv = listener->OnStopRequest(parserChannel, nullptr, status); |
michael@0 | 300 | // Failure returned from OnStopRequest does not affect the final status of |
michael@0 | 301 | // the channel, so we do not need to call Cancel(rv) as we do above. |
michael@0 | 302 | |
michael@0 | 303 | if (NS_FAILED(rv)) { |
michael@0 | 304 | return NS_ERROR_FAILURE; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | domDocument.swap(*aResult); |
michael@0 | 308 | |
michael@0 | 309 | return NS_OK; |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | NS_IMETHODIMP |
michael@0 | 313 | DOMParser::Init(nsIPrincipal* principal, nsIURI* documentURI, |
michael@0 | 314 | nsIURI* baseURI, nsIScriptGlobalObject* aScriptObject) |
michael@0 | 315 | { |
michael@0 | 316 | NS_ENSURE_STATE(!mAttemptedInit); |
michael@0 | 317 | mAttemptedInit = true; |
michael@0 | 318 | |
michael@0 | 319 | NS_ENSURE_ARG(principal || documentURI); |
michael@0 | 320 | |
michael@0 | 321 | mDocumentURI = documentURI; |
michael@0 | 322 | |
michael@0 | 323 | if (!mDocumentURI) { |
michael@0 | 324 | principal->GetURI(getter_AddRefs(mDocumentURI)); |
michael@0 | 325 | // If we have the system principal, then we'll just use the null principals |
michael@0 | 326 | // uri. |
michael@0 | 327 | if (!mDocumentURI && !nsContentUtils::IsSystemPrincipal(principal)) { |
michael@0 | 328 | return NS_ERROR_INVALID_ARG; |
michael@0 | 329 | } |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | mScriptHandlingObject = do_GetWeakReference(aScriptObject); |
michael@0 | 333 | mPrincipal = principal; |
michael@0 | 334 | nsresult rv; |
michael@0 | 335 | if (!mPrincipal) { |
michael@0 | 336 | nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager(); |
michael@0 | 337 | NS_ENSURE_TRUE(secMan, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 338 | rv = |
michael@0 | 339 | secMan->GetSimpleCodebasePrincipal(mDocumentURI, |
michael@0 | 340 | getter_AddRefs(mPrincipal)); |
michael@0 | 341 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 342 | mOriginalPrincipal = mPrincipal; |
michael@0 | 343 | } else { |
michael@0 | 344 | mOriginalPrincipal = mPrincipal; |
michael@0 | 345 | if (nsContentUtils::IsSystemPrincipal(mPrincipal)) { |
michael@0 | 346 | // Don't give DOMParsers the system principal. Use a null |
michael@0 | 347 | // principal instead. |
michael@0 | 348 | mPrincipal = do_CreateInstance("@mozilla.org/nullprincipal;1", &rv); |
michael@0 | 349 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 350 | |
michael@0 | 351 | if (!mDocumentURI) { |
michael@0 | 352 | rv = mPrincipal->GetURI(getter_AddRefs(mDocumentURI)); |
michael@0 | 353 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 354 | } |
michael@0 | 355 | } |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | mBaseURI = baseURI; |
michael@0 | 359 | // Note: if mBaseURI is null, fine. Leave it like that; that will use the |
michael@0 | 360 | // documentURI as the base. Otherwise for null principals we'll get |
michael@0 | 361 | // nsDocument::SetBaseURI giving errors. |
michael@0 | 362 | |
michael@0 | 363 | NS_POSTCONDITION(mPrincipal, "Must have principal"); |
michael@0 | 364 | NS_POSTCONDITION(mOriginalPrincipal, "Must have original principal"); |
michael@0 | 365 | NS_POSTCONDITION(mDocumentURI, "Must have document URI"); |
michael@0 | 366 | return NS_OK; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | /*static */already_AddRefed<DOMParser> |
michael@0 | 370 | DOMParser::Constructor(const GlobalObject& aOwner, |
michael@0 | 371 | nsIPrincipal* aPrincipal, nsIURI* aDocumentURI, |
michael@0 | 372 | nsIURI* aBaseURI, ErrorResult& rv) |
michael@0 | 373 | { |
michael@0 | 374 | if (!nsContentUtils::IsCallerChrome()) { |
michael@0 | 375 | rv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
michael@0 | 376 | return nullptr; |
michael@0 | 377 | } |
michael@0 | 378 | nsRefPtr<DOMParser> domParser = new DOMParser(aOwner.GetAsSupports()); |
michael@0 | 379 | rv = domParser->InitInternal(aOwner.GetAsSupports(), aPrincipal, aDocumentURI, |
michael@0 | 380 | aBaseURI); |
michael@0 | 381 | if (rv.Failed()) { |
michael@0 | 382 | return nullptr; |
michael@0 | 383 | } |
michael@0 | 384 | return domParser.forget(); |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | /*static */already_AddRefed<DOMParser> |
michael@0 | 388 | DOMParser::Constructor(const GlobalObject& aOwner, |
michael@0 | 389 | ErrorResult& rv) |
michael@0 | 390 | { |
michael@0 | 391 | nsCOMPtr<nsIPrincipal> prin; |
michael@0 | 392 | nsCOMPtr<nsIURI> documentURI; |
michael@0 | 393 | nsCOMPtr<nsIURI> baseURI; |
michael@0 | 394 | // No arguments; use the subject principal |
michael@0 | 395 | nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager(); |
michael@0 | 396 | if (!secMan) { |
michael@0 | 397 | rv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 398 | return nullptr; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | rv = secMan->GetSubjectPrincipal(getter_AddRefs(prin)); |
michael@0 | 402 | if (rv.Failed()) { |
michael@0 | 403 | return nullptr; |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | // We're called from JS; there better be a subject principal, really. |
michael@0 | 407 | if (!prin) { |
michael@0 | 408 | rv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 409 | return nullptr; |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | nsRefPtr<DOMParser> domParser = new DOMParser(aOwner.GetAsSupports()); |
michael@0 | 413 | rv = domParser->InitInternal(aOwner.GetAsSupports(), prin, documentURI, baseURI); |
michael@0 | 414 | if (rv.Failed()) { |
michael@0 | 415 | return nullptr; |
michael@0 | 416 | } |
michael@0 | 417 | return domParser.forget(); |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | nsresult |
michael@0 | 421 | DOMParser::InitInternal(nsISupports* aOwner, nsIPrincipal* prin, |
michael@0 | 422 | nsIURI* documentURI, nsIURI* baseURI) |
michael@0 | 423 | { |
michael@0 | 424 | AttemptedInitMarker marker(&mAttemptedInit); |
michael@0 | 425 | if (!documentURI) { |
michael@0 | 426 | // No explicit documentURI; grab document and base URIs off the window our |
michael@0 | 427 | // constructor was called on. Error out if anything untoward happens. |
michael@0 | 428 | |
michael@0 | 429 | // Note that this is a behavior change as far as I can tell -- we're now |
michael@0 | 430 | // using the base URI and document URI of the window off of which the |
michael@0 | 431 | // DOMParser is created, not the window in which parse*() is called. |
michael@0 | 432 | // Does that matter? |
michael@0 | 433 | |
michael@0 | 434 | // Also note that |cx| matches what GetDocumentFromContext() would return, |
michael@0 | 435 | // while GetDocumentFromCaller() gives us the window that the DOMParser() |
michael@0 | 436 | // call was made on. |
michael@0 | 437 | |
michael@0 | 438 | nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aOwner); |
michael@0 | 439 | if (!window) { |
michael@0 | 440 | return NS_ERROR_UNEXPECTED; |
michael@0 | 441 | } |
michael@0 | 442 | |
michael@0 | 443 | baseURI = window->GetDocBaseURI(); |
michael@0 | 444 | documentURI = window->GetDocumentURI(); |
michael@0 | 445 | if (!documentURI) { |
michael@0 | 446 | return NS_ERROR_UNEXPECTED; |
michael@0 | 447 | } |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | nsCOMPtr<nsIScriptGlobalObject> scriptglobal = do_QueryInterface(aOwner); |
michael@0 | 451 | return Init(prin, documentURI, baseURI, scriptglobal); |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | void |
michael@0 | 455 | DOMParser::Init(nsIPrincipal* aPrincipal, nsIURI* aDocumentURI, |
michael@0 | 456 | nsIURI* aBaseURI, mozilla::ErrorResult& rv) |
michael@0 | 457 | { |
michael@0 | 458 | AttemptedInitMarker marker(&mAttemptedInit); |
michael@0 | 459 | |
michael@0 | 460 | JSContext *cx = nsContentUtils::GetCurrentJSContext(); |
michael@0 | 461 | if (!cx) { |
michael@0 | 462 | rv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 463 | return; |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | nsIScriptContext* scriptContext = GetScriptContextFromJSContext(cx); |
michael@0 | 467 | |
michael@0 | 468 | nsCOMPtr<nsIPrincipal> principal = aPrincipal; |
michael@0 | 469 | |
michael@0 | 470 | if (!principal && !aDocumentURI) { |
michael@0 | 471 | nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager(); |
michael@0 | 472 | if (!secMan) { |
michael@0 | 473 | rv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 474 | return; |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | rv = secMan->GetSubjectPrincipal(getter_AddRefs(principal)); |
michael@0 | 478 | if (rv.Failed()) { |
michael@0 | 479 | return; |
michael@0 | 480 | } |
michael@0 | 481 | |
michael@0 | 482 | // We're called from JS; there better be a subject principal, really. |
michael@0 | 483 | if (!principal) { |
michael@0 | 484 | rv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 485 | return; |
michael@0 | 486 | } |
michael@0 | 487 | } |
michael@0 | 488 | |
michael@0 | 489 | rv = Init(principal, aDocumentURI, aBaseURI, |
michael@0 | 490 | scriptContext ? scriptContext->GetGlobalObject() : nullptr); |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | nsresult |
michael@0 | 494 | DOMParser::SetUpDocument(DocumentFlavor aFlavor, nsIDOMDocument** aResult) |
michael@0 | 495 | { |
michael@0 | 496 | nsCOMPtr<nsIScriptGlobalObject> scriptHandlingObject = |
michael@0 | 497 | do_QueryReferent(mScriptHandlingObject); |
michael@0 | 498 | nsresult rv; |
michael@0 | 499 | if (!mPrincipal) { |
michael@0 | 500 | NS_ENSURE_TRUE(!mAttemptedInit, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 501 | AttemptedInitMarker marker(&mAttemptedInit); |
michael@0 | 502 | |
michael@0 | 503 | nsCOMPtr<nsIPrincipal> prin = |
michael@0 | 504 | do_CreateInstance("@mozilla.org/nullprincipal;1", &rv); |
michael@0 | 505 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 506 | |
michael@0 | 507 | rv = Init(prin, nullptr, nullptr, scriptHandlingObject); |
michael@0 | 508 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | NS_ASSERTION(mPrincipal, "Must have principal by now"); |
michael@0 | 512 | NS_ASSERTION(mDocumentURI, "Must have document URI by now"); |
michael@0 | 513 | |
michael@0 | 514 | // Here we have to cheat a little bit... Setting the base URI won't |
michael@0 | 515 | // work if the document has a null principal, so use |
michael@0 | 516 | // mOriginalPrincipal when creating the document, then reset the |
michael@0 | 517 | // principal. |
michael@0 | 518 | return NS_NewDOMDocument(aResult, EmptyString(), EmptyString(), nullptr, |
michael@0 | 519 | mDocumentURI, mBaseURI, |
michael@0 | 520 | mOriginalPrincipal, |
michael@0 | 521 | true, |
michael@0 | 522 | scriptHandlingObject, |
michael@0 | 523 | aFlavor); |
michael@0 | 524 | } |