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

Side by Side Diff: chrome/browser/android/tab_state.cc

Issue 1154283003: Change most uses of Pickle to base::Pickle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/android/tab_state.h" 5 #include "chrome/browser/android/tab_state.h"
6 6
7 #include <jni.h> 7 #include <jni.h>
8 #include <limits> 8 #include <limits>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 15 matching lines...) Expand all
26 #include "jni/TabState_jni.h" 26 #include "jni/TabState_jni.h"
27 27
28 using base::android::ConvertUTF16ToJavaString; 28 using base::android::ConvertUTF16ToJavaString;
29 using base::android::ConvertUTF8ToJavaString; 29 using base::android::ConvertUTF8ToJavaString;
30 using base::android::ScopedJavaLocalRef; 30 using base::android::ScopedJavaLocalRef;
31 using content::NavigationController; 31 using content::NavigationController;
32 using content::WebContents; 32 using content::WebContents;
33 33
34 namespace { 34 namespace {
35 35
36 bool WriteStateHeaderToPickle(bool off_the_record, int entry_count, 36 bool WriteStateHeaderToPickle(bool off_the_record,
37 int current_entry_index, Pickle* pickle) { 37 int entry_count,
38 int current_entry_index,
39 base::Pickle* pickle) {
38 return pickle->WriteBool(off_the_record) && 40 return pickle->WriteBool(off_the_record) &&
39 pickle->WriteInt(entry_count) && 41 pickle->WriteInt(entry_count) &&
40 pickle->WriteInt(current_entry_index); 42 pickle->WriteInt(current_entry_index);
41 } 43 }
42 44
43 // Migrates a pickled SerializedNavigationEntry from Android tab version 0 to 45 // Migrates a pickled SerializedNavigationEntry from Android tab version 0 to
44 // 2 or (Chrome 18->26). 46 // 2 or (Chrome 18->26).
45 // 47 //
46 // Due to the fact that all SerializedNavigationEntrys were previously stored 48 // Due to the fact that all SerializedNavigationEntrys were previously stored
47 // in a single pickle on Android, this function has to read the fields exactly 49 // in a single pickle on Android, this function has to read the fields exactly
(...skipping 11 matching lines...) Expand all
59 // transition_type 61 // transition_type
60 // type_mask 62 // type_mask
61 // 63 //
62 // 2. For each tab navigation: 64 // 2. For each tab navigation:
63 // referrer 65 // referrer
64 // is_overriding_user_agent 66 // is_overriding_user_agent
65 // 67 //
66 void UpgradeNavigationFromV0ToV2( 68 void UpgradeNavigationFromV0ToV2(
67 std::vector<sessions::SerializedNavigationEntry>* navigations, 69 std::vector<sessions::SerializedNavigationEntry>* navigations,
68 int entry_count, 70 int entry_count,
69 PickleIterator* iterator) { 71 base::PickleIterator* iterator) {
70
71 for (int i = 0; i < entry_count; ++i) { 72 for (int i = 0; i < entry_count; ++i) {
72 Pickle v2_pickle; 73 base::Pickle v2_pickle;
73 std::string virtual_url_spec; 74 std::string virtual_url_spec;
74 std::string str_referrer; 75 std::string str_referrer;
75 base::string16 title; 76 base::string16 title;
76 std::string content_state; 77 std::string content_state;
77 int transition_type_int; 78 int transition_type_int;
78 if (!iterator->ReadString(&virtual_url_spec) || 79 if (!iterator->ReadString(&virtual_url_spec) ||
79 !iterator->ReadString(&str_referrer) || 80 !iterator->ReadString(&str_referrer) ||
80 !iterator->ReadString16(&title) || 81 !iterator->ReadString16(&title) ||
81 !iterator->ReadString(&content_state) || 82 !iterator->ReadString(&content_state) ||
82 !iterator->ReadInt(&transition_type_int)) 83 !iterator->ReadInt(&transition_type_int))
(...skipping 14 matching lines...) Expand all
97 v2_pickle.WriteInt(0); 98 v2_pickle.WriteInt(0);
98 // original_request_url_spec 99 // original_request_url_spec
99 v2_pickle.WriteString(std::string()); 100 v2_pickle.WriteString(std::string());
100 // is_overriding_user_agent 101 // is_overriding_user_agent
101 v2_pickle.WriteBool(false); 102 v2_pickle.WriteBool(false);
102 // timestamp_internal_value 103 // timestamp_internal_value
103 v2_pickle.WriteInt64(0); 104 v2_pickle.WriteInt64(0);
104 // search_terms 105 // search_terms
105 v2_pickle.WriteString16(base::string16()); 106 v2_pickle.WriteString16(base::string16());
106 107
107 PickleIterator tab_navigation_pickle_iterator(v2_pickle); 108 base::PickleIterator tab_navigation_pickle_iterator(v2_pickle);
108 sessions::SerializedNavigationEntry nav; 109 sessions::SerializedNavigationEntry nav;
109 if (nav.ReadFromPickle(&tab_navigation_pickle_iterator)) { 110 if (nav.ReadFromPickle(&tab_navigation_pickle_iterator)) {
110 navigations->push_back(nav); 111 navigations->push_back(nav);
111 } else { 112 } else {
112 LOG(ERROR) << "Failed to read SerializedNavigationEntry from pickle " 113 LOG(ERROR) << "Failed to read SerializedNavigationEntry from pickle "
113 << "(index=" << i << ", url=" << virtual_url_spec; 114 << "(index=" << i << ", url=" << virtual_url_spec;
114 } 115 }
115 116
116 } 117 }
117 118
(...skipping 27 matching lines...) Expand all
145 // type_mask 146 // type_mask
146 // referrer 147 // referrer
147 // original_request_url 148 // original_request_url
148 // is_overriding_user_agent 149 // is_overriding_user_agent
149 // timestamp 150 // timestamp
150 // 151 //
151 // And finally search_terms was added and this function appends it. 152 // And finally search_terms was added and this function appends it.
152 void UpgradeNavigationFromV1ToV2( 153 void UpgradeNavigationFromV1ToV2(
153 std::vector<sessions::SerializedNavigationEntry>* navigations, 154 std::vector<sessions::SerializedNavigationEntry>* navigations,
154 int entry_count, 155 int entry_count,
155 PickleIterator* iterator) { 156 base::PickleIterator* iterator) {
156 for (int i = 0; i < entry_count; ++i) { 157 for (int i = 0; i < entry_count; ++i) {
157 Pickle v2_pickle; 158 base::Pickle v2_pickle;
158 159
159 int index; 160 int index;
160 std::string virtual_url_spec; 161 std::string virtual_url_spec;
161 base::string16 title; 162 base::string16 title;
162 std::string content_state; 163 std::string content_state;
163 int transition_type_int; 164 int transition_type_int;
164 if (!iterator->ReadInt(&index) || 165 if (!iterator->ReadInt(&index) ||
165 !iterator->ReadString(&virtual_url_spec) || 166 !iterator->ReadString(&virtual_url_spec) ||
166 !iterator->ReadString16(&title) || 167 !iterator->ReadString16(&title) ||
167 !iterator->ReadString(&content_state) || 168 !iterator->ReadString(&content_state) ||
(...skipping 28 matching lines...) Expand all
196 if (iterator->ReadBool(&is_overriding_user_agent)) 197 if (iterator->ReadBool(&is_overriding_user_agent))
197 v2_pickle.WriteBool(is_overriding_user_agent); 198 v2_pickle.WriteBool(is_overriding_user_agent);
198 199
199 int64 timestamp_internal_value = 0; 200 int64 timestamp_internal_value = 0;
200 if (iterator->ReadInt64(&timestamp_internal_value)) 201 if (iterator->ReadInt64(&timestamp_internal_value))
201 v2_pickle.WriteInt64(timestamp_internal_value); 202 v2_pickle.WriteInt64(timestamp_internal_value);
202 203
203 // Force output of search_terms 204 // Force output of search_terms
204 v2_pickle.WriteString16(base::string16()); 205 v2_pickle.WriteString16(base::string16());
205 206
206 PickleIterator tab_navigation_pickle_iterator(v2_pickle); 207 base::PickleIterator tab_navigation_pickle_iterator(v2_pickle);
207 sessions::SerializedNavigationEntry nav; 208 sessions::SerializedNavigationEntry nav;
208 if (nav.ReadFromPickle(&tab_navigation_pickle_iterator)) { 209 if (nav.ReadFromPickle(&tab_navigation_pickle_iterator)) {
209 navigations->push_back(nav); 210 navigations->push_back(nav);
210 } else { 211 } else {
211 LOG(ERROR) << "Failed to read SerializedNavigationEntry from pickle " 212 LOG(ERROR) << "Failed to read SerializedNavigationEntry from pickle "
212 << "(index=" << i << ", url=" << virtual_url_spec; 213 << "(index=" << i << ", url=" << virtual_url_spec;
213 } 214 }
214 } 215 }
215 } 216 }
216 217
217 // Extracts state and navigation entries from the given Pickle data and returns 218 // Extracts state and navigation entries from the given Pickle data and returns
218 // whether un-pickling the data succeeded 219 // whether un-pickling the data succeeded
219 bool ExtractNavigationEntries( 220 bool ExtractNavigationEntries(
220 void* data, 221 void* data,
221 int size, 222 int size,
222 int saved_state_version, 223 int saved_state_version,
223 bool* is_off_the_record, 224 bool* is_off_the_record,
224 int* current_entry_index, 225 int* current_entry_index,
225 std::vector<sessions::SerializedNavigationEntry>* navigations) { 226 std::vector<sessions::SerializedNavigationEntry>* navigations) {
226 int entry_count; 227 int entry_count;
227 Pickle pickle(static_cast<char*>(data), size); 228 base::Pickle pickle(static_cast<char*>(data), size);
228 PickleIterator iter(pickle); 229 base::PickleIterator iter(pickle);
229 if (!iter.ReadBool(is_off_the_record) || !iter.ReadInt(&entry_count) || 230 if (!iter.ReadBool(is_off_the_record) || !iter.ReadInt(&entry_count) ||
230 !iter.ReadInt(current_entry_index)) { 231 !iter.ReadInt(current_entry_index)) {
231 LOG(ERROR) << "Failed to restore state from byte array (length=" << size 232 LOG(ERROR) << "Failed to restore state from byte array (length=" << size
232 << ")."; 233 << ").";
233 return false; 234 return false;
234 } 235 }
235 236
236 if (!saved_state_version) { 237 if (!saved_state_version) {
237 // When |saved_state_version| is 0, it predates our notion of each tab 238 // When |saved_state_version| is 0, it predates our notion of each tab
238 // having a saved version id. For that version of tab serialization, we 239 // having a saved version id. For that version of tab serialization, we
(...skipping 12 matching lines...) Expand all
251 int tab_navigation_data_length = 0; 252 int tab_navigation_data_length = 0;
252 const char* tab_navigation_data = NULL; 253 const char* tab_navigation_data = NULL;
253 if (!iter.ReadInt(&tab_navigation_data_length) || 254 if (!iter.ReadInt(&tab_navigation_data_length) ||
254 !iter.ReadBytes(&tab_navigation_data, tab_navigation_data_length)) { 255 !iter.ReadBytes(&tab_navigation_data, tab_navigation_data_length)) {
255 LOG(ERROR) 256 LOG(ERROR)
256 << "Failed to restore tab entry from byte array. " 257 << "Failed to restore tab entry from byte array. "
257 << "(SerializedNavigationEntry size=" << tab_navigation_data_length 258 << "(SerializedNavigationEntry size=" << tab_navigation_data_length
258 << ")."; 259 << ").";
259 return false; // It's dangerous to keep deserializing now, give up. 260 return false; // It's dangerous to keep deserializing now, give up.
260 } 261 }
261 Pickle tab_navigation_pickle(tab_navigation_data, 262 base::Pickle tab_navigation_pickle(tab_navigation_data,
262 tab_navigation_data_length); 263 tab_navigation_data_length);
263 PickleIterator tab_navigation_pickle_iterator(tab_navigation_pickle); 264 base::PickleIterator tab_navigation_pickle_iterator(
265 tab_navigation_pickle);
264 sessions::SerializedNavigationEntry nav; 266 sessions::SerializedNavigationEntry nav;
265 if (!nav.ReadFromPickle(&tab_navigation_pickle_iterator)) 267 if (!nav.ReadFromPickle(&tab_navigation_pickle_iterator))
266 return false; // If we failed to read a navigation, give up on others. 268 return false; // If we failed to read a navigation, give up on others.
267 269
268 navigations->push_back(nav); 270 navigations->push_back(nav);
269 } 271 }
270 } 272 }
271 273
272 // Validate the data. 274 // Validate the data.
273 if (*current_entry_index < 0 || 275 if (*current_entry_index < 0 ||
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 } 316 }
315 317
316 // Common implementation for GetContentsStateAsByteBuffer() and 318 // Common implementation for GetContentsStateAsByteBuffer() and
317 // CreateContentsStateAsByteBuffer(). Does not assume ownership of the 319 // CreateContentsStateAsByteBuffer(). Does not assume ownership of the
318 // navigations. 320 // navigations.
319 ScopedJavaLocalRef<jobject> WebContentsState::WriteNavigationsAsByteBuffer( 321 ScopedJavaLocalRef<jobject> WebContentsState::WriteNavigationsAsByteBuffer(
320 JNIEnv* env, 322 JNIEnv* env,
321 bool is_off_the_record, 323 bool is_off_the_record,
322 const std::vector<content::NavigationEntry*>& navigations, 324 const std::vector<content::NavigationEntry*>& navigations,
323 int current_entry) { 325 int current_entry) {
324 Pickle pickle; 326 base::Pickle pickle;
325 if (!WriteStateHeaderToPickle(is_off_the_record, navigations.size(), 327 if (!WriteStateHeaderToPickle(is_off_the_record, navigations.size(),
326 current_entry, &pickle)) { 328 current_entry, &pickle)) {
327 LOG(ERROR) << "Failed to serialize tab state (entry count=" << 329 LOG(ERROR) << "Failed to serialize tab state (entry count=" <<
328 navigations.size() << ")."; 330 navigations.size() << ").";
329 return ScopedJavaLocalRef<jobject>(); 331 return ScopedJavaLocalRef<jobject>();
330 } 332 }
331 333
332 // Write out all of the NavigationEntrys. 334 // Write out all of the NavigationEntrys.
333 for (size_t i = 0; i < navigations.size(); ++i) { 335 for (size_t i = 0; i < navigations.size(); ++i) {
334 // Write each SerializedNavigationEntry as a separate pickle to avoid 336 // Write each SerializedNavigationEntry as a separate pickle to avoid
335 // optional reads of one tab bleeding into the next tab's data. 337 // optional reads of one tab bleeding into the next tab's data.
336 Pickle tab_navigation_pickle; 338 base::Pickle tab_navigation_pickle;
337 // Max size taken from BaseSessionService::CreateUpdateTabNavigationCommand. 339 // Max size taken from BaseSessionService::CreateUpdateTabNavigationCommand.
338 static const size_t max_state_size = 340 static const size_t max_state_size =
339 std::numeric_limits<sessions::SessionCommand::size_type>::max() - 1024; 341 std::numeric_limits<sessions::SessionCommand::size_type>::max() - 1024;
340 sessions::ContentSerializedNavigationBuilder::FromNavigationEntry( 342 sessions::ContentSerializedNavigationBuilder::FromNavigationEntry(
341 i, *navigations[i]) 343 i, *navigations[i])
342 .WriteToPickle(max_state_size, &tab_navigation_pickle); 344 .WriteToPickle(max_state_size, &tab_navigation_pickle);
343 pickle.WriteInt(tab_navigation_pickle.size()); 345 pickle.WriteInt(tab_navigation_pickle.size());
344 pickle.WriteBytes(tab_navigation_pickle.data(), 346 pickle.WriteBytes(tab_navigation_pickle.data(),
345 tab_navigation_pickle.size()); 347 tab_navigation_pickle.size());
346 } 348 }
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 state, 565 state,
564 saved_state_version, 566 saved_state_version,
565 true)); 567 true));
566 if (web_contents.get()) 568 if (web_contents.get())
567 TabAndroid::CreateHistoricalTabFromContents(web_contents.get()); 569 TabAndroid::CreateHistoricalTabFromContents(web_contents.get());
568 } 570 }
569 571
570 bool RegisterTabState(JNIEnv* env) { 572 bool RegisterTabState(JNIEnv* env) {
571 return RegisterNativesImpl(env); 573 return RegisterNativesImpl(env);
572 } 574 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698