2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsRDFResource.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIRDFDelegateFactory.h"
|
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
#include "prlog.h"
|
2007-06-18 22:02:49 -07:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIRDFService* nsRDFResource::gRDFService = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsRDFResource::nsRDFResource(void)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mDelegates(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRDFResource::~nsRDFResource(void)
|
|
|
|
{
|
|
|
|
// Release all of the delegate objects
|
|
|
|
while (mDelegates) {
|
|
|
|
DelegateEntry* doomed = mDelegates;
|
|
|
|
mDelegates = mDelegates->mNext;
|
|
|
|
delete doomed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gRDFService)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gRDFService->UnregisterResource(this);
|
|
|
|
|
|
|
|
if (--gRDFServiceRefCnt == 0)
|
|
|
|
NS_RELEASE(gRDFService);
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsRDFResource, nsIRDFResource, nsIRDFNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRDFNode methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aNode != nullptr, "null ptr");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! aNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIRDFResource* resource;
|
|
|
|
rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2007-07-08 00:08:04 -07:00
|
|
|
*aResult = (static_cast<nsIRDFResource*>(this) == resource);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_RELEASE(resource);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if (rv == NS_NOINTERFACE) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIRDFResource methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::Init(const char* aURI)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aURI != nullptr, "null ptr");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! aURI)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
mURI = aURI;
|
|
|
|
|
|
|
|
if (gRDFServiceRefCnt++ == 0) {
|
|
|
|
nsresult rv = CallGetService(kRDFServiceCID, &gRDFService);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't replace an existing resource with the same URI automatically
|
2011-10-17 07:59:28 -07:00
|
|
|
return gRDFService->RegisterResource(this, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::GetValue(char* *aURI)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aURI, "Null out param.");
|
|
|
|
|
|
|
|
*aURI = ToNewCString(mURI);
|
|
|
|
|
|
|
|
if (!*aURI)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::GetValueUTF8(nsACString& aResult)
|
|
|
|
{
|
|
|
|
aResult = mURI;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::GetValueConst(const char** aURI)
|
|
|
|
{
|
|
|
|
*aURI = mURI.get();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsRDFResource::EqualsString(const char* aURI, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aURI != nullptr, "null ptr");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! aURI)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
NS_PRECONDITION(aResult, "null ptr");
|
|
|
|
|
|
|
|
*aResult = mURI.Equals(aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aKey != nullptr, "null ptr");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! aKey)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DelegateEntry* entry = mDelegates;
|
|
|
|
while (entry) {
|
|
|
|
if (entry->mKey.Equals(aKey)) {
|
|
|
|
rv = entry->mDelegate->QueryInterface(aIID, aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = entry->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX);
|
2007-03-22 10:30:00 -07:00
|
|
|
contractID.Append(aKey);
|
2014-05-21 20:48:51 -07:00
|
|
|
contractID.AppendLiteral("&scheme=");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i = mURI.FindChar(':');
|
2007-03-22 10:30:00 -07:00
|
|
|
contractID += StringHead(mURI, i);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFDelegateFactory> delegateFactory =
|
|
|
|
do_CreateInstance(contractID.get(), &rv);
|
|
|
|
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 00:08:04 -07:00
|
|
|
NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->mKey = aKey;
|
2007-07-08 00:08:04 -07:00
|
|
|
entry->mDelegate = do_QueryInterface(*reinterpret_cast<nsISupports**>(aResult), &rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
|
|
|
|
|
|
|
|
delete entry;
|
2007-07-08 00:08:04 -07:00
|
|
|
NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->mNext = mDelegates;
|
|
|
|
|
|
|
|
mDelegates = entry;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFResource::ReleaseDelegate(const char* aKey)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aKey != nullptr, "null ptr");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! aKey)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
DelegateEntry* entry = mDelegates;
|
|
|
|
DelegateEntry** link = &mDelegates;
|
|
|
|
|
|
|
|
while (entry) {
|
|
|
|
if (entry->mKey.Equals(aKey)) {
|
|
|
|
*link = entry->mNext;
|
|
|
|
delete entry;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
link = &(entry->mNext);
|
|
|
|
entry = entry->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|