Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 "nsCOMPtr.h" |
michael@0 | 7 | #include "nsAutoPtr.h" |
michael@0 | 8 | #include "nsIDOMDocument.h" |
michael@0 | 9 | #include "nsIDOMNode.h" |
michael@0 | 10 | #include "nsIDOMElement.h" |
michael@0 | 11 | #include "nsIDOMEvent.h" |
michael@0 | 12 | #include "nsIDOMXPathNSResolver.h" |
michael@0 | 13 | #include "nsIDocument.h" |
michael@0 | 14 | #include "nsIContent.h" |
michael@0 | 15 | #include "nsComponentManagerUtils.h" |
michael@0 | 16 | #include "nsGkAtoms.h" |
michael@0 | 17 | #include "nsIURI.h" |
michael@0 | 18 | #include "nsIArray.h" |
michael@0 | 19 | #include "nsIScriptContext.h" |
michael@0 | 20 | #include "nsArrayUtils.h" |
michael@0 | 21 | #include "nsPIDOMWindow.h" |
michael@0 | 22 | #include "nsXULContentUtils.h" |
michael@0 | 23 | #include "nsXMLHttpRequest.h" |
michael@0 | 24 | |
michael@0 | 25 | #include "nsXULTemplateQueryProcessorXML.h" |
michael@0 | 26 | #include "nsXULTemplateResultXML.h" |
michael@0 | 27 | #include "nsXULSortService.h" |
michael@0 | 28 | |
michael@0 | 29 | using namespace mozilla::dom; |
michael@0 | 30 | |
michael@0 | 31 | NS_IMPL_ISUPPORTS(nsXMLQuery, nsXMLQuery) |
michael@0 | 32 | |
michael@0 | 33 | //---------------------------------------------------------------------- |
michael@0 | 34 | // |
michael@0 | 35 | // nsXULTemplateResultSetXML |
michael@0 | 36 | // |
michael@0 | 37 | |
michael@0 | 38 | NS_IMPL_ISUPPORTS(nsXULTemplateResultSetXML, nsISimpleEnumerator) |
michael@0 | 39 | |
michael@0 | 40 | NS_IMETHODIMP |
michael@0 | 41 | nsXULTemplateResultSetXML::HasMoreElements(bool *aResult) |
michael@0 | 42 | { |
michael@0 | 43 | // if GetSnapshotLength failed, then the return type was not a set of |
michael@0 | 44 | // nodes, so just return false in this case. |
michael@0 | 45 | uint32_t length; |
michael@0 | 46 | if (NS_SUCCEEDED(mResults->GetSnapshotLength(&length))) |
michael@0 | 47 | *aResult = (mPosition < length); |
michael@0 | 48 | else |
michael@0 | 49 | *aResult = false; |
michael@0 | 50 | |
michael@0 | 51 | return NS_OK; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | NS_IMETHODIMP |
michael@0 | 55 | nsXULTemplateResultSetXML::GetNext(nsISupports **aResult) |
michael@0 | 56 | { |
michael@0 | 57 | nsCOMPtr<nsIDOMNode> node; |
michael@0 | 58 | nsresult rv = mResults->SnapshotItem(mPosition, getter_AddRefs(node)); |
michael@0 | 59 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 60 | |
michael@0 | 61 | nsXULTemplateResultXML* result = |
michael@0 | 62 | new nsXULTemplateResultXML(mQuery, node, mBindingSet); |
michael@0 | 63 | NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 64 | |
michael@0 | 65 | ++mPosition; |
michael@0 | 66 | *aResult = result; |
michael@0 | 67 | NS_ADDREF(result); |
michael@0 | 68 | return NS_OK; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | |
michael@0 | 72 | //---------------------------------------------------------------------- |
michael@0 | 73 | // |
michael@0 | 74 | // nsXULTemplateQueryProcessorXML |
michael@0 | 75 | // |
michael@0 | 76 | |
michael@0 | 77 | static PLDHashOperator |
michael@0 | 78 | TraverseRuleToBindingsMap(nsISupports* aKey, nsXMLBindingSet* aMatch, void* aContext) |
michael@0 | 79 | { |
michael@0 | 80 | nsCycleCollectionTraversalCallback *cb = |
michael@0 | 81 | static_cast<nsCycleCollectionTraversalCallback*>(aContext); |
michael@0 | 82 | NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*cb, "mRuleToBindingsMap key"); |
michael@0 | 83 | cb->NoteXPCOMChild(aKey); |
michael@0 | 84 | NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*cb, "mRuleToBindingsMap value"); |
michael@0 | 85 | cb->NoteNativeChild(aMatch, NS_CYCLE_COLLECTION_PARTICIPANT(nsXMLBindingSet)); |
michael@0 | 86 | return PL_DHASH_NEXT; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULTemplateQueryProcessorXML) |
michael@0 | 90 | |
michael@0 | 91 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULTemplateQueryProcessorXML) |
michael@0 | 92 | tmp->mRuleToBindingsMap.Clear(); |
michael@0 | 93 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot) |
michael@0 | 94 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mEvaluator) |
michael@0 | 95 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mTemplateBuilder) |
michael@0 | 96 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mRequest) |
michael@0 | 97 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 98 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULTemplateQueryProcessorXML) |
michael@0 | 99 | tmp->mRuleToBindingsMap.EnumerateRead(TraverseRuleToBindingsMap, &cb); |
michael@0 | 100 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot) |
michael@0 | 101 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEvaluator) |
michael@0 | 102 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTemplateBuilder) |
michael@0 | 103 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRequest) |
michael@0 | 104 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 105 | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULTemplateQueryProcessorXML) |
michael@0 | 106 | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULTemplateQueryProcessorXML) |
michael@0 | 107 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULTemplateQueryProcessorXML) |
michael@0 | 108 | NS_INTERFACE_MAP_ENTRY(nsIXULTemplateQueryProcessor) |
michael@0 | 109 | NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener) |
michael@0 | 110 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXULTemplateQueryProcessor) |
michael@0 | 111 | NS_INTERFACE_MAP_END |
michael@0 | 112 | |
michael@0 | 113 | /* |
michael@0 | 114 | * Only the first datasource in aDataSource is used, which should be either an |
michael@0 | 115 | * nsIURI of an XML document, or a DOM node. If the former, GetDatasource will |
michael@0 | 116 | * load the document asynchronously and return null in aResult. Once the |
michael@0 | 117 | * document has loaded, the builder's datasource will be set to the XML |
michael@0 | 118 | * document. If the datasource is a DOM node, the node will be returned in |
michael@0 | 119 | * aResult. |
michael@0 | 120 | */ |
michael@0 | 121 | NS_IMETHODIMP |
michael@0 | 122 | nsXULTemplateQueryProcessorXML::GetDatasource(nsIArray* aDataSources, |
michael@0 | 123 | nsIDOMNode* aRootNode, |
michael@0 | 124 | bool aIsTrusted, |
michael@0 | 125 | nsIXULTemplateBuilder* aBuilder, |
michael@0 | 126 | bool* aShouldDelayBuilding, |
michael@0 | 127 | nsISupports** aResult) |
michael@0 | 128 | { |
michael@0 | 129 | *aResult = nullptr; |
michael@0 | 130 | *aShouldDelayBuilding = false; |
michael@0 | 131 | |
michael@0 | 132 | nsresult rv; |
michael@0 | 133 | uint32_t length; |
michael@0 | 134 | |
michael@0 | 135 | aDataSources->GetLength(&length); |
michael@0 | 136 | if (length == 0) |
michael@0 | 137 | return NS_OK; |
michael@0 | 138 | |
michael@0 | 139 | // we get only the first item, because the query processor supports only |
michael@0 | 140 | // one document as a datasource |
michael@0 | 141 | |
michael@0 | 142 | nsCOMPtr<nsIDOMNode> node = do_QueryElementAt(aDataSources, 0); |
michael@0 | 143 | if (node) { |
michael@0 | 144 | return CallQueryInterface(node, aResult); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | nsCOMPtr<nsIURI> uri = do_QueryElementAt(aDataSources, 0); |
michael@0 | 148 | if (!uri) |
michael@0 | 149 | return NS_ERROR_UNEXPECTED; |
michael@0 | 150 | |
michael@0 | 151 | nsAutoCString uriStr; |
michael@0 | 152 | rv = uri->GetSpec(uriStr); |
michael@0 | 153 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 154 | |
michael@0 | 155 | nsCOMPtr<nsIContent> root = do_QueryInterface(aRootNode); |
michael@0 | 156 | if (!root) |
michael@0 | 157 | return NS_ERROR_UNEXPECTED; |
michael@0 | 158 | |
michael@0 | 159 | nsCOMPtr<nsIDocument> doc = root->GetCurrentDoc(); |
michael@0 | 160 | if (!doc) |
michael@0 | 161 | return NS_ERROR_UNEXPECTED; |
michael@0 | 162 | |
michael@0 | 163 | nsIPrincipal *docPrincipal = doc->NodePrincipal(); |
michael@0 | 164 | |
michael@0 | 165 | bool hasHadScriptObject = true; |
michael@0 | 166 | nsIScriptGlobalObject* scriptObject = |
michael@0 | 167 | doc->GetScriptHandlingObject(hasHadScriptObject); |
michael@0 | 168 | NS_ENSURE_STATE(scriptObject); |
michael@0 | 169 | |
michael@0 | 170 | nsIScriptContext *context = scriptObject->GetContext(); |
michael@0 | 171 | NS_ENSURE_TRUE(context, NS_OK); |
michael@0 | 172 | |
michael@0 | 173 | nsCOMPtr<nsIXMLHttpRequest> req = |
michael@0 | 174 | do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv); |
michael@0 | 175 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 176 | |
michael@0 | 177 | rv = req->Init(docPrincipal, context, |
michael@0 | 178 | scriptObject ? scriptObject : doc->GetScopeObject(), |
michael@0 | 179 | nullptr); |
michael@0 | 180 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 181 | |
michael@0 | 182 | rv = req->Open(NS_LITERAL_CSTRING("GET"), uriStr, true, |
michael@0 | 183 | EmptyString(), EmptyString()); |
michael@0 | 184 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 185 | |
michael@0 | 186 | nsCOMPtr<EventTarget> target(do_QueryInterface(req)); |
michael@0 | 187 | rv = target->AddEventListener(NS_LITERAL_STRING("load"), this, false); |
michael@0 | 188 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 189 | |
michael@0 | 190 | rv = target->AddEventListener(NS_LITERAL_STRING("error"), this, false); |
michael@0 | 191 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 192 | |
michael@0 | 193 | rv = req->Send(nullptr); |
michael@0 | 194 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 195 | |
michael@0 | 196 | mTemplateBuilder = aBuilder; |
michael@0 | 197 | mRequest = req; |
michael@0 | 198 | |
michael@0 | 199 | *aShouldDelayBuilding = true; |
michael@0 | 200 | return NS_OK; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | NS_IMETHODIMP |
michael@0 | 204 | nsXULTemplateQueryProcessorXML::InitializeForBuilding(nsISupports* aDatasource, |
michael@0 | 205 | nsIXULTemplateBuilder* aBuilder, |
michael@0 | 206 | nsIDOMNode* aRootNode) |
michael@0 | 207 | { |
michael@0 | 208 | if (mGenerationStarted) |
michael@0 | 209 | return NS_ERROR_UNEXPECTED; |
michael@0 | 210 | |
michael@0 | 211 | // the datasource is either a document or a DOM element |
michael@0 | 212 | nsCOMPtr<nsIDOMDocument> doc = do_QueryInterface(aDatasource); |
michael@0 | 213 | if (doc) |
michael@0 | 214 | doc->GetDocumentElement(getter_AddRefs(mRoot)); |
michael@0 | 215 | else |
michael@0 | 216 | mRoot = do_QueryInterface(aDatasource); |
michael@0 | 217 | NS_ENSURE_STATE(mRoot); |
michael@0 | 218 | |
michael@0 | 219 | mEvaluator = do_CreateInstance("@mozilla.org/dom/xpath-evaluator;1"); |
michael@0 | 220 | NS_ENSURE_TRUE(mEvaluator, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 221 | |
michael@0 | 222 | return NS_OK; |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | NS_IMETHODIMP |
michael@0 | 226 | nsXULTemplateQueryProcessorXML::Done() |
michael@0 | 227 | { |
michael@0 | 228 | mGenerationStarted = false; |
michael@0 | 229 | |
michael@0 | 230 | mRuleToBindingsMap.Clear(); |
michael@0 | 231 | |
michael@0 | 232 | return NS_OK; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | NS_IMETHODIMP |
michael@0 | 236 | nsXULTemplateQueryProcessorXML::CompileQuery(nsIXULTemplateBuilder* aBuilder, |
michael@0 | 237 | nsIDOMNode* aQueryNode, |
michael@0 | 238 | nsIAtom* aRefVariable, |
michael@0 | 239 | nsIAtom* aMemberVariable, |
michael@0 | 240 | nsISupports** _retval) |
michael@0 | 241 | { |
michael@0 | 242 | nsresult rv = NS_OK; |
michael@0 | 243 | |
michael@0 | 244 | *_retval = nullptr; |
michael@0 | 245 | |
michael@0 | 246 | nsCOMPtr<nsIContent> content = do_QueryInterface(aQueryNode); |
michael@0 | 247 | |
michael@0 | 248 | nsAutoString expr; |
michael@0 | 249 | content->GetAttr(kNameSpaceID_None, nsGkAtoms::expr, expr); |
michael@0 | 250 | |
michael@0 | 251 | // if an expression is not specified, then the default is to |
michael@0 | 252 | // just take all of the children |
michael@0 | 253 | if (expr.IsEmpty()) |
michael@0 | 254 | expr.AssignLiteral("*"); |
michael@0 | 255 | |
michael@0 | 256 | nsCOMPtr<nsIDOMXPathExpression> compiledexpr; |
michael@0 | 257 | rv = CreateExpression(expr, aQueryNode, getter_AddRefs(compiledexpr)); |
michael@0 | 258 | if (NS_FAILED(rv)) { |
michael@0 | 259 | nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_BAD_XPATH); |
michael@0 | 260 | return rv; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | nsRefPtr<nsXMLQuery> query = |
michael@0 | 264 | new nsXMLQuery(this, aMemberVariable, compiledexpr); |
michael@0 | 265 | NS_ENSURE_TRUE(query, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 266 | |
michael@0 | 267 | for (nsIContent* condition = content->GetFirstChild(); |
michael@0 | 268 | condition; |
michael@0 | 269 | condition = condition->GetNextSibling()) { |
michael@0 | 270 | |
michael@0 | 271 | if (condition->NodeInfo()->Equals(nsGkAtoms::assign, |
michael@0 | 272 | kNameSpaceID_XUL)) { |
michael@0 | 273 | nsAutoString var; |
michael@0 | 274 | condition->GetAttr(kNameSpaceID_None, nsGkAtoms::var, var); |
michael@0 | 275 | |
michael@0 | 276 | nsAutoString expr; |
michael@0 | 277 | condition->GetAttr(kNameSpaceID_None, nsGkAtoms::expr, expr); |
michael@0 | 278 | |
michael@0 | 279 | // ignore assignments without a variable or an expression |
michael@0 | 280 | if (!var.IsEmpty() && !expr.IsEmpty()) { |
michael@0 | 281 | nsCOMPtr<nsIDOMNode> conditionNode = |
michael@0 | 282 | do_QueryInterface(condition); |
michael@0 | 283 | rv = CreateExpression(expr, conditionNode, |
michael@0 | 284 | getter_AddRefs(compiledexpr)); |
michael@0 | 285 | if (NS_FAILED(rv)) { |
michael@0 | 286 | nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_BAD_ASSIGN_XPATH); |
michael@0 | 287 | return rv; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | nsCOMPtr<nsIAtom> varatom = do_GetAtom(var); |
michael@0 | 291 | |
michael@0 | 292 | rv = query->AddBinding(varatom, compiledexpr); |
michael@0 | 293 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 294 | } |
michael@0 | 295 | } |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | *_retval = query; |
michael@0 | 299 | NS_ADDREF(*_retval); |
michael@0 | 300 | |
michael@0 | 301 | return rv; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | NS_IMETHODIMP |
michael@0 | 305 | nsXULTemplateQueryProcessorXML::GenerateResults(nsISupports* aDatasource, |
michael@0 | 306 | nsIXULTemplateResult* aRef, |
michael@0 | 307 | nsISupports* aQuery, |
michael@0 | 308 | nsISimpleEnumerator** aResults) |
michael@0 | 309 | { |
michael@0 | 310 | if (!aQuery) |
michael@0 | 311 | return NS_ERROR_INVALID_ARG; |
michael@0 | 312 | |
michael@0 | 313 | mGenerationStarted = true; |
michael@0 | 314 | |
michael@0 | 315 | nsCOMPtr<nsXMLQuery> xmlquery = do_QueryInterface(aQuery); |
michael@0 | 316 | if (!xmlquery) |
michael@0 | 317 | return NS_ERROR_INVALID_ARG; |
michael@0 | 318 | |
michael@0 | 319 | nsCOMPtr<nsISupports> supports; |
michael@0 | 320 | nsCOMPtr<nsIDOMNode> context; |
michael@0 | 321 | if (aRef) |
michael@0 | 322 | aRef->GetBindingObjectFor(xmlquery->GetMemberVariable(), |
michael@0 | 323 | getter_AddRefs(supports)); |
michael@0 | 324 | context = do_QueryInterface(supports); |
michael@0 | 325 | if (!context) |
michael@0 | 326 | context = mRoot; |
michael@0 | 327 | |
michael@0 | 328 | nsIDOMXPathExpression* expr = xmlquery->GetResultsExpression(); |
michael@0 | 329 | if (!expr) |
michael@0 | 330 | return NS_ERROR_FAILURE; |
michael@0 | 331 | |
michael@0 | 332 | nsCOMPtr<nsISupports> exprsupportsresults; |
michael@0 | 333 | nsresult rv = expr->Evaluate(context, |
michael@0 | 334 | nsIDOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, |
michael@0 | 335 | nullptr, getter_AddRefs(exprsupportsresults)); |
michael@0 | 336 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 337 | |
michael@0 | 338 | nsCOMPtr<nsIDOMXPathResult> exprresults = |
michael@0 | 339 | do_QueryInterface(exprsupportsresults); |
michael@0 | 340 | |
michael@0 | 341 | nsXULTemplateResultSetXML* results = |
michael@0 | 342 | new nsXULTemplateResultSetXML(xmlquery, exprresults, |
michael@0 | 343 | xmlquery->GetBindingSet()); |
michael@0 | 344 | NS_ENSURE_TRUE(results, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 345 | |
michael@0 | 346 | *aResults = results; |
michael@0 | 347 | NS_ADDREF(*aResults); |
michael@0 | 348 | |
michael@0 | 349 | return NS_OK; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | NS_IMETHODIMP |
michael@0 | 353 | nsXULTemplateQueryProcessorXML::AddBinding(nsIDOMNode* aRuleNode, |
michael@0 | 354 | nsIAtom* aVar, |
michael@0 | 355 | nsIAtom* aRef, |
michael@0 | 356 | const nsAString& aExpr) |
michael@0 | 357 | { |
michael@0 | 358 | if (mGenerationStarted) |
michael@0 | 359 | return NS_ERROR_FAILURE; |
michael@0 | 360 | |
michael@0 | 361 | nsRefPtr<nsXMLBindingSet> bindings = mRuleToBindingsMap.GetWeak(aRuleNode); |
michael@0 | 362 | if (!bindings) { |
michael@0 | 363 | bindings = new nsXMLBindingSet(); |
michael@0 | 364 | mRuleToBindingsMap.Put(aRuleNode, bindings); |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | nsCOMPtr<nsIDOMXPathExpression> compiledexpr; |
michael@0 | 368 | nsresult rv = |
michael@0 | 369 | CreateExpression(aExpr, aRuleNode, getter_AddRefs(compiledexpr)); |
michael@0 | 370 | if (NS_FAILED(rv)) { |
michael@0 | 371 | nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_BAD_BINDING_XPATH); |
michael@0 | 372 | return NS_OK; |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | // aRef isn't currently used for XML query processors |
michael@0 | 376 | return bindings->AddBinding(aVar, compiledexpr); |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | NS_IMETHODIMP |
michael@0 | 380 | nsXULTemplateQueryProcessorXML::TranslateRef(nsISupports* aDatasource, |
michael@0 | 381 | const nsAString& aRefString, |
michael@0 | 382 | nsIXULTemplateResult** aRef) |
michael@0 | 383 | { |
michael@0 | 384 | *aRef = nullptr; |
michael@0 | 385 | |
michael@0 | 386 | // the datasource is either a document or a DOM element |
michael@0 | 387 | nsCOMPtr<nsIDOMElement> rootElement; |
michael@0 | 388 | nsCOMPtr<nsIDOMDocument> doc = do_QueryInterface(aDatasource); |
michael@0 | 389 | if (doc) |
michael@0 | 390 | doc->GetDocumentElement(getter_AddRefs(rootElement)); |
michael@0 | 391 | else |
michael@0 | 392 | rootElement = do_QueryInterface(aDatasource); |
michael@0 | 393 | |
michael@0 | 394 | // if no root element, just return. The document may not have loaded yet |
michael@0 | 395 | if (!rootElement) |
michael@0 | 396 | return NS_OK; |
michael@0 | 397 | |
michael@0 | 398 | nsXULTemplateResultXML* result = |
michael@0 | 399 | new nsXULTemplateResultXML(nullptr, rootElement, nullptr); |
michael@0 | 400 | NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 401 | |
michael@0 | 402 | *aRef = result; |
michael@0 | 403 | NS_ADDREF(*aRef); |
michael@0 | 404 | |
michael@0 | 405 | return NS_OK; |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | |
michael@0 | 409 | NS_IMETHODIMP |
michael@0 | 410 | nsXULTemplateQueryProcessorXML::CompareResults(nsIXULTemplateResult* aLeft, |
michael@0 | 411 | nsIXULTemplateResult* aRight, |
michael@0 | 412 | nsIAtom* aVar, |
michael@0 | 413 | uint32_t aSortHints, |
michael@0 | 414 | int32_t* aResult) |
michael@0 | 415 | { |
michael@0 | 416 | *aResult = 0; |
michael@0 | 417 | if (!aVar) |
michael@0 | 418 | return NS_OK; |
michael@0 | 419 | |
michael@0 | 420 | nsAutoString leftVal; |
michael@0 | 421 | if (aLeft) |
michael@0 | 422 | aLeft->GetBindingFor(aVar, leftVal); |
michael@0 | 423 | |
michael@0 | 424 | nsAutoString rightVal; |
michael@0 | 425 | if (aRight) |
michael@0 | 426 | aRight->GetBindingFor(aVar, rightVal); |
michael@0 | 427 | |
michael@0 | 428 | *aResult = XULSortServiceImpl::CompareValues(leftVal, rightVal, aSortHints); |
michael@0 | 429 | return NS_OK; |
michael@0 | 430 | } |
michael@0 | 431 | |
michael@0 | 432 | nsXMLBindingSet* |
michael@0 | 433 | nsXULTemplateQueryProcessorXML::GetOptionalBindingsForRule(nsIDOMNode* aRuleNode) |
michael@0 | 434 | { |
michael@0 | 435 | return mRuleToBindingsMap.GetWeak(aRuleNode); |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | nsresult |
michael@0 | 439 | nsXULTemplateQueryProcessorXML::CreateExpression(const nsAString& aExpr, |
michael@0 | 440 | nsIDOMNode* aNode, |
michael@0 | 441 | nsIDOMXPathExpression** aCompiledExpr) |
michael@0 | 442 | { |
michael@0 | 443 | nsCOMPtr<nsIDOMXPathNSResolver> nsResolver; |
michael@0 | 444 | |
michael@0 | 445 | nsCOMPtr<nsIDOMDocument> doc; |
michael@0 | 446 | aNode->GetOwnerDocument(getter_AddRefs(doc)); |
michael@0 | 447 | |
michael@0 | 448 | nsCOMPtr<nsIDOMXPathEvaluator> eval = do_QueryInterface(doc); |
michael@0 | 449 | if (eval) { |
michael@0 | 450 | nsresult rv = |
michael@0 | 451 | eval->CreateNSResolver(aNode, getter_AddRefs(nsResolver)); |
michael@0 | 452 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | return mEvaluator->CreateExpression(aExpr, nsResolver, aCompiledExpr); |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | NS_IMETHODIMP |
michael@0 | 459 | nsXULTemplateQueryProcessorXML::HandleEvent(nsIDOMEvent* aEvent) |
michael@0 | 460 | { |
michael@0 | 461 | NS_PRECONDITION(aEvent, "aEvent null"); |
michael@0 | 462 | nsAutoString eventType; |
michael@0 | 463 | aEvent->GetType(eventType); |
michael@0 | 464 | |
michael@0 | 465 | if (eventType.EqualsLiteral("load") && mTemplateBuilder) { |
michael@0 | 466 | NS_ASSERTION(mRequest, "request was not set"); |
michael@0 | 467 | nsCOMPtr<nsIDOMDocument> doc; |
michael@0 | 468 | if (NS_SUCCEEDED(mRequest->GetResponseXML(getter_AddRefs(doc)))) |
michael@0 | 469 | mTemplateBuilder->SetDatasource(doc); |
michael@0 | 470 | |
michael@0 | 471 | // to avoid leak. we don't need it after... |
michael@0 | 472 | mTemplateBuilder = nullptr; |
michael@0 | 473 | mRequest = nullptr; |
michael@0 | 474 | } |
michael@0 | 475 | else if (eventType.EqualsLiteral("error")) { |
michael@0 | 476 | mTemplateBuilder = nullptr; |
michael@0 | 477 | mRequest = nullptr; |
michael@0 | 478 | } |
michael@0 | 479 | |
michael@0 | 480 | return NS_OK; |
michael@0 | 481 | } |