| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "webkit/glue/glue_serialize_deprecated.h" | 5 #include "content/common/page_state_serialization.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <algorithm> |
| 8 #include <limits> |
| 8 | 9 |
| 9 #include "base/pickle.h" | 10 #include "base/pickle.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 11 #include "googleurl/src/gurl.h" | |
| 12 #include "third_party/WebKit/public/platform/WebData.h" | |
| 13 #include "third_party/WebKit/public/platform/WebHTTPBody.h" | |
| 14 #include "third_party/WebKit/public/platform/WebPoint.h" | |
| 15 #include "third_party/WebKit/public/platform/WebString.h" | |
| 16 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 17 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 18 #include "third_party/WebKit/public/web/WebHistoryItem.h" | |
| 19 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | |
| 20 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
| 21 #include "webkit/base/file_path_string_conversions.h" | 15 |
| 22 | 16 namespace content { |
| 23 using WebKit::WebData; | |
| 24 using WebKit::WebHistoryItem; | |
| 25 using WebKit::WebHTTPBody; | |
| 26 using WebKit::WebPoint; | |
| 27 using WebKit::WebSerializedScriptValue; | |
| 28 using WebKit::WebString; | |
| 29 using WebKit::WebUChar; | |
| 30 using WebKit::WebVector; | |
| 31 | |
| 32 namespace webkit_glue { | |
| 33 | |
| 34 namespace { | 17 namespace { |
| 35 | 18 |
| 36 enum IncludeFormData { | 19 //----------------------------------------------------------------------------- |
| 37 NEVER_INCLUDE_FORM_DATA, | 20 |
| 38 INCLUDE_FORM_DATA_WITHOUT_PASSWORDS, | 21 void AppendDataToHttpBody(ExplodedHttpBody* http_body, const char* data, |
| 39 ALWAYS_INCLUDE_FORM_DATA | 22 int data_length) { |
| 40 }; | 23 ExplodedHttpBodyElement element; |
| 24 element.type = WebKit::WebHTTPBody::Element::TypeData; |
| 25 element.data.assign(data, data_length); |
| 26 http_body->elements.push_back(element); |
| 27 } |
| 28 |
| 29 void AppendFileRangeToHttpBody(ExplodedHttpBody* http_body, |
| 30 const base::NullableString16& file_path, |
| 31 int file_start, |
| 32 int file_length, |
| 33 double file_modification_time) { |
| 34 ExplodedHttpBodyElement element; |
| 35 element.type = WebKit::WebHTTPBody::Element::TypeFile; |
| 36 element.file_path = file_path; |
| 37 element.file_start = file_start; |
| 38 element.file_length = file_length; |
| 39 element.file_modification_time = file_modification_time; |
| 40 http_body->elements.push_back(element); |
| 41 } |
| 42 |
| 43 void AppendURLRangeToHttpBody(ExplodedHttpBody* http_body, |
| 44 const GURL& url, |
| 45 int file_start, |
| 46 int file_length, |
| 47 double file_modification_time) { |
| 48 ExplodedHttpBodyElement element; |
| 49 element.type = WebKit::WebHTTPBody::Element::TypeURL; |
| 50 element.url = url; |
| 51 element.file_start = file_start; |
| 52 element.file_length = file_length; |
| 53 element.file_modification_time = file_modification_time; |
| 54 http_body->elements.push_back(element); |
| 55 } |
| 56 |
| 57 void AppendBlobToHttpBody(ExplodedHttpBody* http_body, const GURL& url) { |
| 58 ExplodedHttpBodyElement element; |
| 59 element.type = WebKit::WebHTTPBody::Element::TypeBlob; |
| 60 element.url = url; |
| 61 http_body->elements.push_back(element); |
| 62 } |
| 63 |
| 64 //---------------------------------------------------------------------------- |
| 65 |
| 66 void AppendReferencedFilesFromHttpBody( |
| 67 const std::vector<ExplodedHttpBodyElement>& elements, |
| 68 std::vector<base::NullableString16>* referenced_files) { |
| 69 for (size_t i = 0; i < elements.size(); ++i) { |
| 70 if (elements[i].type == WebKit::WebHTTPBody::Element::TypeFile) |
| 71 referenced_files->push_back(elements[i].file_path); |
| 72 } |
| 73 } |
| 74 |
| 75 bool AppendReferencedFilesFromDocumentState( |
| 76 const std::vector<base::NullableString16>& document_state, |
| 77 std::vector<base::NullableString16>* referenced_files) { |
| 78 if (document_state.empty()) |
| 79 return true; |
| 80 |
| 81 // This algorithm is adapted from Blink's core/html/FormController.cpp code. |
| 82 // We only care about how that code worked when this code snapshot was taken |
| 83 // as this code is only needed for backwards compat. |
| 84 // |
| 85 // For reference, see FormController::formStatesFromStateVector at: |
| 86 // http://src.chromium.org/viewvc/blink/trunk/Source/core/html/FormController.
cpp?pathrev=152274 |
| 87 |
| 88 size_t index = 0; |
| 89 |
| 90 if (document_state.size() < 3) |
| 91 return false; |
| 92 |
| 93 index++; // Skip over magic signature. |
| 94 index++; // Skip over form key. |
| 95 |
| 96 size_t item_count; |
| 97 if (!base::StringToSizeT(document_state[index++].string(), &item_count)) |
| 98 return false; |
| 99 |
| 100 while (item_count--) { |
| 101 if (index + 1 >= document_state.size()) |
| 102 return false; |
| 103 |
| 104 index++; // Skip over name. |
| 105 const base::NullableString16& type = document_state[index++]; |
| 106 |
| 107 if (index >= document_state.size()) |
| 108 return false; |
| 109 |
| 110 size_t value_size; |
| 111 if (!base::StringToSizeT(document_state[index++].string(), &value_size)) |
| 112 return false; |
| 113 |
| 114 if (index + value_size > document_state.size() || |
| 115 index + value_size < index) // Check for overflow. |
| 116 return false; |
| 117 |
| 118 if (EqualsASCII(type.string(), "file")) { |
| 119 if (value_size != 2) |
| 120 return false; |
| 121 |
| 122 referenced_files->push_back(document_state[index++]); |
| 123 index++; // Skip over display name. |
| 124 } else { |
| 125 index += value_size; |
| 126 } |
| 127 } |
| 128 |
| 129 return true; |
| 130 } |
| 131 |
| 132 bool RecursivelyAppendReferencedFiles( |
| 133 const ExplodedFrameState& frame_state, |
| 134 std::vector<base::NullableString16>* referenced_files) { |
| 135 if (!frame_state.http_body.is_null) { |
| 136 AppendReferencedFilesFromHttpBody(frame_state.http_body.elements, |
| 137 referenced_files); |
| 138 } |
| 139 |
| 140 if (!AppendReferencedFilesFromDocumentState(frame_state.document_state, |
| 141 referenced_files)) |
| 142 return false; |
| 143 |
| 144 for (size_t i = 0; i < frame_state.children.size(); ++i) { |
| 145 if (!RecursivelyAppendReferencedFiles(frame_state.children[i], |
| 146 referenced_files)) |
| 147 return false; |
| 148 } |
| 149 |
| 150 return true; |
| 151 } |
| 152 |
| 153 //---------------------------------------------------------------------------- |
| 41 | 154 |
| 42 struct SerializeObject { | 155 struct SerializeObject { |
| 43 SerializeObject() : version(0) {} | 156 SerializeObject() |
| 157 : version(0), |
| 158 parse_error(false) { |
| 159 } |
| 160 |
| 44 SerializeObject(const char* data, int len) | 161 SerializeObject(const char* data, int len) |
| 45 : pickle(data, len), version(0) { iter = PickleIterator(pickle); } | 162 : pickle(data, len), |
| 163 version(0), |
| 164 parse_error(false) { |
| 165 iter = PickleIterator(pickle); |
| 166 } |
| 46 | 167 |
| 47 std::string GetAsString() { | 168 std::string GetAsString() { |
| 48 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); | 169 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); |
| 49 } | 170 } |
| 50 | 171 |
| 51 Pickle pickle; | 172 Pickle pickle; |
| 52 mutable PickleIterator iter; | 173 PickleIterator iter; |
| 53 mutable int version; | 174 int version; |
| 175 bool parse_error; |
| 54 }; | 176 }; |
| 55 | 177 |
| 56 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. | 178 // Version ID of serialized format. |
| 57 // Version ID used in reading/writing history items. | 179 // 11: Min version |
| 58 // 1: Initial revision. | 180 // 12: Adds support for contains_passwords in HTTP body |
| 59 // 2: Added case for NULL string versus "". Version 2 code can read Version 1 | |
| 60 // data, but not vice versa. | |
| 61 // 3: Version 2 was broken, it stored number of WebUChars, not number of bytes. | |
| 62 // This version checks and reads v1 and v2 correctly. | |
| 63 // 4: Adds support for storing FormData::identifier(). | |
| 64 // 5: Adds support for empty FormData | |
| 65 // 6: Adds support for documentSequenceNumbers | |
| 66 // 7: Adds support for stateObject | |
| 67 // 8: Adds support for file range and modification time | |
| 68 // 9: Adds support for itemSequenceNumbers | |
| 69 // 10: Adds support for blob | |
| 70 // 11: Adds support for pageScaleFactor | |
| 71 // 12: Adds support for hasPasswordData in HTTP body | |
| 72 // 13: Adds support for URL (FileSystem URL) | 181 // 13: Adds support for URL (FileSystem URL) |
| 73 // 14: Adds list of referenced files, version written only for first item. | 182 // 14: Adds list of referenced files, version written only for first item. |
| 74 // Should be const, but unit tests may modify it. | |
| 75 // | 183 // |
| 76 // NOTE: If the version is -1, then the pickle contains only a URL string. | 184 // NOTE: If the version is -1, then the pickle contains only a URL string. |
| 77 // See CreateHistoryStateForURL. | 185 // See ReadPageState. |
| 78 // | 186 // |
| 79 int kVersion = 14; | 187 const int kMinVersion = 11; |
| 80 | 188 const int kCurrentVersion = 14; |
| 81 // A bunch of convenience functions to read/write to SerializeObjects. | 189 |
| 82 // The serializers assume the input data is in the correct format and so does | 190 // A bunch of convenience functions to read/write to SerializeObjects. The |
| 83 // no error checking. | 191 // de-serializers assume the input data will be in the correct format and fall |
| 192 // back to returning safe defaults when not. |
| 193 |
| 84 void WriteData(const void* data, int length, SerializeObject* obj) { | 194 void WriteData(const void* data, int length, SerializeObject* obj) { |
| 85 obj->pickle.WriteData(static_cast<const char*>(data), length); | 195 obj->pickle.WriteData(static_cast<const char*>(data), length); |
| 86 } | 196 } |
| 87 | 197 |
| 88 void ReadData(const SerializeObject* obj, const void** data, int* length) { | 198 void ReadData(SerializeObject* obj, const void** data, int* length) { |
| 89 const char* tmp; | 199 const char* tmp; |
| 90 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { | 200 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { |
| 91 *data = tmp; | 201 *data = tmp; |
| 92 } else { | 202 } else { |
| 203 obj->parse_error = true; |
| 93 *data = NULL; | 204 *data = NULL; |
| 94 *length = 0; | 205 *length = 0; |
| 95 } | 206 } |
| 96 } | 207 } |
| 97 | 208 |
| 98 bool ReadBytes(const SerializeObject* obj, const void** data, int length) { | |
| 99 const char *tmp; | |
| 100 if (!obj->pickle.ReadBytes(&obj->iter, &tmp, length)) | |
| 101 return false; | |
| 102 *data = tmp; | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 void WriteInteger(int data, SerializeObject* obj) { | 209 void WriteInteger(int data, SerializeObject* obj) { |
| 107 obj->pickle.WriteInt(data); | 210 obj->pickle.WriteInt(data); |
| 108 } | 211 } |
| 109 | 212 |
| 110 int ReadInteger(const SerializeObject* obj) { | 213 int ReadInteger(SerializeObject* obj) { |
| 111 int tmp; | 214 int tmp; |
| 112 if (obj->pickle.ReadInt(&obj->iter, &tmp)) | 215 if (obj->pickle.ReadInt(&obj->iter, &tmp)) |
| 113 return tmp; | 216 return tmp; |
| 217 obj->parse_error = true; |
| 114 return 0; | 218 return 0; |
| 115 } | 219 } |
| 116 | 220 |
| 117 void ConsumeInteger(const SerializeObject* obj) { | 221 void ConsumeInteger(SerializeObject* obj) { |
| 118 int unused ALLOW_UNUSED = ReadInteger(obj); | 222 int unused ALLOW_UNUSED = ReadInteger(obj); |
| 119 } | 223 } |
| 120 | 224 |
| 121 void WriteInteger64(int64 data, SerializeObject* obj) { | 225 void WriteInteger64(int64 data, SerializeObject* obj) { |
| 122 obj->pickle.WriteInt64(data); | 226 obj->pickle.WriteInt64(data); |
| 123 } | 227 } |
| 124 | 228 |
| 125 int64 ReadInteger64(const SerializeObject* obj) { | 229 int64 ReadInteger64(SerializeObject* obj) { |
| 126 int64 tmp = 0; | 230 int64 tmp = 0; |
| 127 obj->pickle.ReadInt64(&obj->iter, &tmp); | 231 if (obj->pickle.ReadInt64(&obj->iter, &tmp)) |
| 128 return tmp; | 232 return tmp; |
| 233 obj->parse_error = true; |
| 234 return 0; |
| 129 } | 235 } |
| 130 | 236 |
| 131 void WriteReal(double data, SerializeObject* obj) { | 237 void WriteReal(double data, SerializeObject* obj) { |
| 132 WriteData(&data, sizeof(double), obj); | 238 WriteData(&data, sizeof(double), obj); |
| 133 } | 239 } |
| 134 | 240 |
| 135 double ReadReal(const SerializeObject* obj) { | 241 double ReadReal(SerializeObject* obj) { |
| 136 const void* tmp = NULL; | 242 const void* tmp = NULL; |
| 137 int length = 0; | 243 int length = 0; |
| 138 double value = 0.0; | 244 double value = 0.0; |
| 139 ReadData(obj, &tmp, &length); | 245 ReadData(obj, &tmp, &length); |
| 140 if (tmp && length >= static_cast<int>(sizeof(double))) { | 246 if (length == static_cast<int>(sizeof(double))) { |
| 141 // Use memcpy, as tmp may not be correctly aligned. | 247 // Use memcpy, as tmp may not be correctly aligned. |
| 142 memcpy(&value, tmp, sizeof(double)); | 248 memcpy(&value, tmp, sizeof(double)); |
| 249 } else { |
| 250 obj->parse_error = true; |
| 143 } | 251 } |
| 144 return value; | 252 return value; |
| 145 } | 253 } |
| 146 | 254 |
| 147 void WriteBoolean(bool data, SerializeObject* obj) { | 255 void WriteBoolean(bool data, SerializeObject* obj) { |
| 148 obj->pickle.WriteInt(data ? 1 : 0); | 256 obj->pickle.WriteInt(data ? 1 : 0); |
| 149 } | 257 } |
| 150 | 258 |
| 151 bool ReadBoolean(const SerializeObject* obj) { | 259 bool ReadBoolean(SerializeObject* obj) { |
| 152 bool tmp; | 260 bool tmp; |
| 153 if (obj->pickle.ReadBool(&obj->iter, &tmp)) | 261 if (obj->pickle.ReadBool(&obj->iter, &tmp)) |
| 154 return tmp; | 262 return tmp; |
| 263 obj->parse_error = true; |
| 155 return false; | 264 return false; |
| 156 } | 265 } |
| 157 | 266 |
| 158 void WriteGURL(const GURL& url, SerializeObject* obj) { | 267 void WriteGURL(const GURL& url, SerializeObject* obj) { |
| 159 obj->pickle.WriteString(url.possibly_invalid_spec()); | 268 obj->pickle.WriteString(url.possibly_invalid_spec()); |
| 160 } | 269 } |
| 161 | 270 |
| 162 GURL ReadGURL(const SerializeObject* obj) { | 271 GURL ReadGURL(SerializeObject* obj) { |
| 163 std::string spec; | 272 std::string spec; |
| 164 if (obj->pickle.ReadString(&obj->iter, &spec)) | 273 if (obj->pickle.ReadString(&obj->iter, &spec)) |
| 165 return GURL(spec); | 274 return GURL(spec); |
| 275 obj->parse_error = true; |
| 166 return GURL(); | 276 return GURL(); |
| 167 } | 277 } |
| 168 | 278 |
| 169 // Read/WriteString pickle the WebString as <int length><WebUChar* data>. | 279 // WriteString pickles the NullableString16 as <int length><char16* data>. |
| 170 // If length == -1, then the WebString itself is NULL (WebString()). | 280 // If length == -1, then the NullableString16 itself is null. Otherwise the |
| 171 // Otherwise the length is the number of WebUChars (not bytes) in the WebString. | 281 // length is the number of char16 (not bytes) in the NullableString16. |
| 172 void WriteString(const WebString& str, SerializeObject* obj) { | 282 void WriteString(const base::NullableString16& str, SerializeObject* obj) { |
| 173 base::string16 string = str; | 283 if (str.is_null()) { |
| 174 const char16* data = string.data(); | 284 obj->pickle.WriteInt(-1); |
| 175 size_t length_in_uchars = string.length(); | 285 } else { |
| 176 size_t length_in_bytes = length_in_uchars * sizeof(char16); | 286 const char16* data = str.string().data(); |
| 177 switch (kVersion) { | 287 size_t length_in_bytes = str.string().length() * sizeof(char16); |
| 178 case 1: | 288 |
| 179 // Version 1 writes <length in bytes><string data>. | 289 CHECK_LT(length_in_bytes, |
| 180 // It saves WebString() and "" as "". | 290 static_cast<size_t>(std::numeric_limits<int>::max())); |
| 181 obj->pickle.WriteInt(length_in_bytes); | 291 obj->pickle.WriteInt(length_in_bytes); |
| 182 obj->pickle.WriteBytes(data, length_in_bytes); | 292 obj->pickle.WriteBytes(data, length_in_bytes); |
| 183 break; | 293 } |
| 184 case 2: | 294 } |
| 185 // Version 2 writes <length in WebUChar><string data>. | 295 |
| 186 // It uses -1 in the length field to mean WebString(). | 296 // This reads a serialized NullableString16 from obj. If a string can't be |
| 187 if (str.isNull()) { | 297 // read, NULL is returned. |
| 188 obj->pickle.WriteInt(-1); | 298 const char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { |
| 189 } else { | 299 int length_in_bytes; |
| 190 obj->pickle.WriteInt(length_in_uchars); | 300 if (!obj->pickle.ReadInt(&obj->iter, &length_in_bytes)) { |
| 191 obj->pickle.WriteBytes(data, length_in_bytes); | 301 obj->parse_error = true; |
| 192 } | |
| 193 break; | |
| 194 default: | |
| 195 // Version 3+ writes <length in bytes><string data>. | |
| 196 // It uses -1 in the length field to mean WebString(). | |
| 197 if (str.isNull()) { | |
| 198 obj->pickle.WriteInt(-1); | |
| 199 } else { | |
| 200 obj->pickle.WriteInt(length_in_bytes); | |
| 201 obj->pickle.WriteBytes(data, length_in_bytes); | |
| 202 } | |
| 203 break; | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // This reads a serialized WebString from obj. If a string can't be read, | |
| 208 // WebString() is returned. | |
| 209 const WebUChar* ReadStringNoCopy(const SerializeObject* obj, int* num_chars) { | |
| 210 int length; | |
| 211 | |
| 212 // Versions 1, 2, and 3 all start with an integer. | |
| 213 if (!obj->pickle.ReadInt(&obj->iter, &length)) | |
| 214 return NULL; | 302 return NULL; |
| 215 | 303 } |
| 216 // Starting with version 2, -1 means WebString(). | 304 |
| 217 if (length == -1) | 305 if (length_in_bytes < 0) |
| 218 return NULL; | 306 return NULL; |
| 219 | 307 |
| 220 // In version 2, the length field was the length in WebUChars. | 308 const char* data; |
| 221 // In version 1 and 3 it is the length in bytes. | 309 if (!obj->pickle.ReadBytes(&obj->iter, &data, length_in_bytes)) { |
| 222 int bytes = length; | 310 obj->parse_error = true; |
| 223 if (obj->version == 2) | |
| 224 bytes *= sizeof(WebUChar); | |
| 225 | |
| 226 const void* data; | |
| 227 if (!ReadBytes(obj, &data, bytes)) | |
| 228 return NULL; | 311 return NULL; |
| 312 } |
| 229 | 313 |
| 230 if (num_chars) | 314 if (num_chars) |
| 231 *num_chars = bytes / sizeof(WebUChar); | 315 *num_chars = length_in_bytes / sizeof(char16); |
| 232 return static_cast<const WebUChar*>(data); | 316 return reinterpret_cast<const char16*>(data); |
| 233 } | 317 } |
| 234 | 318 |
| 235 WebString ReadString(const SerializeObject* obj) { | 319 base::NullableString16 ReadString(SerializeObject* obj) { |
| 236 int num_chars; | 320 int num_chars; |
| 237 const WebUChar* chars = ReadStringNoCopy(obj, &num_chars); | 321 const char16* chars = ReadStringNoCopy(obj, &num_chars); |
| 238 return chars ? WebString(chars, num_chars) : WebString(); | 322 return chars ? |
| 239 } | 323 base::NullableString16(base::string16(chars, num_chars), false) : |
| 240 | 324 base::NullableString16(); |
| 241 void ConsumeString(const SerializeObject* obj) { | 325 } |
| 242 const WebUChar* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); | 326 |
| 243 } | 327 void ConsumeString(SerializeObject* obj) { |
| 244 | 328 const char16* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); |
| 245 // Writes a Vector of Strings into a SerializeObject for serialization. | 329 } |
| 330 |
| 331 template <typename T> |
| 332 void WriteAndValidateVectorSize(const std::vector<T>& v, SerializeObject* obj) { |
| 333 CHECK_LT(v.size(), std::numeric_limits<int>::max() / sizeof(T)); |
| 334 WriteInteger(static_cast<int>(v.size()), obj); |
| 335 } |
| 336 |
| 337 size_t ReadAndValidateVectorSize(SerializeObject* obj, size_t element_size) { |
| 338 size_t num_elements = static_cast<size_t>(ReadInteger(obj)); |
| 339 |
| 340 // Ensure that resizing a vector to size num_elements makes sense. |
| 341 if (std::numeric_limits<int>::max() / element_size <= num_elements) { |
| 342 obj->parse_error = true; |
| 343 return 0; |
| 344 } |
| 345 |
| 346 // Ensure that it is plausible for the pickle to contain num_elements worth |
| 347 // of data. |
| 348 if (obj->pickle.payload_size() <= num_elements) { |
| 349 obj->parse_error = true; |
| 350 return 0; |
| 351 } |
| 352 |
| 353 return num_elements; |
| 354 } |
| 355 |
| 356 // Writes a Vector of strings into a SerializeObject for serialization. |
| 246 void WriteStringVector( | 357 void WriteStringVector( |
| 247 const WebVector<WebString>& data, SerializeObject* obj) { | 358 const std::vector<base::NullableString16>& data, SerializeObject* obj) { |
| 248 WriteInteger(static_cast<int>(data.size()), obj); | 359 WriteAndValidateVectorSize(data, obj); |
| 249 for (size_t i = 0, c = data.size(); i < c; ++i) { | 360 for (size_t i = 0; i < data.size(); ++i) { |
| 250 unsigned ui = static_cast<unsigned>(i); // sigh | 361 WriteString(data[i], obj); |
| 251 WriteString(data[ui], obj); | 362 } |
| 252 } | 363 } |
| 253 } | 364 |
| 254 | 365 void ReadStringVector(SerializeObject* obj, |
| 255 WebVector<WebString> ReadStringVector(const SerializeObject* obj) { | 366 std::vector<base::NullableString16>* result) { |
| 256 int num_elements = ReadInteger(obj); | 367 size_t num_elements = |
| 257 WebVector<WebString> result(static_cast<size_t>(num_elements)); | 368 ReadAndValidateVectorSize(obj, sizeof(base::NullableString16)); |
| 258 for (int i = 0; i < num_elements; ++i) | 369 |
| 259 result[i] = ReadString(obj); | 370 result->resize(num_elements); |
| 260 return result; | 371 for (size_t i = 0; i < num_elements; ++i) |
| 261 } | 372 (*result)[i] = ReadString(obj); |
| 262 | 373 } |
| 263 void ConsumeStringVector(const SerializeObject* obj) { | 374 |
| 264 int num_elements = ReadInteger(obj); | 375 // Writes an ExplodedHttpBody object into a SerializeObject for serialization. |
| 265 for (int i = 0; i < num_elements; ++i) | 376 void WriteHttpBody(const ExplodedHttpBody& http_body, SerializeObject* obj) { |
| 266 ConsumeString(obj); | 377 WriteBoolean(!http_body.is_null, obj); |
| 267 } | 378 |
| 268 | 379 if (http_body.is_null) |
| 269 // Writes a FormData object into a SerializeObject for serialization. | |
| 270 void WriteFormData(const WebHTTPBody& http_body, SerializeObject* obj) { | |
| 271 WriteBoolean(!http_body.isNull(), obj); | |
| 272 | |
| 273 if (http_body.isNull()) | |
| 274 return; | 380 return; |
| 275 | 381 |
| 276 WriteInteger(static_cast<int>(http_body.elementCount()), obj); | 382 WriteAndValidateVectorSize(http_body.elements, obj); |
| 277 WebHTTPBody::Element element; | 383 for (size_t i = 0; i < http_body.elements.size(); ++i) { |
| 278 for (size_t i = 0; http_body.elementAt(i, element); ++i) { | 384 const ExplodedHttpBodyElement& element = http_body.elements[i]; |
| 279 WriteInteger(element.type, obj); | 385 WriteInteger(element.type, obj); |
| 280 if (element.type == WebHTTPBody::Element::TypeData) { | 386 if (element.type == WebKit::WebHTTPBody::Element::TypeData) { |
| 281 WriteData(element.data.data(), static_cast<int>(element.data.size()), | 387 WriteData(element.data.data(), static_cast<int>(element.data.size()), |
| 282 obj); | 388 obj); |
| 283 } else if (element.type == WebHTTPBody::Element::TypeFile) { | 389 } else if (element.type == WebKit::WebHTTPBody::Element::TypeFile) { |
| 284 WriteString(element.filePath, obj); | 390 WriteString(element.file_path, obj); |
| 285 WriteInteger64(element.fileStart, obj); | 391 WriteInteger64(element.file_start, obj); |
| 286 WriteInteger64(element.fileLength, obj); | 392 WriteInteger64(element.file_length, obj); |
| 287 WriteReal(element.modificationTime, obj); | 393 WriteReal(element.file_modification_time, obj); |
| 288 } else if (element.type == WebHTTPBody::Element::TypeURL) { | 394 } else if (element.type == WebKit::WebHTTPBody::Element::TypeURL) { |
| 289 WriteGURL(element.url, obj); | 395 WriteGURL(element.url, obj); |
| 290 WriteInteger64(element.fileStart, obj); | 396 WriteInteger64(element.file_start, obj); |
| 291 WriteInteger64(element.fileLength, obj); | 397 WriteInteger64(element.file_length, obj); |
| 292 WriteReal(element.modificationTime, obj); | 398 WriteReal(element.file_modification_time, obj); |
| 293 } else { | 399 } else { |
| 294 WriteGURL(element.url, obj); | 400 WriteGURL(element.url, obj); |
| 295 } | 401 } |
| 296 } | 402 } |
| 297 WriteInteger64(http_body.identifier(), obj); | 403 WriteInteger64(http_body.identifier, obj); |
| 298 WriteBoolean(http_body.containsPasswordData(), obj); | 404 WriteBoolean(http_body.contains_passwords, obj); |
| 299 } | 405 } |
| 300 | 406 |
| 301 WebHTTPBody ReadFormData(const SerializeObject* obj) { | 407 void ReadHttpBody(SerializeObject* obj, ExplodedHttpBody* http_body) { |
| 302 // In newer versions, an initial boolean indicates if we have form data. | 408 // An initial boolean indicates if we have an HTTP body. |
| 303 if (obj->version >= 5 && !ReadBoolean(obj)) | 409 if (!ReadBoolean(obj)) |
| 304 return WebHTTPBody(); | 410 return; |
| 305 | 411 http_body->is_null = false; |
| 306 // In older versions, 0 elements implied no form data. | 412 |
| 307 int num_elements = ReadInteger(obj); | 413 int num_elements = ReadInteger(obj); |
| 308 if (num_elements == 0 && obj->version < 5) | |
| 309 return WebHTTPBody(); | |
| 310 | |
| 311 WebHTTPBody http_body; | |
| 312 http_body.initialize(); | |
| 313 | 414 |
| 314 for (int i = 0; i < num_elements; ++i) { | 415 for (int i = 0; i < num_elements; ++i) { |
| 315 int type = ReadInteger(obj); | 416 int type = ReadInteger(obj); |
| 316 if (type == WebHTTPBody::Element::TypeData) { | 417 if (type == WebKit::WebHTTPBody::Element::TypeData) { |
| 317 const void* data; | 418 const void* data; |
| 318 int length = -1; | 419 int length = -1; |
| 319 ReadData(obj, &data, &length); | 420 ReadData(obj, &data, &length); |
| 320 if (length >= 0) | 421 if (length >= 0) { |
| 321 http_body.appendData(WebData(static_cast<const char*>(data), length)); | 422 AppendDataToHttpBody(http_body, static_cast<const char*>(data), |
| 322 } else if (type == WebHTTPBody::Element::TypeFile) { | 423 length); |
| 323 WebString file_path = ReadString(obj); | |
| 324 long long file_start = 0; | |
| 325 long long file_length = -1; | |
| 326 double modification_time = 0.0; | |
| 327 if (obj->version >= 8) { | |
| 328 file_start = ReadInteger64(obj); | |
| 329 file_length = ReadInteger64(obj); | |
| 330 modification_time = ReadReal(obj); | |
| 331 } | 424 } |
| 332 http_body.appendFileRange(file_path, file_start, file_length, | 425 } else if (type == WebKit::WebHTTPBody::Element::TypeFile) { |
| 333 modification_time); | 426 base::NullableString16 file_path = ReadString(obj); |
| 334 } else if (type == WebHTTPBody::Element::TypeURL) { | 427 int64 file_start = ReadInteger64(obj); |
| 428 int64 file_length = ReadInteger64(obj); |
| 429 double file_modification_time = ReadReal(obj); |
| 430 AppendFileRangeToHttpBody(http_body, file_path, file_start, file_length, |
| 431 file_modification_time); |
| 432 } else if (type == WebKit::WebHTTPBody::Element::TypeURL) { |
| 335 GURL url = ReadGURL(obj); | 433 GURL url = ReadGURL(obj); |
| 336 long long file_start = 0; | 434 int64 file_start = ReadInteger64(obj); |
| 337 long long file_length = -1; | 435 int64 file_length = ReadInteger64(obj); |
| 338 double modification_time = 0.0; | 436 double file_modification_time = ReadReal(obj); |
| 339 file_start = ReadInteger64(obj); | 437 AppendURLRangeToHttpBody(http_body, url, file_start, file_length, |
| 340 file_length = ReadInteger64(obj); | 438 file_modification_time); |
| 341 modification_time = ReadReal(obj); | 439 } else if (type == WebKit::WebHTTPBody::Element::TypeBlob) { |
| 342 http_body.appendURLRange(url, file_start, file_length, | |
| 343 modification_time); | |
| 344 } else if (obj->version >= 10) { | |
| 345 GURL blob_url = ReadGURL(obj); | 440 GURL blob_url = ReadGURL(obj); |
| 346 http_body.appendBlob(blob_url); | 441 AppendBlobToHttpBody(http_body, blob_url); |
| 347 } | 442 } |
| 348 } | 443 } |
| 349 if (obj->version >= 4) | 444 http_body->identifier = ReadInteger64(obj); |
| 350 http_body.setIdentifier(ReadInteger64(obj)); | |
| 351 | 445 |
| 352 if (obj->version >= 12) | 446 if (obj->version >= 12) |
| 353 http_body.setContainsPasswordData(ReadBoolean(obj)); | 447 http_body->contains_passwords = ReadBoolean(obj); |
| 354 | 448 } |
| 355 return http_body; | 449 |
| 356 } | 450 // Writes the ExplodedFrameState data into the SerializeObject object for |
| 357 | |
| 358 // Writes the HistoryItem data into the SerializeObject object for | |
| 359 // serialization. | 451 // serialization. |
| 360 void WriteHistoryItem( | 452 void WriteFrameState( |
| 361 const WebHistoryItem& item, SerializeObject* obj, bool is_top) { | 453 const ExplodedFrameState& state, SerializeObject* obj, bool is_top) { |
| 362 // WARNING: This data may be persisted for later use. As such, care must be | 454 // WARNING: This data may be persisted for later use. As such, care must be |
| 363 // taken when changing the serialized format. If a new field needs to be | 455 // taken when changing the serialized format. If a new field needs to be |
| 364 // written, only adding at the end will make it easier to deal with loading | 456 // written, only adding at the end will make it easier to deal with loading |
| 365 // older versions. Similarly, this should NOT save fields with sensitive | 457 // older versions. Similarly, this should NOT save fields with sensitive |
| 366 // data, such as password fields. | 458 // data, such as password fields. |
| 367 | 459 |
| 368 if (kVersion >= 14) { | 460 WriteString(state.url_string, obj); |
| 369 if (is_top) { | 461 WriteString(state.original_url_string, obj); |
| 370 WriteInteger(kVersion, obj); | 462 WriteString(state.target, obj); |
| 371 | 463 WriteString(state.parent, obj); |
| 372 // Insert the list of referenced files, so they can be extracted easily | 464 WriteString(state.title, obj); |
| 373 // from the serialized data (avoiding the need to call into Blink again). | 465 WriteString(state.alternate_title, obj); |
| 374 WriteStringVector(item.getReferencedFilePaths(), obj); | 466 WriteReal(state.visited_time, obj); |
| 375 } | 467 WriteInteger(state.scroll_offset.x(), obj); |
| 376 } else { | 468 WriteInteger(state.scroll_offset.y(), obj); |
| 377 WriteInteger(kVersion, obj); | 469 WriteBoolean(state.is_target_item, obj); |
| 378 } | 470 WriteInteger(state.visit_count, obj); |
| 379 | 471 WriteString(state.referrer, obj); |
| 380 WriteString(item.urlString(), obj); | 472 |
| 381 WriteString(item.originalURLString(), obj); | 473 WriteStringVector(state.document_state, obj); |
| 382 WriteString(item.target(), obj); | 474 |
| 383 WriteString(item.parent(), obj); | 475 WriteReal(state.page_scale_factor, obj); |
| 384 WriteString(item.title(), obj); | 476 WriteInteger64(state.item_sequence_number, obj); |
| 385 WriteString(item.alternateTitle(), obj); | 477 WriteInteger64(state.document_sequence_number, obj); |
| 386 WriteReal(item.lastVisitedTime(), obj); | 478 |
| 387 WriteInteger(item.scrollOffset().x, obj); | 479 bool has_state_object = !state.state_object.is_null(); |
| 388 WriteInteger(item.scrollOffset().y, obj); | 480 WriteBoolean(has_state_object, obj); |
| 389 WriteBoolean(item.isTargetItem(), obj); | 481 if (has_state_object) |
| 390 WriteInteger(item.visitCount(), obj); | 482 WriteString(state.state_object, obj); |
| 391 WriteString(item.referrer(), obj); | 483 |
| 392 | 484 WriteHttpBody(state.http_body, obj); |
| 393 WriteStringVector(item.documentState(), obj); | 485 |
| 394 | 486 // NOTE: It is a quirk of the format that we still have to write the |
| 395 if (kVersion >= 11) | 487 // http_content_type field when the HTTP body is null. That's why this code |
| 396 WriteReal(item.pageScaleFactor(), obj); | 488 // is here instead of inside WriteHttpBody. |
| 397 if (kVersion >= 9) | 489 WriteString(state.http_body.http_content_type, obj); |
| 398 WriteInteger64(item.itemSequenceNumber(), obj); | |
| 399 if (kVersion >= 6) | |
| 400 WriteInteger64(item.documentSequenceNumber(), obj); | |
| 401 if (kVersion >= 7) { | |
| 402 bool has_state_object = !item.stateObject().isNull(); | |
| 403 WriteBoolean(has_state_object, obj); | |
| 404 if (has_state_object) | |
| 405 WriteString(item.stateObject().toString(), obj); | |
| 406 } | |
| 407 | |
| 408 WriteFormData(item.httpBody(), obj); | |
| 409 WriteString(item.httpContentType(), obj); | |
| 410 if (kVersion < 14) | |
| 411 WriteString(item.referrer(), obj); | |
| 412 | 490 |
| 413 // Subitems | 491 // Subitems |
| 414 const WebVector<WebHistoryItem>& children = item.children(); | 492 const std::vector<ExplodedFrameState>& children = state.children; |
| 415 WriteInteger(static_cast<int>(children.size()), obj); | 493 WriteAndValidateVectorSize(children, obj); |
| 416 for (size_t i = 0, c = children.size(); i < c; ++i) | 494 for (size_t i = 0; i < children.size(); ++i) |
| 417 WriteHistoryItem(children[i], obj, false); | 495 WriteFrameState(children[i], obj, false); |
| 418 } | 496 } |
| 419 | 497 |
| 420 // Creates a new HistoryItem tree based on the serialized string. | 498 void ReadFrameState(SerializeObject* obj, bool is_top, |
| 421 // Assumes the data is in the format returned by WriteHistoryItem. | 499 ExplodedFrameState* state) { |
| 422 WebHistoryItem ReadHistoryItem( | 500 if (obj->version < 14 && !is_top) |
| 423 const SerializeObject* obj, | |
| 424 IncludeFormData include_form_data, | |
| 425 bool include_scroll_offset, | |
| 426 bool is_top) { | |
| 427 if (is_top) { | |
| 428 obj->version = ReadInteger(obj); | |
| 429 | |
| 430 if (obj->version == -1) { | |
| 431 GURL url = ReadGURL(obj); | |
| 432 WebHistoryItem item; | |
| 433 item.initialize(); | |
| 434 item.setURLString(WebString::fromUTF8(url.possibly_invalid_spec())); | |
| 435 return item; | |
| 436 } | |
| 437 | |
| 438 if (obj->version > kVersion || obj->version < 1) | |
| 439 return WebHistoryItem(); | |
| 440 | |
| 441 if (obj->version >= 14) | |
| 442 ConsumeStringVector(obj); // Skip over list of referenced files. | |
| 443 } else if (obj->version < 14) { | |
| 444 ConsumeInteger(obj); // Skip over redundant version field. | 501 ConsumeInteger(obj); // Skip over redundant version field. |
| 445 } | 502 |
| 446 | 503 state->url_string = ReadString(obj); |
| 447 WebHistoryItem item; | 504 state->original_url_string = ReadString(obj); |
| 448 item.initialize(); | 505 state->target = ReadString(obj); |
| 449 | 506 state->parent = ReadString(obj); |
| 450 item.setURLString(ReadString(obj)); | 507 state->title = ReadString(obj); |
| 451 item.setOriginalURLString(ReadString(obj)); | 508 state->alternate_title = ReadString(obj); |
| 452 item.setTarget(ReadString(obj)); | 509 state->visited_time = ReadReal(obj); |
| 453 item.setParent(ReadString(obj)); | |
| 454 item.setTitle(ReadString(obj)); | |
| 455 item.setAlternateTitle(ReadString(obj)); | |
| 456 item.setLastVisitedTime(ReadReal(obj)); | |
| 457 | 510 |
| 458 int x = ReadInteger(obj); | 511 int x = ReadInteger(obj); |
| 459 int y = ReadInteger(obj); | 512 int y = ReadInteger(obj); |
| 460 if (include_scroll_offset) | 513 state->scroll_offset = gfx::Point(x, y); |
| 461 item.setScrollOffset(WebPoint(x, y)); | 514 |
| 462 | 515 state->is_target_item = ReadBoolean(obj); |
| 463 item.setIsTargetItem(ReadBoolean(obj)); | 516 state->visit_count = ReadInteger(obj); |
| 464 item.setVisitCount(ReadInteger(obj)); | 517 state->referrer = ReadString(obj); |
| 465 item.setReferrer(ReadString(obj)); | 518 |
| 466 | 519 ReadStringVector(obj, &state->document_state); |
| 467 item.setDocumentState(ReadStringVector(obj)); | 520 |
| 468 | 521 state->page_scale_factor = ReadReal(obj); |
| 469 if (obj->version >= 11) | 522 state->item_sequence_number = ReadInteger64(obj); |
| 470 item.setPageScaleFactor(ReadReal(obj)); | 523 state->document_sequence_number = ReadInteger64(obj); |
| 471 if (obj->version >= 9) | 524 |
| 472 item.setItemSequenceNumber(ReadInteger64(obj)); | 525 bool has_state_object = ReadBoolean(obj); |
| 473 if (obj->version >= 6) | 526 if (has_state_object) |
| 474 item.setDocumentSequenceNumber(ReadInteger64(obj)); | 527 state->state_object = ReadString(obj); |
| 475 if (obj->version >= 7) { | 528 |
| 476 bool has_state_object = ReadBoolean(obj); | 529 ReadHttpBody(obj, &state->http_body); |
| 477 if (has_state_object) { | 530 |
| 478 item.setStateObject( | 531 // NOTE: It is a quirk of the format that we still have to read the |
| 479 WebSerializedScriptValue::fromString(ReadString(obj))); | 532 // http_content_type field when the HTTP body is null. That's why this code |
| 480 } | 533 // is here instead of inside ReadHttpBody. |
| 481 } | 534 state->http_body.http_content_type = ReadString(obj); |
| 482 | |
| 483 // The extra referrer string is read for backwards compat. | |
| 484 const WebHTTPBody& http_body = ReadFormData(obj); | |
| 485 const WebString& http_content_type = ReadString(obj); | |
| 486 | 535 |
| 487 if (obj->version < 14) | 536 if (obj->version < 14) |
| 488 ConsumeString(obj); // Skip unused referrer string. | 537 ConsumeString(obj); // Skip unused referrer string. |
| 489 | 538 |
| 490 if (include_form_data == ALWAYS_INCLUDE_FORM_DATA || | |
| 491 (include_form_data == INCLUDE_FORM_DATA_WITHOUT_PASSWORDS && | |
| 492 !http_body.isNull() && !http_body.containsPasswordData())) { | |
| 493 // Include the full HTTP body. | |
| 494 item.setHTTPBody(http_body); | |
| 495 item.setHTTPContentType(http_content_type); | |
| 496 } else if (!http_body.isNull()) { | |
| 497 // Don't include the data in the HTTP body, but include its identifier. This | |
| 498 // enables fetching data from the cache. | |
| 499 WebHTTPBody empty_http_body; | |
| 500 empty_http_body.initialize(); | |
| 501 empty_http_body.setIdentifier(http_body.identifier()); | |
| 502 item.setHTTPBody(empty_http_body); | |
| 503 } | |
| 504 | |
| 505 #if defined(OS_ANDROID) | 539 #if defined(OS_ANDROID) |
| 506 if (obj->version == 11) { | 540 if (obj->version == 11) { |
| 507 // Now-unused values that shipped in this version of Chrome for Android when | 541 // Now-unused values that shipped in this version of Chrome for Android when |
| 508 // it was on a private branch. | 542 // it was on a private branch. |
| 509 ReadReal(obj); | 543 ReadReal(obj); |
| 510 ReadBoolean(obj); | 544 ReadBoolean(obj); |
| 511 | 545 |
| 512 // In this version, pageScaleFactor included deviceScaleFactor and scroll | 546 // In this version, page_scale_factor included deviceScaleFactor and scroll |
| 513 // offsets were premultiplied by pageScaleFactor. | 547 // offsets were premultiplied by pageScaleFactor. |
| 514 if (item.pageScaleFactor()) { | 548 if (state->page_scale_factor) { |
| 515 if (include_scroll_offset) | 549 state->scroll_offset = |
| 516 item.setScrollOffset( | 550 gfx::Point(state->scroll_offset.x() / state->page_scale_factor, |
| 517 WebPoint(item.scrollOffset().x / item.pageScaleFactor(), | 551 state->scroll_offset.y() / state->page_scale_factor); |
| 518 item.scrollOffset().y / item.pageScaleFactor())); | 552 state->page_scale_factor = (state->page_scale_factor / |
| 519 item.setPageScaleFactor(item.pageScaleFactor() / | |
| 520 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 553 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() |
| 521 .device_scale_factor()); | 554 .device_scale_factor()); |
| 522 } | 555 } |
| 523 } | 556 } |
| 524 #endif | 557 #endif |
| 525 | 558 |
| 526 // Subitems | 559 // Subitems |
| 527 int num_children = ReadInteger(obj); | 560 size_t num_children = |
| 528 for (int i = 0; i < num_children; ++i) | 561 ReadAndValidateVectorSize(obj, sizeof(ExplodedFrameState)); |
| 529 item.appendToChildren(ReadHistoryItem(obj, | 562 state->children.resize(num_children); |
| 530 include_form_data, | 563 for (size_t i = 0; i < num_children; ++i) |
| 531 include_scroll_offset, | 564 ReadFrameState(obj, false, &state->children[i]); |
| 532 false)); | |
| 533 | |
| 534 return item; | |
| 535 } | 565 } |
| 536 | 566 |
| 537 // Reconstruct a HistoryItem from a string, using our JSON Value deserializer. | 567 void WritePageState(const ExplodedPageState& state, SerializeObject* obj) { |
| 538 // This assumes that the given serialized string has all the required key,value | 568 WriteInteger(obj->version, obj); |
| 539 // pairs, and does minimal error checking. The form data of the post is restored | 569 WriteStringVector(state.referenced_files, obj); |
| 540 // if |include_form_data| is |ALWAYS_INCLUDE_FORM_DATA| or if the data doesn't | 570 WriteFrameState(state.top, obj, true); |
| 541 // contain passwords and |include_form_data| is | |
| 542 // |INCLUDE_FORM_DATA_WITHOUT_PASSWORDS|. Otherwise the form data is empty. If | |
| 543 // |include_scroll_offset| is true, the scroll offset is restored. | |
| 544 WebHistoryItem HistoryItemFromString( | |
| 545 const std::string& serialized_item, | |
| 546 IncludeFormData include_form_data, | |
| 547 bool include_scroll_offset) { | |
| 548 if (serialized_item.empty()) | |
| 549 return WebHistoryItem(); | |
| 550 | |
| 551 SerializeObject obj(serialized_item.data(), | |
| 552 static_cast<int>(serialized_item.length())); | |
| 553 return ReadHistoryItem(&obj, include_form_data, include_scroll_offset, true); | |
| 554 } | 571 } |
| 555 | 572 |
| 556 void ToFilePathVector(const WebVector<WebString>& input, | 573 void ReadPageState(SerializeObject* obj, ExplodedPageState* state) { |
| 557 std::vector<base::FilePath>* output) { | 574 obj->version = ReadInteger(obj); |
| 558 for (size_t i = 0; i < input.size(); ++i) | 575 |
| 559 output->push_back(webkit_base::WebStringToFilePath(input[i])); | 576 if (obj->version == -1) { |
| 577 GURL url = ReadGURL(obj); |
| 578 // NOTE: GURL::possibly_invalid_spec() always returns valid UTF-8. |
| 579 state->top.url_string = state->top.original_url_string = |
| 580 base::NullableString16(UTF8ToUTF16(url.possibly_invalid_spec()), false); |
| 581 return; |
| 582 } |
| 583 |
| 584 if (obj->version > kCurrentVersion || obj->version < kMinVersion) { |
| 585 obj->parse_error = true; |
| 586 return; |
| 587 } |
| 588 |
| 589 if (obj->version >= 14) |
| 590 ReadStringVector(obj, &state->referenced_files); |
| 591 |
| 592 ReadFrameState(obj, true, &state->top); |
| 593 |
| 594 if (obj->version < 14) |
| 595 RecursivelyAppendReferencedFiles(state->top, &state->referenced_files); |
| 596 |
| 597 // De-dupe |
| 598 state->referenced_files.erase( |
| 599 std::unique(state->referenced_files.begin(), |
| 600 state->referenced_files.end()), |
| 601 state->referenced_files.end()); |
| 560 } | 602 } |
| 561 | 603 |
| 562 } // namespace | 604 } // namespace |
| 563 | 605 |
| 564 // Serialize a HistoryItem to a string, using our JSON Value serializer. | 606 ExplodedHttpBodyElement::ExplodedHttpBodyElement() |
| 565 std::string HistoryItemToString(const WebHistoryItem& item) { | 607 : type(WebKit::WebHTTPBody::Element::TypeData), |
| 566 if (item.isNull()) | 608 file_start(0), |
| 567 return std::string(); | 609 file_length(-1), |
| 568 | 610 file_modification_time(std::numeric_limits<double>::quiet_NaN()) { |
| 569 SerializeObject obj; | |
| 570 WriteHistoryItem(item, &obj, true); | |
| 571 return obj.GetAsString(); | |
| 572 } | 611 } |
| 573 | 612 |
| 574 WebHistoryItem HistoryItemFromString(const std::string& serialized_item) { | 613 ExplodedHttpBodyElement::~ExplodedHttpBodyElement() { |
| 575 return HistoryItemFromString(serialized_item, ALWAYS_INCLUDE_FORM_DATA, true); | |
| 576 } | 614 } |
| 577 | 615 |
| 578 std::vector<base::FilePath> FilePathsFromHistoryState( | 616 ExplodedHttpBody::ExplodedHttpBody() |
| 579 const std::string& content_state) { | 617 : identifier(0), |
| 580 // TODO(darin): We should avoid using the WebKit API here, so that we do not | 618 contains_passwords(false), |
| 581 // need to have WebKit initialized before calling this method. | 619 is_null(true) { |
| 582 | |
| 583 std::vector<base::FilePath> result; | |
| 584 | |
| 585 // In newer versions of the format, the set of referenced files is computed | |
| 586 // at serialization time. | |
| 587 SerializeObject obj(content_state.data(), | |
| 588 static_cast<int>(content_state.length())); | |
| 589 obj.version = ReadInteger(&obj); | |
| 590 | |
| 591 if (obj.version > kVersion || obj.version < 1) | |
| 592 return result; | |
| 593 | |
| 594 if (obj.version >= 14) { | |
| 595 ToFilePathVector(ReadStringVector(&obj), &result); | |
| 596 } else { | |
| 597 // TODO(darin): Delete this code path after we branch for M29. | |
| 598 const WebHistoryItem& item = | |
| 599 HistoryItemFromString(content_state, ALWAYS_INCLUDE_FORM_DATA, true); | |
| 600 if (!item.isNull()) | |
| 601 ToFilePathVector(item.getReferencedFilePaths(), &result); | |
| 602 } | |
| 603 return result; | |
| 604 } | 620 } |
| 605 | 621 |
| 606 // For testing purposes only. | 622 ExplodedHttpBody::~ExplodedHttpBody() { |
| 607 void HistoryItemToVersionedString(const WebHistoryItem& item, int version, | |
| 608 std::string* serialized_item) { | |
| 609 if (item.isNull()) { | |
| 610 serialized_item->clear(); | |
| 611 return; | |
| 612 } | |
| 613 | |
| 614 // Temporarily change the version. | |
| 615 int real_version = kVersion; | |
| 616 kVersion = version; | |
| 617 | |
| 618 SerializeObject obj; | |
| 619 WriteHistoryItem(item, &obj, true); | |
| 620 *serialized_item = obj.GetAsString(); | |
| 621 | |
| 622 kVersion = real_version; | |
| 623 } | 623 } |
| 624 | 624 |
| 625 int HistoryItemCurrentVersion() { | 625 ExplodedFrameState::ExplodedFrameState() |
| 626 return kVersion; | 626 : item_sequence_number(0), |
| 627 document_sequence_number(0), |
| 628 visit_count(0), |
| 629 visited_time(0.0), |
| 630 page_scale_factor(0.0), |
| 631 is_target_item(false) { |
| 627 } | 632 } |
| 628 | 633 |
| 629 std::string RemovePasswordDataFromHistoryState( | 634 ExplodedFrameState::~ExplodedFrameState() { |
| 630 const std::string& content_state) { | |
| 631 // TODO(darin): We should avoid using the WebKit API here, so that we do not | |
| 632 // need to have WebKit initialized before calling this method. | |
| 633 const WebHistoryItem& item = | |
| 634 HistoryItemFromString( | |
| 635 content_state, INCLUDE_FORM_DATA_WITHOUT_PASSWORDS, true); | |
| 636 if (item.isNull()) { | |
| 637 // Couldn't parse the string, return an empty string. | |
| 638 return std::string(); | |
| 639 } | |
| 640 | |
| 641 return HistoryItemToString(item); | |
| 642 } | 635 } |
| 643 | 636 |
| 644 std::string RemoveScrollOffsetFromHistoryState( | 637 ExplodedPageState::ExplodedPageState() { |
| 645 const std::string& content_state) { | |
| 646 // TODO(darin): We should avoid using the WebKit API here, so that we do not | |
| 647 // need to have WebKit initialized before calling this method. | |
| 648 const WebHistoryItem& item = | |
| 649 HistoryItemFromString(content_state, ALWAYS_INCLUDE_FORM_DATA, false); | |
| 650 if (item.isNull()) { | |
| 651 // Couldn't parse the string, return an empty string. | |
| 652 return std::string(); | |
| 653 } | |
| 654 | |
| 655 return HistoryItemToString(item); | |
| 656 } | 638 } |
| 657 | 639 |
| 658 std::string CreateHistoryStateForURL(const GURL& url) { | 640 ExplodedPageState::~ExplodedPageState() { |
| 659 // We avoid using the WebKit API here, so that we do not need to have WebKit | |
| 660 // initialized before calling this method. Instead, we write a simple | |
| 661 // serialization of the given URL with a dummy version number of -1. This | |
| 662 // will be interpreted by ReadHistoryItem as a request to create a default | |
| 663 // WebHistoryItem. | |
| 664 SerializeObject obj; | |
| 665 WriteInteger(-1, &obj); | |
| 666 WriteGURL(url, &obj); | |
| 667 return obj.GetAsString(); | |
| 668 } | 641 } |
| 669 | 642 |
| 670 } // namespace webkit_glue | 643 bool DecodePageState(const std::string& encoded, ExplodedPageState* exploded) { |
| 644 *exploded = ExplodedPageState(); |
| 645 |
| 646 if (encoded.empty()) |
| 647 return true; |
| 648 |
| 649 SerializeObject obj(encoded.data(), static_cast<int>(encoded.size())); |
| 650 ReadPageState(&obj, exploded); |
| 651 return !obj.parse_error; |
| 652 } |
| 653 |
| 654 bool EncodePageState(const ExplodedPageState& exploded, std::string* encoded) { |
| 655 SerializeObject obj; |
| 656 obj.version = kCurrentVersion; |
| 657 WritePageState(exploded, &obj); |
| 658 *encoded = obj.GetAsString(); |
| 659 return true; |
| 660 } |
| 661 |
| 662 } // namespace content |
| OLD | NEW |