From 8a4091c2104913294b4dc7b06c8e9fb2c022f51e Mon Sep 17 00:00:00 2001
From: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Date: Tue, 16 Aug 2016 14:51:33 +0200
Subject: [PATCH] Fix crash when trying to navigate in new window but is
 blocked

Ensure that if QWebEnginePage::createWindow returns 'this' that we fall
back to navigating in current tab.

Change-Id: Idffe25dcafaaf3c824815b3cf1f0e400eaec2923
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
---
 src/core/web_contents_delegate_qt.cpp       |  1 +
 src/webenginewidgets/api/qwebenginepage.cpp | 11 ++++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/core/web_contents_delegate_qt.cpp b/src/core/web_contents_delegate_qt.cpp
index 97f0e515d..5e9157069 100644
--- a/src/core/web_contents_delegate_qt.cpp
+++ b/src/core/web_contents_delegate_qt.cpp
@@ -97,6 +97,7 @@ content::WebContents *WebContentsDelegateQt::OpenURLFromTab(content::WebContents
         if (targetAdapter)
             target = targetAdapter->webContents();
     }
+    Q_ASSERT(target);
 
     content::NavigationController::LoadURLParams load_url_params(params.url);
     load_url_params.referrer = params.referrer;
diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp
index dbbac1aed..f10833b9b 100644
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -279,9 +279,14 @@ void QWebEnginePagePrivate::adoptNewWindow(QSharedPointer<WebContentsAdapter> ne
     if (newPage->d_func() == this) {
         // If createWindow returns /this/ we must delay the adoption.
         Q_ASSERT(q == newPage);
-        QTimer::singleShot(0, q, [this, newPage, newWebContents, initialGeometry] () {
-            adoptNewWindowImpl(newPage, newWebContents, initialGeometry);
-        });
+        // WebContents might be null if we just opened a new page for navigation, in that case
+        // avoid referencing newWebContents so that it is deleted and WebContentsDelegateQt::OpenURLFromTab
+        // will fall back to navigating current page.
+        if (newWebContents->webContents()) {
+            QTimer::singleShot(0, q, [this, newPage, newWebContents, initialGeometry] () {
+                adoptNewWindowImpl(newPage, newWebContents, initialGeometry);
+            });
+        }
     } else {
         adoptNewWindowImpl(newPage, newWebContents, initialGeometry);
     }
-- 
GitLab