Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "config.h" | 5 #include "config.h" |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 | 9 |
| 10 MSVC_PUSH_WARNING_LEVEL(0); | 10 MSVC_PUSH_WARNING_LEVEL(0); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 mutable int version; | 38 mutable int version; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. | 41 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. |
| 42 // Version ID used in reading/writing history items. | 42 // Version ID used in reading/writing history items. |
| 43 // 1: Initial revision. | 43 // 1: Initial revision. |
| 44 // 2: Added case for NULL string versus "". Version 2 code can read Version 1 | 44 // 2: Added case for NULL string versus "". Version 2 code can read Version 1 |
| 45 // data, but not vice versa. | 45 // data, but not vice versa. |
| 46 // 3: Version 2 was broken, it stored number of UChars, not number of bytes. | 46 // 3: Version 2 was broken, it stored number of UChars, not number of bytes. |
| 47 // This version checks and reads v1 and v2 correctly. | 47 // This version checks and reads v1 and v2 correctly. |
| 48 // 4: Adds support for storing FormData::identifier(). | |
| 48 // Should be const, but unit tests may modify it. | 49 // Should be const, but unit tests may modify it. |
| 49 int kVersion = 3; | 50 int kVersion = 4; |
| 50 | 51 |
| 51 // A bunch of convenience functions to read/write to SerializeObjects. | 52 // A bunch of convenience functions to read/write to SerializeObjects. |
| 52 // The serializers assume the input data is in the correct format and so does | 53 // The serializers assume the input data is in the correct format and so does |
| 53 // no error checking. | 54 // no error checking. |
| 54 inline void WriteData(const void* data, int length, SerializeObject* obj) { | 55 inline void WriteData(const void* data, int length, SerializeObject* obj) { |
| 55 obj->pickle.WriteData(static_cast<const char*>(data), length); | 56 obj->pickle.WriteData(static_cast<const char*>(data), length); |
| 56 } | 57 } |
| 57 | 58 |
| 58 inline void ReadData(const SerializeObject* obj, const void** data, | 59 inline void ReadData(const SerializeObject* obj, const void** data, |
| 59 int* length) { | 60 int* length) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 74 inline void WriteInteger(int data, SerializeObject* obj) { | 75 inline void WriteInteger(int data, SerializeObject* obj) { |
| 75 obj->pickle.WriteInt(data); | 76 obj->pickle.WriteInt(data); |
| 76 } | 77 } |
| 77 | 78 |
| 78 inline int ReadInteger(const SerializeObject* obj) { | 79 inline int ReadInteger(const SerializeObject* obj) { |
| 79 int tmp; | 80 int tmp; |
| 80 obj->pickle.ReadInt(&obj->iter, &tmp); | 81 obj->pickle.ReadInt(&obj->iter, &tmp); |
| 81 return tmp; | 82 return tmp; |
| 82 } | 83 } |
| 83 | 84 |
| 85 inline void WriteInteger64(int64 data, SerializeObject* obj) { | |
| 86 obj->pickle.WriteInt64(data); | |
| 87 } | |
| 88 | |
| 89 inline int64 ReadInteger64(const SerializeObject* obj) { | |
| 90 int64 tmp; | |
|
sky
2009/03/24 20:34:40
should you set this to 0, just in case ReadInt64 f
darin (slow to review)
2009/03/24 21:12:15
Good idea!
| |
| 91 obj->pickle.ReadInt64(&obj->iter, &tmp); | |
| 92 return tmp; | |
| 93 } | |
| 94 | |
| 84 inline void WriteReal(double data, SerializeObject* obj) { | 95 inline void WriteReal(double data, SerializeObject* obj) { |
| 85 WriteData(&data, sizeof(double), obj); | 96 WriteData(&data, sizeof(double), obj); |
| 86 } | 97 } |
| 87 | 98 |
| 88 inline double ReadReal(const SerializeObject* obj) { | 99 inline double ReadReal(const SerializeObject* obj) { |
| 89 const void* tmp; | 100 const void* tmp; |
| 90 int length; | 101 int length; |
| 91 ReadData(obj, &tmp, &length); | 102 ReadData(obj, &tmp, &length); |
| 92 return *static_cast<const double*>(tmp); | 103 return *static_cast<const double*>(tmp); |
| 93 } | 104 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 117 // Version 2 writes <length in UChars><string data>. | 128 // Version 2 writes <length in UChars><string data>. |
| 118 // It uses -1 in the length field to mean String(). | 129 // It uses -1 in the length field to mean String(). |
| 119 if (data.isNull()) { | 130 if (data.isNull()) { |
| 120 obj->pickle.WriteInt(-1); | 131 obj->pickle.WriteInt(-1); |
| 121 } else { | 132 } else { |
| 122 obj->pickle.WriteInt(data.length()); | 133 obj->pickle.WriteInt(data.length()); |
| 123 obj->pickle.WriteBytes(data.characters(), | 134 obj->pickle.WriteBytes(data.characters(), |
| 124 data.length() * sizeof(UChar)); | 135 data.length() * sizeof(UChar)); |
| 125 } | 136 } |
| 126 break; | 137 break; |
| 127 case 3: | 138 default: |
| 128 // Version 3 writes <length in bytes><string data>. | 139 // Version 3+ writes <length in bytes><string data>. |
| 129 // It uses -1 in the length field to mean String(). | 140 // It uses -1 in the length field to mean String(). |
| 130 if (data.isNull()) { | 141 if (data.isNull()) { |
| 131 obj->pickle.WriteInt(-1); | 142 obj->pickle.WriteInt(-1); |
| 132 } else { | 143 } else { |
| 133 obj->pickle.WriteInt(data.length() * sizeof(UChar)); | 144 obj->pickle.WriteInt(data.length() * sizeof(UChar)); |
| 134 obj->pickle.WriteBytes(data.characters(), | 145 obj->pickle.WriteBytes(data.characters(), |
| 135 data.length() * sizeof(UChar)); | 146 data.length() * sizeof(UChar)); |
| 136 } | 147 } |
| 137 break; | 148 break; |
| 138 default: | |
| 139 NOTREACHED(); | |
| 140 break; | |
| 141 } | 149 } |
| 142 } | 150 } |
| 143 | 151 |
| 144 // This reads a serialized String from obj. If a string can't be read, | 152 // This reads a serialized String from obj. If a string can't be read, |
| 145 // String() is returned. | 153 // String() is returned. |
| 146 inline String ReadString(const SerializeObject* obj) { | 154 inline String ReadString(const SerializeObject* obj) { |
| 147 int length; | 155 int length; |
| 148 | 156 |
| 149 // Versions 1, 2, and 3 all start with an integer. | 157 // Versions 1, 2, and 3 all start with an integer. |
| 150 if (!obj->pickle.ReadInt(&obj->iter, &length)) | 158 if (!obj->pickle.ReadInt(&obj->iter, &length)) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 for (size_t i = 0, c = form_data->elements().size(); i < c; ++i) { | 200 for (size_t i = 0, c = form_data->elements().size(); i < c; ++i) { |
| 193 const FormDataElement& e = form_data->elements().at(i); | 201 const FormDataElement& e = form_data->elements().at(i); |
| 194 WriteInteger(e.m_type, obj); | 202 WriteInteger(e.m_type, obj); |
| 195 | 203 |
| 196 if (e.m_type == FormDataElement::data) { | 204 if (e.m_type == FormDataElement::data) { |
| 197 WriteData(e.m_data.data(), static_cast<int>(e.m_data.size()), obj); | 205 WriteData(e.m_data.data(), static_cast<int>(e.m_data.size()), obj); |
| 198 } else { | 206 } else { |
| 199 WriteString(e.m_filename, obj); | 207 WriteString(e.m_filename, obj); |
| 200 } | 208 } |
| 201 } | 209 } |
| 210 WriteInteger64(form_data->identifier(), obj); | |
| 202 } | 211 } |
| 203 | 212 |
| 204 static PassRefPtr<FormData> ReadFormData(const SerializeObject* obj) { | 213 static PassRefPtr<FormData> ReadFormData(const SerializeObject* obj) { |
| 205 int num_elements = ReadInteger(obj); | 214 int num_elements = ReadInteger(obj); |
| 206 if (num_elements == 0) | 215 if (num_elements == 0) |
| 207 return NULL; | 216 return NULL; |
| 208 | 217 |
| 209 RefPtr<FormData> form_data = FormData::create(); | 218 RefPtr<FormData> form_data = FormData::create(); |
| 210 | 219 |
| 211 for (int i = 0; i < num_elements; ++i) { | 220 for (int i = 0; i < num_elements; ++i) { |
| 212 int type = ReadInteger(obj); | 221 int type = ReadInteger(obj); |
| 213 if (type == FormDataElement::data) { | 222 if (type == FormDataElement::data) { |
| 214 const void* data; | 223 const void* data; |
| 215 int length; | 224 int length; |
| 216 ReadData(obj, &data, &length); | 225 ReadData(obj, &data, &length); |
| 217 form_data->appendData(static_cast<const char*>(data), length); | 226 form_data->appendData(static_cast<const char*>(data), length); |
| 218 } else { | 227 } else { |
| 219 form_data->appendFile(ReadString(obj)); | 228 form_data->appendFile(ReadString(obj)); |
| 220 } | 229 } |
| 221 } | 230 } |
| 231 if (obj->version >= 4) | |
| 232 form_data->setIdentifier(ReadInteger64(obj)); | |
| 222 | 233 |
| 223 return form_data.release(); | 234 return form_data.release(); |
| 224 } | 235 } |
| 225 | 236 |
| 226 // Writes the HistoryItem data into the SerializeObject object for | 237 // Writes the HistoryItem data into the SerializeObject object for |
| 227 // serialization. | 238 // serialization. |
| 228 static void WriteHistoryItem(const HistoryItem* item, SerializeObject* obj) { | 239 static void WriteHistoryItem(const HistoryItem* item, SerializeObject* obj) { |
| 229 // WARNING: This data may be persisted for later use. As such, care must be | 240 // WARNING: This data may be persisted for later use. As such, care must be |
| 230 // taken when changing the serialized format. If a new field needs to be | 241 // taken when changing the serialized format. If a new field needs to be |
| 231 // written, only adding at the end will make it easier to deal with loading | 242 // written, only adding at the end will make it easier to deal with loading |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 257 for (size_t i = 0, c = item->children().size(); i < c; ++i) | 268 for (size_t i = 0, c = item->children().size(); i < c; ++i) |
| 258 WriteHistoryItem(item->children().at(i).get(), obj); | 269 WriteHistoryItem(item->children().at(i).get(), obj); |
| 259 } | 270 } |
| 260 | 271 |
| 261 // Creates a new HistoryItem tree based on the serialized string. | 272 // Creates a new HistoryItem tree based on the serialized string. |
| 262 // Assumes the data is in the format returned by WriteHistoryItem. | 273 // Assumes the data is in the format returned by WriteHistoryItem. |
| 263 static PassRefPtr<HistoryItem> ReadHistoryItem(const SerializeObject* obj) { | 274 static PassRefPtr<HistoryItem> ReadHistoryItem(const SerializeObject* obj) { |
| 264 // See note in WriteHistoryItem. on this. | 275 // See note in WriteHistoryItem. on this. |
| 265 obj->version = ReadInteger(obj); | 276 obj->version = ReadInteger(obj); |
| 266 | 277 |
| 267 if (obj->version > kVersion) | 278 if (obj->version > kVersion || obj->version < 1) |
| 268 return NULL; | 279 return NULL; |
| 269 | 280 |
| 270 RefPtr<HistoryItem> item = HistoryItem::create(); | 281 RefPtr<HistoryItem> item = HistoryItem::create(); |
| 271 | 282 |
| 272 item->setURLString(ReadString(obj)); | 283 item->setURLString(ReadString(obj)); |
| 273 item->setOriginalURLString(ReadString(obj)); | 284 item->setOriginalURLString(ReadString(obj)); |
| 274 item->setTarget(ReadString(obj)); | 285 item->setTarget(ReadString(obj)); |
| 275 item->setParent(ReadString(obj)); | 286 item->setParent(ReadString(obj)); |
| 276 item->setTitle(ReadString(obj)); | 287 item->setTitle(ReadString(obj)); |
| 277 item->setAlternateTitle(ReadString(obj)); | 288 item->setAlternateTitle(ReadString(obj)); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 | 364 |
| 354 std::string CreateHistoryStateForURL(const GURL& url) { | 365 std::string CreateHistoryStateForURL(const GURL& url) { |
| 355 // TODO(eseide): We probably should be passing a list visit time other than 0 | 366 // TODO(eseide): We probably should be passing a list visit time other than 0 |
| 356 RefPtr<HistoryItem> item(HistoryItem::create(GURLToKURL(url), String(), 0)); | 367 RefPtr<HistoryItem> item(HistoryItem::create(GURLToKURL(url), String(), 0)); |
| 357 std::string data; | 368 std::string data; |
| 358 HistoryItemToString(item, &data); | 369 HistoryItemToString(item, &data); |
| 359 return data; | 370 return data; |
| 360 } | 371 } |
| 361 | 372 |
| 362 } // namespace webkit_glue | 373 } // namespace webkit_glue |
| OLD | NEW |