Bug 1208371 - Add unique principalId attribute to nsIPrincipal. r?bz,roc draft
authorAndreas Pehrson <pehrsons@gmail.com>
Tue, 02 Feb 2016 13:20:56 +0800
changeset 327850 25541cb87e3f1d941f2018132bc39b706a114ad9
parent 327699 1b3c17817cd0449588fb05db841252fec06f0dfe
child 327851 d5fe731e041bef3faf9529e2bc13effafe90f82a
push id10316
push userpehrsons@gmail.com
push dateTue, 02 Feb 2016 08:23:14 +0000
reviewersbz, roc
bugs1208371
milestone47.0a1
Bug 1208371 - Add unique principalId attribute to nsIPrincipal. r?bz,roc We can pass this in MediaChunks going through the MediaStreamGraph off the main thread. This will be matched to real principals on main thread later to see when data flows under the new principal.
caps/BasePrincipal.cpp
caps/BasePrincipal.h
caps/nsIPrincipal.idl
--- a/caps/BasePrincipal.cpp
+++ b/caps/BasePrincipal.cpp
@@ -19,16 +19,18 @@
 #include "nsScriptSecurityManager.h"
 #include "nsServiceManagerUtils.h"
 
 #include "mozilla/dom/CSPDictionariesBinding.h"
 #include "mozilla/dom/quota/QuotaManager.h"
 #include "mozilla/dom/ToJSValue.h"
 #include "mozilla/dom/URLSearchParams.h"
 
+static uint64_t gPrincipalCounter = 0;
+
 namespace mozilla {
 
 using dom::URLParams;
 
 void
 PrincipalOriginAttributes::InheritFromDocShellToDoc(const DocShellOriginAttributes& aAttrs,
                                                     const nsIURI* aURI)
 {
@@ -249,17 +251,21 @@ OriginAttributes::PopulateFromOrigin(con
     return true;
   }
 
   aOriginNoSuffix = Substring(origin, 0, pos);
   return PopulateFromSuffix(Substring(origin, pos));
 }
 
 BasePrincipal::BasePrincipal()
-{}
+  : mPrincipalId(++gPrincipalCounter)
+{
+  MOZ_ASSERT(NS_IsMainThread(), "gPrincipalCounter is not atomic");
+  MOZ_ASSERT(mPrincipalId != 0, "principalId 0 is invalid");
+}
 
 BasePrincipal::~BasePrincipal()
 {}
 
 NS_IMETHODIMP
 BasePrincipal::GetOrigin(nsACString& aOrigin)
 {
   nsresult rv = GetOriginInternal(aOrigin);
@@ -509,16 +515,23 @@ BasePrincipal::GetAppId(uint32_t* aAppId
 NS_IMETHODIMP
 BasePrincipal::GetUserContextId(uint32_t* aUserContextId)
 {
   *aUserContextId = UserContextId();
   return NS_OK;
 }
 
 NS_IMETHODIMP
+BasePrincipal::GetPrincipalId(uint64_t* aResult)
+{
+  *aResult = mPrincipalId;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
 BasePrincipal::GetIsInBrowserElement(bool* aIsInBrowserElement)
 {
   *aIsInBrowserElement = IsInBrowserElement();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BasePrincipal::GetUnknownAppId(bool* aUnknownAppId)
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -213,16 +213,17 @@ public:
   NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix) final;
   NS_IMETHOD GetOriginAttributes(JSContext* aCx, JS::MutableHandle<JS::Value> aVal) final;
   NS_IMETHOD GetOriginSuffix(nsACString& aOriginSuffix) 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;
   NS_IMETHOD GetUserContextId(uint32_t* aUserContextId) final;
+  NS_IMETHOD GetPrincipalId(uint64_t* aResult) final;
 
   virtual bool IsOnCSSUnprefixingWhitelist() override { return false; }
 
   virtual bool IsCodebasePrincipal() const { return false; };
 
   static BasePrincipal* Cast(nsIPrincipal* aPrin) { return static_cast<BasePrincipal*>(aPrin); }
   static already_AddRefed<BasePrincipal>
   CreateCodebasePrincipal(nsIURI* aURI, const PrincipalOriginAttributes& aAttrs);
@@ -256,13 +257,15 @@ protected:
 
   // Helper to check whether this principal is associated with an addon that
   // allows unprivileged code to load aURI.
   bool AddonAllowsLoad(nsIURI* aURI);
 
   nsCOMPtr<nsIContentSecurityPolicy> mCSP;
   nsCOMPtr<nsIContentSecurityPolicy> mPreloadCSP;
   PrincipalOriginAttributes mOriginAttributes;
+
+  const uint64_t mPrincipalId;
 };
 
 } // namespace mozilla
 
 #endif /* mozilla_BasePrincipal_h */
--- a/caps/nsIPrincipal.idl
+++ b/caps/nsIPrincipal.idl
@@ -339,16 +339,23 @@ interface nsIPrincipal : nsISerializable
     /**
      * Returns true if this principal's origin is recognized as being on the
      * whitelist of sites that can use the CSS Unprefixing Service.
      *
      * (This interface provides a trivial implementation, just returning false;
      * subclasses can implement something more complex as-needed.)
      */
     [noscript,notxpcom,nostdcall] bool IsOnCSSUnprefixingWhitelist();
+
+    /**
+     * Globally unique identifier of this principal that can for instance be
+     * passed to non-main threads for later identification of the principal.
+     * A valid principalId is never 0.
+     */
+    [noscript,infallible] readonly attribute unsigned long long principalId;
 };
 
 /**
  * If nsSystemPrincipal is too risky to use, but we want a principal to access
  * more than one origin, nsExpandedPrincipals letting us define an array of
  * principals it subsumes. So script with an nsExpandedPrincipals will gain
  * same origin access when at least one of its principals it contains gained
  * sameorigin acccess. An nsExpandedPrincipal will be subsumed by the system