OLD | NEW |
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 // SnapshotCreator enters the isolate, so we don't call Isolate::Enter() here. |
| 103 |
| 104 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. |
| 105 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); |
| 106 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); |
| 107 g_main_thread_per_isolate_data = this; |
| 108 } |
| 109 |
74 V8PerIsolateData::~V8PerIsolateData() {} | 110 V8PerIsolateData::~V8PerIsolateData() {} |
75 | 111 |
76 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { | 112 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { |
77 DCHECK(g_main_thread_per_isolate_data); | 113 DCHECK(g_main_thread_per_isolate_data); |
78 return g_main_thread_per_isolate_data->GetIsolate(); | 114 return g_main_thread_per_isolate_data->GetIsolate(); |
79 } | 115 } |
80 | 116 |
81 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner) { | 117 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner, |
82 V8PerIsolateData* data = new V8PerIsolateData(task_runner); | 118 intptr_t* reference_table, |
| 119 V8ContextMode context_mode) { |
| 120 DCHECK(context_mode == V8ContextMode::kDontUseSnapshot || reference_table); |
| 121 |
| 122 V8PerIsolateData* data = nullptr; |
| 123 if (context_mode == V8ContextMode::kTakeSnapshot) { |
| 124 CHECK(reference_table); |
| 125 data = new V8PerIsolateData(reference_table); |
| 126 } else { |
| 127 if (!RuntimeEnabledFeatures::V8ContextSnapshotEnabled()) { |
| 128 context_mode = V8ContextMode::kDontUseSnapshot; |
| 129 reference_table = nullptr; |
| 130 } |
| 131 data = new V8PerIsolateData(task_runner, reference_table, context_mode); |
| 132 } |
| 133 DCHECK(data); |
| 134 |
83 v8::Isolate* isolate = data->GetIsolate(); | 135 v8::Isolate* isolate = data->GetIsolate(); |
84 isolate->SetData(gin::kEmbedderBlink, data); | 136 isolate->SetData(gin::kEmbedderBlink, data); |
85 return isolate; | 137 return isolate; |
86 } | 138 } |
87 | 139 |
88 void V8PerIsolateData::EnableIdleTasks( | 140 void V8PerIsolateData::EnableIdleTasks( |
89 v8::Isolate* isolate, | 141 v8::Isolate* isolate, |
90 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { | 142 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { |
91 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); | 143 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); |
92 } | 144 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( | 208 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( |
157 GetIsolate(), callback, data, signature, length); | 209 GetIsolate(), callback, data, signature, length); |
158 templ->RemovePrototype(); | 210 templ->RemovePrototype(); |
159 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); | 211 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); |
160 return templ; | 212 return templ; |
161 } | 213 } |
162 | 214 |
163 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( | 215 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( |
164 const DOMWrapperWorld& world, | 216 const DOMWrapperWorld& world, |
165 const void* key) { | 217 const void* key) { |
| 218 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) { |
| 219 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key); |
| 220 return interface_template_map_for_v8_snapshot_.Get(type); |
| 221 } |
| 222 |
166 auto& map = SelectInterfaceTemplateMap(world); | 223 auto& map = SelectInterfaceTemplateMap(world); |
167 auto result = map.find(key); | 224 auto result = map.find(key); |
168 if (result != map.end()) | 225 if (result != map.end()) |
169 return result->value.Get(GetIsolate()); | 226 return result->value.Get(GetIsolate()); |
170 return v8::Local<v8::FunctionTemplate>(); | 227 return v8::Local<v8::FunctionTemplate>(); |
171 } | 228 } |
172 | 229 |
173 void V8PerIsolateData::SetInterfaceTemplate( | 230 void V8PerIsolateData::SetInterfaceTemplate( |
174 const DOMWrapperWorld& world, | 231 const DOMWrapperWorld& world, |
175 const void* key, | 232 const void* key, |
176 v8::Local<v8::FunctionTemplate> value) { | 233 v8::Local<v8::FunctionTemplate> value) { |
177 auto& map = SelectInterfaceTemplateMap(world); | 234 if (GetV8ContextMode() == V8ContextMode::kTakeSnapshot) { |
178 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); | 235 auto& map = interface_template_map_for_v8_snapshot_; |
| 236 const WrapperTypeInfo* type = reinterpret_cast<const WrapperTypeInfo*>(key); |
| 237 map.Set(type, value); |
| 238 } else { |
| 239 auto& map = SelectInterfaceTemplateMap(world); |
| 240 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); |
| 241 } |
| 242 } |
| 243 |
| 244 void V8PerIsolateData::ClearPersistentsForV8Snapshot() { |
| 245 interface_template_map_for_v8_snapshot_.Clear(); |
| 246 private_property_.reset(); |
179 } | 247 } |
180 | 248 |
181 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( | 249 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( |
182 const void* lookup_key, | 250 const void* lookup_key, |
183 const char* const names[], | 251 const char* const names[], |
184 size_t count) { | 252 size_t count) { |
185 auto it = eternal_name_cache_.find(lookup_key); | 253 auto it = eternal_name_cache_.find(lookup_key); |
186 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; | 254 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; |
187 if (UNLIKELY(it == eternal_name_cache_.end())) { | 255 if (UNLIKELY(it == eternal_name_cache_.end())) { |
188 v8::Isolate* isolate = this->GetIsolate(); | 256 v8::Isolate* isolate = this->GetIsolate(); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 ScriptWrappableVisitor* current = CurrentVisitor(); | 370 ScriptWrappableVisitor* current = CurrentVisitor(); |
303 if (current) | 371 if (current) |
304 current->PerformCleanup(); | 372 current->PerformCleanup(); |
305 | 373 |
306 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( | 374 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( |
307 saved_visitor_); | 375 saved_visitor_); |
308 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); | 376 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); |
309 } | 377 } |
310 | 378 |
311 } // namespace blink | 379 } // namespace blink |
OLD | NEW |