1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "content/public/common/content_switches.h"
7 #include "content/public/common/referrer.h"
12 Referrer
Referrer::SanitizeForRequest(const GURL
& request
,
13 const Referrer
& referrer
) {
14 Referrer
sanitized_referrer(referrer
.url
.GetAsReferrer(), referrer
.policy
);
16 if (!request
.SchemeIsHTTPOrHTTPS() ||
17 !sanitized_referrer
.url
.SchemeIsHTTPOrHTTPS()) {
18 sanitized_referrer
.url
= GURL();
19 return sanitized_referrer
;
22 bool is_downgrade
= sanitized_referrer
.url
.SchemeIsCryptographic() &&
23 !request
.SchemeIsCryptographic();
25 if (sanitized_referrer
.policy
< 0 ||
26 sanitized_referrer
.policy
> blink::WebReferrerPolicyLast
) {
28 sanitized_referrer
.policy
= blink::WebReferrerPolicyNever
;
31 switch (sanitized_referrer
.policy
) {
32 case blink::WebReferrerPolicyDefault
:
34 sanitized_referrer
.url
= GURL();
35 } else if (request
.GetOrigin() != sanitized_referrer
.url
.GetOrigin() &&
36 base::CommandLine::ForCurrentProcess()->HasSwitch(
37 switches::kReducedReferrerGranularity
)) {
38 sanitized_referrer
.url
= sanitized_referrer
.url
.GetOrigin();
41 case blink::WebReferrerPolicyNoReferrerWhenDowngrade
:
43 sanitized_referrer
.url
= GURL();
45 case blink::WebReferrerPolicyAlways
:
47 case blink::WebReferrerPolicyNever
:
48 sanitized_referrer
.url
= GURL();
50 case blink::WebReferrerPolicyOrigin
:
51 sanitized_referrer
.url
= sanitized_referrer
.url
.GetOrigin();
53 case blink::WebReferrerPolicyOriginWhenCrossOrigin
:
54 if (request
.GetOrigin() != sanitized_referrer
.url
.GetOrigin())
55 sanitized_referrer
.url
= sanitized_referrer
.url
.GetOrigin();
58 return sanitized_referrer
;
61 } // namespace content