Skip to content
Permalink
Browse files

M78: printing: Disable V8 when using PDF code for N-up conversion.

The input PDF for N-up conversion is one that is destined to go to a
printer. So it should not have any JS embedded. Given this, the PDF
module should not have to initialize V8 to interpret any JS. Besides
doing less work, this also avoid a crash on Mac, where security
hardening means the N-up conversion service process does not have the
entitlement for JIT which is required to initialize V8.

(cherry picked from commit 26df5ef)

Bug: 1000936
Change-Id: I2cd1f951f940fe4acf51624c0453e7de7a4edb64
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1799686
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#696068}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1808386
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/branch-heads/3904@{#267}
Cr-Branched-From: 675968a-refs/heads/master@{#693954}
  • Loading branch information
leizleiz committed Sep 17, 2019
1 parent a5b5429 commit 2adf73ac8568fe57764d654424aaa80d25497644
Showing with 31 additions and 16 deletions.
  1. +8 −8 pdf/pdf.cc
  2. +3 −1 pdf/pdf_engine.h
  3. +1 −1 pdf/pdf_ppapi.cc
  4. +17 −5 pdf/pdfium/pdfium_engine.cc
  5. +2 −1 pdf/pdfium/pdfium_test_base.cc
@@ -29,11 +29,11 @@ class ScopedSdkInitializer {
}

// Must be called.
bool Init() {
bool Init(bool enable_v8) {
#if DCHECK_IS_ON()
initialized_ = true;
#endif
return IsSDKInitializedViaPepper() || InitializeSDK();
return IsSDKInitializedViaPepper() || InitializeSDK(enable_v8);
}

private:
@@ -63,7 +63,7 @@ bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
bool autorotate,
bool use_color) {
ScopedSdkInitializer scoped_sdk_initializer;
if (!scoped_sdk_initializer.Init())
if (!scoped_sdk_initializer.Init(/*enable_v8=*/true))
return false;

PDFEngineExports* engine_exports = PDFEngineExports::Get();
@@ -94,7 +94,7 @@ bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int* page_count,
double* max_page_width) {
ScopedSdkInitializer scoped_sdk_initializer;
if (!scoped_sdk_initializer.Init())
if (!scoped_sdk_initializer.Init(/*enable_v8=*/true))
return false;

PDFEngineExports* engine_exports = PDFEngineExports::Get();
@@ -106,7 +106,7 @@ bool GetPDFPageSizeByIndex(base::span<const uint8_t> pdf_buffer,
double* width,
double* height) {
ScopedSdkInitializer scoped_sdk_initializer;
if (!scoped_sdk_initializer.Init())
if (!scoped_sdk_initializer.Init(/*enable_v8=*/true))
return false;

chrome_pdf::PDFEngineExports* engine_exports =
@@ -125,7 +125,7 @@ bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
bool autorotate,
bool use_color) {
ScopedSdkInitializer scoped_sdk_initializer;
if (!scoped_sdk_initializer.Init())
if (!scoped_sdk_initializer.Init(/*enable_v8=*/true))
return false;

PDFEngineExports* engine_exports = PDFEngineExports::Get();
@@ -142,7 +142,7 @@ std::vector<uint8_t> ConvertPdfPagesToNupPdf(
const gfx::Size& page_size,
const gfx::Rect& printable_area) {
ScopedSdkInitializer scoped_sdk_initializer;
if (!scoped_sdk_initializer.Init())
if (!scoped_sdk_initializer.Init(/*enable_v8=*/false))
return std::vector<uint8_t>();

PDFEngineExports* engine_exports = PDFEngineExports::Get();
@@ -156,7 +156,7 @@ std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
const gfx::Size& page_size,
const gfx::Rect& printable_area) {
ScopedSdkInitializer scoped_sdk_initializer;
if (!scoped_sdk_initializer.Init())
if (!scoped_sdk_initializer.Init(/*enable_v8=*/false))
return std::vector<uint8_t>();

PDFEngineExports* engine_exports = PDFEngineExports::Get();
@@ -56,7 +56,9 @@ class VarDictionary;
namespace chrome_pdf {

// Do one time initialization of the SDK.
bool InitializeSDK();
// If |enable_v8| is false, then the PDFEngine will not be able to run
// JavaScript.
bool InitializeSDK(bool enable_v8);
// Tells the SDK that we're shutting down.
void ShutdownSDK();

@@ -52,7 +52,7 @@ pp::Instance* PDFModule::CreateInstance(PP_Instance instance) {
v8::V8::SetNativesDataBlob(&natives);
v8::V8::SetSnapshotDataBlob(&snapshot);
}
if (!InitializeSDK())
if (!InitializeSDK(/*enable_v8=*/true))
return nullptr;
g_sdk_initialized_via_pepper = true;
}
@@ -239,6 +239,10 @@ std::string GetDocumentMetadata(FPDF_DOCUMENT doc, const std::string& key) {

gin::IsolateHolder* g_isolate_holder = nullptr;

bool IsV8Initialized() {
return !!g_isolate_holder;
}

void SetUpV8() {
const char* recommended = FPDF_GetRecommendedV8Flags();
v8::V8::SetFlagsFromString(recommended, strlen(recommended));
@@ -360,13 +364,17 @@ wchar_t SimplifyForSearch(wchar_t c) {

} // namespace

bool InitializeSDK() {
SetUpV8();

bool InitializeSDK(bool enable_v8) {
FPDF_LIBRARY_CONFIG config;
config.version = 2;
config.m_pUserFontPaths = nullptr;
config.m_pIsolate = v8::Isolate::GetCurrent();

if (enable_v8) {
SetUpV8();
config.m_pIsolate = v8::Isolate::GetCurrent();
} else {
config.m_pIsolate = nullptr;
}
config.m_v8EmbedderSlot = gin::kEmbedderPDFium;
FPDF_InitLibraryWithConfig(&config);

@@ -381,7 +389,8 @@ bool InitializeSDK() {

void ShutdownSDK() {
FPDF_DestroyLibrary();
TearDownV8();
if (IsV8Initialized())
TearDownV8();
}

std::unique_ptr<PDFEngine> PDFEngine::Create(PDFEngine::Client* client,
@@ -395,6 +404,9 @@ PDFiumEngine::PDFiumEngine(PDFEngine::Client* client, bool enable_javascript)
mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA,
PDFiumPage::LinkTarget()),
print_(this) {
if (enable_javascript)
DCHECK(IsV8Initialized());

find_factory_.Initialize(this);
password_factory_.Initialize(this);

@@ -62,7 +62,8 @@ std::unique_ptr<PDFiumEngine> PDFiumTestBase::InitializeEngine(
const base::FilePath::CharType* pdf_name) {
SetDocumentForTest(pdf_name);
pp::URLLoader dummy_loader;
auto engine = std::make_unique<PDFiumEngine>(client, true);
auto engine =
std::make_unique<PDFiumEngine>(client, /*enable_javascript=*/false);
if (!engine->New("https://chromium.org/dummy.pdf", "") ||
!engine->HandleDocumentLoad(dummy_loader)) {
return nullptr;

0 comments on commit 2adf73a

Please sign in to comment.
You can’t perform that action at this time.