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

Side by Side Diff: third_party/WebKit/Source/platform/bindings/V8PerIsolateData.cpp

Issue 2841443005: [Bindings] Create and use V8 context snapshots (Closed)
Patch Set: Work for all comments Created 3 years, 6 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 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "platform/bindings/V8PerIsolateData.h" 26 #include "platform/bindings/V8PerIsolateData.h"
27 27
28 #include <memory> 28 #include <memory>
29 29
30 #include "platform/RuntimeEnabledFeatures.h"
30 #include "platform/ScriptForbiddenScope.h" 31 #include "platform/ScriptForbiddenScope.h"
31 #include "platform/WebTaskRunner.h" 32 #include "platform/WebTaskRunner.h"
32 #include "platform/bindings/DOMDataStore.h" 33 #include "platform/bindings/DOMDataStore.h"
33 #include "platform/bindings/V8Binding.h" 34 #include "platform/bindings/V8Binding.h"
34 #include "platform/bindings/V8ObjectConstructor.h" 35 #include "platform/bindings/V8ObjectConstructor.h"
35 #include "platform/bindings/V8PrivateProperty.h" 36 #include "platform/bindings/V8PrivateProperty.h"
36 #include "platform/bindings/V8ValueCache.h" 37 #include "platform/bindings/V8ValueCache.h"
37 #include "platform/wtf/LeakAnnotations.h" 38 #include "platform/wtf/LeakAnnotations.h"
38 #include "platform/wtf/PtrUtil.h" 39 #include "platform/wtf/PtrUtil.h"
39 #include "public/platform/Platform.h" 40 #include "public/platform/Platform.h"
40 #include "v8/include/v8-debug.h" 41 #include "v8/include/v8-debug.h"
41 42
42 namespace blink { 43 namespace blink {
43 44
44 static V8PerIsolateData* g_main_thread_per_isolate_data = 0; 45 static V8PerIsolateData* g_main_thread_per_isolate_data = 0;
45 46
46 static void BeforeCallEnteredCallback(v8::Isolate* isolate) { 47 static void BeforeCallEnteredCallback(v8::Isolate* isolate) {
47 CHECK(!ScriptForbiddenScope::IsScriptForbidden()); 48 CHECK(!ScriptForbiddenScope::IsScriptForbidden());
48 } 49 }
49 50
50 static void MicrotasksCompletedCallback(v8::Isolate* isolate) { 51 static void MicrotasksCompletedCallback(v8::Isolate* isolate) {
51 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks(); 52 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks();
52 } 53 }
53 54
54 V8PerIsolateData::V8PerIsolateData(WebTaskRunner* task_runner) 55 V8PerIsolateData::V8PerIsolateData(WebTaskRunner* task_runner,
55 : isolate_holder_( 56 intptr_t* table,
57 V8ContextMode v8_context_mode)
58 : v8_context_mode_(v8_context_mode),
59 isolate_holder_(
56 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr, 60 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr,
57 gin::IsolateHolder::kSingleThread, 61 gin::IsolateHolder::kSingleThread,
58 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait 62 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait
59 : gin::IsolateHolder::kAllowAtomicsWait), 63 : gin::IsolateHolder::kAllowAtomicsWait,
64 table,
65 v8_context_mode_ == V8ContextMode::kUseSnapshot ? &startup_data_
66 : nullptr),
67 interface_template_map_for_v8_snapshot_(GetIsolate()),
60 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))), 68 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))),
61 private_property_(V8PrivateProperty::Create()), 69 private_property_(V8PrivateProperty::Create()),
62 constructor_mode_(ConstructorMode::kCreateNewObject), 70 constructor_mode_(ConstructorMode::kCreateNewObject),
63 use_counter_disabled_(false), 71 use_counter_disabled_(false),
64 is_handling_recursion_level_error_(false), 72 is_handling_recursion_level_error_(false),
65 is_reporting_exception_(false) { 73 is_reporting_exception_(false) {
74 // If it fails to load the snapshot file, falls back to kDontUseSnapshot mode.
75 // TODO(peria): Remove this fallback routine.
76 if (v8_context_mode_ == V8ContextMode::kUseSnapshot && !startup_data_.data) {
77 v8_context_mode_ = V8ContextMode::kDontUseSnapshot;
78 }
79
66 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. 80 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
67 GetIsolate()->Enter(); 81 GetIsolate()->Enter();
68 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); 82 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
69 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); 83 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback);
70 if (IsMainThread()) 84 if (IsMainThread())
71 g_main_thread_per_isolate_data = this; 85 g_main_thread_per_isolate_data = this;
72 } 86 }
73 87
88 V8PerIsolateData::V8PerIsolateData(intptr_t* reference_table)
89 : v8_context_mode_(V8ContextMode::kTakeSnapshot),
90 isolate_holder_(reference_table, nullptr),
91 interface_template_map_for_v8_snapshot_(GetIsolate()),
92 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))),
93 private_property_(V8PrivateProperty::Create()),
94 constructor_mode_(ConstructorMode::kCreateNewObject),
95 use_counter_disabled_(false),
96 is_handling_recursion_level_error_(false),
97 is_reporting_exception_(false) {
98 // This constructor assumes to be used to take V8 snapshot of context of
99 // Blink, and it must be run on the main thread.
100 CHECK(IsMainThread());
101
102 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
Yuki 2017/06/20 14:20:11 s/FIXME/TODO(peria)/
peria 2017/06/21 07:19:16 Acknowledged.
103 // SnapshotCreator enters the isolate, so don't do it again.
104 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
105 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback);
106 g_main_thread_per_isolate_data = this;
107 }
108
74 V8PerIsolateData::~V8PerIsolateData() {} 109 V8PerIsolateData::~V8PerIsolateData() {}
75 110
76 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { 111 v8::Isolate* V8PerIsolateData::MainThreadIsolate() {
77 DCHECK(g_main_thread_per_isolate_data); 112 DCHECK(g_main_thread_per_isolate_data);
78 return g_main_thread_per_isolate_data->GetIsolate(); 113 return g_main_thread_per_isolate_data->GetIsolate();
79 } 114 }
80 115
81 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner) { 116 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner,
82 V8PerIsolateData* data = new V8PerIsolateData(task_runner); 117 intptr_t* reference_table,
118 V8ContextMode context_mode) {
119 DCHECK(context_mode == V8ContextMode::kDontUseSnapshot || reference_table);
120
121 V8PerIsolateData* data = nullptr;
122 if (context_mode == V8ContextMode::kTakeSnapshot) {
123 CHECK(reference_table);
124 data = new V8PerIsolateData(reference_table);
125 } else {
126 if (!RuntimeEnabledFeatures::V8ContextSnapshotEnabled()) {
127 context_mode = V8ContextMode::kDontUseSnapshot;
128 reference_table = nullptr;
129 }
130 data = new V8PerIsolateData(task_runner, reference_table, context_mode);
131 }
132 DCHECK(data);
133
83 v8::Isolate* isolate = data->GetIsolate(); 134 v8::Isolate* isolate = data->GetIsolate();
84 isolate->SetData(gin::kEmbedderBlink, data); 135 isolate->SetData(gin::kEmbedderBlink, data);
85 return isolate; 136 return isolate;
86 } 137 }
87 138
88 void V8PerIsolateData::EnableIdleTasks( 139 void V8PerIsolateData::EnableIdleTasks(
89 v8::Isolate* isolate, 140 v8::Isolate* isolate,
90 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { 141 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) {
91 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); 142 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner));
92 } 143 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( 207 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(
157 GetIsolate(), callback, data, signature, length); 208 GetIsolate(), callback, data, signature, length);
158 templ->RemovePrototype(); 209 templ->RemovePrototype();
159 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); 210 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ));
160 return templ; 211 return templ;
161 } 212 }
162 213
163 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( 214 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate(
164 const DOMWrapperWorld& world, 215 const DOMWrapperWorld& world,
165 const void* key) { 216 const void* key) {
217 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) {
218 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key);
219 return interface_template_map_for_v8_snapshot_.Get(type);
220 }
221
166 auto& map = SelectInterfaceTemplateMap(world); 222 auto& map = SelectInterfaceTemplateMap(world);
167 auto result = map.find(key); 223 auto result = map.find(key);
168 if (result != map.end()) 224 if (result != map.end())
169 return result->value.Get(GetIsolate()); 225 return result->value.Get(GetIsolate());
170 return v8::Local<v8::FunctionTemplate>(); 226 return v8::Local<v8::FunctionTemplate>();
171 } 227 }
172 228
173 void V8PerIsolateData::SetInterfaceTemplate( 229 void V8PerIsolateData::SetInterfaceTemplate(
174 const DOMWrapperWorld& world, 230 const DOMWrapperWorld& world,
175 const void* key, 231 const void* key,
176 v8::Local<v8::FunctionTemplate> value) { 232 v8::Local<v8::FunctionTemplate> value) {
177 auto& map = SelectInterfaceTemplateMap(world); 233 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) {
178 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); 234 auto& map = interface_template_map_for_v8_snapshot_;
235 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key);
236 map.Set(type, value);
237 } else {
238 auto& map = SelectInterfaceTemplateMap(world);
239 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value));
240 }
241 }
242
243 void V8PerIsolateData::ClearPersistentsForV8Snapshot() {
244 interface_template_map_for_v8_snapshot_.Clear();
245 private_property_.reset();
179 } 246 }
180 247
181 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( 248 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache(
182 const void* lookup_key, 249 const void* lookup_key,
183 const char* const names[], 250 const char* const names[],
184 size_t count) { 251 size_t count) {
185 auto it = eternal_name_cache_.find(lookup_key); 252 auto it = eternal_name_cache_.find(lookup_key);
186 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; 253 const Vector<v8::Eternal<v8::Name>>* vector = nullptr;
187 if (UNLIKELY(it == eternal_name_cache_.end())) { 254 if (UNLIKELY(it == eternal_name_cache_.end())) {
188 v8::Isolate* isolate = this->GetIsolate(); 255 v8::Isolate* isolate = this->GetIsolate();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 ScriptWrappableVisitor* current = CurrentVisitor(); 369 ScriptWrappableVisitor* current = CurrentVisitor();
303 if (current) 370 if (current)
304 current->PerformCleanup(); 371 current->PerformCleanup();
305 372
306 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( 373 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap(
307 saved_visitor_); 374 saved_visitor_);
308 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); 375 isolate_->SetEmbedderHeapTracer(CurrentVisitor());
309 } 376 }
310 377
311 } // namespace blink 378 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698