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

Side by Side Diff: components/sessions/serialized_navigation_entry.cc

Issue 658073004: Rewrites SerializedNavigationEntry to not have any //content member variables. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/serialized_navigation_entry.h" 5 #include "components/sessions/serialized_navigation_entry.h"
6 6
7 #include "base/pickle.h" 7 #include "base/pickle.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "content/public/browser/favicon_status.h" 9 #include "content/public/browser/favicon_status.h"
10 #include "content/public/browser/navigation_controller.h" 10 #include "content/public/browser/navigation_controller.h"
(...skipping 10 matching lines...) Expand all
21 21
22 SerializedNavigationEntry::SerializedNavigationEntry() 22 SerializedNavigationEntry::SerializedNavigationEntry()
23 : index_(-1), 23 : index_(-1),
24 unique_id_(0), 24 unique_id_(0),
25 transition_type_(ui::PAGE_TRANSITION_TYPED), 25 transition_type_(ui::PAGE_TRANSITION_TYPED),
26 has_post_data_(false), 26 has_post_data_(false),
27 post_id_(-1), 27 post_id_(-1),
28 is_overriding_user_agent_(false), 28 is_overriding_user_agent_(false),
29 http_status_code_(0), 29 http_status_code_(0),
30 is_restored_(false), 30 is_restored_(false),
31 blocked_state_(STATE_INVALID) {} 31 blocked_state_(STATE_INVALID) {
32 referrer_policy_ = GetDefaultReferrerPolicy();
33 }
32 34
33 SerializedNavigationEntry::~SerializedNavigationEntry() {} 35 SerializedNavigationEntry::~SerializedNavigationEntry() {}
34 36
35 // static 37 // static
36 SerializedNavigationEntry SerializedNavigationEntry::FromNavigationEntry( 38 SerializedNavigationEntry SerializedNavigationEntry::FromNavigationEntry(
37 int index, 39 int index,
38 const NavigationEntry& entry) { 40 const NavigationEntry& entry) {
39 SerializedNavigationEntry navigation; 41 SerializedNavigationEntry navigation;
40 navigation.index_ = index; 42 navigation.index_ = index;
41 navigation.unique_id_ = entry.GetUniqueID(); 43 navigation.unique_id_ = entry.GetUniqueID();
42 navigation.referrer_ = entry.GetReferrer(); 44 navigation.referrer_url_ = entry.GetReferrer().url;
45 navigation.referrer_policy_ = entry.GetReferrer().policy;
43 navigation.virtual_url_ = entry.GetVirtualURL(); 46 navigation.virtual_url_ = entry.GetVirtualURL();
44 navigation.title_ = entry.GetTitle(); 47 navigation.title_ = entry.GetTitle();
45 navigation.page_state_ = entry.GetPageState(); 48 navigation.encoded_page_state_ = entry.GetPageState().ToEncodedData();
46 navigation.transition_type_ = entry.GetTransitionType(); 49 navigation.transition_type_ = entry.GetTransitionType();
47 navigation.has_post_data_ = entry.GetHasPostData(); 50 navigation.has_post_data_ = entry.GetHasPostData();
48 navigation.post_id_ = entry.GetPostID(); 51 navigation.post_id_ = entry.GetPostID();
49 navigation.original_request_url_ = entry.GetOriginalRequestURL(); 52 navigation.original_request_url_ = entry.GetOriginalRequestURL();
50 navigation.is_overriding_user_agent_ = entry.GetIsOverridingUserAgent(); 53 navigation.is_overriding_user_agent_ = entry.GetIsOverridingUserAgent();
51 navigation.timestamp_ = entry.GetTimestamp(); 54 navigation.timestamp_ = entry.GetTimestamp();
52 navigation.is_restored_ = entry.IsRestored(); 55 navigation.is_restored_ = entry.IsRestored();
53 // If you want to navigate a named frame in Chrome, you will first need to 56 // If you want to navigate a named frame in Chrome, you will first need to
54 // add support for persisting it. It is currently only used for layout tests. 57 // add support for persisting it. It is currently only used for layout tests.
55 CHECK(entry.GetFrameToNavigate().empty()); 58 CHECK(entry.GetFrameToNavigate().empty());
56 entry.GetExtraData(kSearchTermsKey, &navigation.search_terms_); 59 entry.GetExtraData(kSearchTermsKey, &navigation.search_terms_);
57 if (entry.GetFavicon().valid) 60 if (entry.GetFavicon().valid)
58 navigation.favicon_url_ = entry.GetFavicon().url; 61 navigation.favicon_url_ = entry.GetFavicon().url;
59 navigation.http_status_code_ = entry.GetHttpStatusCode(); 62 navigation.http_status_code_ = entry.GetHttpStatusCode();
60 navigation.redirect_chain_ = entry.GetRedirectChain(); 63 navigation.redirect_chain_ = entry.GetRedirectChain();
61 64
62 return navigation; 65 return navigation;
63 } 66 }
64 67
65 SerializedNavigationEntry SerializedNavigationEntry::FromSyncData( 68 SerializedNavigationEntry SerializedNavigationEntry::FromSyncData(
66 int index, 69 int index,
67 const sync_pb::TabNavigation& sync_data) { 70 const sync_pb::TabNavigation& sync_data) {
68 SerializedNavigationEntry navigation; 71 SerializedNavigationEntry navigation;
69 navigation.index_ = index; 72 navigation.index_ = index;
70 navigation.unique_id_ = sync_data.unique_id(); 73 navigation.unique_id_ = sync_data.unique_id();
71 navigation.referrer_ = content::Referrer( 74 navigation.referrer_url_ = GURL(sync_data.referrer());
72 GURL(sync_data.referrer()), 75 navigation.referrer_policy_ = sync_data.referrer_policy();
73 static_cast<blink::WebReferrerPolicy>(sync_data.referrer_policy()));
74 navigation.virtual_url_ = GURL(sync_data.virtual_url()); 76 navigation.virtual_url_ = GURL(sync_data.virtual_url());
75 navigation.title_ = base::UTF8ToUTF16(sync_data.title()); 77 navigation.title_ = base::UTF8ToUTF16(sync_data.title());
76 navigation.page_state_ = 78 navigation.encoded_page_state_ = sync_data.state();
77 content::PageState::CreateFromEncodedData(sync_data.state());
78 79
79 uint32 transition = 0; 80 uint32 transition = 0;
80 if (sync_data.has_page_transition()) { 81 if (sync_data.has_page_transition()) {
81 switch (sync_data.page_transition()) { 82 switch (sync_data.page_transition()) {
82 case sync_pb::SyncEnums_PageTransition_LINK: 83 case sync_pb::SyncEnums_PageTransition_LINK:
83 transition = ui::PAGE_TRANSITION_LINK; 84 transition = ui::PAGE_TRANSITION_LINK;
84 break; 85 break;
85 case sync_pb::SyncEnums_PageTransition_TYPED: 86 case sync_pb::SyncEnums_PageTransition_TYPED:
86 transition = ui::PAGE_TRANSITION_TYPED; 87 transition = ui::PAGE_TRANSITION_TYPED;
87 break; 88 break;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 HAS_POST_DATA = 1 204 HAS_POST_DATA = 1
204 }; 205 };
205 206
206 } // namespace 207 } // namespace
207 208
208 // Pickle order: 209 // Pickle order:
209 // 210 //
210 // index_ 211 // index_
211 // virtual_url_ 212 // virtual_url_
212 // title_ 213 // title_
213 // page_state_ 214 // encoded_page_state_
rohitrao (ping after 24h) 2014/10/17 15:15:54 The PageState was always written to the pickle as
214 // transition_type_ 215 // transition_type_
215 // 216 //
216 // Added on later: 217 // Added on later:
217 // 218 //
218 // type_mask (has_post_data_) 219 // type_mask (has_post_data_)
219 // referrer_ 220 // referrer_url_
221 // referrer_policy_
rohitrao (ping after 24h) 2014/10/17 15:15:53 I've added referrer_policy_ here to be explicit, b
220 // original_request_url_ 222 // original_request_url_
221 // is_overriding_user_agent_ 223 // is_overriding_user_agent_
222 // timestamp_ 224 // timestamp_
223 // search_terms_ 225 // search_terms_
224 // http_status_code_ 226 // http_status_code_
225 227
226 void SerializedNavigationEntry::WriteToPickle(int max_size, 228 void SerializedNavigationEntry::WriteToPickle(int max_size,
227 Pickle* pickle) const { 229 Pickle* pickle) const {
228 pickle->WriteInt(index_); 230 pickle->WriteInt(index_);
229 231
230 int bytes_written = 0; 232 int bytes_written = 0;
231 233
232 WriteStringToPickle(pickle, &bytes_written, max_size, 234 WriteStringToPickle(pickle, &bytes_written, max_size,
233 virtual_url_.spec()); 235 virtual_url_.spec());
234 236
235 WriteString16ToPickle(pickle, &bytes_written, max_size, title_); 237 WriteString16ToPickle(pickle, &bytes_written, max_size, title_);
236 238
237 content::PageState page_state = page_state_; 239 const std::string encoded_page_state = GetSanitizedPageStateForPickle();
238 if (has_post_data_) 240 WriteStringToPickle(pickle, &bytes_written, max_size, encoded_page_state);
239 page_state = page_state.RemovePasswordData();
240
241 WriteStringToPickle(pickle, &bytes_written, max_size,
242 page_state.ToEncodedData());
243 241
244 pickle->WriteInt(transition_type_); 242 pickle->WriteInt(transition_type_);
245 243
246 const int type_mask = has_post_data_ ? HAS_POST_DATA : 0; 244 const int type_mask = has_post_data_ ? HAS_POST_DATA : 0;
247 pickle->WriteInt(type_mask); 245 pickle->WriteInt(type_mask);
248 246
249 WriteStringToPickle( 247 WriteStringToPickle(
250 pickle, &bytes_written, max_size, 248 pickle, &bytes_written, max_size,
251 referrer_.url.is_valid() ? referrer_.url.spec() : std::string()); 249 referrer_url_.is_valid() ? referrer_url_.spec() : std::string());
252 250
253 pickle->WriteInt(referrer_.policy); 251 pickle->WriteInt(referrer_policy_);
254 252
255 // Save info required to override the user agent. 253 // Save info required to override the user agent.
256 WriteStringToPickle( 254 WriteStringToPickle(
257 pickle, &bytes_written, max_size, 255 pickle, &bytes_written, max_size,
258 original_request_url_.is_valid() ? 256 original_request_url_.is_valid() ?
259 original_request_url_.spec() : std::string()); 257 original_request_url_.spec() : std::string());
260 pickle->WriteBool(is_overriding_user_agent_); 258 pickle->WriteBool(is_overriding_user_agent_);
261 pickle->WriteInt64(timestamp_.ToInternalValue()); 259 pickle->WriteInt64(timestamp_.ToInternalValue());
262 260
263 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_); 261 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_);
264 262
265 pickle->WriteInt(http_status_code_); 263 pickle->WriteInt(http_status_code_);
266 } 264 }
267 265
268 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) { 266 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) {
269 *this = SerializedNavigationEntry(); 267 *this = SerializedNavigationEntry();
270 std::string virtual_url_spec, page_state_data; 268 std::string virtual_url_spec;
271 int transition_type_int = 0; 269 int transition_type_int = 0;
272 if (!iterator->ReadInt(&index_) || 270 if (!iterator->ReadInt(&index_) ||
273 !iterator->ReadString(&virtual_url_spec) || 271 !iterator->ReadString(&virtual_url_spec) ||
274 !iterator->ReadString16(&title_) || 272 !iterator->ReadString16(&title_) ||
275 !iterator->ReadString(&page_state_data) || 273 !iterator->ReadString(&encoded_page_state_) ||
276 !iterator->ReadInt(&transition_type_int)) 274 !iterator->ReadInt(&transition_type_int))
277 return false; 275 return false;
278 virtual_url_ = GURL(virtual_url_spec); 276 virtual_url_ = GURL(virtual_url_spec);
279 page_state_ = content::PageState::CreateFromEncodedData(page_state_data);
280 transition_type_ = ui::PageTransitionFromInt(transition_type_int); 277 transition_type_ = ui::PageTransitionFromInt(transition_type_int);
281 278
282 // type_mask did not always exist in the written stream. As such, we 279 // type_mask did not always exist in the written stream. As such, we
283 // don't fail if it can't be read. 280 // don't fail if it can't be read.
284 int type_mask = 0; 281 int type_mask = 0;
285 bool has_type_mask = iterator->ReadInt(&type_mask); 282 bool has_type_mask = iterator->ReadInt(&type_mask);
286 283
287 if (has_type_mask) { 284 if (has_type_mask) {
288 has_post_data_ = type_mask & HAS_POST_DATA; 285 has_post_data_ = type_mask & HAS_POST_DATA;
289 // the "referrer" property was added after type_mask to the written 286 // the "referrer" property was added after type_mask to the written
290 // stream. As such, we don't fail if it can't be read. 287 // stream. As such, we don't fail if it can't be read.
291 std::string referrer_spec; 288 std::string referrer_spec;
292 if (!iterator->ReadString(&referrer_spec)) 289 if (!iterator->ReadString(&referrer_spec))
293 referrer_spec = std::string(); 290 referrer_spec = std::string();
291 referrer_url_ = GURL(referrer_spec);
292
294 // The "referrer policy" property was added even later, so we fall back to 293 // The "referrer policy" property was added even later, so we fall back to
295 // the default policy if the property is not present. 294 // the default policy if the property is not present.
296 int policy_int; 295 if (!iterator->ReadInt(&referrer_policy_))
297 blink::WebReferrerPolicy policy; 296 referrer_policy_ = GetDefaultReferrerPolicy();
298 if (iterator->ReadInt(&policy_int))
299 policy = static_cast<blink::WebReferrerPolicy>(policy_int);
300 else
301 policy = blink::WebReferrerPolicyDefault;
302 referrer_ = content::Referrer(GURL(referrer_spec), policy);
303 297
304 // If the original URL can't be found, leave it empty. 298 // If the original URL can't be found, leave it empty.
305 std::string original_request_url_spec; 299 std::string original_request_url_spec;
306 if (!iterator->ReadString(&original_request_url_spec)) 300 if (!iterator->ReadString(&original_request_url_spec))
307 original_request_url_spec = std::string(); 301 original_request_url_spec = std::string();
308 original_request_url_ = GURL(original_request_url_spec); 302 original_request_url_ = GURL(original_request_url_spec);
309 303
310 // Default to not overriding the user agent if we don't have info. 304 // Default to not overriding the user agent if we don't have info.
311 if (!iterator->ReadBool(&is_overriding_user_agent_)) 305 if (!iterator->ReadBool(&is_overriding_user_agent_))
312 is_overriding_user_agent_ = false; 306 is_overriding_user_agent_ = false;
(...skipping 19 matching lines...) Expand all
332 326
333 return true; 327 return true;
334 } 328 }
335 329
336 scoped_ptr<NavigationEntry> SerializedNavigationEntry::ToNavigationEntry( 330 scoped_ptr<NavigationEntry> SerializedNavigationEntry::ToNavigationEntry(
337 int page_id, 331 int page_id,
338 content::BrowserContext* browser_context) const { 332 content::BrowserContext* browser_context) const {
339 scoped_ptr<NavigationEntry> entry( 333 scoped_ptr<NavigationEntry> entry(
340 content::NavigationController::CreateNavigationEntry( 334 content::NavigationController::CreateNavigationEntry(
341 virtual_url_, 335 virtual_url_,
342 referrer_, 336 content::Referrer(
337 referrer_url_,
338 static_cast<blink::WebReferrerPolicy>(referrer_policy_)),
343 // Use a transition type of reload so that we don't incorrectly 339 // Use a transition type of reload so that we don't incorrectly
344 // increase the typed count. 340 // increase the typed count.
345 ui::PAGE_TRANSITION_RELOAD, 341 ui::PAGE_TRANSITION_RELOAD,
346 false, 342 false,
347 // The extra headers are not sync'ed across sessions. 343 // The extra headers are not sync'ed across sessions.
348 std::string(), 344 std::string(),
349 browser_context)); 345 browser_context));
350 346
351 entry->SetTitle(title_); 347 entry->SetTitle(title_);
352 entry->SetPageState(page_state_); 348 entry->SetPageState(
349 content::PageState::CreateFromEncodedData(encoded_page_state_));
353 entry->SetPageID(page_id); 350 entry->SetPageID(page_id);
354 entry->SetHasPostData(has_post_data_); 351 entry->SetHasPostData(has_post_data_);
355 entry->SetPostID(post_id_); 352 entry->SetPostID(post_id_);
356 entry->SetOriginalRequestURL(original_request_url_); 353 entry->SetOriginalRequestURL(original_request_url_);
357 entry->SetIsOverridingUserAgent(is_overriding_user_agent_); 354 entry->SetIsOverridingUserAgent(is_overriding_user_agent_);
358 entry->SetTimestamp(timestamp_); 355 entry->SetTimestamp(timestamp_);
359 entry->SetExtraData(kSearchTermsKey, search_terms_); 356 entry->SetExtraData(kSearchTermsKey, search_terms_);
360 entry->SetHttpStatusCode(http_status_code_); 357 entry->SetHttpStatusCode(http_status_code_);
361 entry->SetRedirectChain(redirect_chain_); 358 entry->SetRedirectChain(redirect_chain_);
362 359
363 // These fields should have default values. 360 // These fields should have default values.
364 DCHECK_EQ(STATE_INVALID, blocked_state_); 361 DCHECK_EQ(STATE_INVALID, blocked_state_);
365 DCHECK_EQ(0u, content_pack_categories_.size()); 362 DCHECK_EQ(0u, content_pack_categories_.size());
366 363
367 return entry.Pass(); 364 return entry.Pass();
368 } 365 }
369 366
370 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well? 367 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well?
371 // See http://crbug.com/67068. 368 // See http://crbug.com/67068.
372 sync_pb::TabNavigation SerializedNavigationEntry::ToSyncData() const { 369 sync_pb::TabNavigation SerializedNavigationEntry::ToSyncData() const {
373 sync_pb::TabNavigation sync_data; 370 sync_pb::TabNavigation sync_data;
374 sync_data.set_virtual_url(virtual_url_.spec()); 371 sync_data.set_virtual_url(virtual_url_.spec());
375 sync_data.set_referrer(referrer_.url.spec()); 372 sync_data.set_referrer(referrer_url_.spec());
376 sync_data.set_referrer_policy(referrer_.policy); 373 sync_data.set_referrer_policy(referrer_policy_);
377 sync_data.set_title(base::UTF16ToUTF8(title_)); 374 sync_data.set_title(base::UTF16ToUTF8(title_));
378 375
379 // Page transition core. 376 // Page transition core.
380 COMPILE_ASSERT(ui::PAGE_TRANSITION_LAST_CORE == 377 COMPILE_ASSERT(ui::PAGE_TRANSITION_LAST_CORE ==
381 ui::PAGE_TRANSITION_KEYWORD_GENERATED, 378 ui::PAGE_TRANSITION_KEYWORD_GENERATED,
382 PageTransitionCoreBounds); 379 PageTransitionCoreBounds);
383 switch (ui::PageTransitionStripQualifier(transition_type_)) { 380 switch (ui::PageTransitionStripQualifier(transition_type_)) {
384 case ui::PAGE_TRANSITION_LINK: 381 case ui::PAGE_TRANSITION_LINK:
385 sync_data.set_page_transition( 382 sync_data.set_page_transition(
386 sync_pb::SyncEnums_PageTransition_LINK); 383 sync_pb::SyncEnums_PageTransition_LINK);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 std::vector<NavigationEntry*> entries; 499 std::vector<NavigationEntry*> entries;
503 for (std::vector<SerializedNavigationEntry>::const_iterator 500 for (std::vector<SerializedNavigationEntry>::const_iterator
504 it = navigations.begin(); it != navigations.end(); ++it) { 501 it = navigations.begin(); it != navigations.end(); ++it) {
505 entries.push_back( 502 entries.push_back(
506 it->ToNavigationEntry(page_id, browser_context).release()); 503 it->ToNavigationEntry(page_id, browser_context).release());
507 ++page_id; 504 ++page_id;
508 } 505 }
509 return entries; 506 return entries;
510 } 507 }
511 508
509 // TODO(rohitrao): Move this content-specific code into a
510 // SerializedNavigationEntryHelper class.
511 int SerializedNavigationEntry::GetDefaultReferrerPolicy() const {
rohitrao (ping after 24h) 2014/10/17 15:15:53 I've tried to move the remaining content-specific
512 return blink::WebReferrerPolicyDefault;
513 }
514
515 std::string SerializedNavigationEntry::GetSanitizedPageStateForPickle() const {
blundell 2014/10/17 15:19:28 nit: Add a TODO here like you have in the other tw
rohitrao (ping after 24h) 2014/10/20 13:24:49 Done.
516 content::PageState page_state =
517 content::PageState::CreateFromEncodedData(encoded_page_state_);
rohitrao (ping after 24h) 2014/10/17 15:15:53 There will be some places where we now do an addit
518 if (has_post_data_)
519 page_state = page_state.RemovePasswordData();
520
521 return page_state.ToEncodedData();
522 }
523
524 // TODO(rohitrao): Move this content-specific code into a
525 // SerializedNavigationEntryHelper class.
512 void SerializedNavigationEntry::Sanitize() { 526 void SerializedNavigationEntry::Sanitize() {
527 content::Referrer old_referrer(
528 referrer_url_,
529 static_cast<blink::WebReferrerPolicy>(referrer_policy_));
513 content::Referrer new_referrer = 530 content::Referrer new_referrer =
514 content::Referrer::SanitizeForRequest(virtual_url_, referrer_); 531 content::Referrer::SanitizeForRequest(virtual_url_, old_referrer);
515 532
516 // No need to compare the policy, as it doesn't change during 533 // No need to compare the policy, as it doesn't change during
517 // sanitization. If there has been a change, the referrer needs to be 534 // sanitization. If there has been a change, the referrer needs to be
518 // stripped from the page state as well. 535 // stripped from the page state as well.
519 if (referrer_.url != new_referrer.url) { 536 if (referrer_url_ != new_referrer.url) {
520 referrer_ = content::Referrer(); 537 referrer_url_ = GURL();
521 page_state_ = page_state_.RemoveReferrer(); 538 referrer_policy_ = GetDefaultReferrerPolicy();
539 encoded_page_state_ =
540 content::PageState::CreateFromEncodedData(encoded_page_state_)
541 .RemoveReferrer()
542 .ToEncodedData();
522 } 543 }
523 } 544 }
524 545
525 } // namespace sessions 546 } // namespace sessions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698