From dbd9299e859bca50e7876de9da6e031dd1dba4d2 Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Thu, 28 Mar 2019 11:53:56 +0200 Subject: [PATCH 1/3] The "Clear Disk Caches" command also clears the KTX cache (on the next restart) --- interface/src/Menu.cpp | 12 ++++++++++-- interface/src/Menu.h | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 4cf78c23ee..6caf3c670d 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -632,8 +632,16 @@ Menu::Menu() { QString("hifi/tablet/TabletNetworkingPreferences.qml"), "NetworkingPreferencesDialog"); }); addActionToQMenuAndActionHash(networkMenu, MenuOption::ReloadContent, 0, qApp, SLOT(reloadResourceCaches())); - addActionToQMenuAndActionHash(networkMenu, MenuOption::ClearDiskCache, 0, - DependencyManager::get().data(), SLOT(clearCache())); + + action = addActionToQMenuAndActionHash(networkMenu, MenuOption::ClearDiskCaches); + connect(action, &QAction::triggered, [] { + // This cache is cleared immediately + DependencyManager::get()->clearCache(); + + // Clear the KTX cache on the next restart. It can't be cleared immediately because its files might be in use. + Setting::Handle(KTXCache::SETTING_VERSION_NAME, KTXCache::INVALID_VERSION).set(KTXCache::INVALID_VERSION); + }); + addCheckableActionToQMenuAndActionHash(networkMenu, MenuOption::DisableActivityLogger, 0, diff --git a/interface/src/Menu.h b/interface/src/Menu.h index 70687786a9..b8a72c3e93 100644 --- a/interface/src/Menu.h +++ b/interface/src/Menu.h @@ -56,7 +56,7 @@ namespace MenuOption { const QString CalibrateCamera = "Calibrate Camera"; const QString CenterPlayerInView = "Center Player In View"; const QString Chat = "Chat..."; - const QString ClearDiskCache = "Clear Disk Cache"; + const QString ClearDiskCaches = "Clear Disk Caches (requires restart)"; const QString Collisions = "Collisions"; const QString Connexion = "Activate 3D Connexion Devices"; const QString Console = "Console..."; From ef163c836da400ba131a1525c296c2572deaa353 Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Fri, 26 Jul 2019 15:25:24 +0300 Subject: [PATCH 2/3] Enable QtWebEngine to store its cache on disk In the past, QtWebEngine stored the cache on disk automatically. But apparently due to a recent change to Qt, this has changed: a Web Engine Profile that is constructed without a Storage Name is placed in "Off the Record" mode, which means that the cache is stored only in memory. Therefore, we must assign a Storage Name and disable "Off the Record" mode. --- libraries/ui/src/ui/types/FileTypeProfile.cpp | 3 +++ libraries/ui/src/ui/types/HFWebEngineProfile.cpp | 1 + 2 files changed, 4 insertions(+) diff --git a/libraries/ui/src/ui/types/FileTypeProfile.cpp b/libraries/ui/src/ui/types/FileTypeProfile.cpp index 073460903e..d8d977f00a 100644 --- a/libraries/ui/src/ui/types/FileTypeProfile.cpp +++ b/libraries/ui/src/ui/types/FileTypeProfile.cpp @@ -24,6 +24,9 @@ FileTypeProfile::FileTypeProfile(QQmlContext* parent) : static const QString WEB_ENGINE_USER_AGENT = "Chrome/48.0 (HighFidelityInterface)"; setHttpUserAgent(WEB_ENGINE_USER_AGENT); + setStorageName(QML_WEB_ENGINE_STORAGE_NAME); + setOffTheRecord(false); + auto requestInterceptor = new RequestInterceptor(this); setRequestInterceptor(requestInterceptor); } diff --git a/libraries/ui/src/ui/types/HFWebEngineProfile.cpp b/libraries/ui/src/ui/types/HFWebEngineProfile.cpp index ef1d009f09..c2c1007527 100644 --- a/libraries/ui/src/ui/types/HFWebEngineProfile.cpp +++ b/libraries/ui/src/ui/types/HFWebEngineProfile.cpp @@ -22,6 +22,7 @@ static const QString QML_WEB_ENGINE_STORAGE_NAME = "qmlWebEngine"; HFWebEngineProfile::HFWebEngineProfile(QQmlContext* parent) : Parent(parent) { setStorageName(QML_WEB_ENGINE_STORAGE_NAME); + setOffTheRecord(false); // we use the HFWebEngineRequestInterceptor to make sure that web requests are authenticated for the interface user setRequestInterceptor(new RequestInterceptor(this)); From d7ef7a052daf37385ebe64b5288a490435527387 Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Mon, 18 Jun 2018 14:53:32 +0300 Subject: [PATCH 3/3] 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. --- interface/src/Menu.cpp | 8 +++++++- libraries/ui/src/ui/types/FileTypeProfile.cpp | 19 ++++++++++++++++++ libraries/ui/src/ui/types/FileTypeProfile.h | 4 ++++ .../ui/src/ui/types/HFWebEngineProfile.cpp | 20 +++++++++++++++++++ .../ui/src/ui/types/HFWebEngineProfile.h | 4 ++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 6caf3c670d..2450762789 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #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()->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(KTXCache::SETTING_VERSION_NAME, KTXCache::INVALID_VERSION).set(KTXCache::INVALID_VERSION); diff --git a/libraries/ui/src/ui/types/FileTypeProfile.cpp b/libraries/ui/src/ui/types/FileTypeProfile.cpp index d8d977f00a..d08aac5fe4 100644 --- a/libraries/ui/src/ui/types/FileTypeProfile.cpp +++ b/libraries/ui/src/ui/types/FileTypeProfile.cpp @@ -11,6 +11,8 @@ #include "FileTypeProfile.h" +#include +#include #include #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_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 lock(FileTypeProfile_mutex); + FileTypeProfile_instances.insert(this); +} + +FileTypeProfile::~FileTypeProfile() { + std::lock_guard 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 lock(FileTypeProfile_mutex); + foreach (auto instance, FileTypeProfile_instances) { + instance->clearHttpCache(); + } +} #endif \ No newline at end of file diff --git a/libraries/ui/src/ui/types/FileTypeProfile.h b/libraries/ui/src/ui/types/FileTypeProfile.h index 7ddfdd0aed..8d8b921846 100644 --- a/libraries/ui/src/ui/types/FileTypeProfile.h +++ b/libraries/ui/src/ui/types/FileTypeProfile.h @@ -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) {} diff --git a/libraries/ui/src/ui/types/HFWebEngineProfile.cpp b/libraries/ui/src/ui/types/HFWebEngineProfile.cpp index c2c1007527..a618411f1b 100644 --- a/libraries/ui/src/ui/types/HFWebEngineProfile.cpp +++ b/libraries/ui/src/ui/types/HFWebEngineProfile.cpp @@ -11,6 +11,8 @@ #include "HFWebEngineProfile.h" +#include +#include #include #include "RequestFilters.h" @@ -19,6 +21,9 @@ static const QString QML_WEB_ENGINE_STORAGE_NAME = "qmlWebEngine"; +static std::set 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 lock(HFWebEngineProfile_mutex); + HFWebEngineProfile_instances.insert(this); +} + +HFWebEngineProfile::~HFWebEngineProfile() { + std::lock_guard 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 lock(HFWebEngineProfile_mutex); + foreach (auto instance, HFWebEngineProfile_instances) { + instance->clearHttpCache(); + } +} + #endif diff --git a/libraries/ui/src/ui/types/HFWebEngineProfile.h b/libraries/ui/src/ui/types/HFWebEngineProfile.h index 6b84ad6f80..872cb31901 100644 --- a/libraries/ui/src/ui/types/HFWebEngineProfile.h +++ b/libraries/ui/src/ui/types/HFWebEngineProfile.h @@ -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) {}