1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Pierre Phaneuf <pp@ludusdesign.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsRDFResource.h"
40 : #include "nsIServiceManager.h"
41 : #include "nsIRDFDelegateFactory.h"
42 : #include "nsIRDFService.h"
43 : #include "nsRDFCID.h"
44 : #include "prlog.h"
45 : #include "nsComponentManagerUtils.h"
46 : #include "nsServiceManagerUtils.h"
47 :
48 : static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
49 :
50 : nsIRDFService* nsRDFResource::gRDFService = nsnull;
51 : nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0;
52 :
53 : ////////////////////////////////////////////////////////////////////////////////
54 :
55 56424 : nsRDFResource::nsRDFResource(void)
56 56424 : : mDelegates(nsnull)
57 : {
58 56424 : }
59 :
60 169263 : nsRDFResource::~nsRDFResource(void)
61 : {
62 : // Release all of the delegate objects
63 112842 : while (mDelegates) {
64 0 : DelegateEntry* doomed = mDelegates;
65 0 : mDelegates = mDelegates->mNext;
66 0 : delete doomed;
67 : }
68 :
69 56421 : if (!gRDFService)
70 : return;
71 :
72 56421 : gRDFService->UnregisterResource(this);
73 :
74 56421 : if (--gRDFServiceRefCnt == 0)
75 1403 : NS_RELEASE(gRDFService);
76 225684 : }
77 :
78 2082402 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsRDFResource, nsIRDFResource, nsIRDFNode)
79 :
80 : ////////////////////////////////////////////////////////////////////////////////
81 : // nsIRDFNode methods:
82 :
83 : NS_IMETHODIMP
84 0 : nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult)
85 : {
86 0 : NS_PRECONDITION(aNode != nsnull, "null ptr");
87 0 : if (! aNode)
88 0 : return NS_ERROR_NULL_POINTER;
89 :
90 : nsresult rv;
91 : nsIRDFResource* resource;
92 0 : rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource);
93 0 : if (NS_SUCCEEDED(rv)) {
94 0 : *aResult = (static_cast<nsIRDFResource*>(this) == resource);
95 0 : NS_RELEASE(resource);
96 0 : return NS_OK;
97 : }
98 0 : else if (rv == NS_NOINTERFACE) {
99 0 : *aResult = false;
100 0 : return NS_OK;
101 : }
102 : else {
103 0 : return rv;
104 : }
105 : }
106 :
107 : ////////////////////////////////////////////////////////////////////////////////
108 : // nsIRDFResource methods:
109 :
110 : NS_IMETHODIMP
111 56424 : nsRDFResource::Init(const char* aURI)
112 : {
113 56424 : NS_PRECONDITION(aURI != nsnull, "null ptr");
114 56424 : if (! aURI)
115 0 : return NS_ERROR_NULL_POINTER;
116 :
117 56424 : mURI = aURI;
118 :
119 56424 : if (gRDFServiceRefCnt++ == 0) {
120 1404 : nsresult rv = CallGetService(kRDFServiceCID, &gRDFService);
121 1404 : if (NS_FAILED(rv)) return rv;
122 : }
123 :
124 : // don't replace an existing resource with the same URI automatically
125 56424 : return gRDFService->RegisterResource(this, true);
126 : }
127 :
128 : NS_IMETHODIMP
129 2 : nsRDFResource::GetValue(char* *aURI)
130 : {
131 2 : NS_ASSERTION(aURI, "Null out param.");
132 :
133 2 : *aURI = ToNewCString(mURI);
134 :
135 2 : if (!*aURI)
136 0 : return NS_ERROR_OUT_OF_MEMORY;
137 :
138 2 : return NS_OK;
139 : }
140 :
141 : NS_IMETHODIMP
142 1244 : nsRDFResource::GetValueUTF8(nsACString& aResult)
143 : {
144 1244 : aResult = mURI;
145 1244 : return NS_OK;
146 : }
147 :
148 : NS_IMETHODIMP
149 114325 : nsRDFResource::GetValueConst(const char** aURI)
150 : {
151 114325 : *aURI = mURI.get();
152 114325 : return NS_OK;
153 : }
154 :
155 : NS_IMETHODIMP
156 0 : nsRDFResource::EqualsString(const char* aURI, bool* aResult)
157 : {
158 0 : NS_PRECONDITION(aURI != nsnull, "null ptr");
159 0 : if (! aURI)
160 0 : return NS_ERROR_NULL_POINTER;
161 :
162 0 : NS_PRECONDITION(aResult, "null ptr");
163 :
164 0 : *aResult = mURI.Equals(aURI);
165 0 : return NS_OK;
166 : }
167 :
168 : NS_IMETHODIMP
169 0 : nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
170 : {
171 0 : NS_PRECONDITION(aKey != nsnull, "null ptr");
172 0 : if (! aKey)
173 0 : return NS_ERROR_NULL_POINTER;
174 :
175 : nsresult rv;
176 0 : *aResult = nsnull;
177 :
178 0 : DelegateEntry* entry = mDelegates;
179 0 : while (entry) {
180 0 : if (entry->mKey.Equals(aKey)) {
181 0 : rv = entry->mDelegate->QueryInterface(aIID, aResult);
182 0 : return rv;
183 : }
184 :
185 0 : entry = entry->mNext;
186 : }
187 :
188 : // Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1
189 0 : nsCAutoString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX);
190 0 : contractID.Append(aKey);
191 0 : contractID.Append("&scheme=");
192 :
193 0 : PRInt32 i = mURI.FindChar(':');
194 0 : contractID += StringHead(mURI, i);
195 :
196 : nsCOMPtr<nsIRDFDelegateFactory> delegateFactory =
197 0 : do_CreateInstance(contractID.get(), &rv);
198 0 : if (NS_FAILED(rv)) return rv;
199 :
200 0 : rv = delegateFactory->CreateDelegate(this, aKey, aIID, aResult);
201 0 : if (NS_FAILED(rv)) return rv;
202 :
203 : // Okay, we've successfully created a delegate. Let's remember it.
204 0 : entry = new DelegateEntry;
205 0 : if (! entry) {
206 0 : NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
207 0 : return NS_ERROR_OUT_OF_MEMORY;
208 : }
209 :
210 0 : entry->mKey = aKey;
211 0 : entry->mDelegate = do_QueryInterface(*reinterpret_cast<nsISupports**>(aResult), &rv);
212 0 : if (NS_FAILED(rv)) {
213 0 : NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
214 :
215 0 : delete entry;
216 0 : NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
217 0 : return NS_ERROR_FAILURE;
218 : }
219 :
220 0 : entry->mNext = mDelegates;
221 :
222 0 : mDelegates = entry;
223 :
224 0 : return NS_OK;
225 : }
226 :
227 : NS_IMETHODIMP
228 0 : nsRDFResource::ReleaseDelegate(const char* aKey)
229 : {
230 0 : NS_PRECONDITION(aKey != nsnull, "null ptr");
231 0 : if (! aKey)
232 0 : return NS_ERROR_NULL_POINTER;
233 :
234 0 : DelegateEntry* entry = mDelegates;
235 0 : DelegateEntry** link = &mDelegates;
236 :
237 0 : while (entry) {
238 0 : if (entry->mKey.Equals(aKey)) {
239 0 : *link = entry->mNext;
240 0 : delete entry;
241 0 : return NS_OK;
242 : }
243 :
244 0 : link = &(entry->mNext);
245 0 : entry = entry->mNext;
246 : }
247 :
248 0 : NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
249 0 : return NS_OK;
250 : }
251 :
252 :
253 :
254 : ////////////////////////////////////////////////////////////////////////////////
|