content/xul/templates/src/nsRDFPropertyTestNode.cpp

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 #include "nsRDFPropertyTestNode.h"
michael@0 7 #include "nsString.h"
michael@0 8 #include "nsXULContentUtils.h"
michael@0 9
michael@0 10 #include "prlog.h"
michael@0 11 #ifdef PR_LOGGING
michael@0 12 extern PRLogModuleInfo* gXULTemplateLog;
michael@0 13 #include "nsIRDFLiteral.h"
michael@0 14 #endif
michael@0 15
michael@0 16 nsRDFPropertyTestNode::nsRDFPropertyTestNode(TestNode* aParent,
michael@0 17 nsXULTemplateQueryProcessorRDF* aProcessor,
michael@0 18 nsIAtom* aSourceVariable,
michael@0 19 nsIRDFResource* aProperty,
michael@0 20 nsIAtom* aTargetVariable)
michael@0 21 : nsRDFTestNode(aParent),
michael@0 22 mProcessor(aProcessor),
michael@0 23 mSourceVariable(aSourceVariable),
michael@0 24 mSource(nullptr),
michael@0 25 mProperty(aProperty),
michael@0 26 mTargetVariable(aTargetVariable),
michael@0 27 mTarget(nullptr)
michael@0 28 {
michael@0 29 #ifdef PR_LOGGING
michael@0 30 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 31 const char* prop = "(null)";
michael@0 32 if (aProperty)
michael@0 33 aProperty->GetValueConst(&prop);
michael@0 34
michael@0 35 nsAutoString svar(NS_LITERAL_STRING("(none)"));
michael@0 36 if (mSourceVariable)
michael@0 37 mSourceVariable->ToString(svar);
michael@0 38
michael@0 39 nsAutoString tvar(NS_LITERAL_STRING("(none)"));
michael@0 40 if (mTargetVariable)
michael@0 41 mTargetVariable->ToString(tvar);
michael@0 42
michael@0 43 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 44 ("nsRDFPropertyTestNode[%p]: parent=%p source=%s property=%s target=%s",
michael@0 45 this, aParent, NS_ConvertUTF16toUTF8(svar).get(), prop, NS_ConvertUTF16toUTF8(tvar).get()));
michael@0 46 }
michael@0 47 #endif
michael@0 48 }
michael@0 49
michael@0 50
michael@0 51 nsRDFPropertyTestNode::nsRDFPropertyTestNode(TestNode* aParent,
michael@0 52 nsXULTemplateQueryProcessorRDF* aProcessor,
michael@0 53 nsIRDFResource* aSource,
michael@0 54 nsIRDFResource* aProperty,
michael@0 55 nsIAtom* aTargetVariable)
michael@0 56 : nsRDFTestNode(aParent),
michael@0 57 mProcessor(aProcessor),
michael@0 58 mSourceVariable(0),
michael@0 59 mSource(aSource),
michael@0 60 mProperty(aProperty),
michael@0 61 mTargetVariable(aTargetVariable),
michael@0 62 mTarget(nullptr)
michael@0 63 {
michael@0 64 #ifdef PR_LOGGING
michael@0 65 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 66 const char* source = "(null)";
michael@0 67 if (aSource)
michael@0 68 aSource->GetValueConst(&source);
michael@0 69
michael@0 70 const char* prop = "(null)";
michael@0 71 if (aProperty)
michael@0 72 aProperty->GetValueConst(&prop);
michael@0 73
michael@0 74 nsAutoString tvar(NS_LITERAL_STRING("(none)"));
michael@0 75 if (mTargetVariable)
michael@0 76 mTargetVariable->ToString(tvar);
michael@0 77
michael@0 78 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 79 ("nsRDFPropertyTestNode[%p]: parent=%p source=%s property=%s target=%s",
michael@0 80 this, aParent, source, prop, NS_ConvertUTF16toUTF8(tvar).get()));
michael@0 81 }
michael@0 82 #endif
michael@0 83 }
michael@0 84
michael@0 85
michael@0 86 nsRDFPropertyTestNode::nsRDFPropertyTestNode(TestNode* aParent,
michael@0 87 nsXULTemplateQueryProcessorRDF* aProcessor,
michael@0 88 nsIAtom* aSourceVariable,
michael@0 89 nsIRDFResource* aProperty,
michael@0 90 nsIRDFNode* aTarget)
michael@0 91 : nsRDFTestNode(aParent),
michael@0 92 mProcessor(aProcessor),
michael@0 93 mSourceVariable(aSourceVariable),
michael@0 94 mSource(nullptr),
michael@0 95 mProperty(aProperty),
michael@0 96 mTargetVariable(0),
michael@0 97 mTarget(aTarget)
michael@0 98 {
michael@0 99 #ifdef PR_LOGGING
michael@0 100 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 101 nsAutoString svar(NS_LITERAL_STRING("(none)"));
michael@0 102 if (mSourceVariable)
michael@0 103 mSourceVariable->ToString(svar);
michael@0 104
michael@0 105 const char* prop = "(null)";
michael@0 106 if (aProperty)
michael@0 107 aProperty->GetValueConst(&prop);
michael@0 108
michael@0 109 nsAutoString target;
michael@0 110 nsXULContentUtils::GetTextForNode(aTarget, target);
michael@0 111
michael@0 112 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 113 ("nsRDFPropertyTestNode[%p]: parent=%p source=%s property=%s target=%s",
michael@0 114 this, aParent, NS_ConvertUTF16toUTF8(svar).get(), prop, NS_ConvertUTF16toUTF8(target).get()));
michael@0 115 }
michael@0 116 #endif
michael@0 117 }
michael@0 118
michael@0 119
michael@0 120 nsresult
michael@0 121 nsRDFPropertyTestNode::FilterInstantiations(InstantiationSet& aInstantiations,
michael@0 122 bool* aCantHandleYet) const
michael@0 123 {
michael@0 124 nsresult rv;
michael@0 125
michael@0 126 if (aCantHandleYet)
michael@0 127 *aCantHandleYet = false;
michael@0 128
michael@0 129 nsIRDFDataSource* ds = mProcessor->GetDataSource();
michael@0 130
michael@0 131 InstantiationSet::Iterator last = aInstantiations.Last();
michael@0 132 for (InstantiationSet::Iterator inst = aInstantiations.First(); inst != last; ++inst) {
michael@0 133 bool hasSourceBinding;
michael@0 134 nsCOMPtr<nsIRDFResource> sourceRes;
michael@0 135
michael@0 136 if (mSource) {
michael@0 137 hasSourceBinding = true;
michael@0 138 sourceRes = mSource;
michael@0 139 }
michael@0 140 else {
michael@0 141 nsCOMPtr<nsIRDFNode> sourceValue;
michael@0 142 hasSourceBinding = inst->mAssignments.GetAssignmentFor(mSourceVariable,
michael@0 143 getter_AddRefs(sourceValue));
michael@0 144 sourceRes = do_QueryInterface(sourceValue);
michael@0 145 }
michael@0 146
michael@0 147 bool hasTargetBinding;
michael@0 148 nsCOMPtr<nsIRDFNode> targetValue;
michael@0 149
michael@0 150 if (mTarget) {
michael@0 151 hasTargetBinding = true;
michael@0 152 targetValue = mTarget;
michael@0 153 }
michael@0 154 else {
michael@0 155 hasTargetBinding = inst->mAssignments.GetAssignmentFor(mTargetVariable,
michael@0 156 getter_AddRefs(targetValue));
michael@0 157 }
michael@0 158
michael@0 159 #ifdef PR_LOGGING
michael@0 160 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 161 const char* source = "(unbound)";
michael@0 162 if (hasSourceBinding)
michael@0 163 sourceRes->GetValueConst(&source);
michael@0 164
michael@0 165 nsAutoString target(NS_LITERAL_STRING("(unbound)"));
michael@0 166 if (hasTargetBinding)
michael@0 167 nsXULContentUtils::GetTextForNode(targetValue, target);
michael@0 168
michael@0 169 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 170 ("nsRDFPropertyTestNode[%p]: FilterInstantiations() source=[%s] target=[%s]",
michael@0 171 this, source, NS_ConvertUTF16toUTF8(target).get()));
michael@0 172 }
michael@0 173 #endif
michael@0 174
michael@0 175 if (hasSourceBinding && hasTargetBinding) {
michael@0 176 // it's a consistency check. see if we have a assignment that is consistent
michael@0 177 bool hasAssertion;
michael@0 178 rv = ds->HasAssertion(sourceRes, mProperty, targetValue,
michael@0 179 true, &hasAssertion);
michael@0 180 if (NS_FAILED(rv)) return rv;
michael@0 181
michael@0 182 #ifdef PR_LOGGING
michael@0 183 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 184 (" consistency check => %s", hasAssertion ? "passed" : "failed"));
michael@0 185 #endif
michael@0 186
michael@0 187 if (hasAssertion) {
michael@0 188 // it's consistent.
michael@0 189 Element* element =
michael@0 190 new nsRDFPropertyTestNode::Element(sourceRes, mProperty,
michael@0 191 targetValue);
michael@0 192
michael@0 193 if (! element)
michael@0 194 return NS_ERROR_OUT_OF_MEMORY;
michael@0 195
michael@0 196 inst->AddSupportingElement(element);
michael@0 197 }
michael@0 198 else {
michael@0 199 // it's inconsistent. remove it.
michael@0 200 aInstantiations.Erase(inst--);
michael@0 201 }
michael@0 202 }
michael@0 203 else if ((hasSourceBinding && ! hasTargetBinding) ||
michael@0 204 (! hasSourceBinding && hasTargetBinding)) {
michael@0 205 // it's an open ended query on the source or
michael@0 206 // target. figure out what matches and add as a
michael@0 207 // cross-product.
michael@0 208 nsCOMPtr<nsISimpleEnumerator> results;
michael@0 209 if (hasSourceBinding) {
michael@0 210 rv = ds->GetTargets(sourceRes,
michael@0 211 mProperty,
michael@0 212 true,
michael@0 213 getter_AddRefs(results));
michael@0 214 }
michael@0 215 else {
michael@0 216 rv = ds->GetSources(mProperty,
michael@0 217 targetValue,
michael@0 218 true,
michael@0 219 getter_AddRefs(results));
michael@0 220 if (NS_FAILED(rv)) return rv;
michael@0 221 }
michael@0 222
michael@0 223 while (1) {
michael@0 224 bool hasMore;
michael@0 225 rv = results->HasMoreElements(&hasMore);
michael@0 226 if (NS_FAILED(rv)) return rv;
michael@0 227
michael@0 228 if (! hasMore)
michael@0 229 break;
michael@0 230
michael@0 231 nsCOMPtr<nsISupports> isupports;
michael@0 232 rv = results->GetNext(getter_AddRefs(isupports));
michael@0 233 if (NS_FAILED(rv)) return rv;
michael@0 234
michael@0 235 nsIAtom* variable;
michael@0 236 nsCOMPtr<nsIRDFNode> value;
michael@0 237
michael@0 238 if (hasSourceBinding) {
michael@0 239 variable = mTargetVariable;
michael@0 240
michael@0 241 value = do_QueryInterface(isupports);
michael@0 242 NS_ASSERTION(value != nullptr, "target is not an nsIRDFNode");
michael@0 243
michael@0 244 #ifdef PR_LOGGING
michael@0 245 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 246 nsAutoString s(NS_LITERAL_STRING("(none found)"));
michael@0 247 if (value)
michael@0 248 nsXULContentUtils::GetTextForNode(value, s);
michael@0 249
michael@0 250 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 251 (" target => %s", NS_ConvertUTF16toUTF8(s).get()));
michael@0 252 }
michael@0 253 #endif
michael@0 254
michael@0 255 if (! value) continue;
michael@0 256
michael@0 257 targetValue = value;
michael@0 258 }
michael@0 259 else {
michael@0 260 variable = mSourceVariable;
michael@0 261
michael@0 262 nsCOMPtr<nsIRDFResource> source = do_QueryInterface(isupports);
michael@0 263 NS_ASSERTION(source != nullptr, "source is not an nsIRDFResource");
michael@0 264
michael@0 265 #ifdef PR_LOGGING
michael@0 266 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 267 const char* s = "(none found)";
michael@0 268 if (source)
michael@0 269 source->GetValueConst(&s);
michael@0 270
michael@0 271 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 272 (" source => %s", s));
michael@0 273 }
michael@0 274 #endif
michael@0 275
michael@0 276 if (! source) continue;
michael@0 277
michael@0 278 value = sourceRes = source;
michael@0 279 }
michael@0 280
michael@0 281 // Copy the original instantiation, and add it to the
michael@0 282 // instantiation set with the new assignment that we've
michael@0 283 // introduced. Ownership will be transferred to the
michael@0 284 Instantiation newinst = *inst;
michael@0 285 newinst.AddAssignment(variable, value);
michael@0 286
michael@0 287 Element* element =
michael@0 288 new nsRDFPropertyTestNode::Element(sourceRes, mProperty,
michael@0 289 targetValue);
michael@0 290
michael@0 291 if (! element)
michael@0 292 return NS_ERROR_OUT_OF_MEMORY;
michael@0 293
michael@0 294 newinst.AddSupportingElement(element);
michael@0 295
michael@0 296 aInstantiations.Insert(inst, newinst);
michael@0 297 }
michael@0 298
michael@0 299 // finally, remove the "under specified" instantiation.
michael@0 300 aInstantiations.Erase(inst--);
michael@0 301 }
michael@0 302 else {
michael@0 303 if (!aCantHandleYet) {
michael@0 304 nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_TRIPLE_UNBOUND);
michael@0 305 // Neither source nor target assignment!
michael@0 306 return NS_ERROR_UNEXPECTED;
michael@0 307 }
michael@0 308
michael@0 309 *aCantHandleYet = true;
michael@0 310 return NS_OK;
michael@0 311 }
michael@0 312 }
michael@0 313
michael@0 314 return NS_OK;
michael@0 315 }
michael@0 316
michael@0 317 bool
michael@0 318 nsRDFPropertyTestNode::CanPropagate(nsIRDFResource* aSource,
michael@0 319 nsIRDFResource* aProperty,
michael@0 320 nsIRDFNode* aTarget,
michael@0 321 Instantiation& aInitialBindings) const
michael@0 322 {
michael@0 323 bool result;
michael@0 324
michael@0 325 if ((mProperty.get() != aProperty) ||
michael@0 326 (mSource && mSource.get() != aSource) ||
michael@0 327 (mTarget && mTarget.get() != aTarget)) {
michael@0 328 result = false;
michael@0 329 }
michael@0 330 else {
michael@0 331 if (mSourceVariable)
michael@0 332 aInitialBindings.AddAssignment(mSourceVariable, aSource);
michael@0 333
michael@0 334 if (mTargetVariable)
michael@0 335 aInitialBindings.AddAssignment(mTargetVariable, aTarget);
michael@0 336
michael@0 337 result = true;
michael@0 338 }
michael@0 339
michael@0 340 #ifdef PR_LOGGING
michael@0 341 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 342 const char* source;
michael@0 343 aSource->GetValueConst(&source);
michael@0 344
michael@0 345 const char* property;
michael@0 346 aProperty->GetValueConst(&property);
michael@0 347
michael@0 348 nsAutoString target;
michael@0 349 nsXULContentUtils::GetTextForNode(aTarget, target);
michael@0 350
michael@0 351 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 352 ("nsRDFPropertyTestNode[%p]: CanPropagate([%s]==[%s]=>[%s]) => %s",
michael@0 353 this, source, property, NS_ConvertUTF16toUTF8(target).get(),
michael@0 354 result ? "true" : "false"));
michael@0 355 }
michael@0 356 #endif
michael@0 357
michael@0 358 return result;
michael@0 359 }
michael@0 360
michael@0 361 void
michael@0 362 nsRDFPropertyTestNode::Retract(nsIRDFResource* aSource,
michael@0 363 nsIRDFResource* aProperty,
michael@0 364 nsIRDFNode* aTarget) const
michael@0 365 {
michael@0 366 if (aProperty == mProperty.get()) {
michael@0 367 #ifdef PR_LOGGING
michael@0 368 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 369 const char* source;
michael@0 370 aSource->GetValueConst(&source);
michael@0 371
michael@0 372 const char* property;
michael@0 373 aProperty->GetValueConst(&property);
michael@0 374
michael@0 375 nsAutoString target;
michael@0 376 nsXULContentUtils::GetTextForNode(aTarget, target);
michael@0 377
michael@0 378 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 379 ("nsRDFPropertyTestNode[%p]: Retract([%s]==[%s]=>[%s])",
michael@0 380 this, source, property, NS_ConvertUTF16toUTF8(target).get()));
michael@0 381 }
michael@0 382 #endif
michael@0 383
michael@0 384 mProcessor->RetractElement(Element(aSource, aProperty, aTarget));
michael@0 385 }
michael@0 386 }
michael@0 387

mercurial