nsresult DOMImplementation::CreateDocument(const nsAString& aNamespaceURI, const nsAString& aQualifiedName, nsIDOMDocumentType* aDoctype, nsIDocument** aDocument, nsIDOMDocument** aDOMDocument) { *aDocument = nullptr; *aDOMDocument = nullptr; nsresult rv; if (!aQualifiedName.IsEmpty()) { const nsAFlatString& qName = PromiseFlatString(aQualifiedName); const char16_t *colon; rv = nsContentUtils::CheckQName(qName, true, &colon); NS_ENSURE_SUCCESS(rv, rv); if (colon && (DOMStringIsNull(aNamespaceURI) || (Substring(qName.get(), colon).EqualsLiteral("xml") && !aNamespaceURI.EqualsLiteral("http://www.w3.org/XML/1998/namespace")))) { return NS_ERROR_DOM_NAMESPACE_ERR; } } nsCOMPtr<nsIGlobalObject> scriptHandlingObject = do_QueryReferent(mScriptObject); NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject); nsCOMPtr<nsIDOMDocument> document; rv = NS_NewDOMDocument(getter_AddRefs(document), aNamespaceURI, aQualifiedName, aDoctype, mDocumentURI, mBaseURI, mOwner->NodePrincipal(), true, scriptHandlingObject, DocumentFlavorLegacyGuess); NS_ENSURE_SUCCESS(rv, rv); // When DOMImplementation's createDocument method is invoked with // namespace set to HTML Namespace use the registry of the associated // document to the new instance. nsCOMPtr<nsIDocument> doc = do_QueryInterface(document); if (aNamespaceURI.EqualsLiteral("http://www.w3.org/1999/xhtml")) { doc->UseRegistryFromDocument(mOwner); } doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE); doc.forget(aDocument); document.forget(aDOMDocument); return NS_OK; }
nsresult NS_NewXBLDocument(nsIDOMDocument** aInstancePtrResult, nsIURI* aDocumentURI, nsIURI* aBaseURI, nsIPrincipal* aPrincipal) { nsresult rv = NS_NewDOMDocument(aInstancePtrResult, NS_LITERAL_STRING("http://www.mozilla.org/xbl"), NS_LITERAL_STRING("bindings"), nullptr, aDocumentURI, aBaseURI, aPrincipal, false, nullptr, DocumentFlavorLegacyGuess); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDocument> idoc = do_QueryInterface(*aInstancePtrResult); nsDocument* doc = static_cast<nsDocument*>(idoc.get()); doc->SetLoadedAsInteractiveData(true); doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE); return NS_OK; }
nsresult gfxSVGGlyphsDocument::ParseDocument(const uint8_t *aBuffer, uint32_t aBufLen) { // Mostly pulled from nsDOMParser::ParseFromStream nsCOMPtr<nsIInputStream> stream; nsresult rv = CreateBufferedStream(aBuffer, aBufLen, stream); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIPrincipal> principal = do_CreateInstance("@mozilla.org/nullprincipal;1", &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIURI> uri; principal->GetURI(getter_AddRefs(uri)); nsCOMPtr<nsIDOMDocument> domDoc; rv = NS_NewDOMDocument(getter_AddRefs(domDoc), EmptyString(), // aNamespaceURI EmptyString(), // aQualifiedName nullptr, // aDoctype uri, uri, principal, false, // aLoadedAsData nullptr, // aEventObject DocumentFlavorSVG); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIChannel> channel; rv = NS_NewInputStreamChannel(getter_AddRefs(channel), uri, nullptr /* stream */, SVG_CONTENT_TYPE, UTF8_CHARSET); NS_ENSURE_SUCCESS(rv, rv); channel->SetOwner(principal); nsCOMPtr<nsIDocument> document(do_QueryInterface(domDoc)); if (!document) { return NS_ERROR_FAILURE; } document->SetReadyStateInternal(nsIDocument::READYSTATE_UNINITIALIZED); nsCOMPtr<nsIStreamListener> listener; rv = document->StartDocumentLoad("external-resource", channel, nullptr, // aLoadGroup nullptr, // aContainer getter_AddRefs(listener), true /* aReset */); if (NS_FAILED(rv) || !listener) { return NS_ERROR_FAILURE; } document->SetBaseURI(uri); document->SetPrincipal(principal); rv = listener->OnStartRequest(channel, nullptr /* aContext */); if (NS_FAILED(rv)) { channel->Cancel(rv); } nsresult status; channel->GetStatus(&status); if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) { rv = listener->OnDataAvailable(channel, nullptr /* aContext */, stream, 0, aBufLen); if (NS_FAILED(rv)) { channel->Cancel(rv); } channel->GetStatus(&status); } rv = listener->OnStopRequest(channel, nullptr /* aContext */, status); NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); document.swap(mDocument); return NS_OK; }
NS_IMETHODIMP ImportLoader::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext) { AutoError ae(this); nsIPrincipal* principal = Principal(); nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); if (!channel) { return NS_ERROR_DOM_ABORT_ERR; } if (nsContentUtils::IsSystemPrincipal(principal)) { // We should never import non-system documents and run their scripts with system principal! nsCOMPtr<nsIPrincipal> channelPrincipal; nsContentUtils::GetSecurityManager()->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal)); if (!nsContentUtils::IsSystemPrincipal(channelPrincipal)) { return NS_ERROR_FAILURE; } } channel->SetOwner(principal); nsAutoCString type; channel->GetContentType(type); if (!type.EqualsLiteral("text/html")) { NS_WARNING("ImportLoader wrong content type"); return NS_ERROR_DOM_ABORT_ERR; } // The scope object is same for all the imports in an import tree, // let's get it form the import parent. nsCOMPtr<nsIGlobalObject> global = mImportParent->GetScopeObject(); nsCOMPtr<nsIDOMDocument> importDoc; nsCOMPtr<nsIURI> baseURI = mImportParent->GetBaseURI(); const nsAString& emptyStr = EmptyString(); nsresult rv = NS_NewDOMDocument(getter_AddRefs(importDoc), emptyStr, emptyStr, nullptr, mURI, baseURI, principal, false, global, DocumentFlavorHTML); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); // The imported document must know which master document it belongs to. mDocument = do_QueryInterface(importDoc); nsCOMPtr<nsIDocument> master = mImportParent->MasterDocument(); mDocument->SetMasterDocument(master); // We want to inherit the sandbox flags and fullscreen enabled flag // from the master document. mDocument->SetSandboxFlags(master->GetSandboxFlags()); // We have to connect the blank document we created with the channel we opened, // and create its own LoadGroup for it. nsCOMPtr<nsIStreamListener> listener; nsCOMPtr<nsILoadGroup> loadGroup; channel->GetLoadGroup(getter_AddRefs(loadGroup)); nsCOMPtr<nsILoadGroup> newLoadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID); NS_ENSURE_TRUE(newLoadGroup, NS_ERROR_OUT_OF_MEMORY); newLoadGroup->SetLoadGroup(loadGroup); rv = mDocument->StartDocumentLoad("import", channel, newLoadGroup, nullptr, getter_AddRefs(listener), true); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); nsCOMPtr<nsIURI> originalURI; rv = channel->GetOriginalURI(getter_AddRefs(originalURI)); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); nsCOMPtr<nsIURI> URI; rv = channel->GetURI(getter_AddRefs(URI)); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); MOZ_ASSERT(URI, "URI of a channel should never be null"); bool equals; rv = URI->Equals(originalURI, &equals); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); if (!equals) { // In case of a redirection we must add the new URI to the import map. Manager()->AddLoaderWithNewURI(this, URI); } // Let's start the parser. mParserStreamListener = listener; rv = listener->OnStartRequest(aRequest, aContext); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); ae.Pass(); return NS_OK; }
nsresult DOMImplementation::CreateHTMLDocument(const nsAString& aTitle, nsIDocument** aDocument, nsIDOMDocument** aDOMDocument) { *aDocument = nullptr; *aDOMDocument = nullptr; NS_ENSURE_STATE(mOwner); nsCOMPtr<nsIDOMDocumentType> doctype; // Indicate that there is no internal subset (not just an empty one) nsresult rv = NS_NewDOMDocumentType(getter_AddRefs(doctype), mOwner->NodeInfoManager(), nsGkAtoms::html, // aName EmptyString(), // aPublicId EmptyString(), // aSystemId NullString()); // aInternalSubset NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIGlobalObject> scriptHandlingObject = do_QueryReferent(mScriptObject); NS_ENSURE_STATE(!mScriptObject || scriptHandlingObject); nsCOMPtr<nsIDOMDocument> document; rv = NS_NewDOMDocument(getter_AddRefs(document), EmptyString(), EmptyString(), doctype, mDocumentURI, mBaseURI, mOwner->NodePrincipal(), true, scriptHandlingObject, DocumentFlavorLegacyGuess); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDocument> doc = do_QueryInterface(document); nsCOMPtr<nsIContent> root; rv = doc->CreateElem(NS_LITERAL_STRING("html"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(root)); NS_ENSURE_SUCCESS(rv, rv); rv = doc->AppendChildTo(root, false); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIContent> head; rv = doc->CreateElem(NS_LITERAL_STRING("head"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(head)); NS_ENSURE_SUCCESS(rv, rv); rv = root->AppendChildTo(head, false); NS_ENSURE_SUCCESS(rv, rv); if (!DOMStringIsNull(aTitle)) { nsCOMPtr<nsIContent> title; rv = doc->CreateElem(NS_LITERAL_STRING("title"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(title)); NS_ENSURE_SUCCESS(rv, rv); rv = head->AppendChildTo(title, false); NS_ENSURE_SUCCESS(rv, rv); nsRefPtr<nsTextNode> titleText = new nsTextNode(doc->NodeInfoManager()); rv = titleText->SetText(aTitle, false); NS_ENSURE_SUCCESS(rv, rv); rv = title->AppendChildTo(titleText, false); NS_ENSURE_SUCCESS(rv, rv); } nsCOMPtr<nsIContent> body; rv = doc->CreateElem(NS_LITERAL_STRING("body"), nullptr, kNameSpaceID_XHTML, getter_AddRefs(body)); NS_ENSURE_SUCCESS(rv, rv); rv = root->AppendChildTo(body, false); NS_ENSURE_SUCCESS(rv, rv); doc->SetReadyStateInternal(nsIDocument::READYSTATE_COMPLETE); doc.forget(aDocument); document.forget(aDOMDocument); return NS_OK; }