Bug 1297687 - Part 2: Allow specifying an OriginAttribute when creating an expanded principal; r=bholley a=ritu
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 09 Sep 2016 18:52:55 -0400
changeset 348381 06dd44643f5328db2ff949fe35fd20b0b04caaac
parent 348380 023ebfa3cf3dcc9d454327479a1e86b87f43d13d
child 348382 9431fe63e758b2beeee367c82b620be4d9a4fb7e
push id6434
push userkwierso@gmail.com
push dateWed, 28 Sep 2016 21:09:34 +0000
treeherdermozilla-beta@5237c35095f4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley, ritu
bugs1297687
milestone50.0
Bug 1297687 - Part 2: Allow specifying an OriginAttribute when creating an expanded principal; r=bholley a=ritu
caps/nsPrincipal.cpp
caps/nsPrincipal.h
ipc/glue/BackgroundUtils.cpp
ipc/glue/PBackgroundSharedTypes.ipdlh
--- a/caps/nsPrincipal.cpp
+++ b/caps/nsPrincipal.cpp
@@ -669,26 +669,33 @@ struct OriginComparator
     NS_ENSURE_SUCCESS(rv, false);
     nsAutoCString originB;
     rv = b->GetOrigin(originB);
     NS_ENSURE_SUCCESS(rv, false);
     return a == b;
   }
 };
 
-nsExpandedPrincipal::nsExpandedPrincipal(nsTArray<nsCOMPtr <nsIPrincipal> > &aWhiteList)
+nsExpandedPrincipal::nsExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteList)
 {
   // We force the principals to be sorted by origin so that nsExpandedPrincipal
   // origins can have a canonical form.
   OriginComparator c;
   for (size_t i = 0; i < aWhiteList.Length(); ++i) {
     mPrincipals.InsertElementSorted(aWhiteList[i], c);
   }
 }
 
+nsExpandedPrincipal::nsExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteList,
+                                         const PrincipalOriginAttributes& aAttrs)
+  : nsExpandedPrincipal(aWhiteList)
+{
+  mOriginAttributes = aAttrs;
+}
+
 nsExpandedPrincipal::~nsExpandedPrincipal()
 { }
 
 NS_IMETHODIMP
 nsExpandedPrincipal::GetDomain(nsIURI** aDomain)
 {
   *aDomain = nullptr;
   return NS_OK;
--- a/caps/nsPrincipal.h
+++ b/caps/nsPrincipal.h
@@ -62,17 +62,19 @@ protected:
 
   bool SubsumesInternal(nsIPrincipal* aOther, DocumentDomainConsideration aConsideration) override;
   bool MayLoadInternal(nsIURI* aURI) override;
 };
 
 class nsExpandedPrincipal : public nsIExpandedPrincipal, public mozilla::BasePrincipal
 {
 public:
-  explicit nsExpandedPrincipal(nsTArray< nsCOMPtr<nsIPrincipal> > &aWhiteList);
+  nsExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteList,
+                      const mozilla::PrincipalOriginAttributes& aAttrs);
+  explicit nsExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteList);
 
   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 GetHashValue(uint32_t* aHashValue) override;
   NS_IMETHOD GetURI(nsIURI** aURI) override;
--- a/ipc/glue/BackgroundUtils.cpp
+++ b/ipc/glue/BackgroundUtils.cpp
@@ -88,29 +88,29 @@ PrincipalInfoToPrincipal(const Principal
       }
 
       return principal.forget();
     }
 
     case PrincipalInfo::TExpandedPrincipalInfo: {
       const ExpandedPrincipalInfo& info = aPrincipalInfo.get_ExpandedPrincipalInfo();
 
-      nsTArray< nsCOMPtr<nsIPrincipal> > whitelist;
+      nsTArray<nsCOMPtr<nsIPrincipal>> whitelist;
       nsCOMPtr<nsIPrincipal> wlPrincipal;
 
       for (uint32_t i = 0; i < info.whitelist().Length(); i++) {
         wlPrincipal = PrincipalInfoToPrincipal(info.whitelist()[i], &rv);
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return nullptr;
         }
         // append that principal to the whitelist
         whitelist.AppendElement(wlPrincipal);
       }
 
-      RefPtr<nsExpandedPrincipal> expandedPrincipal = new nsExpandedPrincipal(whitelist);
+      RefPtr<nsExpandedPrincipal> expandedPrincipal = new nsExpandedPrincipal(whitelist, info.attrs());
       if (!expandedPrincipal) {
         NS_WARNING("could not instantiate expanded principal");
         return nullptr;
       }
 
       principal = expandedPrincipal;
       return principal.forget();
     }
@@ -173,17 +173,19 @@ PrincipalToPrincipalInfo(nsIPrincipal* a
       rv = PrincipalToPrincipalInfo((*whitelist)[i], &info);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       // append that spec to the whitelist
       whitelistInfo.AppendElement(info);
     }
 
-    *aPrincipalInfo = ExpandedPrincipalInfo(Move(whitelistInfo));
+    *aPrincipalInfo =
+      ExpandedPrincipalInfo(BasePrincipal::Cast(aPrincipal)->OriginAttributesRef(),
+                            Move(whitelistInfo));
     return NS_OK;
   }
 
   // must be a content principal
 
   nsCOMPtr<nsIURI> uri;
   rv = aPrincipal->GetURI(getter_AddRefs(uri));
   if (NS_WARN_IF(NS_FAILED(rv))) {
--- a/ipc/glue/PBackgroundSharedTypes.ipdlh
+++ b/ipc/glue/PBackgroundSharedTypes.ipdlh
@@ -19,16 +19,17 @@ struct SystemPrincipalInfo
 
 struct NullPrincipalInfo
 {
   PrincipalOriginAttributes attrs;
 };
 
 struct ExpandedPrincipalInfo
 {
+  PrincipalOriginAttributes attrs;
   PrincipalInfo[] whitelist;
 };
 
 union PrincipalInfo
 {
   ContentPrincipalInfo;
   SystemPrincipalInfo;
   NullPrincipalInfo;