Skip to content

Commit

Permalink
Use frame associated URLLoaderFactory in PrepareFrameAndViewForPrint
Browse files Browse the repository at this point in the history
In order to deprecate Platform::CreateDefaultURLLoaderFactory, this
change removes its call from PrepareFrameAndViewForPrint instead of
Platform::CreateDefaultURLLoaderFactory, use the
RenderFrameImpl::CreateURLLoaderFactory.

Bug: 891872
Change-Id: I128348c2ee284ddc27b17b4bdc9bf6f3bdc82608
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2043615
Reviewed-by: Kent Tamura <tkent@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: Steven Holte <holte@chromium.org>
Reviewed-by: Wei Li <weili@chromium.org>
Reviewed-by: Łukasz Anforowicz <lukasza@chromium.org>
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747226}
  • Loading branch information
yutakahirano authored and Commit Bot committed Mar 5, 2020
1 parent 14a6938 commit c45e93c
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 0 deletions.
16 changes: 16 additions & 0 deletions components/printing/renderer/print_render_frame_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

#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"
#include "base/logging.h"
#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"
Expand All @@ -38,13 +40,15 @@
#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"
#include "third_party/blink/public/common/frame/sandbox_flags.h"
#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"
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -990,6 +996,16 @@ void PrepareFrameAndViewForPrint::FrameDetached(DetachType detach_type) {

std::unique_ptr<blink::WebURLLoaderFactory>
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<blink::WebFailingURLLoaderFactory>();
}
return frame->Client()->CreateURLLoaderFactory();
}
return blink::Platform::Current()->CreateDefaultURLLoaderFactory();
}

Expand Down
5 changes: 5 additions & 0 deletions printing/printing_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions printing/printing_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions third_party/blink/public/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions third_party/blink/public/platform/web_failing_url_loader_factory.h
Original file line number Diff line number Diff line change
@@ -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<WebURLLoader> CreateURLLoader(
const WebURLRequest&,
std::unique_ptr<scheduler::WebResourceLoadingTaskRunnerHandle>) override;
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_FAILING_URL_LOADER_FACTORY_H_
1 change: 1 addition & 0 deletions third_party/blink/renderer/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/renderer/platform/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <memory>

#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<scheduler::WebResourceLoadingTaskRunnerHandle>
task_runner_handle)
: task_runner_handle_(std::move(task_runner_handle)) {}
~FailingLoader() override = default;

// WebURLLoader implementation:
void LoadSynchronously(
std::unique_ptr<network::ResourceRequest> request,
scoped_refptr<WebURLRequest::ExtraData> 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<WebURLError>& 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<network::ResourceRequest> request,
scoped_refptr<WebURLRequest::ExtraData> 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<base::SingleThreadTaskRunner> 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<scheduler::WebResourceLoadingTaskRunnerHandle>
task_runner_handle_;

// This must be the last member.
base::WeakPtrFactory<FailingLoader> weak_factory_{this};
};

} // namespace

std::unique_ptr<WebURLLoader> WebFailingURLLoaderFactory::CreateURLLoader(
const WebURLRequest&,
std::unique_ptr<scheduler::WebResourceLoadingTaskRunnerHandle> handle) {
return std::make_unique<FailingLoader>(std::move(handle));
}

} // namespace blink
12 changes: 12 additions & 0 deletions tools/metrics/histograms/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125592,6 +125592,18 @@ uploading your change for review.
</summary>
</histogram>

<histogram name="Printing.FrameIsActiveOnCreateLoaderFactory"
enum="BooleanEnabled" expires_after="2020-07-31">
<owner>yhirano@chromium.org</owner>
<owner>lukasza@chromium.org</owner>
<summary>
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.
</summary>
</histogram>

<histogram name="Printing.PrinterCapabilities" units="ms" expires_after="M80">
<owner>vkuzkokov@chromium.org</owner>
<owner>skau@chromium.org</owner>
Expand Down

0 comments on commit c45e93c

Please sign in to comment.