From b0fb8cc23c0ae555726f873101961676f96f6f07 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 8 Feb 2018 14:01:32 +0000 Subject: [v8-platform] Store the platform in a unique_ptr. (Try 2) We want to change the signature of {CreateDefaultPlatform} in the V8 API to return a unique_ptr instead of a raw pointer to indicate that the caller owns the platform. With this change we prepare pdfium for this change. In this second attempt, keep the old InitializeV8ForPDFium() method around to complete the Chromium DEPS roll. Once that lands safely, remove it from PDFium. This relands commit 608e8dd6 and commit 3355f459, which commit b3a3eaab reverts. Change-Id: Icc60b17ca202637d34ae242c0785d939194d0fe6 Reviewed-on: https://pdfium-review.googlesource.com/25950 Commit-Queue: dsinclair Reviewed-by: dsinclair --- testing/embedder_test_main.cpp | 13 ++++++----- testing/test_support.cpp | 49 +++++++++++++++++++++++++++++++----------- testing/test_support.h | 14 ++++++++++++ 3 files changed, 56 insertions(+), 20 deletions(-) (limited to 'testing') diff --git a/testing/embedder_test_main.cpp b/testing/embedder_test_main.cpp index ade6118e64..0c3ceb60ce 100644 --- a/testing/embedder_test_main.cpp +++ b/testing/embedder_test_main.cpp @@ -34,16 +34,16 @@ class Environment : public testing::Environment { #ifdef PDF_ENABLE_V8 #ifdef V8_USE_EXTERNAL_STARTUP_DATA if (g_v8_natives && g_v8_snapshot) { - InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr, - &platform_); + platform_ = InitializeV8ForPDFiumWithStartupData( + g_exe_path, std::string(), nullptr, nullptr); } else { g_v8_natives = new v8::StartupData; g_v8_snapshot = new v8::StartupData; - InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives, - g_v8_snapshot, &platform_); + platform_ = InitializeV8ForPDFiumWithStartupData( + g_exe_path, std::string(), g_v8_natives, g_v8_snapshot); } #else - InitializeV8ForPDFium(g_exe_path, &platform_); + platform_ = InitializeV8ForPDFium(g_exe_path); #endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // FPDF_ENABLE_V8 } @@ -51,13 +51,12 @@ class Environment : public testing::Environment { void TearDown() override { #ifdef PDF_ENABLE_V8 v8::V8::ShutdownPlatform(); - delete platform_; #endif // PDF_ENABLE_V8 } private: #ifdef PDF_ENABLE_V8 - v8::Platform* platform_; + std::unique_ptr platform_; #endif // PDF_ENABLE_V8 }; diff --git a/testing/test_support.cpp b/testing/test_support.cpp index b32ec7de96..af7f3d02ad 100644 --- a/testing/test_support.cpp +++ b/testing/test_support.cpp @@ -60,17 +60,18 @@ bool GetExternalData(const std::string& exe_path, } #endif // V8_USE_EXTERNAL_STARTUP_DATA -void InitializeV8Common(const char* exe_path, v8::Platform** platform) { - v8::V8::InitializeICUDefaultLocation(exe_path); +std::unique_ptr InitializeV8Common(const std::string& exe_path) { + v8::V8::InitializeICUDefaultLocation(exe_path.c_str()); - *platform = v8::platform::CreateDefaultPlatform(); - v8::V8::InitializePlatform(*platform); + std::unique_ptr platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); // By enabling predictable mode, V8 won't post any background tasks. // By enabling GC, it makes it easier to chase use-after-free. const char v8_flags[] = "--predictable --expose-gc"; v8::V8::SetFlagsFromString(v8_flags, static_cast(strlen(v8_flags))); v8::V8::Initialize(); + return platform; } #endif // PDF_ENABLE_V8 @@ -177,26 +178,48 @@ std::string GenerateMD5Base16(const uint8_t* data, uint32_t size) { #ifdef PDF_ENABLE_V8 #ifdef V8_USE_EXTERNAL_STARTUP_DATA -bool InitializeV8ForPDFium(const std::string& exe_path, - const std::string& bin_dir, - v8::StartupData* natives_blob, - v8::StartupData* snapshot_blob, - v8::Platform** platform) { - InitializeV8Common(exe_path.c_str(), platform); +std::unique_ptr InitializeV8ForPDFiumWithStartupData( + const std::string& exe_path, + const std::string& bin_dir, + v8::StartupData* natives_blob, + v8::StartupData* snapshot_blob) { + std::unique_ptr platform = InitializeV8Common(exe_path); if (natives_blob && snapshot_blob) { if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob)) - return false; + return nullptr; if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob)) - return false; + return nullptr; v8::V8::SetNativesDataBlob(natives_blob); v8::V8::SetSnapshotDataBlob(snapshot_blob); } + return platform; +} + +bool InitializeV8ForPDFium(const std::string& exe_path, + const std::string& bin_dir, + v8::StartupData* natives_blob, + v8::StartupData* snapshot_blob, + v8::Platform** platform) { + std::unique_ptr new_platform = + InitializeV8ForPDFiumWithStartupData(exe_path, bin_dir, natives_blob, + snapshot_blob); + if (!new_platform) + return false; + *platform = new_platform.release(); return true; } #else // V8_USE_EXTERNAL_STARTUP_DATA +std::unique_ptr InitializeV8ForPDFium( + const std::string& exe_path) { + return InitializeV8Common(exe_path); +} + bool InitializeV8ForPDFium(const std::string& exe_path, v8::Platform** platform) { - InitializeV8Common(exe_path.c_str(), platform); + std::unique_ptr new_platform = InitializeV8ForPDFium(exe_path); + if (!new_platform) + return false; + *platform = new_platform.release(); return true; } #endif // V8_USE_EXTERNAL_STARTUP_DATA diff --git a/testing/test_support.h b/testing/test_support.h index ec4b4aeacb..00d72b35d1 100644 --- a/testing/test_support.h +++ b/testing/test_support.h @@ -91,12 +91,26 @@ class Platform; namespace v8 { class StartupData; } + +// |natives_blob| and |snapshot_blob| are optional out parameters. They should +// either both be valid or both be nullptrs. +std::unique_ptr InitializeV8ForPDFiumWithStartupData( + const std::string& exe_path, + const std::string& bin_dir, + v8::StartupData* natives_blob, + v8::StartupData* snapshot_blob); + +// TODO(thestig): Remove this in the near future once it has no callers. bool InitializeV8ForPDFium(const std::string& exe_path, const std::string& bin_dir, v8::StartupData* natives_blob, v8::StartupData* snapshot_blob, v8::Platform** platform); #else // V8_USE_EXTERNAL_STARTUP_DATA +std::unique_ptr InitializeV8ForPDFium( + const std::string& exe_path); + +// TODO(thestig): Remove this in the near future once it has no callers. bool InitializeV8ForPDFium(const std::string& exe_path, v8::Platform** platform); #endif // V8_USE_EXTERNAL_STARTUP_DATA -- cgit v1.2.3