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

Unified Diff: android_webview/native/state_serializer.cc

Issue 11348113: Android WebView save/restoreState and copyBackForwardList (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: android_webview/native/state_serializer.cc
diff --git a/android_webview/native/state_serializer.cc b/android_webview/native/state_serializer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..92d3bf9ffd3fe5f9040bf614f72258b56b68c5ca
--- /dev/null
+++ b/android_webview/native/state_serializer.cc
@@ -0,0 +1,233 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "android_webview/native/state_serializer.h"
+
+#include <string>
+#include <vector>
+
+#include "base/pickle.h"
+#include "base/time.h"
+#include "content/public/browser/navigation_controller.h"
+#include "content/public/browser/navigation_entry.h"
+#include "content/public/browser/web_contents.h"
+
+using std::string;
+using std::vector;
+
+namespace android_webview {
+
+namespace {
+const uint32 AW_STATE_VERSION = 20121116;
+} // namespace
+
+bool WriteToPickle(const content::WebContents& web_contents,
+ Pickle* pickle) {
+ DCHECK(pickle);
+
+ if (!WriteHeaderToPickle(pickle))
+ return false;
+
+ const content::NavigationController& controller =
+ web_contents.GetController();
+ const int entry_count = controller.GetEntryCount();
+ const int selected_entry = controller.GetCurrentEntryIndex();
+ DCHECK(entry_count >= 0);
+ DCHECK(selected_entry >= -1); // -1 is valid
+ // TODO(boliu): _thispatch_ check correct even if entry_count == 0
+ DCHECK(selected_entry < entry_count);
+
+ if (!pickle->WriteInt(entry_count))
+ return false;
+
+ if (!pickle->WriteInt(selected_entry))
+ return false;
+
+ for (int i = 0; i < entry_count; ++i) {
+ if (!WriteNavigationEntryToPickle(*controller.GetEntryAtIndex(i), pickle))
+ return false;
+ }
+
+ return true;
+}
+
+bool RestoreFromPickle(PickleIterator* iterator,
+ content::WebContents* web_contents) {
+ if (!RestoreHeaderFromPickle(iterator))
+ return false;
+
+ int entry_count = -1;
+ int selected_entry = -2; // -1 is a valid value
+ if (!iterator->ReadInt(&entry_count))
+ return false;
+ if (!iterator->ReadInt(&selected_entry))
+ return false;
+ if (entry_count < 0)
+ return false;
+ if (selected_entry < -1)
+ return false;
+ if (selected_entry >= entry_count)
+ return false;
+
+ scoped_ptr<content::NavigationEntry> restored_entries[entry_count];
+ vector<content::NavigationEntry*> restored_entries_vector(entry_count);
+ for (int i = 0; i < entry_count; ++i) {
+ restored_entries[i].reset(content::NavigationEntry::Create());
+ restored_entries_vector[i] = restored_entries[i].get();
+ if (!RestoreNavigationEntryFromPickle(iterator, restored_entries[i].get()))
+ return false;
+ }
+
+ web_contents->GetController().Restore(
+ selected_entry,
+ content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY,
+ &restored_entries_vector);
+
+ content::NavigationEntry* unused_warning ALLOW_UNUSED;
+ if (0 == restored_entries_vector.size()) {
+ for (int i = 0; i < entry_count; ++i) {
+ unused_warning = restored_entries[i].release();
+ }
+ } else {
+ return false;
+ }
+
+ return true;
+}
+
+bool WriteHeaderToPickle(Pickle* pickle) {
+ return pickle->WriteUInt32(AW_STATE_VERSION);
+}
+
+bool RestoreHeaderFromPickle(PickleIterator* iterator) {
+ uint32 state_version = -1;
+ if (!iterator->ReadUInt32(&state_version))
+ return false;
+
+ if (AW_STATE_VERSION != state_version)
+ return false;
+
+ return true;
+}
+
+bool WriteNavigationEntryToPickle(const content::NavigationEntry& entry,
+ Pickle* pickle) {
+ if (!pickle->WriteString(entry.GetURL().spec()))
+ return false;
+
+ if (!pickle->WriteString(entry.GetVirtualURL().spec()))
+ return false;
+
+ // TODO(boliu): _thispatch_ why is base url for data url not persisted?
+
+ const content::Referrer& referrer = entry.GetReferrer();
+ if (!pickle->WriteString(referrer.url.spec()))
+ return false;
+ if (!pickle->WriteInt(static_cast<int>(referrer.policy)))
+ return false;
+
+ if (!pickle->WriteString16(entry.GetTitle()))
+ return false;
+
+ if (!pickle->WriteString(entry.GetContentState()))
+ return false;
+
+ /*
+ TODO(boliu): _thispatch_ transition type gets restored to reload? Check.
+ if (!pickle->WriteInt(static_cast<int>(entry.GetTransitionType())))
+ return false;
+ */
+
+ if (!pickle->WriteBool(static_cast<int>(entry.GetHasPostData())))
+ return false;
+
+ if (!pickle->WriteString(entry.GetOriginalRequestURL().spec()))
+ return false;
+
+ if (!pickle->WriteBool(static_cast<int>(entry.GetIsOverridingUserAgent())))
+ return false;
+
+ if (!pickle->WriteInt64(entry.GetTimestamp().ToInternalValue()))
+ return false;
+
+ return true;
+}
+
+bool RestoreNavigationEntryFromPickle(PickleIterator* iterator,
+ content::NavigationEntry* entry) {
+ {
+ string url;
+ if (!iterator->ReadString(&url))
+ return false;
+ entry->SetURL(GURL(url));
+ }
+
+ {
+ string virtual_url;
+ if (!iterator->ReadString(&virtual_url))
+ return false;
+ entry->SetVirtualURL(GURL(virtual_url));
+ }
+
+ {
+ content::Referrer referrer;
+ string referrer_url;
+ int policy;
+
+ if (!iterator->ReadString(&referrer_url))
+ return false;
+ if (!iterator->ReadInt(&policy))
+ return false;
+
+ referrer.url = GURL(referrer_url);
+ referrer.policy = static_cast<WebKit::WebReferrerPolicy>(policy);
+ entry->SetReferrer(referrer);
+ }
+
+ {
+ string16 title;
+ if (!iterator->ReadString16(&title))
+ return false;
+ entry->SetTitle(title);
+ }
+
+ {
+ string content_state;
+ if (!iterator->ReadString(&content_state))
+ return false;
+ entry->SetContentState(content_state);
+ }
+
+ {
+ bool has_post_data;
+ if (!iterator->ReadBool(&has_post_data))
+ return false;
+ entry->SetHasPostData(has_post_data);
+ }
+
+ {
+ string original_request_url;
+ if (!iterator->ReadString(&original_request_url))
+ return false;
+ entry->SetOriginalRequestURL(GURL(original_request_url));
+ }
+
+ {
+ bool is_overriding_user_agent;
+ if (!iterator->ReadBool(&is_overriding_user_agent))
+ return false;
+ entry->SetIsOverridingUserAgent(is_overriding_user_agent);
+ }
+
+ {
+ int64 timestamp;
+ if (!iterator->ReadInt64(&timestamp))
+ return false;
+ entry->SetTimestamp(base::Time::FromInternalValue(timestamp));
+ }
+
+ return true;
+}
+
+} // namespace android_webview

Powered by Google App Engine
This is Rietveld 408576698