2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-02-17 23:42:09 +00:00
|
|
|
|
|
|
|
#include "nsRDFResource.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-11-24 20:51:39 +00:00
|
|
|
#include "nsIRDFDelegateFactory.h"
|
1999-02-17 23:42:09 +00:00
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsRDFCID.h"
|
1999-03-09 09:44:27 +00:00
|
|
|
#include "prlog.h"
|
2007-06-19 05:02:49 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
1999-02-17 23:42:09 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIRDFService* nsRDFResource::gRDFService = nullptr;
|
1999-09-21 20:02:50 +00:00
|
|
|
nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0;
|
1999-03-02 01:44:01 +00:00
|
|
|
|
1999-02-17 23:42:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-02-26 10:17:14 +00:00
|
|
|
nsRDFResource::nsRDFResource(void)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mDelegates(nullptr)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRDFResource::~nsRDFResource(void)
|
|
|
|
{
|
1999-11-24 20:51:39 +00:00
|
|
|
// Release all of the delegate objects
|
|
|
|
while (mDelegates) {
|
|
|
|
DelegateEntry* doomed = mDelegates;
|
|
|
|
mDelegates = mDelegates->mNext;
|
|
|
|
delete doomed;
|
|
|
|
}
|
|
|
|
|
2004-03-09 14:42:24 +00:00
|
|
|
if (!gRDFService)
|
2003-09-11 01:42:16 +00:00
|
|
|
return;
|
1999-02-17 23:42:09 +00:00
|
|
|
|
2003-09-11 01:42:16 +00:00
|
|
|
gRDFService->UnregisterResource(this);
|
1999-09-21 20:02:50 +00:00
|
|
|
|
2004-11-07 23:59:35 +00:00
|
|
|
if (--gRDFServiceRefCnt == 0)
|
|
|
|
NS_RELEASE(gRDFService);
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsRDFResource, nsIRDFResource, nsIRDFNode)
|
1999-02-17 23:42:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRDFNode methods:
|
|
|
|
|
1999-02-26 10:17:14 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aNode != nullptr, "null ptr");
|
1999-06-25 20:51:13 +00:00
|
|
|
if (! aNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-02-17 23:42:09 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsIRDFResource* resource;
|
2000-01-11 20:49:15 +00:00
|
|
|
rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource);
|
1999-06-25 20:51:13 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2007-07-08 07:08:04 +00:00
|
|
|
*aResult = (static_cast<nsIRDFResource*>(this) == resource);
|
1999-02-17 23:42:09 +00:00
|
|
|
NS_RELEASE(resource);
|
1999-06-25 20:51:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if (rv == NS_NOINTERFACE) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*aResult = false;
|
1999-06-25 20:51:13 +00:00
|
|
|
return NS_OK;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
else {
|
1999-06-25 20:51:13 +00:00
|
|
|
return rv;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRDFResource methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-03-29 19:52:54 +00:00
|
|
|
nsRDFResource::Init(const char* aURI)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aURI != nullptr, "null ptr");
|
1999-03-29 19:52:54 +00:00
|
|
|
if (! aURI)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2004-03-09 14:42:24 +00:00
|
|
|
mURI = aURI;
|
1999-03-29 19:52:54 +00:00
|
|
|
|
1999-09-21 20:02:50 +00:00
|
|
|
if (gRDFServiceRefCnt++ == 0) {
|
2003-09-11 01:42:16 +00:00
|
|
|
nsresult rv = CallGetService(kRDFServiceCID, &gRDFService);
|
1999-09-21 20:02:50 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-03-31 19:22:10 +00:00
|
|
|
}
|
|
|
|
|
1999-03-29 19:52:54 +00:00
|
|
|
// don't replace an existing resource with the same URI automatically
|
2011-10-17 14:59:28 +00:00
|
|
|
return gRDFService->RegisterResource(this, true);
|
1999-03-29 19:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-06-25 20:51:13 +00:00
|
|
|
nsRDFResource::GetValue(char* *aURI)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
2004-03-09 14:42:24 +00:00
|
|
|
NS_ASSERTION(aURI, "Null out param.");
|
1999-03-29 19:52:54 +00:00
|
|
|
|
2004-03-09 14:42:24 +00:00
|
|
|
*aURI = ToNewCString(mURI);
|
|
|
|
|
|
|
|
if (!*aURI)
|
1999-03-29 19:52:54 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2004-03-09 14:42:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::GetValueUTF8(nsACString& aResult)
|
|
|
|
{
|
|
|
|
aResult = mURI;
|
|
|
|
return NS_OK;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-06-25 20:51:13 +00:00
|
|
|
nsRDFResource::GetValueConst(const char** aURI)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
2004-03-09 14:42:24 +00:00
|
|
|
*aURI = mURI.get();
|
1999-03-26 04:44:24 +00:00
|
|
|
return NS_OK;
|
1999-02-17 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsRDFResource::EqualsString(const char* aURI, bool* aResult)
|
1999-02-17 23:42:09 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aURI != nullptr, "null ptr");
|
1999-06-25 20:51:13 +00:00
|
|
|
if (! aURI)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-03-26 04:44:24 +00:00
|
|
|
|
2004-03-09 14:42:24 +00:00
|
|
|
NS_PRECONDITION(aResult, "null ptr");
|
1999-03-26 04:44:24 +00:00
|
|
|
|
2004-03-09 14:42:24 +00:00
|
|
|
*aResult = mURI.Equals(aURI);
|
1999-02-17 23:42:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-24 20:51:39 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aKey != nullptr, "null ptr");
|
1999-11-24 20:51:39 +00:00
|
|
|
if (! aKey)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
1999-11-24 20:51:39 +00:00
|
|
|
|
|
|
|
DelegateEntry* entry = mDelegates;
|
|
|
|
while (entry) {
|
2000-09-03 22:45:50 +00:00
|
|
|
if (entry->mKey.Equals(aKey)) {
|
1999-11-24 20:51:39 +00:00
|
|
|
rv = entry->mDelegate->QueryInterface(aIID, aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = entry->mNext;
|
|
|
|
}
|
|
|
|
|
2000-09-13 23:57:52 +00:00
|
|
|
// Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX);
|
2000-09-13 23:57:52 +00:00
|
|
|
contractID.Append(aKey);
|
2014-05-22 03:48:51 +00:00
|
|
|
contractID.AppendLiteral("&scheme=");
|
1999-11-24 20:51:39 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t i = mURI.FindChar(':');
|
2004-03-09 14:42:24 +00:00
|
|
|
contractID += StringHead(mURI, i);
|
1999-11-24 20:51:39 +00:00
|
|
|
|
2001-10-25 06:42:23 +00:00
|
|
|
nsCOMPtr<nsIRDFDelegateFactory> delegateFactory =
|
2001-10-26 21:37:07 +00:00
|
|
|
do_CreateInstance(contractID.get(), &rv);
|
1999-11-24 20:51:39 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = delegateFactory->CreateDelegate(this, aKey, aIID, aResult);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Okay, we've successfully created a delegate. Let's remember it.
|
|
|
|
entry = new DelegateEntry;
|
|
|
|
if (! entry) {
|
2007-07-08 07:08:04 +00:00
|
|
|
NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
|
1999-11-24 20:51:39 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->mKey = aKey;
|
2007-07-08 07:08:04 +00:00
|
|
|
entry->mDelegate = do_QueryInterface(*reinterpret_cast<nsISupports**>(aResult), &rv);
|
2001-01-23 23:10:53 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
|
|
|
|
|
|
|
|
delete entry;
|
2007-07-08 07:08:04 +00:00
|
|
|
NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
|
2001-01-23 23:10:53 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-11-24 20:51:39 +00:00
|
|
|
entry->mNext = mDelegates;
|
|
|
|
|
|
|
|
mDelegates = entry;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::ReleaseDelegate(const char* aKey)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aKey != nullptr, "null ptr");
|
1999-11-24 20:51:39 +00:00
|
|
|
if (! aKey)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
DelegateEntry* entry = mDelegates;
|
|
|
|
DelegateEntry** link = &mDelegates;
|
|
|
|
|
|
|
|
while (entry) {
|
2000-09-03 22:45:50 +00:00
|
|
|
if (entry->mKey.Equals(aKey)) {
|
1999-11-24 20:51:39 +00:00
|
|
|
*link = entry->mNext;
|
|
|
|
delete entry;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
link = &(entry->mNext);
|
|
|
|
entry = entry->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-02-17 23:42:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|