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.
5 #include "android_webview/native/state_serializer.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/pickle.h"
11 #include "base/time/time.h"
12 #include "content/public/browser/child_process_security_policy.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/page_state.h"
19 // Reasons for not re-using TabNavigation under chrome/ as of 20121116:
20 // * Android WebView has different requirements for fields to store since
21 // we are the only ones using values like BaseURLForDataURL.
22 // * TabNavigation does unnecessary copying of data, which in Android
23 // WebView case, is undesired since save/restore is called in Android
25 // * TabNavigation is tightly integrated with the rest of chrome session
26 // restore and sync code, and has other purpose in addition to serializing
31 namespace android_webview
{
35 // Sanity check value that we are restoring from a valid pickle.
36 // This can potentially used as an actual serialization version number in the
37 // future if we ever decide to support restoring from older versions.
38 const uint32 AW_STATE_VERSION
= 20130814;
42 bool WriteToPickle(const content::WebContents
& web_contents
,
46 if (!internal::WriteHeaderToPickle(pickle
))
49 const content::NavigationController
& controller
=
50 web_contents
.GetController();
51 const int entry_count
= controller
.GetEntryCount();
52 const int selected_entry
= controller
.GetCurrentEntryIndex();
53 DCHECK(entry_count
>= 0);
54 DCHECK(selected_entry
>= -1); // -1 is valid
55 DCHECK(selected_entry
< entry_count
);
57 if (!pickle
->WriteInt(entry_count
))
60 if (!pickle
->WriteInt(selected_entry
))
63 for (int i
= 0; i
< entry_count
; ++i
) {
64 if (!internal::WriteNavigationEntryToPickle(*controller
.GetEntryAtIndex(i
),
69 // Please update AW_STATE_VERSION if serialization format is changed.
74 bool RestoreFromPickle(PickleIterator
* iterator
,
75 content::WebContents
* web_contents
) {
79 if (!internal::RestoreHeaderFromPickle(iterator
))
83 int selected_entry
= -2; // -1 is a valid value
85 if (!iterator
->ReadInt(&entry_count
))
88 if (!iterator
->ReadInt(&selected_entry
))
93 if (selected_entry
< -1)
95 if (selected_entry
>= entry_count
)
98 ScopedVector
<content::NavigationEntry
> restored_entries
;
99 for (int i
= 0; i
< entry_count
; ++i
) {
100 restored_entries
.push_back(content::NavigationEntry::Create());
101 if (!internal::RestoreNavigationEntryFromPickle(iterator
,
102 restored_entries
[i
]))
105 restored_entries
[i
]->SetPageID(i
);
108 // |web_contents| takes ownership of these entries after this call.
109 content::NavigationController
& controller
= web_contents
->GetController();
112 content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY
,
113 &restored_entries
.get());
114 DCHECK_EQ(0u, restored_entries
.size());
116 if (controller
.GetActiveEntry()) {
117 // Set up the file access rights for the selected navigation entry.
118 // TODO(joth): This is duplicated from chrome/.../session_restore.cc and
119 // should be shared e.g. in NavigationController. http://crbug.com/68222
120 const int id
= web_contents
->GetRenderProcessHost()->GetID();
121 const content::PageState
& page_state
=
122 controller
.GetActiveEntry()->GetPageState();
123 const std::vector
<base::FilePath
>& file_paths
=
124 page_state
.GetReferencedFiles();
125 for (std::vector
<base::FilePath
>::const_iterator file
= file_paths
.begin();
126 file
!= file_paths
.end(); ++file
) {
127 content::ChildProcessSecurityPolicy::GetInstance()->GrantReadFile(id
,
132 controller
.LoadIfNecessary();
139 bool WriteHeaderToPickle(Pickle
* pickle
) {
140 return pickle
->WriteUInt32(AW_STATE_VERSION
);
143 bool RestoreHeaderFromPickle(PickleIterator
* iterator
) {
144 uint32 state_version
= -1;
145 if (!iterator
->ReadUInt32(&state_version
))
148 if (AW_STATE_VERSION
!= state_version
)
154 bool WriteNavigationEntryToPickle(const content::NavigationEntry
& entry
,
156 if (!pickle
->WriteString(entry
.GetURL().spec()))
159 if (!pickle
->WriteString(entry
.GetVirtualURL().spec()))
162 const content::Referrer
& referrer
= entry
.GetReferrer();
163 if (!pickle
->WriteString(referrer
.url
.spec()))
165 if (!pickle
->WriteInt(static_cast<int>(referrer
.policy
)))
168 if (!pickle
->WriteString16(entry
.GetTitle()))
171 if (!pickle
->WriteString(entry
.GetPageState().ToEncodedData()))
174 if (!pickle
->WriteBool(static_cast<int>(entry
.GetHasPostData())))
177 if (!pickle
->WriteString(entry
.GetOriginalRequestURL().spec()))
180 if (!pickle
->WriteString(entry
.GetBaseURLForDataURL().spec()))
183 if (!pickle
->WriteBool(static_cast<int>(entry
.GetIsOverridingUserAgent())))
186 if (!pickle
->WriteInt64(entry
.GetTimestamp().ToInternalValue()))
189 if (!pickle
->WriteInt(entry
.GetHttpStatusCode()))
192 // Please update AW_STATE_VERSION if serialization format is changed.
197 bool RestoreNavigationEntryFromPickle(PickleIterator
* iterator
,
198 content::NavigationEntry
* entry
) {
201 if (!iterator
->ReadString(&url
))
203 entry
->SetURL(GURL(url
));
208 if (!iterator
->ReadString(&virtual_url
))
210 entry
->SetVirtualURL(GURL(virtual_url
));
214 content::Referrer referrer
;
218 if (!iterator
->ReadString(&referrer_url
))
220 if (!iterator
->ReadInt(&policy
))
223 referrer
.url
= GURL(referrer_url
);
224 referrer
.policy
= static_cast<blink::WebReferrerPolicy
>(policy
);
225 entry
->SetReferrer(referrer
);
229 base::string16 title
;
230 if (!iterator
->ReadString16(&title
))
232 entry
->SetTitle(title
);
236 string content_state
;
237 if (!iterator
->ReadString(&content_state
))
240 content::PageState::CreateFromEncodedData(content_state
));
245 if (!iterator
->ReadBool(&has_post_data
))
247 entry
->SetHasPostData(has_post_data
);
251 string original_request_url
;
252 if (!iterator
->ReadString(&original_request_url
))
254 entry
->SetOriginalRequestURL(GURL(original_request_url
));
258 string base_url_for_data_url
;
259 if (!iterator
->ReadString(&base_url_for_data_url
))
261 entry
->SetBaseURLForDataURL(GURL(base_url_for_data_url
));
265 bool is_overriding_user_agent
;
266 if (!iterator
->ReadBool(&is_overriding_user_agent
))
268 entry
->SetIsOverridingUserAgent(is_overriding_user_agent
);
273 if (!iterator
->ReadInt64(×tamp
))
275 entry
->SetTimestamp(base::Time::FromInternalValue(timestamp
));
279 int http_status_code
;
280 if (!iterator
->ReadInt(&http_status_code
))
282 entry
->SetHttpStatusCode(http_status_code
);
288 } // namespace internal
290 } // namespace android_webview