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

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

Powered by Google App Engine
This is Rietveld 408576698