Bug 1501206 - Introduce a new function ReferrerPolicyToString() r=ckerschb
authorThomas Nguyen <tnguyen@mozilla.com>
Thu, 25 Oct 2018 13:53:21 +0000
changeset 491338 219cc9ec62c81b76b210fe0fb2d3e14d6dff6c2a
parent 491337 0e96d60023213064e9dfef68c59979459248fd7a
child 491339 58bb389b133cea716345b7ba78beec04202321aa
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersckerschb
bugs1501206
milestone65.0a1
Bug 1501206 - Introduce a new function ReferrerPolicyToString() r=ckerschb We may need this function to convert ReferrerPolicy enum to string then we can display referrer policy applied to a request. MozReview-Commit-ID: B3xPAiykcOV Differential Revision: https://phabricator.services.mozilla.com/D9664
dom/html/nsGenericHTMLElement.cpp
netwerk/base/ReferrerPolicy.h
--- a/dom/html/nsGenericHTMLElement.cpp
+++ b/dom/html/nsGenericHTMLElement.cpp
@@ -1123,24 +1123,48 @@ nsGenericHTMLElement::ParseImageAttribut
   return false;
 }
 
 bool
 nsGenericHTMLElement::ParseReferrerAttribute(const nsAString& aString,
                                              nsAttrValue& aResult)
 {
   static const nsAttrValue::EnumTable kReferrerTable[] = {
-    { net::kRPS_No_Referrer, static_cast<int16_t>(net::RP_No_Referrer) },
-    { net::kRPS_Origin, static_cast<int16_t>(net::RP_Origin) },
-    { net::kRPS_Origin_When_Cross_Origin, static_cast<int16_t>(net::RP_Origin_When_Crossorigin) },
-    { net::kRPS_No_Referrer_When_Downgrade, static_cast<int16_t>(net::RP_No_Referrer_When_Downgrade) },
-    { net::kRPS_Unsafe_URL, static_cast<int16_t>(net::RP_Unsafe_URL) },
-    { net::kRPS_Strict_Origin, static_cast<int16_t>(net::RP_Strict_Origin) },
-    { net::kRPS_Same_Origin, static_cast<int16_t>(net::RP_Same_Origin) },
-    { net::kRPS_Strict_Origin_When_Cross_Origin, static_cast<int16_t>(net::RP_Strict_Origin_When_Cross_Origin) },
+    {
+      ReferrerPolicyToString(net::RP_No_Referrer),
+      static_cast<int16_t>(net::RP_No_Referrer)
+    },
+    {
+      ReferrerPolicyToString(net::RP_Origin),
+      static_cast<int16_t>(net::RP_Origin)
+    },
+    {
+      ReferrerPolicyToString(net::RP_Origin_When_Crossorigin),
+      static_cast<int16_t>(net::RP_Origin_When_Crossorigin)
+    },
+    {
+      ReferrerPolicyToString(net::RP_No_Referrer_When_Downgrade),
+      static_cast<int16_t>(net::RP_No_Referrer_When_Downgrade)
+    },
+    {
+      ReferrerPolicyToString(net::RP_Unsafe_URL),
+      static_cast<int16_t>(net::RP_Unsafe_URL)
+    },
+    {
+      ReferrerPolicyToString(net::RP_Strict_Origin),
+      static_cast<int16_t>(net::RP_Strict_Origin)
+    },
+    {
+      ReferrerPolicyToString(net::RP_Same_Origin),
+      static_cast<int16_t>(net::RP_Same_Origin)
+    },
+    {
+      ReferrerPolicyToString(net::RP_Strict_Origin_When_Cross_Origin),
+      static_cast<int16_t>(net::RP_Strict_Origin_When_Cross_Origin)
+    },
     { nullptr, 0 }
   };
   return aResult.ParseEnumValue(aString, kReferrerTable, false);
 }
 
 bool
 nsGenericHTMLElement::ParseFrameborderValue(const nsAString& aString,
                                             nsAttrValue& aResult)
--- a/netwerk/base/ReferrerPolicy.h
+++ b/netwerk/base/ReferrerPolicy.h
@@ -36,81 +36,72 @@ enum ReferrerPolicy {
   /* spec tokens: strict-origin-when-cross-origin */
   RP_Strict_Origin_When_Cross_Origin = nsIHttpChannel::REFERRER_POLICY_STRICT_ORIGIN_WHEN_XORIGIN,
 
   /* spec tokens: empty string */
   /* The empty string "" corresponds to no referrer policy, or unset policy */
   RP_Unset                       = nsIHttpChannel::REFERRER_POLICY_UNSET,
 };
 
-/* spec tokens: never no-referrer */
-const char kRPS_Never[]                       = "never";
-const char kRPS_No_Referrer[]                 = "no-referrer";
-
-/* spec tokens: origin */
-const char kRPS_Origin[]                      = "origin";
+// Referrer Policy spec tokens. Order matters here, make sure it matches the
+// order as in nsIHttpChannel.idl
+static const char* kReferrerPolicyString[] = {
+  "",                               // REFERRER_POLICY_UNSET
+  "no-referrer-when-downgrade",     // REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE
+  "no-referrer",                    // REFERRER_POLICY_NO_REFERRER
+  "origin",                         // REFERRER_POLICY_ORIGIN
+  "origin-when-cross-origin",       // REFERRER_POLICY_ORIGIN_WHEN_XORIGIN
+  "unsafe-url",                     // REFERRER_POLICY_UNSAFE_URL
+  "same-origin",                    // REFERRER_POLICY_SAME_ORIGIN
+  "strict-origin",                  // REFERRER_POLICY_STRICT_ORIGIN
+  "strict-origin-when-cross-origin" // REFERRER_POLICY_STRICT_ORIGIN_WHEN_XORIGIN
+};
 
-/* spec tokens: default no-referrer-when-downgrade */
+/* spec tokens: never */
+const char kRPS_Never[]                       = "never";
+
+/* spec tokens: default */
 const char kRPS_Default[]                     = "default";
-const char kRPS_No_Referrer_When_Downgrade[]  = "no-referrer-when-downgrade";
 
-/* spec tokens: origin-when-cross-origin */
-const char kRPS_Origin_When_Cross_Origin[]    = "origin-when-cross-origin";
+/* spec tokens: origin-when-crossorigin */
 const char kRPS_Origin_When_Crossorigin[]     = "origin-when-crossorigin";
 
-/* spec tokens: same-origin */
-const char kRPS_Same_Origin[]                 = "same-origin";
-
-/* spec tokens: strict-origin */
-const char kRPS_Strict_Origin[]               = "strict-origin";
-
-/* spec tokens: strict-origin-when-cross-origin */
-const char kRPS_Strict_Origin_When_Cross_Origin[] = "strict-origin-when-cross-origin";
-
-/* spec tokens: always unsafe-url */
+/* spec tokens: always */
 const char kRPS_Always[]                      = "always";
-const char kRPS_Unsafe_URL[]                  = "unsafe-url";
 
 inline ReferrerPolicy
 ReferrerPolicyFromString(const nsAString& content)
 {
   if (content.IsEmpty()) {
     return RP_No_Referrer;
   }
 
   nsString lowerContent(content);
   ToLowerCase(lowerContent);
   // This is implemented step by step as described in the Referrer Policy
   // specification, section "Determine token's Policy".
-  if (lowerContent.EqualsLiteral(kRPS_Never) ||
-      lowerContent.EqualsLiteral(kRPS_No_Referrer)) {
+
+  uint16_t numStr =
+    (sizeof(kReferrerPolicyString) / sizeof(kReferrerPolicyString[0]));
+  for (uint16_t i = 0; i < numStr; i++) {
+    if (lowerContent.EqualsASCII(kReferrerPolicyString[i])) {
+      return static_cast<ReferrerPolicy>(i);
+    }
+  }
+
+  if (lowerContent.EqualsLiteral(kRPS_Never)) {
     return RP_No_Referrer;
   }
-  if (lowerContent.EqualsLiteral(kRPS_Origin)) {
-    return RP_Origin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Default) ||
-      lowerContent.EqualsLiteral(kRPS_No_Referrer_When_Downgrade)) {
+  if (lowerContent.EqualsLiteral(kRPS_Default)) {
     return RP_No_Referrer_When_Downgrade;
   }
-  if (lowerContent.EqualsLiteral(kRPS_Origin_When_Cross_Origin) ||
-      lowerContent.EqualsLiteral(kRPS_Origin_When_Crossorigin)) {
+  if (lowerContent.EqualsLiteral(kRPS_Origin_When_Crossorigin)) {
     return RP_Origin_When_Crossorigin;
   }
-  if (lowerContent.EqualsLiteral(kRPS_Same_Origin)) {
-    return RP_Same_Origin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Strict_Origin)) {
-    return RP_Strict_Origin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Strict_Origin_When_Cross_Origin)) {
-    return RP_Strict_Origin_When_Cross_Origin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Always) ||
-      lowerContent.EqualsLiteral(kRPS_Unsafe_URL)) {
+  if (lowerContent.EqualsLiteral(kRPS_Always)) {
     return RP_Unsafe_URL;
   }
   // Spec says if none of the previous match, use empty string.
   return RP_Unset;
 
 }
 
 inline ReferrerPolicy
@@ -120,42 +111,31 @@ AttributeReferrerPolicyFromString(const 
   // Spec says the empty string "" corresponds to no referrer policy, or RP_Unset
   if (content.IsEmpty()) {
     return RP_Unset;
   }
 
   nsString lowerContent(content);
   ToLowerCase(lowerContent);
 
-  if (lowerContent.EqualsLiteral(kRPS_No_Referrer)) {
-    return RP_No_Referrer;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Origin)) {
-    return RP_Origin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_No_Referrer_When_Downgrade)) {
-    return RP_No_Referrer_When_Downgrade;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Origin_When_Cross_Origin)) {
-    return RP_Origin_When_Crossorigin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Unsafe_URL)) {
-    return RP_Unsafe_URL;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Strict_Origin)) {
-    return RP_Strict_Origin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Same_Origin)) {
-    return RP_Same_Origin;
-  }
-  if (lowerContent.EqualsLiteral(kRPS_Strict_Origin_When_Cross_Origin)) {
-    return RP_Strict_Origin_When_Cross_Origin;
+  uint16_t numStr =
+    (sizeof(kReferrerPolicyString) / sizeof(kReferrerPolicyString[0]));
+  for (uint16_t i = 0; i < numStr; i++) {
+    if (lowerContent.EqualsASCII(kReferrerPolicyString[i])) {
+      return static_cast<ReferrerPolicy>(i);
+    }
   }
 
   // Spec says invalid value default is empty string state
   // So, return RP_Unset if none of the previous match, return RP_Unset
   return RP_Unset;
 }
 
+inline const char*
+ReferrerPolicyToString(ReferrerPolicy aPolicy)
+{
+  return kReferrerPolicyString[static_cast<uint32_t>(aPolicy)];
+}
+
 } // namespace net
 } // namespace mozilla
 
 #endif