Initialize PDF SDK with policy in chrome_pdf::ConvertPdf*()
This CL sets a global enterprise policy variable from callers of `chrome_pdf::ConvertPdfPagesToNupPdf()` and `chrome_pdf::ConvertPdfDocumentToNupPdf()` before they are called, so that `ScopedSdkInitializer()` can initialize PDF SDK based on the enterprise policy. - Adds a new method `SetUseSkiaRendererPolicy()` to `PdfNupConverter` so that the policy value can be passed from the browser process to the printing services. - Adds a global variable `g_use_skia_renderer_enabled_by_policy` in pdf.cc so that it can store the enterprise policy status and be used by `ScopedSdkInitializer()`. - Adds TODO comments for `ScopedSdkInitializer()` direct callers which still need access to the enterprise policy values. Bug: 1440430 Change-Id: Iac1c75906c3ab304d6ec396de3c5d3b2f196ee98 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4529206 Commit-Queue: Nigi <nigi@chromium.org> Code-Coverage: Findit <findit-for-me@appspot.gserviceaccount.com> Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Cr-Commit-Position: refs/heads/main@{#1144845}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
106d571015
commit
11f2ca7209
chrome
browser
services
printing
pdf
@ -7,14 +7,24 @@
|
||||
#include <utility>
|
||||
|
||||
#include "base/functional/bind.h"
|
||||
#include "chrome/browser/pdf/pdf_pref_names.h"
|
||||
#include "chrome/browser/printing/printing_service.h"
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
#include "chrome/services/printing/public/mojom/pdf_nup_converter.mojom.h"
|
||||
#include "chrome/services/printing/public/mojom/printing_service.mojom.h"
|
||||
#include "components/prefs/pref_service.h"
|
||||
|
||||
namespace printing {
|
||||
|
||||
PdfNupConverterClient::PdfNupConverterClient(content::WebContents* web_contents)
|
||||
: content::WebContentsUserData<PdfNupConverterClient>(*web_contents) {}
|
||||
: content::WebContentsUserData<PdfNupConverterClient>(*web_contents) {
|
||||
const PrefService* prefs =
|
||||
Profile::FromBrowserContext(web_contents->GetBrowserContext())
|
||||
->GetPrefs();
|
||||
if (prefs && prefs->IsManagedPreference(prefs::kPdfUseSkiaRendererEnabled)) {
|
||||
skia_policy_ = prefs->GetBoolean(prefs::kPdfUseSkiaRendererEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
PdfNupConverterClient::~PdfNupConverterClient() {}
|
||||
|
||||
@ -80,6 +90,9 @@ PdfNupConverterClient::CreatePdfNupConverterRemote() {
|
||||
GetPrintingService()->BindPdfNupConverter(
|
||||
pdf_nup_converter.BindNewPipeAndPassReceiver());
|
||||
pdf_nup_converter->SetWebContentsURL(GetWebContents().GetLastCommittedURL());
|
||||
if (skia_policy_) {
|
||||
pdf_nup_converter->SetUseSkiaRendererPolicy(*skia_policy_);
|
||||
}
|
||||
return pdf_nup_converter;
|
||||
}
|
||||
|
||||
|
@ -63,6 +63,10 @@ class PdfNupConverterClient
|
||||
// mojo::Remote.
|
||||
std::map<int, mojo::Remote<mojom::PdfNupConverter>> pdf_nup_converter_map_;
|
||||
|
||||
// Indicates whether to use Skia renderer is enabled by enterprise policy.
|
||||
// A nullopt value indicates that such enterprise policy is not set.
|
||||
absl::optional<bool> skia_policy_;
|
||||
|
||||
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
||||
};
|
||||
|
||||
|
@ -102,4 +102,8 @@ void PdfNupConverter::SetWebContentsURL(const GURL& url) {
|
||||
crash_key.Set(url.spec());
|
||||
}
|
||||
|
||||
void PdfNupConverter::SetUseSkiaRendererPolicy(bool use_skia) {
|
||||
chrome_pdf::SetUseSkiaRendererPolicy(use_skia);
|
||||
}
|
||||
|
||||
} // namespace printing
|
||||
|
@ -34,6 +34,7 @@ class PdfNupConverter : public printing::mojom::PdfNupConverter {
|
||||
base::ReadOnlySharedMemoryRegion src_pdf_region,
|
||||
NupDocumentConvertCallback callback) override;
|
||||
void SetWebContentsURL(const GURL& url) override;
|
||||
void SetUseSkiaRendererPolicy(bool use_skia) override;
|
||||
};
|
||||
|
||||
} // namespace printing
|
||||
|
@ -59,4 +59,9 @@ interface PdfNupConverter {
|
||||
// Sets the URL which is committed in the main frame of the WebContents,
|
||||
// for use in crash diagnosis.
|
||||
SetWebContentsURL(url.mojom.Url url);
|
||||
|
||||
// Sets the status for enterprise policy `kPdfUseSkiaRendererEnabled`. It
|
||||
// should be called immediately once `mojom::NupPageConvert` remote is
|
||||
// created and only when the enterprise policy is managed.
|
||||
SetUseSkiaRendererPolicy(bool use_skia);
|
||||
};
|
||||
|
32
pdf/pdf.cc
32
pdf/pdf.cc
@ -13,6 +13,7 @@
|
||||
#include "pdf/pdf_engine.h"
|
||||
#include "pdf/pdf_features.h"
|
||||
#include "pdf/pdf_init.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/size_f.h"
|
||||
|
||||
@ -20,16 +21,17 @@ namespace chrome_pdf {
|
||||
|
||||
namespace {
|
||||
|
||||
absl::optional<bool> g_use_skia_renderer_enabled_by_policy;
|
||||
|
||||
class ScopedSdkInitializer {
|
||||
public:
|
||||
explicit ScopedSdkInitializer(bool enable_v8) {
|
||||
if (!IsSDKInitializedViaPlugin()) {
|
||||
// TODO(crbug.com/1440430): Modify ScopedSdkInitializer() so that the
|
||||
// option to use Skia can be based on enterprise policy if such policy is
|
||||
// set.
|
||||
InitializeSDK(enable_v8,
|
||||
base::FeatureList::IsEnabled(features::kPdfUseSkiaRenderer),
|
||||
FontMappingMode::kNoMapping);
|
||||
InitializeSDK(
|
||||
enable_v8,
|
||||
g_use_skia_renderer_enabled_by_policy.value_or(
|
||||
base::FeatureList::IsEnabled(features::kPdfUseSkiaRenderer)),
|
||||
FontMappingMode::kNoMapping);
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,7 +46,13 @@ class ScopedSdkInitializer {
|
||||
|
||||
} // namespace
|
||||
|
||||
void SetUseSkiaRendererPolicy(bool use_skia) {
|
||||
g_use_skia_renderer_enabled_by_policy = use_skia;
|
||||
}
|
||||
|
||||
#if BUILDFLAG(IS_CHROMEOS)
|
||||
// TODO(crbug.com/1440430): Make sure its callers set
|
||||
// `g_use_skia_renderer_enabled_by_policy` before calling this function.
|
||||
std::vector<uint8_t> CreateFlattenedPdf(
|
||||
base::span<const uint8_t> input_buffer) {
|
||||
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
|
||||
@ -53,6 +61,8 @@ std::vector<uint8_t> CreateFlattenedPdf(
|
||||
#endif // BUILDFLAG(IS_CHROMEOS)
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
// TODO(crbug.com/1440430): Make sure its callers set
|
||||
// `g_use_skia_renderer_enabled_by_policy` before calling this function.
|
||||
bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
|
||||
int page_index,
|
||||
HDC dc,
|
||||
@ -84,6 +94,8 @@ void SetPDFUsePrintMode(int mode) {
|
||||
}
|
||||
#endif // BUILDFLAG(IS_WIN)
|
||||
|
||||
// TODO(crbug.com/1440430): Make sure its callers set
|
||||
// `g_use_skia_renderer_enabled_by_policy` before calling this function.
|
||||
bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
|
||||
int* page_count,
|
||||
float* max_page_width) {
|
||||
@ -92,12 +104,16 @@ bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
|
||||
return engine_exports->GetPDFDocInfo(pdf_buffer, page_count, max_page_width);
|
||||
}
|
||||
|
||||
// TODO(crbug.com/1440430): Make sure its callers set
|
||||
// `g_use_skia_renderer_enabled_by_policy` before calling this function.
|
||||
absl::optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer) {
|
||||
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
|
||||
PDFEngineExports* engine_exports = PDFEngineExports::Get();
|
||||
return engine_exports->IsPDFDocTagged(pdf_buffer);
|
||||
}
|
||||
|
||||
// TODO(crbug.com/1440430): Make sure its callers set
|
||||
// `g_use_skia_renderer_enabled_by_policy` before calling this function.
|
||||
base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
|
||||
int page_index) {
|
||||
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
|
||||
@ -105,6 +121,8 @@ base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
|
||||
return engine_exports->GetPDFStructTreeForPage(pdf_buffer, page_index);
|
||||
}
|
||||
|
||||
// TODO(crbug.com/1440430): Make sure its callers set
|
||||
// `g_use_skia_renderer_enabled_by_policy` before calling this function.
|
||||
absl::optional<gfx::SizeF> GetPDFPageSizeByIndex(
|
||||
base::span<const uint8_t> pdf_buffer,
|
||||
int page_index) {
|
||||
@ -114,6 +132,8 @@ 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,
|
||||
|
@ -10,7 +10,6 @@
|
||||
#include "base/containers/span.h"
|
||||
#include "base/values.h"
|
||||
#include "build/build_config.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
#include <windows.h>
|
||||
@ -24,6 +23,8 @@ class SizeF;
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
void SetUseSkiaRendererPolicy(bool use_skia);
|
||||
|
||||
#if BUILDFLAG(IS_CHROMEOS)
|
||||
// Create a flattened PDF document from an existing PDF document.
|
||||
// `input_buffer` is the buffer that contains the entire PDF document to be
|
||||
|
Reference in New Issue
Block a user