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/content/content_serialized_navigation_driver.h"
7 #include "base/memory/singleton.h"
8 #include "components/sessions/serialized_navigation_entry.h"
9 #include "content/public/common/page_state.h"
10 #include "content/public/common/referrer.h"
15 const int kObsoleteReferrerPolicyAlways
= 0;
16 const int kObsoleteReferrerPolicyDefault
= 1;
17 const int kObsoleteReferrerPolicyNever
= 2;
18 const int kObsoleteReferrerPolicyOrigin
= 3;
22 SerializedNavigationDriver
* SerializedNavigationDriver::Get() {
23 return ContentSerializedNavigationDriver::GetInstance();
27 ContentSerializedNavigationDriver
*
28 ContentSerializedNavigationDriver::GetInstance() {
29 return Singleton
<ContentSerializedNavigationDriver
,
30 LeakySingletonTraits
<ContentSerializedNavigationDriver
>>::get();
33 ContentSerializedNavigationDriver::ContentSerializedNavigationDriver() {
36 ContentSerializedNavigationDriver::~ContentSerializedNavigationDriver() {
39 int ContentSerializedNavigationDriver::GetDefaultReferrerPolicy() const {
40 return blink::WebReferrerPolicyDefault
;
43 bool ContentSerializedNavigationDriver::MapReferrerPolicyToOldValues(
45 int* mapped_referrer_policy
) const {
46 switch (referrer_policy
) {
47 case blink::WebReferrerPolicyAlways
:
48 case blink::WebReferrerPolicyDefault
:
49 // "always" and "default" are the same value in all versions.
50 *mapped_referrer_policy
= referrer_policy
;
53 case blink::WebReferrerPolicyOrigin
:
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 ContentSerializedNavigationDriver::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
= blink::WebReferrerPolicyNever
;
83 ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
84 const SerializedNavigationEntry
* navigation
) const {
85 if (!navigation
->has_post_data_
) {
86 return navigation
->encoded_page_state_
;
88 content::PageState page_state
=
89 content::PageState::CreateFromEncodedData(
90 navigation
->encoded_page_state_
);
91 return page_state
.RemovePasswordData().ToEncodedData();
94 void ContentSerializedNavigationDriver::Sanitize(
95 SerializedNavigationEntry
* navigation
) const {
96 content::Referrer
old_referrer(
97 navigation
->referrer_url_
,
98 static_cast<blink::WebReferrerPolicy
>(navigation
->referrer_policy_
));
99 content::Referrer new_referrer
=
100 content::Referrer::SanitizeForRequest(navigation
->virtual_url_
,
103 // No need to compare the policy, as it doesn't change during
104 // sanitization. If there has been a change, the referrer needs to be
105 // stripped from the page state as well.
106 if (navigation
->referrer_url_
!= new_referrer
.url
) {
107 navigation
->referrer_url_
= GURL();
108 navigation
->referrer_policy_
= GetDefaultReferrerPolicy();
109 navigation
->encoded_page_state_
=
110 StripReferrerFromPageState(navigation
->encoded_page_state_
);
114 std::string
ContentSerializedNavigationDriver::StripReferrerFromPageState(
115 const std::string
& page_state
) const {
116 return content::PageState::CreateFromEncodedData(page_state
)
121 } // namespace sessions