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 Singleton
<ContentSerializedNavigationDriver
,
31 LeakySingletonTraits
<ContentSerializedNavigationDriver
>>::get();
34 ContentSerializedNavigationDriver::ContentSerializedNavigationDriver() {
37 ContentSerializedNavigationDriver::~ContentSerializedNavigationDriver() {
40 int ContentSerializedNavigationDriver::GetDefaultReferrerPolicy() const {
41 return blink::WebReferrerPolicyDefault
;
44 bool ContentSerializedNavigationDriver::MapReferrerPolicyToOldValues(
46 int* mapped_referrer_policy
) const {
47 switch (referrer_policy
) {
48 case blink::WebReferrerPolicyAlways
:
49 case blink::WebReferrerPolicyDefault
:
50 // "always" and "default" are the same value in all versions.
51 *mapped_referrer_policy
= referrer_policy
;
54 case blink::WebReferrerPolicyOrigin
:
55 // "origin" exists in the old encoding.
56 *mapped_referrer_policy
= kObsoleteReferrerPolicyOrigin
;
60 // Everything else is mapped to never.
61 *mapped_referrer_policy
= kObsoleteReferrerPolicyNever
;
66 bool ContentSerializedNavigationDriver::MapReferrerPolicyToNewValues(
68 int* mapped_referrer_policy
) const {
69 switch (referrer_policy
) {
70 case kObsoleteReferrerPolicyAlways
:
71 case kObsoleteReferrerPolicyDefault
:
72 // "always" and "default" are the same value in all versions.
73 *mapped_referrer_policy
= referrer_policy
;
77 // Since we don't know what encoding was used, we map the rest to "never".
78 *mapped_referrer_policy
= blink::WebReferrerPolicyNever
;
84 ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
85 const SerializedNavigationEntry
* navigation
) const {
86 if (!navigation
->has_post_data_
) {
87 return navigation
->encoded_page_state_
;
89 content::PageState page_state
=
90 content::PageState::CreateFromEncodedData(
91 navigation
->encoded_page_state_
);
92 return page_state
.RemovePasswordData().ToEncodedData();
95 void ContentSerializedNavigationDriver::Sanitize(
96 SerializedNavigationEntry
* navigation
) const {
97 content::Referrer
old_referrer(
98 navigation
->referrer_url_
,
99 static_cast<blink::WebReferrerPolicy
>(navigation
->referrer_policy_
));
100 content::Referrer new_referrer
=
101 content::Referrer::SanitizeForRequest(navigation
->virtual_url_
,
104 // No need to compare the policy, as it doesn't change during
105 // sanitization. If there has been a change, the referrer needs to be
106 // stripped from the page state as well.
107 if (navigation
->referrer_url_
!= new_referrer
.url
) {
108 navigation
->referrer_url_
= GURL();
109 navigation
->referrer_policy_
= GetDefaultReferrerPolicy();
110 navigation
->encoded_page_state_
=
111 StripReferrerFromPageState(navigation
->encoded_page_state_
);
114 #if defined(OS_ANDROID)
115 // Rewrite the old new tab and welcome page URLs to the new NTP URL.
116 if (navigation
->virtual_url_
.SchemeIs(content::kChromeUIScheme
) &&
117 (navigation
->virtual_url_
.host() == "welcome" ||
118 navigation
->virtual_url_
.host() == "newtab")) {
119 navigation
->virtual_url_
= GURL("chrome-native://newtab/");
120 navigation
->original_request_url_
= navigation
->virtual_url_
;
121 navigation
->encoded_page_state_
= content::PageState::CreateFromURL(
122 navigation
->virtual_url_
).ToEncodedData();
124 #endif // defined(OS_ANDROID)
127 std::string
ContentSerializedNavigationDriver::StripReferrerFromPageState(
128 const std::string
& page_state
) const {
129 return content::PageState::CreateFromEncodedData(page_state
)
134 } // namespace sessions