0

Explicitly prevent unsupported feature reporting in PDFiumEngineExports.

When PDFium code is running in utility processes via
PDFiumEngineExports, unsupported PDF features are not reported via UMA,
because that would interfere with the PDF Viewer's UMA numbers. This is
all fine until ScopedUnsupportedFeature hits a NOTREACHED() because it
tries to report the unsupported PDF features and notices there is no
PDFiumEngine to report to.

Add a way to initialize ScopedUnsupportedFeature so that it knows there
is no PDFiumEngine, and use this mechanism everywhere in
PDFiumEngineExports.

Bug: 1229726
Change-Id: I836dc45e93401bc883a584a19b7107994386ab33
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3031988
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#902254}
This commit is contained in:
Lei Zhang
2021-07-16 01:16:14 +00:00
committed by Chromium LUCI CQ
parent a1df82a91e
commit 666e4d325a
3 changed files with 42 additions and 5 deletions

@ -13,6 +13,7 @@
#include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
#include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
#include "pdf/pdfium/pdfium_print.h"
#include "pdf/pdfium/pdfium_unsupported_features.h"
#include "printing/nup_parameters.h"
#include "printing/units.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
@ -229,6 +230,8 @@ PDFiumEngineExports::~PDFiumEngineExports() {}
#if defined(OS_CHROMEOS)
std::vector<uint8_t> PDFiumEngineExports::CreateFlattenedPdf(
base::span<const uint8_t> input_buffer) {
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(input_buffer);
if (!doc)
return std::vector<uint8_t>();
@ -242,6 +245,8 @@ bool PDFiumEngineExports::RenderPDFPageToDC(
int page_number,
const RenderingSettings& settings,
HDC dc) {
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
if (!doc)
return false;
@ -326,6 +331,8 @@ bool PDFiumEngineExports::RenderPDFPageToBitmap(
int page_number,
const RenderingSettings& settings,
void* bitmap_buffer) {
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
if (!doc)
return false;
@ -359,6 +366,8 @@ std::vector<uint8_t> PDFiumEngineExports::ConvertPdfPagesToNupPdf(
if (!IsValidPrintableArea(page_size, printable_area))
return std::vector<uint8_t>();
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = CreatePdfDoc(std::move(input_buffers));
if (!doc)
return std::vector<uint8_t>();
@ -375,6 +384,8 @@ std::vector<uint8_t> PDFiumEngineExports::ConvertPdfDocumentToNupPdf(
if (!IsValidPrintableArea(page_size, printable_area))
return std::vector<uint8_t>();
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(input_buffer);
if (!doc)
return std::vector<uint8_t>();
@ -386,6 +397,8 @@ std::vector<uint8_t> PDFiumEngineExports::ConvertPdfDocumentToNupPdf(
bool PDFiumEngineExports::GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int* page_count,
float* max_page_width) {
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
if (!doc)
return false;
@ -412,6 +425,8 @@ bool PDFiumEngineExports::GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
absl::optional<bool> PDFiumEngineExports::IsPDFDocTagged(
base::span<const uint8_t> pdf_buffer) {
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
if (!doc)
return absl::nullopt;
@ -422,6 +437,8 @@ absl::optional<bool> PDFiumEngineExports::IsPDFDocTagged(
base::Value PDFiumEngineExports::GetPDFStructTreeForPage(
base::span<const uint8_t> pdf_buffer,
int page_index) {
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
if (!doc)
return base::Value(base::Value::Type::NONE);
@ -450,6 +467,8 @@ base::Value PDFiumEngineExports::GetPDFStructTreeForPage(
absl::optional<gfx::SizeF> PDFiumEngineExports::GetPDFPageSizeByIndex(
base::span<const uint8_t> pdf_buffer,
int page_number) {
ScopedUnsupportedFeature scoped_unsupported_feature(
ScopedUnsupportedFeature::kNoEngine);
ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
if (!doc)
return absl::nullopt;

@ -4,7 +4,7 @@
#include "pdf/pdfium/pdfium_unsupported_features.h"
#include "base/notreached.h"
#include "base/check.h"
#include "pdf/pdfium/pdfium_engine.h"
#include "third_party/pdfium/public/fpdf_ext.h"
@ -12,11 +12,13 @@ namespace chrome_pdf {
namespace {
// Only check `g_engine_for_unsupported` if `g_engine_available` is true.
bool g_engine_available = true;
PDFiumEngine* g_engine_for_unsupported = nullptr;
void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
if (!g_engine_for_unsupported) {
NOTREACHED();
DCHECK(!g_engine_available);
return;
}
@ -73,12 +75,23 @@ void InitializeUnsupportedFeaturesHandler() {
}
ScopedUnsupportedFeature::ScopedUnsupportedFeature(PDFiumEngine* engine)
: old_engine_(g_engine_for_unsupported) {
: saved_engine_available_(g_engine_available),
saved_engine_(g_engine_for_unsupported) {
DCHECK(engine);
g_engine_available = true;
g_engine_for_unsupported = engine;
}
ScopedUnsupportedFeature::ScopedUnsupportedFeature(NoEngine no_engine)
: saved_engine_available_(g_engine_available),
saved_engine_(g_engine_for_unsupported) {
g_engine_available = false;
g_engine_for_unsupported = nullptr;
}
ScopedUnsupportedFeature::~ScopedUnsupportedFeature() {
g_engine_for_unsupported = old_engine_;
g_engine_for_unsupported = saved_engine_;
g_engine_available = saved_engine_available_;
}
} // namespace chrome_pdf

@ -15,13 +15,18 @@ void InitializeUnsupportedFeaturesHandler();
// our global callback when an unsupported feature is reached.
class ScopedUnsupportedFeature {
public:
// For use by PDFiumEngineExports when there is no PDFiumEngine instance.
enum NoEngine { kNoEngine };
explicit ScopedUnsupportedFeature(PDFiumEngine* engine);
explicit ScopedUnsupportedFeature(NoEngine no_engine);
ScopedUnsupportedFeature(const ScopedUnsupportedFeature&) = delete;
ScopedUnsupportedFeature& operator=(const ScopedUnsupportedFeature&) = delete;
~ScopedUnsupportedFeature();
private:
PDFiumEngine* const old_engine_;
const bool saved_engine_available_;
PDFiumEngine* const saved_engine_;
};
} // namespace chrome_pdf