diff options
Diffstat (limited to 'src/core/web_contents_delegate_qt.cpp')
-rw-r--r-- | src/core/web_contents_delegate_qt.cpp | 203 |
1 files changed, 128 insertions, 75 deletions
diff --git a/src/core/web_contents_delegate_qt.cpp b/src/core/web_contents_delegate_qt.cpp index 292043f78..4df73fb69 100644 --- a/src/core/web_contents_delegate_qt.cpp +++ b/src/core/web_contents_delegate_qt.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/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.LGPL3 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-3.0.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 (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only // Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be @@ -43,13 +7,19 @@ #include "web_contents_delegate_qt.h" -#include "profile_adapter.h" +#include "certificate_error_controller.h" #include "color_chooser_controller.h" #include "color_chooser_qt.h" #include "custom_handlers/protocol_handler_registry_factory.h" #include "custom_handlers/register_protocol_handler_request_controller_impl.h" +#include "desktop_media_controller.h" +#include "desktop_media_controller_p.h" #include "file_picker_controller.h" +#include "find_text_helper.h" +#include "javascript_dialog_manager_qt.h" #include "media_capture_devices_dispatcher.h" +#include "native_web_keyboard_event_qt.h" +#include "profile_adapter.h" #include "profile_qt.h" #include "qwebengineloadinginfo.h" #include "qwebengineregisterprotocolhandlerrequest.h" @@ -67,11 +37,11 @@ #include "components/custom_handlers/protocol_handler_registry.h" #include "components/web_cache/browser/web_cache_manager.h" #include "content/browser/renderer_host/render_frame_host_impl.h" -#include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/file_select_listener.h" #include "content/public/browser/invalidate_type.h" +#include "content/public/browser/media_stream_request.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_handle.h" #include "content/public/browser/render_view_host.h" @@ -81,6 +51,7 @@ #include "content/public/common/url_constants.h" #include "net/base/data_url.h" #include "net/base/url_util.h" +#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h" #include <QDesktopServices> #include <QTimer> @@ -237,11 +208,11 @@ QUrl WebContentsDelegateQt::url(content::WebContents *source) const m_pendingUrlUpdate = false; return newUrl; } -void WebContentsDelegateQt::AddNewContents(content::WebContents* source, std::unique_ptr<content::WebContents> new_contents, const GURL &target_url, - WindowOpenDisposition disposition, const gfx::Rect& initial_pos, bool user_gesture, bool* was_blocked) +void WebContentsDelegateQt::AddNewContents(content::WebContents *source, std::unique_ptr<content::WebContents> new_contents, const GURL &target_url, + WindowOpenDisposition disposition, const blink::mojom::WindowFeatures &window_features, bool user_gesture, bool *was_blocked) { Q_UNUSED(source) - QSharedPointer<WebContentsAdapter> newAdapter = createWindow(std::move(new_contents), disposition, initial_pos, toQt(target_url), user_gesture); + QSharedPointer<WebContentsAdapter> newAdapter = createWindow(std::move(new_contents), disposition, window_features.bounds, toQt(target_url), 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. @@ -276,9 +247,9 @@ void WebContentsDelegateQt::LoadProgressChanged(double progress) bool WebContentsDelegateQt::HandleKeyboardEvent(content::WebContents *, const content::NativeWebKeyboardEvent &event) { - Q_ASSERT(!event.skip_in_browser); + Q_ASSERT(!event.skip_if_unhandled); if (event.os_event) - m_viewClient->unhandledKeyEvent(reinterpret_cast<QKeyEvent *>(event.os_event)); + m_viewClient->unhandledKeyEvent(ToKeyEvent(event.os_event)); // FIXME: ? return true; } @@ -287,6 +258,10 @@ void WebContentsDelegateQt::RenderFrameCreated(content::RenderFrameHost *render_ { content::FrameTreeNode *node = static_cast<content::RenderFrameHostImpl *>(render_frame_host)->frame_tree_node(); m_frameFocusedObserver.addNode(node); + + // If it's a child frame (render_widget_host_view_child_frame) install an InputEventObserver on + // it. Note that it is only needed for WheelEventAck. + RenderWidgetHostViewQt::registerInputEventObserver(web_contents(), render_frame_host); } void WebContentsDelegateQt::PrimaryMainFrameRenderProcessGone(base::TerminationStatus status) @@ -304,6 +279,7 @@ void WebContentsDelegateQt::PrimaryMainFrameRenderProcessGone(base::TerminationS || status == base::TERMINATION_STATUS_STILL_RUNNING) { return; } + LOG(INFO) << "ProcessGone: " << int(status) << " (" << web_contents()->GetCrashedErrorCode() << ")"; setLoadingState(LoadingState::Unloaded); } @@ -338,6 +314,20 @@ void WebContentsDelegateQt::RenderViewHostChanged(content::RenderViewHost *, con Q_ASSERT(rwhv->delegate()); rwhv->delegate()->adapterClientChanged(m_viewClient); m_viewClient->zoomUpdateIsNeeded(); + auto backgroundColor = m_viewClient->backgroundColor(); + if (backgroundColor != Qt::white) + m_viewClient->webContentsAdapter()->setBackgroundColor(backgroundColor); + } +} + +void WebContentsDelegateQt::RenderViewReady() +{ + // The render view might have returned after a crash without us getting a RenderViewHostChanged call + content::RenderWidgetHostView *newHostView = web_contents()->GetRenderWidgetHostView(); + if (newHostView) { + auto *rwhv = static_cast<RenderWidgetHostViewQt *>(newHostView); + Q_ASSERT(rwhv->delegate()); + rwhv->delegate()->updateAdapterClientIfNeeded(m_viewClient); } } @@ -399,7 +389,8 @@ void WebContentsDelegateQt::emitLoadFinished(bool isErrorPage) ? QWebEngineLoadingInfo::LoadStoppedStatus : QWebEngineLoadingInfo::LoadFailedStatus); QWebEngineLoadingInfo info(m_loadingInfo.url, loadStatus, m_loadingInfo.isErrorPage, m_loadingInfo.errorDescription, m_loadingInfo.errorCode, - QWebEngineLoadingInfo::ErrorDomain(m_loadingInfo.errorDomain)); + QWebEngineLoadingInfo::ErrorDomain(m_loadingInfo.errorDomain), + m_loadingInfo.responseHeaders); m_viewClient->loadFinished(std::move(info)); m_viewClient->updateNavigationActions(); } @@ -427,6 +418,20 @@ void WebContentsDelegateQt::DidFinishNavigation(content::NavigationHandle *navig emitLoadCommitted(); } + const net::HttpResponseHeaders * const responseHeaders = navigation_handle->GetResponseHeaders(); + if (responseHeaders != nullptr) { + m_loadingInfo.responseHeaders.clear(); + std::size_t iter = 0; + std::string headerName; + std::string headerValue; + while (responseHeaders->EnumerateHeaderLines(&iter, &headerName, &headerValue)) { + m_loadingInfo.responseHeaders.insert( + QByteArray::fromStdString(headerName), + QByteArray::fromStdString(headerValue) + ); + } + } + // Success is reported by DidFinishLoad, but DidFailLoad is now dead code and needs to be handled below if (navigation_handle->GetNetErrorCode() == net::OK) return; @@ -493,7 +498,7 @@ void WebContentsDelegateQt::DidFailLoad(content::RenderFrameHost* render_frame_h { setLoadingState(LoadingState::Loaded); - if (render_frame_host != web_contents()->GetMainFrame()) + if (render_frame_host != web_contents()->GetPrimaryMainFrame()) return; if (validated_url.spec() == content::kUnreachableWebDataURL) { @@ -616,9 +621,45 @@ void WebContentsDelegateQt::FindReply(content::WebContents *source, int request_ m_findTextHelper->handleFindReply(source, request_id, number_of_matches, selection_rect, active_match_ordinal, final_update); } +static void processMediaAccessRequest(content::WebContents *webContents, + const content::MediaStreamRequest &request, + content::MediaResponseCallback callback, + content::DesktopMediaID id) +{ + MediaCaptureDevicesDispatcher::GetInstance()->processMediaAccessRequest( + webContents, request, std::move(callback), id); +} + +static inline bool needsPickerDialog(const content::MediaStreamRequest &request) +{ + return (request.requested_video_device_id.empty() && // device already selected in chooseDesktopMedia + (request.video_type == blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE + || request.video_type == blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE)); +} + void WebContentsDelegateQt::RequestMediaAccessPermission(content::WebContents *web_contents, const content::MediaStreamRequest &request, content::MediaResponseCallback callback) { - MediaCaptureDevicesDispatcher::GetInstance()->processMediaAccessRequest(web_contents, request, std::move(callback)); + if (needsPickerDialog(request)) { +#if QT_CONFIG(webengine_webrtc) + base::OnceCallback<void(content::DesktopMediaID)> cb = base::BindOnce( + &processMediaAccessRequest, web_contents, std::move(request), std::move(callback)); + // ownership is taken by the request + auto *controller = new DesktopMediaController( + new DesktopMediaControllerPrivate(std::move(cb))); + QObject::connect(controller, &DesktopMediaController::mediaListsInitialized, [controller, delegate = AsWeakPtr()]() { + if (delegate) + delegate->adapterClient()->desktopMediaRequested(controller); + else + controller->deleteLater(); + }); +#else + // To keep the old behavior return the default screen even if webrtc is disabled + processMediaAccessRequest(web_contents, std::move(request), std::move(callback), + content::DesktopMediaID(content::DesktopMediaID::TYPE_SCREEN, 0)); +#endif // QT_CONFIG(webengine_webrtc) + } else { + processMediaAccessRequest(web_contents, std::move(request), std::move(callback), {}); + } } void WebContentsDelegateQt::SetContentsBounds(content::WebContents *source, const gfx::Rect &bounds) @@ -629,8 +670,8 @@ void WebContentsDelegateQt::SetContentsBounds(content::WebContents *source, cons QRect frameGeometry(toQt(bounds)); QRect geometry; if (RenderWidgetHostViewQt *rwhv = static_cast<RenderWidgetHostViewQt*>(web_contents()->GetRenderWidgetHostView())) { - if (rwhv->delegate() && rwhv->delegate()->window()) - geometry = frameGeometry.marginsRemoved(rwhv->delegate()->window()->frameMargins()); + if (rwhv->delegate() && rwhv->delegate()->Window()) + geometry = frameGeometry.marginsRemoved(rwhv->delegate()->Window()->frameMargins()); } m_viewClient->requestGeometryChange(geometry, frameGeometry); } @@ -641,12 +682,6 @@ void WebContentsDelegateQt::UpdateTargetURL(content::WebContents* source, const m_viewClient->didUpdateTargetURL(toQt(url)); } -void WebContentsDelegateQt::OnVisibilityChanged(content::Visibility visibility) -{ - if (visibility != content::Visibility::HIDDEN) - web_cache::WebCacheManager::GetInstance()->ObserveActivity(web_contents()->GetMainFrame()->GetProcess()->GetID()); -} - void WebContentsDelegateQt::ActivateContents(content::WebContents* contents) { QWebEngineSettings *settings = m_viewClient->webEngineSettings(); @@ -725,8 +760,8 @@ void WebContentsDelegateQt::launchExternalURL(const QUrl &url, ui::PageTransitio } if (navigationAllowedByPolicy) { - m_viewClient->navigationRequested(pageTransitionToNavigationType(page_transition), url, navigationRequestAccepted, is_main_frame); -#ifndef QT_NO_DESKTOPSERVICES + m_viewClient->navigationRequested(pageTransitionToNavigationType(page_transition), url, navigationRequestAccepted, is_main_frame, false); +#if QT_CONFIG(desktopservices) if (navigationRequestAccepted) QDesktopServices::openUrl(url); #endif @@ -751,12 +786,6 @@ void WebContentsDelegateQt::BeforeUnloadFired(content::WebContents *tab, bool pr m_viewClient->windowCloseRejected(); } -void WebContentsDelegateQt::BeforeUnloadFired(bool proceed, const base::TimeTicks &proceed_time) -{ - Q_UNUSED(proceed); - Q_UNUSED(proceed_time); -} - bool WebContentsDelegateQt::CheckMediaAccessPermission(content::RenderFrameHost *, const GURL& security_origin, blink::mojom::MediaStreamType type) { switch (type) { @@ -766,7 +795,7 @@ bool WebContentsDelegateQt::CheckMediaAccessPermission(content::RenderFrameHost return m_viewClient->profileAdapter()->checkPermission(toQt(security_origin), ProfileAdapter::VideoCapturePermission); default: LOG(INFO) << "WebContentsDelegateQt::CheckMediaAccessPermission: " - << "Unsupported media stream type checked" << type; + << "Unsupported media stream type checked " << type; return false; } } @@ -775,8 +804,8 @@ void WebContentsDelegateQt::RegisterProtocolHandler(content::RenderFrameHost *fr { content::BrowserContext *context = frameHost->GetBrowserContext(); - content::ProtocolHandler handler = - content::ProtocolHandler::CreateProtocolHandler(protocol, url); + custom_handlers::ProtocolHandler handler = + custom_handlers::ProtocolHandler::CreateProtocolHandler(protocol, url); custom_handlers::ProtocolHandlerRegistry *registry = ProtocolHandlerRegistryFactory::GetForBrowserContext(context); @@ -792,8 +821,8 @@ void WebContentsDelegateQt::UnregisterProtocolHandler(content::RenderFrameHost * { content::BrowserContext* context = frameHost->GetBrowserContext(); - content::ProtocolHandler handler = - content::ProtocolHandler::CreateProtocolHandler(protocol, url); + custom_handlers::ProtocolHandler handler = + custom_handlers::ProtocolHandler::CreateProtocolHandler(protocol, url); custom_handlers::ProtocolHandlerRegistry* registry = ProtocolHandlerRegistryFactory::GetForBrowserContext(context); @@ -827,6 +856,15 @@ void WebContentsDelegateQt::ResourceLoadComplete(content::RenderFrameHost* rende } } +void WebContentsDelegateQt::InnerWebContentsAttached(content::WebContents *inner_web_contents, + content::RenderFrameHost *render_frame_host, + bool is_full_page) +{ + blink::web_pref::WebPreferences guestPrefs = inner_web_contents->GetOrCreateWebPreferences(); + webEngineSettings()->overrideWebPreferences(inner_web_contents, &guestPrefs); + inner_web_contents->SetWebPreferences(guestPrefs); +} + FindTextHelper *WebContentsDelegateQt::findTextHelper() { return m_findTextHelper.data(); @@ -890,6 +928,7 @@ int &WebContentsDelegateQt::streamCount(blink::mojom::MediaStreamType type) case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE: case blink::mojom::MediaStreamType::DISPLAY_AUDIO_CAPTURE: case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_THIS_TAB: + case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET: return m_desktopStreamCount; case blink::mojom::MediaStreamType::NO_SERVICE: @@ -901,22 +940,36 @@ int &WebContentsDelegateQt::streamCount(blink::mojom::MediaStreamType type) return m_videoStreamCount; } -void WebContentsDelegateQt::addDevices(const blink::MediaStreamDevices &devices) +void WebContentsDelegateQt::addDevices(const blink::mojom::StreamDevices &devices) { - for (const auto &device : devices) - ++streamCount(device.type); + if (devices.audio_device.has_value()) + addDevice(devices.audio_device.value()); + if (devices.video_device.has_value()) + addDevice(devices.video_device.value()); webContentsAdapter()->updateRecommendedState(); } -void WebContentsDelegateQt::removeDevices(const blink::MediaStreamDevices &devices) +void WebContentsDelegateQt::removeDevices(const blink::mojom::StreamDevices &devices) { - for (const auto &device : devices) - ++streamCount(device.type); + if (devices.audio_device.has_value()) + removeDevice(devices.audio_device.value()); + if (devices.video_device.has_value()) + removeDevice(devices.video_device.value()); webContentsAdapter()->updateRecommendedState(); } +void WebContentsDelegateQt::addDevice(const blink::MediaStreamDevice &device) +{ + ++streamCount(device.type); +} + +void WebContentsDelegateQt::removeDevice(const blink::MediaStreamDevice &device) +{ + --streamCount(device.type); +} + FrameFocusedObserver::FrameFocusedObserver(WebContentsAdapterClient *adapterClient) : m_viewClient(adapterClient) {} |