1 // Copyright 2013 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 "ios/web/public/referrer_util.h"
7 #include "base/logging.h"
8 #include "ios/web/public/referrer.h"
13 GURL
ReferrerForHeader(const GURL
& referrer
) {
14 DCHECK(referrer
.is_valid());
15 GURL::Replacements replacements
;
16 replacements
.ClearUsername();
17 replacements
.ClearPassword();
18 replacements
.ClearRef();
19 return referrer
.ReplaceComponents(replacements
);
22 std::string
ReferrerHeaderValueForNavigation(
23 const GURL
& destination
,
24 const web::Referrer
& referrer
) {
25 std::string referrer_value
;
26 bool leaving_secure_scheme
= referrer
.url
.SchemeIsCryptographic() &&
27 !destination
.SchemeIsCryptographic();
28 if (referrer
.policy
== ReferrerPolicyAlways
||
29 (referrer
.policy
== ReferrerPolicyDefault
&& !leaving_secure_scheme
)) {
30 if (referrer
.url
.is_valid())
31 referrer_value
= ReferrerForHeader(referrer
.url
).spec();
32 } else if (referrer
.policy
== ReferrerPolicyOrigin
) {
33 referrer_value
= referrer
.url
.GetOrigin().spec();
35 // Policy is Never, or it's Default with a secure->insecure transition, so
38 return referrer_value
;
41 net::URLRequest::ReferrerPolicy
PolicyForNavigation(
42 const GURL
& destination
,
43 const web::Referrer
& referrer
) {
44 net::URLRequest::ReferrerPolicy net_referrer_policy
=
45 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
;
46 if (referrer
.url
.is_valid()) {
47 GURL
referrer_url(ReferrerHeaderValueForNavigation(destination
, referrer
));
48 if (!referrer_url
.is_empty()) {
49 switch (referrer
.policy
) {
50 case ReferrerPolicyDefault
:
52 case ReferrerPolicyAlways
:
53 case ReferrerPolicyNever
:
54 case ReferrerPolicyOrigin
:
55 net_referrer_policy
= net::URLRequest::NEVER_CLEAR_REFERRER
;
62 return net_referrer_policy
;
65 ReferrerPolicy
ReferrerPolicyFromString(const std::string
& policy
) {
66 if (policy
== "never")
67 return ReferrerPolicyNever
;
68 if (policy
== "always")
69 return ReferrerPolicyAlways
;
70 if (policy
== "origin")
71 return ReferrerPolicyOrigin
;
72 return web::ReferrerPolicyDefault
;