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