rdf/util/src/nsRDFResource.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial