diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc index 124136ccafa70f..12268c0ca1b7ec 100644 --- a/components/printing/renderer/print_render_frame_helper.cc +++ b/components/printing/renderer/print_render_frame_helper.cc @@ -14,6 +14,7 @@ #include "base/auto_reset.h" #include "base/bind.h" +#include "base/feature_list.h" #include "base/i18n/rtl.h" #include "base/json/json_writer.h" #include "base/location.h" @@ -21,6 +22,7 @@ #include "base/macros.h" #include "base/memory/shared_memory_mapping.h" #include "base/metrics/histogram_functions.h" +#include "base/metrics/histogram_macros.h" #include "base/process/process_handle.h" #include "base/single_thread_task_runner.h" #include "base/strings/string_number_conversions.h" @@ -38,6 +40,7 @@ #include "net/base/escape.h" #include "printing/buildflags/buildflags.h" #include "printing/metafile_skia.h" +#include "printing/printing_features.h" #include "printing/units.h" #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h" #include "third_party/blink/public/common/frame/frame_owner_element_type.h" @@ -45,6 +48,7 @@ #include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/web_data.h" #include "third_party/blink/public/platform/web_double_size.h" +#include "third_party/blink/public/platform/web_failing_url_loader_factory.h" #include "third_party/blink/public/platform/web_size.h" #include "third_party/blink/public/platform/web_url.h" #include "third_party/blink/public/platform/web_url_request.h" @@ -803,6 +807,7 @@ class PrepareFrameAndViewForPrint : public blink::WebViewClient, void CopySelection(const WebPreferences& preferences); FrameReference frame_; + FrameReference original_frame_; blink::WebNavigationControl* navigation_control_ = nullptr; blink::WebNode node_to_print_; bool owns_web_view_ = false; @@ -826,6 +831,7 @@ PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( const blink::WebNode& node, bool ignore_css_margins) : frame_(frame), + original_frame_(frame), node_to_print_(node), should_print_backgrounds_(params.should_print_backgrounds), should_print_selection_only_(params.selection_only) { @@ -990,6 +996,16 @@ void PrepareFrameAndViewForPrint::FrameDetached(DetachType detach_type) { std::unique_ptr PrepareFrameAndViewForPrint::CreateURLLoaderFactory() { + if (base::FeatureList::IsEnabled( + features::kUseFrameAssociatedLoaderFactory)) { + blink::WebLocalFrame* frame = original_frame_.GetFrame(); + UMA_HISTOGRAM_BOOLEAN("Printing.FrameIsActiveOnCreateLoaderFactory", + !!frame); + if (!frame) { + return std::make_unique(); + } + return frame->Client()->CreateURLLoaderFactory(); + } return blink::Platform::Current()->CreateDefaultURLLoaderFactory(); } diff --git a/printing/printing_features.cc b/printing/printing_features.cc index 26b94dea36d464..d7fb6a5a26de28 100644 --- a/printing/printing_features.cc +++ b/printing/printing_features.cc @@ -36,5 +36,10 @@ bool ShouldPrintUsingXps(bool source_is_pdf) { } #endif // defined(OS_WIN) +// When enabled, PrintRenderFrameHelper uses a frame-associated +// URLLoaderFactory rather than renderer-associated one. +const base::Feature kUseFrameAssociatedLoaderFactory{ + "UseFrameAssociatedLoaderFactory", base::FEATURE_ENABLED_BY_DEFAULT}; + } // namespace features } // namespace printing diff --git a/printing/printing_features.h b/printing/printing_features.h index dc816231069dae..1ea2faa85e1b54 100644 --- a/printing/printing_features.h +++ b/printing/printing_features.h @@ -31,6 +31,8 @@ PRINTING_EXPORT bool IsXpsPrintCapabilityRequired(); PRINTING_EXPORT bool ShouldPrintUsingXps(bool source_is_pdf); #endif +PRINTING_EXPORT extern const base::Feature kUseFrameAssociatedLoaderFactory; + } // namespace features } // namespace printing diff --git a/third_party/blink/public/BUILD.gn b/third_party/blink/public/BUILD.gn index 24330e3d887f44..f3a6621835805c 100644 --- a/third_party/blink/public/BUILD.gn +++ b/third_party/blink/public/BUILD.gn @@ -192,6 +192,7 @@ source_set("blink_headers") { "platform/web_encrypted_media_key_information.h", "platform/web_encrypted_media_request.h", "platform/web_encrypted_media_types.h", + "platform/web_failing_url_loader_factory.h", "platform/web_fetch_client_settings_object.h", "platform/web_file_system_type.h", "platform/web_float_rect.h", diff --git a/third_party/blink/public/platform/web_failing_url_loader_factory.h b/third_party/blink/public/platform/web_failing_url_loader_factory.h new file mode 100644 index 00000000000000..06747adfcd559e --- /dev/null +++ b/third_party/blink/public/platform/web_failing_url_loader_factory.h @@ -0,0 +1,28 @@ +// Copyright 2020 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 THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_FAILING_URL_LOADER_FACTORY_H_ +#define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_FAILING_URL_LOADER_FACTORY_H_ + +#include "third_party/blink/public/platform/web_common.h" +#include "third_party/blink/public/platform/web_url_loader_factory.h" + +namespace blink { + +// A WebURLLoaderFactory implementation that creates WebURLLoaders that +// always fail loading. +class BLINK_PLATFORM_EXPORT WebFailingURLLoaderFactory final + : public WebURLLoaderFactory { + public: + WebFailingURLLoaderFactory() = default; + ~WebFailingURLLoaderFactory() override = default; + + std::unique_ptr CreateURLLoader( + const WebURLRequest&, + std::unique_ptr) override; +}; + +} // namespace blink + +#endif // THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_FAILING_URL_LOADER_FACTORY_H_ diff --git a/third_party/blink/renderer/platform/BUILD.gn b/third_party/blink/renderer/platform/BUILD.gn index 1223f56d337337..937114e311e2ce 100644 --- a/third_party/blink/renderer/platform/BUILD.gn +++ b/third_party/blink/renderer/platform/BUILD.gn @@ -508,6 +508,7 @@ jumbo_component("platform") { "exported/web_encrypted_media_client.cc", "exported/web_encrypted_media_key_information.cc", "exported/web_encrypted_media_request.cc", + "exported/web_failing_url_loader_factory.cc", "exported/web_font.cc", "exported/web_font_description.cc", "exported/web_http_body.cc", diff --git a/third_party/blink/renderer/platform/DEPS b/third_party/blink/renderer/platform/DEPS index b9506e0b124c9d..99201dec859992 100644 --- a/third_party/blink/renderer/platform/DEPS +++ b/third_party/blink/renderer/platform/DEPS @@ -62,6 +62,8 @@ include_rules = [ "+services/metrics/public/cpp/ukm_entry_builder.h", "+services/metrics/public/cpp/ukm_recorder.h", "+services/metrics/public/cpp/ukm_source_id.h", + "+services/network/public/cpp", + "+services/network/public/mojom", "+services/viz/public/mojom/compositing/compositor_frame_sink.mojom-blink.h", "+skia/ext", #TODO(nverne): remove this diff --git a/third_party/blink/renderer/platform/exported/web_failing_url_loader_factory.cc b/third_party/blink/renderer/platform/exported/web_failing_url_loader_factory.cc new file mode 100644 index 00000000000000..e1633daf19b224 --- /dev/null +++ b/third_party/blink/renderer/platform/exported/web_failing_url_loader_factory.cc @@ -0,0 +1,88 @@ +// Copyright 2020 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 "third_party/blink/public/platform/web_failing_url_loader_factory.h" + +#include + +#include "base/memory/ref_counted.h" +#include "base/memory/weak_ptr.h" +#include "base/single_thread_task_runner.h" +#include "services/network/public/cpp/resource_request.h" +#include "third_party/blink/public/platform/scheduler/web_resource_loading_task_runner_handle.h" +#include "third_party/blink/public/platform/web_url_error.h" +#include "third_party/blink/public/platform/web_url_loader.h" +#include "third_party/blink/public/platform/web_url_loader_client.h" +#include "third_party/blink/renderer/platform/loader/fetch/resource_error.h" +#include "third_party/blink/renderer/platform/weborigin/kurl.h" + +namespace blink { + +namespace { + +// A WebURLLoader which always fails loading. +class FailingLoader final : public WebURLLoader { + public: + explicit FailingLoader( + std::unique_ptr + task_runner_handle) + : task_runner_handle_(std::move(task_runner_handle)) {} + ~FailingLoader() override = default; + + // WebURLLoader implementation: + void LoadSynchronously( + std::unique_ptr request, + scoped_refptr request_extra_data, + int requestor_id, + bool download_to_network_cache_only, + bool pass_response_pipe_to_client, + bool no_mime_sniffing, + base::TimeDelta timeout_interval, + WebURLLoaderClient*, + WebURLResponse&, + base::Optional& error, + WebData&, + int64_t& encoded_data_length, + int64_t& encoded_body_length, + WebBlobInfo& downloaded_blob) override { + error = ResourceError::Failure(KURL(request->url)); + } + void LoadAsynchronously( + std::unique_ptr request, + scoped_refptr request_extra_data, + int requestor_id, + bool download_to_network_cache_only, + bool no_mime_sniffing, + WebURLLoaderClient* client) override { + GetTaskRunner()->PostTask( + FROM_HERE, WTF::Bind(&FailingLoader::Fail, weak_factory_.GetWeakPtr(), + KURL(request->url), WTF::Unretained(client))); + } + void SetDefersLoading(bool) override {} + void DidChangePriority(WebURLRequest::Priority, int) override {} + scoped_refptr GetTaskRunner() override { + return task_runner_handle_->GetTaskRunner(); + } + + private: + void Fail(const KURL& url, WebURLLoaderClient* client) { + client->DidFail(ResourceError::Failure(url), 0, 0, 0); + } + + const std::unique_ptr + task_runner_handle_; + + // This must be the last member. + base::WeakPtrFactory weak_factory_{this}; +}; + +} // namespace + +std::unique_ptr WebFailingURLLoaderFactory::CreateURLLoader( + const WebURLRequest&, + std::unique_ptr handle) { + return std::make_unique(std::move(handle)); +} + +} // namespace blink diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml index 6b9c76cacfdcb6..b9c63bf177b7c3 100644 --- a/tools/metrics/histograms/histograms.xml +++ b/tools/metrics/histograms/histograms.xml @@ -125592,6 +125592,18 @@ uploading your change for review. + + yhirano@chromium.org + lukasza@chromium.org + + Records whether the associated frame is active when + PrepareFrameAndViewForPrint::CreateURLLoaderFactory is called. If it is + always active we want to replace the CreateFailingURLLoaderFactory call with + a DCHECK. + + + vkuzkokov@chromium.org skau@chromium.org