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