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

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

Issue 2759333002: Move chrome-specific SerializedNavigation code to chrome/. (Closed)
Patch Set: 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) { 23 ContentSerializedNavigationDriver* g_instance = nullptr;
24 return url.SchemeIs(content::kChromeUIScheme) &&
25 (url.host_piece() == content::kChromeUIHistoryHost ||
26 url.host_piece() == content::kChromeUIUberHost);
27 }
28 24
29 } // namespace 25 } // namespace
30 26
31 // static 27 // static
32 SerializedNavigationDriver* SerializedNavigationDriver::Get() { 28 SerializedNavigationDriver* SerializedNavigationDriver::Get() {
33 return ContentSerializedNavigationDriver::GetInstance(); 29 return ContentSerializedNavigationDriver::GetInstance();
34 } 30 }
35 31
36 // static 32 // static
37 ContentSerializedNavigationDriver* 33 ContentSerializedNavigationDriver*
38 ContentSerializedNavigationDriver::GetInstance() { 34 ContentSerializedNavigationDriver::GetInstance() {
39 return base::Singleton< 35 if (g_instance)
36 return g_instance;
37
38 auto* instance = base::Singleton<
40 ContentSerializedNavigationDriver, 39 ContentSerializedNavigationDriver,
41 base::LeakySingletonTraits<ContentSerializedNavigationDriver>>::get(); 40 base::LeakySingletonTraits<ContentSerializedNavigationDriver>>::get();
41 g_instance = instance;
42 return instance;
43 }
44
45 // static
46 void ContentSerializedNavigationDriver::SetInstance(
47 ContentSerializedNavigationDriver* instance) {
48 DCHECK(!g_instance || !instance);
49 g_instance = instance;
42 } 50 }
43 51
44 ContentSerializedNavigationDriver::ContentSerializedNavigationDriver() { 52 ContentSerializedNavigationDriver::ContentSerializedNavigationDriver() {
45 } 53 }
46 54
47 ContentSerializedNavigationDriver::~ContentSerializedNavigationDriver() { 55 ContentSerializedNavigationDriver::~ContentSerializedNavigationDriver() {
48 } 56 }
49 57
50 int ContentSerializedNavigationDriver::GetDefaultReferrerPolicy() const { 58 int ContentSerializedNavigationDriver::GetDefaultReferrerPolicy() const {
51 return blink::WebReferrerPolicyDefault; 59 return blink::WebReferrerPolicyDefault;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 default: 94 default:
87 // Since we don't know what encoding was used, we map the rest to "never". 95 // Since we don't know what encoding was used, we map the rest to "never".
88 *mapped_referrer_policy = blink::WebReferrerPolicyNever; 96 *mapped_referrer_policy = blink::WebReferrerPolicyNever;
89 return false; 97 return false;
90 } 98 }
91 } 99 }
92 100
93 std::string 101 std::string
94 ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle( 102 ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
95 const SerializedNavigationEntry* navigation) const { 103 const SerializedNavigationEntry* navigation) const {
96 if (!navigation->has_post_data_) { 104 if (!navigation->has_post_data())
97 return navigation->encoded_page_state_; 105 return navigation->encoded_page_state();
98 } 106
99 content::PageState page_state = 107 content::PageState page_state = content::PageState::CreateFromEncodedData(
100 content::PageState::CreateFromEncodedData( 108 navigation->encoded_page_state());
101 navigation->encoded_page_state_);
102 return page_state.RemovePasswordData().ToEncodedData(); 109 return page_state.RemovePasswordData().ToEncodedData();
103 } 110 }
104 111
105 void ContentSerializedNavigationDriver::Sanitize( 112 void ContentSerializedNavigationDriver::Sanitize(
106 SerializedNavigationEntry* navigation) const { 113 SerializedNavigationEntry* navigation) const {
107 content::Referrer old_referrer(
108 navigation->referrer_url_,
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 } 114 }
165 115
166 std::string ContentSerializedNavigationDriver::StripReferrerFromPageState( 116 std::string ContentSerializedNavigationDriver::StripReferrerFromPageState(
167 const std::string& page_state) const { 117 const std::string& page_state) const {
168 return content::PageState::CreateFromEncodedData(page_state) 118 return content::PageState::CreateFromEncodedData(page_state)
169 .RemoveReferrer() 119 .RemoveReferrer()
170 .ToEncodedData(); 120 .ToEncodedData();
171 } 121 }
172 122
173 void ContentSerializedNavigationDriver::RegisterExtendedInfoHandler( 123 void ContentSerializedNavigationDriver::RegisterExtendedInfoHandler(
174 const std::string& key, 124 const std::string& key,
175 std::unique_ptr<ExtendedInfoHandler> handler) { 125 std::unique_ptr<ExtendedInfoHandler> handler) {
176 DCHECK(!key.empty()); 126 DCHECK(!key.empty());
177 DCHECK(!extended_info_handler_map_.count(key)); 127 DCHECK(!extended_info_handler_map_.count(key));
178 DCHECK(handler.get()); 128 DCHECK(handler.get());
179 extended_info_handler_map_[key] = std::move(handler); 129 extended_info_handler_map_[key] = std::move(handler);
180 } 130 }
181 131
182 const ContentSerializedNavigationDriver::ExtendedInfoHandlerMap& 132 const ContentSerializedNavigationDriver::ExtendedInfoHandlerMap&
183 ContentSerializedNavigationDriver::GetAllExtendedInfoHandlers() const { 133 ContentSerializedNavigationDriver::GetAllExtendedInfoHandlers() const {
184 return extended_info_handler_map_; 134 return extended_info_handler_map_;
185 } 135 }
186 136
187 } // namespace sessions 137 } // namespace sessions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698