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/. */
6 #include "nsRDFResource.h"
7 #include "nsIServiceManager.h"
8 #include "nsIRDFDelegateFactory.h"
9 #include "nsIRDFService.h"
12 #include "nsComponentManagerUtils.h"
13 #include "nsServiceManagerUtils.h"
15 static NS_DEFINE_CID(kRDFServiceCID
, NS_RDFSERVICE_CID
);
17 nsIRDFService
* nsRDFResource::gRDFService
= nullptr;
18 nsrefcnt
nsRDFResource::gRDFServiceRefCnt
= 0;
20 ////////////////////////////////////////////////////////////////////////////////
22 nsRDFResource::nsRDFResource(void)
27 nsRDFResource::~nsRDFResource(void)
29 // Release all of the delegate objects
31 DelegateEntry
* doomed
= mDelegates
;
32 mDelegates
= mDelegates
->mNext
;
39 gRDFService
->UnregisterResource(this);
41 if (--gRDFServiceRefCnt
== 0)
42 NS_RELEASE(gRDFService
);
45 NS_IMPL_ISUPPORTS(nsRDFResource
, nsIRDFResource
, nsIRDFNode
)
47 ////////////////////////////////////////////////////////////////////////////////
48 // nsIRDFNode methods:
51 nsRDFResource::EqualsNode(nsIRDFNode
* aNode
, bool* aResult
)
53 NS_PRECONDITION(aNode
!= nullptr, "null ptr");
55 return NS_ERROR_NULL_POINTER
;
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
);
65 else if (rv
== NS_NOINTERFACE
) {
74 ////////////////////////////////////////////////////////////////////////////////
75 // nsIRDFResource methods:
78 nsRDFResource::Init(const char* aURI
)
80 NS_PRECONDITION(aURI
!= nullptr, "null ptr");
82 return NS_ERROR_NULL_POINTER
;
86 if (gRDFServiceRefCnt
++ == 0) {
87 nsresult rv
= CallGetService(kRDFServiceCID
, &gRDFService
);
88 if (NS_FAILED(rv
)) return rv
;
91 // don't replace an existing resource with the same URI automatically
92 return gRDFService
->RegisterResource(this, true);
96 nsRDFResource::GetValue(char* *aURI
)
98 NS_ASSERTION(aURI
, "Null out param.");
100 *aURI
= ToNewCString(mURI
);
103 return NS_ERROR_OUT_OF_MEMORY
;
109 nsRDFResource::GetValueUTF8(nsACString
& aResult
)
116 nsRDFResource::GetValueConst(const char** aURI
)
123 nsRDFResource::EqualsString(const char* aURI
, bool* aResult
)
125 NS_PRECONDITION(aURI
!= nullptr, "null ptr");
127 return NS_ERROR_NULL_POINTER
;
129 NS_PRECONDITION(aResult
, "null ptr");
131 *aResult
= mURI
.Equals(aURI
);
136 nsRDFResource::GetDelegate(const char* aKey
, REFNSIID aIID
, void** aResult
)
138 NS_PRECONDITION(aKey
!= nullptr, "null ptr");
140 return NS_ERROR_NULL_POINTER
;
145 DelegateEntry
* entry
= mDelegates
;
147 if (entry
->mKey
.Equals(aKey
)) {
148 rv
= entry
->mDelegate
->QueryInterface(aIID
, aResult
);
152 entry
= entry
->mNext
;
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
.AppendLiteral("&scheme=");
160 int32_t i
= mURI
.FindChar(':');
161 contractID
+= StringHead(mURI
, i
);
163 nsCOMPtr
<nsIRDFDelegateFactory
> delegateFactory
=
164 do_CreateInstance(contractID
.get(), &rv
);
165 if (NS_FAILED(rv
)) return rv
;
167 rv
= delegateFactory
->CreateDelegate(this, aKey
, aIID
, aResult
);
168 if (NS_FAILED(rv
)) return rv
;
170 // Okay, we've successfully created a delegate. Let's remember it.
171 entry
= new DelegateEntry
;
173 NS_RELEASE(*reinterpret_cast<nsISupports
**>(aResult
));
174 return NS_ERROR_OUT_OF_MEMORY
;
178 entry
->mDelegate
= do_QueryInterface(*reinterpret_cast<nsISupports
**>(aResult
), &rv
);
180 NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
183 NS_RELEASE(*reinterpret_cast<nsISupports
**>(aResult
));
184 return NS_ERROR_FAILURE
;
187 entry
->mNext
= mDelegates
;
195 nsRDFResource::ReleaseDelegate(const char* aKey
)
197 NS_PRECONDITION(aKey
!= nullptr, "null ptr");
199 return NS_ERROR_NULL_POINTER
;
201 DelegateEntry
* entry
= mDelegates
;
202 DelegateEntry
** link
= &mDelegates
;
205 if (entry
->mKey
.Equals(aKey
)) {
206 *link
= entry
->mNext
;
211 link
= &(entry
->mNext
);
212 entry
= entry
->mNext
;
215 NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
221 ////////////////////////////////////////////////////////////////////////////////