Merge mozilla-b2g34 to 2.1s. a=merge
[gecko.git] / rdf / util / nsRDFResource.cpp
blobf7920667b1dee623c43e957dd5a5a0a55ffb0b46
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"
10 #include "nsRDFCID.h"
11 #include "prlog.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)
23 : mDelegates(nullptr)
27 nsRDFResource::~nsRDFResource(void)
29 // Release all of the delegate objects
30 while (mDelegates) {
31 DelegateEntry* doomed = mDelegates;
32 mDelegates = mDelegates->mNext;
33 delete doomed;
36 if (!gRDFService)
37 return;
39 gRDFService->UnregisterResource(this);
41 if (--gRDFServiceRefCnt == 0)
42 NS_RELEASE(gRDFService);
45 NS_IMPL_ISUPPORTS(nsRDFResource, nsIRDFResource, nsIRDFNode)
47 ////////////////////////////////////////////////////////////////////////////////
48 // nsIRDFNode methods:
50 NS_IMETHODIMP
51 nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult)
53 NS_PRECONDITION(aNode != nullptr, "null ptr");
54 if (! aNode)
55 return NS_ERROR_NULL_POINTER;
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;
65 else if (rv == NS_NOINTERFACE) {
66 *aResult = false;
67 return NS_OK;
69 else {
70 return rv;
74 ////////////////////////////////////////////////////////////////////////////////
75 // nsIRDFResource methods:
77 NS_IMETHODIMP
78 nsRDFResource::Init(const char* aURI)
80 NS_PRECONDITION(aURI != nullptr, "null ptr");
81 if (! aURI)
82 return NS_ERROR_NULL_POINTER;
84 mURI = aURI;
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);
95 NS_IMETHODIMP
96 nsRDFResource::GetValue(char* *aURI)
98 NS_ASSERTION(aURI, "Null out param.");
100 *aURI = ToNewCString(mURI);
102 if (!*aURI)
103 return NS_ERROR_OUT_OF_MEMORY;
105 return NS_OK;
108 NS_IMETHODIMP
109 nsRDFResource::GetValueUTF8(nsACString& aResult)
111 aResult = mURI;
112 return NS_OK;
115 NS_IMETHODIMP
116 nsRDFResource::GetValueConst(const char** aURI)
118 *aURI = mURI.get();
119 return NS_OK;
122 NS_IMETHODIMP
123 nsRDFResource::EqualsString(const char* aURI, bool* aResult)
125 NS_PRECONDITION(aURI != nullptr, "null ptr");
126 if (! aURI)
127 return NS_ERROR_NULL_POINTER;
129 NS_PRECONDITION(aResult, "null ptr");
131 *aResult = mURI.Equals(aURI);
132 return NS_OK;
135 NS_IMETHODIMP
136 nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
138 NS_PRECONDITION(aKey != nullptr, "null ptr");
139 if (! aKey)
140 return NS_ERROR_NULL_POINTER;
142 nsresult rv;
143 *aResult = nullptr;
145 DelegateEntry* entry = mDelegates;
146 while (entry) {
147 if (entry->mKey.Equals(aKey)) {
148 rv = entry->mDelegate->QueryInterface(aIID, aResult);
149 return rv;
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;
172 if (! entry) {
173 NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
174 return NS_ERROR_OUT_OF_MEMORY;
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!");
182 delete entry;
183 NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
184 return NS_ERROR_FAILURE;
187 entry->mNext = mDelegates;
189 mDelegates = entry;
191 return NS_OK;
194 NS_IMETHODIMP
195 nsRDFResource::ReleaseDelegate(const char* aKey)
197 NS_PRECONDITION(aKey != nullptr, "null ptr");
198 if (! aKey)
199 return NS_ERROR_NULL_POINTER;
201 DelegateEntry* entry = mDelegates;
202 DelegateEntry** link = &mDelegates;
204 while (entry) {
205 if (entry->mKey.Equals(aKey)) {
206 *link = entry->mNext;
207 delete entry;
208 return NS_OK;
211 link = &(entry->mNext);
212 entry = entry->mNext;
215 NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
216 return NS_OK;
221 ////////////////////////////////////////////////////////////////////////////////