Chromium Code Reviews| Index: android_webview/native/state_serializer.cc |
| diff --git a/android_webview/native/state_serializer.cc b/android_webview/native/state_serializer.cc |
| index 65eceb3fcde1fa63e71e124fc0064fb05790357f..2a7c6729f4faeab9c06ae5c30fe7b822bb5d5547 100644 |
| --- a/android_webview/native/state_serializer.cc |
| +++ b/android_webview/native/state_serializer.cc |
| @@ -31,10 +31,17 @@ namespace android_webview { |
| namespace { |
| -// Sanity check value that we are restoring from a valid pickle. |
| -// This can potentially used as an actual serialization version number in the |
| -// future if we ever decide to support restoring from older versions. |
| -const uint32_t AW_STATE_VERSION = 20151204; |
| +const uint32_t AW_STATE_VERSION_INITIAL = 20130814; |
| +const uint32_t AW_STATE_VERSION_DATA_URL = 20151204; |
| + |
| +const uint32_t AW_STATE_VERSION = AW_STATE_VERSION_DATA_URL; |
| + |
| +const uint32_t SUPPORTED_VERSIONS[] = { |
|
mnaganov (inactive)
2016/02/10 16:37:45
Do we really need an array? Since there just 2 pos
sbergner
2016/02/11 07:23:28
Sure, makes sense.
|
| + AW_STATE_VERSION_INITIAL, |
| + AW_STATE_VERSION_DATA_URL |
| +}; |
| +const uint32_t NUM_SUPPORTED_VERSIONS = |
|
mnaganov (inactive)
2016/02/10 16:37:45
nit: there is `arraysize` in base/macros.h
|
| + sizeof(SUPPORTED_VERSIONS) / sizeof(SUPPORTED_VERSIONS[0]); |
| } // namespace |
| @@ -65,7 +72,10 @@ bool WriteToPickle(const content::WebContents& web_contents, |
| return false; |
| } |
| - // Please update AW_STATE_VERSION if serialization format is changed. |
| + // Please update AW_STATE_VERSION and SUPPORTED_VERSIONS if serialization |
| + // format is changed. |
| + // Make sure the serialization format is updated in a backwards compatible |
| + // way. |
| return true; |
| } |
| @@ -75,7 +85,8 @@ bool RestoreFromPickle(base::PickleIterator* iterator, |
| DCHECK(iterator); |
| DCHECK(web_contents); |
| - if (!internal::RestoreHeaderFromPickle(iterator)) |
| + uint32_t state_version = internal::RestoreHeaderFromPickle(iterator); |
| + if (!state_version) |
| return false; |
| int entry_count = -1; |
| @@ -98,7 +109,8 @@ bool RestoreFromPickle(base::PickleIterator* iterator, |
| entries.reserve(entry_count); |
| for (int i = 0; i < entry_count; ++i) { |
| entries.push_back(content::NavigationEntry::Create()); |
| - if (!internal::RestoreNavigationEntryFromPickle(iterator, entries[i].get())) |
| + if (!internal::RestoreNavigationEntryFromPickle(state_version, iterator, |
| + entries[i].get())) |
| return false; |
| entries[i]->SetPageID(i); |
| @@ -139,19 +151,36 @@ bool WriteHeaderToPickle(base::Pickle* pickle) { |
| return pickle->WriteUInt32(AW_STATE_VERSION); |
| } |
| -bool RestoreHeaderFromPickle(base::PickleIterator* iterator) { |
| +uint32_t RestoreHeaderFromPickle(base::PickleIterator* iterator) { |
| uint32_t state_version = -1; |
| if (!iterator->ReadUInt32(&state_version)) |
| - return false; |
| + return 0; |
| - if (AW_STATE_VERSION != state_version) |
| - return false; |
| + if (IsSupportedVersion(state_version)) { |
| + return state_version; |
| + } |
| - return true; |
| + return 0; |
| +} |
| + |
| +bool IsSupportedVersion(uint32_t state_version) { |
| + for (uint32_t version_index = 0; version_index < NUM_SUPPORTED_VERSIONS; |
| + version_index++) { |
| + if (state_version == SUPPORTED_VERSIONS[version_index]) |
| + return true; |
| + } |
| + return false; |
| } |
| bool WriteNavigationEntryToPickle(const content::NavigationEntry& entry, |
| base::Pickle* pickle) { |
| + return WriteNavigationEntryToPickle(AW_STATE_VERSION, entry, pickle); |
| +} |
| + |
| +bool WriteNavigationEntryToPickle(uint32_t state_version, |
| + const content::NavigationEntry& entry, |
| + base::Pickle* pickle) { |
| + DCHECK(IsSupportedVersion(state_version)); |
| if (!pickle->WriteString(entry.GetURL().spec())) |
| return false; |
| @@ -179,7 +208,7 @@ bool WriteNavigationEntryToPickle(const content::NavigationEntry& entry, |
| if (!pickle->WriteString(entry.GetBaseURLForDataURL().spec())) |
| return false; |
| - { |
| + if (state_version >= AW_STATE_VERSION_DATA_URL) { |
| const char* data = nullptr; |
| size_t size = 0; |
| scoped_refptr<const base::RefCountedString> s = entry.GetDataURLAsString(); |
| @@ -209,6 +238,13 @@ bool WriteNavigationEntryToPickle(const content::NavigationEntry& entry, |
| bool RestoreNavigationEntryFromPickle(base::PickleIterator* iterator, |
| content::NavigationEntry* entry) { |
| + return RestoreNavigationEntryFromPickle(AW_STATE_VERSION, iterator, entry); |
| +} |
| + |
| +bool RestoreNavigationEntryFromPickle(uint32_t state_version, |
| + base::PickleIterator* iterator, |
| + content::NavigationEntry* entry) { |
| + DCHECK(IsSupportedVersion(state_version)); |
| { |
| string url; |
| if (!iterator->ReadString(&url)) |
| @@ -274,7 +310,7 @@ bool RestoreNavigationEntryFromPickle(base::PickleIterator* iterator, |
| entry->SetBaseURLForDataURL(GURL(base_url_for_data_url)); |
| } |
| - { |
| + if (state_version >= AW_STATE_VERSION_DATA_URL) { |
| const char* data; |
| int size; |
| if (!iterator->ReadData(&data, &size)) |