コード例 #1
0
/*!
    Cleans the cache so that its size is under the maximum cache size.
    Returns the current size of the cache.

    When the current size of the cache is greater than the maximumCacheSize()
    older cache files are removed until the total size is less then 90% of
    maximumCacheSize() starting with the oldest ones first using the file
    creation date to determine how old a cache file is.

    Subclasses can reimplement this function to change the order that cache
    files are removed taking into account information in the application
    knows about that QNetworkDiskCache does not, for example the number of times
    a cache is accessed.

    Note: cacheSize() calls expire if the current cache size is unknown.

    \sa maximumCacheSize(), fileMetaData()
 */
qint64 QNetworkDiskCache::expire()
{
    Q_D(QNetworkDiskCache);
    if (d->currentCacheSize >= 0 && d->currentCacheSize < maximumCacheSize())
        return d->currentCacheSize;

    if (cacheDirectory().isEmpty()) {
        qWarning() << "QNetworkDiskCache::expire() The cache directory is not set";
        return 0;
    }

    // close file handle to prevent "in use" error when QFile::remove() is called
    d->lastItem.reset();

    QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot;
    QDirIterator it(cacheDirectory(), filters, QDirIterator::Subdirectories);

    QMultiMap<QDateTime, QString> cacheItems;
    qint64 totalSize = 0;
    while (it.hasNext()) {
        QString path = it.next();
        QFileInfo info = it.fileInfo();
        QString fileName = info.fileName();
        if (fileName.endsWith(CACHE_POSTFIX)) {
            cacheItems.insert(info.created(), path);
            totalSize += info.size();
        }
    }

    int removedFiles = 0;
    qint64 goal = (maximumCacheSize() * 9) / 10;
    QMultiMap<QDateTime, QString>::const_iterator i = cacheItems.constBegin();
    while (i != cacheItems.constEnd()) {
        if (totalSize < goal)
            break;
        QString name = i.value();
        QFile file(name);
        qint64 size = file.size();
        file.remove();
        totalSize -= size;
        ++removedFiles;
        ++i;
    }
#if defined(QNETWORKDISKCACHE_DEBUG)
    if (removedFiles > 0) {
        qDebug() << "QNetworkDiskCache::expire()"
                << "Removed:" << removedFiles
                << "Kept:" << cacheItems.count() - removedFiles;
    }
#endif
    return totalSize;
}
コード例 #2
0
PluginInfoCache::PluginInfoCache()
    : m_cacheFile(g_key_file_new())
    , m_readOnlyMode(false)
{
    GUniquePtr<char> cacheDirectory(g_build_filename(g_get_user_cache_dir(), "webkitgtk", nullptr));
    if (WebCore::makeAllDirectories(cacheDirectory.get())) {
        m_cachePath.reset(g_build_filename(cacheDirectory.get(), "plugins", nullptr));
        g_key_file_load_from_file(m_cacheFile.get(), m_cachePath.get(), G_KEY_FILE_NONE, nullptr);
    }

    if (g_key_file_has_group(m_cacheFile.get(), "schema")) {
        unsigned schemaVersion = static_cast<unsigned>(g_key_file_get_integer(m_cacheFile.get(), "schema", "version", nullptr));
        if (schemaVersion < gSchemaVersion) {
            // Cache file using an old schema, create a new empty file.
            m_cacheFile.reset(g_key_file_new());
        } else if (schemaVersion > gSchemaVersion) {
            // Cache file using a newer schema, use the cache in read only mode.
            m_readOnlyMode = true;
        } else {
            // Same schema version, we don't need to update it.
            return;
        }
    }

    g_key_file_set_integer(m_cacheFile.get(), "schema", "version", static_cast<unsigned>(gSchemaVersion));
}
コード例 #3
0
ファイル: PluginInfoCache.cpp プロジェクト: endlessm/WebKit
PluginInfoCache::PluginInfoCache()
    : m_cacheFile(g_key_file_new())
    , m_saveToFileIdle(RunLoop::main(), this, &PluginInfoCache::saveToFile)
    , m_readOnlyMode(false)
{
    m_saveToFileIdle.setPriority(G_PRIORITY_DEFAULT_IDLE);

    GUniquePtr<char> cacheDirectory(g_build_filename(g_get_user_cache_dir(), "webkitgtk", nullptr));
    if (WebCore::makeAllDirectories(cacheDirectory.get())) {
        // Delete old cache file.
        GUniquePtr<char> oldCachePath(g_build_filename(cacheDirectory.get(), "plugins", nullptr));
        WebCore::deleteFile(WebCore::filenameToString(oldCachePath.get()));

        m_cachePath.reset(g_build_filename(cacheDirectory.get(), cacheFilenameForCurrentDisplay(), nullptr));
        g_key_file_load_from_file(m_cacheFile.get(), m_cachePath.get(), G_KEY_FILE_NONE, nullptr);
    }

    if (g_key_file_has_group(m_cacheFile.get(), "schema")) {
        unsigned schemaVersion = static_cast<unsigned>(g_key_file_get_integer(m_cacheFile.get(), "schema", "version", nullptr));
        if (schemaVersion < gSchemaVersion) {
            // Cache file using an old schema, create a new empty file.
            m_cacheFile.reset(g_key_file_new());
        } else if (schemaVersion > gSchemaVersion) {
            // Cache file using a newer schema, use the cache in read only mode.
            m_readOnlyMode = true;
        } else {
            // Same schema version, we don't need to update it.
            return;
        }
    }

    g_key_file_set_integer(m_cacheFile.get(), "schema", "version", static_cast<unsigned>(gSchemaVersion));
}
コード例 #4
0
	void NetworkDiskCache::collectGarbage ()
	{
		if (GarbageCollectorWatcher_)
			return;

		if (cacheDirectory ().isEmpty ())
			return;

		GarbageCollectorWatcher_ = new QFutureWatcher<qint64> (this);
		connect (GarbageCollectorWatcher_,
				SIGNAL (finished ()),
				this,
				SLOT (handleCollectorFinished ()));

		auto future = QtConcurrent::run (Collector,
				cacheDirectory (), maximumCacheSize () * 9 / 10, &InsertRemoveMutex_);
		GarbageCollectorWatcher_->setFuture (future);
	}
コード例 #5
0
ファイル: WebContextGtk.cpp プロジェクト: kcomkar/webkit
WTF::String WebContext::applicationCacheDirectory()
{
    GOwnPtr<gchar> cacheDirectory(g_build_filename(g_get_user_cache_dir(), "webkitgtk", "applications", NULL));
    return WebCore::filenameToString(cacheDirectory.get());
}
コード例 #6
0
WTF::String WebContext::platformDefaultApplicationCacheDirectory() const
{
    GUniquePtr<gchar> cacheDirectory(g_build_filename(g_get_user_cache_dir(), "webkitgtk", "applications", nullptr));
    return WebCore::filenameToString(cacheDirectory.get());
}