Bug 1164292 - Make all nsIPrincipal implementations inherit BasePrincipal and hoist some repeated code. r=gabor
authorBobby Holley <bobbyholley@gmail.com>
Mon, 11 May 2015 14:36:56 -0700
changeset 264916 1f283549fee9060c305c98333fd091c472874898
parent 264915 f9583ef3d16b1b1e29b0fa4388e02799d4ff0bd4
child 264917 217e812d233192fe5402f5c885d4d37108f3c524
push id2087
push userwcosta@mozilla.com
push dateSun, 17 May 2015 13:14:37 +0000
reviewersgabor
bugs1164292
milestone41.0a1
Bug 1164292 - Make all nsIPrincipal implementations inherit BasePrincipal and hoist some repeated code. r=gabor Losing the NS_DECL_NSIPRINCIPAL isn't great, but I think it's worth it to share more code.
caps/BasePrincipal.cpp
caps/BasePrincipal.h
caps/nsNullPrincipal.cpp
caps/nsNullPrincipal.h
caps/nsPrincipal.cpp
caps/nsPrincipal.h
caps/nsSystemPrincipal.cpp
caps/nsSystemPrincipal.h
--- a/caps/BasePrincipal.cpp
+++ b/caps/BasePrincipal.cpp
@@ -22,9 +22,17 @@ BasePrincipal::SetCsp(nsIContentSecurity
   // get set anew when a new principal is created.
   if (mCSP)
     return NS_ERROR_ALREADY_INITIALIZED;
 
   mCSP = aCsp;
   return NS_OK;
 }
 
+NS_IMETHODIMP
+BasePrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
+{
+  *aIsNullPrincipal = false;
+  return NS_OK;
+}
+
+
 } // namespace mozilla
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -20,16 +20,19 @@ namespace mozilla {
  * We should merge nsJSPrincipals into this class at some point.
  */
 class BasePrincipal : public nsJSPrincipals
 {
 public:
   BasePrincipal() {}
   NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp);
   NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp);
+  NS_IMETHOD GetIsNullPrincipal(bool* aIsNullPrincipal) override;
+
+  virtual bool IsOnCSSUnprefixingWhitelist() override { return false; }
 
 protected:
   virtual ~BasePrincipal() {}
 
   nsCOMPtr<nsIContentSecurityPolicy> mCSP;
 };
 
 } // namespace mozilla
--- a/caps/nsNullPrincipal.cpp
+++ b/caps/nsNullPrincipal.cpp
@@ -103,35 +103,16 @@ nsNullPrincipal::GetHashValue(uint32_t *
 
 NS_IMETHODIMP 
 nsNullPrincipal::GetURI(nsIURI** aURI)
 {
   return NS_EnsureSafeToReturn(mURI, aURI);
 }
 
 NS_IMETHODIMP
-nsNullPrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
-{
-  NS_IF_ADDREF(*aCsp = mCSP);
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsNullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp)
-{
-  // If CSP was already set, it should not be destroyed!  Instead, it should
-  // get set anew when a new principal is created.
-  if (mCSP)
-    return NS_ERROR_ALREADY_INITIALIZED;
-
-  mCSP = aCsp;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
 nsNullPrincipal::GetDomain(nsIURI** aDomain)
 {
   return NS_EnsureSafeToReturn(mURI, aDomain);
 }
 
 NS_IMETHODIMP
 nsNullPrincipal::SetDomain(nsIURI* aDomain)
 {
@@ -243,22 +224,16 @@ nsNullPrincipal::GetIsNullPrincipal(bool
 
 NS_IMETHODIMP
 nsNullPrincipal::GetBaseDomain(nsACString& aBaseDomain)
 {
   // For a null principal, we use our unique uuid as the base domain.
   return mURI->GetPath(aBaseDomain);
 }
 
-bool
-nsNullPrincipal::IsOnCSSUnprefixingWhitelist()
-{
-  return false;
-}
-
 /**
  * nsISerializable implementation
  */
 NS_IMETHODIMP
 nsNullPrincipal::Read(nsIObjectInputStream* aStream)
 {
   // Note - nsNullPrincipal use NS_GENERIC_FACTORY_CONSTRUCTOR_INIT, which means
   // that the Init() method has already been invoked by the time we deserialize.
--- a/caps/nsNullPrincipal.h
+++ b/caps/nsNullPrincipal.h
@@ -13,36 +13,55 @@
 #define nsNullPrincipal_h__
 
 #include "nsIPrincipal.h"
 #include "nsJSPrincipals.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsCOMPtr.h"
 #include "nsIContentSecurityPolicy.h"
 
+#include "mozilla/BasePrincipal.h"
+
 class nsIURI;
 
 #define NS_NULLPRINCIPAL_CID \
 { 0xa0bd8b42, 0xf6bf, 0x4fb9, \
   { 0x93, 0x42, 0x90, 0xbf, 0xc9, 0xb7, 0xa1, 0xab } }
 #define NS_NULLPRINCIPAL_CONTRACTID "@mozilla.org/nullprincipal;1"
 
 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
 
-class nsNullPrincipal final : public nsJSPrincipals
+class nsNullPrincipal final : public mozilla::BasePrincipal
 {
 public:
   // This should only be used by deserialization, and the factory constructor.
   // Other consumers should use the Create and CreateWithInheritedAttributes
   // methods.
   nsNullPrincipal() {}
 
-  NS_DECL_NSIPRINCIPAL
   NS_DECL_NSISERIALIZABLE
+
   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
+  NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval) override;
+  NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval) override;
+  NS_IMETHOD GetHashValue(uint32_t* aHashValue) override;
+  NS_IMETHOD GetURI(nsIURI** aURI) override;
+  NS_IMETHOD GetDomain(nsIURI** aDomain) override;
+  NS_IMETHOD SetDomain(nsIURI* aDomain) override;
+  NS_IMETHOD GetOrigin(char** aOrigin) override;
+  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;
 
   // Returns null on failure.
   static already_AddRefed<nsNullPrincipal> CreateWithInheritedAttributes(nsIPrincipal *aInheritFrom);
 
   // Returns null on failure.
   static already_AddRefed<nsNullPrincipal>
     Create(uint32_t aAppId = nsIScriptSecurityManager::NO_APP_ID,
            bool aInMozBrowser = false);
--- a/caps/nsPrincipal.cpp
+++ b/caps/nsPrincipal.cpp
@@ -410,23 +410,16 @@ nsPrincipal::GetIsInBrowserElement(bool*
 NS_IMETHODIMP
 nsPrincipal::GetUnknownAppId(bool* aUnknownAppId)
 {
   *aUnknownAppId = mAppId == nsIScriptSecurityManager::UNKNOWN_APP_ID;
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
-{
-  *aIsNullPrincipal = false;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
 nsPrincipal::GetBaseDomain(nsACString& aBaseDomain)
 {
   // For a file URI, we return the file path.
   if (NS_URIIsLocalFile(mCodebase)) {
     nsCOMPtr<nsIURL> url = do_QueryInterface(mCodebase);
 
     if (url) {
       return url->GetFilePath(aBaseDomain);
@@ -920,23 +913,16 @@ nsExpandedPrincipal::GetIsInBrowserEleme
 NS_IMETHODIMP
 nsExpandedPrincipal::GetUnknownAppId(bool* aUnknownAppId)
 {
   *aUnknownAppId = false;
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsExpandedPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
-{
-  *aIsNullPrincipal = false;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
 nsExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain)
 {
   return NS_ERROR_NOT_AVAILABLE;
 }
 
 bool
 nsExpandedPrincipal::IsOnCSSUnprefixingWhitelist()
 {
--- a/caps/nsPrincipal.h
+++ b/caps/nsPrincipal.h
@@ -32,17 +32,16 @@ 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;
   virtual bool IsOnCSSUnprefixingWhitelist() override;
 
   nsPrincipal();
 
   // Init() must be called before the principal is in a usable state.
   nsresult Init(nsIURI* aCodebase,
                 uint32_t aAppId,
@@ -121,17 +120,16 @@ 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;
   virtual bool IsOnCSSUnprefixingWhitelist() override;
   virtual void GetScriptLocation(nsACString &aStr) override;
 
 private:
   nsTArray< nsCOMPtr<nsIPrincipal> > mPrincipals;
 };
 
--- a/caps/nsSystemPrincipal.cpp
+++ b/caps/nsSystemPrincipal.cpp
@@ -153,36 +153,22 @@ nsSystemPrincipal::GetIsInBrowserElement
 NS_IMETHODIMP
 nsSystemPrincipal::GetUnknownAppId(bool* aUnknownAppId)
 {
   *aUnknownAppId = false;
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsSystemPrincipal::GetIsNullPrincipal(bool* aIsNullPrincipal)
-{
-  *aIsNullPrincipal = false;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
 nsSystemPrincipal::GetBaseDomain(nsACString& aBaseDomain)
 {
   // No base domain for chrome.
   return NS_OK;
 }
 
-bool
-nsSystemPrincipal::IsOnCSSUnprefixingWhitelist()
-{
-  // chrome stylesheets should not be fed to the CSS Unprefixing Service.
-  return false;
-}
-
 //////////////////////////////////////////
 // Methods implementing nsISerializable //
 //////////////////////////////////////////
 
 NS_IMETHODIMP
 nsSystemPrincipal::Read(nsIObjectInputStream* aStream)
 {
     // no-op: CID is sufficient to identify the mSystemPrincipal singleton
--- a/caps/nsSystemPrincipal.h
+++ b/caps/nsSystemPrincipal.h
@@ -1,35 +1,55 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* The privileged system principal. */
 
 #ifndef nsSystemPrincipal_h__
 #define nsSystemPrincipal_h__
 
 #include "nsIPrincipal.h"
 #include "nsJSPrincipals.h"
 
+#include "mozilla/BasePrincipal.h"
+
 #define NS_SYSTEMPRINCIPAL_CID \
 { 0x4a6212db, 0xaccb, 0x11d3, \
 { 0xb7, 0x65, 0x0, 0x60, 0xb0, 0xb6, 0xce, 0xcb }}
 #define NS_SYSTEMPRINCIPAL_CONTRACTID "@mozilla.org/systemprincipal;1"
 
 
-class nsSystemPrincipal final : public nsJSPrincipals
+class nsSystemPrincipal final : public mozilla::BasePrincipal
 {
 public:
-    NS_DECL_NSIPRINCIPAL
-    NS_DECL_NSISERIALIZABLE
-    NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
+  NS_DECL_NSISERIALIZABLE
+  NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
+  NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval) override;
+  NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval) override;
+  NS_IMETHOD GetHashValue(uint32_t* aHashValue) override;
+  NS_IMETHOD GetURI(nsIURI** aURI) override;
+  NS_IMETHOD GetDomain(nsIURI** aDomain) override;
+  NS_IMETHOD SetDomain(nsIURI* aDomain) override;
+  NS_IMETHOD GetOrigin(char** aOrigin) override;
+  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 GetCsp(nsIContentSecurityPolicy** aCsp);
+  NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp);
+  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() {}
+  nsSystemPrincipal() {}
 
-    virtual void GetScriptLocation(nsACString &aStr) override;
+  virtual void GetScriptLocation(nsACString &aStr) override;
 
 protected:
-    virtual ~nsSystemPrincipal(void) {}
+  virtual ~nsSystemPrincipal(void) {}
 };
 
 #endif // nsSystemPrincipal_h__