Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 nsTemplateRule_h__ |
michael@0 | 7 | #define nsTemplateRule_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsCOMPtr.h" |
michael@0 | 10 | #include "nsIAtom.h" |
michael@0 | 11 | #include "nsIRDFDataSource.h" |
michael@0 | 12 | #include "nsIRDFResource.h" |
michael@0 | 13 | #include "nsIContent.h" |
michael@0 | 14 | #include "nsIDOMNode.h" |
michael@0 | 15 | #include "nsTArray.h" |
michael@0 | 16 | #include "nsString.h" |
michael@0 | 17 | #include "nsIXULTemplateRuleFilter.h" |
michael@0 | 18 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 19 | |
michael@0 | 20 | class nsIXULTemplateQueryProcessor; |
michael@0 | 21 | class nsTemplateQuerySet; |
michael@0 | 22 | |
michael@0 | 23 | class nsTemplateCondition |
michael@0 | 24 | { |
michael@0 | 25 | public: |
michael@0 | 26 | // relations that may be used in a rule. They may be negated with the |
michael@0 | 27 | // negate flag. Less and Greater are used for numeric comparisons and |
michael@0 | 28 | // Before and After are used for string comparisons. For Less, Greater, |
michael@0 | 29 | // Before, After, Startswith, Endswith, and Contains, the source is |
michael@0 | 30 | // conceptually on the left of the relation and the target is on the |
michael@0 | 31 | // right. For example, if the relation is Contains, that means Match if |
michael@0 | 32 | // the source contains the target. |
michael@0 | 33 | enum ConditionRelation { |
michael@0 | 34 | eUnknown, |
michael@0 | 35 | eEquals, |
michael@0 | 36 | eLess, |
michael@0 | 37 | eGreater, |
michael@0 | 38 | eBefore, |
michael@0 | 39 | eAfter, |
michael@0 | 40 | eStartswith, |
michael@0 | 41 | eEndswith, |
michael@0 | 42 | eContains |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | nsTemplateCondition(nsIAtom* aSourceVariable, |
michael@0 | 46 | const nsAString& aRelation, |
michael@0 | 47 | nsIAtom* aTargetVariable, |
michael@0 | 48 | bool mIgnoreCase, |
michael@0 | 49 | bool mNegate); |
michael@0 | 50 | |
michael@0 | 51 | nsTemplateCondition(nsIAtom* aSourceVariable, |
michael@0 | 52 | const nsAString& aRelation, |
michael@0 | 53 | const nsAString& aTargets, |
michael@0 | 54 | bool mIgnoreCase, |
michael@0 | 55 | bool mNegate, |
michael@0 | 56 | bool aIsMultiple); |
michael@0 | 57 | |
michael@0 | 58 | nsTemplateCondition(const nsAString& aSource, |
michael@0 | 59 | const nsAString& aRelation, |
michael@0 | 60 | nsIAtom* aTargetVariable, |
michael@0 | 61 | bool mIgnoreCase, |
michael@0 | 62 | bool mNegate); |
michael@0 | 63 | |
michael@0 | 64 | ~nsTemplateCondition() { MOZ_COUNT_DTOR(nsTemplateCondition); } |
michael@0 | 65 | |
michael@0 | 66 | nsTemplateCondition* GetNext() { return mNext; } |
michael@0 | 67 | void SetNext(nsTemplateCondition* aNext) { mNext = aNext; } |
michael@0 | 68 | |
michael@0 | 69 | void SetRelation(const nsAString& aRelation); |
michael@0 | 70 | |
michael@0 | 71 | bool |
michael@0 | 72 | CheckMatch(nsIXULTemplateResult* aResult); |
michael@0 | 73 | |
michael@0 | 74 | bool |
michael@0 | 75 | CheckMatchStrings(const nsAString& aLeftString, |
michael@0 | 76 | const nsAString& aRightString); |
michael@0 | 77 | protected: |
michael@0 | 78 | |
michael@0 | 79 | nsCOMPtr<nsIAtom> mSourceVariable; |
michael@0 | 80 | nsString mSource; |
michael@0 | 81 | ConditionRelation mRelation; |
michael@0 | 82 | nsCOMPtr<nsIAtom> mTargetVariable; |
michael@0 | 83 | nsTArray<nsString> mTargetList; |
michael@0 | 84 | bool mIgnoreCase; |
michael@0 | 85 | bool mNegate; |
michael@0 | 86 | |
michael@0 | 87 | nsTemplateCondition* mNext; |
michael@0 | 88 | }; |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * A rule consists of: |
michael@0 | 92 | * |
michael@0 | 93 | * - Conditions, a set of unbound variables with consistency |
michael@0 | 94 | * constraints that specify the values that each variable can |
michael@0 | 95 | * assume. The conditions must be completely and consistently |
michael@0 | 96 | * "bound" for the rule to be considered "matched". |
michael@0 | 97 | * |
michael@0 | 98 | * - Bindings, a set of unbound variables with consistency constraints |
michael@0 | 99 | * that specify the values that each variable can assume. Unlike the |
michael@0 | 100 | * conditions, the bindings need not be bound for the rule to be |
michael@0 | 101 | * considered matched. |
michael@0 | 102 | * |
michael@0 | 103 | * - Content that should be constructed when the rule is "activated". |
michael@0 | 104 | * |
michael@0 | 105 | */ |
michael@0 | 106 | class nsTemplateRule |
michael@0 | 107 | { |
michael@0 | 108 | public: |
michael@0 | 109 | nsTemplateRule(nsIContent* aRuleNode, |
michael@0 | 110 | nsIContent* aAction, |
michael@0 | 111 | nsTemplateQuerySet* aQuerySet); |
michael@0 | 112 | /** |
michael@0 | 113 | * The copy-constructor should only be called from nsTArray when appending |
michael@0 | 114 | * a new rule, otherwise things break because the copy constructor expects |
michael@0 | 115 | * mBindings and mConditions to be nullptr. |
michael@0 | 116 | */ |
michael@0 | 117 | nsTemplateRule(const nsTemplateRule& aOtherRule); |
michael@0 | 118 | |
michael@0 | 119 | ~nsTemplateRule(); |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Return the <action> node that this rule was constructed from, or its |
michael@0 | 123 | * logical equivalent for shorthand syntaxes. That is, the parent node of |
michael@0 | 124 | * the content that should be generated for this rule. |
michael@0 | 125 | */ |
michael@0 | 126 | nsIContent* GetAction() const { return mAction; } |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * Return the <rule> content node that this rule was constructed from. |
michael@0 | 130 | * @param aResult an out parameter, which will contain the rule node |
michael@0 | 131 | * @return NS_OK if no errors occur. |
michael@0 | 132 | */ |
michael@0 | 133 | nsresult GetRuleNode(nsIDOMNode** aResult) const; |
michael@0 | 134 | |
michael@0 | 135 | void SetVars(nsIAtom* aRefVariable, nsIAtom* aMemberVariable) |
michael@0 | 136 | { |
michael@0 | 137 | mRefVariable = aRefVariable; |
michael@0 | 138 | mMemberVariable = aMemberVariable; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | void SetRuleFilter(nsIXULTemplateRuleFilter* aRuleFilter) |
michael@0 | 142 | { |
michael@0 | 143 | mRuleFilter = aRuleFilter; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | nsIAtom* GetTag() { return mTag; } |
michael@0 | 147 | void SetTag(nsIAtom* aTag) { mTag = aTag; } |
michael@0 | 148 | |
michael@0 | 149 | nsIAtom* GetMemberVariable() { return mMemberVariable; } |
michael@0 | 150 | |
michael@0 | 151 | /** |
michael@0 | 152 | * Set the first condition for the rule. Other conditions are linked |
michael@0 | 153 | * to it using the condition's SetNext method. |
michael@0 | 154 | */ |
michael@0 | 155 | void SetCondition(nsTemplateCondition* aConditions); |
michael@0 | 156 | |
michael@0 | 157 | /** |
michael@0 | 158 | * Check if the result matches the rule by first looking at the conditions. |
michael@0 | 159 | * If the results is accepted by the conditions, the rule filter, if any |
michael@0 | 160 | * was set, is checked. If either check rejects a result, a match cannot |
michael@0 | 161 | * occur for this rule and result. |
michael@0 | 162 | */ |
michael@0 | 163 | bool |
michael@0 | 164 | CheckMatch(nsIXULTemplateResult* aResult) const; |
michael@0 | 165 | |
michael@0 | 166 | /** |
michael@0 | 167 | * Determine if the rule has the specified binding |
michael@0 | 168 | */ |
michael@0 | 169 | bool |
michael@0 | 170 | HasBinding(nsIAtom* aSourceVariable, |
michael@0 | 171 | nsAString& aExpr, |
michael@0 | 172 | nsIAtom* aTargetVariable) const; |
michael@0 | 173 | |
michael@0 | 174 | /** |
michael@0 | 175 | * Add a binding to the rule. A binding consists of an already-bound |
michael@0 | 176 | * source variable, and the RDF property that should be tested to |
michael@0 | 177 | * generate a target value. The target value is bound to a target |
michael@0 | 178 | * variable. |
michael@0 | 179 | * |
michael@0 | 180 | * @param aSourceVariable the source variable that will be used in |
michael@0 | 181 | * the RDF query. |
michael@0 | 182 | * @param aExpr the expression that will be used in the query. |
michael@0 | 183 | * @param aTargetVariable the variable whose value will be bound |
michael@0 | 184 | * to the RDF node that is returned when querying the binding |
michael@0 | 185 | * @return NS_OK if no errors occur. |
michael@0 | 186 | */ |
michael@0 | 187 | nsresult AddBinding(nsIAtom* aSourceVariable, |
michael@0 | 188 | nsAString& aExpr, |
michael@0 | 189 | nsIAtom* aTargetVariable); |
michael@0 | 190 | |
michael@0 | 191 | /** |
michael@0 | 192 | * Inform the query processor of the bindings that are set for a rule. |
michael@0 | 193 | * This should be called after all the bindings for a rule are compiled. |
michael@0 | 194 | */ |
michael@0 | 195 | nsresult |
michael@0 | 196 | AddBindingsToQueryProcessor(nsIXULTemplateQueryProcessor* aProcessor); |
michael@0 | 197 | |
michael@0 | 198 | void Traverse(nsCycleCollectionTraversalCallback &cb) const |
michael@0 | 199 | { |
michael@0 | 200 | cb.NoteXPCOMChild(mRuleNode); |
michael@0 | 201 | cb.NoteXPCOMChild(mAction); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | protected: |
michael@0 | 205 | |
michael@0 | 206 | struct Binding { |
michael@0 | 207 | nsCOMPtr<nsIAtom> mSourceVariable; |
michael@0 | 208 | nsCOMPtr<nsIAtom> mTargetVariable; |
michael@0 | 209 | nsString mExpr; |
michael@0 | 210 | Binding* mNext; |
michael@0 | 211 | Binding* mParent; |
michael@0 | 212 | }; |
michael@0 | 213 | |
michael@0 | 214 | // backreference to the query set which owns this rule |
michael@0 | 215 | nsTemplateQuerySet* mQuerySet; |
michael@0 | 216 | |
michael@0 | 217 | // the <rule> node, or the <template> node if there is no <rule> |
michael@0 | 218 | nsCOMPtr<nsIDOMNode> mRuleNode; |
michael@0 | 219 | |
michael@0 | 220 | // the <action> node, or, if there is no <action>, the container node |
michael@0 | 221 | // which contains the content to generate |
michael@0 | 222 | nsCOMPtr<nsIContent> mAction; |
michael@0 | 223 | |
michael@0 | 224 | // the rule filter set by the builder's SetRuleFilter function |
michael@0 | 225 | nsCOMPtr<nsIXULTemplateRuleFilter> mRuleFilter; |
michael@0 | 226 | |
michael@0 | 227 | // indicates that the rule will only match when generating content |
michael@0 | 228 | // to be inserted into a container with this tag |
michael@0 | 229 | nsCOMPtr<nsIAtom> mTag; |
michael@0 | 230 | |
michael@0 | 231 | // linked-list of the bindings for the rule, owned by the rule. |
michael@0 | 232 | Binding* mBindings; |
michael@0 | 233 | |
michael@0 | 234 | nsCOMPtr<nsIAtom> mRefVariable; |
michael@0 | 235 | nsCOMPtr<nsIAtom> mMemberVariable; |
michael@0 | 236 | |
michael@0 | 237 | nsTemplateCondition* mConditions; // owned by nsTemplateRule |
michael@0 | 238 | }; |
michael@0 | 239 | |
michael@0 | 240 | /** nsTemplateQuerySet |
michael@0 | 241 | * |
michael@0 | 242 | * A single <queryset> which holds the query node and the rules for it. |
michael@0 | 243 | * All builders have at least one queryset, which may be created with an |
michael@0 | 244 | * explicit <queryset> tag or implied if the tag is not used. |
michael@0 | 245 | * |
michael@0 | 246 | * These queryset objects are created and owned by the builder in its |
michael@0 | 247 | * mQuerySets array. |
michael@0 | 248 | */ |
michael@0 | 249 | class nsTemplateQuerySet |
michael@0 | 250 | { |
michael@0 | 251 | protected: |
michael@0 | 252 | nsTArray<nsTemplateRule> mRules; |
michael@0 | 253 | |
michael@0 | 254 | // a number which increments for each successive queryset. It is stored so |
michael@0 | 255 | // it can be used as an optimization when updating results so that it is |
michael@0 | 256 | // known where to insert them into a match. |
michael@0 | 257 | int32_t mPriority; |
michael@0 | 258 | |
michael@0 | 259 | public: |
michael@0 | 260 | |
michael@0 | 261 | // <query> node |
michael@0 | 262 | nsCOMPtr<nsIContent> mQueryNode; |
michael@0 | 263 | |
michael@0 | 264 | // compiled opaque query object returned by the query processor's |
michael@0 | 265 | // CompileQuery call |
michael@0 | 266 | nsCOMPtr<nsISupports> mCompiledQuery; |
michael@0 | 267 | |
michael@0 | 268 | // indicates that the query will only generate content to be inserted into |
michael@0 | 269 | // a container with this tag |
michael@0 | 270 | nsCOMPtr<nsIAtom> mTag; |
michael@0 | 271 | |
michael@0 | 272 | nsTemplateQuerySet(int32_t aPriority) |
michael@0 | 273 | : mPriority(aPriority) |
michael@0 | 274 | { |
michael@0 | 275 | MOZ_COUNT_CTOR(nsTemplateQuerySet); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | ~nsTemplateQuerySet() |
michael@0 | 279 | { |
michael@0 | 280 | MOZ_COUNT_DTOR(nsTemplateQuerySet); |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | int32_t Priority() const |
michael@0 | 284 | { |
michael@0 | 285 | return mPriority; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | nsIAtom* GetTag() { return mTag; } |
michael@0 | 289 | void SetTag(nsIAtom* aTag) { mTag = aTag; } |
michael@0 | 290 | |
michael@0 | 291 | nsTemplateRule* NewRule(nsIContent* aRuleNode, |
michael@0 | 292 | nsIContent* aAction, |
michael@0 | 293 | nsTemplateQuerySet* aQuerySet) |
michael@0 | 294 | { |
michael@0 | 295 | // nsTemplateMatch stores the index as a 16-bit value, |
michael@0 | 296 | // so check to make sure for overflow |
michael@0 | 297 | if (mRules.Length() == INT16_MAX) |
michael@0 | 298 | return nullptr; |
michael@0 | 299 | |
michael@0 | 300 | return mRules.AppendElement(nsTemplateRule(aRuleNode, aAction, |
michael@0 | 301 | aQuerySet)); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | void RemoveRule(nsTemplateRule *aRule) |
michael@0 | 305 | { |
michael@0 | 306 | mRules.RemoveElementAt(aRule - mRules.Elements()); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | int16_t RuleCount() const |
michael@0 | 310 | { |
michael@0 | 311 | return mRules.Length(); |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | nsTemplateRule* GetRuleAt(int16_t aIndex) |
michael@0 | 315 | { |
michael@0 | 316 | if (uint32_t(aIndex) < mRules.Length()) { |
michael@0 | 317 | return &mRules[aIndex]; |
michael@0 | 318 | } |
michael@0 | 319 | return nullptr; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | void Clear() |
michael@0 | 323 | { |
michael@0 | 324 | mRules.Clear(); |
michael@0 | 325 | } |
michael@0 | 326 | }; |
michael@0 | 327 | |
michael@0 | 328 | #endif // nsTemplateRule_h__ |