| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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.h" | 5 #include "webkit/glue/glue_serialize.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 12 #include "webkit/api/public/WebData.h" | 12 #include "webkit/api/public/WebData.h" |
| 13 #include "webkit/api/public/WebHistoryItem.h" | 13 #include "webkit/api/public/WebHistoryItem.h" |
| 14 #include "webkit/api/public/WebHTTPBody.h" | 14 #include "webkit/api/public/WebHTTPBody.h" |
| 15 #include "webkit/api/public/WebHTTPHeaderVisitor.h" |
| 15 #include "webkit/api/public/WebPoint.h" | 16 #include "webkit/api/public/WebPoint.h" |
| 16 #include "webkit/api/public/WebString.h" | 17 #include "webkit/api/public/WebString.h" |
| 17 #include "webkit/api/public/WebVector.h" | 18 #include "webkit/api/public/WebVector.h" |
| 18 #include "webkit/glue/glue_util.h" | 19 #include "webkit/glue/glue_util.h" |
| 19 #include "webkit/glue/webkit_glue.h" | 20 #include "webkit/glue/webkit_glue.h" |
| 20 | 21 |
| 21 using WebKit::WebData; | 22 using WebKit::WebData; |
| 22 using WebKit::WebHistoryItem; | 23 using WebKit::WebHistoryItem; |
| 23 using WebKit::WebHTTPBody; | 24 using WebKit::WebHTTPBody; |
| 25 using WebKit::WebHTTPHeaderVisitor; |
| 24 using WebKit::WebPoint; | 26 using WebKit::WebPoint; |
| 25 using WebKit::WebString; | 27 using WebKit::WebString; |
| 26 using WebKit::WebUChar; | 28 using WebKit::WebUChar; |
| 27 using WebKit::WebVector; | 29 using WebKit::WebVector; |
| 28 | 30 |
| 29 namespace webkit_glue { | 31 namespace webkit_glue { |
| 30 | 32 |
| 31 struct SerializeObject { | 33 struct SerializeObject { |
| 32 SerializeObject() : iter(NULL) {} | 34 SerializeObject() : iter(NULL) {} |
| 33 SerializeObject(const char* data, int len) : pickle(data, len), iter(NULL) {} | 35 SerializeObject(const char* data, int len) : pickle(data, len), iter(NULL) {} |
| 34 | 36 |
| 35 std::string GetAsString() { | 37 std::string GetAsString() { |
| 36 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); | 38 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); |
| 37 } | 39 } |
| 38 | 40 |
| 39 Pickle pickle; | 41 Pickle pickle; |
| 40 mutable void* iter; | 42 mutable void* iter; |
| 41 mutable int version; | 43 mutable int version; |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. | 46 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. |
| 45 // Version ID used in reading/writing history items. | 47 // Version ID used in reading/writing history items. |
| 46 // 1: Initial revision. | 48 // 1: Initial revision. |
| 47 // 2: Added case for NULL string versus "". Version 2 code can read Version 1 | 49 // 2: Added case for NULL string versus "". Version 2 code can read Version 1 |
| 48 // data, but not vice versa. | 50 // data, but not vice versa. |
| 49 // 3: Version 2 was broken, it stored number of WebUChars, not number of bytes. | 51 // 3: Version 2 was broken, it stored number of WebUChars, not number of bytes. |
| 50 // This version checks and reads v1 and v2 correctly. | 52 // This version checks and reads v1 and v2 correctly. |
| 51 // 4: Adds support for storing FormData::identifier(). | 53 // 4: Adds support for storing FormData::identifier(). |
| 52 // 5: Adds support for empty FormData | 54 // 5: Adds support for empty FormData |
| 55 // 6: Adds support for extra HTTP headers. |
| 53 // Should be const, but unit tests may modify it. | 56 // Should be const, but unit tests may modify it. |
| 54 int kVersion = 5; | 57 int kVersion = 6; |
| 55 | 58 |
| 56 // A bunch of convenience functions to read/write to SerializeObjects. | 59 // A bunch of convenience functions to read/write to SerializeObjects. |
| 57 // The serializers assume the input data is in the correct format and so does | 60 // The serializers assume the input data is in the correct format and so does |
| 58 // no error checking. | 61 // no error checking. |
| 59 inline void WriteData(const void* data, int length, SerializeObject* obj) { | 62 inline void WriteData(const void* data, int length, SerializeObject* obj) { |
| 60 obj->pickle.WriteData(static_cast<const char*>(data), length); | 63 obj->pickle.WriteData(static_cast<const char*>(data), length); |
| 61 } | 64 } |
| 62 | 65 |
| 63 inline void ReadData(const SerializeObject* obj, const void** data, | 66 inline void ReadData(const SerializeObject* obj, const void** data, |
| 64 int* length) { | 67 int* length) { |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 // compatibility with the format. | 281 // compatibility with the format. |
| 279 WriteFormData(item.httpBody(), obj); | 282 WriteFormData(item.httpBody(), obj); |
| 280 WriteString(item.httpContentType(), obj); | 283 WriteString(item.httpContentType(), obj); |
| 281 WriteString(item.referrer(), obj); | 284 WriteString(item.referrer(), obj); |
| 282 | 285 |
| 283 // Subitems | 286 // Subitems |
| 284 const WebVector<WebHistoryItem>& children = item.children(); | 287 const WebVector<WebHistoryItem>& children = item.children(); |
| 285 WriteInteger(static_cast<int>(children.size()), obj); | 288 WriteInteger(static_cast<int>(children.size()), obj); |
| 286 for (size_t i = 0, c = children.size(); i < c; ++i) | 289 for (size_t i = 0, c = children.size(); i < c; ++i) |
| 287 WriteHistoryItem(children[i], obj); | 290 WriteHistoryItem(children[i], obj); |
| 291 |
| 292 // Extra HTTP headers. |
| 293 std::map<string16, string16> header_map; |
| 294 class HeaderMapVisitor : public WebHTTPHeaderVisitor { |
| 295 public: |
| 296 HeaderMapVisitor(std::map<string16, string16>* header_map) { |
| 297 map_ = header_map; |
| 298 } |
| 299 virtual void visitHeader(const WebString& name, const WebString& value) { |
| 300 map_->insert(std::make_pair(name, value)); |
| 301 } |
| 302 private: |
| 303 std::map<string16, string16>* map_; |
| 304 } header_map_visitor(&header_map); |
| 305 item.visitHTTPHeaderFields(&header_map_visitor); |
| 306 WriteInteger(static_cast<int>(header_map.size()), obj); |
| 307 std::map<string16, string16>::const_iterator it = header_map.begin(); |
| 308 for (; it != header_map.end(); ++it) { |
| 309 WriteString(it->first, obj); |
| 310 WriteString(it->second, obj); |
| 311 } |
| 288 } | 312 } |
| 289 | 313 |
| 290 // Creates a new HistoryItem tree based on the serialized string. | 314 // Creates a new HistoryItem tree based on the serialized string. |
| 291 // Assumes the data is in the format returned by WriteHistoryItem. | 315 // Assumes the data is in the format returned by WriteHistoryItem. |
| 292 static WebHistoryItem ReadHistoryItem( | 316 static WebHistoryItem ReadHistoryItem( |
| 293 const SerializeObject* obj, bool include_form_data) { | 317 const SerializeObject* obj, bool include_form_data) { |
| 294 // See note in WriteHistoryItem. on this. | 318 // See note in WriteHistoryItem. on this. |
| 295 obj->version = ReadInteger(obj); | 319 obj->version = ReadInteger(obj); |
| 296 | 320 |
| 297 if (obj->version > kVersion || obj->version < 1) | 321 if (obj->version > kVersion || obj->version < 1) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 323 if (include_form_data) { | 347 if (include_form_data) { |
| 324 item.setHTTPBody(http_body); | 348 item.setHTTPBody(http_body); |
| 325 item.setHTTPContentType(http_content_type); | 349 item.setHTTPContentType(http_content_type); |
| 326 } | 350 } |
| 327 | 351 |
| 328 // Subitems | 352 // Subitems |
| 329 int num_children = ReadInteger(obj); | 353 int num_children = ReadInteger(obj); |
| 330 for (int i = 0; i < num_children; ++i) | 354 for (int i = 0; i < num_children; ++i) |
| 331 item.appendToChildren(ReadHistoryItem(obj, include_form_data)); | 355 item.appendToChildren(ReadHistoryItem(obj, include_form_data)); |
| 332 | 356 |
| 357 if (obj->version >= 6) { |
| 358 // Extra HTTP headers. |
| 359 int num_extra_headers = ReadInteger(obj); |
| 360 if (num_extra_headers > 0) { |
| 361 WebString name, value; |
| 362 for (int i = 0; i < num_extra_headers; ++i) { |
| 363 name = ReadString(obj); |
| 364 value = ReadString(obj); |
| 365 item.setHTTPHeaderField(name, value); |
| 366 } |
| 367 } |
| 368 } |
| 369 |
| 333 return item; | 370 return item; |
| 334 } | 371 } |
| 335 | 372 |
| 336 // Serialize a HistoryItem to a string, using our JSON Value serializer. | 373 // Serialize a HistoryItem to a string, using our JSON Value serializer. |
| 337 std::string HistoryItemToString(const WebHistoryItem& item) { | 374 std::string HistoryItemToString(const WebHistoryItem& item) { |
| 338 if (item.isNull()) | 375 if (item.isNull()) |
| 339 return std::string(); | 376 return std::string(); |
| 340 | 377 |
| 341 SerializeObject obj; | 378 SerializeObject obj; |
| 342 WriteHistoryItem(item, &obj); | 379 WriteHistoryItem(item, &obj); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 const WebHistoryItem& item = HistoryItemFromString(content_state, false); | 431 const WebHistoryItem& item = HistoryItemFromString(content_state, false); |
| 395 if (item.isNull()) { | 432 if (item.isNull()) { |
| 396 // Couldn't parse the string, return an empty string. | 433 // Couldn't parse the string, return an empty string. |
| 397 return std::string(); | 434 return std::string(); |
| 398 } | 435 } |
| 399 | 436 |
| 400 return HistoryItemToString(item); | 437 return HistoryItemToString(item); |
| 401 } | 438 } |
| 402 | 439 |
| 403 } // namespace webkit_glue | 440 } // namespace webkit_glue |
| OLD | NEW |