From 4db8c2c92ae8bc722de70e4bd317dce17dea2f74 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Fri, 8 May 2015 12:58:28 +0200 Subject: Reintroduce private browsing mode for example browser Uses QWebEngineProfile to support private browsing mode. Change-Id: I78fa712d2425eb2df519594ee3fa5639bbcbebf6 Reviewed-by: Andras Becsi --- .../browser/browserapplication.cpp | 28 ++++++++++++++++--- .../webenginewidgets/browser/browserapplication.h | 8 ++++++ .../webenginewidgets/browser/browsermainwindow.cpp | 31 +++++++++------------- .../webenginewidgets/browser/browsermainwindow.h | 1 + .../webenginewidgets/browser/downloadmanager.cpp | 5 +--- examples/webenginewidgets/browser/history.cpp | 5 +--- examples/webenginewidgets/browser/history.h | 1 + examples/webenginewidgets/browser/tabwidget.cpp | 21 +++++++++++---- examples/webenginewidgets/browser/tabwidget.h | 4 +++ .../webenginewidgets/browser/toolbarsearch.cpp | 7 ++--- examples/webenginewidgets/browser/webview.cpp | 29 ++++++++++++-------- examples/webenginewidgets/browser/webview.h | 3 ++- 12 files changed, 90 insertions(+), 53 deletions(-) (limited to 'examples') diff --git a/examples/webenginewidgets/browser/browserapplication.cpp b/examples/webenginewidgets/browser/browserapplication.cpp index b1426ea24..3bc801295 100644 --- a/examples/webenginewidgets/browser/browserapplication.cpp +++ b/examples/webenginewidgets/browser/browserapplication.cpp @@ -116,6 +116,8 @@ static void setUserStyleSheet(QWebEngineProfile *profile, const QString &styleSh BrowserApplication::BrowserApplication(int &argc, char **argv) : QApplication(argc, argv) , m_localServer(0) + , m_privateProfile(0) + , m_privateBrowsing(false) { QCoreApplication::setOrganizationName(QLatin1String("Qt")); QCoreApplication::setApplicationName(QLatin1String("demobrowser")); @@ -325,11 +327,8 @@ void BrowserApplication::clean() void BrowserApplication::saveSession() { -#if defined(QTWEBENGINE_PRIVATEBROWSING) - QWebEngineSettings *globalSettings = QWebEngineSettings::globalSettings(); - if (globalSettings->testAttribute(QWebEngineSettings::PrivateBrowsingEnabled)) + if (m_privateBrowsing) return; -#endif clean(); @@ -529,3 +528,24 @@ QIcon BrowserApplication::defaultIcon() const m_defaultIcon = QIcon(QLatin1String(":defaulticon.png")); return m_defaultIcon; } + +void BrowserApplication::setPrivateBrowsing(bool privateBrowsing) +{ + if (m_privateBrowsing == privateBrowsing) + return; + m_privateBrowsing = privateBrowsing; + if (privateBrowsing) { + if (!m_privateProfile) + m_privateProfile = new QWebEngineProfile(this); + Q_FOREACH (BrowserMainWindow* window, mainWindows()) { + window->tabWidget()->setProfile(m_privateProfile); + } + } else { + Q_FOREACH (BrowserMainWindow* window, mainWindows()) { + window->tabWidget()->setProfile(QWebEngineProfile::defaultProfile()); + window->m_lastSearch = QString::null; + window->tabWidget()->clear(); + } + } + emit privateBrowsingChanged(privateBrowsing); +} diff --git a/examples/webenginewidgets/browser/browserapplication.h b/examples/webenginewidgets/browser/browserapplication.h index 97df0e431..26557b8f9 100644 --- a/examples/webenginewidgets/browser/browserapplication.h +++ b/examples/webenginewidgets/browser/browserapplication.h @@ -52,6 +52,7 @@ QT_BEGIN_NAMESPACE class QLocalServer; class QNetworkAccessManager; +class QWebEngineProfile; QT_END_NAMESPACE class BookmarksManager; @@ -77,6 +78,7 @@ public: void saveSession(); bool canRestoreSession() const; + bool privateBrowsing() const { return m_privateBrowsing; } static HistoryManager *historyManager(); static CookieJar *cookieJar(); @@ -95,6 +97,10 @@ public slots: void lastWindowClosed(); void quitBrowser(); #endif + void setPrivateBrowsing(bool); + +signals: + void privateBrowsingChanged(bool); private slots: void postLaunch(); @@ -113,6 +119,8 @@ private: QList > m_mainWindows; QLocalServer *m_localServer; QByteArray m_lastSession; + QWebEngineProfile *m_privateProfile; + bool m_privateBrowsing; mutable QIcon m_defaultIcon; }; diff --git a/examples/webenginewidgets/browser/browsermainwindow.cpp b/examples/webenginewidgets/browser/browsermainwindow.cpp index 98f61d6cb..9595ca7ba 100644 --- a/examples/webenginewidgets/browser/browsermainwindow.cpp +++ b/examples/webenginewidgets/browser/browsermainwindow.cpp @@ -68,6 +68,7 @@ #include #include +#include #include #include @@ -304,11 +305,11 @@ void BrowserMainWindow::setupMenu() fileMenu->addAction(tr("&Print..."), this, SLOT(slotFilePrint()), QKeySequence::Print); fileMenu->addSeparator(); #endif -#if defined(QTWEBENGINE_PRIVATEBROWSING) QAction *action = fileMenu->addAction(tr("Private &Browsing..."), this, SLOT(slotPrivateBrowsing())); action->setCheckable(true); + action->setChecked(BrowserApplication::instance()->privateBrowsing()); + connect(BrowserApplication::instance(), SIGNAL(privateBrowsingChanged(bool)), action, SLOT(setChecked(bool))); fileMenu->addSeparator(); -#endif #if defined(Q_OS_OSX) fileMenu->addAction(tr("&Quit"), BrowserApplication::instance(), SLOT(quitBrowser()), QKeySequence(Qt::CTRL | Qt::Key_Q)); @@ -703,12 +704,11 @@ void BrowserMainWindow::printRequested(QWebEngineFrame *frame) void BrowserMainWindow::slotPrivateBrowsing() { -#if defined(QTWEBENGINE_PRIVATEBROWSING) - QWebEngineSettings *settings = QWebEngineSettings::globalSettings(); - bool pb = settings->testAttribute(QWebEngineSettings::PrivateBrowsingEnabled); - if (!pb) { + if (!BrowserApplication::instance()->privateBrowsing()) { QString title = tr("Are you sure you want to turn on private browsing?"); - QString text = tr("%1

When private browsing in turned on," + QString text = tr("%1

" + "This action will reload all open tabs.
" + "When private browsing in turned on," " webpages are not added to the history," " items are automatically removed from the Downloads window," \ " new cookies are not stored, current cookies can't be accessed," \ @@ -720,20 +720,13 @@ void BrowserMainWindow::slotPrivateBrowsing() QMessageBox::StandardButton button = QMessageBox::question(this, QString(), text, QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok); - if (button == QMessageBox::Ok) { - settings->setAttribute(QWebEngineSettings::PrivateBrowsingEnabled, true); - } - } else { - settings->setAttribute(QWebEngineSettings::PrivateBrowsingEnabled, false); - QList windows = BrowserApplication::instance()->mainWindows(); - for (int i = 0; i < windows.count(); ++i) { - BrowserMainWindow *window = windows.at(i); - window->m_lastSearch = QString::null; - window->tabWidget()->clear(); - } + if (button == QMessageBox::Ok) + BrowserApplication::instance()->setPrivateBrowsing(true); + } else { + // TODO: Also ask here + BrowserApplication::instance()->setPrivateBrowsing(false); } -#endif } void BrowserMainWindow::closeEvent(QCloseEvent *event) diff --git a/examples/webenginewidgets/browser/browsermainwindow.h b/examples/webenginewidgets/browser/browsermainwindow.h index 831504700..a2bbc443f 100644 --- a/examples/webenginewidgets/browser/browsermainwindow.h +++ b/examples/webenginewidgets/browser/browsermainwindow.h @@ -170,6 +170,7 @@ private: QIcon m_stopIcon; QString m_lastSearch; + friend class BrowserApplication; }; #endif // BROWSERMAINWINDOW_H diff --git a/examples/webenginewidgets/browser/downloadmanager.cpp b/examples/webenginewidgets/browser/downloadmanager.cpp index 9609589d7..90b0cec9d 100644 --- a/examples/webenginewidgets/browser/downloadmanager.cpp +++ b/examples/webenginewidgets/browser/downloadmanager.cpp @@ -352,12 +352,9 @@ void DownloadManager::updateRow() downloadsView->setRowHeight(row, widget->minimumSizeHint().height()); bool remove = false; -#if defined(QTWEBENGINE_PRIVATEBROWSING) - QWebEngineSettings *globalSettings = QWebEngineSettings::globalSettings(); if (!widget->downloading() - && globalSettings->testAttribute(QWebEngineSettings::PrivateBrowsingEnabled)) + && BrowserApplication::instance()->privateBrowsing()) remove = true; -#endif if (widget->downloadedSuccessfully() && removePolicy() == DownloadManager::SuccessFullDownload) { diff --git a/examples/webenginewidgets/browser/history.cpp b/examples/webenginewidgets/browser/history.cpp index ffae0f55e..d9d76cd29 100644 --- a/examples/webenginewidgets/browser/history.cpp +++ b/examples/webenginewidgets/browser/history.cpp @@ -189,11 +189,8 @@ void HistoryManager::checkForExpired() void HistoryManager::addHistoryItem(const HistoryItem &item) { -#if defined(QTWEBENGINE_PRIVATEBROWSING) - QWebEngineSettings *globalSettings = QWebEngineSettings::globalSettings(); - if (globalSettings->testAttribute(QWebEngineSettings::PrivateBrowsingEnabled)) + if (BrowserApplication::instance()->privateBrowsing()) return; -#endif m_history.prepend(item); emit entryAdded(item); diff --git a/examples/webenginewidgets/browser/history.h b/examples/webenginewidgets/browser/history.h index 5a747c942..3519d31b6 100644 --- a/examples/webenginewidgets/browser/history.h +++ b/examples/webenginewidgets/browser/history.h @@ -81,6 +81,7 @@ class AutoSaver; class HistoryModel; class HistoryFilterModel; class HistoryTreeModel; + class HistoryManager #if defined(QWEBENGINEHISTORYINTERFACE) : public QWebEngineHistoryInterface diff --git a/examples/webenginewidgets/browser/tabwidget.cpp b/examples/webenginewidgets/browser/tabwidget.cpp index 06ff59ac3..3df1628e0 100644 --- a/examples/webenginewidgets/browser/tabwidget.cpp +++ b/examples/webenginewidgets/browser/tabwidget.cpp @@ -217,6 +217,7 @@ TabWidget::TabWidget(QWidget *parent) , m_lineEditCompleter(0) , m_lineEdits(0) , m_tabBar(new TabBar(this)) + , m_profile(QWebEngineProfile::defaultProfile()) { setElideMode(Qt::ElideRight); @@ -456,6 +457,7 @@ WebView *TabWidget::newTab(bool makeCurrent) // webview WebView *webView = new WebView; + webView->setPage(new WebPage(m_profile, webView)); urlLineEdit->setWebView(webView); connect(webView, SIGNAL(loadStarted()), this, SLOT(webViewLoadStarted())); @@ -581,11 +583,7 @@ void TabWidget::closeTab(int index) #endif hasFocus = tab->hasFocus(); -#if defined(QTWEBENGINE_PRIVATEBROWSING) - QWebEngineSettings *globalSettings = QWebEngineSettings::globalSettings(); - if (!globalSettings->testAttribute(QWebEngineSettings::PrivateBrowsingEnabled)) -#endif - { + if (m_profile == QWebEngineProfile::defaultProfile()) { m_recentlyClosedTabsAction->setEnabled(true); m_recentlyClosedTabs.prepend(tab->url()); if (m_recentlyClosedTabs.size() >= TabWidget::m_recentlyClosedTabsSize) @@ -605,6 +603,19 @@ void TabWidget::closeTab(int index) emit lastTabClosed(); } +void TabWidget::setProfile(QWebEngineProfile *profile) +{ + m_profile = profile; + for (int i = 0; i < count(); ++i) { + QWidget *tabWidget = widget(i); + if (WebView *tab = qobject_cast(tabWidget)) { + WebPage* webPage = new WebPage(m_profile, tab); + webPage->load(tab->page()->url()); + tab->setPage(webPage); + } + } +} + void TabWidget::webViewLoadStarted() { WebView *webView = qobject_cast(sender()); diff --git a/examples/webenginewidgets/browser/tabwidget.h b/examples/webenginewidgets/browser/tabwidget.h index 74e98f46c..5d7f1e2c5 100644 --- a/examples/webenginewidgets/browser/tabwidget.h +++ b/examples/webenginewidgets/browser/tabwidget.h @@ -48,6 +48,7 @@ QT_BEGIN_NAMESPACE class QWebEngineDownloadItem; +class QWebEngineProfile; QT_END_NAMESPACE /* @@ -182,6 +183,8 @@ public: QByteArray saveState() const; bool restoreState(const QByteArray &state); + void setProfile(QWebEngineProfile *profile); + protected: void mouseDoubleClickEvent(QMouseEvent *event); void contextMenuEvent(QContextMenuEvent *event); @@ -226,6 +229,7 @@ private: QCompleter *m_lineEditCompleter; QStackedWidget *m_lineEdits; TabBar *m_tabBar; + QWebEngineProfile *m_profile; }; #endif // TABWIDGET_H diff --git a/examples/webenginewidgets/browser/toolbarsearch.cpp b/examples/webenginewidgets/browser/toolbarsearch.cpp index fa6e4f239..42cdaec18 100644 --- a/examples/webenginewidgets/browser/toolbarsearch.cpp +++ b/examples/webenginewidgets/browser/toolbarsearch.cpp @@ -41,6 +41,7 @@ #include "toolbarsearch.h" #include "autosaver.h" +#include "browserapplication.h" #include #include @@ -109,11 +110,7 @@ void ToolbarSearch::searchNow() if (newList.size() >= m_maxSavedSearches) newList.removeLast(); -#if defined(QTWEBENGINE_PRIVATEBROWSING) - QWebEngineSettings *globalSettings = QWebEngineSettings::globalSettings(); - if (!globalSettings->testAttribute(QWebEngineSettings::PrivateBrowsingEnabled)) -#endif - { + if (!BrowserApplication::instance()->privateBrowsing()) { m_stringListModel->setStringList(newList); m_autosaver->changeOccurred(); } diff --git a/examples/webenginewidgets/browser/webview.cpp b/examples/webenginewidgets/browser/webview.cpp index 6f503a3a2..99986642e 100644 --- a/examples/webenginewidgets/browser/webview.cpp +++ b/examples/webenginewidgets/browser/webview.cpp @@ -68,8 +68,8 @@ #include #include -WebPage::WebPage(QObject *parent) - : QWebEnginePage(parent) +WebPage::WebPage(QWebEngineProfile *profile, QObject *parent) + : QWebEnginePage(profile, parent) , m_keyboardModifiers(Qt::NoModifier) , m_pressedButtons(Qt::NoButton) , m_openInNewTab(false) @@ -129,10 +129,11 @@ bool WebPage::certificateError(const QWebEngineCertificateError &error) class PopupWindow : public QWidget { Q_OBJECT public: - PopupWindow() + PopupWindow(QWebEngineProfile *profile) : m_addressBar(new QLineEdit(this)) , m_view(new WebView(this)) { + m_view->setPage(new WebPage(profile, m_view)); QVBoxLayout *layout = new QVBoxLayout; layout->setMargin(0); setLayout(layout); @@ -182,7 +183,7 @@ QWebEnginePage *WebPage::createWindow(QWebEnginePage::WebWindowType type) BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow(); return mainWindow->currentTab()->page(); } else { - PopupWindow *popup = new PopupWindow; + PopupWindow *popup = new PopupWindow(profile()); popup->setAttribute(Qt::WA_DeleteOnClose); popup->show(); return popup->page(); @@ -306,18 +307,25 @@ void WebPage::proxyAuthenticationRequired(const QUrl &requestUrl, QAuthenticator WebView::WebView(QWidget* parent) : QWebEngineView(parent) , m_progress(0) - , m_page(new WebPage(this)) + , m_page(0) , m_iconReply(0) { - setPage(m_page); -#if defined(QWEBENGINEPAGE_STATUSBARMESSAGE) - connect(page(), SIGNAL(statusBarMessage(QString)), - SLOT(setStatusBarText(QString))); -#endif connect(this, SIGNAL(loadProgress(int)), this, SLOT(setProgress(int))); connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool))); +} + +void WebView::setPage(WebPage *_page) +{ + if (m_page) + m_page->deleteLater(); + m_page = _page; + QWebEngineView::setPage(_page); +#if defined(QWEBENGINEPAGE_STATUSBARMESSAGE) + connect(page(), SIGNAL(statusBarMessage(QString)), + SLOT(setStatusBarText(QString))); +#endif connect(page(), SIGNAL(loadingUrl(QUrl)), this, SIGNAL(urlChanged(QUrl))); connect(page(), SIGNAL(iconUrlChanged(QUrl)), @@ -326,7 +334,6 @@ WebView::WebView(QWidget* parent) #if defined(QWEBENGINEPAGE_UNSUPPORTEDCONTENT) page()->setForwardUnsupportedContent(true); #endif - } void WebView::contextMenuEvent(QContextMenuEvent *event) diff --git a/examples/webenginewidgets/browser/webview.h b/examples/webenginewidgets/browser/webview.h index e694d50ac..a147d780a 100644 --- a/examples/webenginewidgets/browser/webview.h +++ b/examples/webenginewidgets/browser/webview.h @@ -61,7 +61,7 @@ signals: void loadingUrl(const QUrl &url); public: - WebPage(QObject *parent = 0); + WebPage(QWebEngineProfile *profile, QObject *parent = 0); BrowserMainWindow *mainWindow(); protected: @@ -95,6 +95,7 @@ class WebView : public QWebEngineView { public: WebView(QWidget *parent = 0); WebPage *webPage() const { return m_page; } + void setPage(WebPage *page); void loadUrl(const QUrl &url); QUrl url() const; -- cgit v1.2.3