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

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 some comments 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 /* 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(
56 WebTaskRunner* task_runner,
57 intptr_t* table,
58 gin::IsolateHolder::V8ContextMode context_mode)
55 : isolate_holder_( 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 context_mode),
60 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))), 66 string_cache_(WTF::WrapUnique(new StringCache(GetIsolate()))),
61 private_property_(V8PrivateProperty::Create()), 67 private_property_(V8PrivateProperty::Create()),
62 constructor_mode_(ConstructorMode::kCreateNewObject), 68 constructor_mode_(ConstructorMode::kCreateNewObject),
63 use_counter_disabled_(false), 69 use_counter_disabled_(false),
64 is_handling_recursion_level_error_(false), 70 is_handling_recursion_level_error_(false),
65 is_reporting_exception_(false) { 71 is_reporting_exception_(false) {
66 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone. 72 // FIXME: Remove once all v8::Isolate::GetCurrent() calls are gone.
67 GetIsolate()->Enter(); 73 GetIsolate()->Enter();
68 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback); 74 GetIsolate()->AddBeforeCallEnteredCallback(&BeforeCallEnteredCallback);
69 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback); 75 GetIsolate()->AddMicrotasksCompletedCallback(&MicrotasksCompletedCallback);
70 if (IsMainThread()) 76 if (IsMainThread())
71 g_main_thread_per_isolate_data = this; 77 g_main_thread_per_isolate_data = this;
72 } 78 }
73 79
74 V8PerIsolateData::~V8PerIsolateData() {} 80 V8PerIsolateData::~V8PerIsolateData() {}
75 81
76 v8::Isolate* V8PerIsolateData::MainThreadIsolate() { 82 v8::Isolate* V8PerIsolateData::MainThreadIsolate() {
77 DCHECK(g_main_thread_per_isolate_data); 83 DCHECK(g_main_thread_per_isolate_data);
78 return g_main_thread_per_isolate_data->GetIsolate(); 84 return g_main_thread_per_isolate_data->GetIsolate();
79 } 85 }
80 86
81 v8::Isolate* V8PerIsolateData::Initialize(WebTaskRunner* task_runner) { 87 v8::Isolate* V8PerIsolateData::Initialize(
82 V8PerIsolateData* data = new V8PerIsolateData(task_runner); 88 WebTaskRunner* task_runner,
89 intptr_t* table,
90 gin::IsolateHolder::V8ContextMode context_mode) {
91 CHECK(context_mode == gin::IsolateHolder::kUseSnapshot ||
92 context_mode == gin::IsolateHolder::kTakeSnapshot);
93 if (context_mode == gin::IsolateHolder::kUseSnapshot &&
94 !RuntimeEnabledFeatures::v8ContextSnapshotEnabled()) {
95 context_mode = gin::IsolateHolder::kDefault;
96 }
97
98 V8PerIsolateData* data =
99 new V8PerIsolateData(task_runner, table, context_mode);
83 v8::Isolate* isolate = data->GetIsolate(); 100 v8::Isolate* isolate = data->GetIsolate();
84 isolate->SetData(gin::kEmbedderBlink, data); 101 isolate->SetData(gin::kEmbedderBlink, data);
85 return isolate; 102 return isolate;
86 } 103 }
87 104
88 void V8PerIsolateData::EnableIdleTasks( 105 void V8PerIsolateData::EnableIdleTasks(
89 v8::Isolate* isolate, 106 v8::Isolate* isolate,
90 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) { 107 std::unique_ptr<gin::V8IdleTaskRunner> task_runner) {
91 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner)); 108 From(isolate)->isolate_holder_.EnableIdleTasks(std::move(task_runner));
92 } 109 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New( 173 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(
157 GetIsolate(), callback, data, signature, length); 174 GetIsolate(), callback, data, signature, length);
158 templ->RemovePrototype(); 175 templ->RemovePrototype();
159 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ)); 176 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), templ));
160 return templ; 177 return templ;
161 } 178 }
162 179
163 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate( 180 v8::Local<v8::FunctionTemplate> V8PerIsolateData::FindInterfaceTemplate(
164 const DOMWrapperWorld& world, 181 const DOMWrapperWorld& world,
165 const void* key) { 182 const void* key) {
183 if (GetV8ContextMode() == gin::IsolateHolder::kTakeSnapshot) {
haraken 2017/05/20 19:10:03 I don't fully understand this code. Would you help
peria 2017/05/30 08:25:44 We have to remove all references from the embedder
184 auto& map = interface_template_persistent_map_;
185 auto result = map.find(key);
186 if (result != map.end())
187 return result->value.Get(GetIsolate());
188 return v8::Local<v8::FunctionTemplate>();
189 }
190
166 auto& map = SelectInterfaceTemplateMap(world); 191 auto& map = SelectInterfaceTemplateMap(world);
167 auto result = map.find(key); 192 auto result = map.find(key);
168 if (result != map.end()) 193 if (result != map.end())
169 return result->value.Get(GetIsolate()); 194 return result->value.Get(GetIsolate());
170 return v8::Local<v8::FunctionTemplate>(); 195 return v8::Local<v8::FunctionTemplate>();
171 } 196 }
172 197
173 void V8PerIsolateData::SetInterfaceTemplate( 198 void V8PerIsolateData::SetInterfaceTemplate(
174 const DOMWrapperWorld& world, 199 const DOMWrapperWorld& world,
175 const void* key, 200 const void* key,
176 v8::Local<v8::FunctionTemplate> value) { 201 v8::Local<v8::FunctionTemplate> value) {
177 auto& map = SelectInterfaceTemplateMap(world); 202 if (GetV8ContextMode() == gin::IsolateHolder::kTakeSnapshot) {
178 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value)); 203 auto& map = interface_template_persistent_map_;
204 map.insert(key, CopyablePersistent(GetIsolate(), value));
205 } else {
206 auto& map = SelectInterfaceTemplateMap(world);
207 map.insert(key, v8::Eternal<v8::FunctionTemplate>(GetIsolate(), value));
208 }
209 }
210
211 void V8PerIsolateData::ClearPersistents() {
212 interface_template_persistent_map_.clear();
213 private_property_.reset();
haraken 2017/05/20 19:10:03 Why do we need to clear private_property_?
peria 2017/05/30 08:25:44 To clear WindowDocumentCachedAccessor. https://chr
179 } 214 }
180 215
181 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache( 216 const v8::Eternal<v8::Name>* V8PerIsolateData::FindOrCreateEternalNameCache(
182 const void* lookup_key, 217 const void* lookup_key,
183 const char* const names[], 218 const char* const names[],
184 size_t count) { 219 size_t count) {
185 auto it = eternal_name_cache_.find(lookup_key); 220 auto it = eternal_name_cache_.find(lookup_key);
186 const Vector<v8::Eternal<v8::Name>>* vector = nullptr; 221 const Vector<v8::Eternal<v8::Name>>* vector = nullptr;
187 if (UNLIKELY(it == eternal_name_cache_.end())) { 222 if (UNLIKELY(it == eternal_name_cache_.end())) {
188 v8::Isolate* isolate = this->GetIsolate(); 223 v8::Isolate* isolate = this->GetIsolate();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 ScriptWrappableVisitor* current = CurrentVisitor(); 337 ScriptWrappableVisitor* current = CurrentVisitor();
303 if (current) 338 if (current)
304 current->PerformCleanup(); 339 current->PerformCleanup();
305 340
306 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap( 341 V8PerIsolateData::From(isolate_)->script_wrappable_visitor_.swap(
307 saved_visitor_); 342 saved_visitor_);
308 isolate_->SetEmbedderHeapTracer(CurrentVisitor()); 343 isolate_->SetEmbedderHeapTracer(CurrentVisitor());
309 } 344 }
310 345
311 } // namespace blink 346 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698