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.obsolete_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 referrer_url_.is_valid()) { |
217 pickle->WriteInt(referrer_policy_); | 229 WriteStringToPickle(pickle, &bytes_written, max_size, referrer_url_.spec()); |
| 230 } else { |
| 231 WriteStringToPickle(pickle, &bytes_written, max_size, std::string()); |
| 232 } |
| 233 pickle->WriteInt(mapped_referrer_policy); |
218 | 234 |
219 // Save info required to override the user agent. | 235 // Save info required to override the user agent. |
220 WriteStringToPickle( | 236 WriteStringToPickle( |
221 pickle, &bytes_written, max_size, | 237 pickle, &bytes_written, max_size, |
222 original_request_url_.is_valid() ? | 238 original_request_url_.is_valid() ? |
223 original_request_url_.spec() : std::string()); | 239 original_request_url_.spec() : std::string()); |
224 pickle->WriteBool(is_overriding_user_agent_); | 240 pickle->WriteBool(is_overriding_user_agent_); |
225 pickle->WriteInt64(timestamp_.ToInternalValue()); | 241 pickle->WriteInt64(timestamp_.ToInternalValue()); |
226 | 242 |
227 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_); | 243 WriteString16ToPickle(pickle, &bytes_written, max_size, search_terms_); |
228 | 244 |
229 pickle->WriteInt(http_status_code_); | 245 pickle->WriteInt(http_status_code_); |
| 246 |
| 247 pickle->WriteInt(referrer_policy_); |
230 } | 248 } |
231 | 249 |
232 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) { | 250 bool SerializedNavigationEntry::ReadFromPickle(PickleIterator* iterator) { |
233 *this = SerializedNavigationEntry(); | 251 *this = SerializedNavigationEntry(); |
234 std::string virtual_url_spec; | 252 std::string virtual_url_spec; |
235 int transition_type_int = 0; | 253 int transition_type_int = 0; |
236 if (!iterator->ReadInt(&index_) || | 254 if (!iterator->ReadInt(&index_) || |
237 !iterator->ReadString(&virtual_url_spec) || | 255 !iterator->ReadString(&virtual_url_spec) || |
238 !iterator->ReadString16(&title_) || | 256 !iterator->ReadString16(&title_) || |
239 !iterator->ReadString(&encoded_page_state_) || | 257 !iterator->ReadString(&encoded_page_state_) || |
(...skipping 11 matching lines...) Expand all Loading... |
251 has_post_data_ = type_mask & HAS_POST_DATA; | 269 has_post_data_ = type_mask & HAS_POST_DATA; |
252 // the "referrer" property was added after type_mask to the written | 270 // 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. | 271 // stream. As such, we don't fail if it can't be read. |
254 std::string referrer_spec; | 272 std::string referrer_spec; |
255 if (!iterator->ReadString(&referrer_spec)) | 273 if (!iterator->ReadString(&referrer_spec)) |
256 referrer_spec = std::string(); | 274 referrer_spec = std::string(); |
257 referrer_url_ = GURL(referrer_spec); | 275 referrer_url_ = GURL(referrer_spec); |
258 | 276 |
259 // The "referrer policy" property was added even later, so we fall back to | 277 // The "referrer policy" property was added even later, so we fall back to |
260 // the default policy if the property is not present. | 278 // the default policy if the property is not present. |
261 if (!iterator->ReadInt(&referrer_policy_)) | 279 // |
| 280 // Note: due to crbug.com/450589 this value might be incorrect, and a |
| 281 // corrected version is stored later in the pickle. |
| 282 if (!iterator->ReadInt(&referrer_policy_)) { |
262 referrer_policy_ = | 283 referrer_policy_ = |
263 SerializedNavigationDriver::Get()->GetDefaultReferrerPolicy(); | 284 SerializedNavigationDriver::Get()->GetDefaultReferrerPolicy(); |
| 285 } |
264 | 286 |
265 // If the original URL can't be found, leave it empty. | 287 // If the original URL can't be found, leave it empty. |
266 std::string original_request_url_spec; | 288 std::string original_request_url_spec; |
267 if (!iterator->ReadString(&original_request_url_spec)) | 289 if (!iterator->ReadString(&original_request_url_spec)) |
268 original_request_url_spec = std::string(); | 290 original_request_url_spec = std::string(); |
269 original_request_url_ = GURL(original_request_url_spec); | 291 original_request_url_ = GURL(original_request_url_spec); |
270 | 292 |
271 // Default to not overriding the user agent if we don't have info. | 293 // Default to not overriding the user agent if we don't have info. |
272 if (!iterator->ReadBool(&is_overriding_user_agent_)) | 294 if (!iterator->ReadBool(&is_overriding_user_agent_)) |
273 is_overriding_user_agent_ = false; | 295 is_overriding_user_agent_ = false; |
274 | 296 |
275 int64 timestamp_internal_value = 0; | 297 int64 timestamp_internal_value = 0; |
276 if (iterator->ReadInt64(×tamp_internal_value)) { | 298 if (iterator->ReadInt64(×tamp_internal_value)) { |
277 timestamp_ = base::Time::FromInternalValue(timestamp_internal_value); | 299 timestamp_ = base::Time::FromInternalValue(timestamp_internal_value); |
278 } else { | 300 } else { |
279 timestamp_ = base::Time(); | 301 timestamp_ = base::Time(); |
280 } | 302 } |
281 | 303 |
282 // If the search terms field can't be found, leave it empty. | 304 // If the search terms field can't be found, leave it empty. |
283 if (!iterator->ReadString16(&search_terms_)) | 305 if (!iterator->ReadString16(&search_terms_)) |
284 search_terms_.clear(); | 306 search_terms_.clear(); |
285 | 307 |
286 if (!iterator->ReadInt(&http_status_code_)) | 308 if (!iterator->ReadInt(&http_status_code_)) |
287 http_status_code_ = 0; | 309 http_status_code_ = 0; |
| 310 |
| 311 // Correct referrer policy (if present). |
| 312 int correct_referrer_policy; |
| 313 if (iterator->ReadInt(&correct_referrer_policy)) { |
| 314 referrer_policy_ = correct_referrer_policy; |
| 315 } else { |
| 316 int mapped_referrer_policy; |
| 317 if (!SerializedNavigationDriver::Get()->MapReferrerPolicyToNewValues( |
| 318 referrer_policy_, &mapped_referrer_policy)) { |
| 319 referrer_url_ = GURL(); |
| 320 } |
| 321 referrer_policy_ = mapped_referrer_policy; |
| 322 } |
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_obsolete_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 |