Bug 1164977 - Hoist attribute serialization into BasePrincipal. r=gabor
authorBobby Holley <bobbyholley@gmail.com>
Thu, 14 May 2015 13:56:37 -0700
changeset 244465 e18c56c6ade50594df27b3926df2b4f30eb00505
parent 244464 d4e6af40c5e87de3f2e91c6eeca96954d92c0166
child 244466 df6f82e4a2dbc8fe04121ab4f90f48a68ded91fb
push id28781
push usercbook@mozilla.com
push dateTue, 19 May 2015 10:01:15 +0000
treeherdermozilla-central@4fb7ff694bf5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgabor
bugs1164977
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 1164977 - Hoist attribute serialization into BasePrincipal. r=gabor
caps/BasePrincipal.cpp
caps/BasePrincipal.h
caps/nsNullPrincipal.cpp
caps/nsPrincipal.cpp
--- a/caps/BasePrincipal.cpp
+++ b/caps/BasePrincipal.cpp
@@ -1,19 +1,41 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=2 sw=2 et 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/. */
 
 #include "mozilla/BasePrincipal.h"
+
+#include "nsIObjectInputStream.h"
+#include "nsIObjectOutputStream.h"
 #include "nsScriptSecurityManager.h"
 
 namespace mozilla {
 
+void
+BasePrincipal::OriginAttributes::Serialize(nsIObjectOutputStream* aStream) const
+{
+  aStream->Write32(mAppId);
+  aStream->WriteBoolean(mIsInBrowserElement);
+}
+
+nsresult
+BasePrincipal::OriginAttributes::Deserialize(nsIObjectInputStream* aStream)
+{
+  nsresult rv = aStream->Read32(&mAppId);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = aStream->ReadBoolean(&mIsInBrowserElement);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  return NS_OK;
+}
+
 bool
 BasePrincipal::Subsumes(nsIPrincipal* aOther, DocumentDomainConsideration aConsideration)
 {
   MOZ_RELEASE_ASSERT(aOther, "The caller is performing a nonsensical security check!");
   return SubsumesInternal(aOther, aConsideration);
 }
 
 NS_IMETHODIMP
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -6,16 +6,19 @@
 
 #ifndef mozilla_BasePrincipal_h
 #define mozilla_BasePrincipal_h
 
 #include "nsIPrincipal.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsJSPrincipals.h"
 
+class nsIObjectOutputStream;
+class nsIObjectInputStream;
+
 namespace mozilla {
 
 /*
  * Base class from which all nsIPrincipal implementations inherit. Use this for
  * default implementations and other commonalities between principal
  * implementations.
  *
  * We should merge nsJSPrincipals into this class at some point.
@@ -41,31 +44,37 @@ public:
   NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) final;
   NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) final;
 
   virtual bool IsOnCSSUnprefixingWhitelist() override { return false; }
 
   static BasePrincipal* Cast(nsIPrincipal* aPrin) { return static_cast<BasePrincipal*>(aPrin); }
 
   struct OriginAttributes {
+    // NB: If you add any members here, you need to update Serialize/Deserialize
+    // and bump the CIDs of all the principal implementations that invoke those
+    // methods.
     uint32_t mAppId;
     bool mIsInBrowserElement;
 
     OriginAttributes() : mAppId(nsIScriptSecurityManager::NO_APP_ID), mIsInBrowserElement(false) {}
     OriginAttributes(uint32_t aAppId, bool aIsInBrowserElement)
       : mAppId(aAppId), mIsInBrowserElement(aIsInBrowserElement) {}
     bool operator==(const OriginAttributes& aOther) const
     {
       return mAppId == aOther.mAppId &&
              mIsInBrowserElement == aOther.mIsInBrowserElement;
     }
     bool operator!=(const OriginAttributes& aOther) const
     {
       return !(*this == aOther);
     }
+
+    void Serialize(nsIObjectOutputStream* aStream) const;
+    nsresult Deserialize(nsIObjectInputStream* aStream);
   };
 
   const OriginAttributes& OriginAttributesRef() { return mOriginAttributes; }
   uint32_t AppId() const { return mOriginAttributes.mAppId; }
   bool IsInBrowserElement() const { return mOriginAttributes.mIsInBrowserElement; }
 
 protected:
   virtual ~BasePrincipal() {}
--- a/caps/nsNullPrincipal.cpp
+++ b/caps/nsNullPrincipal.cpp
@@ -12,18 +12,16 @@
 
 #include "mozilla/ArrayUtils.h"
 
 #include "nsNullPrincipal.h"
 #include "nsNullPrincipalURI.h"
 #include "nsMemory.h"
 #include "nsNetUtil.h"
 #include "nsIClassInfoImpl.h"
-#include "nsIObjectInputStream.h"
-#include "nsIObjectOutputStream.h"
 #include "nsNetCID.h"
 #include "nsError.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsPrincipal.h"
 #include "nsScriptSecurityManager.h"
 #include "pratom.h"
 
 using namespace mozilla;
@@ -157,25 +155,18 @@ nsNullPrincipal::GetBaseDomain(nsACStrin
  */
 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.
   // This is in contrast to nsPrincipal, which uses NS_GENERIC_FACTORY_CONSTRUCTOR,
   // in which case ::Read needs to invoke Init().
-  nsresult rv = aStream->Read32(&mOriginAttributes.mAppId);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = aStream->ReadBoolean(&mOriginAttributes.mIsInBrowserElement);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
+  return mOriginAttributes.Deserialize(aStream);
 }
 
 NS_IMETHODIMP
 nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
 {
-  aStream->Write32(AppId());
-  aStream->WriteBoolean(IsInBrowserElement());
+  OriginAttributesRef().Serialize(aStream);
   return NS_OK;
 }
 
--- a/caps/nsPrincipal.cpp
+++ b/caps/nsPrincipal.cpp
@@ -10,18 +10,16 @@
 #include "nscore.h"
 #include "nsScriptSecurityManager.h"
 #include "nsString.h"
 #include "nsReadableUtils.h"
 #include "pratom.h"
 #include "nsIURI.h"
 #include "nsJSPrincipals.h"
 #include "nsIEffectiveTLDService.h"
-#include "nsIObjectInputStream.h"
-#include "nsIObjectOutputStream.h"
 #include "nsIClassInfoImpl.h"
 #include "nsIProtocolHandler.h"
 #include "nsError.h"
 #include "nsIContentSecurityPolicy.h"
 #include "nsNetCID.h"
 #include "jswrapper.h"
 
 #include "mozilla/dom/ScriptSettings.h"
@@ -357,31 +355,26 @@ nsPrincipal::Read(nsIObjectInputStream* 
   nsCOMPtr<nsIURI> domain;
   rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   domain = do_QueryInterface(supports);
 
-  uint32_t appId;
-  rv = aStream->Read32(&appId);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool inMozBrowser;
-  rv = aStream->ReadBoolean(&inMozBrowser);
+  OriginAttributes attrs;
+  rv = attrs.Deserialize(aStream);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // This may be null.
   nsCOMPtr<nsIContentSecurityPolicy> csp = do_QueryInterface(supports, &rv);
 
-  OriginAttributes attrs(appId, inMozBrowser);
   rv = Init(codebase, attrs);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = SetCsp(csp);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // need to link in the CSP context here (link in the URI of the protected
   // resource).
@@ -406,18 +399,17 @@ nsPrincipal::Write(nsIObjectOutputStream
   }
 
   rv = NS_WriteOptionalCompoundObject(aStream, mDomain, NS_GET_IID(nsIURI),
                                       true);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
-  aStream->Write32(AppId());
-  aStream->WriteBoolean(IsInBrowserElement());
+  OriginAttributesRef().Serialize(aStream);
 
   rv = NS_WriteOptionalCompoundObject(aStream, mCSP,
                                       NS_GET_IID(nsIContentSecurityPolicy),
                                       true);
   if (NS_FAILED(rv)) {
     return rv;
   }