1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "nsXULTemplateResultRDF.h"
7 #include "nsXULContentUtils.h"
8 
9 // XXXndeakin for some reason, making this class have classinfo breaks trees.
10 //#include "nsIDOMClassInfo.h"
11 
NS_IMPL_CYCLE_COLLECTION(nsXULTemplateResultRDF,mQuery)12 NS_IMPL_CYCLE_COLLECTION(nsXULTemplateResultRDF, mQuery)
13 
14 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULTemplateResultRDF)
15   NS_INTERFACE_MAP_ENTRY(nsIXULTemplateResult)
16   NS_INTERFACE_MAP_ENTRY(nsISupports)
17 NS_INTERFACE_MAP_END
18 
19 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULTemplateResultRDF)
20 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULTemplateResultRDF)
21 
22 nsXULTemplateResultRDF::nsXULTemplateResultRDF(nsIRDFResource* aNode)
23     : mQuery(nullptr),
24       mNode(aNode)
25 {
26 }
27 
nsXULTemplateResultRDF(nsRDFQuery * aQuery,const Instantiation & aInst,nsIRDFResource * aNode)28 nsXULTemplateResultRDF::nsXULTemplateResultRDF(nsRDFQuery* aQuery,
29                                                const Instantiation& aInst,
30                                                nsIRDFResource *aNode)
31     : mQuery(aQuery),
32       mNode(aNode),
33       mInst(aInst)
34 {
35 }
36 
~nsXULTemplateResultRDF()37 nsXULTemplateResultRDF::~nsXULTemplateResultRDF()
38 {
39 }
40 
41 NS_IMETHODIMP
GetIsContainer(bool * aIsContainer)42 nsXULTemplateResultRDF::GetIsContainer(bool* aIsContainer)
43 {
44     *aIsContainer = false;
45 
46     if (mNode) {
47         nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
48         if (processor)
49             return processor->CheckContainer(mNode, aIsContainer);
50     }
51 
52     return NS_OK;
53 }
54 
55 NS_IMETHODIMP
GetIsEmpty(bool * aIsEmpty)56 nsXULTemplateResultRDF::GetIsEmpty(bool* aIsEmpty)
57 {
58     *aIsEmpty = true;
59 
60     if (mNode) {
61         nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
62         if (processor)
63             return processor->CheckEmpty(mNode, aIsEmpty);
64     }
65 
66     return NS_OK;
67 }
68 
69 NS_IMETHODIMP
GetMayProcessChildren(bool * aMayProcessChildren)70 nsXULTemplateResultRDF::GetMayProcessChildren(bool* aMayProcessChildren)
71 {
72     // RDF always allows recursion
73     *aMayProcessChildren = true;
74     return NS_OK;
75 }
76 
77 NS_IMETHODIMP
GetId(nsAString & aId)78 nsXULTemplateResultRDF::GetId(nsAString& aId)
79 {
80     if (! mNode)
81         return NS_ERROR_FAILURE;
82 
83     const char* uri;
84     mNode->GetValueConst(&uri);
85 
86     CopyUTF8toUTF16(uri, aId);
87 
88     return NS_OK;
89 }
90 
91 NS_IMETHODIMP
GetResource(nsIRDFResource ** aResource)92 nsXULTemplateResultRDF::GetResource(nsIRDFResource** aResource)
93 {
94     *aResource = mNode;
95     NS_IF_ADDREF(*aResource);
96     return NS_OK;
97 }
98 
99 NS_IMETHODIMP
GetType(nsAString & aType)100 nsXULTemplateResultRDF::GetType(nsAString& aType)
101 {
102     aType.Truncate();
103 
104     nsresult rv = NS_OK;
105 
106     nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
107     if (processor) {
108         bool found;
109         rv = processor->CheckIsSeparator(mNode, &found);
110         if (NS_SUCCEEDED(rv) && found)
111             aType.AssignLiteral("separator");
112     }
113 
114     return rv;
115 }
116 
117 NS_IMETHODIMP
GetBindingFor(nsIAtom * aVar,nsAString & aValue)118 nsXULTemplateResultRDF::GetBindingFor(nsIAtom* aVar, nsAString& aValue)
119 {
120     nsCOMPtr<nsIRDFNode> val;
121     GetAssignment(aVar, getter_AddRefs(val));
122 
123     return nsXULContentUtils::GetTextForNode(val, aValue);
124 }
125 
126 NS_IMETHODIMP
GetBindingObjectFor(nsIAtom * aVar,nsISupports ** aValue)127 nsXULTemplateResultRDF::GetBindingObjectFor(nsIAtom* aVar, nsISupports** aValue)
128 {
129     GetAssignment(aVar, (nsIRDFNode **)aValue);
130 
131     return NS_OK;
132 }
133 
134 NS_IMETHODIMP
RuleMatched(nsISupports * aQuery,nsIDOMNode * aRuleNode)135 nsXULTemplateResultRDF::RuleMatched(nsISupports* aQuery, nsIDOMNode* aRuleNode)
136 {
137     // when a rule matches, set the bindings that must be used.
138     nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
139     if (processor) {
140         RDFBindingSet* bindings = processor->GetBindingsForRule(aRuleNode);
141         if (bindings) {
142             nsresult rv = mBindingValues.SetBindingSet(bindings);
143             if (NS_FAILED(rv))
144                 return rv;
145 
146             bindings->AddDependencies(mNode, this);
147         }
148     }
149 
150     return NS_OK;
151 }
152 
153 NS_IMETHODIMP
HasBeenRemoved()154 nsXULTemplateResultRDF::HasBeenRemoved()
155 {
156     // when a result is no longer used, clean up the dependencies and
157     // memory elements that refer to it
158     mBindingValues.RemoveDependencies(mNode, this);
159 
160     nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
161     if (processor)
162         processor->RemoveMemoryElements(mInst, this);
163 
164     return NS_OK;
165 }
166 
167 
168 void
GetAssignment(nsIAtom * aVar,nsIRDFNode ** aValue)169 nsXULTemplateResultRDF::GetAssignment(nsIAtom* aVar, nsIRDFNode** aValue)
170 {
171     // look up a variable in the assignments map
172     *aValue = nullptr;
173     mInst.mAssignments.GetAssignmentFor(aVar, aValue);
174 
175     // if not found, look up the variable in the bindings
176     if (! *aValue)
177         mBindingValues.GetAssignmentFor(this, aVar, aValue);
178 }
179 
180 
181 bool
SyncAssignments(nsIRDFResource * aSubject,nsIRDFResource * aPredicate,nsIRDFNode * aTarget)182 nsXULTemplateResultRDF::SyncAssignments(nsIRDFResource* aSubject,
183                                         nsIRDFResource* aPredicate,
184                                         nsIRDFNode* aTarget)
185 {
186     // synchronize the bindings when an assertion is added or removed
187     RDFBindingSet* bindingset = mBindingValues.GetBindingSet();
188     if (bindingset) {
189         return bindingset->SyncAssignments(aSubject, aPredicate, aTarget,
190             (aSubject == mNode) ? mQuery->GetMemberVariable() : nullptr,
191             this, mBindingValues);
192     }
193 
194     return false;
195 }
196 
197 bool
HasMemoryElement(const MemoryElement & aMemoryElement)198 nsXULTemplateResultRDF::HasMemoryElement(const MemoryElement& aMemoryElement)
199 {
200     MemoryElementSet::ConstIterator last = mInst.mSupport.Last();
201     for (MemoryElementSet::ConstIterator element = mInst.mSupport.First();
202                                          element != last; ++element) {
203         if ((*element).Equals(aMemoryElement))
204             return true;
205     }
206 
207     return false;
208 }
209