Bug 1496034 - Improve HTMLIFrameElement::GetFeaturePolicyDefaultOrigin, r=bz
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 12 Oct 2018 09:49:51 +0200
changeset 496597 a6fe44fb6369e1230322a6a1d9d03064e568b7a5
parent 496596 fa33c65b62229b11f418a9f9850f01c59b650d2f
child 496598 57e52347a69b17244922a9a2b60be27db3279689
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1496034
milestone64.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 1496034 - Improve HTMLIFrameElement::GetFeaturePolicyDefaultOrigin, r=bz
dom/html/HTMLIFrameElement.cpp
dom/html/HTMLIFrameElement.h
--- a/dom/html/HTMLIFrameElement.cpp
+++ b/dom/html/HTMLIFrameElement.cpp
@@ -248,56 +248,49 @@ HTMLIFrameElement::WrapNode(JSContext* a
 }
 
 FeaturePolicy*
 HTMLIFrameElement::Policy() const
 {
   return mFeaturePolicy;
 }
 
-nsresult
-HTMLIFrameElement::GetFeaturePolicyDefaultOrigin(nsIPrincipal** aPrincipal) const
+already_AddRefed<nsIPrincipal>
+HTMLIFrameElement::GetFeaturePolicyDefaultOrigin() const
 {
   nsCOMPtr<nsIPrincipal> principal;
 
   if (HasAttr(kNameSpaceID_None, nsGkAtoms::srcdoc)) {
     principal = NodePrincipal();
+    return principal.forget();
   }
 
-  if (!principal) {
-    nsCOMPtr<nsIURI> nodeURI;
-    if (GetURIAttr(nsGkAtoms::src, nullptr, getter_AddRefs(nodeURI)) &&
-        nodeURI) {
-      principal =
-        BasePrincipal::CreateCodebasePrincipal(nodeURI,
-                                               BasePrincipal::Cast(NodePrincipal())->OriginAttributesRef());
-    }
+  nsCOMPtr<nsIURI> nodeURI;
+  if (GetURIAttr(nsGkAtoms::src, nullptr, getter_AddRefs(nodeURI)) &&
+      nodeURI) {
+    principal =
+      BasePrincipal::CreateCodebasePrincipal(nodeURI,
+                                             BasePrincipal::Cast(NodePrincipal())->OriginAttributesRef());
   }
 
   if (!principal) {
     principal = NodePrincipal();
   }
 
-  principal.forget(aPrincipal);
-  return NS_OK;
+  return principal.forget();
 }
 
 void
 HTMLIFrameElement::RefreshFeaturePolicy()
 {
   MOZ_ASSERT(StaticPrefs::dom_security_featurePolicy_enabled());
   mFeaturePolicy->ResetDeclaredPolicy();
 
-  // The origin can change if 'src' attribute changes.
-  nsCOMPtr<nsIPrincipal> origin;
-  nsresult rv = GetFeaturePolicyDefaultOrigin(getter_AddRefs(origin));
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return;
-  }
-
+  // The origin can change if 'src' and 'srcdoc' attributes change.
+  nsCOMPtr<nsIPrincipal> origin = GetFeaturePolicyDefaultOrigin();
   MOZ_ASSERT(origin);
   mFeaturePolicy->SetDefaultOrigin(origin);
 
   nsAutoString allow;
   GetAttr(nsGkAtoms::allow, allow);
 
   if (!allow.IsEmpty()) {
     // Set or reset the FeaturePolicy directives.
--- a/dom/html/HTMLIFrameElement.h
+++ b/dom/html/HTMLIFrameElement.h
@@ -220,23 +220,22 @@ protected:
 private:
   static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
                                     MappedDeclarations&);
 
   static const DOMTokenListSupportedToken sSupportedSandboxTokens[];
 
   void RefreshFeaturePolicy();
 
-  // Implements the declared-origin algorithm as described in Feature-Policy
-  // spec: https://wicg.github.io/feature-policy/#declared-origin
-  // If this iframe has a 'src' attribute, the origin will be the parsing of its
-  // value as URL. If the URL is invalid, or 'src' attribute doesn't exist, the
-  // origin will be the document's origin.
-  nsresult
-  GetFeaturePolicyDefaultOrigin(nsIPrincipal** aDefaultOrigin) const;
+  // If this iframe has a 'srcdoc' attribute, the document's origin will be
+  // returned. Otherwise, if this iframe has a 'src' attribute, the origin will
+  // be the parsing of its value as URL. If the URL is invalid, or 'src'
+  // attribute doesn't exist, the origin will be the document's origin.
+  already_AddRefed<nsIPrincipal>
+  GetFeaturePolicyDefaultOrigin() const;
 
   /**
    * This function is called by AfterSetAttr and OnAttrSetButNotChanged.
    * This function will be called by AfterSetAttr whether the attribute is being
    * set or unset.
    *
    * @param aNamespaceID the namespace of the attr being set
    * @param aName the localname of the attribute being set