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

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 yuki's 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 // TODO(jochen): Re-enable this once https://crbug.com/728583 48 // TODO(jochen): Re-enable this once https://crbug.com/728583
48 // CHECK(!ScriptForbiddenScope::IsScriptForbidden()); 49 // CHECK(!ScriptForbiddenScope::IsScriptForbidden());
49 } 50 }
50 51
51 static void MicrotasksCompletedCallback(v8::Isolate* isolate) { 52 static void MicrotasksCompletedCallback(v8::Isolate* isolate) {
52 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks(); 53 V8PerIsolateData::From(isolate)->RunEndOfScopeTasks();
53 } 54 }
54 55
55 V8PerIsolateData::V8PerIsolateData(WebTaskRunner* task_runner) 56 V8PerIsolateData::V8PerIsolateData(WebTaskRunner* task_runner,
56 : isolate_holder_( 57 intptr_t* table,
58 V8ContextMode v8_context_mode)
59 : v8_context_mode_(v8_context_mode),
60 isolate_holder_(
57 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr, 61 task_runner ? task_runner->ToSingleThreadTaskRunner() : nullptr,
58 gin::IsolateHolder::kSingleThread, 62 gin::IsolateHolder::kSingleThread,
59 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait 63 IsMainThread() ? gin::IsolateHolder::kDisallowAtomicsWait
60 : gin::IsolateHolder::kAllowAtomicsWait), 64 : gin::IsolateHolder::kAllowAtomicsWait,
65 table,
66 v8_context_mode_ == V8ContextMode::kUseSnapshot ? &startup_data_
67 : nullptr),
68 interface_template_map_for_v8_snapshot_(GetIsolate()),
61 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))), 69 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))),
62 private_property_(V8PrivateProperty::Create()), 70 private_property_(V8PrivateProperty::Create()),
63 constructor_mode_(ConstructorMode::kCreateNewObject), 71 constructor_mode_(ConstructorMode::kCreateNewObject),
64 use_counter_disabled_(false), 72 use_counter_disabled_(false),
65 is_handling_recursion_level_error_(false), 73 is_handling_recursion_level_error_(false),
66 is_reporting_exception_(false) { 74 is_reporting_exception_(false) {
75 // If it fails to load the snapshot file, falls back to kDontUseSnapshot mode.
76 // TODO(peria): Remove this fallback routine.
77 if (v8_context_mode_ == V8ContextMode::kUseSnapshot && !startup_data_.data) {
78 v8_context_mode_ = V8ContextMode::kDontUseSnapshot;
79 }
80
67 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. 81 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
68 GetIsolate()->Enter(); 82 GetIsolate()->Enter();
69 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); 83 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
70 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); 84 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback);
71 if (IsMainThread()) 85 if (IsMainThread())
72 g_main_thread_per_isolate_data = this; 86 g_main_thread_per_isolate_data = this;
73 } 87 }
74 88
89 V8PerIsolateData::V8PerIsolateData(intptr_t* reference_table)
90 : v8_context_mode_(V8ContextMode::kTakeSnapshot),
91 isolate_holder_(reference_table, nullptr),
92 interface_template_map_for_v8_snapshot_(GetIsolate()),
93 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))),
94 private_property_(V8PrivateProperty::Create()),
95 constructor_mode_(ConstructorMode::kCreateNewObject),
96 use_counter_disabled_(false),
97 is_handling_recursion_level_error_(false),
98 is_reporting_exception_(false) {
99 // This constructor assumes to be used to take V8 snapshot of context of
100 // Blink, and it must be run on the main thread.
101 CHECK(IsMainThread());
102
103 // SnapshotCreator enters the isolate, so we don't call Isolate::Enter() here.
104
105 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
Yuki 2017/06/23 15:20:45 s/FIXME/TODO(peria)/
peria 2017/06/27 09:52:37 Acknowledged.
106 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
107 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback);
108 g_main_thread_per_isolate_data = this;
109 }
110
75 V8PerIsolateData::~V8PerIsolateData() {} 111 V8PerIsolateData::~V8PerIsolateData() {}
76 112
77 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { 113 v8::Isolate* V8PerIsolateData::MainThreadIsolate() {
78 DCHECK(g_main_thread_per_isolate_data); 114 DCHECK(g_main_thread_per_isolate_data);
79 return g_main_thread_per_isolate_data->GetIsolate(); 115 return g_main_thread_per_isolate_data->GetIsolate();
80 } 116 }
81 117
82 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner) { 118 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner,
83 V8PerIsolateData* data = new V8PerIsolateData(task_runner); 119 intptr_t* reference_table,
120 V8ContextMode context_mode) {
121 DCHECK(context_mode == V8ContextMode::kDontUseSnapshot || reference_table);
122
123 V8PerIsolateData* data = nullptr;
124 if (context_mode == V8ContextMode::kTakeSnapshot) {
125 CHECK(reference_table);
126 data = new V8PerIsolateData(reference_table);
127 } else {
128 if (!RuntimeEnabledFeatures::V8ContextSnapshotEnabled()) {
129 context_mode = V8ContextMode::kDontUseSnapshot;
130 reference_table = nullptr;
131 }
132 data = new V8PerIsolateData(task_runner, reference_table, context_mode);
133 }
134 DCHECK(data);
135
84 v8::Isolate* isolate = data->GetIsolate(); 136 v8::Isolate* isolate = data->GetIsolate();
85 isolate->SetData(gin::kEmbedderBlink, data); 137 isolate->SetData(gin::kEmbedderBlink, data);
86 return isolate; 138 return isolate;
87 } 139 }
88 140
89 void V8PerIsolateData::EnableIdleTasks( 141 void V8PerIsolateData::EnableIdleTasks(
90 v8::Isolate* isolate, 142 v8::Isolate* isolate,
91 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { 143 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) {
92 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); 144 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner));
93 } 145 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( 209 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(
158 GetIsolate(), callback, data, signature, length); 210 GetIsolate(), callback, data, signature, length);
159 templ->RemovePrototype(); 211 templ->RemovePrototype();
160 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); 212 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ));
161 return templ; 213 return templ;
162 } 214 }
163 215
164 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( 216 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate(
165 const DOMWrapperWorld& world, 217 const DOMWrapperWorld& world,
166 const void* key) { 218 const void* key) {
219 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) {
220 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key);
221 return interface_template_map_for_v8_snapshot_.Get(type);
222 }
223
167 auto& map = SelectInterfaceTemplateMap(world); 224 auto& map = SelectInterfaceTemplateMap(world);
168 auto result = map.find(key); 225 auto result = map.find(key);
169 if (result != map.end()) 226 if (result != map.end())
170 return result->value.Get(GetIsolate()); 227 return result->value.Get(GetIsolate());
171 return v8::Local<v8::FunctionTemplate>(); 228 return v8::Local<v8::FunctionTemplate>();
172 } 229 }
173 230
174 void V8PerIsolateData::SetInterfaceTemplate( 231 void V8PerIsolateData::SetInterfaceTemplate(
175 const DOMWrapperWorld& world, 232 const DOMWrapperWorld& world,
176 const void* key, 233 const void* key,
177 v8::Local<v8::FunctionTemplate> value) { 234 v8::Local<v8::FunctionTemplate> value) {
178 auto& map = SelectInterfaceTemplateMap(world); 235 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) {
179 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); 236 auto& map = interface_template_map_for_v8_snapshot_;
237 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key);
238 map.Set(type, value);
239 } else {
240 auto& map = SelectInterfaceTemplateMap(world);
241 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value));
242 }
243 }
244
245 void V8PerIsolateData::ClearPersistentsForV8Snapshot() {
246 interface_template_map_for_v8_snapshot_.Clear();
247 private_property_.reset();
180 } 248 }
181 249
182 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( 250 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache(
183 const void* lookup_key, 251 const void* lookup_key,
184 const char* const names[], 252 const char* const names[],
185 size_t count) { 253 size_t count) {
186 auto it = eternal_name_cache_.find(lookup_key); 254 auto it = eternal_name_cache_.find(lookup_key);
187 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; 255 const Vector<v8::Eternal<v8::Name>>* vector = nullptr;
188 if (UNLIKELY(it == eternal_name_cache_.end())) { 256 if (UNLIKELY(it == eternal_name_cache_.end())) {
189 v8::Isolate* isolate = this->GetIsolate(); 257 v8::Isolate* isolate = this->GetIsolate();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 ScriptWrappableVisitor* current = CurrentVisitor(); 371 ScriptWrappableVisitor* current = CurrentVisitor();
304 if (current) 372 if (current)
305 current->PerformCleanup(); 373 current->PerformCleanup();
306 374
307 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( 375 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap(
308 saved_visitor_); 376 saved_visitor_);
309 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); 377 isolate_->SetEmbedderHeapTracer(CurrentVisitor());
310 } 378 }
311 379
312 } // namespace blink 380 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698