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

Side by Side Diff: gin/v8_initializer.cc

Issue 2841443005: [Bindings] Create and use V8 context snapshots (Closed)
Patch Set: . Created 3 years, 7 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
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 25 matching lines...) Expand all
36 #include "base/path_service.h" 36 #include "base/path_service.h"
37 #endif // V8_USE_EXTERNAL_STARTUP_DATA 37 #endif // V8_USE_EXTERNAL_STARTUP_DATA
38 38
39 namespace gin { 39 namespace gin {
40 40
41 namespace { 41 namespace {
42 42
43 // None of these globals are ever freed nor closed. 43 // None of these globals are ever freed nor closed.
44 base::MemoryMappedFile* g_mapped_natives = nullptr; 44 base::MemoryMappedFile* g_mapped_natives = nullptr;
45 base::MemoryMappedFile* g_mapped_snapshot = nullptr; 45 base::MemoryMappedFile* g_mapped_snapshot = nullptr;
46 base::MemoryMappedFile* g_mapped_context = nullptr;
47
48 const char kV8ContextFileName[] = "context_blob.bin";
Yuki 2017/04/28 13:48:27 kV8ContextSnapshotFilename?
peria 2017/06/01 08:33:31 Done.
46 49
47 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) 50 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
48 51
49 // File handles intentionally never closed. Not using File here because its 52 // File handles intentionally never closed. Not using File here because its
50 // Windows implementation guards against two instances owning the same 53 // Windows implementation guards against two instances owning the same
51 // PlatformFile (which we allow since we know it is never freed). 54 // PlatformFile (which we allow since we know it is never freed).
52 typedef std::map<const char*, 55 typedef std::map<const char*,
53 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>> 56 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>
54 OpenedFileMap; 57 OpenedFileMap;
55 static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files = 58 static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 base::debug::SetCrashKeyValue(kIgnitionEnabledKey, 384 base::debug::SetCrashKeyValue(kIgnitionEnabledKey,
382 ignition_enabled_crash_key); 385 ignition_enabled_crash_key);
383 386
384 387
385 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) 388 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
386 v8::StartupData natives; 389 v8::StartupData natives;
387 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); 390 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
388 natives.raw_size = static_cast<int>(g_mapped_natives->length()); 391 natives.raw_size = static_cast<int>(g_mapped_natives->length());
389 v8::V8::SetNativesDataBlob(&natives); 392 v8::V8::SetNativesDataBlob(&natives);
390 393
391 if (g_mapped_snapshot != NULL) { 394 if (g_mapped_snapshot) {
392 v8::StartupData snapshot; 395 v8::StartupData snapshot;
393 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); 396 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
394 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); 397 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
395 v8::V8::SetSnapshotDataBlob(&snapshot); 398 v8::V8::SetSnapshotDataBlob(&snapshot);
396 } 399 }
397 #endif // V8_USE_EXTERNAL_STARTUP_DATA 400 #endif // V8_USE_EXTERNAL_STARTUP_DATA
398 401
399 v8::V8::SetEntropySource(&GenerateEntropy); 402 v8::V8::SetEntropySource(&GenerateEntropy);
400 v8::V8::Initialize(); 403 v8::V8::Initialize();
401 404
(...skipping 15 matching lines...) Expand all
417 if (g_mapped_snapshot) { 420 if (g_mapped_snapshot) {
418 *snapshot_data_out = 421 *snapshot_data_out =
419 reinterpret_cast<const char*>(g_mapped_snapshot->data()); 422 reinterpret_cast<const char*>(g_mapped_snapshot->data());
420 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); 423 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
421 } else { 424 } else {
422 *snapshot_data_out = NULL; 425 *snapshot_data_out = NULL;
423 *snapshot_size_out = 0; 426 *snapshot_size_out = 0;
424 } 427 }
425 } 428 }
426 429
430 // static
431 void V8Initializer::LoadV8Context() {
432 if (g_mapped_context)
433 return;
434
435 OpenFileIfNecessary(kV8ContextFileName);
436 MapOpenedFile(GetOpenedFile(kV8ContextFileName), &g_mapped_context);
437 }
438
439 // static
440 void V8Initializer::LoadV8ContextFromFD(base::PlatformFile snapshot_pf,
441 int64_t snapshot_offset,
442 int64_t snapshot_size) {
443 if (g_mapped_context)
444 return;
445
446 if (snapshot_pf == base::kInvalidPlatformFile)
447 return;
Yuki 2017/04/28 13:48:27 Is it really okay to simply return? Do we need to
peria 2017/06/01 08:33:31 In this case, we fail to load the blob file. Let's
448
449 base::MemoryMappedFile::Region snapshot_region =
450 base::MemoryMappedFile::Region::kWholeFile;
451 if (snapshot_size != 0 || snapshot_offset != 0) {
452 snapshot_region.offset = snapshot_offset;
453 snapshot_region.size = snapshot_size;
454 }
455
456 LoadV8FileResult result = V8_LOAD_SUCCESS;
457 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_context))
458 result = V8_LOAD_FAILED_MAP;
459 if (result == V8_LOAD_SUCCESS) {
Yuki 2017/04/28 13:48:27 Why don't you simply write if (MapV8File(...)) {
peria 2017/05/30 08:25:41 Done.
460 g_opened_files.Get()[kV8ContextFileName] =
461 std::make_pair(snapshot_pf, snapshot_region);
462 }
463 }
464
465 // static
466 base::PlatformFile V8Initializer::GetOpenV8ContextFileForChildProcesses(
Yuki 2017/04/28 13:48:27 GetOpened? I'm not sure what child processes are.
peria 2017/05/30 08:25:41 Acknowledged.
467 base::MemoryMappedFile::Region* region_out) {
468 const OpenedFileMap::mapped_type& opened =
469 OpenFileIfNecessary(kV8ContextFileName);
470 *region_out = opened.second;
471 return opened.first;
472 }
473
474 // static
475 void V8Initializer::GetV8ContextData(const char** snapshot_data_out,
476 int* snapshot_size_out) {
477 if (g_mapped_context) {
478 *snapshot_data_out =
479 reinterpret_cast<const char*>(g_mapped_context->data());
480 *snapshot_size_out = static_cast<int>(g_mapped_context->length());
481 } else {
482 *snapshot_data_out = nullptr;
483 *snapshot_size_out = 0;
484 }
485 }
486
487 #if defined(OS_ANDROID)
488 // static
489 base::FilePath V8Initializer::GetV8ContextFilePath() {
490 base::FilePath path;
491 GetV8FilePath(kV8ContextFileName, &path);
492 return path;
493 }
494 #endif // defined(OS_ANDROID)
495
427 } // namespace gin 496 } // namespace gin
OLDNEW
« gin/v8_initializer.h ('K') | « gin/v8_initializer.h ('k') | net/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698