diff options
Diffstat (limited to 'src/webenginewidgets/api/qwebengineprofile.cpp')
-rw-r--r-- | src/webenginewidgets/api/qwebengineprofile.cpp | 137 |
1 files changed, 77 insertions, 60 deletions
diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index 746fe55e8..b2fe49da3 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -38,29 +38,29 @@ ****************************************************************************/ #include "qwebengineprofile.h" +#include "qwebengineprofile_p.h" #include "qwebenginecookiestore.h" #include "qwebenginedownloaditem.h" #include "qwebenginedownloaditem_p.h" #include "qwebenginepage.h" -#include "qwebengineprofile_p.h" +#include "qwebenginepage_p.h" #include "qwebenginesettings.h" #include "qwebenginescriptcollection_p.h" - -#include "qwebenginebrowsercontext_p.h" #include "qtwebenginecoreglobal.h" -#include "browser_context_adapter.h" +#include "profile_adapter.h" #include "visited_links_manager_qt.h" #include "web_engine_settings.h" + QT_BEGIN_NAMESPACE -ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::UnknownSaveFormat, QtWebEngineCore::BrowserContextAdapterClient::UnknownSavePageFormat) -ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::SingleHtmlSaveFormat, QtWebEngineCore::BrowserContextAdapterClient::SingleHtmlSaveFormat) -ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::CompleteHtmlSaveFormat, QtWebEngineCore::BrowserContextAdapterClient::CompleteHtmlSaveFormat) -ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::MimeHtmlSaveFormat, QtWebEngineCore::BrowserContextAdapterClient::MimeHtmlSaveFormat) +ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::UnknownSaveFormat, QtWebEngineCore::ProfileAdapterClient::UnknownSavePageFormat) +ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::SingleHtmlSaveFormat, QtWebEngineCore::ProfileAdapterClient::SingleHtmlSaveFormat) +ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::CompleteHtmlSaveFormat, QtWebEngineCore::ProfileAdapterClient::CompleteHtmlSaveFormat) +ASSERT_ENUMS_MATCH(QWebEngineDownloadItem::MimeHtmlSaveFormat, QtWebEngineCore::ProfileAdapterClient::MimeHtmlSaveFormat) -using QtWebEngineCore::BrowserContextAdapter; +using QtWebEngineCore::ProfileAdapter; /*! \class QWebEngineProfile @@ -148,32 +148,41 @@ using QtWebEngineCore::BrowserContextAdapter; \sa QWebEngineDownloadItem, QWebEnginePage::download() */ -QWebEngineProfilePrivate::QWebEngineProfilePrivate(QSharedPointer<QtWebEngineCore::BrowserContextAdapter> browserContext) - : m_settings(new QWebEngineSettings()) - , m_scriptCollection(new QWebEngineScriptCollection(new QWebEngineScriptCollectionPrivate(browserContext->userResourceController()))) - , m_browserContext(new QWebEngineBrowserContext(browserContext, this)) +QWebEngineProfilePrivate::QWebEngineProfilePrivate(ProfileAdapter* profileAdapter) + : m_settings(new QWebEngineSettings()) + , m_profileAdapter(profileAdapter) + , m_scriptCollection(new QWebEngineScriptCollection( + new QWebEngineScriptCollectionPrivate(profileAdapter->userResourceController()))) { + m_profileAdapter->addClient(this); m_settings->d_ptr->initDefaults(); } QWebEngineProfilePrivate::~QWebEngineProfilePrivate() { - delete m_settings; - m_settings = 0; + if (m_profileAdapter) { + // In the case the user sets this profile as the parent of the interceptor + // it can be deleted before the browser-context still referencing it is. + m_profileAdapter->setRequestInterceptor(nullptr); + m_profileAdapter->removeClient(this); + } - Q_FOREACH (QWebEngineDownloadItem* download, m_ongoingDownloads) { + for (QWebEngineDownloadItem *download : qAsConst(m_ongoingDownloads)) { if (download) download->cancel(); } m_ongoingDownloads.clear(); - if (m_browserContext) - m_browserContext->shutdown(); + + if (m_profileAdapter != QtWebEngineCore::ProfileAdapter::defaultProfileAdapter()) + delete m_profileAdapter; + + delete m_settings; } -QSharedPointer<QtWebEngineCore::BrowserContextAdapter> QWebEngineProfilePrivate::browserContext() const +ProfileAdapter* QWebEngineProfilePrivate::profileAdapter() const { - return m_browserContext ? m_browserContext->browserContextRef : nullptr; + return m_profileAdapter; } void QWebEngineProfilePrivate::downloadDestroyed(quint32 downloadId) @@ -194,6 +203,10 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->mimeType = info.mimeType; itemPrivate->savePageFormat = static_cast<QWebEngineDownloadItem::SavePageFormat>(info.savePageFormat); itemPrivate->type = static_cast<QWebEngineDownloadItem::DownloadType>(info.downloadType); + if (info.page && info.page->clientType() == QtWebEngineCore::WebContentsAdapterClient::WidgetsClient) + itemPrivate->page = static_cast<QWebEnginePagePrivate *>(info.page)->q_ptr; + else + itemPrivate->page = nullptr; QWebEngineDownloadItem *download = new QWebEngineDownloadItem(itemPrivate, q); @@ -204,7 +217,7 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) QWebEngineDownloadItem::DownloadState state = download->state(); info.path = download->path(); - info.savePageFormat = static_cast<QtWebEngineCore::BrowserContextAdapterClient::SavePageFormat>( + info.savePageFormat = static_cast<QtWebEngineCore::ProfileAdapterClient::SavePageFormat>( download->savePageFormat()); info.accepted = state != QWebEngineDownloadItem::DownloadCancelled; @@ -245,7 +258,7 @@ void QWebEngineProfilePrivate::downloadUpdated(const DownloadItemInfo &info) */ QWebEngineProfile::QWebEngineProfile(QObject *parent) : QObject(parent) - , d_ptr(new QWebEngineProfilePrivate(QSharedPointer<BrowserContextAdapter>::create(true))) + , d_ptr(new QWebEngineProfilePrivate(new QtWebEngineCore::ProfileAdapter())) { d_ptr->q_ptr = this; } @@ -262,7 +275,7 @@ QWebEngineProfile::QWebEngineProfile(QObject *parent) */ QWebEngineProfile::QWebEngineProfile(const QString &storageName, QObject *parent) : QObject(parent) - , d_ptr(new QWebEngineProfilePrivate(QSharedPointer<BrowserContextAdapter>::create(storageName))) + , d_ptr(new QWebEngineProfilePrivate(new QtWebEngineCore::ProfileAdapter(storageName))) { d_ptr->q_ptr = this; } @@ -290,7 +303,7 @@ QWebEngineProfile::~QWebEngineProfile() QString QWebEngineProfile::storageName() const { const Q_D(QWebEngineProfile); - return d->browserContext()->storageName(); + return d->profileAdapter()->storageName(); } /*! @@ -302,7 +315,7 @@ QString QWebEngineProfile::storageName() const bool QWebEngineProfile::isOffTheRecord() const { const Q_D(QWebEngineProfile); - return d->browserContext()->isOffTheRecord(); + return d->profileAdapter()->isOffTheRecord(); } /*! @@ -321,7 +334,7 @@ bool QWebEngineProfile::isOffTheRecord() const QString QWebEngineProfile::persistentStoragePath() const { const Q_D(QWebEngineProfile); - return d->browserContext()->dataPath(); + return d->profileAdapter()->dataPath(); } /*! @@ -334,7 +347,7 @@ QString QWebEngineProfile::persistentStoragePath() const void QWebEngineProfile::setPersistentStoragePath(const QString &path) { const Q_D(QWebEngineProfile); - d->browserContext()->setDataPath(path); + d->profileAdapter()->setDataPath(path); } /*! @@ -351,7 +364,7 @@ void QWebEngineProfile::setPersistentStoragePath(const QString &path) QString QWebEngineProfile::cachePath() const { const Q_D(QWebEngineProfile); - return d->browserContext()->cachePath(); + return d->profileAdapter()->cachePath(); } /*! @@ -364,7 +377,7 @@ QString QWebEngineProfile::cachePath() const void QWebEngineProfile::setCachePath(const QString &path) { Q_D(QWebEngineProfile); - d->browserContext()->setCachePath(path); + d->profileAdapter()->setCachePath(path); } /*! @@ -379,7 +392,7 @@ void QWebEngineProfile::setCachePath(const QString &path) QString QWebEngineProfile::httpUserAgent() const { const Q_D(QWebEngineProfile); - return d->browserContext()->httpUserAgent(); + return d->profileAdapter()->httpUserAgent(); } /*! @@ -390,7 +403,7 @@ QString QWebEngineProfile::httpUserAgent() const void QWebEngineProfile::setHttpUserAgent(const QString &userAgent) { Q_D(QWebEngineProfile); - d->browserContext()->setHttpUserAgent(userAgent); + d->profileAdapter()->setHttpUserAgent(userAgent); } /*! @@ -403,7 +416,7 @@ void QWebEngineProfile::setHttpUserAgent(const QString &userAgent) QWebEngineProfile::HttpCacheType QWebEngineProfile::httpCacheType() const { const Q_D(QWebEngineProfile); - return QWebEngineProfile::HttpCacheType(d->browserContext()->httpCacheType()); + return QWebEngineProfile::HttpCacheType(d->profileAdapter()->httpCacheType()); } /*! @@ -414,7 +427,7 @@ QWebEngineProfile::HttpCacheType QWebEngineProfile::httpCacheType() const void QWebEngineProfile::setHttpCacheType(QWebEngineProfile::HttpCacheType httpCacheType) { Q_D(QWebEngineProfile); - d->browserContext()->setHttpCacheType(BrowserContextAdapter::HttpCacheType(httpCacheType)); + d->profileAdapter()->setHttpCacheType(ProfileAdapter::HttpCacheType(httpCacheType)); } /*! @@ -425,7 +438,7 @@ void QWebEngineProfile::setHttpCacheType(QWebEngineProfile::HttpCacheType httpCa void QWebEngineProfile::setHttpAcceptLanguage(const QString &httpAcceptLanguage) { Q_D(QWebEngineProfile); - d->browserContext()->setHttpAcceptLanguage(httpAcceptLanguage); + d->profileAdapter()->setHttpAcceptLanguage(httpAcceptLanguage); } /*! @@ -436,7 +449,7 @@ void QWebEngineProfile::setHttpAcceptLanguage(const QString &httpAcceptLanguage) QString QWebEngineProfile::httpAcceptLanguage() const { Q_D(const QWebEngineProfile); - return d->browserContext()->httpAcceptLanguage(); + return d->profileAdapter()->httpAcceptLanguage(); } /*! @@ -449,7 +462,7 @@ QString QWebEngineProfile::httpAcceptLanguage() const QWebEngineProfile::PersistentCookiesPolicy QWebEngineProfile::persistentCookiesPolicy() const { const Q_D(QWebEngineProfile); - return QWebEngineProfile::PersistentCookiesPolicy(d->browserContext()->persistentCookiesPolicy()); + return QWebEngineProfile::PersistentCookiesPolicy(d->profileAdapter()->persistentCookiesPolicy()); } /*! @@ -460,7 +473,7 @@ QWebEngineProfile::PersistentCookiesPolicy QWebEngineProfile::persistentCookiesP void QWebEngineProfile::setPersistentCookiesPolicy(QWebEngineProfile::PersistentCookiesPolicy newPersistentCookiesPolicy) { Q_D(QWebEngineProfile); - d->browserContext()->setPersistentCookiesPolicy(BrowserContextAdapter::PersistentCookiesPolicy(newPersistentCookiesPolicy)); + d->profileAdapter()->setPersistentCookiesPolicy(ProfileAdapter::PersistentCookiesPolicy(newPersistentCookiesPolicy)); } /*! @@ -473,7 +486,7 @@ void QWebEngineProfile::setPersistentCookiesPolicy(QWebEngineProfile::Persistent int QWebEngineProfile::httpCacheMaximumSize() const { const Q_D(QWebEngineProfile); - return d->browserContext()->httpCacheMaxSize(); + return d->profileAdapter()->httpCacheMaxSize(); } /*! @@ -486,7 +499,7 @@ int QWebEngineProfile::httpCacheMaximumSize() const void QWebEngineProfile::setHttpCacheMaximumSize(int maxSize) { Q_D(QWebEngineProfile); - d->browserContext()->setHttpCacheMaxSize(maxSize); + d->profileAdapter()->setHttpCacheMaxSize(maxSize); } /*! @@ -498,7 +511,7 @@ void QWebEngineProfile::setHttpCacheMaximumSize(int maxSize) QWebEngineCookieStore* QWebEngineProfile::cookieStore() { Q_D(QWebEngineProfile); - return d->browserContext()->cookieStore(); + return d->profileAdapter()->cookieStore(); } @@ -514,7 +527,7 @@ QWebEngineCookieStore* QWebEngineProfile::cookieStore() void QWebEngineProfile::setRequestInterceptor(QWebEngineUrlRequestInterceptor *interceptor) { Q_D(QWebEngineProfile); - d->browserContext()->setRequestInterceptor(interceptor); + d->profileAdapter()->setRequestInterceptor(interceptor); } /*! @@ -525,7 +538,7 @@ void QWebEngineProfile::setRequestInterceptor(QWebEngineUrlRequestInterceptor *i void QWebEngineProfile::clearAllVisitedLinks() { Q_D(QWebEngineProfile); - d->browserContext()->visitedLinksManager()->deleteAllVisitedLinkData(); + d->profileAdapter()->visitedLinksManager()->deleteAllVisitedLinkData(); } /*! @@ -536,7 +549,7 @@ void QWebEngineProfile::clearAllVisitedLinks() void QWebEngineProfile::clearVisitedLinks(const QList<QUrl> &urls) { Q_D(QWebEngineProfile); - d->browserContext()->visitedLinksManager()->deleteVisitedLinkDataForUrls(urls); + d->profileAdapter()->visitedLinksManager()->deleteVisitedLinkDataForUrls(urls); } /*! @@ -545,7 +558,7 @@ void QWebEngineProfile::clearVisitedLinks(const QList<QUrl> &urls) bool QWebEngineProfile::visitedLinksContainsUrl(const QUrl &url) const { Q_D(const QWebEngineProfile); - return d->browserContext()->visitedLinksManager()->containsUrl(url); + return d->profileAdapter()->visitedLinksManager()->containsUrl(url); } /*! @@ -570,8 +583,8 @@ QWebEngineScriptCollection *QWebEngineProfile::scripts() const QWebEngineProfile *QWebEngineProfile::defaultProfile() { static QWebEngineProfile* profile = new QWebEngineProfile( - new QWebEngineProfilePrivate(BrowserContextAdapter::defaultContext()), - BrowserContextAdapter::globalQObjectRoot()); + new QWebEngineProfilePrivate(ProfileAdapter::createDefaultProfileAdapter()), + ProfileAdapter::globalQObjectRoot()); return profile; } @@ -593,7 +606,7 @@ QWebEngineProfile *QWebEngineProfile::defaultProfile() void QWebEngineProfile::setSpellCheckLanguages(const QStringList &languages) { Q_D(QWebEngineProfile); - d->browserContext()->setSpellCheckLanguages(languages); + d->profileAdapter()->setSpellCheckLanguages(languages); } /*! @@ -604,7 +617,7 @@ void QWebEngineProfile::setSpellCheckLanguages(const QStringList &languages) QStringList QWebEngineProfile::spellCheckLanguages() const { const Q_D(QWebEngineProfile); - return d->browserContext()->spellCheckLanguages(); + return d->profileAdapter()->spellCheckLanguages(); } /*! @@ -616,7 +629,7 @@ QStringList QWebEngineProfile::spellCheckLanguages() const void QWebEngineProfile::setSpellCheckEnabled(bool enable) { Q_D(QWebEngineProfile); - d->browserContext()->setSpellCheckEnabled(enable); + d->profileAdapter()->setSpellCheckEnabled(enable); } /*! \since 5.8 @@ -627,7 +640,7 @@ void QWebEngineProfile::setSpellCheckEnabled(bool enable) bool QWebEngineProfile::isSpellCheckEnabled() const { const Q_D(QWebEngineProfile); - return d->browserContext()->isSpellCheckEnabled(); + return d->profileAdapter()->isSpellCheckEnabled(); } /*! @@ -647,8 +660,8 @@ QWebEngineSettings *QWebEngineProfile::settings() const const QWebEngineUrlSchemeHandler *QWebEngineProfile::urlSchemeHandler(const QByteArray &scheme) const { const Q_D(QWebEngineProfile); - if (d->browserContext()->customUrlSchemeHandlers().contains(scheme)) - return d->browserContext()->customUrlSchemeHandlers().value(scheme); + if (d->profileAdapter()->customUrlSchemeHandlers().contains(scheme)) + return d->profileAdapter()->customUrlSchemeHandlers().value(scheme); return 0; } @@ -667,22 +680,26 @@ static bool checkInternalScheme(const QByteArray &scheme) \since 5.6 Registers a handler \a handler for custom URL scheme \a scheme in the profile. + + It is recommended to first register the scheme with \l + QWebEngineUrlScheme::registerScheme at application startup. */ void QWebEngineProfile::installUrlSchemeHandler(const QByteArray &scheme, QWebEngineUrlSchemeHandler *handler) { Q_D(QWebEngineProfile); Q_ASSERT(handler); - if (checkInternalScheme(scheme)) { + QByteArray canonicalScheme = scheme.toLower(); + if (checkInternalScheme(canonicalScheme)) { qWarning("Cannot install a URL scheme handler overriding internal scheme: %s", scheme.constData()); return; } - if (d->browserContext()->customUrlSchemeHandlers().contains(scheme)) { - if (d->browserContext()->customUrlSchemeHandlers().value(scheme) != handler) + if (d->profileAdapter()->customUrlSchemeHandlers().contains(canonicalScheme)) { + if (d->profileAdapter()->customUrlSchemeHandlers().value(canonicalScheme) != handler) qWarning("URL scheme handler already installed for the scheme: %s", scheme.constData()); return; } - d->browserContext()->addCustomUrlSchemeHandler(scheme, handler); + d->profileAdapter()->addCustomUrlSchemeHandler(canonicalScheme, handler); connect(handler, SIGNAL(_q_destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler*)), this, SLOT(destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler*))); } @@ -697,7 +714,7 @@ void QWebEngineProfile::removeUrlSchemeHandler(QWebEngineUrlSchemeHandler *handl { Q_D(QWebEngineProfile); Q_ASSERT(handler); - if (!d->browserContext()->removeCustomUrlSchemeHandler(handler)) + if (!d->profileAdapter()->removeCustomUrlSchemeHandler(handler)) return; disconnect(handler, SIGNAL(_q_destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler*)), this, SLOT(destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler*))); } @@ -712,7 +729,7 @@ void QWebEngineProfile::removeUrlSchemeHandler(QWebEngineUrlSchemeHandler *handl void QWebEngineProfile::removeUrlScheme(const QByteArray &scheme) { Q_D(QWebEngineProfile); - QWebEngineUrlSchemeHandler *handler = d->browserContext()->takeCustomUrlSchemeHandler(scheme); + QWebEngineUrlSchemeHandler *handler = d->profileAdapter()->takeCustomUrlSchemeHandler(scheme); if (!handler) return; disconnect(handler, SIGNAL(_q_destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler*)), this, SLOT(destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler*))); @@ -726,7 +743,7 @@ void QWebEngineProfile::removeUrlScheme(const QByteArray &scheme) void QWebEngineProfile::removeAllUrlSchemeHandlers() { Q_D(QWebEngineProfile); - d->browserContext()->clearCustomUrlSchemeHandlers(); + d->profileAdapter()->clearCustomUrlSchemeHandlers(); } void QWebEngineProfile::destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler *obj) @@ -742,7 +759,7 @@ void QWebEngineProfile::destroyedUrlSchemeHandler(QWebEngineUrlSchemeHandler *ob void QWebEngineProfile::clearHttpCache() { Q_D(QWebEngineProfile); - d->browserContext()->clearHttpCache(); + d->profileAdapter()->clearHttpCache(); } QT_END_NAMESPACE |