Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: components/sessions/content/content_serialized_navigation_driver.cc

Issue 2759333002: Move chrome-specific SerializedNavigation code to chrome/. (Closed)
Patch Set: Rebase, fix Android? Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/sessions/content/content_serialized_navigation_driver.h" 5 #include "components/sessions/content/content_serialized_navigation_driver.h"
6 6
7 #include <utility>
8
7 #include "base/memory/singleton.h" 9 #include "base/memory/singleton.h"
8 #include "build/build_config.h"
9 #include "components/sessions/core/serialized_navigation_entry.h" 10 #include "components/sessions/core/serialized_navigation_entry.h"
10 #include "content/public/common/content_features.h"
11 #include "content/public/common/page_state.h" 11 #include "content/public/common/page_state.h"
12 #include "content/public/common/referrer.h" 12 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
13 #include "content/public/common/url_constants.h"
14 13
15 namespace sessions { 14 namespace sessions {
16 15
17 namespace { 16 namespace {
17
18 const int kObsoleteReferrerPolicyAlways = 0; 18 const int kObsoleteReferrerPolicyAlways = 0;
19 const int kObsoleteReferrerPolicyDefault = 1; 19 const int kObsoleteReferrerPolicyDefault = 1;
20 const int kObsoleteReferrerPolicyNever = 2; 20 const int kObsoleteReferrerPolicyNever = 2;
21 const int kObsoleteReferrerPolicyOrigin = 3; 21 const int kObsoleteReferrerPolicyOrigin = 3;
22 22
23 bool IsUberOrUberReplacementURL(const GURL& url) {
24 return url.SchemeIs(content::kChromeUIScheme) &&
25 (url.host_piece() == content::kChromeUIHistoryHost ||
26 url.host_piece() == content::kChromeUIUberHost);
27 }
28
29 } // namespace 23 } // namespace
30 24
31 // static 25 // static
32 SerializedNavigationDriver* SerializedNavigationDriver::Get() { 26 SerializedNavigationDriver* SerializedNavigationDriver::Get() {
33 return ContentSerializedNavigationDriver::GetInstance(); 27 return ContentSerializedNavigationDriver::GetInstance();
34 } 28 }
35 29
36 // static 30 // static
31 void SerializedNavigationDriver::SetSanitizeCallback(
32 const SanitizeCallback& callback) {
33 return ContentSerializedNavigationDriver::GetInstance()->set_callback(
34 callback);
35 }
36
37 // static
37 ContentSerializedNavigationDriver* 38 ContentSerializedNavigationDriver*
38 ContentSerializedNavigationDriver::GetInstance() { 39 ContentSerializedNavigationDriver::GetInstance() {
39 return base::Singleton< 40 return base::Singleton<
40 ContentSerializedNavigationDriver, 41 ContentSerializedNavigationDriver,
41 base::LeakySingletonTraits<ContentSerializedNavigationDriver>>::get(); 42 base::LeakySingletonTraits<ContentSerializedNavigationDriver>>::get();
42 } 43 }
43 44
44 ContentSerializedNavigationDriver::ContentSerializedNavigationDriver() { 45 ContentSerializedNavigationDriver::ContentSerializedNavigationDriver() {
45 } 46 }
46 47
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 default: 87 default:
87 // Since we don't know what encoding was used, we map the rest to "never". 88 // Since we don't know what encoding was used, we map the rest to "never".
88 *mapped_referrer_policy = blink::WebReferrerPolicyNever; 89 *mapped_referrer_policy = blink::WebReferrerPolicyNever;
89 return false; 90 return false;
90 } 91 }
91 } 92 }
92 93
93 std::string 94 std::string
94 ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle( 95 ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
95 const SerializedNavigationEntry* navigation) const { 96 const SerializedNavigationEntry* navigation) const {
96 if (!navigation->has_post_data_) { 97 if (!navigation->has_post_data())
97 return navigation->encoded_page_state_; 98 return navigation->encoded_page_state();
98 } 99
99 content::PageState page_state = 100 content::PageState page_state = content::PageState::CreateFromEncodedData(
100 content::PageState::CreateFromEncodedData( 101 navigation->encoded_page_state());
101 navigation->encoded_page_state_);
102 return page_state.RemovePasswordData().ToEncodedData(); 102 return page_state.RemovePasswordData().ToEncodedData();
103 } 103 }
104 104
105 void ContentSerializedNavigationDriver::Sanitize( 105 void ContentSerializedNavigationDriver::Sanitize(
106 SerializedNavigationEntry* navigation) const { 106 SerializedNavigationEntry* navigation) const {
107 content::Referrer old_referrer( 107 if (callback_)
Dan Beam 2017/03/21 05:47:50 do you need this if?
Lei Zhang 2017/03/21 17:12:51 The alternative is to call SetSanitizeCallback() w
108 navigation->referrer_url_, 108 callback_.Run(navigation);
109 static_cast<blink::WebReferrerPolicy>(navigation->referrer_policy_));
110 content::Referrer new_referrer =
111 content::Referrer::SanitizeForRequest(navigation->virtual_url_,
112 old_referrer);
113
114 // Clear any Uber UI page state so that these pages are reloaded rather than
115 // restored from page state. This fixes session restore when WebUI URLs
116 // change.
117 if (IsUberOrUberReplacementURL(navigation->virtual_url_) &&
118 IsUberOrUberReplacementURL(navigation->original_request_url_)) {
119 navigation->encoded_page_state_.clear();
120 }
121
122 // No need to compare the policy, as it doesn't change during
123 // sanitization. If there has been a change, the referrer needs to be
124 // stripped from the page state as well.
125 if (navigation->referrer_url_ != new_referrer.url) {
126 navigation->referrer_url_ = GURL();
127 navigation->referrer_policy_ = GetDefaultReferrerPolicy();
128 navigation->encoded_page_state_ =
129 StripReferrerFromPageState(navigation->encoded_page_state_);
130 }
131
132 #if defined(OS_ANDROID)
133 // Rewrite the old new tab and welcome page URLs to the new NTP URL.
134 if (navigation->virtual_url_.SchemeIs(content::kChromeUIScheme) &&
135 (navigation->virtual_url_.host_piece() == "welcome" ||
136 navigation->virtual_url_.host_piece() == "newtab")) {
137 navigation->virtual_url_ = GURL("chrome-native://newtab/");
138 navigation->original_request_url_ = navigation->virtual_url_;
139 navigation->encoded_page_state_ = content::PageState::CreateFromURL(
140 navigation->virtual_url_).ToEncodedData();
141 }
142
143 if (base::FeatureList::IsEnabled(features::kNativeAndroidHistoryManager) &&
144 navigation->virtual_url_.SchemeIs(content::kChromeUIScheme) &&
145 (navigation->virtual_url_.host_piece() == content::kChromeUIHistoryHost ||
146 navigation->virtual_url_.host_piece() ==
147 content::kChromeUIHistoryFrameHost)) {
148 // Rewrite the old history Web UI to the new android native history.
149 navigation->virtual_url_ = GURL(content::kChromeUINativeHistoryURL);
150 navigation->original_request_url_ = navigation->virtual_url_;
151 navigation->encoded_page_state_ = content::PageState::CreateFromURL(
152 navigation->virtual_url_).ToEncodedData();
153 } else if (
154 !base::FeatureList::IsEnabled(features::kNativeAndroidHistoryManager) &&
155 navigation->virtual_url_.SchemeIs(content::kChromeNativeUIScheme) &&
156 navigation->virtual_url_.host_piece() == content::kChromeUIHistoryHost) {
157 // If the android native history UI has been disabled, redirect
158 // chrome-native://history to the old web UI.
159 navigation->virtual_url_ = GURL(content::kChromeUIHistoryURL);
160 navigation->original_request_url_ = navigation->virtual_url_;
161 navigation->encoded_page_state_.clear();
162 }
163 #endif // defined(OS_ANDROID)
164 } 109 }
165 110
166 std::string ContentSerializedNavigationDriver::StripReferrerFromPageState( 111 std::string ContentSerializedNavigationDriver::StripReferrerFromPageState(
167 const std::string& page_state) const { 112 const std::string& page_state) const {
168 return content::PageState::CreateFromEncodedData(page_state) 113 return content::PageState::CreateFromEncodedData(page_state)
169 .RemoveReferrer() 114 .RemoveReferrer()
170 .ToEncodedData(); 115 .ToEncodedData();
171 } 116 }
172 117
173 void ContentSerializedNavigationDriver::RegisterExtendedInfoHandler( 118 void ContentSerializedNavigationDriver::RegisterExtendedInfoHandler(
174 const std::string& key, 119 const std::string& key,
175 std::unique_ptr<ExtendedInfoHandler> handler) { 120 std::unique_ptr<ExtendedInfoHandler> handler) {
176 DCHECK(!key.empty()); 121 DCHECK(!key.empty());
177 DCHECK(!extended_info_handler_map_.count(key)); 122 DCHECK(!extended_info_handler_map_.count(key));
178 DCHECK(handler.get()); 123 DCHECK(handler.get());
179 extended_info_handler_map_[key] = std::move(handler); 124 extended_info_handler_map_[key] = std::move(handler);
180 } 125 }
181 126
182 const ContentSerializedNavigationDriver::ExtendedInfoHandlerMap& 127 const ContentSerializedNavigationDriver::ExtendedInfoHandlerMap&
183 ContentSerializedNavigationDriver::GetAllExtendedInfoHandlers() const { 128 ContentSerializedNavigationDriver::GetAllExtendedInfoHandlers() const {
184 return extended_info_handler_map_; 129 return extended_info_handler_map_;
185 } 130 }
186 131
187 } // namespace sessions 132 } // namespace sessions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698