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"
11 #include "content/public/common/url_constants.h"
16 const int kObsoleteReferrerPolicyAlways
= 0;
17 const int kObsoleteReferrerPolicyDefault
= 1;
18 const int kObsoleteReferrerPolicyNever
= 2;
19 const int kObsoleteReferrerPolicyOrigin
= 3;
23 SerializedNavigationDriver
* SerializedNavigationDriver::Get() {
24 return ContentSerializedNavigationDriver::GetInstance();
28 ContentSerializedNavigationDriver
*
29 ContentSerializedNavigationDriver::GetInstance() {
30 return base::Singleton
<
31 ContentSerializedNavigationDriver
,
32 base::LeakySingletonTraits
<ContentSerializedNavigationDriver
>>::get();
35 ContentSerializedNavigationDriver::ContentSerializedNavigationDriver() {
38 ContentSerializedNavigationDriver::~ContentSerializedNavigationDriver() {
41 int ContentSerializedNavigationDriver::GetDefaultReferrerPolicy() const {
42 return blink::WebReferrerPolicyDefault
;
45 bool ContentSerializedNavigationDriver::MapReferrerPolicyToOldValues(
47 int* mapped_referrer_policy
) const {
48 switch (referrer_policy
) {
49 case blink::WebReferrerPolicyAlways
:
50 case blink::WebReferrerPolicyDefault
:
51 // "always" and "default" are the same value in all versions.
52 *mapped_referrer_policy
= referrer_policy
;
55 case blink::WebReferrerPolicyOrigin
:
56 // "origin" exists in the old encoding.
57 *mapped_referrer_policy
= kObsoleteReferrerPolicyOrigin
;
61 // Everything else is mapped to never.
62 *mapped_referrer_policy
= kObsoleteReferrerPolicyNever
;
67 bool ContentSerializedNavigationDriver::MapReferrerPolicyToNewValues(
69 int* mapped_referrer_policy
) const {
70 switch (referrer_policy
) {
71 case kObsoleteReferrerPolicyAlways
:
72 case kObsoleteReferrerPolicyDefault
:
73 // "always" and "default" are the same value in all versions.
74 *mapped_referrer_policy
= referrer_policy
;
78 // Since we don't know what encoding was used, we map the rest to "never".
79 *mapped_referrer_policy
= blink::WebReferrerPolicyNever
;
85 ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
86 const SerializedNavigationEntry
* navigation
) const {
87 if (!navigation
->has_post_data_
) {
88 return navigation
->encoded_page_state_
;
90 content::PageState page_state
=
91 content::PageState::CreateFromEncodedData(
92 navigation
->encoded_page_state_
);
93 return page_state
.RemovePasswordData().ToEncodedData();
96 void ContentSerializedNavigationDriver::Sanitize(
97 SerializedNavigationEntry
* navigation
) const {
98 content::Referrer
old_referrer(
99 navigation
->referrer_url_
,
100 static_cast<blink::WebReferrerPolicy
>(navigation
->referrer_policy_
));
101 content::Referrer new_referrer
=
102 content::Referrer::SanitizeForRequest(navigation
->virtual_url_
,
105 // No need to compare the policy, as it doesn't change during
106 // sanitization. If there has been a change, the referrer needs to be
107 // stripped from the page state as well.
108 if (navigation
->referrer_url_
!= new_referrer
.url
) {
109 navigation
->referrer_url_
= GURL();
110 navigation
->referrer_policy_
= GetDefaultReferrerPolicy();
111 navigation
->encoded_page_state_
=
112 StripReferrerFromPageState(navigation
->encoded_page_state_
);
115 #if defined(OS_ANDROID)
116 // Rewrite the old new tab and welcome page URLs to the new NTP URL.
117 if (navigation
->virtual_url_
.SchemeIs(content::kChromeUIScheme
) &&
118 (navigation
->virtual_url_
.host() == "welcome" ||
119 navigation
->virtual_url_
.host() == "newtab")) {
120 navigation
->virtual_url_
= GURL("chrome-native://newtab/");
121 navigation
->original_request_url_
= navigation
->virtual_url_
;
122 navigation
->encoded_page_state_
= content::PageState::CreateFromURL(
123 navigation
->virtual_url_
).ToEncodedData();
125 #endif // defined(OS_ANDROID)
128 std::string
ContentSerializedNavigationDriver::StripReferrerFromPageState(
129 const std::string
& page_state
) const {
130 return content::PageState::CreateFromEncodedData(page_state
)
135 } // namespace sessions