The "Clear Disk Caches" command also deletes the HTTP cache used by QtWebEngine

Note that this only clears the HTTP cache (used for files), but not the directory used for
persistent data such as Cookies, Local Databases, etc. There's no Qt API to do that.
Fortunately, this directory should be small.
This commit is contained in:
Oren Hurvitz 2018-06-18 14:53:32 +03:00
parent ef163c836d
commit d7ef7a052d
5 changed files with 54 additions and 1 deletions

View file

@ -29,6 +29,8 @@
#include <VrMenu.h>
#include <ScriptEngines.h>
#include <MenuItemProperties.h>
#include <ui/types/FileTypeProfile.h>
#include <ui/types/HFWebEngineProfile.h>
#include "Application.h"
#include "AccountManager.h"
@ -635,8 +637,12 @@ Menu::Menu() {
action = addActionToQMenuAndActionHash(networkMenu, MenuOption::ClearDiskCaches);
connect(action, &QAction::triggered, [] {
// This cache is cleared immediately
// The following caches are cleared immediately
DependencyManager::get<AssetClient>()->clearCache();
#ifndef Q_OS_ANDROID
FileTypeProfile::clearCache();
HFWebEngineProfile::clearCache();
#endif
// Clear the KTX cache on the next restart. It can't be cleared immediately because its files might be in use.
Setting::Handle<int>(KTXCache::SETTING_VERSION_NAME, KTXCache::INVALID_VERSION).set(KTXCache::INVALID_VERSION);

View file

@ -11,6 +11,8 @@
#include "FileTypeProfile.h"
#include <set>
#include <mutex>
#include <QtQml/QQmlContext>
#include "RequestFilters.h"
@ -18,6 +20,9 @@
#if !defined(Q_OS_ANDROID)
static const QString QML_WEB_ENGINE_STORAGE_NAME = "qmlWebEngine";
static std::set<FileTypeProfile*> FileTypeProfile_instances;
static std::mutex FileTypeProfile_mutex;
FileTypeProfile::FileTypeProfile(QQmlContext* parent) :
ContextAwareProfile(parent)
{
@ -29,6 +34,14 @@ FileTypeProfile::FileTypeProfile(QQmlContext* parent) :
auto requestInterceptor = new RequestInterceptor(this);
setRequestInterceptor(requestInterceptor);
std::lock_guard<std::mutex> lock(FileTypeProfile_mutex);
FileTypeProfile_instances.insert(this);
}
FileTypeProfile::~FileTypeProfile() {
std::lock_guard<std::mutex> lock(FileTypeProfile_mutex);
FileTypeProfile_instances.erase(this);
}
void FileTypeProfile::RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) {
@ -40,5 +53,11 @@ void FileTypeProfile::registerWithContext(QQmlContext* context) {
context->setContextProperty("FileTypeProfile", new FileTypeProfile(context));
}
void FileTypeProfile::clearCache() {
std::lock_guard<std::mutex> lock(FileTypeProfile_mutex);
foreach (auto instance, FileTypeProfile_instances) {
instance->clearHttpCache();
}
}
#endif

View file

@ -25,8 +25,12 @@ class FileTypeProfile : public ContextAwareProfile {
public:
static void registerWithContext(QQmlContext* parent);
static void clearCache();
protected:
FileTypeProfile(QQmlContext* parent);
virtual ~FileTypeProfile();
class RequestInterceptor : public Parent::RequestInterceptor {
public:
RequestInterceptor(ContextAwareProfile* parent) : Parent::RequestInterceptor(parent) {}

View file

@ -11,6 +11,8 @@
#include "HFWebEngineProfile.h"
#include <set>
#include <mutex>
#include <QtQml/QQmlContext>
#include "RequestFilters.h"
@ -19,6 +21,9 @@
static const QString QML_WEB_ENGINE_STORAGE_NAME = "qmlWebEngine";
static std::set<HFWebEngineProfile*> HFWebEngineProfile_instances;
static std::mutex HFWebEngineProfile_mutex;
HFWebEngineProfile::HFWebEngineProfile(QQmlContext* parent) : Parent(parent)
{
setStorageName(QML_WEB_ENGINE_STORAGE_NAME);
@ -26,6 +31,14 @@ HFWebEngineProfile::HFWebEngineProfile(QQmlContext* parent) : Parent(parent)
// we use the HFWebEngineRequestInterceptor to make sure that web requests are authenticated for the interface user
setRequestInterceptor(new RequestInterceptor(this));
std::lock_guard<std::mutex> lock(HFWebEngineProfile_mutex);
HFWebEngineProfile_instances.insert(this);
}
HFWebEngineProfile::~HFWebEngineProfile() {
std::lock_guard<std::mutex> lock(HFWebEngineProfile_mutex);
HFWebEngineProfile_instances.erase(this);
}
void HFWebEngineProfile::RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) {
@ -36,4 +49,11 @@ void HFWebEngineProfile::registerWithContext(QQmlContext* context) {
context->setContextProperty("HFWebEngineProfile", new HFWebEngineProfile(context));
}
void HFWebEngineProfile::clearCache() {
std::lock_guard<std::mutex> lock(HFWebEngineProfile_mutex);
foreach (auto instance, HFWebEngineProfile_instances) {
instance->clearHttpCache();
}
}
#endif

View file

@ -23,8 +23,12 @@ class HFWebEngineProfile : public ContextAwareProfile {
public:
static void registerWithContext(QQmlContext* parent);
static void clearCache();
protected:
HFWebEngineProfile(QQmlContext* parent);
virtual ~HFWebEngineProfile();
class RequestInterceptor : public Parent::RequestInterceptor {
public:
RequestInterceptor(ContextAwareProfile* parent) : Parent::RequestInterceptor(parent) {}