Files
palemoon27/dom/xul/templates/nsXULTemplateQueryProcessorXML.cpp
T
roytam1 444160f7b2 import changes from `dev' branch of rmottola/Arctic-Fox:
- align permissions to gecko (2b5293932d)
- Bug 1214759 - backport SkDashPathEffect::asPoints fix from upstream Skia. r=jmuizelaar (4f58a0ef9a)
- Bug 1216020 - Comment all ALLOW_COMPILER_WARNINGS lines. r=glandium. (30a485d631)
- bug 1174747 - Fix sqlite3 build on iOS. r=glandium (ad59695677)
- Bug 1229615 - Conditionalize mp4parse.h export. r=kinetik (f7e95a1afa)
- Bug 1211741 - Remove libstagefright ID3 files. r=k17e (016da13fed)
- Bug 1202266 - Suppress '-Wformat-security' in libstagefright CXXFLAGS. r=kentuckyfriedtakahe Bug 1143336 - Fix and suppress -Wunreachable-code-return warnings in media code. r=cajbir (f8f58de405)
- Bug 1225923 - part 1 - convert all needs-to-copy instances of AppendElement(nsDependentString(...)); r=erahm (e061f9cb53)
- Bug 1225923 - part 2 - fix AppendElement(nsDependentString(...)) call in HTMLInputElement.cpp; r=baku (69d4b4c288)
- Bug 1179089 - Let TelephonyRequestParent hold the callback objects. r=hsinyi (05a6d68415)
- Bug 1225923 - part 3 - fix AppendElement(nsDependentString(...)) calls in dom/telephony/; r=btseng (31cb1e8cb0)
- Bug 1225923 - part 4 - fix AppendElement(nsDependentCString(...)) call in nsClipboardProxy.cpp; r=roc (978e7bfe19)
- Bug 1225923 - part 5 - fix AppendElement(nsDependentAtomString(...)) calls; r=bz (82c93922bb)
- fix (761e252105)
- Bug 1187114 (part 1) - Replace nsBaseHashtable::EnumerateRead() calls in dom/xul/ with iterators. r=mccr8. (12b2947f5b)
- Bug 1187114 (part 2) - Replace nsBaseHashtable::EnumerateRead() calls in dom/xul/ with iterators. r=mccr8. (db1ad0cd0f)
- Bug 1187114 (part 3) - Replace nsBaseHashtable::EnumerateRead() calls in dom/xul/ with iterators. r=mccr8. (0b46fdbb38)
- Bug 1137009 - Do not persist xul window attributes when in fullscreen. r=enndeakin (6bef2373dc)
- Bug 1206958 - Use channel->asyncOpen2() in dom/xul/XULDocument.cpp (r=sicking) (21b1a90678)
- Bug 1216945 - Uninitialised value use in nsXULWindow::SizeShellTo. r=bzbarsky. (989ce9ff88)
- Bug 1189132 - Skip unmap during window destruction on GTK, working around talos hang with GL layers. r=karlt (34f29cb72b)
- Bug 1206964 - Use channel->AsyncOpen2() in netwerk/base/nsNetUtil.cpp (r=sicking) (fbb5158a68)
- Bug 1225682 - Don't use nsAuto{,C}String as class member variables in xpfe/. r=neil (bd453fc5f5)
- Bug 1221992 - Prevent ServiceWorkerClients.OpenWindow from opening tabs in private mode windows. r=smaug (f7da0cf34c)
- Bug 1034922 - Make nsSiteWindow's destructor private, and expose it to nsContentTreeOwner via friend class. r=Enn (87d8dfa052)
- Bug 1214174 - Windowless WebBrowserChrome2Stub should implement nsIEmbeddingSiteWindow GetDimensions. r=billm (4f4b44532d)
- Bug 1214174 - Add windowless browser test case. r=billm (2ece018afd)
- Bug 1160770 - Unnecessary return in tabScrolling property in XPFE autocomplete.xml NPOTB r=Neil DONTBUILD (7fe5f13994)
- Bug 1192943 - Use channel->asycnOpen2 in xpfe/components/directory/nsDirectoryViewer.cpp (r=sicking) (79c2f93134)
- Bug 1186790 - Replace nsBaseHashtable::EnumerateRead() calls in xpfe/with iterators. r=neil. (ee73ea015d)
- Bug 1232450 - use UniquePtr<T[]> instead of nsAutoArrayPtr<T> in xulrunner/; r=Yoric (ffa7a84864)
- Bug 1174011, fix xulrunner-stub compile, r=gladium DONTBUILD (bb9381a707)
- Bug 1216723 - Add a new -forbid- Safe Browsing list type. r=gcp,r=smaug (9f164a56d8)
- cleanup, do not run PP (0d7c0cd4c0)
- Bug 1204338 Followup to Bug 403800 Refactor netError.dtd/netErrorApp.dtd to simplify per-application overrides r=Pike (5e1bbcb45c)
- Bug 1154347 - Don't set -fomit-frame-pointer on all of skia. r=glandium, r=gw280 (af8db2b7b9)
- Bug 1136958 - Remove duplicate SkDiscardableMemory_none.cpp from gfx/skia/moz.build. r=gps (9bbebeecc4)
- bits of  Bug 1188462 - Rename gfx/skia/trun (d839e73b23)
- Bug 1215899 - Suppress clang and gcc warnings in gfx/skia. r=gw280 (e7859525d0)
- Bug 1222166 - use gcc/clang warning flags for clang-cl in moz.build files; r=glandium (b885ca1dff)
- Bug 1082598 - Part 1: Update Skia to master revision 53c5d5fb795fe04bec050c0583223027c25b839b. r=jrmuizel (4baf696313)
- Bug 1082598 - Part 2: Fix Moz2D Skia usage for Skia update. r=jrmuizel (d6192e1b0d)
- Bug 1082598 - Part 3: Fix thebes gfxPlatform shutdown for Skia. r=jrmuizel (ecaf498adf)
- Bug 1082598 - Part 6: Fix layers for Skia update. r=jrmuizel (e80b4e204c)
- Bug 1082598 - Part 7: Update moz.build for Skia update. r=jrmuizel (121c4c6e21)
- Bug 1082598 - Part 8: Fix OpenGL interface glue for Skia update. r=jrmuizel (327cebb260)
- Bug 1082598 - Part 9: Fix test failures for Skia update. r=jrmuizel (1d0a6a6a9a)
- missing bit of Bug 1082598 - Part 1: Update Skia (559cb5a38a)
- cleanup strange misspatch (5d759aadaf)
2023-06-14 15:12:12 +08:00

449 lines
14 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsIDOMDocument.h"
#include "nsIDOMNode.h"
#include "nsIDOMElement.h"
#include "nsIDOMEvent.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsComponentManagerUtils.h"
#include "nsGkAtoms.h"
#include "nsIURI.h"
#include "nsIArray.h"
#include "nsIScriptContext.h"
#include "nsArrayUtils.h"
#include "nsPIDOMWindow.h"
#include "nsXULContentUtils.h"
#include "nsXMLHttpRequest.h"
#include "mozilla/dom/XPathEvaluator.h"
#include "nsXULTemplateQueryProcessorXML.h"
#include "nsXULTemplateResultXML.h"
#include "nsXULSortService.h"
#include "mozilla/dom/Element.h"
using namespace mozilla;
using namespace mozilla::dom;
NS_IMPL_ISUPPORTS(nsXMLQuery, nsXMLQuery)
//----------------------------------------------------------------------
//
// nsXULTemplateResultSetXML
//
NS_IMPL_ISUPPORTS(nsXULTemplateResultSetXML, nsISimpleEnumerator)
NS_IMETHODIMP
nsXULTemplateResultSetXML::HasMoreElements(bool *aResult)
{
// if GetSnapshotLength failed, then the return type was not a set of
// nodes, so just return false in this case.
ErrorResult rv;
uint32_t length = mResults->GetSnapshotLength(rv);
*aResult = !rv.Failed() && mPosition < length;
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateResultSetXML::GetNext(nsISupports **aResult)
{
ErrorResult rv;
nsINode* node = mResults->SnapshotItem(mPosition, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
nsXULTemplateResultXML* result =
new nsXULTemplateResultXML(mQuery, node->AsContent(), mBindingSet);
++mPosition;
*aResult = result;
NS_ADDREF(result);
return NS_OK;
}
//----------------------------------------------------------------------
//
// nsXULTemplateQueryProcessorXML
//
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULTemplateQueryProcessorXML)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULTemplateQueryProcessorXML)
tmp->mRuleToBindingsMap.Clear();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEvaluator)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTemplateBuilder)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRequest)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULTemplateQueryProcessorXML)
for (auto it = tmp->mRuleToBindingsMap.Iter(); !it.Done(); it.Next()) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRuleToBindingsMap key");
cb.NoteXPCOMChild(it.Key());
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEvaluator)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTemplateBuilder)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRequest)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULTemplateQueryProcessorXML)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULTemplateQueryProcessorXML)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULTemplateQueryProcessorXML)
NS_INTERFACE_MAP_ENTRY(nsIXULTemplateQueryProcessor)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXULTemplateQueryProcessor)
NS_INTERFACE_MAP_END
/*
* Only the first datasource in aDataSource is used, which should be either an
* nsIURI of an XML document, or a DOM node. If the former, GetDatasource will
* load the document asynchronously and return null in aResult. Once the
* document has loaded, the builder's datasource will be set to the XML
* document. If the datasource is a DOM node, the node will be returned in
* aResult.
*/
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::GetDatasource(nsIArray* aDataSources,
nsIDOMNode* aRootNode,
bool aIsTrusted,
nsIXULTemplateBuilder* aBuilder,
bool* aShouldDelayBuilding,
nsISupports** aResult)
{
*aResult = nullptr;
*aShouldDelayBuilding = false;
nsresult rv;
uint32_t length;
aDataSources->GetLength(&length);
if (length == 0)
return NS_OK;
// we get only the first item, because the query processor supports only
// one document as a datasource
nsCOMPtr<nsIDOMNode> node = do_QueryElementAt(aDataSources, 0);
if (node) {
return CallQueryInterface(node, aResult);
}
nsCOMPtr<nsIURI> uri = do_QueryElementAt(aDataSources, 0);
if (!uri)
return NS_ERROR_UNEXPECTED;
nsAutoCString uriStr;
rv = uri->GetSpec(uriStr);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIContent> root = do_QueryInterface(aRootNode);
if (!root)
return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIDocument> doc = root->GetUncomposedDoc();
if (!doc)
return NS_ERROR_UNEXPECTED;
nsIPrincipal *docPrincipal = doc->NodePrincipal();
bool hasHadScriptObject = true;
nsIScriptGlobalObject* scriptObject =
doc->GetScriptHandlingObject(hasHadScriptObject);
NS_ENSURE_STATE(scriptObject);
nsIScriptContext *context = scriptObject->GetContext();
NS_ENSURE_TRUE(context, NS_OK);
nsCOMPtr<nsIXMLHttpRequest> req =
do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = req->Init(docPrincipal, context,
scriptObject ? scriptObject : doc->GetScopeObject(),
nullptr, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
rv = req->Open(NS_LITERAL_CSTRING("GET"), uriStr, true,
EmptyString(), EmptyString());
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<EventTarget> target(do_QueryInterface(req));
rv = target->AddEventListener(NS_LITERAL_STRING("load"), this, false);
NS_ENSURE_SUCCESS(rv, rv);
rv = target->AddEventListener(NS_LITERAL_STRING("error"), this, false);
NS_ENSURE_SUCCESS(rv, rv);
rv = req->Send(nullptr);
NS_ENSURE_SUCCESS(rv, rv);
mTemplateBuilder = aBuilder;
mRequest = req;
*aShouldDelayBuilding = true;
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::InitializeForBuilding(nsISupports* aDatasource,
nsIXULTemplateBuilder* aBuilder,
nsIDOMNode* aRootNode)
{
if (mGenerationStarted)
return NS_ERROR_UNEXPECTED;
// the datasource is either a document or a DOM element
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDatasource);
if (doc)
mRoot = doc->GetDocumentElement();
else
mRoot = do_QueryInterface(aDatasource);
NS_ENSURE_STATE(mRoot);
mEvaluator = new XPathEvaluator();
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::Done()
{
mGenerationStarted = false;
mRuleToBindingsMap.Clear();
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::CompileQuery(nsIXULTemplateBuilder* aBuilder,
nsIDOMNode* aQueryNode,
nsIAtom* aRefVariable,
nsIAtom* aMemberVariable,
nsISupports** _retval)
{
*_retval = nullptr;
nsCOMPtr<nsIContent> content = do_QueryInterface(aQueryNode);
nsAutoString expr;
content->GetAttr(kNameSpaceID_None, nsGkAtoms::expr, expr);
// if an expression is not specified, then the default is to
// just take all of the children
if (expr.IsEmpty())
expr.Assign('*');
ErrorResult rv;
nsAutoPtr<XPathExpression> compiledexpr;
compiledexpr = CreateExpression(expr, content, rv);
if (rv.Failed()) {
nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_BAD_XPATH);
return rv.StealNSResult();
}
RefPtr<nsXMLQuery> query =
new nsXMLQuery(this, aMemberVariable, Move(compiledexpr));
for (nsIContent* condition = content->GetFirstChild();
condition;
condition = condition->GetNextSibling()) {
if (condition->NodeInfo()->Equals(nsGkAtoms::assign,
kNameSpaceID_XUL)) {
nsAutoString var;
condition->GetAttr(kNameSpaceID_None, nsGkAtoms::var, var);
nsAutoString expr;
condition->GetAttr(kNameSpaceID_None, nsGkAtoms::expr, expr);
// ignore assignments without a variable or an expression
if (!var.IsEmpty() && !expr.IsEmpty()) {
compiledexpr = CreateExpression(expr, condition, rv);
if (rv.Failed()) {
nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_BAD_ASSIGN_XPATH);
return rv.StealNSResult();
}
nsCOMPtr<nsIAtom> varatom = do_GetAtom(var);
query->AddBinding(varatom, Move(compiledexpr));
}
}
}
query.forget(_retval);
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::GenerateResults(nsISupports* aDatasource,
nsIXULTemplateResult* aRef,
nsISupports* aQuery,
nsISimpleEnumerator** aResults)
{
if (!aQuery)
return NS_ERROR_INVALID_ARG;
mGenerationStarted = true;
nsCOMPtr<nsXMLQuery> xmlquery = do_QueryInterface(aQuery);
if (!xmlquery)
return NS_ERROR_INVALID_ARG;
nsCOMPtr<nsISupports> supports;
nsCOMPtr<nsINode> context;
if (aRef)
aRef->GetBindingObjectFor(xmlquery->GetMemberVariable(),
getter_AddRefs(supports));
context = do_QueryInterface(supports);
if (!context)
context = mRoot;
XPathExpression* expr = xmlquery->GetResultsExpression();
if (!expr)
return NS_ERROR_FAILURE;
ErrorResult rv;
RefPtr<XPathResult> exprresults =
expr->Evaluate(*context, XPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
nullptr, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
RefPtr<nsXULTemplateResultSetXML> results =
new nsXULTemplateResultSetXML(xmlquery, exprresults.forget(),
xmlquery->GetBindingSet());
results.forget(aResults);
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::AddBinding(nsIDOMNode* aRuleNode,
nsIAtom* aVar,
nsIAtom* aRef,
const nsAString& aExpr)
{
if (mGenerationStarted)
return NS_ERROR_FAILURE;
RefPtr<nsXMLBindingSet> bindings = mRuleToBindingsMap.GetWeak(aRuleNode);
if (!bindings) {
bindings = new nsXMLBindingSet();
mRuleToBindingsMap.Put(aRuleNode, bindings);
}
nsCOMPtr<nsINode> ruleNode = do_QueryInterface(aRuleNode);
ErrorResult rv;
nsAutoPtr<XPathExpression> compiledexpr;
compiledexpr = CreateExpression(aExpr, ruleNode, rv);
if (rv.Failed()) {
rv.SuppressException();
nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_BAD_BINDING_XPATH);
return NS_OK;
}
// aRef isn't currently used for XML query processors
bindings->AddBinding(aVar, Move(compiledexpr));
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::TranslateRef(nsISupports* aDatasource,
const nsAString& aRefString,
nsIXULTemplateResult** aRef)
{
*aRef = nullptr;
// the datasource is either a document or a DOM element
nsCOMPtr<Element> rootElement;
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDatasource);
if (doc)
rootElement = doc->GetRootElement();
else
rootElement = do_QueryInterface(aDatasource);
// if no root element, just return. The document may not have loaded yet
if (!rootElement)
return NS_OK;
RefPtr<nsXULTemplateResultXML> result = new nsXULTemplateResultXML(nullptr, rootElement, nullptr);
result.forget(aRef);
return NS_OK;
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::CompareResults(nsIXULTemplateResult* aLeft,
nsIXULTemplateResult* aRight,
nsIAtom* aVar,
uint32_t aSortHints,
int32_t* aResult)
{
*aResult = 0;
if (!aVar)
return NS_OK;
nsAutoString leftVal;
if (aLeft)
aLeft->GetBindingFor(aVar, leftVal);
nsAutoString rightVal;
if (aRight)
aRight->GetBindingFor(aVar, rightVal);
*aResult = XULSortServiceImpl::CompareValues(leftVal, rightVal, aSortHints);
return NS_OK;
}
nsXMLBindingSet*
nsXULTemplateQueryProcessorXML::GetOptionalBindingsForRule(nsIDOMNode* aRuleNode)
{
return mRuleToBindingsMap.GetWeak(aRuleNode);
}
XPathExpression*
nsXULTemplateQueryProcessorXML::CreateExpression(const nsAString& aExpr,
nsINode* aNode,
ErrorResult& aRv)
{
return mEvaluator->CreateExpression(aExpr, aNode, aRv);
}
NS_IMETHODIMP
nsXULTemplateQueryProcessorXML::HandleEvent(nsIDOMEvent* aEvent)
{
NS_PRECONDITION(aEvent, "aEvent null");
nsAutoString eventType;
aEvent->GetType(eventType);
if (eventType.EqualsLiteral("load") && mTemplateBuilder) {
NS_ASSERTION(mRequest, "request was not set");
nsCOMPtr<nsIDOMDocument> doc;
if (NS_SUCCEEDED(mRequest->GetResponseXML(getter_AddRefs(doc))))
mTemplateBuilder->SetDatasource(doc);
// to avoid leak. we don't need it after...
mTemplateBuilder = nullptr;
mRequest = nullptr;
}
else if (eventType.EqualsLiteral("error")) {
mTemplateBuilder = nullptr;
mRequest = nullptr;
}
return NS_OK;
}