Bug 308590 patch 5: Clean up QIs to nsIURL that are really checks for "Does this support GetRef/SetRef", now that nsIURI supports GetRef/SetRef too. r=bz

This commit is contained in:
Daniel Holbert 2011-05-21 18:12:46 -07:00
parent 57db49e40e
commit ff123ba64d
13 changed files with 116 additions and 232 deletions

View File

@ -307,13 +307,12 @@ nsresult
Link::SetHash(const nsAString &aHash)
{
nsCOMPtr<nsIURI> uri(GetURIToMutate());
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
if (!url) {
if (!uri) {
// Ignore failures to be compatible with NS4.
return NS_OK;
}
(void)url->SetRef(NS_ConvertUTF16toUTF8(aHash));
(void)uri->SetRef(NS_ConvertUTF16toUTF8(aHash));
SetHrefAttribute(uri);
return NS_OK;
}
@ -444,15 +443,14 @@ Link::GetHash(nsAString &_hash)
_hash.Truncate();
nsCOMPtr<nsIURI> uri(GetURI());
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
if (!url) {
// Do not throw! Not having a valid URI or URL should result in an empty
if (!uri) {
// Do not throw! Not having a valid URI should result in an empty
// string.
return NS_OK;
}
nsCAutoString ref;
nsresult rv = url->GetRef(ref);
nsresult rv = uri->GetRef(ref);
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
NS_UnescapeURL(ref); // XXX may result in random non-ASCII bytes!
_hash.Assign(PRUnichar('#'));

View File

@ -694,14 +694,10 @@ nsExternalResourceMap::RequestResource(nsIURI* aURI,
// First, make sure we strip the ref from aURI.
nsCOMPtr<nsIURI> clone;
aURI->Clone(getter_AddRefs(clone));
if (!clone) {
nsresult rv = aURI->CloneIgnoringRef(getter_AddRefs(clone));
if (NS_FAILED(rv) || !clone) {
return nsnull;
}
nsCOMPtr<nsIURL> url(do_QueryInterface(clone));
if (url) {
url->SetRef(EmptyCString());
}
ExternalResource* resource;
mMap.Get(clone, &resource);
@ -712,14 +708,11 @@ nsExternalResourceMap::RequestResource(nsIURI* aURI,
nsRefPtr<PendingLoad> load;
mPendingLoads.Get(clone, getter_AddRefs(load));
if (load) {
NS_ADDREF(*aPendingLoad = load);
load.forget(aPendingLoad);
return nsnull;
}
load = new PendingLoad(aDisplayDocument);
if (!load) {
return nsnull;
}
if (!mPendingLoads.Put(clone, load)) {
return nsnull;
@ -730,7 +723,7 @@ nsExternalResourceMap::RequestResource(nsIURI* aURI,
// chances are it failed for good reasons (security check, etc).
AddExternalResource(clone, nsnull, nsnull, aDisplayDocument);
} else {
NS_ADDREF(*aPendingLoad = load);
load.forget(aPendingLoad);
}
return nsnull;

View File

@ -1580,18 +1580,6 @@ nsFrameLoader::CheckForRecursiveLoad(nsIURI* aURI)
}
// Bug 136580: Check for recursive frame loading
// pre-grab these for speed
nsCOMPtr<nsIURI> cloneURI;
rv = aURI->Clone(getter_AddRefs(cloneURI));
NS_ENSURE_SUCCESS(rv, rv);
// Bug 98158/193011: We need to ignore data after the #
nsCOMPtr<nsIURL> cloneURL(do_QueryInterface(cloneURI)); // QI can fail
if (cloneURL) {
rv = cloneURL->SetRef(EmptyCString());
NS_ENSURE_SUCCESS(rv,rv);
}
PRInt32 matchCount = 0;
treeItem->GetSameTypeParent(getter_AddRefs(parentAsItem));
while (parentAsItem) {
@ -1602,17 +1590,9 @@ nsFrameLoader::CheckForRecursiveLoad(nsIURI* aURI)
nsCOMPtr<nsIURI> parentURI;
parentAsNav->GetCurrentURI(getter_AddRefs(parentURI));
if (parentURI) {
nsCOMPtr<nsIURI> parentClone;
rv = parentURI->Clone(getter_AddRefs(parentClone));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURL> parentURL(do_QueryInterface(parentClone));
if (parentURL) {
rv = parentURL->SetRef(EmptyCString());
NS_ENSURE_SUCCESS(rv,rv);
}
// Bug 98158/193011: We need to ignore data after the #
PRBool equal;
rv = cloneURI->Equals(parentClone, &equal);
rv = aURI->EqualsExceptRef(parentURI, &equal);
NS_ENSURE_SUCCESS(rv, rv);
if (equal) {

View File

@ -40,7 +40,6 @@
#include "nsContentUtils.h"
#include "nsIURI.h"
#include "nsBindingManager.h"
#include "nsIURL.h"
#include "nsEscape.h"
#include "nsXBLPrototypeBinding.h"
#include "nsIDOMNode.h"
@ -48,50 +47,23 @@
#include "nsIDOMElement.h"
#include "nsCycleCollectionParticipant.h"
static PRBool EqualExceptRef(nsIURL* aURL1, nsIURL* aURL2)
{
nsCOMPtr<nsIURI> u1;
nsCOMPtr<nsIURI> u2;
nsresult rv = aURL1->Clone(getter_AddRefs(u1));
if (NS_SUCCEEDED(rv)) {
rv = aURL2->Clone(getter_AddRefs(u2));
}
if (NS_FAILED(rv))
return PR_FALSE;
nsCOMPtr<nsIURL> url1 = do_QueryInterface(u1);
nsCOMPtr<nsIURL> url2 = do_QueryInterface(u2);
if (!url1 || !url2) {
NS_WARNING("Cloning a URL produced a non-URL");
return PR_FALSE;
}
url1->SetRef(EmptyCString());
url2->SetRef(EmptyCString());
PRBool equal;
rv = url1->Equals(url2, &equal);
return NS_SUCCEEDED(rv) && equal;
}
void
nsReferencedElement::Reset(nsIContent* aFromContent, nsIURI* aURI,
PRBool aWatch, PRBool aReferenceImage)
{
NS_ABORT_IF_FALSE(aFromContent, "Reset() expects non-null content pointer");
NS_ABORT_IF_FALSE(aURI, "Reset() expects non-null URI for referenced elem");
Unlink();
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
if (!url)
return;
nsCAutoString refPart;
url->GetRef(refPart);
aURI->GetRef(refPart);
// Unescape %-escapes in the reference. The result will be in the
// origin charset of the URL, hopefully...
NS_UnescapeURL(refPart);
nsCAutoString charset;
url->GetOriginCharset(charset);
aURI->GetOriginCharset(charset);
nsAutoString ref;
nsresult rv = nsContentUtils::ConvertStringFromCharset(charset, refPart, ref);
if (NS_FAILED(rv)) {
@ -109,9 +81,10 @@ nsReferencedElement::Reset(nsIContent* aFromContent, nsIURI* aURI,
if (bindingParent) {
nsXBLBinding* binding = doc->BindingManager()->GetBinding(bindingParent);
if (binding) {
nsCOMPtr<nsIURL> bindingDocumentURL =
do_QueryInterface(binding->PrototypeBinding()->DocURI());
if (EqualExceptRef(url, bindingDocumentURL)) {
PRBool isEqualExceptRef;
rv = aURI->EqualsExceptRef(binding->PrototypeBinding()->DocURI(),
&isEqualExceptRef);
if (NS_SUCCEEDED(rv) && isEqualExceptRef) {
// XXX sXBL/XBL2 issue
// Our content is an anonymous XBL element from a binding inside the
// same document that the referenced URI points to. In order to avoid
@ -139,13 +112,12 @@ nsReferencedElement::Reset(nsIContent* aFromContent, nsIURI* aURI,
}
}
nsCOMPtr<nsIURL> documentURL = do_QueryInterface(doc->GetDocumentURI());
// We've already checked that |url| is an nsIURL. So if the document URI is
// not an nsIURL then |url| is certainly not going to be pointing to the same
// document as the document URI.
if (!documentURL || !EqualExceptRef(url, documentURL)) {
PRBool isEqualExceptRef;
rv = aURI->EqualsExceptRef(doc->GetDocumentURI(), &isEqualExceptRef);
if (NS_FAILED(rv) || !isEqualExceptRef) {
nsRefPtr<nsIDocument::ExternalResourceLoad> load;
doc = doc->RequestExternalResource(url, aFromContent, getter_AddRefs(load));
doc = doc->RequestExternalResource(aURI, aFromContent,
getter_AddRefs(load));
if (!doc) {
if (!load || !aWatch) {
// Nothing will ever happen here

View File

@ -83,16 +83,11 @@ PRBool nsXMLEventsListener::InitXMLEventsListener(nsIDocument * aDocument,
nsIURI *baseURI = aDocument->GetDocBaseURI();
rv = NS_NewURI( getter_AddRefs(handlerURI), handlerURIStr, nsnull, baseURI);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIURL> handlerURL(do_QueryInterface(handlerURI));
if (handlerURL) {
handlerURL->GetRef(handlerRef);
handlerURL->SetRef(EmptyCString());
//We support only XML Events Basic.
docURI->Equals(handlerURL, &equals);
if (equals) {
handler =
aDocument->GetElementById(NS_ConvertUTF8toUTF16(handlerRef));
}
handlerURI->GetRef(handlerRef);
// We support only XML Events Basic.
rv = docURI->EqualsExceptRef(handlerURI, &equals);
if (NS_SUCCEEDED(rv) && equals) {
handler = aDocument->GetElementById(NS_ConvertUTF8toUTF16(handlerRef));
}
}
}

View File

@ -305,16 +305,13 @@ nsXBLPrototypeBinding::Init(const nsACString& aID,
nsresult rv = aInfo->DocumentURI()->Clone(getter_AddRefs(mBindingURI));
NS_ENSURE_SUCCESS(rv, rv);
// The binding URI might not be a nsIURL (e.g. for data: URIs). In that case,
// we always use the first binding, so we don't need to keep track of the ID.
nsCOMPtr<nsIURL> bindingURL = do_QueryInterface(mBindingURI);
if (bindingURL) {
if (aFirstBinding) {
rv = mBindingURI->Clone(getter_AddRefs(mAlternateBindingURI));
NS_ENSURE_SUCCESS(rv, rv);
}
bindingURL->SetRef(aID);
// The binding URI might be an immutable URI (e.g. for about: URIs). In that case,
// we'll fail in SetRef below, but that doesn't matter much for now.
if (aFirstBinding) {
rv = mBindingURI->Clone(getter_AddRefs(mAlternateBindingURI));
NS_ENSURE_SUCCESS(rv, rv);
}
mBindingURI->SetRef(aID);
mXBLDocInfoWeak = aInfo;

View File

@ -862,9 +862,7 @@ nsXBLService::GetBinding(nsIContent* aBoundElement, nsIURI* aURI,
return NS_ERROR_FAILURE;
nsCAutoString ref;
nsCOMPtr<nsIURL> url(do_QueryInterface(aURI));
if (url)
url->GetRef(ref);
aURI->GetRef(ref);
nsCOMPtr<nsIDocument> boundDocument = aBoundElement->GetOwnerDoc();
@ -1129,12 +1127,8 @@ nsXBLService::LoadBindingDocumentInfo(nsIContent* aBoundElement,
nsRefPtr<nsXBLDocumentInfo> info;
nsCOMPtr<nsIURI> documentURI;
rv = aBindingURI->Clone(getter_AddRefs(documentURI));
rv = aBindingURI->CloneIgnoringRef(getter_AddRefs(documentURI));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURL> documentURL(do_QueryInterface(documentURI));
if (documentURL)
documentURL->SetRef(EmptyCString());
#ifdef MOZ_XUL
// We've got a file. Check our XBL document cache.

View File

@ -743,15 +743,10 @@ TX_CompileStylesheet(nsINode* aNode, txMozillaXSLTProcessor* aProcessor,
nsIURI* docUri = doc->GetDocumentURI();
NS_ENSURE_TRUE(docUri, NS_ERROR_FAILURE);
docUri->Clone(getter_AddRefs(uri));
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
// We need to remove the ref, a URL with a ref would mean that we have an
// We need to remove the ref, a URI with a ref would mean that we have an
// embedded stylesheet.
nsCOMPtr<nsIURL> url = do_QueryInterface(uri);
if (url) {
url->SetRef(EmptyCString());
}
docUri->CloneIgnoringRef(getter_AddRefs(uri));
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
uri->GetSpec(spec);
NS_ConvertUTF8toUTF16 stylesheetURI(spec);

View File

@ -328,34 +328,29 @@ nsXULContentUtils::MakeElementURI(nsIDocument* aDocument,
// Convert an element's ID to a URI that can be used to refer to
// the element in the XUL graph.
nsIURI *docURL = aDocument->GetDocumentURI();
NS_ENSURE_TRUE(docURL, NS_ERROR_UNEXPECTED);
nsIURI *docURI = aDocument->GetDocumentURI();
NS_ENSURE_TRUE(docURI, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIURI> docURIClone;
nsresult rv = docURL->Clone(getter_AddRefs(docURIClone));
nsRefPtr<nsIURI> docURIClone;
nsresult rv = docURI->Clone(getter_AddRefs(docURIClone));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURL> mutableURL(do_QueryInterface(docURIClone));
if (!mutableURL) {
nsCString uri;
rv = docURL->GetSpec(aURI);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString ref;
NS_EscapeURL(NS_ConvertUTF16toUTF8(aElementID), esc_FilePath | esc_AlwaysCopy, ref);
aURI.Append('#');
aURI.Append(ref);
return NS_OK;
rv = docURIClone->SetRef(NS_ConvertUTF16toUTF8(aElementID));
if (NS_SUCCEEDED(rv)) {
return docURIClone->GetSpec(aURI);
}
NS_ENSURE_TRUE(mutableURL, NS_ERROR_NOT_AVAILABLE);
rv = mutableURL->SetRef(NS_ConvertUTF16toUTF8(aElementID));
// docURIClone is apparently immutable. Fine - we can append ref manually.
rv = docURI->GetSpec(aURI);
NS_ENSURE_SUCCESS(rv, rv);
return mutableURL->GetSpec(aURI);
nsCAutoString ref;
NS_EscapeURL(NS_ConvertUTF16toUTF8(aElementID), esc_FilePath | esc_AlwaysCopy, ref);
aURI.Append('#');
aURI.Append(ref);
return NS_OK;
}
@ -390,27 +385,9 @@ nsXULContentUtils::MakeElementID(nsIDocument* aDocument,
aDocument->GetDocumentCharacterSet().get());
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURL> url = do_QueryInterface(uri);
if (url) {
nsCAutoString ref;
url->GetRef(ref);
CopyUTF8toUTF16(ref, aElementID);
} else {
const char* start = aURI.BeginReading();
const char* end = aURI.EndReading();
const char* chr = end;
while (--chr >= start) {
if (*chr == '#') {
nsDependentCSubstring ref = Substring(chr + 1, end);
nsCAutoString unescaped;
CopyUTF8toUTF16(NS_UnescapeURL(ref, esc_FilePath, unescaped), aElementID);
return NS_OK;
}
}
aElementID.Truncate();
}
nsCAutoString ref;
uri->GetRef(ref);
CopyUTF8toUTF16(ref, aElementID);
return NS_OK;
}

View File

@ -374,46 +374,45 @@ nsLocation::GetHash(nsAString& aHash)
nsCOMPtr<nsIURI> uri;
nsresult rv = GetURI(getter_AddRefs(uri));
if (NS_FAILED(rv) || !uri) {
return rv;
}
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
nsCAutoString ref;
nsAutoString unicodeRef;
if (url) {
nsCAutoString ref;
nsAutoString unicodeRef;
rv = uri->GetRef(ref);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsITextToSubURI> textToSubURI(
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
rv = url->GetRef(ref);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsITextToSubURI> textToSubURI(
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv)) {
nsCAutoString charset;
url->GetOriginCharset(charset);
nsCAutoString charset;
uri->GetOriginCharset(charset);
rv = textToSubURI->UnEscapeURIForUI(charset, ref, unicodeRef);
}
rv = textToSubURI->UnEscapeURIForUI(charset, ref, unicodeRef);
}
if (NS_FAILED(rv)) {
// Oh, well. No intl here!
NS_UnescapeURL(ref);
CopyASCIItoUTF16(ref, unicodeRef);
rv = NS_OK;
}
if (NS_FAILED(rv)) {
// Oh, well. No intl here!
NS_UnescapeURL(ref);
CopyASCIItoUTF16(ref, unicodeRef);
rv = NS_OK;
}
}
if (NS_SUCCEEDED(rv) && !unicodeRef.IsEmpty()) {
aHash.Assign(PRUnichar('#'));
aHash.Append(unicodeRef);
}
if (NS_SUCCEEDED(rv) && !unicodeRef.IsEmpty()) {
aHash.Assign(PRUnichar('#'));
aHash.Append(unicodeRef);
}
if (aHash == mCachedHash) {
// Work around ShareThis stupidly polling location.hash every
// 5ms all the time by handing out the same exact string buffer
// we handed out last time.
aHash = mCachedHash;
} else {
mCachedHash = aHash;
}
if (aHash == mCachedHash) {
// Work around ShareThis stupidly polling location.hash every
// 5ms all the time by handing out the same exact string buffer
// we handed out last time.
aHash = mCachedHash;
} else {
mCachedHash = aHash;
}
return rv;
@ -424,17 +423,17 @@ nsLocation::SetHash(const nsAString& aHash)
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_FAILED(rv) || !uri) {
return rv;
}
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
if (url) {
NS_ConvertUTF16toUTF8 hash(aHash);
if (hash.IsEmpty() || hash.First() != PRUnichar('#')) {
hash.Insert(PRUnichar('#'), 0);
}
rv = url->SetRef(hash);
if (NS_SUCCEEDED(rv)) {
SetURI(url);
}
NS_ConvertUTF16toUTF8 hash(aHash);
if (hash.IsEmpty() || hash.First() != PRUnichar('#')) {
hash.Insert(PRUnichar('#'), 0);
}
rv = uri->SetRef(hash);
if (NS_SUCCEEDED(rv)) {
SetURI(uri);
}
return rv;

View File

@ -865,16 +865,13 @@ HttpBaseChannel::SetReferrer(nsIURI *referrer)
// (1) modify it
// (2) keep a reference to it after returning from this function
//
rv = referrer->Clone(getter_AddRefs(clone));
// Use CloneIgnoringRef to strip away any fragment per RFC 2616 section 14.36
rv = referrer->CloneIgnoringRef(getter_AddRefs(clone));
if (NS_FAILED(rv)) return rv;
// strip away any userpass; we don't want to be giving out passwords ;-)
clone->SetUserPass(EmptyCString());
// strip away any fragment per RFC 2616 section 14.36
nsCOMPtr<nsIURL> url = do_QueryInterface(clone);
if (url)
url->SetRef(EmptyCString());
rv = clone->SetUserPass(EmptyCString());
if (NS_FAILED(rv)) return rv;
nsCAutoString spec;
rv = clone->GetAsciiSpec(spec);

View File

@ -3384,17 +3384,14 @@ nsHttpChannel::ContinueProcessRedirectionAfterFallback(nsresult rv)
// move the reference of the old location to the new one if the new
// one has none.
nsCOMPtr<nsIURL> newURL = do_QueryInterface(mRedirectURI);
if (newURL) {
nsCAutoString ref;
rv = newURL->GetRef(ref);
if (NS_SUCCEEDED(rv) && ref.IsEmpty()) {
nsCOMPtr<nsIURL> baseURL(do_QueryInterface(mURI));
if (baseURL) {
baseURL->GetRef(ref);
if (!ref.IsEmpty())
newURL->SetRef(ref);
}
nsCAutoString ref;
rv = mRedirectURI->GetRef(ref);
if (NS_SUCCEEDED(rv) && ref.IsEmpty()) {
mURI->GetRef(ref);
if (!ref.IsEmpty()) {
// NOTE: SetRef will fail if mRedirectURI is immutable
// (e.g. an about: URI)... Oh well.
mRedirectURI->SetRef(ref);
}
}

View File

@ -104,13 +104,9 @@ private:
static nsresult
DropReferenceFromURL(nsIURI * aURI)
{
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
if (url) {
nsresult rv = url->SetRef(EmptyCString());
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
// XXXdholbert If this SetRef fails, callers of this method probably
// want to call aURI->CloneIgnoringRef() and use the result of that.
return aURI->SetRef(EmptyCString());
}
//-----------------------------------------------------------------------------
@ -1163,15 +1159,9 @@ nsOfflineCacheUpdate::GetCacheKey(nsIURI *aURI, nsACString &aKey)
aKey.Truncate();
nsCOMPtr<nsIURI> newURI;
nsresult rv = aURI->Clone(getter_AddRefs(newURI));
nsresult rv = aURI->CloneIgnoringRef(getter_AddRefs(newURI));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURL> newURL;
newURL = do_QueryInterface(newURI);
if (newURL) {
newURL->SetRef(EmptyCString());
}
rv = newURI->GetAsciiSpec(aKey);
NS_ENSURE_SUCCESS(rv, rv);