From bbf229e66ff7f493906848765708dbdefb0a1cb4 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Sun, 19 Jan 2025 18:38:53 -0300 Subject: [PATCH] Athlete level WebEngineProfile for cookies and storage Used by WebChart for autologin and Map charts for cache, but not for OAuth dialogs nor Python web charts. Fixes #4484 --- src/Charts/RideMapWindow.cpp | 2 +- src/Charts/RideMapWindow.h | 6 ------ src/Core/Context.cpp | 6 ++++++ src/Core/Context.h | 4 ++++ src/Gui/MainWindow.cpp | 4 ++++ src/Train/LiveMapWebPageWindow.cpp | 5 +---- src/Train/MeterWidget.cpp | 3 +-- src/Train/WebPageWindow.cpp | 19 +------------------ src/Train/WebPageWindow.h | 3 --- 9 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/Charts/RideMapWindow.cpp b/src/Charts/RideMapWindow.cpp index 251c68d10..30dbeed80 100644 --- a/src/Charts/RideMapWindow.cpp +++ b/src/Charts/RideMapWindow.cpp @@ -142,7 +142,7 @@ RideMapWindow::RideMapWindow(Context *context, int mapType) : GcChartWindow(cont view = new QWebEngineView(this); // stop stealing focus! view->settings()->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false); - view->setPage(new mapWebPage()); + view->setPage(new QWebEnginePage(context->webEngineProfile)); view->setContentsMargins(0,0,0,0); view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); view->setAcceptDrops(false); diff --git a/src/Charts/RideMapWindow.h b/src/Charts/RideMapWindow.h index 56687a445..20678dc28 100644 --- a/src/Charts/RideMapWindow.h +++ b/src/Charts/RideMapWindow.h @@ -56,12 +56,6 @@ struct PositionItem { }; -// trick the maps api into ignoring gestures by -// pretending to be chrome. see: http://developer.qt.nokia.com/forums/viewthread/1643/P15 -class mapWebPage : public QWebEnginePage -{ -}; - class MapWebBridge : public QObject { Q_OBJECT diff --git a/src/Core/Context.cpp b/src/Core/Context.cpp index 135cb5c49..f8f14376d 100644 --- a/src/Core/Context.cpp +++ b/src/Core/Context.cpp @@ -30,6 +30,7 @@ #include #include #include +#include // singleton static GlobalContext *globalContext = NULL; @@ -132,6 +133,7 @@ GlobalContext *GlobalContext::context() } bool Context::isValid(Context *p) { return p != NULL &&_contexts.contains(p); } + Context::Context(MainWindow *mainWindow): mainWindow(mainWindow) { ride = NULL; @@ -148,6 +150,10 @@ Context::Context(MainWindow *mainWindow): mainWindow(mainWindow) cdbUserMetricListDialog = NULL; #endif + // WebEngineProfile - cookies and storage + webEngineProfile = new QWebEngineProfile("Default", this); + webEngineProfile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies); + _contexts.append(this); } diff --git a/src/Core/Context.h b/src/Core/Context.h index dca03d491..96b9024c8 100644 --- a/src/Core/Context.h +++ b/src/Core/Context.h @@ -72,6 +72,7 @@ class AthleteTab; class NavigationModel; class RideMetadata; class ColorEngine; +class QWebEngineProfile; class GlobalContext : public QObject @@ -161,6 +162,9 @@ class Context : public QObject CloudDBUserMetricListDialog *cdbUserMetricListDialog; #endif + // WebEngineProfile for this user + QWebEngineProfile* webEngineProfile; + public slots: // ********************************************* diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index 8fc423b97..1d09d455b 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -27,6 +27,7 @@ #include #include #include +#include // DATA STRUCTURES #include "MainWindow.h" @@ -149,6 +150,9 @@ MainWindow::MainWindow(const QDir &home) // bootstrap Context *context = new Context(this); context->athlete = new Athlete(context, home); + QString temp = const_cast(context->athlete->directoryStructure())->temp().absolutePath(); + context->webEngineProfile->setCachePath(temp); + context->webEngineProfile->setPersistentStoragePath(temp); currentAthleteTab = new AthleteTab(context); setWindowIcon(QIcon(":images/gc.png")); diff --git a/src/Train/LiveMapWebPageWindow.cpp b/src/Train/LiveMapWebPageWindow.cpp index 1c99bd9b7..59af31027 100644 --- a/src/Train/LiveMapWebPageWindow.cpp +++ b/src/Train/LiveMapWebPageWindow.cpp @@ -35,9 +35,6 @@ #include "ErgFile.h" #include "LocationInterpolation.h" -//#include -//#include - // overlay helper #include "AbstractView.h" #include "GcOverlayWidget.h" @@ -105,7 +102,7 @@ LiveMapWebPageWindow::LiveMapWebPageWindow(Context *context) : GcChartWindow(con // set webview for map view = new QWebEngineView(this); - webPage = view->page(); + webPage = new QWebEnginePage(context->webEngineProfile); view->setPage(webPage); view->setContentsMargins(0,10,0,0); diff --git a/src/Train/MeterWidget.cpp b/src/Train/MeterWidget.cpp index 77b4f4e04..5436cf4ec 100644 --- a/src/Train/MeterWidget.cpp +++ b/src/Train/MeterWidget.cpp @@ -23,7 +23,6 @@ #include "Units.h" #include "LocationInterpolation.h" #include -#include #include MeterWidget::MeterWidget(QString Name, QWidget *parent, QString Source) : QWidget(parent), m_Name(Name), m_container(parent), m_Source(Source) @@ -516,7 +515,7 @@ LiveMapWidget::LiveMapWidget(QString Name, QWidget* parent, QString Source, Cont m_osmURL = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"; forceSquareRatio = false; liveMapView = new QWebEngineView(this); - webPage = new QWebEnginePage(liveMapView); + webPage = new QWebEnginePage(context->webEngineProfile); liveMapView->setPage(webPage); routeInitialized = false; mapInitialized = false; diff --git a/src/Train/WebPageWindow.cpp b/src/Train/WebPageWindow.cpp index 974556f5f..9cb52ef7a 100644 --- a/src/Train/WebPageWindow.cpp +++ b/src/Train/WebPageWindow.cpp @@ -141,32 +141,15 @@ WebPageWindow::WebPageWindow(Context *context) : GcChartWindow(context), context view = new QWebEngineView(this); connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool))); - //view->page()->profile()->setHttpUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"); - // add a download interceptor WebDownloadInterceptor *interceptor = new WebDownloadInterceptor; view->page()->profile()->setUrlRequestInterceptor(interceptor); - // cookies and storage - view->page()->profile()->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies); - QString temp = const_cast(context->athlete->directoryStructure())->temp().absolutePath(); - view->page()->profile()->setCachePath(temp); - view->page()->profile()->setPersistentStoragePath(temp); - - // web scheme handler - not needed must compile with QT 5.9 or higher - //WebSchemeHandler *handler = new WebSchemeHandler(view); - //view->page()->profile()->installUrlSchemeHandler("filesystem:https", handler); - //view->page()->profile()->installUrlSchemeHandler("filesystem", handler); - // add some settings view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true); view->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); -#if QT_VERSION < 0x060000 - view->setPage(new simpleWebPage()); -#else - view->setPage(new QWebEnginePage(new QWebEngineProfile("Default"))); -#endif + view->setPage(new QWebEnginePage(context->webEngineProfile)); view->setContentsMargins(0,0,0,0); view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); view->setAcceptDrops(false); diff --git a/src/Train/WebPageWindow.h b/src/Train/WebPageWindow.h index 55c5847ed..e0f1d0056 100644 --- a/src/Train/WebPageWindow.h +++ b/src/Train/WebPageWindow.h @@ -54,9 +54,6 @@ class QWebEngineDownloadItem; #else class QWebEngineDownloadRequest; #endif -class simpleWebPage : public QWebEnginePage -{ -}; class WebPageWindow : public GcChartWindow {