| OLD | NEW |
| 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 "sync/protocol/session_specifics.pb.h" | 12 #include "sync/protocol/session_specifics.pb.h" |
| 13 #include "sync/util/time.h" | 13 #include "sync/util/time.h" |
| 14 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" | 14 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
| 15 | 15 |
| 16 using content::NavigationEntry; | 16 using content::NavigationEntry; |
| 17 | 17 |
| 18 namespace sessions { | 18 namespace sessions { |
| 19 | 19 |
| 20 const char kSearchTermsKey[] = "search_terms"; | 20 const char kSearchTermsKey[] = "search_terms"; |
| 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_(content::PAGE_TRANSITION_TYPED), | 25 transition_type_(content::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 blocked_state_(STATE_INVALID) {} | 30 blocked_state_(STATE_INVALID) {} |
| 30 | 31 |
| 31 SerializedNavigationEntry::~SerializedNavigationEntry() {} | 32 SerializedNavigationEntry::~SerializedNavigationEntry() {} |
| 32 | 33 |
| 33 // static | 34 // static |
| 34 SerializedNavigationEntry SerializedNavigationEntry::FromNavigationEntry( | 35 SerializedNavigationEntry SerializedNavigationEntry::FromNavigationEntry( |
| 35 int index, | 36 int index, |
| 36 const NavigationEntry& entry) { | 37 const NavigationEntry& entry) { |
| 37 SerializedNavigationEntry navigation; | 38 SerializedNavigationEntry navigation; |
| 38 navigation.index_ = index; | 39 navigation.index_ = index; |
| 39 navigation.unique_id_ = entry.GetUniqueID(); | 40 navigation.unique_id_ = entry.GetUniqueID(); |
| 40 navigation.referrer_ = entry.GetReferrer(); | 41 navigation.referrer_ = entry.GetReferrer(); |
| 41 navigation.virtual_url_ = entry.GetVirtualURL(); | 42 navigation.virtual_url_ = entry.GetVirtualURL(); |
| 42 navigation.title_ = entry.GetTitle(); | 43 navigation.title_ = entry.GetTitle(); |
| 43 navigation.page_state_ = entry.GetPageState(); | 44 navigation.page_state_ = entry.GetPageState(); |
| 44 navigation.transition_type_ = entry.GetTransitionType(); | 45 navigation.transition_type_ = entry.GetTransitionType(); |
| 45 navigation.has_post_data_ = entry.GetHasPostData(); | 46 navigation.has_post_data_ = entry.GetHasPostData(); |
| 46 navigation.post_id_ = entry.GetPostID(); | 47 navigation.post_id_ = entry.GetPostID(); |
| 47 navigation.original_request_url_ = entry.GetOriginalRequestURL(); | 48 navigation.original_request_url_ = entry.GetOriginalRequestURL(); |
| 48 navigation.is_overriding_user_agent_ = entry.GetIsOverridingUserAgent(); | 49 navigation.is_overriding_user_agent_ = entry.GetIsOverridingUserAgent(); |
| 49 navigation.timestamp_ = entry.GetTimestamp(); | 50 navigation.timestamp_ = entry.GetTimestamp(); |
| 50 // If you want to navigate a named frame in Chrome, you will first need to | 51 // If you want to navigate a named frame in Chrome, you will first need to |
| 51 // add support for persisting it. It is currently only used for layout tests. | 52 // add support for persisting it. It is currently only used for layout tests. |
| 52 CHECK(entry.GetFrameToNavigate().empty()); | 53 CHECK(entry.GetFrameToNavigate().empty()); |
| 53 entry.GetExtraData(kSearchTermsKey, &navigation.search_terms_); | 54 entry.GetExtraData(kSearchTermsKey, &navigation.search_terms_); |
| 54 if (entry.GetFavicon().valid) | 55 if (entry.GetFavicon().valid) |
| 55 navigation.favicon_url_ = entry.GetFavicon().url; | 56 navigation.favicon_url_ = entry.GetFavicon().url; |
| 57 navigation.http_status_code_ = entry.GetHttpStatusCode(); |
| 56 | 58 |
| 57 return navigation; | 59 return navigation; |
| 58 } | 60 } |
| 59 | 61 |
| 60 SerializedNavigationEntry SerializedNavigationEntry::FromSyncData( | 62 SerializedNavigationEntry SerializedNavigationEntry::FromSyncData( |
| 61 int index, | 63 int index, |
| 62 const sync_pb::TabNavigation& sync_data) { | 64 const sync_pb::TabNavigation& sync_data) { |
| 63 SerializedNavigationEntry navigation; | 65 SerializedNavigationEntry navigation; |
| 64 navigation.index_ = index; | 66 navigation.index_ = index; |
| 65 navigation.unique_id_ = sync_data.unique_id(); | 67 navigation.unique_id_ = sync_data.unique_id(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 transition |= content::PAGE_TRANSITION_CHAIN_END; | 138 transition |= content::PAGE_TRANSITION_CHAIN_END; |
| 137 | 139 |
| 138 navigation.transition_type_ = | 140 navigation.transition_type_ = |
| 139 static_cast<content::PageTransition>(transition); | 141 static_cast<content::PageTransition>(transition); |
| 140 | 142 |
| 141 navigation.timestamp_ = base::Time(); | 143 navigation.timestamp_ = base::Time(); |
| 142 navigation.search_terms_ = UTF8ToUTF16(sync_data.search_terms()); | 144 navigation.search_terms_ = UTF8ToUTF16(sync_data.search_terms()); |
| 143 if (sync_data.has_favicon_url()) | 145 if (sync_data.has_favicon_url()) |
| 144 navigation.favicon_url_ = GURL(sync_data.favicon_url()); | 146 navigation.favicon_url_ = GURL(sync_data.favicon_url()); |
| 145 | 147 |
| 148 navigation.http_status_code_ = sync_data.http_status_code(); |
| 149 |
| 146 // We shouldn't sync session data for managed users down at the moment. | 150 // We shouldn't sync session data for managed users down at the moment. |
| 147 DCHECK(!sync_data.has_blocked_state()); | 151 DCHECK(!sync_data.has_blocked_state()); |
| 148 DCHECK_EQ(0, sync_data.content_pack_categories_size()); | 152 DCHECK_EQ(0, sync_data.content_pack_categories_size()); |
| 149 | 153 |
| 150 return navigation; | 154 return navigation; |
| 151 } | 155 } |
| 152 | 156 |
| 153 namespace { | 157 namespace { |
| 154 | 158 |
| 155 // Helper used by SerializedNavigationEntry::WriteToPickle(). It writes |str| to | 159 // Helper used by SerializedNavigationEntry::WriteToPickle(). It writes |str| to |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 // transition_type_ | 213 // transition_type_ |
| 210 // | 214 // |
| 211 // Added on later: | 215 // Added on later: |
| 212 // | 216 // |
| 213 // type_mask (has_post_data_) | 217 // type_mask (has_post_data_) |
| 214 // referrer_ | 218 // referrer_ |
| 215 // original_request_url_ | 219 // original_request_url_ |
| 216 // is_overriding_user_agent_ | 220 // is_overriding_user_agent_ |
| 217 // timestamp_ | 221 // timestamp_ |
| 218 // search_terms_ | 222 // search_terms_ |
| 223 // http_status_code_ |
| 219 | 224 |
| 220 void SerializedNavigationEntry::WriteToPickle(int max_size, | 225 void SerializedNavigationEntry::WriteToPickle(int max_size, |
| 221 Pickle* pickle) const { | 226 Pickle* pickle) const { |
| 222 pickle->WriteInt(index_); | 227 pickle->WriteInt(index_); |
| 223 | 228 |
| 224 int bytes_written = 0; | 229 int bytes_written = 0; |
| 225 | 230 |
| 226 WriteStringToPickle(pickle, &bytes_written, max_size, | 231 WriteStringToPickle(pickle, &bytes_written, max_size, |
| 227 virtual_url_.spec()); | 232 virtual_url_.spec()); |
| 228 | 233 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 248 | 253 |
| 249 // Save info required to override the user agent. | 254 // Save info required to override the user agent. |
| 250 WriteStringToPickle( | 255 WriteStringToPickle( |
| 251 pickle, &bytes_written, max_size, | 256 pickle, &bytes_written, max_size, |
| 252 original_request_url_.is_valid() ? | 257 original_request_url_.is_valid() ? |
| 253 original_request_url_.spec() : std::string()); | 258 original_request_url_.spec() : std::string()); |
| 254 pickle->WriteBool(is_overriding_user_agent_); | 259 pickle->WriteBool(is_overriding_user_agent_); |
| 255 pickle->WriteInt64(timestamp_.ToInternalValue()); | 260 pickle->WriteInt64(timestamp_.ToInternalValue()); |
| 256 | 261 |
| 257 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_); | 262 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_); |
| 263 |
| 264 pickle->WriteInt(http_status_code_); |
| 258 } | 265 } |
| 259 | 266 |
| 260 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) { | 267 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) { |
| 261 *this = SerializedNavigationEntry(); | 268 *this = SerializedNavigationEntry(); |
| 262 std::string virtual_url_spec, page_state_data; | 269 std::string virtual_url_spec, page_state_data; |
| 263 int transition_type_int = 0; | 270 int transition_type_int = 0; |
| 264 if (!iterator->ReadInt(&index_) || | 271 if (!iterator->ReadInt(&index_) || |
| 265 !iterator->ReadString(&virtual_url_spec) || | 272 !iterator->ReadString(&virtual_url_spec) || |
| 266 !iterator->ReadString16(&title_) || | 273 !iterator->ReadString16(&title_) || |
| 267 !iterator->ReadString(&page_state_data) || | 274 !iterator->ReadString(&page_state_data) || |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 int64 timestamp_internal_value = 0; | 313 int64 timestamp_internal_value = 0; |
| 307 if (iterator->ReadInt64(×tamp_internal_value)) { | 314 if (iterator->ReadInt64(×tamp_internal_value)) { |
| 308 timestamp_ = base::Time::FromInternalValue(timestamp_internal_value); | 315 timestamp_ = base::Time::FromInternalValue(timestamp_internal_value); |
| 309 } else { | 316 } else { |
| 310 timestamp_ = base::Time(); | 317 timestamp_ = base::Time(); |
| 311 } | 318 } |
| 312 | 319 |
| 313 // If the search terms field can't be found, leave it empty. | 320 // If the search terms field can't be found, leave it empty. |
| 314 if (!iterator->ReadString16(&search_terms_)) | 321 if (!iterator->ReadString16(&search_terms_)) |
| 315 search_terms_.clear(); | 322 search_terms_.clear(); |
| 323 |
| 324 if (!iterator->ReadInt(&http_status_code_)) |
| 325 http_status_code_ = 0; |
| 316 } | 326 } |
| 317 | 327 |
| 318 return true; | 328 return true; |
| 319 } | 329 } |
| 320 | 330 |
| 321 scoped_ptr<NavigationEntry> SerializedNavigationEntry::ToNavigationEntry( | 331 scoped_ptr<NavigationEntry> SerializedNavigationEntry::ToNavigationEntry( |
| 322 int page_id, | 332 int page_id, |
| 323 content::BrowserContext* browser_context) const { | 333 content::BrowserContext* browser_context) const { |
| 324 scoped_ptr<NavigationEntry> entry( | 334 scoped_ptr<NavigationEntry> entry( |
| 325 content::NavigationController::CreateNavigationEntry( | 335 content::NavigationController::CreateNavigationEntry( |
| 326 virtual_url_, | 336 virtual_url_, |
| 327 referrer_, | 337 referrer_, |
| 328 // Use a transition type of reload so that we don't incorrectly | 338 // Use a transition type of reload so that we don't incorrectly |
| 329 // increase the typed count. | 339 // increase the typed count. |
| 330 content::PAGE_TRANSITION_RELOAD, | 340 content::PAGE_TRANSITION_RELOAD, |
| 331 false, | 341 false, |
| 332 // The extra headers are not sync'ed across sessions. | 342 // The extra headers are not sync'ed across sessions. |
| 333 std::string(), | 343 std::string(), |
| 334 browser_context)); | 344 browser_context)); |
| 335 | 345 |
| 336 entry->SetTitle(title_); | 346 entry->SetTitle(title_); |
| 337 entry->SetPageState(page_state_); | 347 entry->SetPageState(page_state_); |
| 338 entry->SetPageID(page_id); | 348 entry->SetPageID(page_id); |
| 339 entry->SetHasPostData(has_post_data_); | 349 entry->SetHasPostData(has_post_data_); |
| 340 entry->SetPostID(post_id_); | 350 entry->SetPostID(post_id_); |
| 341 entry->SetOriginalRequestURL(original_request_url_); | 351 entry->SetOriginalRequestURL(original_request_url_); |
| 342 entry->SetIsOverridingUserAgent(is_overriding_user_agent_); | 352 entry->SetIsOverridingUserAgent(is_overriding_user_agent_); |
| 343 entry->SetTimestamp(timestamp_); | 353 entry->SetTimestamp(timestamp_); |
| 344 entry->SetExtraData(kSearchTermsKey, search_terms_); | 354 entry->SetExtraData(kSearchTermsKey, search_terms_); |
| 355 entry->SetHttpStatusCode(http_status_code_); |
| 345 | 356 |
| 346 // These fields should have default values. | 357 // These fields should have default values. |
| 347 DCHECK_EQ(STATE_INVALID, blocked_state_); | 358 DCHECK_EQ(STATE_INVALID, blocked_state_); |
| 348 DCHECK_EQ(0u, content_pack_categories_.size()); | 359 DCHECK_EQ(0u, content_pack_categories_.size()); |
| 349 | 360 |
| 350 return entry.Pass(); | 361 return entry.Pass(); |
| 351 } | 362 } |
| 352 | 363 |
| 353 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well? | 364 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well? |
| 354 // See http://crbug.com/67068. | 365 // See http://crbug.com/67068. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 sync_data.set_navigation_chain_end( | 444 sync_data.set_navigation_chain_end( |
| 434 (transition_type_ & content::PAGE_TRANSITION_CHAIN_END) != 0); | 445 (transition_type_ & content::PAGE_TRANSITION_CHAIN_END) != 0); |
| 435 | 446 |
| 436 sync_data.set_unique_id(unique_id_); | 447 sync_data.set_unique_id(unique_id_); |
| 437 sync_data.set_timestamp_msec(syncer::TimeToProtoTime(timestamp_)); | 448 sync_data.set_timestamp_msec(syncer::TimeToProtoTime(timestamp_)); |
| 438 // The full-resolution timestamp works as a global ID. | 449 // The full-resolution timestamp works as a global ID. |
| 439 sync_data.set_global_id(timestamp_.ToInternalValue()); | 450 sync_data.set_global_id(timestamp_.ToInternalValue()); |
| 440 | 451 |
| 441 sync_data.set_search_terms(UTF16ToUTF8(search_terms_)); | 452 sync_data.set_search_terms(UTF16ToUTF8(search_terms_)); |
| 442 | 453 |
| 454 sync_data.set_http_status_code(http_status_code_); |
| 455 |
| 443 if (favicon_url_.is_valid()) | 456 if (favicon_url_.is_valid()) |
| 444 sync_data.set_favicon_url(favicon_url_.spec()); | 457 sync_data.set_favicon_url(favicon_url_.spec()); |
| 445 | 458 |
| 446 if (blocked_state_ != STATE_INVALID) { | 459 if (blocked_state_ != STATE_INVALID) { |
| 447 sync_data.set_blocked_state( | 460 sync_data.set_blocked_state( |
| 448 static_cast<sync_pb::TabNavigation_BlockedState>(blocked_state_)); | 461 static_cast<sync_pb::TabNavigation_BlockedState>(blocked_state_)); |
| 449 } | 462 } |
| 450 | 463 |
| 451 for (std::set<std::string>::const_iterator it = | 464 for (std::set<std::string>::const_iterator it = |
| 452 content_pack_categories_.begin(); | 465 content_pack_categories_.begin(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 466 for (std::vector<SerializedNavigationEntry>::const_iterator | 479 for (std::vector<SerializedNavigationEntry>::const_iterator |
| 467 it = navigations.begin(); it != navigations.end(); ++it) { | 480 it = navigations.begin(); it != navigations.end(); ++it) { |
| 468 entries.push_back( | 481 entries.push_back( |
| 469 it->ToNavigationEntry(page_id, browser_context).release()); | 482 it->ToNavigationEntry(page_id, browser_context).release()); |
| 470 ++page_id; | 483 ++page_id; |
| 471 } | 484 } |
| 472 return entries; | 485 return entries; |
| 473 } | 486 } |
| 474 | 487 |
| 475 } // namespace sessions | 488 } // namespace sessions |
| OLD | NEW |