diff options
20 files changed, 1211 insertions, 475 deletions
diff --git a/examples/webenginewidgets/browser/browserapplication.cpp b/examples/webenginewidgets/browser/browserapplication.cpp index 44713901d..ddcebfb8e 100644 --- a/examples/webenginewidgets/browser/browserapplication.cpp +++ b/examples/webenginewidgets/browser/browserapplication.cpp @@ -66,6 +66,7 @@ #include <QtNetwork/QNetworkProxy> #include <QtNetwork/QSslSocket> +#include <QWebEngineProfile> #include <QWebEngineSettings> #include <QtCore/QDebug> @@ -230,6 +231,8 @@ void BrowserApplication::loadSettings() settings.beginGroup(QLatin1String("websettings")); QWebEngineSettings *defaultSettings = QWebEngineSettings::globalSettings(); + QWebEngineProfile *defaultProfile = QWebEngineProfile::defaultProfile(); + QString standardFontFamily = defaultSettings->fontFamily(QWebEngineSettings::StandardFont); int standardFontSize = defaultSettings->fontSize(QWebEngineSettings::DefaultFontSize); QFont standardFont = QFont(standardFontFamily, standardFontSize); @@ -255,6 +258,14 @@ void BrowserApplication::loadSettings() QUrl url = settings.value(QLatin1String("userStyleSheet")).toUrl(); defaultSettings->setUserStyleSheetUrl(url); #endif + defaultProfile->setHttpUserAgent(settings.value(QLatin1String("httpUserAgent")).toString()); + settings.endGroup(); + settings.beginGroup(QLatin1String("cookies")); + + QWebEngineProfile::PersistentCookiesPolicy persistentCookiesPolicy = QWebEngineProfile::PersistentCookiesPolicy(settings.value(QLatin1String("persistentCookiesPolicy")).toInt()); + defaultProfile->setPersistentCookiesPolicy(persistentCookiesPolicy); + QString pdataPath = settings.value(QLatin1String("persistentDataPath")).toString(); + defaultProfile->setPersistentStoragePath(pdataPath); settings.endGroup(); } diff --git a/examples/webenginewidgets/browser/settings.cpp b/examples/webenginewidgets/browser/settings.cpp index 835e7a9b5..753ef033d 100644 --- a/examples/webenginewidgets/browser/settings.cpp +++ b/examples/webenginewidgets/browser/settings.cpp @@ -58,9 +58,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent) { setupUi(this); - connect(exceptionsButton, SIGNAL(clicked()), this, SLOT(showExceptions())); connect(setHomeToCurrentPageButton, SIGNAL(clicked()), this, SLOT(setHomeToCurrentPage())); - connect(cookiesButton, SIGNAL(clicked()), this, SLOT(showCookies())); connect(standardFontButton, SIGNAL(clicked()), this, SLOT(chooseFont())); connect(fixedFontButton, SIGNAL(clicked()), this, SLOT(chooseFixedFont())); @@ -89,6 +87,10 @@ void SettingsDialog::loadDefaults() #endif enableScrollAnimator->setChecked(defaultSettings->testAttribute(QWebEngineSettings::ScrollAnimatorEnabled)); + + persistentDataPath->setText(QWebEngineProfile::defaultProfile()->persistentStoragePath()); + sessionCookiesCombo->setCurrentIndex(QWebEngineProfile::defaultProfile()->persistentCookiesPolicy()); + httpUserAgent->setText(QWebEngineProfile::defaultProfile()->httpUserAgent()); } void SettingsDialog::loadFromSettings() @@ -137,47 +139,19 @@ void SettingsDialog::loadFromSettings() enablePlugins->setChecked(settings.value(QLatin1String("enablePlugins"), enablePlugins->isChecked()).toBool()); userStyleSheet->setText(settings.value(QLatin1String("userStyleSheet")).toUrl().toString()); enableScrollAnimator->setChecked(settings.value(QLatin1String("enableScrollAnimator"), enableScrollAnimator->isChecked()).toBool()); + httpUserAgent->setText(settings.value(QLatin1String("httpUserAgent"), httpUserAgent->text()).toString()); settings.endGroup(); -#if defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) // Privacy settings.beginGroup(QLatin1String("cookies")); - QByteArray value = settings.value(QLatin1String("acceptCookies"), QLatin1String("AcceptOnlyFromSitesNavigatedTo")).toByteArray(); - QMetaEnum acceptPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("AcceptPolicy")); - CookieJar::AcceptPolicy acceptCookies = acceptPolicyEnum.keyToValue(value) == -1 ? - CookieJar::AcceptOnlyFromSitesNavigatedTo : - static_cast<CookieJar::AcceptPolicy>(acceptPolicyEnum.keyToValue(value)); - switch (acceptCookies) { - case CookieJar::AcceptAlways: - acceptCombo->setCurrentIndex(0); - break; - case CookieJar::AcceptNever: - acceptCombo->setCurrentIndex(1); - break; - case CookieJar::AcceptOnlyFromSitesNavigatedTo: - acceptCombo->setCurrentIndex(2); - break; - } + int persistentCookiesPolicy = settings.value(QLatin1String("persistentCookiesPolicy"), sessionCookiesCombo->currentIndex()).toInt(); + sessionCookiesCombo->setCurrentIndex(persistentCookiesPolicy); + + QString pdataPath = settings.value(QLatin1String("persistentDataPath"), persistentDataPath->text()).toString(); + persistentDataPath->setText(pdataPath); - value = settings.value(QLatin1String("keepCookiesUntil"), QLatin1String("Expire")).toByteArray(); - QMetaEnum keepPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("KeepPolicy")); - CookieJar::KeepPolicy keepCookies = keepPolicyEnum.keyToValue(value) == -1 ? - CookieJar::KeepUntilExpire : - static_cast<CookieJar::KeepPolicy>(keepPolicyEnum.keyToValue(value)); - switch (keepCookies) { - case CookieJar::KeepUntilExpire: - keepUntilCombo->setCurrentIndex(0); - break; - case CookieJar::KeepUntilExit: - keepUntilCombo->setCurrentIndex(1); - break; - case CookieJar::KeepUntilTimeLimit: - keepUntilCombo->setCurrentIndex(2); - break; - } settings.endGroup(); -#endif // Proxy settings.beginGroup(QLatin1String("proxy")); @@ -227,47 +201,19 @@ void SettingsDialog::saveToSettings() settings.setValue(QLatin1String("userStyleSheet"), QUrl::fromLocalFile(userStyleSheetString)); else settings.setValue(QLatin1String("userStyleSheet"), QUrl(userStyleSheetString)); + settings.setValue(QLatin1String("httpUserAgent"), httpUserAgent->text()); settings.endGroup(); -#if defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) //Privacy settings.beginGroup(QLatin1String("cookies")); - CookieJar::KeepPolicy keepCookies; - switch (acceptCombo->currentIndex()) { - default: - case 0: - keepCookies = CookieJar::KeepUntilExpire; - break; - case 1: - keepCookies = CookieJar::KeepUntilExit; - break; - case 2: - keepCookies = CookieJar::KeepUntilTimeLimit; - break; - } - QMetaEnum acceptPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("AcceptPolicy")); - settings.setValue(QLatin1String("acceptCookies"), QLatin1String(acceptPolicyEnum.valueToKey(keepCookies))); - - CookieJar::KeepPolicy keepPolicy; - switch (keepUntilCombo->currentIndex()) { - default: - case 0: - keepPolicy = CookieJar::KeepUntilExpire; - break; - case 1: - keepPolicy = CookieJar::KeepUntilExit; - break; - case 2: - keepPolicy = CookieJar::KeepUntilTimeLimit; - break; - } + int persistentCookiesPolicy = sessionCookiesCombo->currentIndex(); + settings.setValue(QLatin1String("persistentCookiesPolicy"), persistentCookiesPolicy); - QMetaEnum keepPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("KeepPolicy")); - settings.setValue(QLatin1String("keepCookiesUntil"), QLatin1String(keepPolicyEnum.valueToKey(keepPolicy))); + QString pdataPath = persistentDataPath->text(); + settings.setValue(QLatin1String("persistentDataPath"), pdataPath); settings.endGroup(); -#endif // proxy settings.beginGroup(QLatin1String("proxy")); diff --git a/examples/webenginewidgets/browser/settings.ui b/examples/webenginewidgets/browser/settings.ui index 08374ca7a..7cafdae4b 100644 --- a/examples/webenginewidgets/browser/settings.ui +++ b/examples/webenginewidgets/browser/settings.ui @@ -1,7 +1,8 @@ -<ui version="4.0" > +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> <class>Settings</class> - <widget class="QDialog" name="Settings" > - <property name="geometry" > + <widget class="QDialog" name="Settings"> + <property name="geometry"> <rect> <x>0</x> <y>0</y> @@ -9,64 +10,56 @@ <height>322</height> </rect> </property> - <property name="windowTitle" > + <property name="windowTitle"> <string>Settings</string> </property> - <layout class="QGridLayout" name="gridLayout" > - <item row="2" column="0" > - <widget class="QDialogButtonBox" name="buttonBox" > - <property name="orientation" > + <layout class="QGridLayout" name="gridLayout"> + <item row="2" column="0"> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> <enum>Qt::Horizontal</enum> </property> - <property name="standardButtons" > + <property name="standardButtons"> <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> </property> </widget> </item> - <item row="1" column="0" > - <widget class="QTabWidget" name="tabWidget" > - <property name="currentIndex" > - <number>0</number> + <item row="1" column="0"> + <widget class="QTabWidget" name="tabWidget"> + <property name="currentIndex"> + <number>4</number> </property> - <widget class="QWidget" name="tab" > - <property name="geometry" > - <rect> - <x>0</x> - <y>0</y> - <width>627</width> - <height>243</height> - </rect> - </property> - <attribute name="title" > + <widget class="QWidget" name="tab"> + <attribute name="title"> <string>General</string> </attribute> - <layout class="QGridLayout" name="gridLayout_4" > - <item row="0" column="0" > - <widget class="QLabel" name="label_3" > - <property name="text" > + <layout class="QGridLayout" name="gridLayout_4"> + <item row="0" column="0"> + <widget class="QLabel" name="label_3"> + <property name="text"> <string>Home:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="0" column="1" colspan="2" > - <widget class="QLineEdit" name="homeLineEdit" /> + <item row="0" column="1" colspan="2"> + <widget class="QLineEdit" name="homeLineEdit"/> </item> - <item row="1" column="1" > - <widget class="QPushButton" name="setHomeToCurrentPageButton" > - <property name="text" > + <item row="1" column="1"> + <widget class="QPushButton" name="setHomeToCurrentPageButton"> + <property name="text"> <string>Set to current page</string> </property> </widget> </item> - <item row="1" column="2" > - <spacer name="horizontalSpacer" > - <property name="orientation" > + <item row="1" column="2"> + <spacer name="horizontalSpacer"> + <property name="orientation"> <enum>Qt::Horizontal</enum> </property> - <property name="sizeHint" stdset="0" > + <property name="sizeHint" stdset="0"> <size> <width>280</width> <height>18</height> @@ -74,103 +67,103 @@ </property> </spacer> </item> - <item row="2" column="0" > - <widget class="QLabel" name="label_4" > - <property name="text" > + <item row="2" column="0"> + <widget class="QLabel" name="label_4"> + <property name="text"> <string>Remove history items:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="2" column="1" colspan="2" > - <widget class="QComboBox" name="expireHistory" > + <item row="2" column="1" colspan="2"> + <widget class="QComboBox" name="expireHistory"> <item> - <property name="text" > + <property name="text"> <string>After one day</string> </property> </item> <item> - <property name="text" > + <property name="text"> <string>After one week</string> </property> </item> <item> - <property name="text" > + <property name="text"> <string>After two weeks</string> </property> </item> <item> - <property name="text" > + <property name="text"> <string>After one month</string> </property> </item> <item> - <property name="text" > + <property name="text"> <string>After one year</string> </property> </item> <item> - <property name="text" > + <property name="text"> <string>Manually</string> </property> </item> </widget> </item> - <item row="3" column="0" > - <widget class="QLabel" name="label_7" > - <property name="text" > + <item row="3" column="0"> + <widget class="QLabel" name="label_7"> + <property name="text"> <string>Save downloads to:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="3" column="1" colspan="2" > - <widget class="QLineEdit" name="downloadsLocation" /> + <item row="3" column="1" colspan="2"> + <widget class="QLineEdit" name="downloadsLocation"/> </item> - <item row="4" column="0" > - <widget class="QLabel" name="label_8" > - <property name="text" > + <item row="4" column="0"> + <widget class="QLabel" name="label_8"> + <property name="text"> <string>Open links from applications:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="4" column="1" colspan="2" > - <widget class="QComboBox" name="openLinksIn" > + <item row="4" column="1" colspan="2"> + <widget class="QComboBox" name="openLinksIn"> <item> - <property name="text" > + <property name="text"> <string>In a tab in the current window</string> </property> </item> <item> - <property name="text" > + <property name="text"> <string>In a new window</string> </property> </item> </widget> </item> <item row="5" column="1" colspan="2"> - <widget class="QCheckBox" name="enableScrollAnimator" > - <property name="text" > - <string>Enable Scroll Animator</string> + <widget class="QCheckBox" name="enableScrollAnimator"> + <property name="text"> + <string>Enable Scroll Animator</string> </property> - <property name="checked" > - <bool>true</bool> + <property name="checked"> + <bool>true</bool> </property> </widget> </item> - <item row="6" column="1" colspan="2" > + <item row="6" column="1" colspan="2"> <spacer> - <property name="orientation" > + <property name="orientation"> <enum>Qt::Vertical</enum> </property> - <property name="sizeHint" stdset="0" > + <property name="sizeHint" stdset="0"> <size> <width>391</width> <height>262</height> @@ -180,91 +173,83 @@ </item> </layout> </widget> - <widget class="QWidget" name="tab_3" > - <property name="geometry" > - <rect> - <x>0</x> - <y>0</y> - <width>627</width> - <height>243</height> - </rect> - </property> - <attribute name="title" > + <widget class="QWidget" name="tab_3"> + <attribute name="title"> <string>Appearance</string> </attribute> - <layout class="QGridLayout" name="gridLayout_3" > - <item row="0" column="0" > - <widget class="QLabel" name="label_5" > - <property name="text" > + <layout class="QGridLayout" name="gridLayout_3"> + <item row="0" column="0"> + <widget class="QLabel" name="label_5"> + <property name="text"> <string>Standard font:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="0" column="1" > - <widget class="QLabel" name="standardLabel" > - <property name="sizePolicy" > - <sizepolicy vsizetype="Preferred" hsizetype="Expanding" > + <item row="0" column="1"> + <widget class="QLabel" name="standardLabel"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> - <property name="frameShape" > + <property name="frameShape"> <enum>QFrame::StyledPanel</enum> </property> - <property name="text" > + <property name="text"> <string>Times 16</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> - <item row="0" column="2" > - <widget class="QPushButton" name="standardFontButton" > - <property name="text" > + <item row="0" column="2"> + <widget class="QPushButton" name="standardFontButton"> + <property name="text"> <string>Select...</string> </property> </widget> </item> - <item row="1" column="0" > - <widget class="QLabel" name="label_6" > - <property name="text" > + <item row="1" column="0"> + <widget class="QLabel" name="label_6"> + <property name="text"> <string>Fixed-width font:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="1" column="1" > - <widget class="QLabel" name="fixedLabel" > - <property name="frameShape" > + <item row="1" column="1"> + <widget class="QLabel" name="fixedLabel"> + <property name="frameShape"> <enum>QFrame::StyledPanel</enum> </property> - <property name="text" > + <property name="text"> <string>Courier 13</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> - <item row="1" column="2" > - <widget class="QPushButton" name="fixedFontButton" > - <property name="text" > + <item row="1" column="2"> + <widget class="QPushButton" name="fixedFontButton"> + <property name="text"> <string>Select...</string> </property> </widget> </item> - <item row="2" column="1" > - <spacer name="verticalSpacer" > - <property name="orientation" > + <item row="2" column="1"> + <spacer name="verticalSpacer"> + <property name="orientation"> <enum>Qt::Vertical</enum> </property> - <property name="sizeHint" stdset="0" > + <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>93</height> @@ -274,41 +259,33 @@ </item> </layout> </widget> - <widget class="QWidget" name="tab_2" > - <property name="geometry" > - <rect> - <x>0</x> - <y>0</y> - <width>627</width> - <height>243</height> - </rect> - </property> - <attribute name="title" > + <widget class="QWidget" name="tab_2"> + <attribute name="title"> <string>Privacy</string> </attribute> - <layout class="QVBoxLayout" name="verticalLayout_3" > + <layout class="QVBoxLayout" name="verticalLayout_3"> <item> - <widget class="QGroupBox" name="groupBox" > - <property name="title" > + <widget class="QGroupBox" name="groupBox"> + <property name="title"> <string>Web Content</string> </property> - <layout class="QVBoxLayout" name="verticalLayout_2" > + <layout class="QVBoxLayout" name="verticalLayout_2"> <item> - <widget class="QCheckBox" name="enablePlugins" > - <property name="text" > + <widget class="QCheckBox" name="enablePlugins"> + <property name="text"> <string>Enable Plugins</string> </property> - <property name="checked" > + <property name="checked"> <bool>true</bool> </property> </widget> </item> <item> - <widget class="QCheckBox" name="enableJavascript" > - <property name="text" > + <widget class="QCheckBox" name="enableJavascript"> + <property name="text"> <string>Enable Javascript</string> </property> - <property name="checked" > + <property name="checked"> <bool>true</bool> </property> </widget> @@ -317,186 +294,151 @@ </widget> </item> <item> - <widget class="QGroupBox" name="cookiesGroupBox" > - <property name="title" > + <widget class="QGroupBox" name="cookiesGroupBox"> + <property name="title"> <string>Cookies</string> </property> - <layout class="QGridLayout" > - <item row="0" column="0" > - <widget class="QLabel" name="label_2" > - <property name="text" > - <string>Accept Cookies:</string> + <layout class="QGridLayout"> + <property name="leftMargin"> + <number>9</number> + </property> + <item row="0" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Persistent Cookie Policy</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="0" column="1" > - <widget class="QComboBox" name="acceptCombo" > + <item row="0" column="1"> + <widget class="QComboBox" name="sessionCookiesCombo"> <item> - <property name="text" > - <string>Always</string> + <property name="text"> + <string>Treat all cookies as session cookies</string> </property> </item> <item> - <property name="text" > - <string>Never</string> + <property name="text"> + <string>Allow persistent cookies</string> </property> </item> <item> - <property name="text" > - <string>Only from sites you navigate to</string> + <property name="text"> + <string>Treat all cookies as persistent cookies</string> </property> </item> </widget> </item> - <item row="0" column="2" > - <widget class="QPushButton" name="exceptionsButton" > - <property name="text" > - <string>Exceptions...</string> - </property> - </widget> - </item> - <item row="1" column="0" > - <widget class="QLabel" name="label" > - <property name="text" > - <string>Keep until:</string> + <item row="1" column="0"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Persistent Data Path:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="1" column="1" > - <widget class="QComboBox" name="keepUntilCombo" > - <item> - <property name="text" > - <string>They expire</string> - </property> - </item> - <item> - <property name="text" > - <string>I exit the application</string> - </property> - </item> - <item> - <property name="text" > - <string>At most 90 days</string> - </property> - </item> - </widget> - </item> - <item row="1" column="2" > - <widget class="QPushButton" name="cookiesButton" > - <property name="text" > - <string>Cookies...</string> - </property> - </widget> + <item row="1" column="1"> + <widget class="QLineEdit" name="persistentDataPath"/> </item> </layout> </widget> </item> <item> - <spacer> - <property name="orientation" > + <spacer name="verticalSpacer_4"> + <property name="orientation"> <enum>Qt::Vertical</enum> </property> - <property name="sizeHint" stdset="0" > + <property name="sizeHint" stdset="0"> <size> - <width>371</width> - <height>177</height> + <width>20</width> + <height>40</height> </size> </property> </spacer> </item> </layout> </widget> - <widget class="QWidget" name="tab_4" > - <property name="geometry" > - <rect> - <x>0</x> - <y>0</y> - <width>627</width> - <height>243</height> - </rect> - </property> - <attribute name="title" > + <widget class="QWidget" name="tab_4"> + <attribute name="title"> <string>Proxy</string> </attribute> - <layout class="QVBoxLayout" name="verticalLayout" > + <layout class="QVBoxLayout" name="verticalLayout"> <item> - <widget class="QGroupBox" name="proxySupport" > - <property name="title" > + <widget class="QGroupBox" name="proxySupport"> + <property name="title"> <string>Enable proxy</string> </property> - <property name="checkable" > + <property name="checkable"> <bool>true</bool> </property> - <layout class="QGridLayout" name="gridLayout_6" > - <item row="0" column="0" > - <widget class="QLabel" name="label_9" > - <property name="text" > + <layout class="QGridLayout" name="gridLayout_6"> + <item row="0" column="0"> + <widget class="QLabel" name="label_9"> + <property name="text"> <string>Type:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="0" column="1" colspan="2" > - <widget class="QComboBox" name="proxyType" > + <item row="0" column="1" colspan="2"> + <widget class="QComboBox" name="proxyType"> <item> - <property name="text" > + <property name="text"> <string>Socks5</string> </property> </item> <item> - <property name="text" > + <property name="text"> <string>Http</string> </property> </item> </widget> </item> - <item row="1" column="0" > - <widget class="QLabel" name="label_10" > - <property name="text" > + <item row="1" column="0"> + <widget class="QLabel" name="label_10"> + <property name="text"> <string>Host:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="1" column="1" colspan="2" > - <widget class="QLineEdit" name="proxyHostName" /> + <item row="1" column="1" colspan="2"> + <widget class="QLineEdit" name="proxyHostName"/> </item> - <item row="2" column="0" > - <widget class="QLabel" name="label_11" > - <property name="text" > + <item row="2" column="0"> + <widget class="QLabel" name="label_11"> + <property name="text"> <string>Port:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="2" column="1" > - <widget class="QSpinBox" name="proxyPort" > - <property name="maximum" > + <item row="2" column="1"> + <widget class="QSpinBox" name="proxyPort"> + <property name="maximum"> <number>10000</number> </property> - <property name="value" > + <property name="value"> <number>1080</number> </property> </widget> </item> - <item row="2" column="2" > - <spacer name="horizontalSpacer_2" > - <property name="orientation" > + <item row="2" column="2"> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> <enum>Qt::Horizontal</enum> </property> - <property name="sizeHint" stdset="0" > + <property name="sizeHint" stdset="0"> <size> <width>293</width> <height>20</height> @@ -504,42 +446,42 @@ </property> </spacer> </item> - <item row="3" column="0" > - <widget class="QLabel" name="label_12" > - <property name="text" > + <item row="3" column="0"> + <widget class="QLabel" name="label_12"> + <property name="text"> <string>User Name:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="3" column="1" colspan="2" > - <widget class="QLineEdit" name="proxyUserName" /> + <item row="3" column="1" colspan="2"> + <widget class="QLineEdit" name="proxyUserName"/> </item> - <item row="4" column="0" > - <widget class="QLabel" name="label_13" > - <property name="text" > + <item row="4" column="0"> + <widget class="QLabel" name="label_13"> + <property name="text"> <string>Password:</string> </property> - <property name="alignment" > + <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> - <item row="4" column="1" colspan="2" > - <widget class="QLineEdit" name="proxyPassword" > - <property name="echoMode" > + <item row="4" column="1" colspan="2"> + <widget class="QLineEdit" name="proxyPassword"> + <property name="echoMode"> <enum>QLineEdit::Password</enum> </property> </widget> </item> - <item row="5" column="0" > - <spacer name="verticalSpacer_2" > - <property name="orientation" > + <item row="5" column="0"> + <spacer name="verticalSpacer_2"> + <property name="orientation"> <enum>Qt::Vertical</enum> </property> - <property name="sizeHint" stdset="0" > + <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>8</height> @@ -552,27 +494,27 @@ </item> </layout> </widget> - <widget class="QWidget" name="tab_5" > - <attribute name="title" > + <widget class="QWidget" name="tab_5"> + <attribute name="title"> <string>Advanced</string> </attribute> - <layout class="QGridLayout" name="gridLayout_2" > - <item row="0" column="0" > - <widget class="QLabel" name="label_14" > - <property name="text" > + <layout class="QGridLayout" name="gridLayout_2"> + <item row="0" column="0"> + <widget class="QLabel" name="label_14"> + <property name="text"> <string>Style Sheet:</string> </property> </widget> </item> - <item row="0" column="1" > - <widget class="QLineEdit" name="userStyleSheet" /> + <item row="0" column="1"> + <widget class="QLineEdit" name="userStyleSheet"/> </item> - <item row="1" column="1" > - <spacer name="verticalSpacer_3" > - <property name="orientation" > + <item row="2" column="1"> + <spacer name="verticalSpacer_3"> + <property name="orientation"> <enum>Qt::Vertical</enum> </property> - <property name="sizeHint" stdset="0" > + <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>176</height> @@ -580,6 +522,16 @@ </property> </spacer> </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="httpUserAgent"/> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_15"> + <property name="text"> + <string>HTTP User-Agent:</string> + </property> + </widget> + </item> </layout> </widget> </widget> @@ -594,11 +546,11 @@ <receiver>Settings</receiver> <slot>accept()</slot> <hints> - <hint type="sourcelabel" > + <hint type="sourcelabel"> <x>248</x> <y>254</y> </hint> - <hint type="destinationlabel" > + <hint type="destinationlabel"> <x>157</x> <y>274</y> </hint> @@ -610,11 +562,11 @@ <receiver>Settings</receiver> <slot>reject()</slot> <hints> - <hint type="sourcelabel" > + <hint type="sourcelabel"> <x>316</x> <y>260</y> </hint> - <hint type="destinationlabel" > + <hint type="destinationlabel"> <x>286</x> <y>274</y> </hint> diff --git a/src/core/browser_context_adapter.cpp b/src/core/browser_context_adapter.cpp index ccb6a385d..133dd688a 100644 --- a/src/core/browser_context_adapter.cpp +++ b/src/core/browser_context_adapter.cpp @@ -37,8 +37,12 @@ #include "browser_context_adapter.h" #include "browser_context_qt.h" +#include "content_client_qt.h" #include "web_engine_context.h" #include "web_engine_visited_links_manager.h" +#include "url_request_context_getter_qt.h" + +#include "net/proxy/proxy_service.h" #include <QCoreApplication> #include <QDir> @@ -57,11 +61,22 @@ inline QString buildLocationFromStandardPath(const QString &standardPath, const } } -BrowserContextAdapter::BrowserContextAdapter(const QString &name, bool offTheRecord) - : m_name(name) - , m_offTheRecord(offTheRecord) +BrowserContextAdapter::BrowserContextAdapter(bool offTheRecord) + : m_offTheRecord(offTheRecord) + , m_browserContext(new BrowserContextQt(this)) + , m_httpCacheType(DiskHttpCache) + , m_persistentCookiesPolicy(AllowPersistentCookies) + , m_httpCacheMaxSize(0) +{ +} + +BrowserContextAdapter::BrowserContextAdapter(const QString &storageName) + : m_name(storageName) + , m_offTheRecord(false) , m_browserContext(new BrowserContextQt(this)) - , m_visitedLinksManager(new WebEngineVisitedLinksManager(this)) + , m_httpCacheType(DiskHttpCache) + , m_persistentCookiesPolicy(AllowPersistentCookies) + , m_httpCacheMaxSize(0) { } @@ -69,6 +84,24 @@ BrowserContextAdapter::~BrowserContextAdapter() { } +void BrowserContextAdapter::setStorageName(const QString &storageName) +{ + if (storageName == m_name) + return; + m_name = storageName; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateStorageSettings(); +} + +void BrowserContextAdapter::setOffTheRecord(bool offTheRecord) +{ + if (offTheRecord == m_offTheRecord) + return; + m_offTheRecord = offTheRecord; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateStorageSettings(); +} + BrowserContextQt *BrowserContextAdapter::browserContext() { return m_browserContext.data(); @@ -76,6 +109,8 @@ BrowserContextQt *BrowserContextAdapter::browserContext() WebEngineVisitedLinksManager *BrowserContextAdapter::visitedLinksManager() { + if (!m_visitedLinksManager) + m_visitedLinksManager.reset(new WebEngineVisitedLinksManager(this)); return m_visitedLinksManager.data(); } @@ -91,10 +126,124 @@ BrowserContextAdapter* BrowserContextAdapter::offTheRecordContext() QString BrowserContextAdapter::dataPath() const { - return buildLocationFromStandardPath(QStandardPaths::writableLocation(QStandardPaths::DataLocation), m_name); + if (m_offTheRecord) + return QString(); + if (!m_dataPath.isEmpty()) + return m_dataPath; + if (!m_name.isNull()) + return buildLocationFromStandardPath(QStandardPaths::writableLocation(QStandardPaths::DataLocation), m_name); + return QString(); +} + +void BrowserContextAdapter::setDataPath(const QString &path) +{ + if (m_dataPath == path) + return; + m_dataPath = path; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateStorageSettings(); } QString BrowserContextAdapter::cachePath() const { - return buildLocationFromStandardPath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), m_name); + if (m_offTheRecord) + return QString(); + if (!m_cachePath.isEmpty()) + return m_cachePath; + if (!m_name.isNull()) + return buildLocationFromStandardPath(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), m_name); + return QString(); +} + +void BrowserContextAdapter::setCachePath(const QString &path) +{ + if (m_cachePath == path) + return; + m_cachePath = path; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateHttpCache(); +} + +QString BrowserContextAdapter::cookiesPath() const +{ + if (m_offTheRecord) + return QString(); + QString basePath = dataPath(); + if (!basePath.isEmpty()) + return basePath % QDir::separator() % QLatin1String("Coookies"); + return QString(); +} + +QString BrowserContextAdapter::httpCachePath() const +{ + if (m_offTheRecord) + return QString(); + QString basePath = cachePath(); + if (!basePath.isEmpty()) + return basePath % QDir::separator() % QLatin1String("Cache"); + return QString(); +} + +QString BrowserContextAdapter::httpUserAgent() const +{ + if (m_httpUserAgent.isNull()) + return QString::fromStdString(ContentClientQt::getUserAgent()); + return m_httpUserAgent; +} + +void BrowserContextAdapter::setHttpUserAgent(const QString &userAgent) +{ + if (m_httpUserAgent == userAgent) + return; + m_httpUserAgent = userAgent; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateUserAgent(); +} + +BrowserContextAdapter::HttpCacheType BrowserContextAdapter::httpCacheType() const +{ + if (isOffTheRecord() || httpCachePath().isEmpty()) + return MemoryHttpCache; + return m_httpCacheType; +} + +void BrowserContextAdapter::setHttpCacheType(BrowserContextAdapter::HttpCacheType newhttpCacheType) +{ + BrowserContextAdapter::HttpCacheType oldCacheType = httpCacheType(); + m_httpCacheType = newhttpCacheType; + if (oldCacheType == httpCacheType()) + return; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateHttpCache(); +} + +BrowserContextAdapter::PersistentCookiesPolicy BrowserContextAdapter::persistentCookiesPolicy() const +{ + if (isOffTheRecord() || cookiesPath().isEmpty()) + return NoPersistentCookies; + return m_persistentCookiesPolicy; +} + +void BrowserContextAdapter::setPersistentCookiesPolicy(BrowserContextAdapter::PersistentCookiesPolicy newPersistentCookiesPolicy) +{ + BrowserContextAdapter::PersistentCookiesPolicy oldPolicy = persistentCookiesPolicy(); + m_persistentCookiesPolicy = newPersistentCookiesPolicy; + if (oldPolicy == persistentCookiesPolicy()) + return; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateCookieStore(); +} + +int BrowserContextAdapter::httpCacheMaxSize() const +{ + return m_httpCacheMaxSize; +} + +void BrowserContextAdapter::setHttpCacheMaxSize(int maxSize) +{ + if (m_httpCacheMaxSize == maxSize) + return; + m_httpCacheMaxSize = maxSize; + if (m_browserContext->url_request_getter_) + m_browserContext->url_request_getter_->updateHttpCache(); } diff --git a/src/core/browser_context_adapter.h b/src/core/browser_context_adapter.h index e4e046c8e..dabd8a11e 100644 --- a/src/core/browser_context_adapter.h +++ b/src/core/browser_context_adapter.h @@ -46,10 +46,11 @@ class BrowserContextQt; class WebEngineVisitedLinksManager; -// Make a QSharedData if we need to open arbitrary BrowserContextAdapter beyond the defaults. -class QWEBENGINE_EXPORT BrowserContextAdapter // : public QSharedData +class QWEBENGINE_EXPORT BrowserContextAdapter : public QSharedData { public: + explicit BrowserContextAdapter(bool offTheRecord = false); + explicit BrowserContextAdapter(const QString &storagePrefix); virtual ~BrowserContextAdapter(); static BrowserContextAdapter* defaultContext(); @@ -58,19 +59,57 @@ public: WebEngineVisitedLinksManager *visitedLinksManager(); BrowserContextQt *browserContext(); + + QString storageName() const { return m_name; } + void setStorageName(const QString &storageName); + bool isOffTheRecord() const { return m_offTheRecord; } + void setOffTheRecord(bool offTheRecord); + QString dataPath() const; + void setDataPath(const QString &path); + QString cachePath() const; + void setCachePath(const QString &path); + + QString httpCachePath() const; + QString cookiesPath() const; + + QString httpUserAgent() const; + void setHttpUserAgent(const QString &userAgent); + + // KEEP IN SYNC with API or add mapping layer + enum HttpCacheType { + MemoryHttpCache = 0, + DiskHttpCache + }; + + enum PersistentCookiesPolicy { + NoPersistentCookies = 0, + AllowPersistentCookies, + ForcePersistentCookies + }; + + HttpCacheType httpCacheType() const; + void setHttpCacheType(BrowserContextAdapter::HttpCacheType); + + PersistentCookiesPolicy persistentCookiesPolicy() const; + void setPersistentCookiesPolicy(BrowserContextAdapter::PersistentCookiesPolicy); -protected: - BrowserContextAdapter(const QString &name, bool offTheRecord = false); + int httpCacheMaxSize() const; + void setHttpCacheMaxSize(int maxSize); private: - const QString m_name; + QString m_name; bool m_offTheRecord; QScopedPointer<BrowserContextQt> m_browserContext; QScopedPointer<WebEngineVisitedLinksManager> m_visitedLinksManager; - friend class WebEngineContext; + QString m_dataPath; + QString m_cachePath; + QString m_httpUserAgent; + HttpCacheType m_httpCacheType; + PersistentCookiesPolicy m_persistentCookiesPolicy; + int m_httpCacheMaxSize; Q_DISABLE_COPY(BrowserContextAdapter) }; diff --git a/src/core/browser_context_qt.cpp b/src/core/browser_context_qt.cpp index 2484ba879..e2cc8851b 100644 --- a/src/core/browser_context_qt.cpp +++ b/src/core/browser_context_qt.cpp @@ -51,7 +51,6 @@ BrowserContextQt::BrowserContextQt(BrowserContextAdapter *adapter) : m_adapter(adapter) { - resourceContext.reset(new ResourceContextQt(this)); } BrowserContextQt::~BrowserContextQt() @@ -102,6 +101,8 @@ net::URLRequestContextGetter *BrowserContextQt::GetMediaRequestContextForStorage content::ResourceContext *BrowserContextQt::GetResourceContext() { + if (!resourceContext) + resourceContext.reset(new ResourceContextQt(this)); return resourceContext.get(); } @@ -128,7 +129,7 @@ content::PushMessagingService *BrowserContextQt::GetPushMessagingService() net::URLRequestContextGetter *BrowserContextQt::CreateRequestContext(content::ProtocolHandlerMap *protocol_handlers) { - url_request_getter_ = new URLRequestContextGetterQt(this, protocol_handlers); - static_cast<ResourceContextQt*>(resourceContext.get())->set_url_request_context_getter(url_request_getter_.get()); + url_request_getter_ = new URLRequestContextGetterQt(m_adapter, protocol_handlers); + static_cast<ResourceContextQt*>(GetResourceContext())->set_url_request_context_getter(url_request_getter_.get()); return url_request_getter_.get(); } diff --git a/src/core/browser_context_qt.h b/src/core/browser_context_qt.h index 634db6f3f..c11a234ab 100644 --- a/src/core/browser_context_qt.h +++ b/src/core/browser_context_qt.h @@ -44,6 +44,7 @@ #include "download_manager_delegate_qt.h" class BrowserContextAdapter; +class URLRequestContextGetterQt; class BrowserContextQt : public content::BrowserContext { @@ -70,9 +71,10 @@ public: private: scoped_ptr<content::ResourceContext> resourceContext; - scoped_refptr<net::URLRequestContextGetter> url_request_getter_; + scoped_refptr<URLRequestContextGetterQt> url_request_getter_; scoped_ptr<DownloadManagerDelegateQt> downloadManagerDelegate; BrowserContextAdapter *m_adapter; + friend class BrowserContextAdapter; DISALLOW_COPY_AND_ASSIGN(BrowserContextQt); }; diff --git a/src/core/url_request_context_getter_qt.cpp b/src/core/url_request_context_getter_qt.cpp index 282e5e94f..0d72845b5 100644 --- a/src/core/url_request_context_getter_qt.cpp +++ b/src/core/url_request_context_getter_qt.cpp @@ -61,141 +61,229 @@ #include "net/url_request/ftp_protocol_handler.h" #include "net/ftp/ftp_network_layer.h" -#include "browser_context_qt.h" +#include "browser_context_adapter.h" #include "content_client_qt.h" #include "network_delegate_qt.h" #include "qrc_protocol_handler_qt.h" +#include "type_conversion.h" static const char kQrcSchemeQt[] = "qrc"; using content::BrowserThread; -URLRequestContextGetterQt::URLRequestContextGetterQt(BrowserContextQt *browserContext, content::ProtocolHandlerMap *protocolHandlers) +URLRequestContextGetterQt::URLRequestContextGetterQt(BrowserContextAdapter *browserContext, content::ProtocolHandlerMap *protocolHandlers) : m_ignoreCertificateErrors(false) + , m_updateStorageSettings(false) + , m_updateCookieStore(false) + , m_updateHttpCache(false) , m_browserContext(browserContext) { std::swap(m_protocolHandlers, *protocolHandlers); - // We must create the proxy config service on the UI loop on Linux because it - // must synchronously run on the glib message loop. This will be passed to - // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). -//#ifdef Q_OS_LINUX - m_proxyConfigService.reset(net::ProxyService::CreateSystemProxyConfigService(BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO)->message_loop_proxy() - , BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE))); -//#endif + updateStorageSettings(); } net::URLRequestContext *URLRequestContextGetterQt::GetURLRequestContext() { if (!m_urlRequestContext) { - m_urlRequestContext.reset(new net::URLRequestContext()); - m_networkDelegate.reset(new NetworkDelegateQt); + m_networkDelegate.reset(new NetworkDelegateQt); m_urlRequestContext->set_network_delegate(m_networkDelegate.get()); - scoped_refptr<net::CookieStore> cookieStore; - if (m_browserContext->IsOffTheRecord()) { - cookieStore = content::CreateCookieStore(content::CookieStoreConfig(base::FilePath(), content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES, NULL, NULL)); - } else { - base::FilePath cookiesPath = m_browserContext->GetPath().Append(FILE_PATH_LITERAL("Cookies")); - cookieStore = content::CreateCookieStore(content::CookieStoreConfig(cookiesPath, content::CookieStoreConfig::PERSISTANT_SESSION_COOKIES, NULL, NULL)); - } - - m_storage.reset(new net::URLRequestContextStorage(m_urlRequestContext.get())); - m_storage->set_cookie_store(cookieStore.get()); - m_storage->set_server_bound_cert_service(new net::ServerBoundCertService( - new net::DefaultServerBoundCertStore(NULL), - base::WorkerPool::GetTaskRunner(true))); - m_storage->set_http_user_agent_settings( - new net::StaticHttpUserAgentSettings("en-us,en", ContentClientQt::getUserAgent())); - - scoped_ptr<net::HostResolver> host_resolver( - net::HostResolver::CreateDefaultResolver(NULL)); - - m_storage->set_cert_verifier(net::CertVerifier::CreateDefault()); - - m_storage->set_proxy_service(net::ProxyService::CreateUsingSystemProxyResolver(m_proxyConfigService.release(), 0, NULL)); - - m_storage->set_ssl_config_service(new net::SSLConfigServiceDefaults); - m_storage->set_transport_security_state(new net::TransportSecurityState()); - - m_storage->set_http_auth_handler_factory( - net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); - m_storage->set_http_server_properties(scoped_ptr<net::HttpServerProperties>(new net::HttpServerPropertiesImpl)); - - net::HttpCache::DefaultBackend* main_backend; - if (m_browserContext->IsOffTheRecord()) { - main_backend = - new net::HttpCache::DefaultBackend( - net::MEMORY_CACHE, - net::CACHE_BACKEND_DEFAULT, - base::FilePath(), - 0, - BrowserThread::GetMessageLoopProxyForThread( - BrowserThread::CACHE)); - } else { - base::FilePath cache_path = m_browserContext->GetCachePath().Append(FILE_PATH_LITERAL("Cache")); - main_backend = - new net::HttpCache::DefaultBackend( - net::DISK_CACHE, - net::CACHE_BACKEND_DEFAULT, - cache_path, - 0, - BrowserThread::GetMessageLoopProxyForThread( - BrowserThread::CACHE)); - } - - net::HttpNetworkSession::Params network_session_params; - network_session_params.transport_security_state = - m_urlRequestContext->transport_security_state(); - network_session_params.cert_verifier = - m_urlRequestContext->cert_verifier(); - network_session_params.server_bound_cert_service = - m_urlRequestContext->server_bound_cert_service(); - network_session_params.proxy_service = - m_urlRequestContext->proxy_service(); - network_session_params.ssl_config_service = - m_urlRequestContext->ssl_config_service(); - network_session_params.http_auth_handler_factory = - m_urlRequestContext->http_auth_handler_factory(); - network_session_params.network_delegate = - m_networkDelegate.get(); - network_session_params.http_server_properties = - m_urlRequestContext->http_server_properties(); - network_session_params.ignore_certificate_errors = - m_ignoreCertificateErrors; - - // Give |m_storage| ownership at the end in case it's |mapped_host_resolver|. - m_storage->set_host_resolver(host_resolver.Pass()); - network_session_params.host_resolver = - m_urlRequestContext->host_resolver(); - - net::HttpCache* main_cache = new net::HttpCache( - network_session_params, main_backend); - m_storage->set_http_transaction_factory(main_cache); - - - m_jobFactory.reset(new net::URLRequestJobFactoryImpl()); - - // Chromium has a few protocol handlers ready for us, only pick blob: and throw away the rest. - content::ProtocolHandlerMap::iterator it = m_protocolHandlers.find(url::kBlobScheme); - Q_ASSERT(it != m_protocolHandlers.end()); - m_jobFactory->SetProtocolHandler(it->first, it->second.release()); - m_protocolHandlers.clear(); - - m_jobFactory->SetProtocolHandler(url::kDataScheme, new net::DataProtocolHandler()); - m_jobFactory->SetProtocolHandler(url::kFileScheme, new net::FileProtocolHandler( - content::BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); - m_jobFactory->SetProtocolHandler(kQrcSchemeQt, new QrcProtocolHandlerQt()); - m_jobFactory->SetProtocolHandler(url::kFtpScheme, new net::FtpProtocolHandler( - new net::FtpNetworkLayer(m_urlRequestContext->host_resolver()))); - m_urlRequestContext->set_job_factory(m_jobFactory.get()); + generateStorage(); + generateJobFactory(); } return m_urlRequestContext.get(); } +void URLRequestContextGetterQt::updateStorageSettings() +{ + Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); + if (!m_proxyConfigService) { + // We must create the proxy config service on the UI loop on Linux because it + // must synchronously run on the glib message loop. This will be passed to + // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). + m_proxyConfigService.reset(net::ProxyService::CreateSystemProxyConfigService( + BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO)->message_loop_proxy(), + BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE)) + ); + } + if (m_storage && !m_updateStorageSettings) { + m_updateStorageSettings = true; + content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, base::Bind(&URLRequestContextGetterQt::generateStorage, this)); + } +} + +void URLRequestContextGetterQt::generateStorage() +{ + Q_ASSERT(m_urlRequestContext); + Q_ASSERT(m_proxyConfigService); + m_updateStorageSettings = false; + + m_storage.reset(new net::URLRequestContextStorage(m_urlRequestContext.get())); + + generateCookieStore(); + generateUserAgent(); + + m_storage->set_server_bound_cert_service(new net::ServerBoundCertService( + new net::DefaultServerBoundCertStore(NULL), + base::WorkerPool::GetTaskRunner(true))); + + m_storage->set_cert_verifier(net::CertVerifier::CreateDefault()); + m_storage->set_proxy_service(net::ProxyService::CreateUsingSystemProxyResolver( + m_proxyConfigService.release(), 0, NULL)); + m_storage->set_ssl_config_service(new net::SSLConfigServiceDefaults); + m_storage->set_transport_security_state(new net::TransportSecurityState()); + + scoped_ptr<net::HostResolver> host_resolver(net::HostResolver::CreateDefaultResolver(NULL)); + m_storage->set_http_auth_handler_factory(net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); + m_storage->set_http_server_properties(scoped_ptr<net::HttpServerProperties>(new net::HttpServerPropertiesImpl)); + + // Give |m_storage| ownership at the end in case it's |mapped_host_resolver|. + m_storage->set_host_resolver(host_resolver.Pass()); + + generateHttpCache(); +} + +void URLRequestContextGetterQt::updateCookieStore() +{ + if (m_urlRequestContext && !m_updateCookieStore && !m_updateStorageSettings) { + m_updateCookieStore = true; + content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, base::Bind(&URLRequestContextGetterQt::generateCookieStore, this)); + } +} + +void URLRequestContextGetterQt::generateCookieStore() +{ + Q_ASSERT(m_urlRequestContext); + Q_ASSERT(m_storage); + m_updateCookieStore = false; + + // Unset it first to get a chance to destroy and flush the old cookie store before before opening a new on possibly the same file. + m_storage->set_cookie_store(0); + + net::CookieStore* cookieStore = 0; + switch (m_browserContext->persistentCookiesPolicy()) { + case BrowserContextAdapter::NoPersistentCookies: + cookieStore = + content::CreateCookieStore(content::CookieStoreConfig( + base::FilePath(), + content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES, + NULL, NULL) + ); + break; + case BrowserContextAdapter::AllowPersistentCookies: + cookieStore = + content::CreateCookieStore(content::CookieStoreConfig( + base::FilePath(toFilePathString(m_browserContext->cookiesPath())), + content::CookieStoreConfig::PERSISTANT_SESSION_COOKIES, + NULL, NULL) + ); + break; + case BrowserContextAdapter::ForcePersistentCookies: + cookieStore = + content::CreateCookieStore(content::CookieStoreConfig( + base::FilePath(toFilePathString(m_browserContext->cookiesPath())), + content::CookieStoreConfig::RESTORED_SESSION_COOKIES, + NULL, NULL) + ); + break; + } + m_storage->set_cookie_store(cookieStore); +} + +void URLRequestContextGetterQt::updateUserAgent() +{ + if (m_urlRequestContext && !m_updateStorageSettings) + content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, base::Bind(&URLRequestContextGetterQt::generateUserAgent, this)); +} + +void URLRequestContextGetterQt::generateUserAgent() +{ + Q_ASSERT(m_urlRequestContext); + Q_ASSERT(m_storage); + + m_storage->set_http_user_agent_settings( + new net::StaticHttpUserAgentSettings("en-us,en", m_browserContext->httpUserAgent().toStdString())); +} + +void URLRequestContextGetterQt::updateHttpCache() +{ + if (m_urlRequestContext && !m_updateHttpCache && !m_updateStorageSettings) { + m_updateHttpCache = true; + content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, base::Bind(&URLRequestContextGetterQt::generateHttpCache, this)); + } +} + +void URLRequestContextGetterQt::generateHttpCache() +{ + Q_ASSERT(m_urlRequestContext); + Q_ASSERT(m_storage); + m_updateHttpCache = false; + + net::HttpCache::DefaultBackend* main_backend = 0; + switch (m_browserContext->httpCacheType()) { + case BrowserContextAdapter::MemoryHttpCache: + main_backend = + new net::HttpCache::DefaultBackend( + net::MEMORY_CACHE, + net::CACHE_BACKEND_DEFAULT, + base::FilePath(), + m_browserContext->httpCacheMaxSize(), + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE) + ); + break; + case BrowserContextAdapter::DiskHttpCache: + main_backend = + new net::HttpCache::DefaultBackend( + net::DISK_CACHE, + net::CACHE_BACKEND_DEFAULT, + base::FilePath(toFilePathString(m_browserContext->httpCachePath())), + m_browserContext->httpCacheMaxSize(), + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE) + ); + break; + } + + net::HttpNetworkSession::Params network_session_params; + network_session_params.transport_security_state = m_urlRequestContext->transport_security_state(); + network_session_params.cert_verifier = m_urlRequestContext->cert_verifier(); + network_session_params.server_bound_cert_service = m_urlRequestContext->server_bound_cert_service(); + network_session_params.proxy_service = m_urlRequestContext->proxy_service(); + network_session_params.ssl_config_service = m_urlRequestContext->ssl_config_service(); + network_session_params.http_auth_handler_factory = m_urlRequestContext->http_auth_handler_factory(); + network_session_params.network_delegate = m_networkDelegate.get(); + network_session_params.http_server_properties = m_urlRequestContext->http_server_properties(); + network_session_params.ignore_certificate_errors = m_ignoreCertificateErrors; + network_session_params.host_resolver = m_urlRequestContext->host_resolver(); + + m_storage->set_http_transaction_factory(new net::HttpCache(network_session_params, main_backend)); +} + +void URLRequestContextGetterQt::generateJobFactory() +{ + Q_ASSERT(m_urlRequestContext); + Q_ASSERT(!m_jobFactory); + m_jobFactory.reset(new net::URLRequestJobFactoryImpl()); + + // Chromium has a few protocol handlers ready for us, only pick blob: and throw away the rest. + content::ProtocolHandlerMap::iterator it = m_protocolHandlers.find(url::kBlobScheme); + Q_ASSERT(it != m_protocolHandlers.end()); + m_jobFactory->SetProtocolHandler(it->first, it->second.release()); + m_protocolHandlers.clear(); + + m_jobFactory->SetProtocolHandler(url::kDataScheme, new net::DataProtocolHandler()); + m_jobFactory->SetProtocolHandler(url::kFileScheme, new net::FileProtocolHandler( + content::BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( + base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); + m_jobFactory->SetProtocolHandler(kQrcSchemeQt, new QrcProtocolHandlerQt()); + m_jobFactory->SetProtocolHandler(url::kFtpScheme, + new net::FtpProtocolHandler(new net::FtpNetworkLayer(m_urlRequestContext->host_resolver()))); + + m_urlRequestContext->set_job_factory(m_jobFactory.get()); +} scoped_refptr<base::SingleThreadTaskRunner> URLRequestContextGetterQt::GetNetworkTaskRunner() const { diff --git a/src/core/url_request_context_getter_qt.h b/src/core/url_request_context_getter_qt.h index 2c42322bd..5f6048403 100644 --- a/src/core/url_request_context_getter_qt.h +++ b/src/core/url_request_context_getter_qt.h @@ -40,6 +40,7 @@ #include "net/url_request/url_request_context_getter.h" #include "base/files/file_path.h" +#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/single_thread_task_runner.h" #include "content/public/browser/content_browser_client.h" @@ -50,26 +51,41 @@ #include "qglobal.h" namespace net { -class HostResolver; class MappedHostResolver; class NetworkDelegate; class ProxyConfigService; } -class BrowserContextQt; +class BrowserContextAdapter; class URLRequestContextGetterQt : public net::URLRequestContextGetter { public: - explicit URLRequestContextGetterQt(BrowserContextQt *browserContext, content::ProtocolHandlerMap *protocolHandlers); + explicit URLRequestContextGetterQt(BrowserContextAdapter *browserContext, content::ProtocolHandlerMap *protocolHandlers); virtual net::URLRequestContext *GetURLRequestContext() Q_DECL_OVERRIDE; virtual scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const Q_DECL_OVERRIDE; + // Called on the UI thread: + void updateStorageSettings(); + void updateUserAgent(); + void updateCookieStore(); + void updateHttpCache(); + private: virtual ~URLRequestContextGetterQt() {} + // Called on the IO thread: + void generateStorage(); + void generateCookieStore(); + void generateHttpCache(); + void generateUserAgent(); + void generateJobFactory(); + bool m_ignoreCertificateErrors; - BrowserContextQt *m_browserContext; + volatile bool m_updateStorageSettings; + volatile bool m_updateCookieStore; + volatile bool m_updateHttpCache; + BrowserContextAdapter *m_browserContext; content::ProtocolHandlerMap m_protocolHandlers; scoped_ptr<net::ProxyConfigService> m_proxyConfigService; diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index 678b558c1..0b1aba1cf 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -347,10 +347,13 @@ void WebContentsAdapter::initialize(WebContentsAdapterClient *adapterClient) { Q_D(WebContentsAdapter); d->adapterClient = adapterClient; + // We keep a reference to browserContextAdapter to keep it alive as long as we use it. + // This is needed in case the QML WebEngineProfile is garbage collected before the WebEnginePage. + d->browserContextAdapter = adapterClient->browserContextAdapter(); // Create our own if a WebContents wasn't provided at construction. if (!d->webContents) - d->webContents.reset(createBlankWebContents(adapterClient, adapterClient->browserContextAdapter()->browserContext())); + d->webContents.reset(createBlankWebContents(adapterClient, d->browserContextAdapter->browserContext())); // This might replace any adapter that has been initialized with this WebEngineSettings. adapterClient->webEngineSettings()->setWebContentsAdapter(this); @@ -631,7 +634,7 @@ void WebContentsAdapter::enableInspector(bool enable) BrowserContextQt* WebContentsAdapter::browserContext() { Q_D(WebContentsAdapter); - return static_cast<BrowserContextQt*>(d->webContents->GetBrowserContext()); + return d->browserContextAdapter->browserContext(); } QAccessibleInterface *WebContentsAdapter::browserAccessible() diff --git a/src/core/web_contents_adapter_p.h b/src/core/web_contents_adapter_p.h index 28df0113a..f47c05de0 100644 --- a/src/core/web_contents_adapter_p.h +++ b/src/core/web_contents_adapter_p.h @@ -42,6 +42,9 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include <QExplicitlySharedDataPointer> + +class BrowserContextAdapter; class QtRenderViewObserverHost; class WebContentsAdapterClient; class WebContentsDelegateQt; @@ -52,6 +55,7 @@ public: WebContentsAdapterPrivate(); ~WebContentsAdapterPrivate(); scoped_refptr<WebEngineContext> engineContext; + QExplicitlySharedDataPointer<BrowserContextAdapter> browserContextAdapter; scoped_ptr<content::WebContents> webContents; scoped_ptr<WebContentsDelegateQt> webContentsDelegate; scoped_ptr<QtRenderViewObserverHost> renderViewObserverHost; diff --git a/src/core/web_engine_context.cpp b/src/core/web_engine_context.cpp index b17ee124c..6f335d2fc 100644 --- a/src/core/web_engine_context.cpp +++ b/src/core/web_engine_context.cpp @@ -142,15 +142,15 @@ scoped_refptr<WebEngineContext> WebEngineContext::current() BrowserContextAdapter* WebEngineContext::defaultBrowserContext() { if (!m_defaultBrowserContext) - m_defaultBrowserContext.reset(new BrowserContextAdapter(QStringLiteral("Default"), false)); - return m_defaultBrowserContext.get(); + m_defaultBrowserContext = new BrowserContextAdapter(QStringLiteral("Default")); + return m_defaultBrowserContext.data(); } BrowserContextAdapter* WebEngineContext::offTheRecordBrowserContext() { if (!m_offTheRecordBrowserContext) - m_offTheRecordBrowserContext.reset(new BrowserContextAdapter(QStringLiteral("OTR"), true)); - return m_offTheRecordBrowserContext.get(); + m_offTheRecordBrowserContext = new BrowserContextAdapter(true); + return m_offTheRecordBrowserContext.data(); } #ifndef CHROMIUM_VERSION diff --git a/src/core/web_engine_context.h b/src/core/web_engine_context.h index 0b3ad8678..a44a48d3a 100644 --- a/src/core/web_engine_context.h +++ b/src/core/web_engine_context.h @@ -40,6 +40,8 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include <QExplicitlySharedDataPointer> + namespace base { class RunLoop; } @@ -72,8 +74,8 @@ private: #if defined(OS_ANDROID) scoped_ptr<SurfaceFactoryQt> m_surfaceFactory; #endif - scoped_ptr<BrowserContextAdapter> m_defaultBrowserContext; - scoped_ptr<BrowserContextAdapter> m_offTheRecordBrowserContext; + QExplicitlySharedDataPointer<BrowserContextAdapter> m_defaultBrowserContext; + QExplicitlySharedDataPointer<BrowserContextAdapter> m_offTheRecordBrowserContext; }; #endif // WEB_ENGINE_CONTEXT_H diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp index 3f5fd53b5..f5994d304 100644 --- a/src/webenginewidgets/api/qwebenginepage.cpp +++ b/src/webenginewidgets/api/qwebenginepage.cpp @@ -28,6 +28,8 @@ #include "javascript_dialog_controller.h" #include "qwebenginehistory.h" #include "qwebenginehistory_p.h" +#include "qwebengineprofile.h" +#include "qwebengineprofile_p.h" #include "qwebenginesettings.h" #include "qwebenginesettings_p.h" #include "qwebengineview.h" @@ -167,9 +169,10 @@ void CallbackDirectory::CallbackSharedDataPointer::doDeref() } } -QWebEnginePagePrivate::QWebEnginePagePrivate() +QWebEnginePagePrivate::QWebEnginePagePrivate(QWebEngineProfile *_profile) : adapter(new WebContentsAdapter) , history(new QWebEngineHistory(new QWebEngineHistoryPrivate(this))) + , profile(_profile ? _profile : QWebEngineProfile::defaultProfile()) , settings(new QWebEngineSettings) , view(0) , isLoading(false) @@ -419,12 +422,29 @@ void QWebEnginePagePrivate::recreateFromSerializedHistory(QDataStream &input) BrowserContextAdapter *QWebEnginePagePrivate::browserContextAdapter() { - return BrowserContextAdapter::defaultContext(); + return profile->d_ptr->browserContext(); } QWebEnginePage::QWebEnginePage(QObject* parent) : QObject(parent) - , d_ptr(new QWebEnginePagePrivate) + , d_ptr(new QWebEnginePagePrivate()) +{ + Q_D(QWebEnginePage); + d->q_ptr = this; + d->adapter->initialize(d); +} + +/*! + Constructs an empty QWebEnginePage in the QWebEngineProfile \a profile with parent \a parent. + + If the profile is not the default profile the caller must ensure the profile is alive for as + long as the page is. + + \since 5.5 +*/ +QWebEnginePage::QWebEnginePage(QWebEngineProfile *profile, QObject* parent) + : QObject(parent) + , d_ptr(new QWebEnginePagePrivate(profile)) { Q_D(QWebEnginePage); d->q_ptr = this; @@ -460,6 +480,16 @@ QWidget *QWebEnginePage::view() const return d->view; } +/*! + Returns the QWebEngineProfile the page belongs to. + \since 5.5 +*/ +QWebEngineProfile *QWebEnginePage::profile() const +{ + Q_D(const QWebEnginePage); + return d->profile; +} + bool QWebEnginePage::hasSelection() const { return !selectedText().isEmpty(); diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h index afb62ceda..e70b90d10 100644 --- a/src/webenginewidgets/api/qwebenginepage.h +++ b/src/webenginewidgets/api/qwebenginepage.h @@ -50,8 +50,9 @@ QT_BEGIN_NAMESPACE class QMenu; class QWebEngineHistory; class QWebEnginePage; -class QWebEngineSettings; class QWebEnginePagePrivate; +class QWebEngineProfile; +class QWebEngineSettings; namespace QtWebEnginePrivate { @@ -171,6 +172,7 @@ public: }; explicit QWebEnginePage(QObject *parent = 0); + QWebEnginePage(QWebEngineProfile *profile, QObject *parent = 0); ~QWebEnginePage(); QWebEngineHistory *history() const; @@ -180,6 +182,8 @@ public: bool hasSelection() const; QString selectedText() const; + QWebEngineProfile *profile() const; + #ifndef QT_NO_ACTION QAction *action(WebAction action) const; #endif diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h index 23d577c94..2f44bc732 100644 --- a/src/webenginewidgets/api/qwebenginepage_p.h +++ b/src/webenginewidgets/api/qwebenginepage_p.h @@ -49,6 +49,7 @@ class WebContentsAdapter; QT_BEGIN_NAMESPACE class QWebEngineHistory; class QWebEnginePage; +class QWebEngineProfile; class QWebEngineSettings; class QWebEngineView; @@ -101,7 +102,7 @@ public: Q_DECLARE_PUBLIC(QWebEnginePage) QWebEnginePage *q_ptr; - QWebEnginePagePrivate(); + QWebEnginePagePrivate(QWebEngineProfile *profile = 0); ~QWebEnginePagePrivate(); virtual RenderWidgetHostViewQtDelegate* CreateRenderWidgetHostViewQtDelegate(RenderWidgetHostViewQtDelegateClient *client) Q_DECL_OVERRIDE; @@ -151,6 +152,7 @@ public: QExplicitlySharedDataPointer<WebContentsAdapter> adapter; QWebEngineHistory *history; + QWebEngineProfile *profile; QWebEngineSettings *settings; QWebEngineView *view; QSize viewportSize; diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp new file mode 100644 index 000000000..1487f06ce --- /dev/null +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -0,0 +1,320 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwebengineprofile.h" + +#include "qwebenginepage.h" +#include "qwebengineprofile_p.h" + +#include "browser_context_adapter.h" + +QT_BEGIN_NAMESPACE + +/*! + \class QWebEngineProfile + \brief The QWebEngineProfile class provides a web-engine profile shared by multiple pages. + \since 5.5 + + \inmodule QtWebEngineWidgets + + QWebEngineProfile contains settings and history shared by all QWebEnginePages that belong + to the profile. + + A default profile is built-in that all web pages not specifically created with another profile + belongs to. +*/ + +/*! + \enum QWebEngineProfile::HttpCacheType + + This enum describes the HTTP cache types QtWebEngine can be configured to use. + + \value MemoryHttpCache Use a in-memory cache. This is the only setting possible if off-the-record is set or no cache path is available. + \value DiskHttpCache Use a disk cache. This is the default. +*/ + +/*! + \enum QWebEngineProfile::PersistentCookiesPolicy + + This enum describes policy for cookie persistency. + + \value NoPersistentCookies Both session and persistent cookies are stored in memory. This is the only setting possible if off-the-record is set or no persistent data path is available. + \value AllowPersistentCookies Cookies marked persistent are save and restored from disk, session cookies are only stored to disk for crash recovery. This is the default setting. + \value ForcePersistentCookies Both session and persistent cookies are save and restored from disk. +*/ + +QWebEngineProfilePrivate::QWebEngineProfilePrivate(BrowserContextAdapter* browserContext, bool ownsContext) + : m_browserContext(browserContext) +{ + if (ownsContext) + m_browserContextRef = browserContext; +} + +QWebEngineProfilePrivate::~QWebEngineProfilePrivate() +{ +} + +/*! + Constructs a new off-the-record profile. + + An off-the-record profile leaves no record on the local machine, and has no persistent data or cache. + Thus, the HTTP cache can only be in memory and the cookies only be non-persistent, trying to change + these settings will have no effect. + + \sa isOffTheRecord() +*/ +QWebEngineProfile::QWebEngineProfile(QObject *parent) + : QObject(parent) + , d_ptr(new QWebEngineProfilePrivate(new BrowserContextAdapter(true), true)) +{ +} + +/*! + Constructs a new profile with storage name \a storageName. + + The storage name must be unique. + + A disk-based QWebEngineProfile should be destroyed on or before application exit, otherwise the cache + and persistent data may not be fully flushed to disk. + + \sa storageName() +*/ +QWebEngineProfile::QWebEngineProfile(const QString &storageName, QObject *parent) + : QObject(parent) + , d_ptr(new QWebEngineProfilePrivate(new BrowserContextAdapter(storageName), true)) +{ +} + +/*! \internal +*/ +QWebEngineProfile::QWebEngineProfile(QWebEngineProfilePrivate *privatePtr) + : d_ptr(privatePtr) +{ +} + +/*! \internal +*/ +QWebEngineProfile::~QWebEngineProfile() +{ +} + +/*! + Returns the storage name for the profile. + + The storage name is used to give each profile that uses the disk separate subdirectories for persistent data and cache. +*/ +QString QWebEngineProfile::storageName() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->storageName(); +} + +/*! + Returns true if this is an off-the-record profile that leaves no record on the computer. + + This will force cookies and HTTP cache to be in memory, but also force all other normally + persistent data to be stored in memory. +*/ +bool QWebEngineProfile::isOffTheRecord() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->isOffTheRecord(); +} + +/*! + Returns the path used to store persistent data for the browser and web content. + + Persistent data includes persistent cookies, HTML5 local storage and visited links. + + By default this is below QStandardPaths::writableLocation(QStandardPaths::DataLocation) in a storage name specific directory. + + \sa setPersistentStoragePath(), storageName(), QStandardPaths::writableLocation() +*/ +QString QWebEngineProfile::persistentStoragePath() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->dataPath(); +} + +/*! + Overrides the default path used to store persistent web engine data. + + If set to the null string, the default path is restored. + + \sa persistentStoragePath() +*/ +void QWebEngineProfile::setPersistentStoragePath(const QString &path) +{ + const Q_D(QWebEngineProfile); + d->browserContext()->setDataPath(path); +} + +/*! + Returns the path used for caches. + + By default this is below QStandardPaths::writableLocation(QStandardPaths::CacheLocation) in a storage name specific directory. + + \sa setCachePath(), storageName(), QStandardPaths::writableLocation() +*/ +QString QWebEngineProfile::cachePath() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->cachePath(); +} + +/*! + Overrides the default path used for disk caches. + + If set to the null string, the default path is restored. + + \sa cachePath() +*/ +void QWebEngineProfile::setCachePath(const QString &path) +{ + Q_D(QWebEngineProfile); + d->browserContext()->setCachePath(path); +} + +/*! + Returns the user-agent string send with HTTP to identify the browser. + + \sa setHttpUserAgent() +*/ +QString QWebEngineProfile::httpUserAgent() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->httpUserAgent(); +} + +/*! + Overrides the default user-agent string, setting it to \a userAgent. + + \sa httpUserAgent() +*/ +void QWebEngineProfile::setHttpUserAgent(const QString &userAgent) +{ + Q_D(QWebEngineProfile); + d->browserContext()->setHttpUserAgent(userAgent); +} + +/*! + Returns the type of HTTP cache used. + + If the profile is off-the-record MemoryHttpCache is returned. + + \sa setHttpCacheType(), cachePath() +*/ +QWebEngineProfile::HttpCacheType QWebEngineProfile::httpCacheType() const +{ + const Q_D(QWebEngineProfile); + return QWebEngineProfile::HttpCacheType(d->browserContext()->httpCacheType()); +} + +/*! + Sets the HTTP cache type to \a httpCacheType. + + \sa httpCacheType(), setCachePath() +*/ +void QWebEngineProfile::setHttpCacheType(QWebEngineProfile::HttpCacheType httpCacheType) +{ + Q_D(QWebEngineProfile); + d->browserContext()->setHttpCacheType(BrowserContextAdapter::HttpCacheType(httpCacheType)); +} + +/*! + Returns the current policy for persistent cookies. + + If the profile is off-the-record NoPersistentCookies is returned. + + \sa setPersistentCookiesPolicy() +*/ +QWebEngineProfile::PersistentCookiesPolicy QWebEngineProfile::persistentCookiesPolicy() const +{ + const Q_D(QWebEngineProfile); + return QWebEngineProfile::PersistentCookiesPolicy(d->browserContext()->persistentCookiesPolicy()); +} + +/*! + Sets the policy for persistent cookies to \a newPersistentCookiesPolicy. + + \sa persistentCookiesPolicy() +*/ +void QWebEngineProfile::setPersistentCookiesPolicy(QWebEngineProfile::PersistentCookiesPolicy newPersistentCookiesPolicy) +{ + Q_D(QWebEngineProfile); + d->browserContext()->setPersistentCookiesPolicy(BrowserContextAdapter::PersistentCookiesPolicy(newPersistentCookiesPolicy)); +} + +/*! + Returns the maximum size of the HTTP size. + + Will return 0 if the size is automatically controlled by QtWebEngine. + + \sa setHttpCacheMaximumSize(), httpCacheType() +*/ +int QWebEngineProfile::httpCacheMaximumSize() const +{ + const Q_D(QWebEngineProfile); + return d->browserContext()->httpCacheMaxSize(); +} + +/*! + Sets the maximum size of the HTTP cache to \a maxSize. + + Setting it to 0 means the size will be controlled automatically by QtWebEngine. + + \sa httpCacheMaximumSize(), setHttpCacheType() +*/ +void QWebEngineProfile::setHttpCacheMaximumSize(int maxSize) +{ + Q_D(QWebEngineProfile); + d->browserContext()->setHttpCacheMaxSize(maxSize); +} + +/*! + Returns the default profile. + + The default profile uses the storage name "Default". + + \sa storageName() +*/ +QWebEngineProfile *QWebEngineProfile::defaultProfile() +{ + static QWebEngineProfile profile(new QWebEngineProfilePrivate(BrowserContextAdapter::defaultContext(), false)); + return &profile; +} + +QT_END_NAMESPACE diff --git a/src/webenginewidgets/api/qwebengineprofile.h b/src/webenginewidgets/api/qwebengineprofile.h new file mode 100644 index 000000000..d21eed372 --- /dev/null +++ b/src/webenginewidgets/api/qwebengineprofile.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWEBENGINEPROFILE_H +#define QWEBENGINEPROFILE_H + +#include "qtwebenginewidgetsglobal.h" + +#include <QObject> +#include <QScopedPointer> +#include <QString> + +QT_BEGIN_NAMESPACE + +class QObject; +class QWebEnginePage; +class QWebEnginePagePrivate; +class QWebEngineProfilePrivate; + +class QWEBENGINEWIDGETS_EXPORT QWebEngineProfile : public QObject { + Q_OBJECT +public: + explicit QWebEngineProfile(QObject *parent = 0); + explicit QWebEngineProfile(const QString &name, QObject *parent = 0); + virtual ~QWebEngineProfile(); + + enum HttpCacheType { + MemoryHttpCache, + DiskHttpCache + }; + + enum PersistentCookiesPolicy { + NoPersistentCookies, + AllowPersistentCookies, + ForcePersistentCookies + }; + + QString storageName() const; + bool isOffTheRecord() const; + + QString persistentStoragePath() const; + void setPersistentStoragePath(const QString &path); + + QString cachePath() const; + void setCachePath(const QString &path); + + QString httpUserAgent() const; + void setHttpUserAgent(const QString &userAgent); + + HttpCacheType httpCacheType() const; + void setHttpCacheType(QWebEngineProfile::HttpCacheType); + + PersistentCookiesPolicy persistentCookiesPolicy() const; + void setPersistentCookiesPolicy(QWebEngineProfile::PersistentCookiesPolicy); + + int httpCacheMaximumSize() const; + void setHttpCacheMaximumSize(int maxSize); + + static QWebEngineProfile *defaultProfile(); + +private: + Q_DECLARE_PRIVATE(QWebEngineProfile); + QWebEngineProfile(QWebEngineProfilePrivate *); + + friend class QWebEnginePagePrivate; + QScopedPointer<QWebEngineProfilePrivate> d_ptr; +}; + +QT_END_NAMESPACE + +#endif // QWEBENGINEPROFILE_H diff --git a/src/webenginewidgets/api/qwebengineprofile_p.h b/src/webenginewidgets/api/qwebengineprofile_p.h new file mode 100644 index 000000000..3a08b6b1e --- /dev/null +++ b/src/webenginewidgets/api/qwebengineprofile_p.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWEBENGINEPROFILE_P_H +#define QWEBENGINEPROFILE_P_H + +class BrowserContextAdapter; + +QT_BEGIN_NAMESPACE + +class QWebEngineSettings; + +class QWebEngineProfilePrivate { +public: + QWebEngineProfilePrivate(BrowserContextAdapter* browserContext, bool ownsContext); + ~QWebEngineProfilePrivate(); + + BrowserContextAdapter *browserContext() const { return m_browserContext; } + +private: + BrowserContextAdapter *m_browserContext; + QExplicitlySharedDataPointer<BrowserContextAdapter> m_browserContextRef; +}; + +QT_END_NAMESPACE + +#endif // QWEBENGINEPROFILE_P_H diff --git a/src/webenginewidgets/webenginewidgets.pro b/src/webenginewidgets/webenginewidgets.pro index 296b62475..aabcc710d 100644 --- a/src/webenginewidgets/webenginewidgets.pro +++ b/src/webenginewidgets/webenginewidgets.pro @@ -17,8 +17,9 @@ SOURCES = \ api/qwebenginecertificateerror.cpp \ api/qwebenginehistory.cpp \ api/qwebenginepage.cpp \ + api/qwebengineprofile.cpp \ api/qwebenginesettings.cpp \ - api/qwebengineview.cpp\ + api/qwebengineview.cpp \ render_widget_host_view_qt_delegate_widget.cpp HEADERS = \ @@ -27,6 +28,8 @@ HEADERS = \ api/qwebenginehistory.h \ api/qwebenginepage.h \ api/qwebenginepage_p.h \ + api/qwebengineprofile.h \ + api/qwebengineprofile_p.h \ api/qwebenginesettings.h \ api/qwebenginesettings_p.h \ api/qwebengineview.h \ |