diff options
author | Jüri Valdmann <juri.valdmann@qt.io> | 2017-08-02 16:31:11 +0200 |
---|---|---|
committer | Jüri Valdmann <juri.valdmann@qt.io> | 2017-10-25 08:00:17 +0000 |
commit | a9452507f484589e15f8947d699be691dcaa4194 (patch) | |
tree | 33d7e5c8d082833fe256552670fb0facd74b86f0 | |
parent | b2c1d565cc4c704dfb266d1291dd50a9e4d1c4bc (diff) | |
download | qtwebengine-a9452507f484589e15f8947d699be691dcaa4194.tar.gz |
Add off-the-record mode to simplebrowser example
Change-Id: I9b87f870c4f6ad63142bc0497722b0c95095cd6b
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
7 files changed, 86 insertions, 15 deletions
diff --git a/examples/webenginewidgets/simplebrowser/browser.cpp b/examples/webenginewidgets/simplebrowser/browser.cpp index f5a69793f..c50974531 100644 --- a/examples/webenginewidgets/simplebrowser/browser.cpp +++ b/examples/webenginewidgets/simplebrowser/browser.cpp @@ -61,11 +61,15 @@ 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() +BrowserWindow *Browser::createWindow(bool offTheRecord) { - auto mainWindow = new BrowserWindow(this); + auto profile = offTheRecord ? &m_otrProfile : QWebEngineProfile::defaultProfile(); + auto mainWindow = new BrowserWindow(this, profile); m_windows.append(mainWindow); QObject::connect(mainWindow, &QObject::destroyed, [this, mainWindow]() { m_windows.removeOne(mainWindow); diff --git a/examples/webenginewidgets/simplebrowser/browser.h b/examples/webenginewidgets/simplebrowser/browser.h index 0ea30a7f5..9240cc987 100644 --- a/examples/webenginewidgets/simplebrowser/browser.h +++ b/examples/webenginewidgets/simplebrowser/browser.h @@ -54,6 +54,7 @@ #include "downloadmanagerwidget.h" #include <QVector> +#include <QWebEngineProfile> class BrowserWindow; @@ -64,12 +65,13 @@ public: QVector<BrowserWindow*> windows() { return m_windows; } - BrowserWindow *createWindow(); + BrowserWindow *createWindow(bool offTheRecord = false); DownloadManagerWidget &downloadManagerWidget() { return m_downloadManagerWidget; } private: QVector<BrowserWindow*> m_windows; DownloadManagerWidget m_downloadManagerWidget; + QWebEngineProfile m_otrProfile; }; #endif // BROWSER_H diff --git a/examples/webenginewidgets/simplebrowser/browserwindow.cpp b/examples/webenginewidgets/simplebrowser/browserwindow.cpp index e114ae0f3..016d58afe 100644 --- a/examples/webenginewidgets/simplebrowser/browserwindow.cpp +++ b/examples/webenginewidgets/simplebrowser/browserwindow.cpp @@ -67,9 +67,10 @@ #include <QVBoxLayout> #include <QWebEngineProfile> -BrowserWindow::BrowserWindow(Browser *browser) +BrowserWindow::BrowserWindow(Browser *browser, QWebEngineProfile *profile) : m_browser(browser) - , m_tabWidget(new TabWidget(this)) + , m_profile(profile) + , m_tabWidget(new TabWidget(profile, this)) , m_progressBar(new QProgressBar(this)) , m_historyBackAction(nullptr) , m_historyForwardAction(nullptr) @@ -141,6 +142,7 @@ QMenu *BrowserWindow::createFileMenu(TabWidget *tabWidget) { QMenu *fileMenu = new QMenu(tr("&File")); fileMenu->addAction(tr("&New Window"), this, &BrowserWindow::handleNewWindowTriggered, QKeySequence::New); + fileMenu->addAction(tr("New &Incognito Window"), this, &BrowserWindow::handleNewIncognitoWindowTriggered); QAction *newTabAction = new QAction(tr("New &Tab"), this); newTabAction->setShortcuts(QKeySequence::AddTab); @@ -403,10 +405,14 @@ void BrowserWindow::handleWebActionEnabledChanged(QWebEnginePage::WebAction acti void BrowserWindow::handleWebViewTitleChanged(const QString &title) { + QString suffix = m_profile->isOffTheRecord() + ? tr("Qt Simple Browser (Incognito)") + : tr("Qt Simple Browser"); + if (title.isEmpty()) - setWindowTitle(tr("Qt Simple Browser")); + setWindowTitle(suffix); else - setWindowTitle(tr("%1 - Qt Simple Browser").arg(title)); + setWindowTitle(title + " - " + suffix); } void BrowserWindow::handleNewWindowTriggered() @@ -414,6 +420,11 @@ void BrowserWindow::handleNewWindowTriggered() m_browser->createWindow(); } +void BrowserWindow::handleNewIncognitoWindowTriggered() +{ + m_browser->createWindow(/* offTheRecord: */ true); +} + void BrowserWindow::handleFileOpenTriggered() { QUrl url = QFileDialog::getOpenFileUrl(this, tr("Open Web Resource"), QString(), diff --git a/examples/webenginewidgets/simplebrowser/browserwindow.h b/examples/webenginewidgets/simplebrowser/browserwindow.h index 36f00605c..b522a6b9d 100644 --- a/examples/webenginewidgets/simplebrowser/browserwindow.h +++ b/examples/webenginewidgets/simplebrowser/browserwindow.h @@ -69,7 +69,7 @@ class BrowserWindow : public QMainWindow Q_OBJECT public: - BrowserWindow(Browser *browser); + BrowserWindow(Browser *browser, QWebEngineProfile *profile); QSize sizeHint() const override; TabWidget *tabWidget() const; WebView *currentTab() const; @@ -80,6 +80,7 @@ protected: private slots: void handleNewWindowTriggered(); + void handleNewIncognitoWindowTriggered(); void handleFileOpenTriggered(); void handleFindActionTriggered(); void handleShowWindowTriggered(); @@ -97,6 +98,7 @@ private: private: Browser *m_browser; + QWebEngineProfile *m_profile; TabWidget *m_tabWidget; QProgressBar *m_progressBar; QAction *m_historyBackAction; diff --git a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc index 741ad864d..4f44d9f6c 100644 --- a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc +++ b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc @@ -202,9 +202,6 @@ \section1 Implementing WebPage Functionality - As mentioned earlier, each \c WebView contains a \c WebPage instance that - was created by using QWebEngineProfile::defaultProfile(). - We implement \c WebPage as a subclass of QWebEnginePage to enable HTTP, proxy authentication, and ignoring SSL certificate errors when accessing web pages: @@ -242,8 +239,16 @@ over to \c TabWidget::setUrl: \quotefromfile webenginewidgets/simplebrowser/browserwindow.cpp + \skipto BrowserWindow::BrowserWindow + \printline BrowserWindow::BrowserWindow + \skipto /^\{/ + \printline /^\{/ + \dots \skipto connect(m_urlLineEdit - \printline connect + \printuntil }); + \dots + \skipto /^\}/ + \printline /^\}/ The call is forwarded to the currently selected tab: @@ -255,6 +260,50 @@ The \c setUrl() method of \c WebView just forwards the \c url to the associated \c WebPage, which in turn starts the downloading of the page's content in the background. + \section1 Implementing Private Browsing + + \e{Private browsing}, \e{incognito mode}, or \e{off-the-record mode} is a + feature of many browsers where normally persistent data, such as cookies, + the HTTP cache, or browsing history, is kept only in memory, leaving no + trace on disk. In this example we will implement private browsing on the + window level with tabs in one window all in either normal or private mode. + Alternatively we could implement private browsing on the tab-level, with + some tabs in a window in normal mode, others in private mode. + + Implementing private browsing is quite easy using Qt WebEngine. 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: + + \quotefromfile webenginewidgets/simplebrowser/browser.h + \skipto /^class Browser$/ + \printuntil public: + \dots + \skipto createWindow + \printline createWindow + \skipto private: + \printline private: + \dots + \skipto m_otrProfile + \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: + + \quotefromfile webenginewidgets/simplebrowser/browser.cpp + \skipto Browser::createWindow + \printuntil mainWindow = new BrowserWindow + \skipto return mainWindow + \printuntil /^\}/ + + The \c BrowserWindow and \c TabWidget objects will then ensure that all \l + QWebEnginePage objects contained in a window will use this profile. + \section1 Managing Downloads Downloads are associated with a \l QWebEngineProfile. Whenever a download is diff --git a/examples/webenginewidgets/simplebrowser/tabwidget.cpp b/examples/webenginewidgets/simplebrowser/tabwidget.cpp index 426bce3a7..1c6cfbe00 100644 --- a/examples/webenginewidgets/simplebrowser/tabwidget.cpp +++ b/examples/webenginewidgets/simplebrowser/tabwidget.cpp @@ -55,8 +55,9 @@ #include <QTabBar> #include <QWebEngineProfile> -TabWidget::TabWidget(QWidget *parent) +TabWidget::TabWidget(QWebEngineProfile *profile, QWidget *parent) : QTabWidget(parent) + , m_profile(profile) { QTabBar *tabBar = this->tabBar(); tabBar->setTabsClosable(true); @@ -201,7 +202,7 @@ WebView *TabWidget::createTab() WebView *TabWidget::createBackgroundTab() { WebView *webView = new WebView; - WebPage *webPage = new WebPage(QWebEngineProfile::defaultProfile(), webView); + WebPage *webPage = new WebPage(m_profile, webView); webView->setPage(webPage); setupView(webView); int index = addTab(webView, tr("(Untitled)")); diff --git a/examples/webenginewidgets/simplebrowser/tabwidget.h b/examples/webenginewidgets/simplebrowser/tabwidget.h index f33ecf897..5b09f2708 100644 --- a/examples/webenginewidgets/simplebrowser/tabwidget.h +++ b/examples/webenginewidgets/simplebrowser/tabwidget.h @@ -65,7 +65,7 @@ class TabWidget : public QTabWidget Q_OBJECT public: - TabWidget(QWidget *parent = nullptr); + TabWidget(QWebEngineProfile *profile, QWidget *parent = nullptr); WebView *currentWebView() const; @@ -100,6 +100,8 @@ private slots: private: WebView *webView(int index) const; void setupView(WebView *webView); + + QWebEngineProfile *m_profile; }; #endif // TABWIDGET_H |