0

Initialize V8 in PDFium from external files when plugin is running in the renderer process.

BUG=421063

Review URL: https://codereview.chromium.org/705623002

Cr-Commit-Position: refs/heads/master@{#304020}
This commit is contained in:
baixo
2014-11-13 04:41:04 -08:00
committed by Commit bot
parent f881f652f0
commit f28960dabf
11 changed files with 86 additions and 9 deletions

@ -38,6 +38,7 @@
'type': 'static_library',
'dependencies': [
'<(DEPTH)/content/content.gyp:content_renderer',
'<(DEPTH)/gin/gin.gyp:gin',
'<(DEPTH)/ppapi/ppapi_internal.gyp:ppapi_shared',
'<(DEPTH)/third_party/icu/icu.gyp:icuuc',
'<(DEPTH)/third_party/icu/icu.gyp:icui18n',

@ -20,6 +20,7 @@ static_library("renderer") {
"//components/resources:components_resources",
"//components/strings",
"//content/public/renderer",
"//gin",
"//ppapi:ppapi_shared",
"//third_party/icu",
"//v8",

@ -1,5 +1,6 @@
include_rules = [
"+components/strings/grit/components_strings.h",
"+gin",
"+grit/components_scaled_resources.h",
"+skia/ext",
"+ui/gfx",

@ -16,6 +16,7 @@
#include "content/public/renderer/pepper_plugin_instance.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h"
#include "gin/public/isolate_holder.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/private/ppb_pdf.h"
#include "ppapi/c/trusted/ppb_browser_font_trusted.h"
@ -329,6 +330,14 @@ void SetLinkUnderCursor(PP_Instance instance_id, const char* url) {
instance->SetLinkUnderCursor(url);
}
void GetV8ExternalSnapshotData(const char** natives_data_out,
int* natives_size_out,
const char** snapshot_data_out,
int* snapshot_size_out) {
gin::IsolateHolder::GetV8ExternalSnapshotData(natives_data_out,
natives_size_out, snapshot_data_out, snapshot_size_out);
}
const PPB_PDF ppb_pdf = { //
&GetLocalizedString, //
&GetResourceImage, //
@ -349,6 +358,7 @@ const PPB_PDF ppb_pdf = { //
&IsOutOfProcess, //
&SetSelectedText, //
&SetLinkUnderCursor, //
&GetV8ExternalSnapshotData, //
};
} // namespace

@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/rand_util.h"
@ -19,7 +20,6 @@
#include "gin/run_microtasks_observer.h"
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
#include "base/files/memory_mapped_file.h"
#include "base/path_service.h"
#endif // V8_USE_EXTERNAL_STARTUP_DATA
@ -34,10 +34,10 @@ bool GenerateEntropy(unsigned char* buffer, size_t amount) {
return true;
}
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
base::MemoryMappedFile* g_mapped_natives = NULL;
base::MemoryMappedFile* g_mapped_snapshot = NULL;
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
bool MapV8Files(base::FilePath* natives_path, base::FilePath* snapshot_path,
int natives_fd = -1, int snapshot_fd = -1) {
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
@ -80,7 +80,13 @@ bool IsolateHolder::LoadV8Snapshot() {
return true;
base::FilePath data_path;
PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path);
PathService::Get(
#if defined(OS_ANDROID)
base::DIR_ANDROID_APP_DATA,
#elif defined(OS_POSIX)
base::DIR_EXE,
#endif
&data_path);
DCHECK(!data_path.empty());
base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin");
@ -98,6 +104,22 @@ bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) {
}
#endif // V8_USE_EXTERNAL_STARTUP_DATA
//static
void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out,
int* natives_size_out,
const char** snapshot_data_out,
int* snapshot_size_out) {
if (!g_mapped_natives || !g_mapped_snapshot) {
*natives_data_out = *snapshot_data_out = NULL;
*natives_size_out = *snapshot_size_out = 0;
return;
}
*natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
*snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
*natives_size_out = static_cast<int>(g_mapped_natives->length());
*snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
}
IsolateHolder::IsolateHolder() {
CHECK(g_array_buffer_allocator)
<< "You need to invoke gin::IsolateHolder::Initialize first";
@ -158,14 +180,14 @@ void IsolateHolder::Initialize(ScriptMode mode,
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
v8::StartupData natives;
natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
natives.raw_size = g_mapped_natives->length();
natives.compressed_size = g_mapped_natives->length();
natives.raw_size = static_cast<int>(g_mapped_natives->length());
natives.compressed_size = static_cast<int>(g_mapped_natives->length());
v8::V8::SetNativesDataBlob(&natives);
v8::StartupData snapshot;
snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
snapshot.raw_size = g_mapped_snapshot->length();
snapshot.compressed_size = g_mapped_snapshot->length();
snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
snapshot.compressed_size = static_cast<int>(g_mapped_snapshot->length());
v8::V8::SetSnapshotDataBlob(&snapshot);
#endif // V8_USE_EXTERNAL_STARTUP_DATA
v8::V8::Initialize();

@ -52,11 +52,13 @@ class GIN_EXPORT IsolateHolder {
void RemoveRunMicrotasksObserver();
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
#ifdef OS_ANDROID
static bool LoadV8SnapshotFD(int natives_fd, int snapshot_fd);
#endif
static bool LoadV8Snapshot();
#endif // V8_USE_EXTERNAL_STARTUP_DATA
static void GetV8ExternalSnapshotData(const char** natives_data_out,
int* natives_size_out,
const char** snapshot_data_out,
int* snapshot_size_out);
private:
v8::Isolate* isolate_;

@ -6,4 +6,5 @@ include_rules = [
"+ppapi",
"+third_party/pdfium/fpdfsdk/include",
"+ui/events/keycodes/keyboard_codes.h",
"+v8/include/v8.h"
]

@ -41,6 +41,7 @@
#include "ppapi/cpp/resource.h"
#include "ppapi/cpp/url_request_info.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "v8/include/v8.h"
#if defined(OS_MACOSX)
#include "base/mac/mac_util.h"
@ -319,6 +320,17 @@ Instance::~Instance() {
}
bool Instance::Init(uint32_t argc, const char* argn[], const char* argv[]) {
v8::StartupData natives;
v8::StartupData snapshot;
pp::PDF::GetV8ExternalSnapshotData(&natives.data, &natives.raw_size,
&snapshot.data, &snapshot.raw_size);
if (natives.data) {
natives.compressed_size = natives.raw_size;
snapshot.compressed_size = snapshot.raw_size;
v8::V8::SetNativesDataBlob(&natives);
v8::V8::SetSnapshotDataBlob(&snapshot);
}
// For now, we hide HiDPI support behind a flag.
if (pp::PDF::IsFeatureEnabled(this, PP_PDFFEATURE_HIDPI))
hidpi_enabled_ = true;

@ -169,6 +169,13 @@ struct PPB_PDF {
// Sets the link currently under the cursor.
void (*SetLinkUnderCursor)(PP_Instance instance, const char* url);
// Gets pointers to both the mmap'd V8 snapshot files and their sizes.
// This is needed when loading V8's initial snapshot from external files.
void (*GetV8ExternalSnapshotData)(const char** natives_data_out,
int* natives_size_out,
const char** snapshot_data_out,
int* snapshot_size_out);
};
#endif // PPAPI_C_PRIVATE_PPB_PDF_H_

@ -197,4 +197,20 @@ void PDF::SetLinkUnderCursor(const InstanceHandle& instance, const char* url) {
get_interface<PPB_PDF>()->SetLinkUnderCursor(instance.pp_instance(), url);
}
// static
void PDF::GetV8ExternalSnapshotData(const char** natives_data_out,
int* natives_size_out,
const char** snapshot_data_out,
int* snapshot_size_out) {
if (has_interface<PPB_PDF>()) {
get_interface<PPB_PDF>()->GetV8ExternalSnapshotData(natives_data_out,
natives_size_out, snapshot_data_out, snapshot_size_out);
return;
}
*natives_data_out = NULL;
*snapshot_data_out = NULL;
*natives_size_out = 0;
*snapshot_size_out = 0;
}
} // namespace pp

@ -63,6 +63,10 @@ class PDF {
const char* selected_text);
static void SetLinkUnderCursor(const InstanceHandle& instance,
const char* url);
static void GetV8ExternalSnapshotData(const char** natives_data_out,
int* natives_size_out,
const char** snapshot_data_out,
int* snapshot_size_out);
};
} // namespace pp