OLD | NEW |
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/public/isolate_holder.h" | 5 #include "gin/public/isolate_holder.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
| 10 #include "base/files/file.h" |
10 #include "base/files/memory_mapped_file.h" | 11 #include "base/files/memory_mapped_file.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
14 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
15 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
16 #include "crypto/sha2.h" | 17 #include "crypto/sha2.h" |
17 #include "gin/array_buffer.h" | 18 #include "gin/array_buffer.h" |
18 #include "gin/debug_impl.h" | 19 #include "gin/debug_impl.h" |
19 #include "gin/function_template.h" | 20 #include "gin/function_template.h" |
(...skipping 16 matching lines...) Expand all Loading... |
36 | 37 |
37 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 38 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
38 base::RandBytes(buffer, amount); | 39 base::RandBytes(buffer, amount); |
39 return true; | 40 return true; |
40 } | 41 } |
41 | 42 |
42 base::MemoryMappedFile* g_mapped_natives = NULL; | 43 base::MemoryMappedFile* g_mapped_natives = NULL; |
43 base::MemoryMappedFile* g_mapped_snapshot = NULL; | 44 base::MemoryMappedFile* g_mapped_snapshot = NULL; |
44 | 45 |
45 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 46 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 47 #if !defined(OS_MACOSX) |
| 48 const int kV8SnapshotBasePathKey = |
| 49 #if defined(OS_ANDROID) |
| 50 base::DIR_ANDROID_APP_DATA; |
| 51 #elif defined(OS_POSIX) |
| 52 base::DIR_EXE; |
| 53 #elif defined(OS_WIN) |
| 54 base::DIR_MODULE; |
| 55 #endif // OS_ANDROID |
| 56 #endif // !OS_MACOSX |
| 57 |
| 58 const char kNativesFileName[] = "natives_blob.bin"; |
| 59 const char kSnapshotFileName[] = "snapshot_blob.bin"; |
| 60 |
| 61 void GetV8FilePaths(base::FilePath* natives_path_out, |
| 62 base::FilePath* snapshot_path_out) { |
| 63 #if !defined(OS_MACOSX) |
| 64 base::FilePath data_path; |
| 65 PathService::Get(kV8SnapshotBasePathKey, &data_path); |
| 66 DCHECK(!data_path.empty()); |
| 67 |
| 68 *natives_path_out = data_path.AppendASCII(kNativesFileName); |
| 69 *snapshot_path_out = data_path.AppendASCII(kSnapshotFileName); |
| 70 #else // !defined(OS_MACOSX) |
| 71 base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
| 72 base::SysUTF8ToCFStringRef(kNativesFileName)); |
| 73 *natives_path_out = |
| 74 base::mac::PathForFrameworkBundleResource(natives_file_name); |
| 75 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( |
| 76 base::SysUTF8ToCFStringRef(kSnapshotFileName)); |
| 77 *snapshot_path_out = |
| 78 base::mac::PathForFrameworkBundleResource(snapshot_file_name); |
| 79 DCHECK(!natives_path.empty()); |
| 80 DCHECK(!snapshot_path.empty()); |
| 81 #endif // !defined(OS_MACOSX) |
| 82 } |
| 83 |
46 bool MapV8Files(base::FilePath* natives_path, | 84 bool MapV8Files(base::FilePath* natives_path, |
47 base::FilePath* snapshot_path, | 85 base::FilePath* snapshot_path, |
48 int natives_fd = -1, | 86 int natives_fd = -1, |
49 int snapshot_fd = -1, | 87 int snapshot_fd = -1, |
50 base::MemoryMappedFile::Region natives_region = | 88 base::MemoryMappedFile::Region natives_region = |
51 base::MemoryMappedFile::Region::kWholeFile, | 89 base::MemoryMappedFile::Region::kWholeFile, |
52 base::MemoryMappedFile::Region snapshot_region = | 90 base::MemoryMappedFile::Region snapshot_region = |
53 base::MemoryMappedFile::Region::kWholeFile) { | 91 base::MemoryMappedFile::Region::kWholeFile) { |
54 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | 92 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
55 | 93 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } // namespace | 148 } // namespace |
111 | 149 |
112 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 150 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
113 | 151 |
114 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 152 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
115 // Defined in gen/gin/v8_snapshot_fingerprint.cc | 153 // Defined in gen/gin/v8_snapshot_fingerprint.cc |
116 extern const unsigned char g_natives_fingerprint[]; | 154 extern const unsigned char g_natives_fingerprint[]; |
117 extern const unsigned char g_snapshot_fingerprint[]; | 155 extern const unsigned char g_snapshot_fingerprint[]; |
118 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA | 156 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
119 | 157 |
120 #if !defined(OS_MACOSX) | |
121 const int IsolateHolder::kV8SnapshotBasePathKey = | |
122 #if defined(OS_ANDROID) | |
123 base::DIR_ANDROID_APP_DATA; | |
124 #elif defined(OS_POSIX) | |
125 base::DIR_EXE; | |
126 #elif defined(OS_WIN) | |
127 base::DIR_MODULE; | |
128 #endif // OS_ANDROID | |
129 #endif // !OS_MACOSX | |
130 | |
131 const char IsolateHolder::kNativesFileName[] = "natives_blob.bin"; | |
132 const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin"; | |
133 | |
134 // static | 158 // static |
135 bool IsolateHolder::LoadV8Snapshot() { | 159 bool IsolateHolder::LoadV8Snapshot() { |
136 if (g_mapped_natives && g_mapped_snapshot) | 160 if (g_mapped_natives && g_mapped_snapshot) |
137 return true; | 161 return true; |
138 | 162 |
139 #if !defined(OS_MACOSX) | 163 base::FilePath natives_path; |
140 base::FilePath data_path; | 164 base::FilePath snapshot_path; |
141 PathService::Get(kV8SnapshotBasePathKey, &data_path); | 165 GetV8FilePaths(&natives_path, &snapshot_path); |
142 DCHECK(!data_path.empty()); | |
143 | |
144 base::FilePath natives_path = data_path.AppendASCII(kNativesFileName); | |
145 base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName); | |
146 #else // !defined(OS_MACOSX) | |
147 base::ScopedCFTypeRef<CFStringRef> natives_file_name( | |
148 base::SysUTF8ToCFStringRef(kNativesFileName)); | |
149 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource( | |
150 natives_file_name); | |
151 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( | |
152 base::SysUTF8ToCFStringRef(kSnapshotFileName)); | |
153 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource( | |
154 snapshot_file_name); | |
155 DCHECK(!natives_path.empty()); | |
156 DCHECK(!snapshot_path.empty()); | |
157 #endif // !defined(OS_MACOSX) | |
158 | |
159 if (!MapV8Files(&natives_path, &snapshot_path)) | 166 if (!MapV8Files(&natives_path, &snapshot_path)) |
160 return false; | 167 return false; |
161 | 168 |
162 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 169 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
163 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && | 170 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) && |
164 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); | 171 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint); |
165 #else | 172 #else |
166 return true; | 173 return true; |
167 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA | 174 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
168 } | 175 } |
(...skipping 18 matching lines...) Expand all Loading... |
187 base::MemoryMappedFile::Region snapshot_region = | 194 base::MemoryMappedFile::Region snapshot_region = |
188 base::MemoryMappedFile::Region::kWholeFile; | 195 base::MemoryMappedFile::Region::kWholeFile; |
189 if (natives_size != 0 || natives_offset != 0) { | 196 if (natives_size != 0 || natives_offset != 0) { |
190 snapshot_region = | 197 snapshot_region = |
191 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); | 198 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); |
192 } | 199 } |
193 | 200 |
194 return MapV8Files( | 201 return MapV8Files( |
195 NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region); | 202 NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region); |
196 } | 203 } |
| 204 |
| 205 // static |
| 206 bool IsolateHolder::OpenV8FilesForChildProcesses( |
| 207 base::PlatformFile* natives_fd_out, |
| 208 base::PlatformFile* snapshot_fd_out) { |
| 209 base::FilePath natives_data_path; |
| 210 base::FilePath snapshot_data_path; |
| 211 GetV8FilePaths(&natives_data_path, &snapshot_data_path); |
| 212 |
| 213 int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 214 base::File natives_data_file(natives_data_path, file_flags); |
| 215 base::File snapshot_data_file(snapshot_data_path, file_flags); |
| 216 |
| 217 if (!natives_data_file.IsValid() || !snapshot_data_file.IsValid()) |
| 218 return false; |
| 219 |
| 220 *natives_fd_out = natives_data_file.TakePlatformFile(); |
| 221 *snapshot_fd_out = snapshot_data_file.TakePlatformFile(); |
| 222 return true; |
| 223 } |
197 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 224 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
198 | 225 |
199 //static | 226 //static |
200 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, | 227 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, |
201 int* natives_size_out, | 228 int* natives_size_out, |
202 const char** snapshot_data_out, | 229 const char** snapshot_data_out, |
203 int* snapshot_size_out) { | 230 int* snapshot_size_out) { |
204 if (!g_mapped_natives || !g_mapped_snapshot) { | 231 if (!g_mapped_natives || !g_mapped_snapshot) { |
205 *natives_data_out = *snapshot_data_out = NULL; | 232 *natives_data_out = *snapshot_data_out = NULL; |
206 *natives_size_out = *snapshot_size_out = 0; | 233 *natives_size_out = *snapshot_size_out = 0; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); | 318 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
292 } | 319 } |
293 | 320 |
294 void IsolateHolder::RemoveRunMicrotasksObserver() { | 321 void IsolateHolder::RemoveRunMicrotasksObserver() { |
295 DCHECK(task_observer_.get()); | 322 DCHECK(task_observer_.get()); |
296 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | 323 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
297 task_observer_.reset(); | 324 task_observer_.reset(); |
298 } | 325 } |
299 | 326 |
300 } // namespace gin | 327 } // namespace gin |
OLD | NEW |