Initialize PDF SDK with policy in chrome_pdf::RenderPDFPageToBitmap()
This CL adds a new method `SetUseSkiaRendererPolicy()` to mojom interface `PdfThumbnailer` and `PdfToPwgRasterConverter` so that the enterprise policy regarding Skia as PDF renderer is set as soon as the remote is bound to the printing service. Bug: 1440430 Change-Id: I16efed03674b20f7601ca5e3fe91b467e99a454f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4533928 Reviewed-by: Lei Zhang <thestig@chromium.org> Reviewed-by: Matthew Denton <mpdenton@chromium.org> Commit-Queue: Nigi <nigi@chromium.org> Cr-Commit-Position: refs/heads/main@{#1145082}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
3112a2adfe
commit
3c6349195b
chrome
browser
ash
printing
ui
services
pdf
@ -3799,6 +3799,7 @@ source_set("ash") {
|
||||
"//chrome/browser/nearby_sharing/common",
|
||||
"//chrome/browser/nearby_sharing/logging",
|
||||
"//chrome/browser/nearby_sharing/public/cpp",
|
||||
"//chrome/browser/pdf:pdf_pref_names",
|
||||
"//chrome/browser/policy:onc",
|
||||
"//chrome/browser/profiles",
|
||||
"//chrome/browser/resources:component_extension_resources",
|
||||
|
@ -16,11 +16,13 @@
|
||||
#include "chrome/browser/ash/extensions/file_manager/private_api_util.h"
|
||||
#include "chrome/browser/ash/file_manager/fileapi_util.h"
|
||||
#include "chrome/browser/ash/file_manager/path_util.h"
|
||||
#include "chrome/browser/pdf/pdf_pref_names.h"
|
||||
#include "chrome/browser/printing/printing_service.h"
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
#include "chrome/common/extensions/api/file_manager_private.h"
|
||||
#include "chrome/common/extensions/api/file_manager_private_internal.h"
|
||||
#include "chrome/services/printing/public/mojom/printing_service.mojom.h"
|
||||
#include "components/prefs/pref_service.h"
|
||||
#include "components/signin/public/base/consent_level.h"
|
||||
#include "content/public/browser/browser_task_traits.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
@ -254,6 +256,12 @@ void FileManagerPrivateInternalGetPdfThumbnailFunction::FetchThumbnail(
|
||||
pdf_thumbnailer_.set_disconnect_handler(base::BindOnce(
|
||||
&FileManagerPrivateInternalGetPdfThumbnailFunction::ThumbnailDisconnected,
|
||||
base::Unretained(this)));
|
||||
const PrefService* prefs =
|
||||
Profile::FromBrowserContext(browser_context())->GetPrefs();
|
||||
if (prefs && prefs->IsManagedPreference(prefs::kPdfUseSkiaRendererEnabled)) {
|
||||
pdf_thumbnailer_->SetUseSkiaRendererPolicy(
|
||||
prefs->GetBoolean(prefs::kPdfUseSkiaRendererEnabled));
|
||||
}
|
||||
auto params = printing::mojom::ThumbParams::New(
|
||||
/*size_px=*/size, /*dpi=*/gfx::Size(kDpi, kDpi),
|
||||
/*stretch_to_bounds=*/false, /*keep_aspect_ratio=*/true);
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "printing/pdf_render_settings.h"
|
||||
#include "printing/pwg_raster_settings.h"
|
||||
#include "printing/units.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
|
||||
@ -47,7 +48,8 @@ class PwgRasterConverterHelper
|
||||
PwgRasterConverterHelper(const PwgRasterConverterHelper&) = delete;
|
||||
PwgRasterConverterHelper& operator=(const PwgRasterConverterHelper&) = delete;
|
||||
|
||||
void Convert(const base::RefCountedMemory* data,
|
||||
void Convert(const absl::optional<bool>& use_skia,
|
||||
const base::RefCountedMemory* data,
|
||||
PwgRasterConverter::ResultCallback callback);
|
||||
|
||||
private:
|
||||
@ -77,6 +79,7 @@ PwgRasterConverterHelper::~PwgRasterConverterHelper() {
|
||||
}
|
||||
|
||||
void PwgRasterConverterHelper::Convert(
|
||||
const absl::optional<bool>& use_skia,
|
||||
const base::RefCountedMemory* data,
|
||||
PwgRasterConverter::ResultCallback callback) {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||
@ -96,6 +99,10 @@ void PwgRasterConverterHelper::Convert(
|
||||
return;
|
||||
}
|
||||
|
||||
if (use_skia) {
|
||||
pdf_to_pwg_raster_converter_remote_->SetUseSkiaRendererPolicy(*use_skia);
|
||||
}
|
||||
|
||||
// TODO(thestig): Write `data` into shared memory in the first place, to avoid
|
||||
// this memcpy().
|
||||
memcpy(memory.mapping.memory(), data->front(), data->size());
|
||||
@ -132,7 +139,8 @@ class PwgRasterConverterImpl : public PwgRasterConverter {
|
||||
|
||||
~PwgRasterConverterImpl() override;
|
||||
|
||||
void Start(const base::RefCountedMemory* data,
|
||||
void Start(const absl::optional<bool>& use_skia,
|
||||
const base::RefCountedMemory* data,
|
||||
const PdfRenderSettings& conversion_settings,
|
||||
const PwgRasterSettings& bitmap_settings,
|
||||
ResultCallback callback) override;
|
||||
@ -149,14 +157,15 @@ PwgRasterConverterImpl::PwgRasterConverterImpl() = default;
|
||||
|
||||
PwgRasterConverterImpl::~PwgRasterConverterImpl() = default;
|
||||
|
||||
void PwgRasterConverterImpl::Start(const base::RefCountedMemory* data,
|
||||
void PwgRasterConverterImpl::Start(const absl::optional<bool>& use_skia,
|
||||
const base::RefCountedMemory* data,
|
||||
const PdfRenderSettings& conversion_settings,
|
||||
const PwgRasterSettings& bitmap_settings,
|
||||
ResultCallback callback) {
|
||||
cancelable_callback_.Reset(std::move(callback));
|
||||
utility_client_ = base::MakeRefCounted<PwgRasterConverterHelper>(
|
||||
conversion_settings, bitmap_settings);
|
||||
utility_client_->Convert(data, cancelable_callback_.callback());
|
||||
utility_client_->Convert(use_skia, data, cancelable_callback_.callback());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -49,7 +49,8 @@ class PwgRasterConverter {
|
||||
const cloud_devices::CloudDeviceDescription& printer_capabilities,
|
||||
const cloud_devices::CloudDeviceDescription& ticket);
|
||||
|
||||
virtual void Start(const base::RefCountedMemory* data,
|
||||
virtual void Start(const absl::optional<bool>& use_skia,
|
||||
const base::RefCountedMemory* data,
|
||||
const PdfRenderSettings& conversion_settings,
|
||||
const PwgRasterSettings& bitmap_settings,
|
||||
ResultCallback callback) = 0;
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "printing/mojom/print.mojom.h"
|
||||
#include "printing/pdf_render_settings.h"
|
||||
#include "printing/pwg_raster_settings.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
|
||||
namespace printing {
|
||||
|
||||
@ -122,7 +123,8 @@ class PdfToPwgRasterBrowserTest : public InProcessBrowserTest {
|
||||
base::ReadOnlySharedMemoryRegion* pwg_region) {
|
||||
bool called = false;
|
||||
base::RunLoop run_loop;
|
||||
converter_->Start(pdf_data, conversion_settings, bitmap_settings,
|
||||
converter_->Start(/*use_skia=*/absl::nullopt, pdf_data, conversion_settings,
|
||||
bitmap_settings,
|
||||
base::BindOnce(&ResultCallbackImpl, &called, pwg_region,
|
||||
run_loop.QuitClosure()));
|
||||
run_loop.Run();
|
||||
|
@ -6061,6 +6061,7 @@ static_library("ui") {
|
||||
"webui/print_preview/printer_handler.h",
|
||||
]
|
||||
deps += [
|
||||
"//chrome/browser/pdf:pdf_pref_names",
|
||||
"//chrome/common/printing",
|
||||
"//chrome/services/printing/public/mojom",
|
||||
"//components/printing/common:mojo_interfaces",
|
||||
|
@ -15,12 +15,14 @@
|
||||
#include "base/strings/string_split.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "base/values.h"
|
||||
#include "chrome/browser/pdf/pdf_pref_names.h"
|
||||
#include "chrome/browser/printing/pwg_raster_converter.h"
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
#include "chrome/browser/ui/webui/print_preview/print_preview_utils.h"
|
||||
#include "components/cloud_devices/common/cloud_device_description.h"
|
||||
#include "components/cloud_devices/common/printer_description.h"
|
||||
#include "components/device_event_log/device_event_log.h"
|
||||
#include "components/prefs/pref_service.h"
|
||||
#include "extensions/browser/api/device_permissions_manager.h"
|
||||
#include "extensions/browser/api/printer_provider/printer_provider_api.h"
|
||||
#include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
|
||||
@ -250,8 +252,14 @@ void ExtensionPrinterHandler::ConvertToPWGRaster(
|
||||
PwgRasterSettings bitmap_settings =
|
||||
PwgRasterConverter::GetBitmapSettings(printer_description, print_ticket);
|
||||
|
||||
absl::optional<bool> use_skia;
|
||||
const PrefService* prefs = profile_->GetPrefs();
|
||||
if (prefs && prefs->IsManagedPreference(prefs::kPdfUseSkiaRendererEnabled)) {
|
||||
use_skia = prefs->GetBoolean(prefs::kPdfUseSkiaRendererEnabled);
|
||||
}
|
||||
|
||||
pwg_raster_converter_->Start(
|
||||
data.get(),
|
||||
use_skia, data.get(),
|
||||
PwgRasterConverter::GetConversionSettings(printer_description, page_size,
|
||||
bitmap_settings.use_color),
|
||||
bitmap_settings,
|
||||
|
@ -303,7 +303,8 @@ class FakePwgRasterConverter : public PwgRasterConverter {
|
||||
|
||||
// PwgRasterConverter implementation. It writes |data| to shared memory.
|
||||
// Also, remembers conversion and bitmap settings passed into the method.
|
||||
void Start(const base::RefCountedMemory* data,
|
||||
void Start(const absl::optional<bool>& use_skia,
|
||||
const base::RefCountedMemory* data,
|
||||
const PdfRenderSettings& conversion_settings,
|
||||
const PwgRasterSettings& bitmap_settings,
|
||||
ResultCallback callback) override {
|
||||
|
@ -82,4 +82,8 @@ void PdfThumbnailer::GetThumbnail(printing::mojom::ThumbParamsPtr params,
|
||||
std::move(callback).Run(result);
|
||||
}
|
||||
|
||||
void PdfThumbnailer::SetUseSkiaRendererPolicy(bool use_skia) {
|
||||
chrome_pdf::SetUseSkiaRendererPolicy(use_skia);
|
||||
}
|
||||
|
||||
} // namespace printing
|
||||
|
@ -25,6 +25,7 @@ class PdfThumbnailer : public printing::mojom::PdfThumbnailer {
|
||||
void GetThumbnail(printing::mojom::ThumbParamsPtr params,
|
||||
base::ReadOnlySharedMemoryRegion pdf_region,
|
||||
GetThumbnailCallback callback) override;
|
||||
void SetUseSkiaRendererPolicy(bool use_skia) override;
|
||||
|
||||
// The maximum width of a thumbnail we accept. If the specified width
|
||||
// exceeds the maximum, an empty, invalid bitmap is returned.
|
||||
|
@ -175,6 +175,36 @@ TEST_F(PdfThumbnailerTest, TooLargeThumbnailSize) {
|
||||
run_loop.Run();
|
||||
EXPECT_TRUE(bitmap_.isNull());
|
||||
}
|
||||
|
||||
TEST_F(PdfThumbnailerTest, CreatePdfThumbnailWithSkiaPolicyEnabled) {
|
||||
base::RunLoop run_loop;
|
||||
auto pdf_region = CreatePdfRegion(kPdfContent);
|
||||
|
||||
thumbnailer_.SetUseSkiaRendererPolicy(/*use_skia=*/true);
|
||||
thumbnailer_.GetThumbnail(
|
||||
std::move(params_), std::move(pdf_region),
|
||||
base::BindOnce(&PdfThumbnailerTest::StoreResult,
|
||||
weak_factory_.GetWeakPtr(), run_loop.QuitClosure()));
|
||||
run_loop.Run();
|
||||
EXPECT_FALSE(bitmap_.isNull());
|
||||
EXPECT_EQ(kThumbWidth, bitmap_.width());
|
||||
EXPECT_EQ(kThumbHeight, bitmap_.height());
|
||||
}
|
||||
|
||||
TEST_F(PdfThumbnailerTest, CreatePdfThumbnailWithSkiaPolicyDisabled) {
|
||||
base::RunLoop run_loop;
|
||||
auto pdf_region = CreatePdfRegion(kPdfContent);
|
||||
|
||||
thumbnailer_.SetUseSkiaRendererPolicy(/*use_skia=*/false);
|
||||
thumbnailer_.GetThumbnail(
|
||||
std::move(params_), std::move(pdf_region),
|
||||
base::BindOnce(&PdfThumbnailerTest::StoreResult,
|
||||
weak_factory_.GetWeakPtr(), run_loop.QuitClosure()));
|
||||
run_loop.Run();
|
||||
EXPECT_FALSE(bitmap_.isNull());
|
||||
EXPECT_EQ(kThumbWidth, bitmap_.width());
|
||||
EXPECT_EQ(kThumbHeight, bitmap_.height());
|
||||
}
|
||||
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
|
||||
|
||||
} // namespace printing
|
||||
|
@ -147,4 +147,8 @@ void PdfToPwgRasterConverter::Convert(
|
||||
std::move(callback).Run(std::move(region), page_count);
|
||||
}
|
||||
|
||||
void PdfToPwgRasterConverter::SetUseSkiaRendererPolicy(bool use_skia) {
|
||||
chrome_pdf::SetUseSkiaRendererPolicy(use_skia);
|
||||
}
|
||||
|
||||
} // namespace printing
|
||||
|
@ -27,6 +27,7 @@ class PdfToPwgRasterConverter
|
||||
const PdfRenderSettings& pdf_settings,
|
||||
const PwgRasterSettings& pwg_raster_settings,
|
||||
ConvertCallback callback) override;
|
||||
void SetUseSkiaRendererPolicy(bool use_skia) override;
|
||||
};
|
||||
|
||||
} // namespace printing
|
||||
|
@ -33,4 +33,9 @@ interface PdfThumbnailer {
|
||||
GetThumbnail(ThumbParams params,
|
||||
mojo_base.mojom.ReadOnlySharedMemoryRegion pdf_region)
|
||||
=> (skia.mojom.BitmapN32? bitmap);
|
||||
|
||||
// Sets the status for enterprise policy `kPdfUseSkiaRendererEnabled`. It
|
||||
// should be called immediately once `mojom::PdfThumbnailer` remote is bound
|
||||
// and only when this policy is managed.
|
||||
SetUseSkiaRendererPolicy(bool use_skia);
|
||||
};
|
||||
|
@ -44,4 +44,9 @@ interface PdfToPwgRasterConverter {
|
||||
PwgRasterSettings pwg_raster_settings)
|
||||
=> (mojo_base.mojom.ReadOnlySharedMemoryRegion? pwg_raster_region,
|
||||
uint32 page_count);
|
||||
|
||||
// Sets the status for enterprise policy `kPdfUseSkiaRendererEnabled`. It
|
||||
// should be called immediately once `mojom::PdfToPwgRasterConverter` remote
|
||||
// is bound and only when this policy is managed.
|
||||
SetUseSkiaRendererPolicy(bool use_skia);
|
||||
};
|
||||
|
@ -132,8 +132,6 @@ absl::optional<gfx::SizeF> GetPDFPageSizeByIndex(
|
||||
return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, page_index);
|
||||
}
|
||||
|
||||
// TODO(crbug.com/1440430): Make sure its callers set
|
||||
// `g_use_skia_renderer_enabled_by_policy` before calling this function.
|
||||
bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
|
||||
int page_index,
|
||||
void* bitmap_buffer,
|
||||
|
Reference in New Issue
Block a user