Roll src/third_party/WebKit 3aea697:d9c6159 (svn 201973:201974)
[chromium-blink-merge.git] / components / sessions / content / content_serialized_navigation_driver.cc
blob9164772b32b60b79c2ada85496c0fffb2ffc3dca
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"
13 namespace sessions {
15 namespace {
16 const int kObsoleteReferrerPolicyAlways = 0;
17 const int kObsoleteReferrerPolicyDefault = 1;
18 const int kObsoleteReferrerPolicyNever = 2;
19 const int kObsoleteReferrerPolicyOrigin = 3;
20 } // namespace
22 // static
23 SerializedNavigationDriver* SerializedNavigationDriver::Get() {
24 return ContentSerializedNavigationDriver::GetInstance();
27 // static
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(
45 int referrer_policy,
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;
52 return true;
54 case blink::WebReferrerPolicyOrigin:
55 // "origin" exists in the old encoding.
56 *mapped_referrer_policy = kObsoleteReferrerPolicyOrigin;
57 return true;
59 default:
60 // Everything else is mapped to never.
61 *mapped_referrer_policy = kObsoleteReferrerPolicyNever;
62 return false;
66 bool ContentSerializedNavigationDriver::MapReferrerPolicyToNewValues(
67 int referrer_policy,
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;
74 return true;
76 default:
77 // Since we don't know what encoding was used, we map the rest to "never".
78 *mapped_referrer_policy = blink::WebReferrerPolicyNever;
79 return false;
83 std::string
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_,
102 old_referrer);
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)
130 .RemoveReferrer()
131 .ToEncodedData();
134 } // namespace sessions