Bug 1356334: Part 1 - Add helpers for safely casting principals without COM overhead. r=bholley
authorKris Maglione <maglione.k@gmail.com>
Sun, 16 Apr 2017 13:32:42 -0700
changeset 375675 1dde6ae5176d04e63905eb5c7c47178371d94260
parent 375674 ee2f48b5ea07b963d5917e7e606a76abed0f9743
child 375676 4b9185a04fe62a75a5a5b97947cb47f81ba2bfd3
push id32358
push userkwierso@gmail.com
push dateFri, 18 Aug 2017 22:53:28 +0000
treeherdermozilla-central@4f4487cc2d30 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1356334
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1356334: Part 1 - Add helpers for safely casting principals without COM overhead. r=bholley MozReview-Commit-ID: EOtK17WiLZl
caps/BasePrincipal.h
caps/ContentPrincipal.h
caps/ExpandedPrincipal.h
caps/NullPrincipal.h
caps/SystemPrincipal.h
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -35,16 +35,29 @@ public:
     eNullPrincipal,
     eCodebasePrincipal,
     eExpandedPrincipal,
     eSystemPrincipal
   };
 
   explicit BasePrincipal(PrincipalKind aKind);
 
+  template<typename T>
+  bool Is() const
+  {
+    return mKind == T::Kind();
+  }
+
+  template<typename T>
+  T* As()
+  {
+    MOZ_ASSERT(Is<T>());
+    return static_cast<T*>(this);
+  }
+
   enum DocumentDomainConsideration { DontConsiderDocumentDomain, ConsiderDocumentDomain};
   bool Subsumes(nsIPrincipal* aOther, DocumentDomainConsideration aConsideration);
 
   NS_IMETHOD GetOrigin(nsACString& aOrigin) final;
   NS_IMETHOD GetOriginNoSuffix(nsACString& aOrigin) final;
   NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval) final;
   NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval) final;
   NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval) final;
--- a/caps/ContentPrincipal.h
+++ b/caps/ContentPrincipal.h
@@ -25,16 +25,18 @@ public:
   NS_IMETHOD GetDomain(nsIURI** aDomain) override;
   NS_IMETHOD SetDomain(nsIURI* aDomain) override;
   NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
   NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
   bool IsCodebasePrincipal() const override { return true; }
 
   ContentPrincipal();
 
+  static PrincipalKind Kind() { return eCodebasePrincipal; }
+
   // Init() must be called before the principal is in a usable state.
   nsresult Init(nsIURI* aCodebase,
                 const mozilla::OriginAttributes& aOriginAttributes,
                 const nsACString& aOriginNoSuffix);
 
   virtual nsresult GetScriptLocation(nsACString& aStr) override;
 
   /**
--- a/caps/ExpandedPrincipal.h
+++ b/caps/ExpandedPrincipal.h
@@ -15,16 +15,18 @@
 class ExpandedPrincipal : public nsIExpandedPrincipal
                         , public mozilla::BasePrincipal
 {
 public:
   static already_AddRefed<ExpandedPrincipal>
   Create(nsTArray<nsCOMPtr<nsIPrincipal>>& aWhiteList,
          const mozilla::OriginAttributes& aAttrs);
 
+  static PrincipalKind Kind() { return eExpandedPrincipal; }
+
   NS_DECL_NSIEXPANDEDPRINCIPAL
   NS_DECL_NSISERIALIZABLE
 
   NS_IMETHOD_(MozExternalRefCountType) AddRef() override { return nsJSPrincipals::AddRef(); };
   NS_IMETHOD_(MozExternalRefCountType) Release() override { return nsJSPrincipals::Release(); };
   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
   NS_IMETHOD GetHashValue(uint32_t* aHashValue) override;
   NS_IMETHOD GetURI(nsIURI** aURI) override;
--- a/caps/NullPrincipal.h
+++ b/caps/NullPrincipal.h
@@ -36,16 +36,18 @@ public:
   // This should only be used by deserialization, and the factory constructor.
   // Other consumers should use the Create and CreateWithInheritedAttributes
   // methods.
   NullPrincipal()
     : BasePrincipal(eNullPrincipal)
   {
   }
 
+  static PrincipalKind Kind() { return eNullPrincipal; }
+
   NS_DECL_NSISERIALIZABLE
 
   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
   NS_IMETHOD GetHashValue(uint32_t* aHashValue) override;
   NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;
   NS_IMETHOD GetURI(nsIURI** aURI) override;
   NS_IMETHOD GetDomain(nsIURI** aDomain) override;
   NS_IMETHOD SetDomain(nsIURI* aDomain) override;
--- a/caps/SystemPrincipal.h
+++ b/caps/SystemPrincipal.h
@@ -25,16 +25,18 @@ class SystemPrincipal final : public moz
   SystemPrincipal()
     : BasePrincipal(eSystemPrincipal)
   {
   }
 
 public:
   static already_AddRefed<SystemPrincipal> Create();
 
+  static PrincipalKind Kind() { return eSystemPrincipal; }
+
   NS_DECL_NSISERIALIZABLE
   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) 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 GetCsp(nsIContentSecurityPolicy** aCsp) override;
   NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;