Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / public / common / referrer.cc
blob183c950673fa095f686c80d0d83629a7890fd8e7
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"
9 namespace content {
11 // static.
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) {
27 NOTREACHED();
28 sanitized_referrer.policy = blink::WebReferrerPolicyNever;
31 switch (sanitized_referrer.policy) {
32 case blink::WebReferrerPolicyDefault:
33 if (is_downgrade) {
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();
40 break;
41 case blink::WebReferrerPolicyNoReferrerWhenDowngrade:
42 if (is_downgrade)
43 sanitized_referrer.url = GURL();
44 break;
45 case blink::WebReferrerPolicyAlways:
46 break;
47 case blink::WebReferrerPolicyNever:
48 sanitized_referrer.url = GURL();
49 break;
50 case blink::WebReferrerPolicyOrigin:
51 sanitized_referrer.url = sanitized_referrer.url.GetOrigin();
52 break;
53 case blink::WebReferrerPolicyOriginWhenCrossOrigin:
54 if (request.GetOrigin() != sanitized_referrer.url.GetOrigin())
55 sanitized_referrer.url = sanitized_referrer.url.GetOrigin();
56 break;
58 return sanitized_referrer;
61 } // namespace content