Bug 1164292 - Hoist refcounting into nsJSPrincipals. r=gabor
authorBobby Holley <bobbyholley@gmail.com>
Mon, 11 May 2015 13:16:36 -0700
changeset 274883 d99fd0b0a9734668292428d20163eea2e02789a6
parent 274882 415a89038ea1adcb61957cf7683542d6f1c7e2cb
child 274884 2444ca5b01948e383ff4c34b32e533e31da323a0
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-beta@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgabor
bugs1164292
milestone41.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 1164292 - Hoist refcounting into nsJSPrincipals. r=gabor This is a special-snowflake reference counting system that's tied to JSPrincipals, so it makes sense to consolidate this on nsJSPrincipals.
caps/nsJSPrincipals.cpp
caps/nsJSPrincipals.h
caps/nsNullPrincipal.cpp
caps/nsNullPrincipal.h
caps/nsPrincipal.cpp
caps/nsPrincipal.h
caps/nsSystemPrincipal.cpp
caps/nsSystemPrincipal.h
--- a/caps/nsJSPrincipals.cpp
+++ b/caps/nsJSPrincipals.cpp
@@ -14,16 +14,40 @@
 #include "nsIJSRuntimeService.h"
 #include "nsIServiceManager.h"
 #include "nsMemory.h"
 #include "nsStringBuffer.h"
 
 // for mozilla::dom::workers::kJSPrincipalsDebugToken
 #include "mozilla/dom/workers/Workers.h"
 
+NS_IMETHODIMP_(MozExternalRefCountType)
+nsJSPrincipals::AddRef()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
+  nsrefcnt count = ++refcount;
+  NS_LOG_ADDREF(this, count, "nsJSPrincipals", sizeof(*this));
+  return count;
+}
+
+NS_IMETHODIMP_(MozExternalRefCountType)
+nsJSPrincipals::Release()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  NS_PRECONDITION(0 != refcount, "dup release");
+  nsrefcnt count = --refcount;
+  NS_LOG_RELEASE(this, count, "nsJSPrincipals");
+  if (count == 0) {
+    delete this;
+  }
+
+  return count;
+}
+
 /* static */ bool
 nsJSPrincipals::Subsume(JSPrincipals *jsprin, JSPrincipals *other)
 {
     bool result;
     nsresult rv = nsJSPrincipals::get(jsprin)->Subsumes(nsJSPrincipals::get(other), &result);
     return NS_SUCCEEDED(rv) && result;
 }
 
--- a/caps/nsJSPrincipals.h
+++ b/caps/nsJSPrincipals.h
@@ -4,51 +4,57 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 /* describes principals by their orginating uris*/
 
 #ifndef nsJSPrincipals_h__
 #define nsJSPrincipals_h__
 #include "jsapi.h"
 #include "nsIPrincipal.h"
 
-struct nsJSPrincipals : nsIPrincipal, JSPrincipals
+class nsJSPrincipals : public nsIPrincipal, public JSPrincipals
 {
+public:
+  /* SpiderMonkey security callbacks. */
   static bool Subsume(JSPrincipals *jsprin, JSPrincipals *other);
   static void Destroy(JSPrincipals *jsprin);
 
   /*
    * Get a weak reference to nsIPrincipal associated with the given JS
-   * principal.
+   * principal, and vice-versa.
    */
   static nsJSPrincipals* get(JSPrincipals *principals) {
     nsJSPrincipals *self = static_cast<nsJSPrincipals *>(principals);
     MOZ_ASSERT_IF(self, self->debugToken == DEBUG_TOKEN);
     return self;
   }
-  
   static nsJSPrincipals* get(nsIPrincipal *principal) {
     nsJSPrincipals *self = static_cast<nsJSPrincipals *>(principal);
     MOZ_ASSERT_IF(self, self->debugToken == DEBUG_TOKEN);
     return self;
   }
 
+  NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override;
+  NS_IMETHOD_(MozExternalRefCountType) Release(void) override;
+
   nsJSPrincipals() {
     refcount = 0;
     setDebugToken(DEBUG_TOKEN);
   }
 
-  virtual ~nsJSPrincipals() {
-    setDebugToken(0);
-  }
-
   /**
    * Return a string that can be used as JS script filename in error reports.
    */
   virtual void GetScriptLocation(nsACString &aStr) = 0;
 
 #ifdef DEBUG
   virtual void dumpImpl() = 0;
 #endif
 
   static const uint32_t DEBUG_TOKEN = 0x0bf41760;
+
+protected:
+  virtual ~nsJSPrincipals() {
+    setDebugToken(0);
+  }
+
 };
 
 #endif /* nsJSPrincipals_h__ */
--- a/caps/nsNullPrincipal.cpp
+++ b/caps/nsNullPrincipal.cpp
@@ -32,46 +32,16 @@ NS_IMPL_CLASSINFO(nsNullPrincipal, nullp
                   NS_NULLPRINCIPAL_CID)
 NS_IMPL_QUERY_INTERFACE_CI(nsNullPrincipal,
                            nsIPrincipal,
                            nsISerializable)
 NS_IMPL_CI_INTERFACE_GETTER(nsNullPrincipal,
                             nsIPrincipal,
                             nsISerializable)
 
-NS_IMETHODIMP_(MozExternalRefCountType)
-nsNullPrincipal::AddRef()
-{
-  NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
-  nsrefcnt count = ++refcount;
-  NS_LOG_ADDREF(this, count, "nsNullPrincipal", sizeof(*this));
-  return count;
-}
-
-NS_IMETHODIMP_(MozExternalRefCountType)
-nsNullPrincipal::Release()
-{
-  NS_PRECONDITION(0 != refcount, "dup release");
-  nsrefcnt count = --refcount;
-  NS_LOG_RELEASE(this, count, "nsNullPrincipal");
-  if (count == 0) {
-    delete this;
-  }
-
-  return count;
-}
-
-nsNullPrincipal::nsNullPrincipal()
-{
-}
-
-nsNullPrincipal::~nsNullPrincipal()
-{
-}
-
 /* static */ already_AddRefed<nsNullPrincipal>
 nsNullPrincipal::CreateWithInheritedAttributes(nsIPrincipal* aInheritFrom)
 {
   nsRefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
   nsresult rv = nullPrin->Init(aInheritFrom->GetAppId(),
                                aInheritFrom->GetIsInBrowserElement());
   return NS_SUCCEEDED(rv) ? nullPrin.forget() : nullptr;
 }
--- a/caps/nsNullPrincipal.h
+++ b/caps/nsNullPrincipal.h
@@ -28,27 +28,21 @@ class nsIURI;
 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
 
 class nsNullPrincipal final : public nsJSPrincipals
 {
 public:
   // This should only be used by deserialization, and the factory constructor.
   // Other consumers should use the Create and CreateWithInheritedAttributes
   // methods.
-  nsNullPrincipal();
-
-  // Our refcount is managed by nsJSPrincipals.  Use this macro to avoid an
-  // extra refcount member.
+  nsNullPrincipal() {}
 
-  // FIXME: bug 327245 -- I sorta wish there were a clean way to share the
-  // nsJSPrincipals munging code between the various principal classes without
-  // giving up the NS_DECL_NSIPRINCIPAL goodness.
-  NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSIPRINCIPAL
   NS_DECL_NSISERIALIZABLE
+  NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) 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);
@@ -58,17 +52,17 @@ public:
 
   virtual void GetScriptLocation(nsACString &aStr) override;
 
 #ifdef DEBUG
   virtual void dumpImpl() override;
 #endif 
 
  protected:
-  virtual ~nsNullPrincipal();
+  virtual ~nsNullPrincipal() {}
 
   nsCOMPtr<nsIURI> mURI;
   nsCOMPtr<nsIContentSecurityPolicy> mCSP;
   uint32_t mAppId;
   bool mInMozBrowser;
 };
 
 #endif // nsNullPrincipal_h__
--- a/caps/nsPrincipal.cpp
+++ b/caps/nsPrincipal.cpp
@@ -44,47 +44,16 @@ static bool URIIsImmutable(nsIURI* aURI)
     mutableObj &&
     NS_SUCCEEDED(mutableObj->GetMutable(&isMutable)) &&
     !isMutable;
 }
 
 // Static member variables
 const char nsBasePrincipal::sInvalid[] = "Invalid";
 
-NS_IMETHODIMP_(MozExternalRefCountType)
-nsBasePrincipal::AddRef()
-{
-  NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
-  // XXXcaa does this need to be threadsafe?  See bug 143559.
-  nsrefcnt count = ++refcount;
-  NS_LOG_ADDREF(this, count, "nsBasePrincipal", sizeof(*this));
-  return count;
-}
-
-NS_IMETHODIMP_(MozExternalRefCountType)
-nsBasePrincipal::Release()
-{
-  NS_PRECONDITION(0 != refcount, "dup release");
-  nsrefcnt count = --refcount;
-  NS_LOG_RELEASE(this, count, "nsBasePrincipal");
-  if (count == 0) {
-    delete this;
-  }
-
-  return count;
-}
-
-nsBasePrincipal::nsBasePrincipal()
-{
-}
-
-nsBasePrincipal::~nsBasePrincipal(void)
-{
-}
-
 NS_IMETHODIMP
 nsBasePrincipal::GetCsp(nsIContentSecurityPolicy** aCsp)
 {
   NS_IF_ADDREF(*aCsp = mCSP);
   return NS_OK;
 }
 
 NS_IMETHODIMP
@@ -111,18 +80,16 @@ void nsPrincipal::dumpImpl()
 NS_IMPL_CLASSINFO(nsPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
                   NS_PRINCIPAL_CID)
 NS_IMPL_QUERY_INTERFACE_CI(nsPrincipal,
                            nsIPrincipal,
                            nsISerializable)
 NS_IMPL_CI_INTERFACE_GETTER(nsPrincipal,
                             nsIPrincipal,
                             nsISerializable)
-NS_IMPL_ADDREF_INHERITED(nsPrincipal, nsBasePrincipal)
-NS_IMPL_RELEASE_INHERITED(nsPrincipal, nsBasePrincipal)
 
 // Called at startup:
 /* static */ void
 nsPrincipal::InitializeStatics()
 {
   Preferences::AddBoolVarCache(
     &gIsWhitelistingTestDomains,
     "layout.css.unprefixing-service.include-test-domains");
@@ -795,18 +762,16 @@ static const char EXPANDED_PRINCIPAL_SPE
 NS_IMPL_CLASSINFO(nsExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
                   NS_EXPANDEDPRINCIPAL_CID)
 NS_IMPL_QUERY_INTERFACE_CI(nsExpandedPrincipal,
                            nsIPrincipal,
                            nsIExpandedPrincipal)
 NS_IMPL_CI_INTERFACE_GETTER(nsExpandedPrincipal,
                              nsIPrincipal,
                              nsIExpandedPrincipal)
-NS_IMPL_ADDREF_INHERITED(nsExpandedPrincipal, nsBasePrincipal)
-NS_IMPL_RELEASE_INHERITED(nsExpandedPrincipal, nsBasePrincipal)
 
 nsExpandedPrincipal::nsExpandedPrincipal(nsTArray<nsCOMPtr <nsIPrincipal> > &aWhiteList)
 {
   mPrincipals.AppendElements(aWhiteList);
 }
 
 nsExpandedPrincipal::~nsExpandedPrincipal()
 { }
--- a/caps/nsPrincipal.h
+++ b/caps/nsPrincipal.h
@@ -6,31 +6,30 @@
 #ifndef nsPrincipal_h__
 #define nsPrincipal_h__
 
 #include "nsAutoPtr.h"
 #include "nsCOMPtr.h"
 #include "nsJSPrincipals.h"
 #include "nsTArray.h"
 #include "nsAutoPtr.h"
+#include "nsIContentSecurityPolicy.h"
 #include "nsIProtocolHandler.h"
 #include "nsNetUtil.h"
 #include "nsScriptSecurityManager.h"
 
 class nsBasePrincipal : public nsJSPrincipals
 {
 public:
-  nsBasePrincipal();
+  nsBasePrincipal() {}
 
 protected:
-  virtual ~nsBasePrincipal();
+  virtual ~nsBasePrincipal() {}
 
 public:
-  NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
-  NS_IMETHOD_(MozExternalRefCountType) Release(void);
   NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp);
   NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp);
 public:
 
   static const char sInvalid[];
 
 protected:
 
@@ -39,18 +38,18 @@ protected:
 #endif
 
   nsCOMPtr<nsIContentSecurityPolicy> mCSP;
 };
 
 class nsPrincipal final : public nsBasePrincipal
 {
 public:
-  NS_DECL_ISUPPORTS_INHERITED
   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;
@@ -128,19 +127,21 @@ class nsExpandedPrincipal : public nsIEx
 {
 public:
   explicit nsExpandedPrincipal(nsTArray< nsCOMPtr<nsIPrincipal> > &aWhiteList);
 
 protected:
   virtual ~nsExpandedPrincipal();
 
 public:
-  NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSIEXPANDEDPRINCIPAL
   NS_DECL_NSISERIALIZABLE
+  NS_IMETHODIMP_(MozExternalRefCountType) AddRef() override { return nsJSPrincipals::AddRef(); };
+  NS_IMETHODIMP_(MozExternalRefCountType) Release() override { return nsJSPrincipals::Release(); };
+  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;
--- a/caps/nsSystemPrincipal.cpp
+++ b/caps/nsSystemPrincipal.cpp
@@ -24,38 +24,16 @@ NS_IMPL_CLASSINFO(nsSystemPrincipal, nul
                   NS_SYSTEMPRINCIPAL_CID)
 NS_IMPL_QUERY_INTERFACE_CI(nsSystemPrincipal,
                            nsIPrincipal,
                            nsISerializable)
 NS_IMPL_CI_INTERFACE_GETTER(nsSystemPrincipal,
                             nsIPrincipal,
                             nsISerializable)
 
-NS_IMETHODIMP_(MozExternalRefCountType)
-nsSystemPrincipal::AddRef()
-{
-  NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
-  nsrefcnt count = ++refcount;
-  NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this));
-  return count;
-}
-
-NS_IMETHODIMP_(MozExternalRefCountType)
-nsSystemPrincipal::Release()
-{
-  NS_PRECONDITION(0 != refcount, "dup release");
-  nsrefcnt count = --refcount;
-  NS_LOG_RELEASE(this, count, "nsSystemPrincipal");
-  if (count == 0) {
-    delete this;
-  }
-
-  return count;
-}
-
 static const char SYSTEM_PRINCIPAL_SPEC[] = "[System Principal]";
 
 void
 nsSystemPrincipal::GetScriptLocation(nsACString &aStr)
 {
     aStr.Assign(SYSTEM_PRINCIPAL_SPEC);
 }
 
@@ -220,20 +198,8 @@ nsSystemPrincipal::Read(nsIObjectInputSt
 }
 
 NS_IMETHODIMP
 nsSystemPrincipal::Write(nsIObjectOutputStream* aStream)
 {
     // no-op: CID is sufficient to identify the mSystemPrincipal singleton
     return NS_OK;
 }
-
-/////////////////////////////////////////////
-// Constructor, Destructor, initialization //
-/////////////////////////////////////////////
-
-nsSystemPrincipal::nsSystemPrincipal()
-{
-}
-
-nsSystemPrincipal::~nsSystemPrincipal()
-{
-}
--- a/caps/nsSystemPrincipal.h
+++ b/caps/nsSystemPrincipal.h
@@ -15,30 +15,25 @@
 { 0x4a6212db, 0xaccb, 0x11d3, \
 { 0xb7, 0x65, 0x0, 0x60, 0xb0, 0xb6, 0xce, 0xcb }}
 #define NS_SYSTEMPRINCIPAL_CONTRACTID "@mozilla.org/systemprincipal;1"
 
 
 class nsSystemPrincipal final : public nsJSPrincipals
 {
 public:
-    // Our refcount is managed by nsJSPrincipals.  Use this macro to avoid
-    // an extra refcount member.
-    NS_DECL_ISUPPORTS_INHERITED
     NS_DECL_NSIPRINCIPAL
     NS_DECL_NSISERIALIZABLE
+    NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
 
-    nsSystemPrincipal();
+    nsSystemPrincipal() {}
 
     virtual void GetScriptLocation(nsACString &aStr) override;
 
 #ifdef DEBUG
     virtual void dumpImpl() override;
 #endif 
 
 protected:
-    virtual ~nsSystemPrincipal(void);
-
-    // XXX Probably unnecessary.  See bug 143559.
-    NS_DECL_OWNINGTHREAD
+    virtual ~nsSystemPrincipal(void) {}
 };
 
 #endif // nsSystemPrincipal_h__