From bf4064cda667ca64b323223d3adada43f5c8413e Mon Sep 17 00:00:00 2001
From: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Date: Wed, 13 Aug 2014 16:52:51 +0200
Subject: [PATCH] Handle new window/tab modifiers also on normal navigations

AddNewContents is only called when window.open is called from JavaScript.

We also want the shift/ctrl modifiers to trigger normal links to open
in the requested disposition. In this case OpenURLFromTab will pass
the detected disposition and we must create a new WebContents
ourselves.

Use the same code path going through
WebContentsAdapterClient::adoptNewWindow except that we pass a null
WebContents pointer to the WebContentsAdapter constructor and let it
create its own when initialized.

Change-Id: I817b0e72aec12723bf92d9b7ad85c1cecbf5e408
Reviewed-by: Andras Becsi <andras.becsi@digia.com>
---
 examples/webenginewidgets/browser/webview.cpp |  2 -
 src/core/core_gyp_generator.pro               |  1 +
 src/core/web_contents_adapter.cpp             | 18 ++---
 src/core/web_contents_adapter.h               |  1 +
 src/core/web_contents_adapter_p.h             | 67 +++++++++++++++++++
 src/core/web_contents_delegate_qt.cpp         | 46 ++++++++-----
 src/core/web_contents_delegate_qt.h           |  2 +
 7 files changed, 105 insertions(+), 32 deletions(-)
 create mode 100644 src/core/web_contents_adapter_p.h

diff --git a/examples/webenginewidgets/browser/webview.cpp b/examples/webenginewidgets/browser/webview.cpp
index d4dd2649c..e32ab71d6 100644
--- a/examples/webenginewidgets/browser/webview.cpp
+++ b/examples/webenginewidgets/browser/webview.cpp
@@ -177,8 +177,6 @@ private:
 
 QWebEnginePage *WebPage::createWindow(QWebEnginePage::WebWindowType type)
 {
-    if (m_keyboardModifiers & Qt::ControlModifier || m_pressedButtons == Qt::MidButton)
-        m_openInNewTab = true;
     if (m_openInNewTab || type == QWebEnginePage::WebBrowserTab) {
         m_openInNewTab = false;
         return mainWindow()->tabWidget()->newTab()->page();
diff --git a/src/core/core_gyp_generator.pro b/src/core/core_gyp_generator.pro
index 2c3767ad4..b10626000 100644
--- a/src/core/core_gyp_generator.pro
+++ b/src/core/core_gyp_generator.pro
@@ -119,6 +119,7 @@ HEADERS = \
         url_request_qrc_job_qt.h \
         web_contents_adapter.h \
         web_contents_adapter_client.h \
+        web_contents_adapter_p.h \
         web_contents_delegate_qt.h \
         web_contents_view_qt.h \
         web_engine_context.h \
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index c765c31ab..d2eb076d6 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -44,6 +44,7 @@
 // found in the LICENSE file.
 
 #include "web_contents_adapter.h"
+#include "web_contents_adapter_p.h"
 
 #include "browser_context_qt.h"
 #include "content_browser_client_qt.h"
@@ -52,7 +53,6 @@
 #include "qt_render_view_observer_host.h"
 #include "type_conversion.h"
 #include "web_contents_adapter_client.h"
-#include "web_contents_delegate_qt.h"
 #include "web_contents_view_qt.h"
 #include "web_engine_context.h"
 #include "web_engine_settings.h"
@@ -297,18 +297,6 @@ void deserializeNavigationHistory(QDataStream &input, int *currentIndex, std::ve
     }
 }
 
-class WebContentsAdapterPrivate {
-public:
-    WebContentsAdapterPrivate();
-    scoped_refptr<WebEngineContext> engineContext;
-    scoped_ptr<content::WebContents> webContents;
-    scoped_ptr<WebContentsDelegateQt> webContentsDelegate;
-    scoped_ptr<QtRenderViewObserverHost> renderViewObserverHost;
-    WebContentsAdapterClient *adapterClient;
-    quint64 nextRequestId;
-    int lastFindRequestId;
-};
-
 WebContentsAdapterPrivate::WebContentsAdapterPrivate()
     // This has to be the first thing we create, and the last we destroy.
     : engineContext(WebEngineContext::current())
@@ -317,6 +305,10 @@ WebContentsAdapterPrivate::WebContentsAdapterPrivate()
 {
 }
 
+WebContentsAdapterPrivate::~WebContentsAdapterPrivate()
+{
+}
+
 QExplicitlySharedDataPointer<WebContentsAdapter> WebContentsAdapter::createFromSerializedNavigationHistory(QDataStream &input, WebContentsAdapterClient *adapterClient)
 {
     int currentIndex;
diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h
index 155c54024..ee8ed0aea 100644
--- a/src/core/web_contents_adapter.h
+++ b/src/core/web_contents_adapter.h
@@ -120,5 +120,6 @@ private:
     Q_DISABLE_COPY(WebContentsAdapter);
     Q_DECLARE_PRIVATE(WebContentsAdapter);
     QScopedPointer<WebContentsAdapterPrivate> d_ptr;
+    friend class WebContentsDelegateQt;
 };
 #endif // WEB_CONTENTS_ADAPTER_H
diff --git a/src/core/web_contents_adapter_p.h b/src/core/web_contents_adapter_p.h
new file mode 100644
index 000000000..143a76a44
--- /dev/null
+++ b/src/core/web_contents_adapter_p.h
@@ -0,0 +1,67 @@
+/****************************************************************************
+**
+** 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 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 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 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#ifndef WEB_CONTENTS_ADAPTER_P_H
+#define WEB_CONTENTS_ADAPTER_P_H
+
+#include "web_contents_adapter.h"
+
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+
+class QtRenderViewObserverHost;
+class WebContentsAdapterClient;
+class WebContentsDelegateQt;
+class WebEngineContext;
+
+class WebContentsAdapterPrivate {
+public:
+    WebContentsAdapterPrivate();
+    ~WebContentsAdapterPrivate();
+    scoped_refptr<WebEngineContext> engineContext;
+    scoped_ptr<content::WebContents> webContents;
+    scoped_ptr<WebContentsDelegateQt> webContentsDelegate;
+    scoped_ptr<QtRenderViewObserverHost> renderViewObserverHost;
+    WebContentsAdapterClient *adapterClient;
+    quint64 nextRequestId;
+    int lastFindRequestId;
+};
+
+#endif // WEB_CONTENTS_ADAPTER_P_H
diff --git a/src/core/web_contents_delegate_qt.cpp b/src/core/web_contents_delegate_qt.cpp
index 08f11cf5c..77d50140a 100644
--- a/src/core/web_contents_delegate_qt.cpp
+++ b/src/core/web_contents_delegate_qt.cpp
@@ -47,8 +47,8 @@
 
 #include "media_capture_devices_dispatcher.h"
 #include "type_conversion.h"
-#include "web_contents_adapter.h"
 #include "web_contents_adapter_client.h"
+#include "web_contents_adapter_p.h"
 #include "web_engine_context.h"
 #include "web_engine_settings.h"
 #include "web_engine_visited_links_manager.h"
@@ -84,8 +84,12 @@ WebContentsDelegateQt::WebContentsDelegateQt(content::WebContents *webContents,
 
 content::WebContents *WebContentsDelegateQt::OpenURLFromTab(content::WebContents *source, const content::OpenURLParams &params)
 {
-    // We already carry the disposition to the application through AddNewContents.
-    Q_UNUSED(params.disposition);
+    content::WebContents *target = source;
+    if (params.disposition != CURRENT_TAB) {
+        WebContentsAdapter *targetAdapter = createWindow(0, params.disposition, gfx::Rect(), params.user_gesture);
+        if (targetAdapter)
+            target = targetAdapter->d_func()->webContents.get();
+    }
 
     content::NavigationController::LoadURLParams load_url_params(params.url);
     load_url_params.referrer = params.referrer;
@@ -98,8 +102,8 @@ content::WebContents *WebContentsDelegateQt::OpenURLFromTab(content::WebContents
     if (params.transferred_global_request_id != content::GlobalRequestID())
         load_url_params.transferred_global_request_id = params.transferred_global_request_id;
 
-    source->GetController().LoadURLWithParams(load_url_params);
-    return source;
+    target->GetController().LoadURLWithParams(load_url_params);
+    return target;
 }
 
 void WebContentsDelegateQt::NavigationStateChanged(const content::WebContents* source, unsigned changed_flags)
@@ -112,18 +116,7 @@ void WebContentsDelegateQt::NavigationStateChanged(const content::WebContents* s
 
 void WebContentsDelegateQt::AddNewContents(content::WebContents* source, content::WebContents* new_contents, WindowOpenDisposition disposition, const gfx::Rect& initial_pos, bool user_gesture, bool* was_blocked)
 {
-    WebContentsAdapter *newAdapter = new WebContentsAdapter(new_contents);
-    // Do the first ref-count manually to be able to know if the application is handling adoptNewWindow through the public API.
-    newAdapter->ref.ref();
-
-    m_viewClient->adoptNewWindow(newAdapter, static_cast<WebContentsAdapterClient::WindowOpenDisposition>(disposition), user_gesture, toQt(initial_pos));
-
-    if (!newAdapter->ref.deref()) {
-        // adoptNewWindow didn't increase the ref-count, new_contents needs to be discarded.
-        delete newAdapter;
-        newAdapter = 0;
-    }
-
+    WebContentsAdapter *newAdapter = createWindow(new_contents, disposition, initial_pos, user_gesture);
     if (was_blocked)
         *was_blocked = !newAdapter;
 }
@@ -257,6 +250,7 @@ void WebContentsDelegateQt::UpdateTargetURL(content::WebContents *source, int32
     Q_UNUSED(page_id)
     m_viewClient->didUpdateTargetURL(toQt(url));
 }
+
 void WebContentsDelegateQt::DidNavigateAnyFrame(const content::LoadCommittedDetails &, const content::FrameNavigateParams &params)
 {
     if (!params.should_update_history)
@@ -264,7 +258,25 @@ void WebContentsDelegateQt::DidNavigateAnyFrame(const content::LoadCommittedDeta
     WebEngineContext::current()->visitedLinksManager()->addUrl(params.url);
 }
 
+
 void WebContentsDelegateQt::overrideWebPreferences(content::WebContents *, WebPreferences *webPreferences)
 {
     m_viewClient->webEngineSettings()->overrideWebPreferences(webPreferences);
 }
+
+WebContentsAdapter *WebContentsDelegateQt::createWindow(content::WebContents *new_contents, WindowOpenDisposition disposition, const gfx::Rect& initial_pos, bool user_gesture)
+{
+    WebContentsAdapter *newAdapter = new WebContentsAdapter(new_contents);
+    // Do the first ref-count manually to be able to know if the application is handling adoptNewWindow through the public API.
+    newAdapter->ref.ref();
+
+    m_viewClient->adoptNewWindow(newAdapter, static_cast<WebContentsAdapterClient::WindowOpenDisposition>(disposition), user_gesture, toQt(initial_pos));
+
+    if (!newAdapter->ref.deref()) {
+        // adoptNewWindow didn't increase the ref-count, newAdapter and its new_contents (if non-null) need to be discarded.
+        delete newAdapter;
+        newAdapter = 0;
+    }
+
+    return newAdapter;
+}
diff --git a/src/core/web_contents_delegate_qt.h b/src/core/web_contents_delegate_qt.h
index 8e1761a0a..7e349ebd4 100644
--- a/src/core/web_contents_delegate_qt.h
+++ b/src/core/web_contents_delegate_qt.h
@@ -91,6 +91,8 @@ public:
     void overrideWebPreferences(content::WebContents *, WebPreferences*);
 
 private:
+    WebContentsAdapter *createWindow(content::WebContents *new_contents, WindowOpenDisposition disposition, const gfx::Rect& initial_pos, bool user_gesture);
+
     WebContentsAdapterClient *m_viewClient;
     QString m_lastSearchedString;
     int m_lastReceivedFindReply;
-- 
GitLab