HistoryItemQt.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include "config.h"
  17. #include "HistoryItem.h"
  18. #include "FormData.h"
  19. #include <wtf/Decoder.h>
  20. #include <wtf/Encoder.h>
  21. #include <wtf/text/CString.h>
  22. using namespace WTF;
  23. namespace WebCore {
  24. static QDataStream& operator<<(QDataStream& stream, const String& str)
  25. {
  26. // could be faster
  27. stream << QString(str);
  28. return stream;
  29. }
  30. static QDataStream& operator>>(QDataStream& stream, String& str)
  31. {
  32. // mabe not the fastest way, but really easy
  33. QString tmp;
  34. stream >> tmp;
  35. str = tmp;
  36. return stream;
  37. }
  38. class QDataStreamCoder : public WTF::Encoder, public WTF::Decoder {
  39. public:
  40. QDataStreamCoder(QDataStream&);
  41. private:
  42. virtual void encodeBytes(const uint8_t*, size_t);
  43. virtual void encodeBool(bool);
  44. virtual void encodeUInt32(uint32_t);
  45. virtual void encodeUInt64(uint64_t);
  46. virtual void encodeInt32(int32_t);
  47. virtual void encodeInt64(int64_t);
  48. virtual void encodeFloat(float);
  49. virtual void encodeDouble(double);
  50. virtual void encodeString(const String&);
  51. virtual bool decodeBytes(Vector<uint8_t>&);
  52. virtual bool decodeBool(bool&);
  53. virtual bool decodeUInt32(uint32_t&);
  54. virtual bool decodeUInt64(uint64_t&);
  55. virtual bool decodeInt32(int32_t&);
  56. virtual bool decodeInt64(int64_t&);
  57. virtual bool decodeFloat(float&);
  58. virtual bool decodeDouble(double&);
  59. virtual bool decodeString(String&);
  60. QDataStream& m_stream;
  61. };
  62. QDataStreamCoder::QDataStreamCoder(QDataStream& stream)
  63. : m_stream(stream)
  64. {
  65. }
  66. void QDataStreamCoder::encodeBytes(const uint8_t* bytes, size_t size)
  67. {
  68. m_stream << qint64(size);
  69. for (; size > 0; --size)
  70. m_stream << bytes++;
  71. }
  72. void QDataStreamCoder::encodeBool(bool value)
  73. {
  74. m_stream << value;
  75. }
  76. void QDataStreamCoder::encodeUInt32(uint32_t value)
  77. {
  78. m_stream << value;
  79. }
  80. void QDataStreamCoder::encodeUInt64(uint64_t value)
  81. {
  82. m_stream << static_cast<quint64>(value);
  83. }
  84. void QDataStreamCoder::encodeInt32(int32_t value)
  85. {
  86. m_stream << value;
  87. }
  88. void QDataStreamCoder::encodeInt64(int64_t value)
  89. {
  90. m_stream << static_cast<qint64>(value);
  91. }
  92. void QDataStreamCoder::encodeFloat(float value)
  93. {
  94. m_stream << value;
  95. }
  96. void QDataStreamCoder::encodeDouble(double value)
  97. {
  98. m_stream << value;
  99. }
  100. void QDataStreamCoder::encodeString(const String& value)
  101. {
  102. m_stream << value;
  103. }
  104. bool QDataStreamCoder::decodeBytes(Vector<uint8_t>& out)
  105. {
  106. out.clear();
  107. qint64 count;
  108. uint8_t byte;
  109. m_stream >> count;
  110. out.reserveCapacity(count);
  111. for (qint64 i = 0; i < count; ++i) {
  112. m_stream >> byte;
  113. out.append(byte);
  114. }
  115. return m_stream.status() == QDataStream::Ok;
  116. }
  117. bool QDataStreamCoder::decodeBool(bool& out)
  118. {
  119. m_stream >> out;
  120. return m_stream.status() == QDataStream::Ok;
  121. }
  122. bool QDataStreamCoder::decodeUInt32(uint32_t& out)
  123. {
  124. m_stream >> out;
  125. return m_stream.status() == QDataStream::Ok;
  126. }
  127. bool QDataStreamCoder::decodeUInt64(uint64_t& out)
  128. {
  129. quint64 tmp;
  130. m_stream >> tmp;
  131. // quint64 is defined to "long long unsigned", incompatible with uint64_t defined as "long unsigned" on 64bits archs.
  132. out = tmp;
  133. return m_stream.status() == QDataStream::Ok;
  134. }
  135. bool QDataStreamCoder::decodeInt32(int32_t& out)
  136. {
  137. m_stream >> out;
  138. return m_stream.status() == QDataStream::Ok;
  139. }
  140. bool QDataStreamCoder::decodeInt64(int64_t& out)
  141. {
  142. qint64 tmp;
  143. m_stream >> tmp;
  144. // qint64 is defined to "long long", incompatible with int64_t defined as "long" on 64bits archs.
  145. out = tmp;
  146. return m_stream.status() == QDataStream::Ok;
  147. }
  148. bool QDataStreamCoder::decodeFloat(float& out)
  149. {
  150. m_stream >> out;
  151. return m_stream.status() == QDataStream::Ok;
  152. }
  153. bool QDataStreamCoder::decodeDouble(double& out)
  154. {
  155. m_stream >> out;
  156. return m_stream.status() == QDataStream::Ok;
  157. }
  158. bool QDataStreamCoder::decodeString(String& out)
  159. {
  160. m_stream >> out;
  161. return m_stream.status() == QDataStream::Ok;
  162. }
  163. PassRefPtr<HistoryItem> HistoryItem::restoreState(QDataStream& in, int version)
  164. {
  165. ASSERT(version == 2);
  166. String url;
  167. String title;
  168. String originalURL;
  169. in >> url >> title >> originalURL;
  170. QDataStreamCoder decoder(in);
  171. RefPtr<HistoryItem> item = decodeBackForwardTree(url, title, originalURL, decoder);
  172. // decodeBackForwardTree has its own stream version. An incompatible input stream version will return null here.
  173. if (!item)
  174. return 0;
  175. // at the end load userData
  176. bool validUserData;
  177. in >> validUserData;
  178. if (validUserData) {
  179. QVariant tmp;
  180. in >> tmp;
  181. item->setUserData(tmp);
  182. }
  183. return item;
  184. }
  185. QDataStream& WebCore::HistoryItem::saveState(QDataStream& out, int version) const
  186. {
  187. ASSERT(version == 2);
  188. out << urlString() << title() << originalURLString();
  189. QDataStreamCoder encoder(out);
  190. encodeBackForwardTree(encoder);
  191. // save user data
  192. if (userData().isValid())
  193. out << true << userData();
  194. else
  195. out << false;
  196. return out;
  197. }
  198. } // namespace WebCore