rdf/util/src/nsRDFResource.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rdf/util/src/nsRDFResource.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsRDFResource.h"
    1.10 +#include "nsIServiceManager.h"
    1.11 +#include "nsIRDFDelegateFactory.h"
    1.12 +#include "nsIRDFService.h"
    1.13 +#include "nsRDFCID.h"
    1.14 +#include "prlog.h"
    1.15 +#include "nsComponentManagerUtils.h"
    1.16 +#include "nsServiceManagerUtils.h"
    1.17 +
    1.18 +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
    1.19 +
    1.20 +nsIRDFService* nsRDFResource::gRDFService = nullptr;
    1.21 +nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0;
    1.22 +
    1.23 +////////////////////////////////////////////////////////////////////////////////
    1.24 +
    1.25 +nsRDFResource::nsRDFResource(void)
    1.26 +    : mDelegates(nullptr)
    1.27 +{
    1.28 +}
    1.29 +
    1.30 +nsRDFResource::~nsRDFResource(void)
    1.31 +{
    1.32 +    // Release all of the delegate objects
    1.33 +    while (mDelegates) {
    1.34 +        DelegateEntry* doomed = mDelegates;
    1.35 +        mDelegates = mDelegates->mNext;
    1.36 +        delete doomed;
    1.37 +    }
    1.38 +
    1.39 +    if (!gRDFService)
    1.40 +        return;
    1.41 +
    1.42 +    gRDFService->UnregisterResource(this);
    1.43 +
    1.44 +    if (--gRDFServiceRefCnt == 0)
    1.45 +        NS_RELEASE(gRDFService);
    1.46 +}
    1.47 +
    1.48 +NS_IMPL_ISUPPORTS(nsRDFResource, nsIRDFResource, nsIRDFNode)
    1.49 +
    1.50 +////////////////////////////////////////////////////////////////////////////////
    1.51 +// nsIRDFNode methods:
    1.52 +
    1.53 +NS_IMETHODIMP
    1.54 +nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult)
    1.55 +{
    1.56 +    NS_PRECONDITION(aNode != nullptr, "null ptr");
    1.57 +    if (! aNode)
    1.58 +        return NS_ERROR_NULL_POINTER;
    1.59 +
    1.60 +    nsresult rv;
    1.61 +    nsIRDFResource* resource;
    1.62 +    rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource);
    1.63 +    if (NS_SUCCEEDED(rv)) {
    1.64 +        *aResult = (static_cast<nsIRDFResource*>(this) == resource);
    1.65 +        NS_RELEASE(resource);
    1.66 +        return NS_OK;
    1.67 +    }
    1.68 +    else if (rv == NS_NOINTERFACE) {
    1.69 +        *aResult = false;
    1.70 +        return NS_OK;
    1.71 +    }
    1.72 +    else {
    1.73 +        return rv;
    1.74 +    }
    1.75 +}
    1.76 +
    1.77 +////////////////////////////////////////////////////////////////////////////////
    1.78 +// nsIRDFResource methods:
    1.79 +
    1.80 +NS_IMETHODIMP
    1.81 +nsRDFResource::Init(const char* aURI)
    1.82 +{
    1.83 +    NS_PRECONDITION(aURI != nullptr, "null ptr");
    1.84 +    if (! aURI)
    1.85 +        return NS_ERROR_NULL_POINTER;
    1.86 +
    1.87 +    mURI = aURI;
    1.88 +
    1.89 +    if (gRDFServiceRefCnt++ == 0) {
    1.90 +        nsresult rv = CallGetService(kRDFServiceCID, &gRDFService);
    1.91 +        if (NS_FAILED(rv)) return rv;
    1.92 +    }
    1.93 +
    1.94 +    // don't replace an existing resource with the same URI automatically
    1.95 +    return gRDFService->RegisterResource(this, true);
    1.96 +}
    1.97 +
    1.98 +NS_IMETHODIMP
    1.99 +nsRDFResource::GetValue(char* *aURI)
   1.100 +{
   1.101 +    NS_ASSERTION(aURI, "Null out param.");
   1.102 +    
   1.103 +    *aURI = ToNewCString(mURI);
   1.104 +
   1.105 +    if (!*aURI)
   1.106 +        return NS_ERROR_OUT_OF_MEMORY;
   1.107 +
   1.108 +    return NS_OK;
   1.109 +}
   1.110 +
   1.111 +NS_IMETHODIMP
   1.112 +nsRDFResource::GetValueUTF8(nsACString& aResult)
   1.113 +{
   1.114 +    aResult = mURI;
   1.115 +    return NS_OK;
   1.116 +}
   1.117 +
   1.118 +NS_IMETHODIMP
   1.119 +nsRDFResource::GetValueConst(const char** aURI)
   1.120 +{
   1.121 +    *aURI = mURI.get();
   1.122 +    return NS_OK;
   1.123 +}
   1.124 +
   1.125 +NS_IMETHODIMP
   1.126 +nsRDFResource::EqualsString(const char* aURI, bool* aResult)
   1.127 +{
   1.128 +    NS_PRECONDITION(aURI != nullptr, "null ptr");
   1.129 +    if (! aURI)
   1.130 +        return NS_ERROR_NULL_POINTER;
   1.131 +
   1.132 +    NS_PRECONDITION(aResult, "null ptr");
   1.133 +
   1.134 +    *aResult = mURI.Equals(aURI);
   1.135 +    return NS_OK;
   1.136 +}
   1.137 +
   1.138 +NS_IMETHODIMP
   1.139 +nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
   1.140 +{
   1.141 +    NS_PRECONDITION(aKey != nullptr, "null ptr");
   1.142 +    if (! aKey)
   1.143 +        return NS_ERROR_NULL_POINTER;
   1.144 +
   1.145 +    nsresult rv;
   1.146 +    *aResult = nullptr;
   1.147 +
   1.148 +    DelegateEntry* entry = mDelegates;
   1.149 +    while (entry) {
   1.150 +        if (entry->mKey.Equals(aKey)) {
   1.151 +            rv = entry->mDelegate->QueryInterface(aIID, aResult);
   1.152 +            return rv;
   1.153 +        }
   1.154 +
   1.155 +        entry = entry->mNext;
   1.156 +    }
   1.157 +
   1.158 +    // Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1
   1.159 +    nsAutoCString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX);
   1.160 +    contractID.Append(aKey);
   1.161 +    contractID.Append("&scheme=");
   1.162 +
   1.163 +    int32_t i = mURI.FindChar(':');
   1.164 +    contractID += StringHead(mURI, i);
   1.165 +
   1.166 +    nsCOMPtr<nsIRDFDelegateFactory> delegateFactory =
   1.167 +             do_CreateInstance(contractID.get(), &rv);
   1.168 +    if (NS_FAILED(rv)) return rv;
   1.169 +
   1.170 +    rv = delegateFactory->CreateDelegate(this, aKey, aIID, aResult);
   1.171 +    if (NS_FAILED(rv)) return rv;
   1.172 +
   1.173 +    // Okay, we've successfully created a delegate. Let's remember it.
   1.174 +    entry = new DelegateEntry;
   1.175 +    if (! entry) {
   1.176 +        NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
   1.177 +        return NS_ERROR_OUT_OF_MEMORY;
   1.178 +    }
   1.179 +    
   1.180 +    entry->mKey      = aKey;
   1.181 +    entry->mDelegate = do_QueryInterface(*reinterpret_cast<nsISupports**>(aResult), &rv);
   1.182 +    if (NS_FAILED(rv)) {
   1.183 +        NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
   1.184 +
   1.185 +        delete entry;
   1.186 +        NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
   1.187 +        return NS_ERROR_FAILURE;
   1.188 +    }
   1.189 +
   1.190 +    entry->mNext     = mDelegates;
   1.191 +
   1.192 +    mDelegates = entry;
   1.193 +
   1.194 +    return NS_OK;
   1.195 +}
   1.196 +
   1.197 +NS_IMETHODIMP
   1.198 +nsRDFResource::ReleaseDelegate(const char* aKey)
   1.199 +{
   1.200 +    NS_PRECONDITION(aKey != nullptr, "null ptr");
   1.201 +    if (! aKey)
   1.202 +        return NS_ERROR_NULL_POINTER;
   1.203 +
   1.204 +    DelegateEntry* entry = mDelegates;
   1.205 +    DelegateEntry** link = &mDelegates;
   1.206 +
   1.207 +    while (entry) {
   1.208 +        if (entry->mKey.Equals(aKey)) {
   1.209 +            *link = entry->mNext;
   1.210 +            delete entry;
   1.211 +            return NS_OK;
   1.212 +        }
   1.213 +
   1.214 +        link = &(entry->mNext);
   1.215 +        entry = entry->mNext;
   1.216 +    }
   1.217 +
   1.218 +    NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
   1.219 +    return NS_OK;
   1.220 +}
   1.221 +
   1.222 +
   1.223 +
   1.224 +////////////////////////////////////////////////////////////////////////////////

mercurial