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.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsRDFResource.h"
michael@0 7 #include "nsIServiceManager.h"
michael@0 8 #include "nsIRDFDelegateFactory.h"
michael@0 9 #include "nsIRDFService.h"
michael@0 10 #include "nsRDFCID.h"
michael@0 11 #include "prlog.h"
michael@0 12 #include "nsComponentManagerUtils.h"
michael@0 13 #include "nsServiceManagerUtils.h"
michael@0 14
michael@0 15 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
michael@0 16
michael@0 17 nsIRDFService* nsRDFResource::gRDFService = nullptr;
michael@0 18 nsrefcnt nsRDFResource::gRDFServiceRefCnt = 0;
michael@0 19
michael@0 20 ////////////////////////////////////////////////////////////////////////////////
michael@0 21
michael@0 22 nsRDFResource::nsRDFResource(void)
michael@0 23 : mDelegates(nullptr)
michael@0 24 {
michael@0 25 }
michael@0 26
michael@0 27 nsRDFResource::~nsRDFResource(void)
michael@0 28 {
michael@0 29 // Release all of the delegate objects
michael@0 30 while (mDelegates) {
michael@0 31 DelegateEntry* doomed = mDelegates;
michael@0 32 mDelegates = mDelegates->mNext;
michael@0 33 delete doomed;
michael@0 34 }
michael@0 35
michael@0 36 if (!gRDFService)
michael@0 37 return;
michael@0 38
michael@0 39 gRDFService->UnregisterResource(this);
michael@0 40
michael@0 41 if (--gRDFServiceRefCnt == 0)
michael@0 42 NS_RELEASE(gRDFService);
michael@0 43 }
michael@0 44
michael@0 45 NS_IMPL_ISUPPORTS(nsRDFResource, nsIRDFResource, nsIRDFNode)
michael@0 46
michael@0 47 ////////////////////////////////////////////////////////////////////////////////
michael@0 48 // nsIRDFNode methods:
michael@0 49
michael@0 50 NS_IMETHODIMP
michael@0 51 nsRDFResource::EqualsNode(nsIRDFNode* aNode, bool* aResult)
michael@0 52 {
michael@0 53 NS_PRECONDITION(aNode != nullptr, "null ptr");
michael@0 54 if (! aNode)
michael@0 55 return NS_ERROR_NULL_POINTER;
michael@0 56
michael@0 57 nsresult rv;
michael@0 58 nsIRDFResource* resource;
michael@0 59 rv = aNode->QueryInterface(NS_GET_IID(nsIRDFResource), (void**)&resource);
michael@0 60 if (NS_SUCCEEDED(rv)) {
michael@0 61 *aResult = (static_cast<nsIRDFResource*>(this) == resource);
michael@0 62 NS_RELEASE(resource);
michael@0 63 return NS_OK;
michael@0 64 }
michael@0 65 else if (rv == NS_NOINTERFACE) {
michael@0 66 *aResult = false;
michael@0 67 return NS_OK;
michael@0 68 }
michael@0 69 else {
michael@0 70 return rv;
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74 ////////////////////////////////////////////////////////////////////////////////
michael@0 75 // nsIRDFResource methods:
michael@0 76
michael@0 77 NS_IMETHODIMP
michael@0 78 nsRDFResource::Init(const char* aURI)
michael@0 79 {
michael@0 80 NS_PRECONDITION(aURI != nullptr, "null ptr");
michael@0 81 if (! aURI)
michael@0 82 return NS_ERROR_NULL_POINTER;
michael@0 83
michael@0 84 mURI = aURI;
michael@0 85
michael@0 86 if (gRDFServiceRefCnt++ == 0) {
michael@0 87 nsresult rv = CallGetService(kRDFServiceCID, &gRDFService);
michael@0 88 if (NS_FAILED(rv)) return rv;
michael@0 89 }
michael@0 90
michael@0 91 // don't replace an existing resource with the same URI automatically
michael@0 92 return gRDFService->RegisterResource(this, true);
michael@0 93 }
michael@0 94
michael@0 95 NS_IMETHODIMP
michael@0 96 nsRDFResource::GetValue(char* *aURI)
michael@0 97 {
michael@0 98 NS_ASSERTION(aURI, "Null out param.");
michael@0 99
michael@0 100 *aURI = ToNewCString(mURI);
michael@0 101
michael@0 102 if (!*aURI)
michael@0 103 return NS_ERROR_OUT_OF_MEMORY;
michael@0 104
michael@0 105 return NS_OK;
michael@0 106 }
michael@0 107
michael@0 108 NS_IMETHODIMP
michael@0 109 nsRDFResource::GetValueUTF8(nsACString& aResult)
michael@0 110 {
michael@0 111 aResult = mURI;
michael@0 112 return NS_OK;
michael@0 113 }
michael@0 114
michael@0 115 NS_IMETHODIMP
michael@0 116 nsRDFResource::GetValueConst(const char** aURI)
michael@0 117 {
michael@0 118 *aURI = mURI.get();
michael@0 119 return NS_OK;
michael@0 120 }
michael@0 121
michael@0 122 NS_IMETHODIMP
michael@0 123 nsRDFResource::EqualsString(const char* aURI, bool* aResult)
michael@0 124 {
michael@0 125 NS_PRECONDITION(aURI != nullptr, "null ptr");
michael@0 126 if (! aURI)
michael@0 127 return NS_ERROR_NULL_POINTER;
michael@0 128
michael@0 129 NS_PRECONDITION(aResult, "null ptr");
michael@0 130
michael@0 131 *aResult = mURI.Equals(aURI);
michael@0 132 return NS_OK;
michael@0 133 }
michael@0 134
michael@0 135 NS_IMETHODIMP
michael@0 136 nsRDFResource::GetDelegate(const char* aKey, REFNSIID aIID, void** aResult)
michael@0 137 {
michael@0 138 NS_PRECONDITION(aKey != nullptr, "null ptr");
michael@0 139 if (! aKey)
michael@0 140 return NS_ERROR_NULL_POINTER;
michael@0 141
michael@0 142 nsresult rv;
michael@0 143 *aResult = nullptr;
michael@0 144
michael@0 145 DelegateEntry* entry = mDelegates;
michael@0 146 while (entry) {
michael@0 147 if (entry->mKey.Equals(aKey)) {
michael@0 148 rv = entry->mDelegate->QueryInterface(aIID, aResult);
michael@0 149 return rv;
michael@0 150 }
michael@0 151
michael@0 152 entry = entry->mNext;
michael@0 153 }
michael@0 154
michael@0 155 // Construct a ContractID of the form "@mozilla.org/rdf/delegate/[key]/[scheme];1
michael@0 156 nsAutoCString contractID(NS_RDF_DELEGATEFACTORY_CONTRACTID_PREFIX);
michael@0 157 contractID.Append(aKey);
michael@0 158 contractID.Append("&scheme=");
michael@0 159
michael@0 160 int32_t i = mURI.FindChar(':');
michael@0 161 contractID += StringHead(mURI, i);
michael@0 162
michael@0 163 nsCOMPtr<nsIRDFDelegateFactory> delegateFactory =
michael@0 164 do_CreateInstance(contractID.get(), &rv);
michael@0 165 if (NS_FAILED(rv)) return rv;
michael@0 166
michael@0 167 rv = delegateFactory->CreateDelegate(this, aKey, aIID, aResult);
michael@0 168 if (NS_FAILED(rv)) return rv;
michael@0 169
michael@0 170 // Okay, we've successfully created a delegate. Let's remember it.
michael@0 171 entry = new DelegateEntry;
michael@0 172 if (! entry) {
michael@0 173 NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
michael@0 174 return NS_ERROR_OUT_OF_MEMORY;
michael@0 175 }
michael@0 176
michael@0 177 entry->mKey = aKey;
michael@0 178 entry->mDelegate = do_QueryInterface(*reinterpret_cast<nsISupports**>(aResult), &rv);
michael@0 179 if (NS_FAILED(rv)) {
michael@0 180 NS_ERROR("nsRDFResource::GetDelegate(): can't QI to nsISupports!");
michael@0 181
michael@0 182 delete entry;
michael@0 183 NS_RELEASE(*reinterpret_cast<nsISupports**>(aResult));
michael@0 184 return NS_ERROR_FAILURE;
michael@0 185 }
michael@0 186
michael@0 187 entry->mNext = mDelegates;
michael@0 188
michael@0 189 mDelegates = entry;
michael@0 190
michael@0 191 return NS_OK;
michael@0 192 }
michael@0 193
michael@0 194 NS_IMETHODIMP
michael@0 195 nsRDFResource::ReleaseDelegate(const char* aKey)
michael@0 196 {
michael@0 197 NS_PRECONDITION(aKey != nullptr, "null ptr");
michael@0 198 if (! aKey)
michael@0 199 return NS_ERROR_NULL_POINTER;
michael@0 200
michael@0 201 DelegateEntry* entry = mDelegates;
michael@0 202 DelegateEntry** link = &mDelegates;
michael@0 203
michael@0 204 while (entry) {
michael@0 205 if (entry->mKey.Equals(aKey)) {
michael@0 206 *link = entry->mNext;
michael@0 207 delete entry;
michael@0 208 return NS_OK;
michael@0 209 }
michael@0 210
michael@0 211 link = &(entry->mNext);
michael@0 212 entry = entry->mNext;
michael@0 213 }
michael@0 214
michael@0 215 NS_WARNING("nsRDFResource::ReleaseDelegate() no delegate found");
michael@0 216 return NS_OK;
michael@0 217 }
michael@0 218
michael@0 219
michael@0 220
michael@0 221 ////////////////////////////////////////////////////////////////////////////////

mercurial