summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSzabolcs David <davidsz@inf.u-szeged.hu>2021-02-19 13:57:34 +0100
committerSzabolcs David <davidsz@inf.u-szeged.hu>2021-04-08 12:45:23 +0200
commitd1398e734c927435f236c2eed36613bedf71acf6 (patch)
treea13da06003b6b142bd5f09e2be110f93f7cb1f3e
parentfb60a0638a47564c1a1e7c2933b699d0205c810e (diff)
Fix embedded PDFs when plugins are disabled
Cover all cases from https://pdfobject.com/static - Plugin placeholder is generally broken: displays garbage and crashes on interaction. Fix it and show when PDFs are included by <embed> or <object> tags. - Do not start an automatical download when the disabled PDF plugin was requested by an iframe. Show a clickable placeholder and let the end-users start it manually. - Remove unused class PluginPlaceholderQt Task-number: QTBUG-76314 Change-Id: I01a0c93ab23f54e4272f5aeb30578de0dcf18932 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r--src/core/content_browser_client_qt.cpp13
-rw-r--r--src/core/core_chromium.pri4
-rw-r--r--src/core/extensions/pdf_iframe_navigation_throttle_qt.cpp219
-rw-r--r--src/core/extensions/pdf_iframe_navigation_throttle_qt.h (renamed from src/core/renderer/plugins/plugin_placeholder_qt.h)54
-rw-r--r--src/core/renderer/content_renderer_client_qt.cpp1
-rw-r--r--src/core/renderer/plugins/loadable_plugin_placeholder_qt.cpp2
-rw-r--r--src/core/renderer/plugins/plugin_placeholder_qt.cpp75
7 files changed, 264 insertions, 104 deletions
diff --git a/src/core/content_browser_client_qt.cpp b/src/core/content_browser_client_qt.cpp
index c2c78ff8b..da1ea8dde 100644
--- a/src/core/content_browser_client_qt.cpp
+++ b/src/core/content_browser_client_qt.cpp
@@ -167,6 +167,7 @@
#include "extensions/common/manifest_handlers/mime_types_handler.h"
#include "extensions/extension_web_contents_observer_qt.h"
#include "extensions/extensions_browser_client_qt.h"
+#include "extensions/pdf_iframe_navigation_throttle_qt.h"
#include "net/plugin_response_interceptor_url_loader_throttle.h"
#endif
@@ -222,6 +223,13 @@ bool IsHandledProtocol(base::StringPiece scheme)
namespace QtWebEngineCore {
+void MaybeAddThrottle(
+ std::unique_ptr<content::NavigationThrottle> maybe_throttle,
+ std::vector<std::unique_ptr<content::NavigationThrottle>>* throttles) {
+ if (maybe_throttle)
+ throttles->push_back(std::move(maybe_throttle));
+}
+
class QtShareGLContext : public gl::GLContext {
public:
QtShareGLContext(QOpenGLContext *qtContext)
@@ -1012,6 +1020,11 @@ std::vector<std::unique_ptr<content::NavigationThrottle>> ContentBrowserClientQt
navigation_handle,
base::BindRepeating(&navigationThrottleCallback),
navigation_interception::SynchronyMode::kSync));
+
+#if BUILDFLAG(ENABLE_EXTENSIONS)
+ MaybeAddThrottle(extensions::PDFIFrameNavigationThrottleQt::MaybeCreateThrottleFor(navigation_handle), &throttles);
+#endif
+
return throttles;
}
diff --git a/src/core/core_chromium.pri b/src/core/core_chromium.pri
index 108dd3221..e978d20a1 100644
--- a/src/core/core_chromium.pri
+++ b/src/core/core_chromium.pri
@@ -113,7 +113,6 @@ SOURCES = \
renderer/web_engine_page_render_frame.cpp \
renderer/render_configuration.cpp \
renderer/user_resource_controller.cpp \
- renderer/plugins/plugin_placeholder_qt.cpp \
renderer_host/web_engine_page_host.cpp \
renderer_host/user_resource_controller_host.cpp \
resource_bundle_qt.cpp \
@@ -218,7 +217,6 @@ HEADERS = \
renderer/web_engine_page_render_frame.h \
renderer/render_configuration.h \
renderer/user_resource_controller.h \
- renderer/plugins/plugin_placeholder_qt.h \
renderer_host/web_engine_page_host.h \
renderer_host/user_resource_controller_host.h \
request_controller.h \
@@ -326,6 +324,7 @@ qtConfig(webengine-extensions) {
extensions/extensions_browser_client_qt.cpp \
extensions/messaging_delegate_qt.cpp \
extensions/mime_handler_view_guest_delegate_qt.cpp \
+ extensions/pdf_iframe_navigation_throttle_qt.cpp \
extensions/plugin_service_filter_qt.cpp \
net/plugin_response_interceptor_url_loader_throttle.cpp \
renderer/extensions/extensions_dispatcher_delegate_qt.cpp \
@@ -345,6 +344,7 @@ qtConfig(webengine-extensions) {
extensions/extensions_browser_client_qt.h \
extensions/messaging_delegate_qt.h \
extensions/mime_handler_view_guest_delegate_qt.h \
+ extensions/pdf_iframe_navigation_throttle_qt.h \
extensions/plugin_service_filter_qt.h \
net/plugin_response_interceptor_url_loader_throttle.h \
renderer/extensions/extensions_dispatcher_delegate_qt.h \
diff --git a/src/core/extensions/pdf_iframe_navigation_throttle_qt.cpp b/src/core/extensions/pdf_iframe_navigation_throttle_qt.cpp
new file mode 100644
index 000000000..7c2fe75f0
--- /dev/null
+++ b/src/core/extensions/pdf_iframe_navigation_throttle_qt.cpp
@@ -0,0 +1,219 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 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$
+**
+****************************************************************************/
+
+// based on //chrome/browser/plugins/pdf_iframe_navigation_throttle.cc
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+
+#include "extensions/pdf_iframe_navigation_throttle_qt.h"
+
+#include "chrome/grit/renderer_resources.h"
+#include "components/strings/grit/components_strings.h"
+#include "content/public/browser/download_utils.h"
+#include "content/public/browser/navigation_handle.h"
+#include "content/public/browser/plugin_service.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_user_data.h"
+#include "content/public/common/webplugininfo.h"
+#include "net/base/escape.h"
+#include "net/http/http_response_headers.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/base/webui/jstemplate_builder.h"
+#include "ui/base/webui/web_ui_util.h"
+
+namespace extensions {
+
+constexpr char kPDFMimeType[] = "application/pdf";
+
+// Used to scope the posted navigation task to the lifetime of |web_contents|.
+class PdfWebContentsLifetimeHelper : public content::WebContentsUserData<PdfWebContentsLifetimeHelper>
+{
+public:
+ explicit PdfWebContentsLifetimeHelper(content::WebContents *web_contents)
+ : web_contents_(web_contents)
+ {}
+
+ base::WeakPtr<PdfWebContentsLifetimeHelper> GetWeakPtr()
+ {
+ return weak_factory_.GetWeakPtr();
+ }
+
+ void NavigateIFrameToPlaceholder(const content::OpenURLParams &url_params)
+ {
+ web_contents_->OpenURL(url_params);
+ }
+
+private:
+ friend class content::WebContentsUserData<PdfWebContentsLifetimeHelper>;
+
+ content::WebContents* const web_contents_;
+ base::WeakPtrFactory<PdfWebContentsLifetimeHelper> weak_factory_{this};
+
+ WEB_CONTENTS_USER_DATA_KEY_DECL();
+};
+
+WEB_CONTENTS_USER_DATA_KEY_IMPL(PdfWebContentsLifetimeHelper)
+
+bool IsPDFPluginEnabled(content::NavigationHandle *navigation_handle, bool *is_stale)
+{
+ content::WebContents *web_contents = navigation_handle->GetWebContents();
+ int process_id = web_contents->GetMainFrame()->GetProcess()->GetID();
+ int routing_id = web_contents->GetMainFrame()->GetRoutingID();
+ content::WebPluginInfo plugin_info;
+ // Will check WebEngineSettings by PluginServiceFilterQt
+ return content::PluginService::GetInstance()->GetPluginInfo(
+ process_id, routing_id, navigation_handle->GetURL(),
+ web_contents->GetMainFrame()->GetLastCommittedOrigin(), kPDFMimeType,
+ false /* allow_wildcard */, is_stale, &plugin_info,
+ nullptr /* actual_mime_type */);
+}
+
+std::string GetPDFPlaceholderHTML(const GURL &pdf_url)
+{
+ std::string template_html = ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(IDR_PDF_PLUGIN_HTML);
+ webui::AppendWebUiCssTextDefaults(&template_html);
+
+ base::DictionaryValue values;
+ values.SetString("fileName", pdf_url.ExtractFileName());
+ values.SetString("open", l10n_util::GetStringUTF8(IDS_ACCNAME_OPEN));
+ values.SetString("pdfUrl", pdf_url.spec());
+
+ return webui::GetI18nTemplateHtml(template_html, &values);
+}
+
+// static
+std::unique_ptr<content::NavigationThrottle>
+PDFIFrameNavigationThrottleQt::MaybeCreateThrottleFor(content::NavigationHandle *handle)
+{
+ if (handle->IsInMainFrame())
+ return nullptr;
+ return std::make_unique<PDFIFrameNavigationThrottleQt>(handle);
+}
+
+PDFIFrameNavigationThrottleQt::PDFIFrameNavigationThrottleQt(content::NavigationHandle *handle)
+ : content::NavigationThrottle(handle)
+{
+}
+
+PDFIFrameNavigationThrottleQt::~PDFIFrameNavigationThrottleQt()
+{
+}
+
+content::NavigationThrottle::ThrottleCheckResult PDFIFrameNavigationThrottleQt::WillProcessResponse()
+{
+ const net::HttpResponseHeaders *response_headers = navigation_handle()->GetResponseHeaders();
+ if (!response_headers)
+ return content::NavigationThrottle::PROCEED;
+
+ std::string mime_type;
+ response_headers->GetMimeType(&mime_type);
+ if (mime_type != kPDFMimeType)
+ return content::NavigationThrottle::PROCEED;
+
+ // We MUST download responses marked as attachments rather than showing
+ // a placeholder.
+ if (content::download_utils::MustDownload(navigation_handle()->GetURL(), response_headers, mime_type))
+ return content::NavigationThrottle::PROCEED;
+
+ bool is_stale = false;
+ bool pdf_plugin_enabled = IsPDFPluginEnabled(navigation_handle(), &is_stale);
+
+ if (is_stale) {
+ // On browser start, the plugin list may not be ready yet.
+ content::PluginService::GetInstance()->GetPlugins(
+ base::BindOnce(&PDFIFrameNavigationThrottleQt::OnPluginsLoaded,
+ weak_factory_.GetWeakPtr()));
+ return content::NavigationThrottle::DEFER;
+ }
+
+ // If the plugin was found, proceed on the navigation. Otherwise fall through
+ // to the placeholder case.
+ if (pdf_plugin_enabled)
+ return content::NavigationThrottle::PROCEED;
+
+ LoadPlaceholderHTML();
+ return content::NavigationThrottle::CANCEL_AND_IGNORE;
+}
+
+const char *PDFIFrameNavigationThrottleQt::GetNameForLogging()
+{
+ return "PDFIFrameNavigationThrottleQt";
+}
+
+void PDFIFrameNavigationThrottleQt::OnPluginsLoaded(
+ const std::vector<content::WebPluginInfo> &plugins)
+{
+ if (IsPDFPluginEnabled(navigation_handle(), nullptr /* is_stale */)) {
+ Resume();
+ } else {
+ LoadPlaceholderHTML();
+ CancelDeferredNavigation(content::NavigationThrottle::CANCEL_AND_IGNORE);
+ }
+}
+
+void PDFIFrameNavigationThrottleQt::LoadPlaceholderHTML()
+{
+ // Prepare the params to navigate to the placeholder.
+ std::string html = GetPDFPlaceholderHTML(navigation_handle()->GetURL());
+ GURL data_url("data:text/html," + net::EscapePath(html));
+ content::OpenURLParams params = content::OpenURLParams::FromNavigationHandle(navigation_handle());
+ params.url = data_url;
+ params.transition = ui::PAGE_TRANSITION_AUTO_SUBFRAME;
+
+ // Post a task to navigate to the placeholder HTML. We don't navigate
+ // synchronously here, as starting a navigation within a navigation is
+ // an antipattern. Use a helper object scoped to the WebContents lifetime to
+ // scope the navigation task to the WebContents lifetime.
+ content::WebContents *web_contents = navigation_handle()->GetWebContents();
+ if (!web_contents)
+ return;
+
+ PdfWebContentsLifetimeHelper::CreateForWebContents(web_contents);
+ PdfWebContentsLifetimeHelper *helper = PdfWebContentsLifetimeHelper::FromWebContents(web_contents);
+ base::SequencedTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE,
+ base::BindOnce(&PdfWebContentsLifetimeHelper::NavigateIFrameToPlaceholder,
+ helper->GetWeakPtr(), std::move(params)));
+}
+
+} // namespace extensions
diff --git a/src/core/renderer/plugins/plugin_placeholder_qt.h b/src/core/extensions/pdf_iframe_navigation_throttle_qt.h
index a99c0d045..37f5bf1d6 100644
--- a/src/core/renderer/plugins/plugin_placeholder_qt.h
+++ b/src/core/extensions/pdf_iframe_navigation_throttle_qt.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2020 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWebEngine module of the Qt Toolkit.
@@ -37,42 +37,46 @@
**
****************************************************************************/
-// Copyright 2013 The Chromium Authors. All rights reserved.
+// based on //chrome/browser/plugins/pdf_iframe_navigation_throttle.h
+// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef PLUGIN_PLACEHOLDER_QT_H
-#define PLUGIN_PLACEHOLDER_QT_H
+#ifndef PDF_IFRAME_NAVIGATION_THROTTLE_QT
+#define PDF_IFRAME_NAVIGATION_THROTTLE_QT
-#include "base/macros.h"
-#include "components/plugins/renderer/plugin_placeholder.h"
-#include "gin/handle.h"
-#include "gin/wrappable.h"
-#include "third_party/blink/public/web/web_plugin_params.h"
+#include "content/public/browser/navigation_throttle.h"
-namespace QtWebEngineCore {
+#include "base/memory/weak_ptr.h"
-// A basic placeholder that supports only hiding.
-class PluginPlaceholderQt final : public plugins::PluginPlaceholderBase
- , public gin::Wrappable<PluginPlaceholderQt>
+namespace content {
+class NavigationHandle;
+struct WebPluginInfo;
+}
+
+namespace extensions {
+
+// This class prevents automatical download of PDFs when they are embedded
+// in subframes and plugins are disabled in API.
+class PDFIFrameNavigationThrottleQt : public content::NavigationThrottle
{
public:
- static gin::WrapperInfo kWrapperInfo;
+ static std::unique_ptr<content::NavigationThrottle> MaybeCreateThrottleFor(content::NavigationHandle *handle);
+
+ explicit PDFIFrameNavigationThrottleQt(content::NavigationHandle *handle);
+ ~PDFIFrameNavigationThrottleQt() override;
- PluginPlaceholderQt(content::RenderFrame* render_frame,
- const blink::WebPluginParams& params,
- const std::string& html_data);
- ~PluginPlaceholderQt() override;
+ // content::NavigationThrottle
+ content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() override;
+ const char *GetNameForLogging() override;
private:
- // WebViewPlugin::Delegate methods:
- v8::Local<v8::Value> GetV8Handle(v8::Isolate* isolate) final;
+ void OnPluginsLoaded(const std::vector<content::WebPluginInfo> &plugins);
+ void LoadPlaceholderHTML();
- // gin::Wrappable method:
- gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
- v8::Isolate* isolate) override;
+ base::WeakPtrFactory<PDFIFrameNavigationThrottleQt> weak_factory_{this};
};
-} // namespace QtWebEngineCore
+} // namespace extensions
-#endif // PLUGIN_PLACEHOLDER_QT_H
+#endif // PDF_IFRAME_NAVIGATION_THROTTLE_QT
diff --git a/src/core/renderer/content_renderer_client_qt.cpp b/src/core/renderer/content_renderer_client_qt.cpp
index 317fde8f7..28815b5c1 100644
--- a/src/core/renderer/content_renderer_client_qt.cpp
+++ b/src/core/renderer/content_renderer_client_qt.cpp
@@ -97,7 +97,6 @@
#if BUILDFLAG(ENABLE_PLUGINS)
#include "plugins/loadable_plugin_placeholder_qt.h"
-#include "plugins/plugin_placeholder_qt.h"
#include "content/common/frame_messages.h"
#endif // ENABLE_PLUGINS
diff --git a/src/core/renderer/plugins/loadable_plugin_placeholder_qt.cpp b/src/core/renderer/plugins/loadable_plugin_placeholder_qt.cpp
index b43d9a24b..9dc5fbd94 100644
--- a/src/core/renderer/plugins/loadable_plugin_placeholder_qt.cpp
+++ b/src/core/renderer/plugins/loadable_plugin_placeholder_qt.cpp
@@ -77,7 +77,7 @@ LoadablePluginPlaceholderQt::~LoadablePluginPlaceholderQt()
LoadablePluginPlaceholderQt* LoadablePluginPlaceholderQt::CreateLoadableMissingPlugin(content::RenderFrame* render_frame,
const blink::WebPluginParams& params)
{
- const base::StringPiece template_html(ui::ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_BLOCKED_PLUGIN_HTML));
+ std::string template_html(ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(IDR_BLOCKED_PLUGIN_HTML));
base::DictionaryValue values;
values.SetString("name", "");
diff --git a/src/core/renderer/plugins/plugin_placeholder_qt.cpp b/src/core/renderer/plugins/plugin_placeholder_qt.cpp
deleted file mode 100644
index a72c29873..000000000
--- a/src/core/renderer/plugins/plugin_placeholder_qt.cpp
+++ /dev/null
@@ -1,75 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 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 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "plugin_placeholder_qt.h"
-
-#include "content/public/renderer/render_frame.h"
-#include "content/public/renderer/v8_value_converter.h"
-#include "gin/object_template_builder.h"
-
-namespace QtWebEngineCore {
-
-// static
-gin::WrapperInfo PluginPlaceholderQt::kWrapperInfo = {gin::kEmbedderNativeGin};
-
-PluginPlaceholderQt::PluginPlaceholderQt(content::RenderFrame* render_frame,
- const blink::WebPluginParams& params,
- const std::string& html_data)
- : PluginPlaceholderBase(render_frame, params, html_data)
-{}
-
-PluginPlaceholderQt::~PluginPlaceholderQt() {}
-
-v8::Local<v8::Value> PluginPlaceholderQt::GetV8Handle(v8::Isolate* isolate)
-{
- return gin::CreateHandle(isolate, this).ToV8();
-}
-
-gin::ObjectTemplateBuilder PluginPlaceholderQt::GetObjectTemplateBuilder(v8::Isolate* isolate)
-{
- return gin::Wrappable<PluginPlaceholderQt>::GetObjectTemplateBuilder(isolate)
- .SetMethod<void (QtWebEngineCore::PluginPlaceholderQt::*)()>(
- "hide", &PluginPlaceholderQt::HideCallback);
-}
-
-} // namespace QtWebEngineCore