summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2019-06-07 11:14:00 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2019-06-07 14:15:07 +0200
commit3b04c83620a465a4f8876413c37696fbbc3a23d6 (patch)
tree7b55507c6dc8e88819ee229764e5547f759cfd74
parent47e69dd008dd7e55dc035063dc7293a19971a0bb (diff)
Set custom user-agent manually on new windows
Chromium forgets to set ShouldOverrideUserAgentInNewTabs(), so we need to manually set the override. Fixes: QTBUG-76249 Change-Id: Id240ee525dacec3cd8389aca058a61d3af62b00a Reviewed-by: Michal Klocek <michal.klocek@qt.io>
-rw-r--r--src/core/web_contents_delegate_qt.cpp5
-rw-r--r--tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp65
2 files changed, 70 insertions, 0 deletions
diff --git a/src/core/web_contents_delegate_qt.cpp b/src/core/web_contents_delegate_qt.cpp
index 95a49f40a..83f68b68d 100644
--- a/src/core/web_contents_delegate_qt.cpp
+++ b/src/core/web_contents_delegate_qt.cpp
@@ -225,6 +225,11 @@ void WebContentsDelegateQt::AddNewContents(content::WebContents* source, std::un
{
Q_UNUSED(source)
QSharedPointer<WebContentsAdapter> newAdapter = createWindow(std::move(new_contents), disposition, initial_pos, user_gesture);
+ // Chromium can forget to pass user-agent override settings to new windows (see QTBUG-61774 and QTBUG-76249),
+ // so set it here. Note the actual value doesn't really matter here. Only the second value does, but we try
+ // to give the correct user-agent anyway.
+ if (newAdapter)
+ newAdapter->webContents()->SetUserAgentOverride(newAdapter->profileAdapter()->httpUserAgent().toStdString(), true);
if (newAdapter && !newAdapter->isInitialized())
newAdapter->loadDefault();
if (was_blocked)
diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
index d73c8e80a..bfe94ba4e 100644
--- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
+++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp
@@ -231,6 +231,8 @@ private Q_SLOTS:
void editActionsWithInitialFocus();
void editActionsWithFocusOnIframe();
+ void customUserAgentInNewTab();
+
private:
static QPoint elementCenter(QWebEnginePage *page, const QString &id);
@@ -4594,6 +4596,69 @@ void tst_QWebEnginePage::editActionsWithFocusOnIframe()
QCOMPARE(page->selectedText(), QStringLiteral("inner"));
}
+void tst_QWebEnginePage::customUserAgentInNewTab()
+{
+ HttpServer server;
+ QByteArray lastUserAgent;
+ connect(&server, &HttpServer::newRequest, [&](HttpReqRep *rr) {
+ QCOMPARE(rr->requestMethod(), "GET");
+ lastUserAgent = rr->requestHeader("user-agent");
+ rr->setResponseBody(QByteArrayLiteral("<html><body>Test</body></html>"));
+ rr->sendResponse();
+ });
+ QVERIFY(server.start());
+
+ class Page : public QWebEnginePage {
+ public:
+ QWebEngineProfile *targetProfile = nullptr;
+ QScopedPointer<QWebEnginePage> newPage;
+ Page(QWebEngineProfile *profile) : QWebEnginePage(profile) {}
+ private:
+ QWebEnginePage *createWindow(WebWindowType) override
+ {
+ newPage.reset(new QWebEnginePage(targetProfile ? targetProfile : profile(), nullptr));
+ return newPage.data();
+ }
+ };
+ QWebEngineProfile profile1, profile2;
+ profile1.setHttpUserAgent(QStringLiteral("custom 1"));
+ profile2.setHttpUserAgent(QStringLiteral("custom 2"));
+ Page page(&profile1);
+ QWebEngineView view;
+ view.resize(500, 500);
+ view.setPage(&page);
+ view.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&view));
+ QSignalSpy spy(&page, &QWebEnginePage::loadFinished);
+
+ // First check we can get the user-agent passed through normally
+ page.setHtml(QString("<html><body><a id='link' target='_blank' href='") +
+ server.url("/test1").toEncoded() +
+ QString("'>link</a></body></html>"));
+ QTRY_COMPARE(spy.count(), 1);
+ QVERIFY(spy.takeFirst().value(0).toBool());
+ QCOMPARE(evaluateJavaScriptSync(&page, QStringLiteral("navigator.userAgent")).toString(), profile1.httpUserAgent());
+ QTest::mouseClick(view.focusProxy(), Qt::LeftButton, 0, elementCenter(&page, "link"));
+ QTRY_VERIFY(page.newPage);
+ QTRY_VERIFY(!lastUserAgent.isEmpty());
+ QCOMPARE(lastUserAgent, profile1.httpUserAgent().toUtf8());
+
+ // Now check we can get the new user-agent of the profile
+ page.newPage.reset();
+ page.targetProfile = &profile2;
+ spy.clear();
+ lastUserAgent = { };
+ page.setHtml(QString("<html><body><a id='link' target='_blank' href='") +
+ server.url("/test2").toEncoded() +
+ QString("'>link</a></body></html>"));
+ QTRY_COMPARE(spy.count(), 1);
+ QVERIFY(spy.takeFirst().value(0).toBool());
+ QTest::mouseClick(view.focusProxy(), Qt::LeftButton, 0, elementCenter(&page, "link"));
+ QTRY_VERIFY(page.newPage);
+ QTRY_VERIFY(!lastUserAgent.isEmpty());
+ QCOMPARE(lastUserAgent, profile2.httpUserAgent().toUtf8());
+}
+
static QByteArrayList params = {QByteArrayLiteral("--use-fake-device-for-media-stream")};
W_QTEST_MAIN(tst_QWebEnginePage, params)