| 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 "components/sessions/core/serialized_navigation_driver.h" | 9 #include "components/sessions/core/serialized_navigation_driver.h" |
| 10 #include "sync/protocol/session_specifics.pb.h" | 10 #include "sync/protocol/session_specifics.pb.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 } | 29 } |
| 30 | 30 |
| 31 SerializedNavigationEntry::~SerializedNavigationEntry() {} | 31 SerializedNavigationEntry::~SerializedNavigationEntry() {} |
| 32 | 32 |
| 33 SerializedNavigationEntry SerializedNavigationEntry::FromSyncData( | 33 SerializedNavigationEntry SerializedNavigationEntry::FromSyncData( |
| 34 int index, | 34 int index, |
| 35 const sync_pb::TabNavigation& sync_data) { | 35 const sync_pb::TabNavigation& sync_data) { |
| 36 SerializedNavigationEntry navigation; | 36 SerializedNavigationEntry navigation; |
| 37 navigation.index_ = index; | 37 navigation.index_ = index; |
| 38 navigation.unique_id_ = sync_data.unique_id(); | 38 navigation.unique_id_ = sync_data.unique_id(); |
| 39 navigation.referrer_url_ = GURL(sync_data.referrer()); | 39 if (sync_data.has_correct_referrer_policy()) { |
| 40 navigation.referrer_policy_ = sync_data.referrer_policy(); | 40 navigation.referrer_url_ = GURL(sync_data.referrer()); |
| 41 navigation.referrer_policy_ = sync_data.correct_referrer_policy(); |
| 42 } else { |
| 43 int mapped_referrer_policy; |
| 44 if (SerializedNavigationDriver::Get()->MapReferrerPolicyToNewValues( |
| 45 sync_data.referrer_policy(), &mapped_referrer_policy)) { |
| 46 navigation.referrer_url_ = GURL(sync_data.referrer()); |
| 47 } else { |
| 48 navigation.referrer_url_ = GURL(); |
| 49 } |
| 50 navigation.referrer_policy_ = mapped_referrer_policy; |
| 51 } |
| 41 navigation.virtual_url_ = GURL(sync_data.virtual_url()); | 52 navigation.virtual_url_ = GURL(sync_data.virtual_url()); |
| 42 navigation.title_ = base::UTF8ToUTF16(sync_data.title()); | 53 navigation.title_ = base::UTF8ToUTF16(sync_data.title()); |
| 43 navigation.encoded_page_state_ = sync_data.state(); | 54 navigation.encoded_page_state_ = sync_data.state(); |
| 44 | 55 |
| 45 uint32 transition = 0; | 56 uint32 transition = 0; |
| 46 if (sync_data.has_page_transition()) { | 57 if (sync_data.has_page_transition()) { |
| 47 switch (sync_data.page_transition()) { | 58 switch (sync_data.page_transition()) { |
| 48 case sync_pb::SyncEnums_PageTransition_LINK: | 59 case sync_pb::SyncEnums_PageTransition_LINK: |
| 49 transition = ui::PAGE_TRANSITION_LINK; | 60 transition = ui::PAGE_TRANSITION_LINK; |
| 50 break; | 61 break; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // index_ | 187 // index_ |
| 177 // virtual_url_ | 188 // virtual_url_ |
| 178 // title_ | 189 // title_ |
| 179 // encoded_page_state_ | 190 // encoded_page_state_ |
| 180 // transition_type_ | 191 // transition_type_ |
| 181 // | 192 // |
| 182 // Added on later: | 193 // Added on later: |
| 183 // | 194 // |
| 184 // type_mask (has_post_data_) | 195 // type_mask (has_post_data_) |
| 185 // referrer_url_ | 196 // referrer_url_ |
| 186 // referrer_policy_ | 197 // referrer_policy_ (broken, crbug.com/450589) |
| 187 // original_request_url_ | 198 // original_request_url_ |
| 188 // is_overriding_user_agent_ | 199 // is_overriding_user_agent_ |
| 189 // timestamp_ | 200 // timestamp_ |
| 190 // search_terms_ | 201 // search_terms_ |
| 191 // http_status_code_ | 202 // http_status_code_ |
| 203 // referrer_policy_ |
| 192 | 204 |
| 193 void SerializedNavigationEntry::WriteToPickle(int max_size, | 205 void SerializedNavigationEntry::WriteToPickle(int max_size, |
| 194 Pickle* pickle) const { | 206 Pickle* pickle) const { |
| 195 pickle->WriteInt(index_); | 207 pickle->WriteInt(index_); |
| 196 | 208 |
| 197 int bytes_written = 0; | 209 int bytes_written = 0; |
| 198 | 210 |
| 199 WriteStringToPickle(pickle, &bytes_written, max_size, | 211 WriteStringToPickle(pickle, &bytes_written, max_size, |
| 200 virtual_url_.spec()); | 212 virtual_url_.spec()); |
| 201 | 213 |
| 202 WriteString16ToPickle(pickle, &bytes_written, max_size, title_); | 214 WriteString16ToPickle(pickle, &bytes_written, max_size, title_); |
| 203 | 215 |
| 204 const std::string encoded_page_state = | 216 const std::string encoded_page_state = |
| 205 SerializedNavigationDriver::Get()->GetSanitizedPageStateForPickle(this); | 217 SerializedNavigationDriver::Get()->GetSanitizedPageStateForPickle(this); |
| 206 WriteStringToPickle(pickle, &bytes_written, max_size, encoded_page_state); | 218 WriteStringToPickle(pickle, &bytes_written, max_size, encoded_page_state); |
| 207 | 219 |
| 208 pickle->WriteInt(transition_type_); | 220 pickle->WriteInt(transition_type_); |
| 209 | 221 |
| 210 const int type_mask = has_post_data_ ? HAS_POST_DATA : 0; | 222 const int type_mask = has_post_data_ ? HAS_POST_DATA : 0; |
| 211 pickle->WriteInt(type_mask); | 223 pickle->WriteInt(type_mask); |
| 212 | 224 |
| 213 WriteStringToPickle( | 225 int mapped_referrer_policy; |
| 214 pickle, &bytes_written, max_size, | 226 if (SerializedNavigationDriver::Get()->MapReferrerPolicyToOldValues( |
| 215 referrer_url_.is_valid() ? referrer_url_.spec() : std::string()); | 227 referrer_policy_, &mapped_referrer_policy)) { |
| 216 | 228 WriteStringToPickle( |
| 217 pickle->WriteInt(referrer_policy_); | 229 pickle, &bytes_written, max_size, |
| 230 referrer_url_.is_valid() ? referrer_url_.spec() : std::string()); |
| 231 } else { |
| 232 WriteStringToPickle(pickle, &bytes_written, max_size, std::string()); |
| 233 } |
| 234 pickle->WriteInt(mapped_referrer_policy); |
| 218 | 235 |
| 219 // Save info required to override the user agent. | 236 // Save info required to override the user agent. |
| 220 WriteStringToPickle( | 237 WriteStringToPickle( |
| 221 pickle, &bytes_written, max_size, | 238 pickle, &bytes_written, max_size, |
| 222 original_request_url_.is_valid() ? | 239 original_request_url_.is_valid() ? |
| 223 original_request_url_.spec() : std::string()); | 240 original_request_url_.spec() : std::string()); |
| 224 pickle->WriteBool(is_overriding_user_agent_); | 241 pickle->WriteBool(is_overriding_user_agent_); |
| 225 pickle->WriteInt64(timestamp_.ToInternalValue()); | 242 pickle->WriteInt64(timestamp_.ToInternalValue()); |
| 226 | 243 |
| 227 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_); | 244 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_); |
| 228 | 245 |
| 229 pickle->WriteInt(http_status_code_); | 246 pickle->WriteInt(http_status_code_); |
| 247 |
| 248 pickle->WriteInt(referrer_policy_); |
| 230 } | 249 } |
| 231 | 250 |
| 232 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) { | 251 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) { |
| 233 *this = SerializedNavigationEntry(); | 252 *this = SerializedNavigationEntry(); |
| 234 std::string virtual_url_spec; | 253 std::string virtual_url_spec; |
| 235 int transition_type_int = 0; | 254 int transition_type_int = 0; |
| 236 if (!iterator->ReadInt(&index_) || | 255 if (!iterator->ReadInt(&index_) || |
| 237 !iterator->ReadString(&virtual_url_spec) || | 256 !iterator->ReadString(&virtual_url_spec) || |
| 238 !iterator->ReadString16(&title_) || | 257 !iterator->ReadString16(&title_) || |
| 239 !iterator->ReadString(&encoded_page_state_) || | 258 !iterator->ReadString(&encoded_page_state_) || |
| (...skipping 11 matching lines...) Expand all Loading... |
| 251 has_post_data_ = type_mask & HAS_POST_DATA; | 270 has_post_data_ = type_mask & HAS_POST_DATA; |
| 252 // the "referrer" property was added after type_mask to the written | 271 // the "referrer" property was added after type_mask to the written |
| 253 // stream. As such, we don't fail if it can't be read. | 272 // stream. As such, we don't fail if it can't be read. |
| 254 std::string referrer_spec; | 273 std::string referrer_spec; |
| 255 if (!iterator->ReadString(&referrer_spec)) | 274 if (!iterator->ReadString(&referrer_spec)) |
| 256 referrer_spec = std::string(); | 275 referrer_spec = std::string(); |
| 257 referrer_url_ = GURL(referrer_spec); | 276 referrer_url_ = GURL(referrer_spec); |
| 258 | 277 |
| 259 // The "referrer policy" property was added even later, so we fall back to | 278 // The "referrer policy" property was added even later, so we fall back to |
| 260 // the default policy if the property is not present. | 279 // the default policy if the property is not present. |
| 261 if (!iterator->ReadInt(&referrer_policy_)) | 280 // |
| 281 // Note: due to crbug.com/450589 this value might be incorrect, and a |
| 282 // corrected version is stored later in the pickle. |
| 283 if (!iterator->ReadInt(&referrer_policy_)) { |
| 262 referrer_policy_ = | 284 referrer_policy_ = |
| 263 SerializedNavigationDriver::Get()->GetDefaultReferrerPolicy(); | 285 SerializedNavigationDriver::Get()->GetDefaultReferrerPolicy(); |
| 286 } else { |
| 287 int mapped_referrer_policy; |
| 288 if (!SerializedNavigationDriver::Get()->MapReferrerPolicyToNewValues( |
| 289 referrer_policy_, &mapped_referrer_policy)) { |
| 290 referrer_url_ = GURL(); |
| 291 } |
| 292 referrer_policy_ = mapped_referrer_policy; |
| 293 } |
| 264 | 294 |
| 265 // If the original URL can't be found, leave it empty. | 295 // If the original URL can't be found, leave it empty. |
| 266 std::string original_request_url_spec; | 296 std::string original_request_url_spec; |
| 267 if (!iterator->ReadString(&original_request_url_spec)) | 297 if (!iterator->ReadString(&original_request_url_spec)) |
| 268 original_request_url_spec = std::string(); | 298 original_request_url_spec = std::string(); |
| 269 original_request_url_ = GURL(original_request_url_spec); | 299 original_request_url_ = GURL(original_request_url_spec); |
| 270 | 300 |
| 271 // Default to not overriding the user agent if we don't have info. | 301 // Default to not overriding the user agent if we don't have info. |
| 272 if (!iterator->ReadBool(&is_overriding_user_agent_)) | 302 if (!iterator->ReadBool(&is_overriding_user_agent_)) |
| 273 is_overriding_user_agent_ = false; | 303 is_overriding_user_agent_ = false; |
| 274 | 304 |
| 275 int64 timestamp_internal_value = 0; | 305 int64 timestamp_internal_value = 0; |
| 276 if (iterator->ReadInt64(×tamp_internal_value)) { | 306 if (iterator->ReadInt64(×tamp_internal_value)) { |
| 277 timestamp_ = base::Time::FromInternalValue(timestamp_internal_value); | 307 timestamp_ = base::Time::FromInternalValue(timestamp_internal_value); |
| 278 } else { | 308 } else { |
| 279 timestamp_ = base::Time(); | 309 timestamp_ = base::Time(); |
| 280 } | 310 } |
| 281 | 311 |
| 282 // If the search terms field can't be found, leave it empty. | 312 // If the search terms field can't be found, leave it empty. |
| 283 if (!iterator->ReadString16(&search_terms_)) | 313 if (!iterator->ReadString16(&search_terms_)) |
| 284 search_terms_.clear(); | 314 search_terms_.clear(); |
| 285 | 315 |
| 286 if (!iterator->ReadInt(&http_status_code_)) | 316 if (!iterator->ReadInt(&http_status_code_)) |
| 287 http_status_code_ = 0; | 317 http_status_code_ = 0; |
| 318 |
| 319 // Correct referrer policy (if present). |
| 320 int correct_referrer_policy; |
| 321 if (iterator->ReadInt(&correct_referrer_policy)) |
| 322 referrer_policy_ = correct_referrer_policy; |
| 288 } | 323 } |
| 289 | 324 |
| 290 SerializedNavigationDriver::Get()->Sanitize(this); | 325 SerializedNavigationDriver::Get()->Sanitize(this); |
| 291 | 326 |
| 292 is_restored_ = true; | 327 is_restored_ = true; |
| 293 | 328 |
| 294 return true; | 329 return true; |
| 295 } | 330 } |
| 296 | 331 |
| 297 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well? | 332 // TODO(zea): perhaps sync state (scroll position, form entries, etc.) as well? |
| 298 // See http://crbug.com/67068. | 333 // See http://crbug.com/67068. |
| 299 sync_pb::TabNavigation SerializedNavigationEntry::ToSyncData() const { | 334 sync_pb::TabNavigation SerializedNavigationEntry::ToSyncData() const { |
| 300 sync_pb::TabNavigation sync_data; | 335 sync_pb::TabNavigation sync_data; |
| 301 sync_data.set_virtual_url(virtual_url_.spec()); | 336 sync_data.set_virtual_url(virtual_url_.spec()); |
| 302 sync_data.set_referrer(referrer_url_.spec()); | 337 int mapped_referrer_policy; |
| 303 sync_data.set_referrer_policy(referrer_policy_); | 338 if (SerializedNavigationDriver::Get()->MapReferrerPolicyToOldValues( |
| 339 referrer_policy_, &mapped_referrer_policy)) { |
| 340 sync_data.set_referrer(referrer_url_.spec()); |
| 341 } else { |
| 342 sync_data.set_referrer(std::string()); |
| 343 } |
| 344 sync_data.set_referrer_policy(mapped_referrer_policy); |
| 345 sync_data.set_correct_referrer_policy(referrer_policy_); |
| 304 sync_data.set_title(base::UTF16ToUTF8(title_)); | 346 sync_data.set_title(base::UTF16ToUTF8(title_)); |
| 305 | 347 |
| 306 // Page transition core. | 348 // Page transition core. |
| 307 static_assert(ui::PAGE_TRANSITION_LAST_CORE == | 349 static_assert(ui::PAGE_TRANSITION_LAST_CORE == |
| 308 ui::PAGE_TRANSITION_KEYWORD_GENERATED, | 350 ui::PAGE_TRANSITION_KEYWORD_GENERATED, |
| 309 "PAGE_TRANSITION_LAST_CORE must equal " | 351 "PAGE_TRANSITION_LAST_CORE must equal " |
| 310 "PAGE_TRANSITION_KEYWORD_GENERATED"); | 352 "PAGE_TRANSITION_KEYWORD_GENERATED"); |
| 311 switch (ui::PageTransitionStripQualifier(transition_type_)) { | 353 switch (ui::PageTransitionStripQualifier(transition_type_)) { |
| 312 case ui::PAGE_TRANSITION_LINK: | 354 case ui::PAGE_TRANSITION_LINK: |
| 313 sync_data.set_page_transition( | 355 sync_data.set_page_transition( |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 redirect_chain_[last_entry].spec()); | 458 redirect_chain_[last_entry].spec()); |
| 417 } | 459 } |
| 418 } | 460 } |
| 419 | 461 |
| 420 sync_data.set_is_restored(is_restored_); | 462 sync_data.set_is_restored(is_restored_); |
| 421 | 463 |
| 422 return sync_data; | 464 return sync_data; |
| 423 } | 465 } |
| 424 | 466 |
| 425 } // namespace sessions | 467 } // namespace sessions |
| OLD | NEW |