1 From 9f0ce086c6a4e487cee1f01acb15290ebff19ac3 Mon Sep 17 00:00:00 2001
2 From: Jonathan Marten <jjm@keelhaul.me.uk>
3 Date: Tue, 12 Mar 2019 08:47:43 +0000
4 Subject: Move management of QWebEngineProfile to WebEnginePage
6 To avoid a crash on KMail quit or viewer window close, caused by the
7 MailWebEnginePage being deleted while its QWebEngineProfile is still
8 active. Simplified management of the profile by handling its creation
9 and deletion within WebEnginePage.
11 Deprecate the 2-argument WebEnginePage constructor which allows an
12 already allocated profile to be used. This constructor is used by
13 Akregator which specifies the global profile; it will be changed to
14 use a private profile instead.
16 Differential Revision: https://phabricator.kde.org/D19559
18 .../src/viewer/webengine/mailwebenginepage.cpp | 10 -------
19 .../src/viewer/webengine/mailwebenginepage.h | 3 +-
20 .../src/viewer/webengine/mailwebengineview.cpp | 3 +-
21 webengineviewer/src/webenginepage.cpp | 22 ++++++++++----
22 webengineviewer/src/webenginepage.h | 35 ++++++++++++++++++++--
23 5 files changed, 52 insertions(+), 21 deletions(-)
25 diff --git a/messageviewer/src/viewer/webengine/mailwebenginepage.cpp b/messageviewer/src/viewer/webengine/mailwebenginepage.cpp
26 index cd91494..f3790b9 100644
27 --- a/messageviewer/src/viewer/webengine/mailwebenginepage.cpp
28 +++ b/messageviewer/src/viewer/webengine/mailwebenginepage.cpp
29 @@ -29,16 +29,6 @@ MailWebEnginePage::MailWebEnginePage(QObject *parent)
33 -MailWebEnginePage::MailWebEnginePage(QWebEngineProfile *profile, QObject *parent)
34 - : WebEngineViewer::WebEnginePage(profile, parent)
39 -MailWebEnginePage::~MailWebEnginePage()
43 void MailWebEnginePage::initialize()
45 settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
46 diff --git a/messageviewer/src/viewer/webengine/mailwebenginepage.h b/messageviewer/src/viewer/webengine/mailwebenginepage.h
47 index 7f8e6eb..d581efd 100644
48 --- a/messageviewer/src/viewer/webengine/mailwebenginepage.h
49 +++ b/messageviewer/src/viewer/webengine/mailwebenginepage.h
50 @@ -28,8 +28,7 @@ class MESSAGEVIEWER_EXPORT MailWebEnginePage : public WebEngineViewer::WebEngine
53 explicit MailWebEnginePage(QObject *parent = nullptr);
54 - explicit MailWebEnginePage(QWebEngineProfile *profile, QObject *parent = nullptr);
55 - ~MailWebEnginePage();
56 + virtual ~MailWebEnginePage() = default;
58 void setPrintElementBackground(bool printElementBackground);
60 diff --git a/messageviewer/src/viewer/webengine/mailwebengineview.cpp b/messageviewer/src/viewer/webengine/mailwebengineview.cpp
61 index 0685a8d..09224cf 100644
62 --- a/messageviewer/src/viewer/webengine/mailwebengineview.cpp
63 +++ b/messageviewer/src/viewer/webengine/mailwebengineview.cpp
65 #include <QContextMenuEvent>
66 #include <WebEngineViewer/WebHitTest>
68 -#include <QWebEngineProfile>
71 #include <WebEngineViewer/WebHitTestResult>
72 @@ -81,7 +80,7 @@ MailWebEngineView::MailWebEngineView(KActionCollection *ac, QWidget *parent)
73 : WebEngineViewer::WebEngineView(parent)
74 , d(new MessageViewer::MailWebEngineViewPrivate)
76 - d->mPageEngine = new MailWebEnginePage(new QWebEngineProfile(this), this);
77 + d->mPageEngine = new MailWebEnginePage(this);
78 setPage(d->mPageEngine);
79 d->mWebViewAccessKey = new WebEngineViewer::WebEngineAccessKey(this, this);
80 d->mWebViewAccessKey->setActionCollection(ac);
81 diff --git a/webengineviewer/src/webenginepage.cpp b/webengineviewer/src/webenginepage.cpp
82 index 447ac26..2fcb6c2 100644
83 --- a/webengineviewer/src/webenginepage.cpp
84 +++ b/webengineviewer/src/webenginepage.cpp
86 using namespace WebEngineViewer;
88 WebEnginePage::WebEnginePage(QObject *parent)
89 - : QWebEnginePage(parent)
90 + : QWebEnginePage(new QWebEngineProfile, parent)
92 + // Create a private (off the record) QWebEngineProfile here to isolate the
93 + // browsing settings, and adopt it as a child so that it will be deleted
94 + // when we are destroyed. The profile must remain active for as long as
95 + // any QWebEnginePage's belonging to it exist, see the API documentation
96 + // of QWebEnginePage::QWebEnginePage(QWebEngineProfile *, QObject *).
97 + // Deleting it as our child on destruction is safe.
99 + // Do not try to save a line of code by setting the parent on construction:
101 + // WebEnginePage::WebEnginePage(QObject *parent)
102 + // : QWebEnginePage(new QWebEngineProfile(this), parent)
104 + // because the QWebEngineProfile constructor will call out to the QWebEnginePage
105 + // and crash because the QWebEnginePage is not fully constructed yet.
106 + profile()->setParent(this);
111 @@ -44,10 +60,6 @@ WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject *parent)
115 -WebEnginePage::~WebEnginePage()
119 void WebEnginePage::init()
121 connect(profile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePage::saveHtml);
122 diff --git a/webengineviewer/src/webenginepage.h b/webengineviewer/src/webenginepage.h
123 index be38368..95c7c76 100644
124 --- a/webengineviewer/src/webenginepage.h
125 +++ b/webengineviewer/src/webenginepage.h
126 @@ -31,10 +31,41 @@ class WEBENGINEVIEWER_EXPORT WebEnginePage : public QWebEnginePage
133 + * A private QWebEngineProfile, only applying to this QWebEnginePage,
134 + * will be created to implement browser settings. It can be accessed via
135 + * @c profile(), but it should not be shared or reused unless care is
136 + * taken that the profile is not deleted until all of the QWebEnginePage's
137 + * belonging to it are deleted first.
139 + * @param parent The parent object
141 explicit WebEnginePage(QObject *parent = nullptr);
142 - explicit WebEnginePage(QWebEngineProfile *profile, QObject *parent = nullptr);
144 - ~WebEnginePage() override;
148 + * The specified QWebEngineProfile will be used. See the description of
149 + * @c WebEnginePage(QObject *) and the API documentation of QWebEnginePage
150 + * for caution regarding the lifetime of the profile.
152 + * @param profile The profile to be used
153 + * @param parent The parent object
154 + * @deprecated Use the single argument constructor, which creates and uses
155 + * a private profile.
157 +#ifndef WEBENGINEVIEWER_NO_DEPRECATED
158 + explicit WEBENGINEVIEWER_DEPRECATED WebEnginePage(QWebEngineProfile *profile, QObject *parent = nullptr);
162 + * Destructor. If there is a private QWebEngineProfile then it will also
165 + virtual ~WebEnginePage() override = default;
167 WebEngineViewer::WebHitTest *hitTestContent(const QPoint &pos);
169 void saveHtml(QWebEngineDownloadItem *download);