content/xul/templates/src/nsXULTemplateBuilder.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 #ifndef nsXULTemplateBuilder_h__
michael@0 7 #define nsXULTemplateBuilder_h__
michael@0 8
michael@0 9 #include "nsStubDocumentObserver.h"
michael@0 10 #include "nsIScriptSecurityManager.h"
michael@0 11 #include "nsIObserver.h"
michael@0 12 #include "nsIRDFCompositeDataSource.h"
michael@0 13 #include "nsIRDFContainer.h"
michael@0 14 #include "nsIRDFContainerUtils.h"
michael@0 15 #include "nsIRDFDataSource.h"
michael@0 16 #include "nsIRDFObserver.h"
michael@0 17 #include "nsIRDFService.h"
michael@0 18 #include "nsIXULTemplateBuilder.h"
michael@0 19
michael@0 20 #include "nsCOMArray.h"
michael@0 21 #include "nsTArray.h"
michael@0 22 #include "nsDataHashtable.h"
michael@0 23 #include "nsTemplateRule.h"
michael@0 24 #include "nsTemplateMatch.h"
michael@0 25 #include "nsIXULTemplateQueryProcessor.h"
michael@0 26 #include "nsCycleCollectionParticipant.h"
michael@0 27
michael@0 28 #include "prlog.h"
michael@0 29 #ifdef PR_LOGGING
michael@0 30 extern PRLogModuleInfo* gXULTemplateLog;
michael@0 31 #endif
michael@0 32
michael@0 33 class nsIContent;
michael@0 34 class nsIObserverService;
michael@0 35 class nsIRDFCompositeDataSource;
michael@0 36 class nsIXULDocument;
michael@0 37
michael@0 38 /**
michael@0 39 * An object that translates an RDF graph into a presentation using a
michael@0 40 * set of rules.
michael@0 41 */
michael@0 42 class nsXULTemplateBuilder : public nsIXULTemplateBuilder,
michael@0 43 public nsIObserver,
michael@0 44 public nsStubDocumentObserver
michael@0 45 {
michael@0 46 void CleanUp(bool aIsFinal);
michael@0 47
michael@0 48 public:
michael@0 49 nsXULTemplateBuilder();
michael@0 50 virtual ~nsXULTemplateBuilder();
michael@0 51
michael@0 52 nsresult InitGlobals();
michael@0 53
michael@0 54 /**
michael@0 55 * Clear the template builder structures. The aIsFinal flag is set to true
michael@0 56 * when the template is going away.
michael@0 57 */
michael@0 58 virtual void Uninit(bool aIsFinal);
michael@0 59
michael@0 60 // nsISupports interface
michael@0 61 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 62 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsXULTemplateBuilder,
michael@0 63 nsIXULTemplateBuilder)
michael@0 64
michael@0 65 // nsIXULTemplateBuilder interface
michael@0 66 NS_DECL_NSIXULTEMPLATEBUILDER
michael@0 67
michael@0 68 // nsIObserver Interface
michael@0 69 NS_DECL_NSIOBSERVER
michael@0 70
michael@0 71 // nsIMutationObserver
michael@0 72 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
michael@0 73 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
michael@0 74 NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
michael@0 75
michael@0 76 /**
michael@0 77 * Remove an old result and/or add a new result. This method will retrieve
michael@0 78 * the set of containers where the result could be inserted and either add
michael@0 79 * the new result to those containers, or remove the result from those
michael@0 80 * containers. UpdateResultInContainer is called for each container.
michael@0 81 *
michael@0 82 * @param aOldResult result to remove
michael@0 83 * @param aNewResult result to add
michael@0 84 * @param aQueryNode query node for new result
michael@0 85 */
michael@0 86 nsresult
michael@0 87 UpdateResult(nsIXULTemplateResult* aOldResult,
michael@0 88 nsIXULTemplateResult* aNewResult,
michael@0 89 nsIDOMNode* aQueryNode);
michael@0 90
michael@0 91 /**
michael@0 92 * Remove an old result and/or add a new result from a specific container.
michael@0 93 *
michael@0 94 * @param aOldResult result to remove
michael@0 95 * @param aNewResult result to add
michael@0 96 * @param aQueryNode queryset for the new result
michael@0 97 * @param aOldId id of old result
michael@0 98 * @param aNewId id of new result
michael@0 99 * @param aInsertionPoint container to remove or add result inside
michael@0 100 */
michael@0 101 nsresult
michael@0 102 UpdateResultInContainer(nsIXULTemplateResult* aOldResult,
michael@0 103 nsIXULTemplateResult* aNewResult,
michael@0 104 nsTemplateQuerySet* aQuerySet,
michael@0 105 nsIRDFResource* aOldId,
michael@0 106 nsIRDFResource* aNewId,
michael@0 107 nsIContent* aInsertionPoint);
michael@0 108
michael@0 109 nsresult
michael@0 110 ComputeContainmentProperties();
michael@0 111
michael@0 112 static bool
michael@0 113 IsTemplateElement(nsIContent* aContent);
michael@0 114
michael@0 115 virtual nsresult
michael@0 116 RebuildAll() = 0; // must be implemented by subclasses
michael@0 117
michael@0 118 void RunnableRebuild() { Rebuild(); }
michael@0 119 void RunnableLoadAndRebuild() {
michael@0 120 Uninit(false); // Reset results
michael@0 121
michael@0 122 nsCOMPtr<nsIDocument> doc = mRoot ? mRoot->GetDocument() : nullptr;
michael@0 123 if (doc) {
michael@0 124 bool shouldDelay;
michael@0 125 LoadDataSources(doc, &shouldDelay);
michael@0 126 if (!shouldDelay) {
michael@0 127 Rebuild();
michael@0 128 }
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 // mRoot should not be cleared until after Uninit is finished so that
michael@0 133 // generated content can be removed during uninitialization.
michael@0 134 void UninitFalse() { Uninit(false); mRoot = nullptr; }
michael@0 135 void UninitTrue() { Uninit(true); mRoot = nullptr; }
michael@0 136
michael@0 137 /**
michael@0 138 * Find the <template> tag that applies for this builder
michael@0 139 */
michael@0 140 nsresult
michael@0 141 GetTemplateRoot(nsIContent** aResult);
michael@0 142
michael@0 143 /**
michael@0 144 * Compile the template's queries
michael@0 145 */
michael@0 146 nsresult
michael@0 147 CompileQueries();
michael@0 148
michael@0 149 /**
michael@0 150 * Compile the template given a <template> in aTemplate. This function
michael@0 151 * is called recursively to handle queries inside a queryset. For the
michael@0 152 * outer pass, aIsQuerySet will be false, while the inner pass this will
michael@0 153 * be true.
michael@0 154 *
michael@0 155 * aCanUseTemplate will be set to true if the template's queries could be
michael@0 156 * compiled, and false otherwise. If false, the entire template is
michael@0 157 * invalid.
michael@0 158 *
michael@0 159 * @param aTemplate <template> to compile
michael@0 160 * @param aQuerySet first queryset
michael@0 161 * @param aIsQuerySet true if
michael@0 162 * @param aPriority the queryset index, incremented when a new one is added
michael@0 163 * @param aCanUseTemplate true if template is valid
michael@0 164 */
michael@0 165 nsresult
michael@0 166 CompileTemplate(nsIContent* aTemplate,
michael@0 167 nsTemplateQuerySet* aQuerySet,
michael@0 168 bool aIsQuerySet,
michael@0 169 int32_t* aPriority,
michael@0 170 bool* aCanUseTemplate);
michael@0 171
michael@0 172 /**
michael@0 173 * Compile a query using the extended syntax. For backwards compatible RDF
michael@0 174 * syntax where there is no <query>, the <conditions> becomes the query.
michael@0 175 *
michael@0 176 * @param aRuleElement <rule> element
michael@0 177 * @param aActionElement <action> element
michael@0 178 * @param aMemberVariable member variable for the query
michael@0 179 * @param aQuerySet the queryset
michael@0 180 */
michael@0 181 nsresult
michael@0 182 CompileExtendedQuery(nsIContent* aRuleElement,
michael@0 183 nsIContent* aActionElement,
michael@0 184 nsIAtom* aMemberVariable,
michael@0 185 nsTemplateQuerySet* aQuerySet);
michael@0 186
michael@0 187 /**
michael@0 188 * Determine the ref variable and tag from inside a RDF query.
michael@0 189 */
michael@0 190 void DetermineRDFQueryRef(nsIContent* aQueryElement, nsIAtom** tag);
michael@0 191
michael@0 192 /**
michael@0 193 * Determine the member variable from inside an action body. It will be
michael@0 194 * the value of the uri attribute on a node.
michael@0 195 */
michael@0 196 already_AddRefed<nsIAtom> DetermineMemberVariable(nsIContent* aElement);
michael@0 197
michael@0 198 /**
michael@0 199 * Compile a simple query. A simple query is one that doesn't have a
michael@0 200 * <query> and should use a default query which would normally just return
michael@0 201 * a list of children of the reference point.
michael@0 202 *
michael@0 203 * @param aRuleElement the <rule>
michael@0 204 * @param aQuerySet the query set
michael@0 205 * @param aCanUseTemplate true if the query is valid
michael@0 206 */
michael@0 207 nsresult
michael@0 208 CompileSimpleQuery(nsIContent* aRuleElement,
michael@0 209 nsTemplateQuerySet* aQuerySet,
michael@0 210 bool* aCanUseTemplate);
michael@0 211
michael@0 212 /**
michael@0 213 * Compile the <conditions> tag in a rule
michael@0 214 *
michael@0 215 * @param aRule template rule
michael@0 216 * @param aConditions <conditions> element
michael@0 217 */
michael@0 218 nsresult
michael@0 219 CompileConditions(nsTemplateRule* aRule, nsIContent* aConditions);
michael@0 220
michael@0 221 /**
michael@0 222 * Compile a <where> tag in a condition. The caller should set
michael@0 223 * *aCurrentCondition to null for the first condition. This value will be
michael@0 224 * updated to point to the new condition before returning. The conditions
michael@0 225 * will be added to the rule aRule by this method.
michael@0 226 *
michael@0 227 * @param aRule template rule
michael@0 228 * @param aCondition <where> element
michael@0 229 * @param aCurrentCondition compiled condition
michael@0 230 */
michael@0 231 nsresult
michael@0 232 CompileWhereCondition(nsTemplateRule* aRule,
michael@0 233 nsIContent* aCondition,
michael@0 234 nsTemplateCondition** aCurrentCondition);
michael@0 235
michael@0 236 /**
michael@0 237 * Compile the <bindings> for an extended template syntax rule.
michael@0 238 */
michael@0 239 nsresult
michael@0 240 CompileBindings(nsTemplateRule* aRule, nsIContent* aBindings);
michael@0 241
michael@0 242 /**
michael@0 243 * Compile a single binding for an extended template syntax rule.
michael@0 244 */
michael@0 245 nsresult
michael@0 246 CompileBinding(nsTemplateRule* aRule, nsIContent* aBinding);
michael@0 247
michael@0 248 /**
michael@0 249 * Add automatic bindings for simple rules
michael@0 250 */
michael@0 251 nsresult
michael@0 252 AddSimpleRuleBindings(nsTemplateRule* aRule, nsIContent* aElement);
michael@0 253
michael@0 254 static void
michael@0 255 AddBindingsFor(nsXULTemplateBuilder* aSelf,
michael@0 256 const nsAString& aVariable,
michael@0 257 void* aClosure);
michael@0 258
michael@0 259 /**
michael@0 260 * Load the datasources for the template. shouldDelayBuilding is an out
michael@0 261 * parameter which will be set to true to indicate that content building
michael@0 262 * should not be performed yet as the datasource has not yet loaded. If
michael@0 263 * false, the datasource has already loaded so building can proceed
michael@0 264 * immediately. In the former case, the datasource or query processor
michael@0 265 * should either rebuild the template or update results when the
michael@0 266 * datasource is loaded as needed.
michael@0 267 */
michael@0 268 nsresult
michael@0 269 LoadDataSources(nsIDocument* aDoc, bool* shouldDelayBuilding);
michael@0 270
michael@0 271 /**
michael@0 272 * Called by LoadDataSources to load a datasource given a uri list
michael@0 273 * in aDataSource. The list is a set of uris separated by spaces.
michael@0 274 * If aIsRDFQuery is true, then this is for an RDF datasource which
michael@0 275 * causes the method to check for additional flags specific to the
michael@0 276 * RDF processor.
michael@0 277 */
michael@0 278 nsresult
michael@0 279 LoadDataSourceUrls(nsIDocument* aDocument,
michael@0 280 const nsAString& aDataSources,
michael@0 281 bool aIsRDFQuery,
michael@0 282 bool* aShouldDelayBuilding);
michael@0 283
michael@0 284 nsresult
michael@0 285 InitHTMLTemplateRoot();
michael@0 286
michael@0 287 /**
michael@0 288 * Determine which rule matches a given result. aContainer is used for
michael@0 289 * tag matching and is optional for non-content generating builders.
michael@0 290 * The returned matched rule is always one of the rules owned by the
michael@0 291 * query set aQuerySet.
michael@0 292 *
michael@0 293 * @param aContainer parent where generated content will be inserted
michael@0 294 * @param aResult result to match
michael@0 295 * @param aQuerySet query set to examine the rules of
michael@0 296 * @param aMatchedRule [out] rule that has matched, or null if any.
michael@0 297 * @param aRuleIndex [out] index of the rule
michael@0 298 */
michael@0 299 nsresult
michael@0 300 DetermineMatchedRule(nsIContent* aContainer,
michael@0 301 nsIXULTemplateResult* aResult,
michael@0 302 nsTemplateQuerySet* aQuerySet,
michael@0 303 nsTemplateRule** aMatchedRule,
michael@0 304 int16_t *aRuleIndex);
michael@0 305
michael@0 306 // XXX sigh, the string template foo doesn't mix with
michael@0 307 // operator->*() on egcs-1.1.2, so we'll need to explicitly pass
michael@0 308 // "this" and use good ol' fashioned static callbacks.
michael@0 309 void
michael@0 310 ParseAttribute(const nsAString& aAttributeValue,
michael@0 311 void (*aVariableCallback)(nsXULTemplateBuilder* aThis, const nsAString&, void*),
michael@0 312 void (*aTextCallback)(nsXULTemplateBuilder* aThis, const nsAString&, void*),
michael@0 313 void* aClosure);
michael@0 314
michael@0 315 nsresult
michael@0 316 SubstituteText(nsIXULTemplateResult* aMatch,
michael@0 317 const nsAString& aAttributeValue,
michael@0 318 nsAString& aResult);
michael@0 319
michael@0 320 static void
michael@0 321 SubstituteTextAppendText(nsXULTemplateBuilder* aThis, const nsAString& aText, void* aClosure);
michael@0 322
michael@0 323 static void
michael@0 324 SubstituteTextReplaceVariable(nsXULTemplateBuilder* aThis, const nsAString& aVariable, void* aClosure);
michael@0 325
michael@0 326 nsresult
michael@0 327 IsSystemPrincipal(nsIPrincipal *principal, bool *result);
michael@0 328
michael@0 329 /**
michael@0 330 * Convenience method which gets a resource for a result. If a result
michael@0 331 * doesn't have a resource set, it will create one from the result's id.
michael@0 332 */
michael@0 333 nsresult GetResultResource(nsIXULTemplateResult* aResult,
michael@0 334 nsIRDFResource** aResource);
michael@0 335
michael@0 336 protected:
michael@0 337 nsCOMPtr<nsISupports> mDataSource;
michael@0 338 nsCOMPtr<nsIRDFDataSource> mDB;
michael@0 339 nsCOMPtr<nsIRDFCompositeDataSource> mCompDB;
michael@0 340
michael@0 341 /**
michael@0 342 * Circular reference, broken when the document is destroyed.
michael@0 343 */
michael@0 344 nsCOMPtr<nsIContent> mRoot;
michael@0 345
michael@0 346 /**
michael@0 347 * The root result, translated from the root element's ref
michael@0 348 */
michael@0 349 nsCOMPtr<nsIXULTemplateResult> mRootResult;
michael@0 350
michael@0 351 nsCOMArray<nsIXULBuilderListener> mListeners;
michael@0 352
michael@0 353 /**
michael@0 354 * The query processor which generates results
michael@0 355 */
michael@0 356 nsCOMPtr<nsIXULTemplateQueryProcessor> mQueryProcessor;
michael@0 357
michael@0 358 /**
michael@0 359 * The list of querysets
michael@0 360 */
michael@0 361 nsTArray<nsTemplateQuerySet *> mQuerySets;
michael@0 362
michael@0 363 /**
michael@0 364 * Set to true if the rules have already been compiled
michael@0 365 */
michael@0 366 bool mQueriesCompiled;
michael@0 367
michael@0 368 /**
michael@0 369 * The default reference and member variables.
michael@0 370 */
michael@0 371 nsCOMPtr<nsIAtom> mRefVariable;
michael@0 372 nsCOMPtr<nsIAtom> mMemberVariable;
michael@0 373
michael@0 374 /**
michael@0 375 * The match map contains nsTemplateMatch objects, one for each unique
michael@0 376 * match found, keyed by the resource for that match. A particular match
michael@0 377 * will contain a linked list of all of the matches for that unique result
michael@0 378 * id. Only one is active at a time. When a match is retracted, look in
michael@0 379 * the match map, remove it, and apply the next valid match in sequence to
michael@0 380 * make active.
michael@0 381 */
michael@0 382 nsDataHashtable<nsISupportsHashKey, nsTemplateMatch*> mMatchMap;
michael@0 383
michael@0 384 // pseudo-constants
michael@0 385 static nsrefcnt gRefCnt;
michael@0 386 static nsIRDFService* gRDFService;
michael@0 387 static nsIRDFContainerUtils* gRDFContainerUtils;
michael@0 388 static nsIScriptSecurityManager* gScriptSecurityManager;
michael@0 389 static nsIPrincipal* gSystemPrincipal;
michael@0 390 static nsIObserverService* gObserverService;
michael@0 391
michael@0 392 enum {
michael@0 393 eDontTestEmpty = (1 << 0),
michael@0 394 eDontRecurse = (1 << 1),
michael@0 395 eLoggingEnabled = (1 << 2)
michael@0 396 };
michael@0 397
michael@0 398 int32_t mFlags;
michael@0 399
michael@0 400 /**
michael@0 401 * Stack-based helper class to maintain a list of ``activated''
michael@0 402 * resources; i.e., resources for which we are currently building
michael@0 403 * content.
michael@0 404 */
michael@0 405 class ActivationEntry {
michael@0 406 public:
michael@0 407 nsIRDFResource *mResource;
michael@0 408 ActivationEntry *mPrevious;
michael@0 409 ActivationEntry **mLink;
michael@0 410
michael@0 411 ActivationEntry(nsIRDFResource *aResource, ActivationEntry **aLink)
michael@0 412 : mResource(aResource),
michael@0 413 mPrevious(*aLink),
michael@0 414 mLink(aLink) { *mLink = this; }
michael@0 415
michael@0 416 ~ActivationEntry() { *mLink = mPrevious; }
michael@0 417 };
michael@0 418
michael@0 419 /**
michael@0 420 * The top of the stack of resources that we're currently building
michael@0 421 * content for.
michael@0 422 */
michael@0 423 ActivationEntry *mTop;
michael@0 424
michael@0 425 /**
michael@0 426 * Determine if a resource is currently on the activation stack.
michael@0 427 */
michael@0 428 bool
michael@0 429 IsActivated(nsIRDFResource *aResource);
michael@0 430
michael@0 431 /**
michael@0 432 * Returns true if content may be generated for a result, or false if it
michael@0 433 * cannot, for example, if it would be created inside a closed container.
michael@0 434 * Those results will be generated when the container is opened.
michael@0 435 * If false is returned, no content should be generated. Possible
michael@0 436 * insertion locations may optionally be set for new content, depending on
michael@0 437 * the builder being used. Note that *aLocations or some items within
michael@0 438 * aLocations may be null.
michael@0 439 */
michael@0 440 virtual bool
michael@0 441 GetInsertionLocations(nsIXULTemplateResult* aResult,
michael@0 442 nsCOMArray<nsIContent>** aLocations) = 0;
michael@0 443
michael@0 444 /**
michael@0 445 * Must be implemented by subclasses. Handle removing the generated
michael@0 446 * output for aOldMatch and adding new output for aNewMatch. Either
michael@0 447 * aOldMatch or aNewMatch may be null. aContext is the location returned
michael@0 448 * from the call to MayGenerateResult.
michael@0 449 */
michael@0 450 virtual nsresult
michael@0 451 ReplaceMatch(nsIXULTemplateResult* aOldResult,
michael@0 452 nsTemplateMatch* aNewMatch,
michael@0 453 nsTemplateRule* aNewMatchRule,
michael@0 454 void *aContext) = 0;
michael@0 455
michael@0 456 /**
michael@0 457 * Must be implemented by subclasses. Handle change in bound
michael@0 458 * variable values for aResult. aModifiedVars contains the set
michael@0 459 * of variables that have changed.
michael@0 460 * @param aResult the ersult for which variable bindings has changed.
michael@0 461 * @param aModifiedVars the set of variables for which the bindings
michael@0 462 * have changed.
michael@0 463 */
michael@0 464 virtual nsresult
michael@0 465 SynchronizeResult(nsIXULTemplateResult* aResult) = 0;
michael@0 466
michael@0 467 /**
michael@0 468 * Output a new match or removed match to the console.
michael@0 469 *
michael@0 470 * @param aId id of the result
michael@0 471 * @param aMatch new or removed match
michael@0 472 * @param aIsNew true for new matched, false for removed matches
michael@0 473 */
michael@0 474 void
michael@0 475 OutputMatchToLog(nsIRDFResource* aId,
michael@0 476 nsTemplateMatch* aMatch,
michael@0 477 bool aIsNew);
michael@0 478
michael@0 479 virtual void Traverse(nsCycleCollectionTraversalCallback &cb) const
michael@0 480 {
michael@0 481 }
michael@0 482
michael@0 483 /**
michael@0 484 * Start observing events from the observer service and the given
michael@0 485 * document.
michael@0 486 *
michael@0 487 * @param aDocument the document to observe
michael@0 488 */
michael@0 489 void StartObserving(nsIDocument* aDocument);
michael@0 490
michael@0 491 /**
michael@0 492 * Stop observing events from the observer service and any associated
michael@0 493 * document.
michael@0 494 */
michael@0 495 void StopObserving();
michael@0 496
michael@0 497 /**
michael@0 498 * Document that we're observing. Weak ref!
michael@0 499 */
michael@0 500 nsIDocument* mObservedDocument;
michael@0 501 };
michael@0 502
michael@0 503 #endif // nsXULTemplateBuilder_h__

mercurial