Commit 54fab5e3 authored by rmcilroy's avatar rmcilroy Committed by Commit bot

Add support to extension_shell and ash_shell to use external V8 snapshot files.

Adds support to extension_shell and ash_shell to use external V8 snapshot files
in preparation for moving ChromeOS and ChromeCast to use this.

Re-factors the chrome_content_browser_client and
content/shell_content_browser_client to allow more reuse
of the code which opens the V8 external snapshot for child processes
by adding IsolateHolder::OpenV8FilesForChildProcesses.

This does not yet switch ChromeOS to use external V8 snapshot files -
this will be done in follow up CL https://codereview.chromium.org/1019123002.

BUG=421063

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

Cr-Commit-Position: refs/heads/master@{#323953}
parent 6f6eba38
include_rules = [
"+content/public",
"+content/shell",
"+gin",
"+sandbox",
]
......@@ -5,14 +5,21 @@
#include "ash/shell/content_client/shell_content_browser_client.h"
#include "ash/shell/content_client/shell_browser_main_parts.h"
#include "content/public/common/content_descriptors.h"
#include "content/shell/browser/shell_browser_context.h"
#include "gin/v8_initializer.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace ash {
namespace shell {
ShellContentBrowserClient::ShellContentBrowserClient()
: shell_browser_main_parts_(NULL) {
:
#if defined(OS_POSIX) && !defined(OS_MACOSX)
v8_natives_fd_(-1),
v8_snapshot_fd_(-1),
#endif // OS_POSIX && !OS_MACOSX
shell_browser_main_parts_(nullptr) {
}
ShellContentBrowserClient::~ShellContentBrowserClient() {
......@@ -34,6 +41,28 @@ net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext(
request_interceptors.Pass());
}
#if defined(OS_POSIX) && !defined(OS_MACOSX)
void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
const base::CommandLine& command_line,
int child_process_id,
content::FileDescriptorInfo* mappings) {
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
if (v8_natives_fd_.get() == -1 || v8_snapshot_fd_.get() == -1) {
int v8_natives_fd = -1;
int v8_snapshot_fd = -1;
if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd,
&v8_snapshot_fd)) {
v8_natives_fd_.reset(v8_natives_fd);
v8_snapshot_fd_.reset(v8_snapshot_fd);
}
}
DCHECK(v8_natives_fd_.get() != -1 && v8_snapshot_fd_.get() != -1);
mappings->Share(kV8NativesDataDescriptor, v8_natives_fd_.get());
mappings->Share(kV8SnapshotDataDescriptor, v8_snapshot_fd_.get());
#endif // V8_USE_EXTERNAL_STARTUP_DATA
}
#endif // OS_POSIX && !OS_MACOSX
content::ShellBrowserContext* ShellContentBrowserClient::browser_context() {
return shell_browser_main_parts_->browser_context();
}
......
......@@ -33,10 +33,21 @@ class ShellContentBrowserClient : public content::ContentBrowserClient {
content::BrowserContext* browser_context,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) override;
#if defined(OS_POSIX) && !defined(OS_MACOSX)
void GetAdditionalMappedFilesForChildProcess(
const base::CommandLine& command_line,
int child_process_id,
content::FileDescriptorInfo* mappings) override;
#endif
content::ShellBrowserContext* browser_context();
private:
#if defined(OS_POSIX) && !defined(OS_MACOSX)
base::ScopedFD v8_natives_fd_;
base::ScopedFD v8_snapshot_fd_;
#endif
ShellBrowserMainParts* shell_browser_main_parts_;
DISALLOW_COPY_AND_ASSIGN(ShellContentBrowserClient);
......
......@@ -2183,23 +2183,16 @@ void ChromeContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
int child_process_id,
FileDescriptorInfo* mappings) {
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
if (v8_snapshot_fd_.get() == -1 && v8_natives_fd_.get() == -1) {
base::FilePath v8_data_path;
PathService::Get(gin::V8Initializer::kV8SnapshotBasePathKey, &v8_data_path);
DCHECK(!v8_data_path.empty());
int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
base::FilePath v8_natives_data_path =
v8_data_path.AppendASCII(gin::V8Initializer::kNativesFileName);
base::FilePath v8_snapshot_data_path =
v8_data_path.AppendASCII(gin::V8Initializer::kSnapshotFileName);
base::File v8_natives_data_file(v8_natives_data_path, file_flags);
base::File v8_snapshot_data_file(v8_snapshot_data_path, file_flags);
DCHECK(v8_natives_data_file.IsValid());
DCHECK(v8_snapshot_data_file.IsValid());
v8_natives_fd_.reset(v8_natives_data_file.TakePlatformFile());
v8_snapshot_fd_.reset(v8_snapshot_data_file.TakePlatformFile());
if (v8_natives_fd_.get() == -1 || v8_snapshot_fd_.get() == -1) {
int v8_natives_fd = -1;
int v8_snapshot_fd = -1;
if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd,
&v8_snapshot_fd)) {
v8_natives_fd_.reset(v8_natives_fd);
v8_snapshot_fd_.reset(v8_snapshot_fd);
}
}
DCHECK(v8_natives_fd_.get() != -1 && v8_snapshot_fd_.get() != -1);
mappings->Share(kV8NativesDataDescriptor, v8_natives_fd_.get());
mappings->Share(kV8SnapshotDataDescriptor, v8_snapshot_fd_.get());
#endif // V8_USE_EXTERNAL_STARTUP_DATA
......
......@@ -333,23 +333,16 @@ void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
int child_process_id,
FileDescriptorInfo* mappings) {
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
if (v8_snapshot_fd_.get() == -1 && v8_natives_fd_.get() == -1) {
base::FilePath v8_data_path;
PathService::Get(gin::V8Initializer::kV8SnapshotBasePathKey, &v8_data_path);
DCHECK(!v8_data_path.empty());
int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
base::FilePath v8_natives_data_path =
v8_data_path.AppendASCII(gin::V8Initializer::kNativesFileName);
base::FilePath v8_snapshot_data_path =
v8_data_path.AppendASCII(gin::V8Initializer::kSnapshotFileName);
base::File v8_natives_data_file(v8_natives_data_path, file_flags);
base::File v8_snapshot_data_file(v8_snapshot_data_path, file_flags);
DCHECK(v8_natives_data_file.IsValid());
DCHECK(v8_snapshot_data_file.IsValid());
v8_natives_fd_.reset(v8_natives_data_file.TakePlatformFile());
v8_snapshot_fd_.reset(v8_snapshot_data_file.TakePlatformFile());
if (v8_natives_fd_.get() == -1 || v8_snapshot_fd_.get() == -1) {
int v8_natives_fd = -1;
int v8_snapshot_fd = -1;
if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd,
&v8_snapshot_fd)) {
v8_natives_fd_.reset(v8_natives_fd);
v8_snapshot_fd_.reset(v8_snapshot_fd);
}
}
DCHECK(v8_natives_fd_.get() != -1 && v8_snapshot_fd_.get() != -1);
mappings->Share(kV8NativesDataDescriptor, v8_natives_fd_.get());
mappings->Share(kV8SnapshotDataDescriptor, v8_snapshot_fd_.get());
#endif // V8_USE_EXTERNAL_STARTUP_DATA
......
......@@ -21,6 +21,8 @@ include_rules = [
"+device/hid",
"+device/usb",
"+gin",
"+google_apis/gaia",
"+net",
......
......@@ -8,6 +8,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/site_instance.h"
#include "content/public/common/content_descriptors.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/shell/browser/shell_browser_context.h"
......@@ -26,6 +27,7 @@
#include "extensions/shell/browser/shell_browser_main_parts.h"
#include "extensions/shell/browser/shell_extension_system.h"
#include "extensions/shell/browser/shell_speech_recognition_manager_delegate.h"
#include "gin/v8_initializer.h"
#include "url/gurl.h"
#if !defined(DISABLE_NACL)
......@@ -45,19 +47,25 @@ using content::BrowserThread;
namespace extensions {
namespace {
ShellContentBrowserClient* g_instance = NULL;
ShellContentBrowserClient* g_instance = nullptr;
} // namespace
ShellContentBrowserClient::ShellContentBrowserClient(
ShellBrowserMainDelegate* browser_main_delegate)
: browser_main_parts_(NULL), browser_main_delegate_(browser_main_delegate) {
:
#if defined(OS_POSIX) && !defined(OS_MACOSX)
v8_natives_fd_(-1),
v8_snapshot_fd_(-1),
#endif // OS_POSIX && !OS_MACOSX
browser_main_parts_(nullptr),
browser_main_delegate_(browser_main_delegate) {
DCHECK(!g_instance);
g_instance = this;
}
ShellContentBrowserClient::~ShellContentBrowserClient() {
g_instance = NULL;
g_instance = nullptr;
}
// static
......@@ -219,7 +227,7 @@ ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id) {
++iter;
}
#endif
return NULL;
return nullptr;
}
void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
......@@ -229,6 +237,28 @@ void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
additional_allowed_schemes->push_back(kExtensionScheme);
}
#if defined(OS_POSIX) && !defined(OS_MACOSX)
void ShellContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
const base::CommandLine& command_line,
int child_process_id,
content::FileDescriptorInfo* mappings) {
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
if (v8_natives_fd_.get() == -1 || v8_snapshot_fd_.get() == -1) {
int v8_natives_fd = -1;
int v8_snapshot_fd = -1;
if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd,
&v8_snapshot_fd)) {
v8_natives_fd_.reset(v8_natives_fd);
v8_snapshot_fd_.reset(v8_snapshot_fd);
}
}
DCHECK(v8_natives_fd_.get() != -1 && v8_snapshot_fd_.get() != -1);
mappings->Share(kV8NativesDataDescriptor, v8_natives_fd_.get());
mappings->Share(kV8SnapshotDataDescriptor, v8_snapshot_fd_.get());
#endif // V8_USE_EXTERNAL_STARTUP_DATA
}
#endif // OS_POSIX && !OS_MACOSX
content::DevToolsManagerDelegate*
ShellContentBrowserClient::GetDevToolsManagerDelegate() {
return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
......
......@@ -58,6 +58,13 @@ class ShellContentBrowserClient : public content::ContentBrowserClient {
int plugin_process_id) override;
void GetAdditionalAllowedSchemesForFileSystem(
std::vector<std::string>* additional_schemes) override;
#if defined(OS_POSIX) && !defined(OS_MACOSX)
void GetAdditionalMappedFilesForChildProcess(
const base::CommandLine& command_line,
int child_process_id,
content::FileDescriptorInfo* mappings) override;
#endif
content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
protected:
......@@ -73,6 +80,11 @@ class ShellContentBrowserClient : public content::ContentBrowserClient {
// Returns the extension or app associated with |site_instance| or NULL.
const Extension* GetExtension(content::SiteInstance* site_instance);
#if defined(OS_POSIX) && !defined(OS_MACOSX)
base::ScopedFD v8_natives_fd_;
base::ScopedFD v8_snapshot_fd_;
#endif
// Owned by content::BrowserMainLoop.
ShellBrowserMainParts* browser_main_parts_;
......
......@@ -24,15 +24,47 @@ namespace gin {
namespace {
bool GenerateEntropy(unsigned char* buffer, size_t amount) {
base::RandBytes(buffer, amount);
return true;
}
base::MemoryMappedFile* g_mapped_natives = nullptr;
base::MemoryMappedFile* g_mapped_snapshot = nullptr;
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
#if !defined(OS_MACOSX)
const int kV8SnapshotBasePathKey =
#if defined(OS_ANDROID)
base::DIR_ANDROID_APP_DATA;
#elif defined(OS_POSIX)
base::DIR_EXE;
#elif defined(OS_WIN)
base::DIR_MODULE;
#endif // OS_ANDROID
#endif // !OS_MACOSX
const char kNativesFileName[] = "natives_blob.bin";
const char kSnapshotFileName[] = "snapshot_blob.bin";
void GetV8FilePaths(base::FilePath* natives_path_out,
base::FilePath* snapshot_path_out) {
#if !defined(OS_MACOSX)
base::FilePath data_path;
PathService::Get(kV8SnapshotBasePathKey, &data_path);
DCHECK(!data_path.empty());
*natives_path_out = data_path.AppendASCII(kNativesFileName);
*snapshot_path_out = data_path.AppendASCII(kSnapshotFileName);
#else // !defined(OS_MACOSX)
base::ScopedCFTypeRef<CFStringRef> natives_file_name(
base::SysUTF8ToCFStringRef(kNativesFileName));
*natives_path_out =
base::mac::PathForFrameworkBundleResource(natives_file_name);
base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
base::SysUTF8ToCFStringRef(kSnapshotFileName));
*snapshot_path_out =
base::mac::PathForFrameworkBundleResource(snapshot_file_name);
DCHECK(!natives_path_out->empty());
DCHECK(!snapshot_path_out->empty());
#endif // !defined(OS_MACOSX)
}
bool MapV8Files(base::File natives_file,
base::File snapshot_file,
base::MemoryMappedFile::Region natives_region =
......@@ -75,58 +107,32 @@ bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file,
#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
#endif // V8_USE_EXTERNAL_STARTUP_DATA
bool GenerateEntropy(unsigned char* buffer, size_t amount) {
base::RandBytes(buffer, amount);
return true;
}
} // namespace
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
// Defined in gen/gin/v8_snapshot_fingerprint.cc
extern const unsigned char g_natives_fingerprint[];
extern const unsigned char g_snapshot_fingerprint[];
#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
#if !defined(OS_MACOSX)
const int V8Initializer::kV8SnapshotBasePathKey =
#if defined(OS_ANDROID)
base::DIR_ANDROID_APP_DATA;
#elif defined(OS_POSIX)
base::DIR_EXE;
#elif defined(OS_WIN)
base::DIR_MODULE;
#endif // OS_ANDROID
#endif // !OS_MACOSX
const char V8Initializer::kNativesFileName[] = "natives_blob.bin";
const char V8Initializer::kSnapshotFileName[] = "snapshot_blob.bin";
// static
bool V8Initializer::LoadV8Snapshot() {
if (g_mapped_natives && g_mapped_snapshot)
return true;
#if !defined(OS_MACOSX)
base::FilePath data_path;
PathService::Get(kV8SnapshotBasePathKey, &data_path);
DCHECK(!data_path.empty());
base::FilePath natives_path = data_path.AppendASCII(kNativesFileName);
base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName);
#else // !defined(OS_MACOSX)
base::ScopedCFTypeRef<CFStringRef> natives_file_name(
base::SysUTF8ToCFStringRef(kNativesFileName));
base::FilePath natives_path =
base::mac::PathForFrameworkBundleResource(natives_file_name);
base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
base::SysUTF8ToCFStringRef(kSnapshotFileName));
base::FilePath snapshot_path =
base::mac::PathForFrameworkBundleResource(snapshot_file_name);
DCHECK(!natives_path.empty());
DCHECK(!snapshot_path.empty());
#endif // !defined(OS_MACOSX)
base::FilePath natives_data_path;
base::FilePath snapshot_data_path;
GetV8FilePaths(&natives_data_path, &snapshot_data_path);
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
if (!MapV8Files(base::File(natives_path, flags),
base::File(snapshot_path, flags)))
if (!MapV8Files(base::File(natives_data_path, flags),
base::File(snapshot_data_path, flags)))
return false;
#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
......@@ -165,6 +171,26 @@ bool V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile natives_pf,
natives_region, snapshot_region);
}
// static
bool V8Initializer::OpenV8FilesForChildProcesses(
base::PlatformFile* natives_fd_out,
base::PlatformFile* snapshot_fd_out) {
base::FilePath natives_data_path;
base::FilePath snapshot_data_path;
GetV8FilePaths(&natives_data_path, &snapshot_data_path);
int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
base::File natives_data_file(natives_data_path, file_flags);
base::File snapshot_data_file(snapshot_data_path, file_flags);
if (!natives_data_file.IsValid() || !snapshot_data_file.IsValid())
return false;
*natives_fd_out = natives_data_file.TakePlatformFile();
*snapshot_fd_out = snapshot_data_file.TakePlatformFile();
return true;
}
#endif // V8_USE_EXTERNAL_STARTUP_DATA
// static
......
......@@ -49,6 +49,12 @@ class GIN_EXPORT V8Initializer {
// snapshot is already loaded, false otherwise.
static bool LoadV8Snapshot();
// Opens the V8 snapshot data files and returns open file descriptors to these
// files in |natives_fd_out| and |snapshot_fd_out|, which can be passed to
// child processes.
static bool OpenV8FilesForChildProcesses(base::PlatformFile* natives_fd_out,
base::PlatformFile* snapshot_fd_out);
#endif // V8_USE_EXTERNAL_STARTUP_DATA
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment