content/xul/templates/src/nsRDFConInstanceTestNode.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 "nsIComponentManager.h"
michael@0 7 #include "nsIRDFContainer.h"
michael@0 8 #include "nsIRDFContainerUtils.h"
michael@0 9 #include "nsIServiceManager.h"
michael@0 10 #include "nsRDFCID.h"
michael@0 11 #include "nsRDFConInstanceTestNode.h"
michael@0 12 #include "nsResourceSet.h"
michael@0 13
michael@0 14 #include "prlog.h"
michael@0 15 #ifdef PR_LOGGING
michael@0 16 #include "nsXULContentUtils.h"
michael@0 17 extern PRLogModuleInfo* gXULTemplateLog;
michael@0 18
michael@0 19 static const char*
michael@0 20 TestToString(nsRDFConInstanceTestNode::Test aTest) {
michael@0 21 switch (aTest) {
michael@0 22 case nsRDFConInstanceTestNode::eFalse: return "false";
michael@0 23 case nsRDFConInstanceTestNode::eTrue: return "true";
michael@0 24 case nsRDFConInstanceTestNode::eDontCare: return "dontcare";
michael@0 25 }
michael@0 26 return "?";
michael@0 27 }
michael@0 28 #endif
michael@0 29
michael@0 30 nsRDFConInstanceTestNode::nsRDFConInstanceTestNode(TestNode* aParent,
michael@0 31 nsXULTemplateQueryProcessorRDF* aProcessor,
michael@0 32 nsIAtom* aContainerVariable,
michael@0 33 Test aContainer,
michael@0 34 Test aEmpty)
michael@0 35 : nsRDFTestNode(aParent),
michael@0 36 mProcessor(aProcessor),
michael@0 37 mContainerVariable(aContainerVariable),
michael@0 38 mContainer(aContainer),
michael@0 39 mEmpty(aEmpty)
michael@0 40 {
michael@0 41 #ifdef PR_LOGGING
michael@0 42 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 43 nsAutoCString props;
michael@0 44
michael@0 45 nsResourceSet& containmentProps = aProcessor->ContainmentProperties();
michael@0 46 nsResourceSet::ConstIterator last = containmentProps.Last();
michael@0 47 nsResourceSet::ConstIterator first = containmentProps.First();
michael@0 48 nsResourceSet::ConstIterator iter;
michael@0 49
michael@0 50 for (iter = first; iter != last; ++iter) {
michael@0 51 if (iter != first)
michael@0 52 props += " ";
michael@0 53
michael@0 54 const char* str;
michael@0 55 iter->GetValueConst(&str);
michael@0 56
michael@0 57 props += str;
michael@0 58 }
michael@0 59
michael@0 60 nsAutoString cvar(NS_LITERAL_STRING("(none)"));
michael@0 61 if (mContainerVariable)
michael@0 62 mContainerVariable->ToString(cvar);
michael@0 63
michael@0 64 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 65 ("nsRDFConInstanceTestNode[%p]: parent=%p member-props=(%s) container-var=%s container=%s empty=%s",
michael@0 66 this,
michael@0 67 aParent,
michael@0 68 props.get(),
michael@0 69 NS_ConvertUTF16toUTF8(cvar).get(),
michael@0 70 TestToString(aContainer),
michael@0 71 TestToString(aEmpty)));
michael@0 72 }
michael@0 73 #endif
michael@0 74 }
michael@0 75
michael@0 76 nsresult
michael@0 77 nsRDFConInstanceTestNode::FilterInstantiations(InstantiationSet& aInstantiations,
michael@0 78 bool* aCantHandleYet) const
michael@0 79 {
michael@0 80 nsresult rv;
michael@0 81
michael@0 82 if (aCantHandleYet)
michael@0 83 *aCantHandleYet = false;
michael@0 84
michael@0 85 nsCOMPtr<nsIRDFContainerUtils> rdfc
michael@0 86 = do_GetService("@mozilla.org/rdf/container-utils;1");
michael@0 87
michael@0 88 if (! rdfc)
michael@0 89 return NS_ERROR_FAILURE;
michael@0 90
michael@0 91 nsIRDFDataSource* ds = mProcessor->GetDataSource();
michael@0 92
michael@0 93 InstantiationSet::Iterator last = aInstantiations.Last();
michael@0 94 for (InstantiationSet::Iterator inst = aInstantiations.First(); inst != last; ++inst) {
michael@0 95 nsCOMPtr<nsIRDFNode> value;
michael@0 96 if (! inst->mAssignments.GetAssignmentFor(mContainerVariable, getter_AddRefs(value))) {
michael@0 97 NS_ERROR("can't do unbounded container testing");
michael@0 98 return NS_ERROR_UNEXPECTED;
michael@0 99 }
michael@0 100
michael@0 101 nsCOMPtr<nsIRDFResource> valueres = do_QueryInterface(value);
michael@0 102 if (! valueres) {
michael@0 103 aInstantiations.Erase(inst--);
michael@0 104 continue;
michael@0 105 }
michael@0 106
michael@0 107 #ifdef PR_LOGGING
michael@0 108 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 109 const char* container = "(unbound)";
michael@0 110 valueres->GetValueConst(&container);
michael@0 111
michael@0 112 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 113 ("nsRDFConInstanceTestNode[%p]::FilterInstantiations() container=[%s]",
michael@0 114 this, container));
michael@0 115 }
michael@0 116 #endif
michael@0 117
michael@0 118 nsCOMPtr<nsIRDFContainer> rdfcontainer;
michael@0 119
michael@0 120 bool isRDFContainer;
michael@0 121 rv = rdfc->IsContainer(ds, valueres, &isRDFContainer);
michael@0 122 if (NS_FAILED(rv)) return rv;
michael@0 123
michael@0 124 if (mEmpty != eDontCare || mContainer != eDontCare) {
michael@0 125 Test empty = eDontCare;
michael@0 126 Test container = eDontCare;
michael@0 127
michael@0 128 if (isRDFContainer) {
michael@0 129 // It's an RDF container. Use the container utilities
michael@0 130 // to deduce what's in it.
michael@0 131 container = eTrue;
michael@0 132
michael@0 133 // XXX should cache the factory
michael@0 134 rdfcontainer = do_CreateInstance("@mozilla.org/rdf/container;1", &rv);
michael@0 135 if (NS_FAILED(rv)) return rv;
michael@0 136
michael@0 137 rv = rdfcontainer->Init(ds, valueres);
michael@0 138 if (NS_FAILED(rv)) return rv;
michael@0 139
michael@0 140 int32_t count;
michael@0 141 rv = rdfcontainer->GetCount(&count);
michael@0 142 if (NS_FAILED(rv)) return rv;
michael@0 143
michael@0 144 empty = (count == 0) ? eTrue : eFalse;
michael@0 145 } else {
michael@0 146 empty = eTrue;
michael@0 147 container = eFalse;
michael@0 148
michael@0 149 // First do the simple check of finding some outward
michael@0 150 // arcs; there should be only a few containment arcs, so this can
michael@0 151 // save us time from dealing with an iterator later on
michael@0 152 nsResourceSet& containmentProps = mProcessor->ContainmentProperties();
michael@0 153 for (nsResourceSet::ConstIterator property = containmentProps.First();
michael@0 154 property != containmentProps.Last();
michael@0 155 ++property) {
michael@0 156 nsCOMPtr<nsIRDFNode> target;
michael@0 157 rv = ds->GetTarget(valueres, *property, true, getter_AddRefs(target));
michael@0 158 if (NS_FAILED(rv)) return rv;
michael@0 159
michael@0 160 if (target != nullptr) {
michael@0 161 // bingo. we found one.
michael@0 162 empty = eFalse;
michael@0 163 container = eTrue;
michael@0 164 break;
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168 // if we still don't think its a container, but we
michael@0 169 // want to know for sure whether it is or not, we need
michael@0 170 // to check ArcLabelsOut for potential container arcs.
michael@0 171 if (container == eFalse && mContainer != eDontCare) {
michael@0 172 nsCOMPtr<nsISimpleEnumerator> arcsout;
michael@0 173 rv = ds->ArcLabelsOut(valueres, getter_AddRefs(arcsout));
michael@0 174 if (NS_FAILED(rv)) return rv;
michael@0 175
michael@0 176 while (1) {
michael@0 177 bool hasmore;
michael@0 178 rv = arcsout->HasMoreElements(&hasmore);
michael@0 179 if (NS_FAILED(rv)) return rv;
michael@0 180
michael@0 181 if (! hasmore)
michael@0 182 break;
michael@0 183
michael@0 184 nsCOMPtr<nsISupports> isupports;
michael@0 185 rv = arcsout->GetNext(getter_AddRefs(isupports));
michael@0 186 if (NS_FAILED(rv)) return rv;
michael@0 187
michael@0 188 nsCOMPtr<nsIRDFResource> property = do_QueryInterface(isupports);
michael@0 189 NS_ASSERTION(property != nullptr, "not a property");
michael@0 190 if (! property)
michael@0 191 return NS_ERROR_UNEXPECTED;
michael@0 192
michael@0 193 if (mProcessor->ContainmentProperties().Contains(property)) {
michael@0 194 container = eTrue;
michael@0 195 break;
michael@0 196 }
michael@0 197 }
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 202 (" empty => %s",
michael@0 203 (empty == mEmpty) ? "consistent" : "inconsistent"));
michael@0 204
michael@0 205 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 206 (" container => %s",
michael@0 207 (container == mContainer) ? "consistent" : "inconsistent"));
michael@0 208
michael@0 209 if (((mEmpty == empty) && (mContainer == container)) ||
michael@0 210 ((mEmpty == eDontCare) && (mContainer == container)) ||
michael@0 211 ((mContainer == eDontCare) && (mEmpty == empty)))
michael@0 212 {
michael@0 213 Element* element =
michael@0 214 new nsRDFConInstanceTestNode::Element(valueres, container, empty);
michael@0 215
michael@0 216 if (! element)
michael@0 217 return NS_ERROR_OUT_OF_MEMORY;
michael@0 218
michael@0 219 inst->AddSupportingElement(element);
michael@0 220 }
michael@0 221 else {
michael@0 222 aInstantiations.Erase(inst--);
michael@0 223 }
michael@0 224 }
michael@0 225 }
michael@0 226
michael@0 227 return NS_OK;
michael@0 228 }
michael@0 229
michael@0 230 bool
michael@0 231 nsRDFConInstanceTestNode::CanPropagate(nsIRDFResource* aSource,
michael@0 232 nsIRDFResource* aProperty,
michael@0 233 nsIRDFNode* aTarget,
michael@0 234 Instantiation& aInitialBindings) const
michael@0 235 {
michael@0 236 nsresult rv;
michael@0 237
michael@0 238 bool canpropagate = false;
michael@0 239
michael@0 240 nsCOMPtr<nsIRDFContainerUtils> rdfc
michael@0 241 = do_GetService("@mozilla.org/rdf/container-utils;1");
michael@0 242
michael@0 243 if (! rdfc)
michael@0 244 return false;
michael@0 245
michael@0 246 // We can certainly propagate ordinal properties
michael@0 247 rv = rdfc->IsOrdinalProperty(aProperty, &canpropagate);
michael@0 248 if (NS_FAILED(rv)) return false;
michael@0 249
michael@0 250 if (! canpropagate) {
michael@0 251 canpropagate = mProcessor->ContainmentProperties().Contains(aProperty);
michael@0 252 }
michael@0 253
michael@0 254 #ifdef PR_LOGGING
michael@0 255 if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
michael@0 256 const char* source;
michael@0 257 aSource->GetValueConst(&source);
michael@0 258
michael@0 259 const char* property;
michael@0 260 aProperty->GetValueConst(&property);
michael@0 261
michael@0 262 nsAutoString target;
michael@0 263 nsXULContentUtils::GetTextForNode(aTarget, target);
michael@0 264
michael@0 265 PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
michael@0 266 ("nsRDFConInstanceTestNode[%p]: CanPropagate([%s]==[%s]=>[%s]) => %s",
michael@0 267 this, source, property, NS_ConvertUTF16toUTF8(target).get(),
michael@0 268 canpropagate ? "true" : "false"));
michael@0 269 }
michael@0 270 #endif
michael@0 271
michael@0 272 if (canpropagate) {
michael@0 273 aInitialBindings.AddAssignment(mContainerVariable, aSource);
michael@0 274 return true;
michael@0 275 }
michael@0 276
michael@0 277 return false;
michael@0 278 }
michael@0 279
michael@0 280 void
michael@0 281 nsRDFConInstanceTestNode::Retract(nsIRDFResource* aSource,
michael@0 282 nsIRDFResource* aProperty,
michael@0 283 nsIRDFNode* aTarget) const
michael@0 284 {
michael@0 285 // XXXwaterson oof. complicated. figure this out.
michael@0 286 if (0) {
michael@0 287 mProcessor->RetractElement(Element(aSource, mContainer, mEmpty));
michael@0 288 }
michael@0 289 }
michael@0 290

mercurial