From 5f81848067f2142b7c78d6ca758c9efc5c1ab1d8 Mon Sep 17 00:00:00 2001 From: Kirill Burtsev Date: Wed, 10 Apr 2019 18:27:31 +0200 Subject: Simple browser example: create off-the-record profile in a lazy manner Do not create multiple profiles immediately on start. Allows to start and run browser with default profile with 'single-process' flag. Change-Id: Idea7da8167152f718a3c2d4086ad4216d8e1b722 Reviewed-by: Leena Miettinen --- .../webenginewidgets/simplebrowser/browser.cpp | 13 ++++++------ examples/webenginewidgets/simplebrowser/browser.h | 2 +- .../simplebrowser/doc/src/simplebrowser.qdoc | 23 +++++++++++++++------- 3 files changed, 24 insertions(+), 14 deletions(-) (limited to 'examples/webenginewidgets') diff --git a/examples/webenginewidgets/simplebrowser/browser.cpp b/examples/webenginewidgets/simplebrowser/browser.cpp index 5c6dbd35e..68458b2a4 100644 --- a/examples/webenginewidgets/simplebrowser/browser.cpp +++ b/examples/webenginewidgets/simplebrowser/browser.cpp @@ -51,8 +51,6 @@ #include "browser.h" #include "browserwindow.h" -#include - Browser::Browser() { // Quit application if the download manager window is the only remaining window @@ -61,14 +59,17 @@ Browser::Browser() QObject::connect( QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, &m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested); - QObject::connect( - &m_otrProfile, &QWebEngineProfile::downloadRequested, - &m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested); } BrowserWindow *Browser::createWindow(bool offTheRecord) { - auto profile = offTheRecord ? &m_otrProfile : QWebEngineProfile::defaultProfile(); + if (offTheRecord && !m_otrProfile) { + m_otrProfile.reset(new QWebEngineProfile); + QObject::connect( + m_otrProfile.get(), &QWebEngineProfile::downloadRequested, + &m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested); + } + auto profile = offTheRecord ? m_otrProfile.get() : QWebEngineProfile::defaultProfile(); auto mainWindow = new BrowserWindow(this, profile, false); m_windows.append(mainWindow); QObject::connect(mainWindow, &QObject::destroyed, [this, mainWindow]() { diff --git a/examples/webenginewidgets/simplebrowser/browser.h b/examples/webenginewidgets/simplebrowser/browser.h index fbc8465d2..4c17121df 100644 --- a/examples/webenginewidgets/simplebrowser/browser.h +++ b/examples/webenginewidgets/simplebrowser/browser.h @@ -73,6 +73,6 @@ public: private: QVector m_windows; DownloadManagerWidget m_downloadManagerWidget; - QWebEngineProfile m_otrProfile; + QScopedPointer m_otrProfile; }; #endif // BROWSER_H diff --git a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc index b5ad13626..251ca5ad8 100644 --- a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc +++ b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc @@ -270,7 +270,7 @@ Implementing private browsing is quite easy using \QWE. All one has to do is to create a new \l{QWebEngineProfile} and use it in the \l{QWebEnginePage} instead of the default profile. In the example this new - profile is created and owned by the \c Browser object: + profile is owned by the \c Browser object: \quotefromfile webenginewidgets/simplebrowser/browser.h \skipto /^class Browser$/ @@ -285,15 +285,24 @@ \printline m_otrProfile \printline /^\};$/ - The default constructor for \l{QWebEngineProfile} already puts it in - \e{off-the-record} mode. All that is left to do is to pass the appropriate - profile down to the appropriate \l QWebEnginePage objects. The \c Browser - object will hand to each new \c BrowserWindow either the global default - profile (see \l{QWebEngineProfile::defaultProfile}) or its own - off-the-record profile: + Required profile for \e{private browsing} is created together with its + first window. The default constructor for \l{QWebEngineProfile} already + puts it in \e{off-the-record} mode. \quotefromfile webenginewidgets/simplebrowser/browser.cpp + \skipto Browser::createWindow + \printuntil m_otrProfile.reset + \dots + + All that is left to do is to pass the appropriate profile down to the + appropriate \l QWebEnginePage objects. The \c Browser object will hand to + each new \c BrowserWindow either the global default profile + (see \l{QWebEngineProfile::defaultProfile}) or one shared + \e{off-the-record} profile instance: + + \dots + \skipto m_otrProfile.get \printuntil mainWindow = new BrowserWindow \skipto return mainWindow \printuntil /^\}/ -- cgit v1.2.3