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

Side by Side Diff: android_webview/native/state_serializer.cc

Issue 11420056: Android WebView save/restoreState and backForardList Part 1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use ScopedVector 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "android_webview/native/state_serializer.h"
6
7 #include <string>
8 #include <vector>
boliu 2012/11/19 19:57:31 Not needed, will remove before cq/submit
boliu 2012/11/20 22:18:16 Done.
9
10 #include "base/memory/scoped_vector.h"
11 #include "base/pickle.h"
12 #include "base/time.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/web_contents.h"
16
17 using std::string;
18 using std::vector;
19
20 namespace android_webview {
21
22 namespace {
23 // Sanity check value that we are restoring from a valid pickle.
24 const uint32 AW_STATE_VERSION = 20121116;
mkosiba (inactive) 2012/11/20 17:40:57 1) I don't think this is a good idea. The next per
boliu 2012/11/20 19:35:38 I see the point that next person might miss this,
joth 2012/11/20 19:51:02 A major motivator for a version number in general
boliu 2012/11/20 22:18:16 Added comment.
25 } // namespace
26
27 bool WriteToPickle(const content::WebContents& web_contents,
28 Pickle* pickle) {
29 DCHECK(pickle);
30
31 if (!WriteHeaderToPickle(pickle))
32 return false;
33
34 const content::NavigationController& controller =
35 web_contents.GetController();
36 const int entry_count = controller.GetEntryCount();
37 const int selected_entry = controller.GetCurrentEntryIndex();
38 DCHECK(entry_count >= 0);
39 DCHECK(selected_entry >= -1); // -1 is valid
40 DCHECK(selected_entry < entry_count);
41
42 if (!pickle->WriteInt(entry_count))
43 return false;
44
45 if (!pickle->WriteInt(selected_entry))
46 return false;
47
48 for (int i = 0; i < entry_count; ++i) {
49 if (!WriteNavigationEntryToPickle(*controller.GetEntryAtIndex(i), pickle))
50 return false;
51 }
52
53 return true;
54 }
55
56 bool RestoreFromPickle(PickleIterator* iterator,
57 content::WebContents* web_contents) {
58 DCHECK(iterator);
59 DCHECK(web_contents);
60
61 if (!RestoreHeaderFromPickle(iterator))
62 return false;
63
64 int entry_count = -1;
65 int selected_entry = -2; // -1 is a valid value
66
67 if (!iterator->ReadInt(&entry_count))
68 return false;
69
70 if (!iterator->ReadInt(&selected_entry))
71 return false;
72
73 if (entry_count < 0)
74 return false;
75 if (selected_entry < -1)
76 return false;
77 if (selected_entry >= entry_count)
78 return false;
79
80 ScopedVector<content::NavigationEntry> restored_entries;
81 for (int i = 0; i < entry_count; ++i) {
82 restored_entries.push_back(content::NavigationEntry::Create());
83 if (!RestoreNavigationEntryFromPickle(iterator, restored_entries[i]))
84 return false;
85 }
86
87 // |web_contents| takes ownership of these entries after this call.
88 web_contents->GetController().Restore(
89 selected_entry,
90 content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY,
91 &restored_entries.get());
92 DCHECK_EQ(0u, restored_entries.size());
93
94 return true;
95 }
96
97 bool WriteHeaderToPickle(Pickle* pickle) {
98 return pickle->WriteUInt32(AW_STATE_VERSION);
99 }
100
101 bool RestoreHeaderFromPickle(PickleIterator* iterator) {
102 uint32 state_version = -1;
103 if (!iterator->ReadUInt32(&state_version))
104 return false;
105
106 if (AW_STATE_VERSION != state_version)
107 return false;
108
109 return true;
110 }
111
112 bool WriteNavigationEntryToPickle(const content::NavigationEntry& entry,
113 Pickle* pickle) {
114 if (!pickle->WriteString(entry.GetURL().spec()))
115 return false;
116
117 if (!pickle->WriteString(entry.GetVirtualURL().spec()))
118 return false;
119
120 const content::Referrer& referrer = entry.GetReferrer();
121 if (!pickle->WriteString(referrer.url.spec()))
122 return false;
123 if (!pickle->WriteInt(static_cast<int>(referrer.policy)))
124 return false;
125
126 if (!pickle->WriteString16(entry.GetTitle()))
127 return false;
128
129 if (!pickle->WriteString(entry.GetContentState()))
130 return false;
131
132 if (!pickle->WriteBool(static_cast<int>(entry.GetHasPostData())))
133 return false;
134
135 if (!pickle->WriteString(entry.GetOriginalRequestURL().spec()))
136 return false;
137
138 if (!pickle->WriteBool(static_cast<int>(entry.GetIsOverridingUserAgent())))
139 return false;
140
141 if (!pickle->WriteInt64(entry.GetTimestamp().ToInternalValue()))
142 return false;
143
144 return true;
145 }
146
147 bool RestoreNavigationEntryFromPickle(PickleIterator* iterator,
148 content::NavigationEntry* entry) {
mkosiba (inactive) 2012/11/20 17:40:57 I bet we already have code for serializing Navigat
boliu 2012/11/20 19:35:38 Yes we do. It's TabNavigation in chrome/browser/se
joth 2012/11/20 19:51:02 May even be worth a block comment at top of this f
boliu 2012/11/20 22:18:16 Done.
149 {
150 string url;
151 if (!iterator->ReadString(&url))
152 return false;
153 entry->SetURL(GURL(url));
154 }
155
156 {
157 string virtual_url;
158 if (!iterator->ReadString(&virtual_url))
159 return false;
160 entry->SetVirtualURL(GURL(virtual_url));
161 }
162
163 {
164 content::Referrer referrer;
165 string referrer_url;
166 int policy;
167
168 if (!iterator->ReadString(&referrer_url))
169 return false;
170 if (!iterator->ReadInt(&policy))
171 return false;
172
173 referrer.url = GURL(referrer_url);
174 referrer.policy = static_cast<WebKit::WebReferrerPolicy>(policy);
175 entry->SetReferrer(referrer);
176 }
177
178 {
179 string16 title;
180 if (!iterator->ReadString16(&title))
181 return false;
182 entry->SetTitle(title);
183 }
184
185 {
186 string content_state;
187 if (!iterator->ReadString(&content_state))
188 return false;
189 entry->SetContentState(content_state);
190 }
191
192 {
193 bool has_post_data;
194 if (!iterator->ReadBool(&has_post_data))
195 return false;
196 entry->SetHasPostData(has_post_data);
197 }
198
199 {
200 string original_request_url;
201 if (!iterator->ReadString(&original_request_url))
202 return false;
203 entry->SetOriginalRequestURL(GURL(original_request_url));
204 }
205
206 {
207 bool is_overriding_user_agent;
208 if (!iterator->ReadBool(&is_overriding_user_agent))
209 return false;
210 entry->SetIsOverridingUserAgent(is_overriding_user_agent);
211 }
212
213 {
214 int64 timestamp;
215 if (!iterator->ReadInt64(&timestamp))
216 return false;
217 entry->SetTimestamp(base::Time::FromInternalValue(timestamp));
joth 2012/11/20 19:51:02 wild idea: as we don't need to support reading the
boliu 2012/11/20 21:29:38 Ignoring the fact that this is totally abusing IPC
218 }
219
220 return true;
221 }
222
223 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698