Bug 1164977 - Hoist all the app attribute handling into BasePrincipal. r=gabor

This commit is contained in:
Bobby Holley 2015-05-13 18:25:40 -07:00
parent d8e55fab8f
commit 96d16ea099
8 changed files with 68 additions and 196 deletions

View File

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/BasePrincipal.h"
#include "nsScriptSecurityManager.h"
namespace mozilla {
@ -34,5 +35,53 @@ BasePrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
return NS_OK;
}
NS_IMETHODIMP
BasePrincipal::GetJarPrefix(nsACString& aJarPrefix)
{
MOZ_ASSERT(mAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
mozilla::GetJarPrefix(mAppId, mIsInBrowserElement, aJarPrefix);
return NS_OK;
}
NS_IMETHODIMP
BasePrincipal::GetAppStatus(uint16_t* aAppStatus)
{
if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
NS_WARNING("Asking for app status on a principal with an unknown app id");
*aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
return NS_OK;
}
*aAppStatus = nsScriptSecurityManager::AppStatusForPrincipal(this);
return NS_OK;
}
NS_IMETHODIMP
BasePrincipal::GetAppId(uint32_t* aAppId)
{
if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
MOZ_ASSERT(false);
*aAppId = nsIScriptSecurityManager::NO_APP_ID;
return NS_OK;
}
*aAppId = mAppId;
return NS_OK;
}
NS_IMETHODIMP
BasePrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
{
*aIsInBrowserElement = mIsInBrowserElement;
return NS_OK;
}
NS_IMETHODIMP
BasePrincipal::GetUnknownAppId(bool* aUnknownAppId)
{
*aUnknownAppId = mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID;
return NS_OK;
}
} // namespace mozilla

View File

@ -8,6 +8,7 @@
#define mozilla_BasePrincipal_h
#include "nsIPrincipal.h"
#include "nsIScriptSecurityManager.h"
#include "nsJSPrincipals.h"
namespace mozilla {
@ -22,10 +23,19 @@ namespace mozilla {
class BasePrincipal : public nsJSPrincipals
{
public:
BasePrincipal() {}
BasePrincipal()
: mAppId(nsIScriptSecurityManager::NO_APP_ID)
, mIsInBrowserElement(false)
{}
NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp) override;
NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;
NS_IMETHOD GetIsNullPrincipal(bool* aIsNullPrincipal) override;
NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix) final;
NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) final;
NS_IMETHOD GetAppId(uint32_t* aAppStatus) final;
NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) final;
NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) final;
virtual bool IsOnCSSUnprefixingWhitelist() override { return false; }
@ -33,6 +43,8 @@ protected:
virtual ~BasePrincipal() {}
nsCOMPtr<nsIContentSecurityPolicy> mCSP;
uint32_t mAppId;
bool mIsInBrowserElement;
};
} // namespace mozilla

View File

@ -61,7 +61,7 @@ nsNullPrincipal::Init(uint32_t aAppId, bool aInMozBrowser)
{
MOZ_ASSERT(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
mAppId = aAppId;
mInMozBrowser = aInMozBrowser;
mIsInBrowserElement = aInMozBrowser;
mURI = nsNullPrincipalURI::Create();
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
@ -171,41 +171,6 @@ nsNullPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport, bool aAllowIfInheritsP
return NS_ERROR_DOM_BAD_URI;
}
NS_IMETHODIMP
nsNullPrincipal::GetJarPrefix(nsACString& aJarPrefix)
{
aJarPrefix.Truncate();
return NS_OK;
}
NS_IMETHODIMP
nsNullPrincipal::GetAppStatus(uint16_t* aAppStatus)
{
*aAppStatus = nsScriptSecurityManager::AppStatusForPrincipal(this);
return NS_OK;
}
NS_IMETHODIMP
nsNullPrincipal::GetAppId(uint32_t* aAppId)
{
*aAppId = mAppId;
return NS_OK;
}
NS_IMETHODIMP
nsNullPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
{
*aIsInBrowserElement = mInMozBrowser;
return NS_OK;
}
NS_IMETHODIMP
nsNullPrincipal::GetUnknownAppId(bool* aUnknownAppId)
{
*aUnknownAppId = false;
return NS_OK;
}
NS_IMETHODIMP
nsNullPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
{
@ -233,7 +198,7 @@ nsNullPrincipal::Read(nsIObjectInputStream* aStream)
nsresult rv = aStream->Read32(&mAppId);
NS_ENSURE_SUCCESS(rv, rv);
rv = aStream->ReadBoolean(&mInMozBrowser);
rv = aStream->ReadBoolean(&mIsInBrowserElement);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
@ -243,7 +208,7 @@ NS_IMETHODIMP
nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
{
aStream->Write32(mAppId);
aStream->WriteBoolean(mInMozBrowser);
aStream->WriteBoolean(mIsInBrowserElement);
return NS_OK;
}

View File

@ -50,11 +50,6 @@ public:
NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval) override;
NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval) override;
NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) override;
NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix) override;
NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) override;
NS_IMETHOD GetAppId(uint32_t* aAppStatus) override;
NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) override;
NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) override;
NS_IMETHOD GetIsNullPrincipal(bool* aIsNullPrincipal) override;
NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
@ -76,8 +71,6 @@ public:
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIContentSecurityPolicy> mCSP;
uint32_t mAppId;
bool mInMozBrowser;
};
#endif // nsNullPrincipal_h__

View File

@ -69,9 +69,7 @@ nsPrincipal::InitializeStatics()
}
nsPrincipal::nsPrincipal()
: mAppId(nsIScriptSecurityManager::UNKNOWN_APP_ID)
, mInMozBrowser(false)
, mCodebaseImmutable(false)
: mCodebaseImmutable(false)
, mDomainImmutable(false)
, mInitialized(false)
{ }
@ -93,7 +91,7 @@ nsPrincipal::Init(nsIURI *aCodebase,
mCodebaseImmutable = URIIsImmutable(mCodebase);
mAppId = aAppId;
mInMozBrowser = aInMozBrowser;
mIsInBrowserElement = aInMozBrowser;
return NS_OK;
}
@ -361,49 +359,6 @@ nsPrincipal::SetDomain(nsIURI* aDomain)
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetJarPrefix(nsACString& aJarPrefix)
{
MOZ_ASSERT(mAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
mozilla::GetJarPrefix(mAppId, mInMozBrowser, aJarPrefix);
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetAppStatus(uint16_t* aAppStatus)
{
*aAppStatus = GetAppStatus();
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetAppId(uint32_t* aAppId)
{
if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
MOZ_ASSERT(false);
*aAppId = nsIScriptSecurityManager::NO_APP_ID;
return NS_OK;
}
*aAppId = mAppId;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
{
*aIsInBrowserElement = mInMozBrowser;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetUnknownAppId(bool* aUnknownAppId)
{
*aUnknownAppId = mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID;
return NS_OK;
}
NS_IMETHODIMP
nsPrincipal::GetBaseDomain(nsACString& aBaseDomain)
{
@ -508,7 +463,7 @@ nsPrincipal::Write(nsIObjectOutputStream* aStream)
}
aStream->Write32(mAppId);
aStream->WriteBoolean(mInMozBrowser);
aStream->WriteBoolean(mIsInBrowserElement);
rv = NS_WriteOptionalCompoundObject(aStream, mCSP,
NS_GET_IID(nsIContentSecurityPolicy),
@ -523,16 +478,6 @@ nsPrincipal::Write(nsIObjectOutputStream* aStream)
return NS_OK;
}
uint16_t
nsPrincipal::GetAppStatus()
{
if (mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
NS_WARNING("Asking for app status on a principal with an unknown app id");
return nsIPrincipal::APP_STATUS_NOT_INSTALLED;
}
return nsScriptSecurityManager::AppStatusForPrincipal(this);
}
// Helper-function to indicate whether the CSS Unprefixing Service
// whitelist should include dummy domains that are only intended for
// use in testing. (Controlled by a pref.)
@ -916,41 +861,6 @@ nsExpandedPrincipal::GetWhiteList(nsTArray<nsCOMPtr<nsIPrincipal> >** aWhiteList
return NS_OK;
}
NS_IMETHODIMP
nsExpandedPrincipal::GetJarPrefix(nsACString& aJarPrefix)
{
aJarPrefix.Truncate();
return NS_OK;
}
NS_IMETHODIMP
nsExpandedPrincipal::GetAppStatus(uint16_t* aAppStatus)
{
*aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
return NS_OK;
}
NS_IMETHODIMP
nsExpandedPrincipal::GetAppId(uint32_t* aAppId)
{
*aAppId = nsIScriptSecurityManager::NO_APP_ID;
return NS_OK;
}
NS_IMETHODIMP
nsExpandedPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
{
*aIsInBrowserElement = false;
return NS_OK;
}
NS_IMETHODIMP
nsExpandedPrincipal::GetUnknownAppId(bool* aUnknownAppId)
{
*aUnknownAppId = false;
return NS_OK;
}
NS_IMETHODIMP
nsExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain)
{

View File

@ -32,11 +32,6 @@ public:
NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval) override;
NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval) override;
NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) override;
NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix) override;
NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) override;
NS_IMETHOD GetAppId(uint32_t* aAppStatus) override;
NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) override;
NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) override;
NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
virtual bool IsOnCSSUnprefixingWhitelist() override;
@ -79,8 +74,6 @@ public:
nsCOMPtr<nsIURI> mDomain;
nsCOMPtr<nsIURI> mCodebase;
uint32_t mAppId;
bool mInMozBrowser;
// If mCodebaseImmutable is true, mCodebase is non-null and immutable
bool mCodebaseImmutable;
bool mDomainImmutable;
@ -89,11 +82,6 @@ public:
protected:
virtual ~nsPrincipal();
/**
* Returns the app status of the principal based on mAppId and mInMozBrowser.
*/
uint16_t GetAppStatus();
};
class nsExpandedPrincipal : public nsIExpandedPrincipal, public mozilla::BasePrincipal
@ -120,11 +108,6 @@ public:
NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval) override;
NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval) override;
NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) override;
NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix) override;
NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) override;
NS_IMETHOD GetAppId(uint32_t* aAppStatus) override;
NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) override;
NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) override;
NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
virtual bool IsOnCSSUnprefixingWhitelist() override;
virtual void GetScriptLocation(nsACString &aStr) override;

View File

@ -122,41 +122,6 @@ nsSystemPrincipal::SetDomain(nsIURI* aDomain)
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetJarPrefix(nsACString& aJarPrefix)
{
aJarPrefix.Truncate();
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetAppStatus(uint16_t* aAppStatus)
{
*aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetAppId(uint32_t* aAppId)
{
*aAppId = nsIScriptSecurityManager::NO_APP_ID;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
{
*aIsInBrowserElement = false;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetUnknownAppId(bool* aUnknownAppId)
{
*aUnknownAppId = false;
return NS_OK;
}
NS_IMETHODIMP
nsSystemPrincipal::GetBaseDomain(nsACString& aBaseDomain)
{

View File

@ -37,11 +37,6 @@ public:
NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) override;
NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp) override;
NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;
NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix) override;
NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) override;
NS_IMETHOD GetAppId(uint32_t* aAppStatus) override;
NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) override;
NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) override;
NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
nsSystemPrincipal() {}