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 "components/sessions/ios/ios_serialized_navigation_driver.h"
7 #include "base/memory/singleton.h"
8 #include "components/sessions/serialized_navigation_entry.h"
9 #include "ios/web/public/referrer.h"
14 const int kObsoleteReferrerPolicyAlways
= 0;
15 const int kObsoleteReferrerPolicyDefault
= 1;
16 const int kObsoleteReferrerPolicyNever
= 2;
17 const int kObsoleteReferrerPolicyOrigin
= 3;
21 SerializedNavigationDriver
* SerializedNavigationDriver::Get() {
22 return IOSSerializedNavigationDriver::GetInstance();
26 IOSSerializedNavigationDriver
*
27 IOSSerializedNavigationDriver::GetInstance() {
28 return base::Singleton
<
29 IOSSerializedNavigationDriver
,
30 base::LeakySingletonTraits
<IOSSerializedNavigationDriver
>>::get();
33 IOSSerializedNavigationDriver::IOSSerializedNavigationDriver() {
36 IOSSerializedNavigationDriver::~IOSSerializedNavigationDriver() {
39 int IOSSerializedNavigationDriver::GetDefaultReferrerPolicy() const {
40 return web::ReferrerPolicyDefault
;
43 bool IOSSerializedNavigationDriver::MapReferrerPolicyToOldValues(
45 int* mapped_referrer_policy
) const {
46 switch (referrer_policy
) {
47 case web::ReferrerPolicyAlways
:
48 case web::ReferrerPolicyDefault
:
49 // "always" and "default" are the same value in all versions.
50 *mapped_referrer_policy
= referrer_policy
;
53 case web::ReferrerPolicyOrigin
:
54 // "origin" exists in the old encoding.
55 *mapped_referrer_policy
= kObsoleteReferrerPolicyOrigin
;
59 // Everything else is mapped to never.
60 *mapped_referrer_policy
= kObsoleteReferrerPolicyNever
;
65 bool IOSSerializedNavigationDriver::MapReferrerPolicyToNewValues(
67 int* mapped_referrer_policy
) const {
68 switch (referrer_policy
) {
69 case kObsoleteReferrerPolicyAlways
:
70 case kObsoleteReferrerPolicyDefault
:
71 // "always" and "default" are the same value in all versions.
72 *mapped_referrer_policy
= referrer_policy
;
76 // Since we don't know what encoding was used, we map the rest to "never".
77 *mapped_referrer_policy
= web::ReferrerPolicyNever
;
83 IOSSerializedNavigationDriver::GetSanitizedPageStateForPickle(
84 const SerializedNavigationEntry
* navigation
) const {
88 void IOSSerializedNavigationDriver::Sanitize(
89 SerializedNavigationEntry
* navigation
) const {
90 web::Referrer
referrer(
91 navigation
->referrer_url_
,
92 static_cast<web::ReferrerPolicy
>(navigation
->referrer_policy_
));
94 if (!navigation
->virtual_url_
.SchemeIsHTTPOrHTTPS() ||
95 !referrer
.url
.SchemeIsHTTPOrHTTPS()) {
96 referrer
.url
= GURL();
98 if (referrer
.policy
< 0 || referrer
.policy
> web::ReferrerPolicyLast
) {
100 referrer
.policy
= web::ReferrerPolicyNever
;
102 bool is_downgrade
= referrer
.url
.SchemeIsCryptographic() &&
103 !navigation
->virtual_url_
.SchemeIsCryptographic();
104 switch (referrer
.policy
) {
105 case web::ReferrerPolicyDefault
:
107 referrer
.url
= GURL();
109 case web::ReferrerPolicyNoReferrerWhenDowngrade
:
111 referrer
.url
= GURL();
112 case web::ReferrerPolicyAlways
:
114 case web::ReferrerPolicyNever
:
115 referrer
.url
= GURL();
117 case web::ReferrerPolicyOrigin
:
118 referrer
.url
= referrer
.url
.GetOrigin();
120 case web::ReferrerPolicyOriginWhenCrossOrigin
:
121 if (navigation
->virtual_url_
.GetOrigin() != referrer
.url
.GetOrigin())
122 referrer
.url
= referrer
.url
.GetOrigin();
127 // Reset the referrer if it has changed.
128 if (navigation
->referrer_url_
!= referrer
.url
) {
129 navigation
->referrer_url_
= GURL();
130 navigation
->referrer_policy_
= GetDefaultReferrerPolicy();
134 std::string
IOSSerializedNavigationDriver::StripReferrerFromPageState(
135 const std::string
& page_state
) const {
136 return std::string();
139 } // namespace sessions