16
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
*
|
|
5 |
* This file is part of Qt Web Runtime.
|
|
6 |
*
|
|
7 |
* This library is free software; you can redistribute it and/or
|
|
8 |
* modify it under the terms of the GNU Lesser General Public License
|
|
9 |
* version 2.1 as published by the Free Software Foundation.
|
|
10 |
*
|
|
11 |
* This library is distributed in the hope that it will be useful,
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 |
* Lesser General Public License for more details.
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU Lesser General Public
|
|
17 |
* License along with this library; if not, write to the Free Software
|
|
18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
|
20 |
*/
|
|
21 |
|
|
22 |
#ifndef FEATHERWEIGHTCACHE_P_H
|
|
23 |
#define FEATHERWEIGHTCACHE_P_H
|
|
24 |
|
|
25 |
#include <QBuffer>
|
|
26 |
#include <QHash>
|
|
27 |
#include <QTemporaryFile>
|
|
28 |
#include <QFile>
|
|
29 |
#include <QNetworkCacheMetaData>
|
|
30 |
#include <QObject>
|
|
31 |
#include <QQueue>
|
|
32 |
#include <QMutex>
|
|
33 |
#include <QWaitCondition>
|
|
34 |
#include <cacheworkerthread.h>
|
|
35 |
|
|
36 |
namespace WRT {
|
|
37 |
class CacheItem
|
|
38 |
{
|
|
39 |
public:
|
|
40 |
CacheItem() : file(0)
|
|
41 |
{
|
|
42 |
}
|
|
43 |
~CacheItem()
|
|
44 |
{
|
|
45 |
reset();
|
|
46 |
}
|
|
47 |
|
|
48 |
QNetworkCacheMetaData metaData;
|
|
49 |
QBuffer data;
|
|
50 |
QTemporaryFile *file;
|
|
51 |
inline qint64 size() const
|
|
52 |
{ return file ? file->size() : data.size(); }
|
|
53 |
|
|
54 |
inline void reset() {
|
|
55 |
metaData = QNetworkCacheMetaData();
|
|
56 |
data.close();
|
|
57 |
delete file;
|
|
58 |
file = 0;
|
|
59 |
}
|
|
60 |
void writeHeader(QFile *device) const;
|
|
61 |
void writeCompressedData(QFile *device) const;
|
|
62 |
bool read(QFile *device, bool readData);
|
|
63 |
|
|
64 |
bool canCompress() const;
|
|
65 |
};
|
|
66 |
|
|
67 |
|
|
68 |
class WorkerThread : public QThread
|
|
69 |
{
|
|
70 |
|
|
71 |
Q_OBJECT
|
|
72 |
|
|
73 |
public:
|
|
74 |
WorkerThread();
|
|
75 |
~WorkerThread();
|
|
76 |
|
|
77 |
void expireLazily(QString cacheDir, qint64 maxCacheSize);
|
|
78 |
|
|
79 |
protected:
|
|
80 |
void run();
|
|
81 |
|
|
82 |
private:
|
|
83 |
QMutex mutex;
|
|
84 |
QWaitCondition condition;
|
|
85 |
bool abort;
|
|
86 |
qint64 expireImpl();
|
|
87 |
|
|
88 |
QString cacheDir;
|
|
89 |
qint64 maxCacheSize;
|
|
90 |
|
|
91 |
signals:
|
|
92 |
void onDiskSizeChanged(qint64 newSize);
|
|
93 |
|
|
94 |
};
|
|
95 |
|
|
96 |
|
|
97 |
#define URL2HASH(url) FeatherWeightCachePrivate::generateId(url).toUInt()
|
|
98 |
|
|
99 |
// Assume this much higher physical disk usage. Platform and media dependent
|
|
100 |
// TODO: On Symbian, we could instead round up number to multiple of TVolumeIOParamInfo.iClusterSize
|
|
101 |
#define FILESYSTEMOVERHEAD 2048
|
|
102 |
#define ROUNDUPTOMULTIPLE(x, multipleof) ( (x + multipleof - 1) & ~(multipleof - 1) )
|
|
103 |
|
|
104 |
class FeatherWeightCachePrivate : public QObject
|
|
105 |
{
|
|
106 |
Q_OBJECT
|
|
107 |
|
|
108 |
public:
|
|
109 |
FeatherWeightCachePrivate(QObject* parent): QObject(parent)
|
|
110 |
, maximumCacheSize(1024 * 1024 * 10) //set the maximum default cache to 10M
|
|
111 |
, currentCacheSize(-1)
|
|
112 |
{
|
|
113 |
// Queued connection because onDiskSizeChanged() is currently always triggered from run() method of worker thread
|
|
114 |
QObject::connect( &beastOfBurden, SIGNAL( onDiskSizeChanged(qint64) ), this, SLOT( updateCacheSize(qint64) ), Qt::DirectConnection );
|
|
115 |
}
|
|
116 |
|
|
117 |
~FeatherWeightCachePrivate()
|
|
118 |
{
|
|
119 |
// When beastOfBurden's d'tor is called
|
|
120 |
// it will wait() and then auto-terminate
|
|
121 |
|
|
122 |
QObject::disconnect(&beastOfBurden, SIGNAL( onDiskSizeChanged(qint64) ), this, SLOT( updateCacheSize(qint64)));
|
|
123 |
}
|
|
124 |
|
|
125 |
qint64 expire();
|
|
126 |
static QByteArray generateId(const QUrl &url);
|
|
127 |
QString cacheFileName(const QUrl &url) const;
|
|
128 |
QString tmpCacheFileName() const;
|
|
129 |
bool removeFile(const QString &file);
|
|
130 |
void storeItem(CacheItem *item);
|
|
131 |
void prepareLayout();
|
|
132 |
static quint32 crc32(const char *data, uint len);
|
|
133 |
|
|
134 |
mutable CacheItem lastItem;
|
|
135 |
QString cacheDirectory;
|
|
136 |
qint64 maximumCacheSize;
|
|
137 |
qint64 currentCacheSize;
|
|
138 |
|
|
139 |
QHash<QIODevice*, CacheItem*> inserting;
|
|
140 |
|
|
141 |
WorkerThread beastOfBurden;
|
|
142 |
|
|
143 |
// a look up table (LUT) that stores file sizes
|
|
144 |
// corresponding to cached URLs. Helps speed up
|
|
145 |
// cache eviction or expiration logic.
|
|
146 |
// key = CRC32 hash of url , value = size of file on disk
|
|
147 |
// QHash<quint32, qint64> lut;
|
|
148 |
|
|
149 |
// bool isTrimPending;
|
|
150 |
|
|
151 |
//Recommended buffer sizes for fast IO on caching volume
|
|
152 |
struct {
|
|
153 |
quint32 readBufSize;
|
|
154 |
quint32 writeBufSize;
|
|
155 |
quint32 clusterSize;
|
|
156 |
} volumeInfo;
|
|
157 |
|
|
158 |
public slots:
|
|
159 |
void updateCacheSize(qint64);
|
|
160 |
};
|
|
161 |
}
|
|
162 |
|
|
163 |
#endif // FEATHERWEIGHTCACHE_P_H
|