Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(730)

Side by Side Diff: gin/v8_initializer.cc

Issue 1019483002: Add support to extension_shell and ash_shell to use external V8 snapshot files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Mac and Win Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gin/v8_initializer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "gin/v8_initializer.h" 5 #include "gin/v8_initializer.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/files/file.h" 8 #include "base/files/file.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/memory_mapped_file.h" 10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/rand_util.h" 12 #include "base/rand_util.h"
13 #include "base/strings/sys_string_conversions.h" 13 #include "base/strings/sys_string_conversions.h"
14 #include "crypto/sha2.h" 14 #include "crypto/sha2.h"
15 15
16 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) 16 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
17 #if defined(OS_MACOSX) 17 #if defined(OS_MACOSX)
18 #include "base/mac/foundation_util.h" 18 #include "base/mac/foundation_util.h"
19 #endif // OS_MACOSX 19 #endif // OS_MACOSX
20 #include "base/path_service.h" 20 #include "base/path_service.h"
21 #endif // V8_USE_EXTERNAL_STARTUP_DATA 21 #endif // V8_USE_EXTERNAL_STARTUP_DATA
22 22
23 namespace gin { 23 namespace gin {
24 24
25 namespace { 25 namespace {
26 26
27 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
28 base::RandBytes(buffer, amount);
29 return true;
30 }
31
32 base::MemoryMappedFile* g_mapped_natives = nullptr; 27 base::MemoryMappedFile* g_mapped_natives = nullptr;
33 base::MemoryMappedFile* g_mapped_snapshot = nullptr; 28 base::MemoryMappedFile* g_mapped_snapshot = nullptr;
34 29
35 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) 30 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
31 #if !defined(OS_MACOSX)
32 const int kV8SnapshotBasePathKey =
33 #if defined(OS_ANDROID)
34 base::DIR_ANDROID_APP_DATA;
35 #elif defined(OS_POSIX)
36 base::DIR_EXE;
37 #elif defined(OS_WIN)
38 base::DIR_MODULE;
39 #endif // OS_ANDROID
40 #endif // !OS_MACOSX
41
42 const char kNativesFileName[] = "natives_blob.bin";
43 const char kSnapshotFileName[] = "snapshot_blob.bin";
44
45 void GetV8FilePaths(base::FilePath* natives_path_out,
46 base::FilePath* snapshot_path_out) {
47 #if !defined(OS_MACOSX)
48 base::FilePath data_path;
49 PathService::Get(kV8SnapshotBasePathKey, &data_path);
50 DCHECK(!data_path.empty());
51
52 *natives_path_out = data_path.AppendASCII(kNativesFileName);
53 *snapshot_path_out = data_path.AppendASCII(kSnapshotFileName);
54 #else // !defined(OS_MACOSX)
55 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
56 base::SysUTF8ToCFStringRef(kNativesFileName));
57 *natives_path_out =
58 base::mac::PathForFrameworkBundleResource(natives_file_name);
59 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
60 base::SysUTF8ToCFStringRef(kSnapshotFileName));
61 *snapshot_path_out =
62 base::mac::PathForFrameworkBundleResource(snapshot_file_name);
63 DCHECK(!natives_path_out->empty());
64 DCHECK(!snapshot_path_out->empty());
65 #endif // !defined(OS_MACOSX)
66 }
67
36 bool MapV8Files(base::File natives_file, 68 bool MapV8Files(base::File natives_file,
37 base::File snapshot_file, 69 base::File snapshot_file,
38 base::MemoryMappedFile::Region natives_region = 70 base::MemoryMappedFile::Region natives_region =
39 base::MemoryMappedFile::Region::kWholeFile, 71 base::MemoryMappedFile::Region::kWholeFile,
40 base::MemoryMappedFile::Region snapshot_region = 72 base::MemoryMappedFile::Region snapshot_region =
41 base::MemoryMappedFile::Region::kWholeFile) { 73 base::MemoryMappedFile::Region::kWholeFile) {
42 g_mapped_natives = new base::MemoryMappedFile; 74 g_mapped_natives = new base::MemoryMappedFile;
43 if (!g_mapped_natives->IsValid()) { 75 if (!g_mapped_natives->IsValid()) {
44 if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) { 76 if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) {
45 delete g_mapped_natives; 77 delete g_mapped_natives;
(...skipping 22 matching lines...) Expand all
68 unsigned char output[crypto::kSHA256Length]; 100 unsigned char output[crypto::kSHA256Length];
69 crypto::SHA256HashString( 101 crypto::SHA256HashString(
70 base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()), 102 base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()),
71 snapshot_file->length()), 103 snapshot_file->length()),
72 output, sizeof(output)); 104 output, sizeof(output));
73 return !memcmp(fingerprint, output, sizeof(output)); 105 return !memcmp(fingerprint, output, sizeof(output));
74 } 106 }
75 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA 107 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
76 #endif // V8_USE_EXTERNAL_STARTUP_DATA 108 #endif // V8_USE_EXTERNAL_STARTUP_DATA
77 109
110 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
111 base::RandBytes(buffer, amount);
112 return true;
113 }
114
78 } // namespace 115 } // namespace
79 116
80 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) 117 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
81
82 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) 118 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
83 // Defined in gen/gin/v8_snapshot_fingerprint.cc 119 // Defined in gen/gin/v8_snapshot_fingerprint.cc
84 extern const unsigned char g_natives_fingerprint[]; 120 extern const unsigned char g_natives_fingerprint[];
85 extern const unsigned char g_snapshot_fingerprint[]; 121 extern const unsigned char g_snapshot_fingerprint[];
86 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA 122 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
87 123
88 #if !defined(OS_MACOSX)
89 const int V8Initializer::kV8SnapshotBasePathKey =
90 #if defined(OS_ANDROID)
91 base::DIR_ANDROID_APP_DATA;
92 #elif defined(OS_POSIX)
93 base::DIR_EXE;
94 #elif defined(OS_WIN)
95 base::DIR_MODULE;
96 #endif // OS_ANDROID
97 #endif // !OS_MACOSX
98
99 const char V8Initializer::kNativesFileName[] = "natives_blob.bin";
100 const char V8Initializer::kSnapshotFileName[] = "snapshot_blob.bin";
101
102 // static 124 // static
103 bool V8Initializer::LoadV8Snapshot() { 125 bool V8Initializer::LoadV8Snapshot() {
104 if (g_mapped_natives && g_mapped_snapshot) 126 if (g_mapped_natives && g_mapped_snapshot)
105 return true; 127 return true;
106 128
107 #if !defined(OS_MACOSX) 129 base::FilePath natives_data_path;
108 base::FilePath data_path; 130 base::FilePath snapshot_data_path;
109 PathService::Get(kV8SnapshotBasePathKey, &data_path); 131 GetV8FilePaths(&natives_data_path, &snapshot_data_path);
110 DCHECK(!data_path.empty());
111
112 base::FilePath natives_path = data_path.AppendASCII(kNativesFileName);
113 base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName);
114 #else // !defined(OS_MACOSX)
115 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
116 base::SysUTF8ToCFStringRef(kNativesFileName));
117 base::FilePath natives_path =
118 base::mac::PathForFrameworkBundleResource(natives_file_name);
119 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
120 base::SysUTF8ToCFStringRef(kSnapshotFileName));
121 base::FilePath snapshot_path =
122 base::mac::PathForFrameworkBundleResource(snapshot_file_name);
123 DCHECK(!natives_path.empty());
124 DCHECK(!snapshot_path.empty());
125 #endif // !defined(OS_MACOSX)
126 132
127 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; 133 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
128 if (!MapV8Files(base::File(natives_path, flags), 134 if (!MapV8Files(base::File(natives_data_path, flags),
129 base::File(snapshot_path, flags))) 135 base::File(snapshot_data_path, flags)))
130 return false; 136 return false;
131 137
132 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) 138 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
133 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && 139 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) &&
134 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); 140 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
135 #else 141 #else
136 return true; 142 return true;
137 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA 143 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
138 } 144 }
139 145
(...skipping 18 matching lines...) Expand all
158 base::MemoryMappedFile::Region::kWholeFile; 164 base::MemoryMappedFile::Region::kWholeFile;
159 if (natives_size != 0 || natives_offset != 0) { 165 if (natives_size != 0 || natives_offset != 0) {
160 snapshot_region = 166 snapshot_region =
161 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); 167 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
162 } 168 }
163 169
164 return MapV8Files(base::File(natives_pf), base::File(snapshot_pf), 170 return MapV8Files(base::File(natives_pf), base::File(snapshot_pf),
165 natives_region, snapshot_region); 171 natives_region, snapshot_region);
166 } 172 }
167 173
174 // static
175 bool V8Initializer::OpenV8FilesForChildProcesses(
176 base::PlatformFile* natives_fd_out,
177 base::PlatformFile* snapshot_fd_out) {
178 base::FilePath natives_data_path;
179 base::FilePath snapshot_data_path;
180 GetV8FilePaths(&natives_data_path, &snapshot_data_path);
181
182 int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
183 base::File natives_data_file(natives_data_path, file_flags);
184 base::File snapshot_data_file(snapshot_data_path, file_flags);
185
186 if (!natives_data_file.IsValid() || !snapshot_data_file.IsValid())
187 return false;
188
189 *natives_fd_out = natives_data_file.TakePlatformFile();
190 *snapshot_fd_out = snapshot_data_file.TakePlatformFile();
191 return true;
192 }
193
168 #endif // V8_USE_EXTERNAL_STARTUP_DATA 194 #endif // V8_USE_EXTERNAL_STARTUP_DATA
169 195
170 // static 196 // static
171 void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode, 197 void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode,
172 v8::ArrayBuffer::Allocator* allocator) { 198 v8::ArrayBuffer::Allocator* allocator) {
173 CHECK(allocator); 199 CHECK(allocator);
174 200
175 static bool v8_is_initialized = false; 201 static bool v8_is_initialized = false;
176 if (v8_is_initialized) 202 if (v8_is_initialized)
177 return; 203 return;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 *natives_size_out = *snapshot_size_out = 0; 238 *natives_size_out = *snapshot_size_out = 0;
213 return; 239 return;
214 } 240 }
215 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); 241 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
216 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); 242 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
217 *natives_size_out = static_cast<int>(g_mapped_natives->length()); 243 *natives_size_out = static_cast<int>(g_mapped_natives->length());
218 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); 244 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
219 } 245 }
220 246
221 } // namespace gin 247 } // namespace gin
OLDNEW
« no previous file with comments | « gin/v8_initializer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698