|
1 /* -*- Mode: C++; tab-width: 2; 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 "nsRDFResource.h" |
|
7 #include "nsIServiceManager.h" |
|
8 #include "nsIRDFDelegateFactory.h" |
|
9 #include "nsIRDFService.h" |
|
10 #include "nsRDFCID.h" |
|
11 #include "prlog.h" |
|
12 #include "nsComponentManagerUtils.h" |
|
13 #include "nsServiceManagerUtils.h" |
|
14 |
|
15 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); |
|
16 |
|
17 nsIRDFService* nsRDFResource::gRDFService = nullptr; |
|
18 nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0; |
|
19 |
|
20 //////////////////////////////////////////////////////////////////////////////// |
|
21 |
|
22 nsRDFResource::nsRDFResource(void) |
|
23 : mDelegates(nullptr) |
|
24 { |
|
25 } |
|
26 |
|
27 nsRDFResource::~nsRDFResource(void) |
|
28 { |
|
29 // Release all of the delegate objects |
|
30 while (mDelegates) { |
|
31 DelegateEntry* doomed = mDelegates; |
|
32 mDelegates = mDelegates->mNext; |
|
33 delete doomed; |
|
34 } |
|
35 |
|
36 if (!gRDFService) |
|
37 return; |
|
38 |
|
39 gRDFService->UnregisterResource(this); |
|
40 |
|
41 if (--gRDFServiceRefCnt == 0) |
|
42 NS_RELEASE(gRDFService); |
|
43 } |
|
44 |
|
45 NS_IMPL_ISUPPORTS(nsRDFResource, nsIRDFResource, nsIRDFNode) |
|
46 |
|
47 //////////////////////////////////////////////////////////////////////////////// |
|
48 // nsIRDFNode methods: |
|
49 |
|
50 NS_IMETHODIMP |
|
51 nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult) |
|
52 { |
|
53 NS_PRECONDITION(aNode != nullptr, "null ptr"); |
|
54 if (! aNode) |
|
55 return NS_ERROR_NULL_POINTER; |
|
56 |
|
57 nsresult rv; |
|
58 nsIRDFResource* resource; |
|
59 rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource); |
|
60 if (NS_SUCCEEDED(rv)) { |
|
61 *aResult = (static_cast<nsIRDFResource*>(this) == resource); |
|
62 NS_RELEASE(resource); |
|
63 return NS_OK; |
|
64 } |
|
65 else if (rv == NS_NOINTERFACE) { |
|
66 *aResult = false; |
|
67 return NS_OK; |
|
68 } |
|
69 else { |
|
70 return rv; |
|
71 } |
|
72 } |
|
73 |
|
74 //////////////////////////////////////////////////////////////////////////////// |
|
75 // nsIRDFResource methods: |
|
76 |
|
77 NS_IMETHODIMP |
|
78 nsRDFResource::Init(const char* aURI) |
|
79 { |
|
80 NS_PRECONDITION(aURI != nullptr, "null ptr"); |
|
81 if (! aURI) |
|
82 return NS_ERROR_NULL_POINTER; |
|
83 |
|
84 mURI = aURI; |
|
85 |
|
86 if (gRDFServiceRefCnt++ == 0) { |
|
87 nsresult rv = CallGetService(kRDFServiceCID, &gRDFService); |
|
88 if (NS_FAILED(rv)) return rv; |
|
89 } |
|
90 |
|
91 // don't replace an existing resource with the same URI automatically |
|
92 return gRDFService->RegisterResource(this, true); |
|
93 } |
|
94 |
|
95 NS_IMETHODIMP |
|
96 nsRDFResource::GetValue(char* *aURI) |
|
97 { |
|
98 NS_ASSERTION(aURI, "Null out param."); |
|
99 |
|
100 *aURI = ToNewCString(mURI); |
|
101 |
|
102 if (!*aURI) |
|
103 return NS_ERROR_OUT_OF_MEMORY; |
|
104 |
|
105 return NS_OK; |
|
106 } |
|
107 |
|
108 NS_IMETHODIMP |
|
109 nsRDFResource::GetValueUTF8(nsACString& aResult) |
|
110 { |
|
111 aResult = mURI; |
|
112 return NS_OK; |
|
113 } |
|
114 |
|
115 NS_IMETHODIMP |
|
116 nsRDFResource::GetValueConst(const char** aURI) |
|
117 { |
|
118 *aURI = mURI.get(); |
|
119 return NS_OK; |
|
120 } |
|
121 |
|
122 NS_IMETHODIMP |
|
123 nsRDFResource::EqualsString(const char* aURI, bool* aResult) |
|
124 { |
|
125 NS_PRECONDITION(aURI != nullptr, "null ptr"); |
|
126 if (! aURI) |
|
127 return NS_ERROR_NULL_POINTER; |
|
128 |
|
129 NS_PRECONDITION(aResult, "null ptr"); |
|
130 |
|
131 *aResult = mURI.Equals(aURI); |
|
132 return NS_OK; |
|
133 } |
|
134 |
|
135 NS_IMETHODIMP |
|
136 nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult) |
|
137 { |
|
138 NS_PRECONDITION(aKey != nullptr, "null ptr"); |
|
139 if (! aKey) |
|
140 return NS_ERROR_NULL_POINTER; |
|
141 |
|
142 nsresult rv; |
|
143 *aResult = nullptr; |
|
144 |
|
145 DelegateEntry* entry = mDelegates; |
|
146 while (entry) { |
|
147 if (entry->mKey.Equals(aKey)) { |
|
148 rv = entry->mDelegate->QueryInterface(aIID, aResult); |
|
149 return rv; |
|
150 } |
|
151 |
|
152 entry = entry->mNext; |
|
153 } |
|
154 |
|
155 // Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1 |
|
156 nsAutoCString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX); |
|
157 contractID.Append(aKey); |
|
158 contractID.Append("&scheme="); |
|
159 |
|
160 int32_t i = mURI.FindChar(':'); |
|
161 contractID += StringHead(mURI, i); |
|
162 |
|
163 nsCOMPtr<nsIRDFDelegateFactory> delegateFactory = |
|
164 do_CreateInstance(contractID.get(), &rv); |
|
165 if (NS_FAILED(rv)) return rv; |
|
166 |
|
167 rv = delegateFactory->CreateDelegate(this, aKey, aIID, aResult); |
|
168 if (NS_FAILED(rv)) return rv; |
|
169 |
|
170 // Okay, we've successfully created a delegate. Let's remember it. |
|
171 entry = new DelegateEntry; |
|
172 if (! entry) { |
|
173 NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult)); |
|
174 return NS_ERROR_OUT_OF_MEMORY; |
|
175 } |
|
176 |
|
177 entry->mKey = aKey; |
|
178 entry->mDelegate = do_QueryInterface(*reinterpret_cast<nsISupports**>(aResult), &rv); |
|
179 if (NS_FAILED(rv)) { |
|
180 NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!"); |
|
181 |
|
182 delete entry; |
|
183 NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult)); |
|
184 return NS_ERROR_FAILURE; |
|
185 } |
|
186 |
|
187 entry->mNext = mDelegates; |
|
188 |
|
189 mDelegates = entry; |
|
190 |
|
191 return NS_OK; |
|
192 } |
|
193 |
|
194 NS_IMETHODIMP |
|
195 nsRDFResource::ReleaseDelegate(const char* aKey) |
|
196 { |
|
197 NS_PRECONDITION(aKey != nullptr, "null ptr"); |
|
198 if (! aKey) |
|
199 return NS_ERROR_NULL_POINTER; |
|
200 |
|
201 DelegateEntry* entry = mDelegates; |
|
202 DelegateEntry** link = &mDelegates; |
|
203 |
|
204 while (entry) { |
|
205 if (entry->mKey.Equals(aKey)) { |
|
206 *link = entry->mNext; |
|
207 delete entry; |
|
208 return NS_OK; |
|
209 } |
|
210 |
|
211 link = &(entry->mNext); |
|
212 entry = entry->mNext; |
|
213 } |
|
214 |
|
215 NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found"); |
|
216 return NS_OK; |
|
217 } |
|
218 |
|
219 |
|
220 |
|
221 //////////////////////////////////////////////////////////////////////////////// |