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