From a809017de50fe6b59682cc7a6cfffcc7f2a3ffee Mon Sep 17 00:00:00 2001 From: Mounir Lamouri Date: Wed, 18 Jul 2012 21:23:44 -0700 Subject: [PATCH] Bug 775354 - Add a static method to be able to compute the origin based on nsPrincipal algorithm. r=mrbkap --- caps/include/nsPrincipal.h | 5 +++++ caps/src/nsPrincipal.cpp | 26 +++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/caps/include/nsPrincipal.h b/caps/include/nsPrincipal.h index 5b691f1d6725..82b9ce5a9c6e 100644 --- a/caps/include/nsPrincipal.h +++ b/caps/include/nsPrincipal.h @@ -153,6 +153,11 @@ public: virtual void GetScriptLocation(nsACString& aStr) MOZ_OVERRIDE; void SetURI(nsIURI* aURI); + /** + * Computes the puny-encoded origin of aURI. + */ + static nsresult GetOriginForURI(nsIURI* aURI, char **aOrigin); + nsCOMPtr mDomain; nsCOMPtr mCodebase; // If mCodebaseImmutable is true, mCodebase is non-null and immutable diff --git a/caps/src/nsPrincipal.cpp b/caps/src/nsPrincipal.cpp index b516e6a07e43..1df8feb065ef 100644 --- a/caps/src/nsPrincipal.cpp +++ b/caps/src/nsPrincipal.cpp @@ -656,18 +656,17 @@ nsPrincipal::GetScriptLocation(nsACString &aStr) } } -NS_IMETHODIMP -nsPrincipal::GetOrigin(char **aOrigin) +/* static */ nsresult +nsPrincipal::GetOriginForURI(nsIURI* aURI, char **aOrigin) { + if (!aURI) { + return NS_ERROR_FAILURE; + } + *aOrigin = nsnull; - nsCOMPtr origin; - if (mCodebase) { - origin = NS_GetInnermostURI(mCodebase); - } - + nsCOMPtr origin = NS_GetInnermostURI(aURI); if (!origin) { - NS_ASSERTION(mCert, "No Domain or Codebase for a non-cert principal"); return NS_ERROR_FAILURE; } @@ -683,8 +682,9 @@ nsPrincipal::GetOrigin(char **aOrigin) rv = origin->GetAsciiHost(hostPort); // Some implementations return an empty string, treat it as no support // for asciiHost by that implementation. - if (hostPort.IsEmpty()) + if (hostPort.IsEmpty()) { rv = NS_ERROR_FAILURE; + } } PRInt32 port; @@ -701,6 +701,7 @@ nsPrincipal::GetOrigin(char **aOrigin) nsCAutoString scheme; rv = origin->GetScheme(scheme); NS_ENSURE_SUCCESS(rv, rv); + *aOrigin = ToNewCString(scheme + NS_LITERAL_CSTRING("://") + hostPort); } else { @@ -711,12 +712,19 @@ nsPrincipal::GetOrigin(char **aOrigin) // both fall back to GetSpec. That needs to be fixed. rv = origin->GetAsciiSpec(spec); NS_ENSURE_SUCCESS(rv, rv); + *aOrigin = ToNewCString(spec); } return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY; } +NS_IMETHODIMP +nsPrincipal::GetOrigin(char **aOrigin) +{ + return GetOriginForURI(mCodebase, aOrigin); +} + NS_IMETHODIMP nsPrincipal::Equals(nsIPrincipal *aOther, bool *aResult) {