DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (4a108e94d3e2)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/* 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/. */

#ifndef ReferrerPolicy_h__
#define ReferrerPolicy_h__

#include "nsStringGlue.h"
#include "nsIHttpChannel.h"

namespace mozilla { namespace net {

enum ReferrerPolicy {
  /* spec tokens: never no-referrer */
  RP_No_Referrer                 = nsIHttpChannel::REFERRER_POLICY_NO_REFERRER,

  /* spec tokens: origin */
  RP_Origin                      = nsIHttpChannel::REFERRER_POLICY_ORIGIN,

  /* spec tokens: default no-referrer-when-downgrade */
  RP_No_Referrer_When_Downgrade  = nsIHttpChannel::REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE,
  RP_Default                     = nsIHttpChannel::REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE,

  /* spec tokens: origin-when-crossorigin */
  RP_Origin_When_Crossorigin     = nsIHttpChannel::REFERRER_POLICY_ORIGIN_WHEN_XORIGIN,

  /* spec tokens: always unsafe-url */
  RP_Unsafe_URL                  = nsIHttpChannel::REFERRER_POLICY_UNSAFE_URL
};

inline ReferrerPolicy
ReferrerPolicyFromString(const nsAString& content)
{
  // This is implemented step by step as described in the Referrer Policy
  // specification, section 6.4 "Determine token's Policy".
  if (content.LowerCaseEqualsLiteral("never") ||
      content.LowerCaseEqualsLiteral("no-referrer")) {
    return RP_No_Referrer;
  }
  if (content.LowerCaseEqualsLiteral("origin")) {
    return RP_Origin;
  }
  if (content.LowerCaseEqualsLiteral("default") ||
      content.LowerCaseEqualsLiteral("no-referrer-when-downgrade")) {
    return RP_No_Referrer_When_Downgrade;
  }
  if (content.LowerCaseEqualsLiteral("origin-when-crossorigin")) {
    return RP_Origin_When_Crossorigin;
  }
  if (content.LowerCaseEqualsLiteral("always") ||
      content.LowerCaseEqualsLiteral("unsafe-url")) {
    return RP_Unsafe_URL;
  }
  // Spec says if none of the previous match, use No_Referrer.
  return RP_No_Referrer;

}

inline bool
IsValidReferrerPolicy(const nsAString& content)
{
  return content.LowerCaseEqualsLiteral("never")
      || content.LowerCaseEqualsLiteral("no-referrer")
      || content.LowerCaseEqualsLiteral("origin")
      || content.LowerCaseEqualsLiteral("default")
      || content.LowerCaseEqualsLiteral("no-referrer-when-downgrade")
      || content.LowerCaseEqualsLiteral("origin-when-crossorigin")
      || content.LowerCaseEqualsLiteral("always")
      || content.LowerCaseEqualsLiteral("unsafe-url");
}

} } //namespace mozilla::net

#endif