Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifndef ReferrerPolicy_h__
6
#define ReferrerPolicy_h__
7
8
#include "nsString.h"
9
#include "nsIHttpChannel.h"
10
#include "nsUnicharUtils.h"
11
12
namespace mozilla {
13
namespace net {
14
15
enum ReferrerPolicy {
16
/* spec tokens: never no-referrer */
17
RP_No_Referrer = nsIHttpChannel::REFERRER_POLICY_NO_REFERRER,
18
19
/* spec tokens: origin */
20
RP_Origin = nsIHttpChannel::REFERRER_POLICY_ORIGIN,
21
22
/* spec tokens: default no-referrer-when-downgrade */
23
RP_No_Referrer_When_Downgrade =
24
nsIHttpChannel::REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE,
25
26
/* spec tokens: origin-when-cross-origin */
27
RP_Origin_When_Crossorigin =
28
nsIHttpChannel::REFERRER_POLICY_ORIGIN_WHEN_XORIGIN,
29
30
/* spec tokens: always unsafe-url */
31
RP_Unsafe_URL = nsIHttpChannel::REFERRER_POLICY_UNSAFE_URL,
32
33
/* spec tokens: same-origin */
34
RP_Same_Origin = nsIHttpChannel::REFERRER_POLICY_SAME_ORIGIN,
35
36
/* spec tokens: strict-origin */
37
RP_Strict_Origin = nsIHttpChannel::REFERRER_POLICY_STRICT_ORIGIN,
38
39
/* spec tokens: strict-origin-when-cross-origin */
40
RP_Strict_Origin_When_Cross_Origin =
41
nsIHttpChannel::REFERRER_POLICY_STRICT_ORIGIN_WHEN_XORIGIN,
42
43
/* spec tokens: empty string */
44
/* The empty string "" corresponds to no referrer policy, or unset policy */
45
RP_Unset = nsIHttpChannel::REFERRER_POLICY_UNSET,
46
};
47
48
// Referrer Policy spec tokens. Order matters here, make sure it matches the
49
// order as in nsIHttpChannel.idl
50
static const char* kReferrerPolicyString[] = {
51
"", // REFERRER_POLICY_UNSET
52
"no-referrer-when-downgrade", // REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE
53
"no-referrer", // REFERRER_POLICY_NO_REFERRER
54
"origin", // REFERRER_POLICY_ORIGIN
55
"origin-when-cross-origin", // REFERRER_POLICY_ORIGIN_WHEN_XORIGIN
56
"unsafe-url", // REFERRER_POLICY_UNSAFE_URL
57
"same-origin", // REFERRER_POLICY_SAME_ORIGIN
58
"strict-origin", // REFERRER_POLICY_STRICT_ORIGIN
59
"strict-origin-when-cross-origin" // REFERRER_POLICY_STRICT_ORIGIN_WHEN_XORIGIN
60
};
61
62
/* spec tokens: never */
63
const char kRPS_Never[] = "never";
64
65
/* spec tokens: default */
66
const char kRPS_Default[] = "default";
67
68
/* spec tokens: origin-when-crossorigin */
69
const char kRPS_Origin_When_Crossorigin[] = "origin-when-crossorigin";
70
71
/* spec tokens: always */
72
const char kRPS_Always[] = "always";
73
74
inline ReferrerPolicy ReferrerPolicyFromString(const nsAString& content) {
75
if (content.IsEmpty()) {
76
return RP_Unset;
77
}
78
79
nsString lowerContent(content);
80
ToLowerCase(lowerContent);
81
82
if (lowerContent.EqualsLiteral(kRPS_Never)) {
83
return RP_No_Referrer;
84
}
85
if (lowerContent.EqualsLiteral(kRPS_Default)) {
86
return RP_No_Referrer_When_Downgrade;
87
}
88
if (lowerContent.EqualsLiteral(kRPS_Origin_When_Crossorigin)) {
89
return RP_Origin_When_Crossorigin;
90
}
91
if (lowerContent.EqualsLiteral(kRPS_Always)) {
92
return RP_Unsafe_URL;
93
}
94
// This is implemented step by step as described in the Referrer Policy
95
// specification, section "Determine token's Policy".
96
97
uint16_t numStr =
98
(sizeof(kReferrerPolicyString) / sizeof(kReferrerPolicyString[0]));
99
for (uint16_t i = 0; i < numStr; i++) {
100
if (lowerContent.EqualsASCII(kReferrerPolicyString[i])) {
101
return static_cast<ReferrerPolicy>(i);
102
}
103
}
104
105
// Spec says if none of the previous match, use empty string.
106
return RP_Unset;
107
}
108
109
inline ReferrerPolicy AttributeReferrerPolicyFromString(
110
const nsAString& content) {
111
// Specs :
113
// Spec says the empty string "" corresponds to no referrer policy, or
114
// RP_Unset
115
if (content.IsEmpty()) {
116
return RP_Unset;
117
}
118
119
nsString lowerContent(content);
120
ToLowerCase(lowerContent);
121
122
uint16_t numStr =
123
(sizeof(kReferrerPolicyString) / sizeof(kReferrerPolicyString[0]));
124
for (uint16_t i = 0; i < numStr; i++) {
125
if (lowerContent.EqualsASCII(kReferrerPolicyString[i])) {
126
return static_cast<ReferrerPolicy>(i);
127
}
128
}
129
130
// Spec says invalid value default is empty string state
131
// So, return RP_Unset if none of the previous match, return RP_Unset
132
return RP_Unset;
133
}
134
135
inline const char* ReferrerPolicyToString(ReferrerPolicy aPolicy) {
136
return kReferrerPolicyString[static_cast<uint32_t>(aPolicy)];
137
}
138
139
} // namespace net
140
} // namespace mozilla
141
142
#endif