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 Singleton
<IOSSerializedNavigationDriver
,
29 LeakySingletonTraits
<IOSSerializedNavigationDriver
>>::get();
32 IOSSerializedNavigationDriver::IOSSerializedNavigationDriver() {
35 IOSSerializedNavigationDriver::~IOSSerializedNavigationDriver() {
38 int IOSSerializedNavigationDriver::GetDefaultReferrerPolicy() const {
39 return web::ReferrerPolicyDefault
;
42 bool IOSSerializedNavigationDriver::MapReferrerPolicyToOldValues(
44 int* mapped_referrer_policy
) const {
45 switch (referrer_policy
) {
46 case web::ReferrerPolicyAlways
:
47 case web::ReferrerPolicyDefault
:
48 // "always" and "default" are the same value in all versions.
49 *mapped_referrer_policy
= referrer_policy
;
52 case web::ReferrerPolicyOrigin
:
53 // "origin" exists in the old encoding.
54 *mapped_referrer_policy
= kObsoleteReferrerPolicyOrigin
;
58 // Everything else is mapped to never.
59 *mapped_referrer_policy
= kObsoleteReferrerPolicyNever
;
64 bool IOSSerializedNavigationDriver::MapReferrerPolicyToNewValues(
66 int* mapped_referrer_policy
) const {
67 switch (referrer_policy
) {
68 case kObsoleteReferrerPolicyAlways
:
69 case kObsoleteReferrerPolicyDefault
:
70 // "always" and "default" are the same value in all versions.
71 *mapped_referrer_policy
= referrer_policy
;
75 // Since we don't know what encoding was used, we map the rest to "never".
76 *mapped_referrer_policy
= web::ReferrerPolicyNever
;
82 IOSSerializedNavigationDriver::GetSanitizedPageStateForPickle(
83 const SerializedNavigationEntry
* navigation
) const {
87 void IOSSerializedNavigationDriver::Sanitize(
88 SerializedNavigationEntry
* navigation
) const {
89 web::Referrer
referrer(
90 navigation
->referrer_url_
,
91 static_cast<web::ReferrerPolicy
>(navigation
->referrer_policy_
));
93 if (!navigation
->virtual_url_
.SchemeIsHTTPOrHTTPS() ||
94 !referrer
.url
.SchemeIsHTTPOrHTTPS()) {
95 referrer
.url
= GURL();
97 if (referrer
.policy
< 0 || referrer
.policy
> web::ReferrerPolicyLast
) {
99 referrer
.policy
= web::ReferrerPolicyNever
;
101 bool is_downgrade
= referrer
.url
.SchemeIsCryptographic() &&
102 !navigation
->virtual_url_
.SchemeIsCryptographic();
103 switch (referrer
.policy
) {
104 case web::ReferrerPolicyDefault
:
106 referrer
.url
= GURL();
108 case web::ReferrerPolicyNoReferrerWhenDowngrade
:
110 referrer
.url
= GURL();
111 case web::ReferrerPolicyAlways
:
113 case web::ReferrerPolicyNever
:
114 referrer
.url
= GURL();
116 case web::ReferrerPolicyOrigin
:
117 referrer
.url
= referrer
.url
.GetOrigin();
119 case web::ReferrerPolicyOriginWhenCrossOrigin
:
120 if (navigation
->virtual_url_
.GetOrigin() != referrer
.url
.GetOrigin())
121 referrer
.url
= referrer
.url
.GetOrigin();
126 // Reset the referrer if it has changed.
127 if (navigation
->referrer_url_
!= referrer
.url
) {
128 navigation
->referrer_url_
= GURL();
129 navigation
->referrer_policy_
= GetDefaultReferrerPolicy();
133 std::string
IOSSerializedNavigationDriver::StripReferrerFromPageState(
134 const std::string
& page_state
) const {
135 return std::string();
138 } // namespace sessions