OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "bindings/core/v8/V8ContextSnapshot.h" |
| 6 |
| 7 #include <array> |
| 8 #include <cstring> |
| 9 |
| 10 #include "bindings/core/v8/GeneratedCodeHelper.h" |
| 11 #include "bindings/core/v8/V8Document.h" |
| 12 #include "bindings/core/v8/V8EventTarget.h" |
| 13 #include "bindings/core/v8/V8HTMLDocument.h" |
| 14 #include "bindings/core/v8/V8Initializer.h" |
| 15 #include "bindings/core/v8/V8Node.h" |
| 16 #include "bindings/core/v8/V8Window.h" |
| 17 #include "platform/bindings/DOMWrapperWorld.h" |
| 18 #include "platform/bindings/V8ObjectConstructor.h" |
| 19 #include "platform/bindings/V8PerIsolateData.h" |
| 20 #include "platform/bindings/V8PrivateProperty.h" |
| 21 #include "platform/instrumentation/tracing/TraceEvent.h" |
| 22 #include "v8/include/v8.h" |
| 23 |
| 24 namespace blink { |
| 25 |
| 26 namespace { |
| 27 |
| 28 intptr_t* g_v8_context_snapshot_reference_table = nullptr; |
| 29 |
| 30 // TODO(peria): This method is almost a copy of |
| 31 // V8PerContext::ConstructorForTypeSlowCase(), so merge with it. |
| 32 v8::Local<v8::Function> ConstructPlainType(v8::Isolate* isolate, |
| 33 const DOMWrapperWorld& world, |
| 34 v8::Local<v8::Context> context, |
| 35 const WrapperTypeInfo* type) { |
| 36 v8::Context::Scope scope(context); |
| 37 // We shouldn't reach this point for the types that are implemented in v8 such |
| 38 // as typed arrays and hence don't have domTemplateFunction. |
| 39 DCHECK(type->dom_template_function); |
| 40 v8::Local<v8::FunctionTemplate> interface_template = |
| 41 type->domTemplate(isolate, world); |
| 42 // Getting the function might fail if we're running out of stack or memory. |
| 43 v8::Local<v8::Function> interface_object = |
| 44 interface_template->GetFunction(context).ToLocalChecked(); |
| 45 |
| 46 if (type->parent_class) { |
| 47 v8::Local<v8::Object> prototype_template = |
| 48 ConstructPlainType(isolate, world, context, type->parent_class); |
| 49 CHECK(interface_object->SetPrototype(context, prototype_template) |
| 50 .ToChecked()); |
| 51 } |
| 52 |
| 53 v8::Local<v8::Value> prototype_value = |
| 54 interface_object->Get(context, V8AtomicString(isolate, "prototype")) |
| 55 .ToLocalChecked(); |
| 56 CHECK(prototype_value->IsObject()); |
| 57 v8::Local<v8::Object> prototype_object = prototype_value.As<v8::Object>(); |
| 58 if (prototype_object->InternalFieldCount() == |
| 59 kV8PrototypeInternalFieldcount && |
| 60 type->wrapper_type_prototype == |
| 61 WrapperTypeInfo::kWrapperTypeObjectPrototype) { |
| 62 prototype_object->SetAlignedPointerInInternalField( |
| 63 kV8PrototypeTypeIndex, const_cast<WrapperTypeInfo*>(type)); |
| 64 } |
| 65 type->PreparePrototypeAndInterfaceObject( |
| 66 context, world, prototype_object, interface_object, interface_template); |
| 67 |
| 68 return interface_object; |
| 69 } |
| 70 |
| 71 // TODO(peria): This method is almost a copy of |
| 72 // V8PerContext::CreateWrapperFromCacheSlowCase(), so merge with it. |
| 73 v8::Local<v8::Object> CreatePlainWrapper(v8::Isolate* isolate, |
| 74 const DOMWrapperWorld& world, |
| 75 v8::Local<v8::Context> context, |
| 76 const WrapperTypeInfo* type) { |
| 77 CHECK(V8HTMLDocument::wrapperTypeInfo.Equals(type)); |
| 78 |
| 79 v8::Context::Scope scope(context); |
| 80 v8::Local<v8::Function> interface_object = |
| 81 ConstructPlainType(isolate, world, context, type); |
| 82 CHECK(!interface_object.IsEmpty()); |
| 83 v8::Local<v8::Object> instance_template = |
| 84 V8ObjectConstructor::NewInstance(isolate, interface_object) |
| 85 .ToLocalChecked(); |
| 86 v8::Local<v8::Object> wrapper = instance_template->Clone(); |
| 87 wrapper->SetAlignedPointerInInternalField(kV8DOMWrapperTypeIndex, |
| 88 const_cast<WrapperTypeInfo*>(type)); |
| 89 return wrapper; |
| 90 } |
| 91 |
| 92 int GetSnapshotIndexForWorld(const DOMWrapperWorld& world) { |
| 93 return world.IsMainWorld() ? 0 : 1; |
| 94 } |
| 95 |
| 96 // Interface templates of those classes are stored in a snapshot without any |
| 97 // runtime enabled features, so we have to install runtime enabled features on |
| 98 // them after instantiation. |
| 99 struct SnapshotInterface { |
| 100 const WrapperTypeInfo* wrapper_type_info; |
| 101 InstallRuntimeEnabledFeaturesOnTemplateFunction install_function; |
| 102 }; |
| 103 SnapshotInterface g_snapshot_interfaces[] = { |
| 104 {&V8Window::wrapperTypeInfo, |
| 105 V8Window::InstallRuntimeEnabledFeaturesOnTemplate}, |
| 106 {&V8HTMLDocument::wrapperTypeInfo, |
| 107 V8HTMLDocument::InstallRuntimeEnabledFeaturesOnTemplate}, |
| 108 {&V8EventTarget::wrapperTypeInfo, |
| 109 V8EventTarget::InstallRuntimeEnabledFeaturesOnTemplate}, |
| 110 {&V8Node::wrapperTypeInfo, V8Node::InstallRuntimeEnabledFeaturesOnTemplate}, |
| 111 {&V8Document::wrapperTypeInfo, |
| 112 V8Document::InstallRuntimeEnabledFeaturesOnTemplate}, |
| 113 }; |
| 114 constexpr size_t kSnapshotInterfaceSize = |
| 115 WTF_ARRAY_LENGTH(g_snapshot_interfaces); |
| 116 |
| 117 enum class InternalFieldType : uint8_t { |
| 118 kNone, |
| 119 kNodeType, |
| 120 kDocumentType, |
| 121 kHTMLDocumentType, |
| 122 kHTMLDocumentObject, |
| 123 }; |
| 124 |
| 125 const WrapperTypeInfo* FieldTypeToWrapperTypeInfo(InternalFieldType type) { |
| 126 switch (type) { |
| 127 case InternalFieldType::kNone: |
| 128 NOTREACHED(); |
| 129 break; |
| 130 case InternalFieldType::kNodeType: |
| 131 return &V8Node::wrapperTypeInfo; |
| 132 case InternalFieldType::kDocumentType: |
| 133 return &V8Document::wrapperTypeInfo; |
| 134 case InternalFieldType::kHTMLDocumentType: |
| 135 return &V8HTMLDocument::wrapperTypeInfo; |
| 136 case InternalFieldType::kHTMLDocumentObject: |
| 137 return &V8HTMLDocument::wrapperTypeInfo; |
| 138 } |
| 139 NOTREACHED(); |
| 140 return nullptr; |
| 141 } |
| 142 |
| 143 struct DataForDeserializer { |
| 144 STACK_ALLOCATED(); |
| 145 Member<Document> document; |
| 146 }; |
| 147 |
| 148 int CountExternalReferenceEntries() { |
| 149 if (!g_v8_context_snapshot_reference_table) |
| 150 return 0; |
| 151 |
| 152 int count = 0; |
| 153 for (intptr_t* p = g_v8_context_snapshot_reference_table; *p; ++p) |
| 154 ++count; |
| 155 return count; |
| 156 } |
| 157 |
| 158 } // namespace |
| 159 |
| 160 v8::Local<v8::Context> V8ContextSnapshot::CreateContextFromSnapshot( |
| 161 v8::Isolate* isolate, |
| 162 const DOMWrapperWorld& world, |
| 163 v8::ExtensionConfiguration* extension_configuration, |
| 164 v8::Local<v8::Object> global_proxy, |
| 165 Document* document) { |
| 166 if (!CanCreateContextFromSnapshot(isolate, world, document)) { |
| 167 return v8::Local<v8::Context>(); |
| 168 } |
| 169 |
| 170 const int index = GetSnapshotIndexForWorld(world); |
| 171 DataForDeserializer data{document}; |
| 172 v8::DeserializeInternalFieldsCallback callback = |
| 173 v8::DeserializeInternalFieldsCallback(&DeserializeInternalField, &data); |
| 174 v8::Local<v8::Context> context = |
| 175 v8::Context::FromSnapshot(isolate, index, callback, |
| 176 extension_configuration, global_proxy) |
| 177 .ToLocalChecked(); |
| 178 VLOG(1) << "A context is created from snapshot for " |
| 179 << (world.IsMainWorld() ? "" : "non-") << "main world"; |
| 180 |
| 181 return context; |
| 182 } |
| 183 |
| 184 void V8ContextSnapshot::InstallRuntimeEnabledFeatures( |
| 185 v8::Local<v8::Context> context, |
| 186 Document* document) { |
| 187 ScriptState* script_state = ScriptState::From(context); |
| 188 v8::Isolate* isolate = script_state->GetIsolate(); |
| 189 const DOMWrapperWorld& world = script_state->World(); |
| 190 if (!CanCreateContextFromSnapshot(isolate, world, document)) { |
| 191 return; |
| 192 } |
| 193 |
| 194 TRACE_EVENT1("v8", "V8ContextSnapshot::InstallRuntimeEnabled", "IsMainFrame", |
| 195 world.IsMainWorld()); |
| 196 |
| 197 v8::Local<v8::String> prototype_str = V8AtomicString(isolate, "prototype"); |
| 198 V8PerContextData* data = script_state->PerContextData(); |
| 199 |
| 200 v8::Local<v8::Object> global_proxy = context->Global(); |
| 201 { |
| 202 v8::Local<v8::Object> window_wrapper = |
| 203 global_proxy->GetPrototype().As<v8::Object>(); |
| 204 const WrapperTypeInfo* type = &V8Window::wrapperTypeInfo; |
| 205 v8::Local<v8::Function> interface = data->ConstructorForType(type); |
| 206 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) |
| 207 .ToLocalChecked() |
| 208 .As<v8::Object>(); |
| 209 V8Window::install_runtime_enabled_features_function_( |
| 210 isolate, world, window_wrapper, prototype, interface); |
| 211 } |
| 212 { |
| 213 const WrapperTypeInfo* type = &V8EventTarget::wrapperTypeInfo; |
| 214 v8::Local<v8::Function> interface = data->ConstructorForType(type); |
| 215 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) |
| 216 .ToLocalChecked() |
| 217 .As<v8::Object>(); |
| 218 V8EventTarget::InstallRuntimeEnabledFeatures( |
| 219 isolate, world, v8::Local<v8::Object>(), prototype, interface); |
| 220 } |
| 221 |
| 222 if (!world.IsMainWorld()) { |
| 223 return; |
| 224 } |
| 225 |
| 226 // The below code handles window.document on the main world. |
| 227 { |
| 228 CHECK(document); |
| 229 DCHECK(document->IsHTMLDocument()); |
| 230 CHECK(document->ContainsWrapper()); |
| 231 v8::Local<v8::Object> document_wrapper = |
| 232 ToV8(document, global_proxy, isolate).As<v8::Object>(); |
| 233 const WrapperTypeInfo* type = &V8HTMLDocument::wrapperTypeInfo; |
| 234 v8::Local<v8::Function> interface = data->ConstructorForType(type); |
| 235 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) |
| 236 .ToLocalChecked() |
| 237 .As<v8::Object>(); |
| 238 V8HTMLDocument::InstallRuntimeEnabledFeatures( |
| 239 isolate, world, document_wrapper, prototype, interface); |
| 240 } |
| 241 { |
| 242 const WrapperTypeInfo* type = &V8Document::wrapperTypeInfo; |
| 243 v8::Local<v8::Function> interface = data->ConstructorForType(type); |
| 244 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) |
| 245 .ToLocalChecked() |
| 246 .As<v8::Object>(); |
| 247 V8Document::InstallRuntimeEnabledFeatures( |
| 248 isolate, world, v8::Local<v8::Object>(), prototype, interface); |
| 249 } |
| 250 { |
| 251 const WrapperTypeInfo* type = &V8Node::wrapperTypeInfo; |
| 252 v8::Local<v8::Function> interface = data->ConstructorForType(type); |
| 253 v8::Local<v8::Object> prototype = interface->Get(context, prototype_str) |
| 254 .ToLocalChecked() |
| 255 .As<v8::Object>(); |
| 256 V8Node::InstallRuntimeEnabledFeatures( |
| 257 isolate, world, v8::Local<v8::Object>(), prototype, interface); |
| 258 } |
| 259 } |
| 260 |
| 261 void V8ContextSnapshot::EnsureInterfaceTemplates(v8::Isolate* isolate) { |
| 262 if (V8PerIsolateData::From(isolate)->GetV8ContextSnapshotMode() != |
| 263 V8PerIsolateData::V8ContextSnapshotMode::kUseSnapshot) { |
| 264 return; |
| 265 } |
| 266 |
| 267 v8::HandleScope handle_scope(isolate); |
| 268 SnapshotInterface& snapshot_window = g_snapshot_interfaces[0]; |
| 269 DCHECK(V8Window::wrapperTypeInfo.Equals(snapshot_window.wrapper_type_info)); |
| 270 // Update the install function for V8Window to work for partial interfaces. |
| 271 snapshot_window.install_function = |
| 272 V8Window::install_runtime_enabled_features_on_template_function_; |
| 273 |
| 274 EnsureInterfaceTemplatesForWorld(isolate, DOMWrapperWorld::MainWorld()); |
| 275 // Any world types other than |kMain| are acceptable for this. |
| 276 RefPtr<DOMWrapperWorld> isolated_world = DOMWrapperWorld::Create( |
| 277 isolate, DOMWrapperWorld::WorldType::kForV8ContextSnapshotNonMain); |
| 278 EnsureInterfaceTemplatesForWorld(isolate, *isolated_world); |
| 279 } |
| 280 |
| 281 void V8ContextSnapshot::SetReferenceTable(intptr_t* table) { |
| 282 DCHECK(!g_v8_context_snapshot_reference_table); |
| 283 g_v8_context_snapshot_reference_table = table; |
| 284 } |
| 285 |
| 286 intptr_t* V8ContextSnapshot::GetReferenceTable() { |
| 287 return g_v8_context_snapshot_reference_table; |
| 288 } |
| 289 |
| 290 v8::StartupData V8ContextSnapshot::TakeSnapshot() { |
| 291 DCHECK_EQ(V8PerIsolateData::From(V8PerIsolateData::MainThreadIsolate()) |
| 292 ->GetV8ContextSnapshotMode(), |
| 293 V8PerIsolateData::V8ContextSnapshotMode::kTakeSnapshot); |
| 294 |
| 295 v8::SnapshotCreator* creator = |
| 296 V8PerIsolateData::From(V8PerIsolateData::MainThreadIsolate()) |
| 297 ->GetSnapshotCreator(); |
| 298 v8::Isolate* isolate = creator->GetIsolate(); |
| 299 CHECK_EQ(isolate, v8::Isolate::GetCurrent()); |
| 300 |
| 301 VLOG(1) << "External reference table has " << CountExternalReferenceEntries() |
| 302 << " entries."; |
| 303 |
| 304 // Disable all runtime enabled features |
| 305 RuntimeEnabledFeatures::SetStableFeaturesEnabled(false); |
| 306 RuntimeEnabledFeatures::SetExperimentalFeaturesEnabled(false); |
| 307 RuntimeEnabledFeatures::SetTestFeaturesEnabled(false); |
| 308 |
| 309 { |
| 310 v8::HandleScope handleScope(isolate); |
| 311 creator->SetDefaultContext(v8::Context::New(isolate)); |
| 312 |
| 313 TakeSnapshotForWorld(creator, DOMWrapperWorld::MainWorld()); |
| 314 // For non main worlds, we can use any type to create a context. |
| 315 TakeSnapshotForWorld( |
| 316 creator, |
| 317 *DOMWrapperWorld::Create( |
| 318 isolate, DOMWrapperWorld::WorldType::kForV8ContextSnapshotNonMain)); |
| 319 } |
| 320 |
| 321 isolate->RemoveMessageListeners(V8Initializer::MessageHandlerInMainThread); |
| 322 |
| 323 return creator->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); |
| 324 } |
| 325 |
| 326 v8::StartupData V8ContextSnapshot::SerializeInternalField( |
| 327 v8::Local<v8::Object> object, |
| 328 int index, |
| 329 void*) { |
| 330 InternalFieldType field_type = InternalFieldType::kNone; |
| 331 const WrapperTypeInfo* wrapper_type = ToWrapperTypeInfo(object); |
| 332 if (kV8DOMWrapperObjectIndex == index) { |
| 333 if (blink::V8HTMLDocument::wrapperTypeInfo.Equals(wrapper_type)) { |
| 334 field_type = InternalFieldType::kHTMLDocumentObject; |
| 335 } |
| 336 DCHECK_LE(kV8DefaultWrapperInternalFieldCount, |
| 337 object->InternalFieldCount()); |
| 338 } else if (kV8DOMWrapperTypeIndex == index) { |
| 339 if (blink::V8HTMLDocument::wrapperTypeInfo.Equals(wrapper_type)) { |
| 340 field_type = InternalFieldType::kHTMLDocumentType; |
| 341 } else if (blink::V8Document::wrapperTypeInfo.Equals(wrapper_type)) { |
| 342 field_type = InternalFieldType::kDocumentType; |
| 343 } else if (blink::V8Node::wrapperTypeInfo.Equals(wrapper_type)) { |
| 344 field_type = InternalFieldType::kNodeType; |
| 345 } |
| 346 DCHECK_LE(kV8PrototypeInternalFieldcount, object->InternalFieldCount()); |
| 347 } |
| 348 CHECK_NE(field_type, InternalFieldType::kNone); |
| 349 |
| 350 int size = sizeof(InternalFieldType); |
| 351 // Allocated memory on |data| will be released in |
| 352 // v8::i::PartialSerializer::SerializeEmbedderFields(). |
| 353 char* data = new char[size]; |
| 354 std::memcpy(data, &field_type, size); |
| 355 |
| 356 return {data, size}; |
| 357 } |
| 358 |
| 359 void V8ContextSnapshot::DeserializeInternalField(v8::Local<v8::Object> object, |
| 360 int index, |
| 361 v8::StartupData payload, |
| 362 void* ptr) { |
| 363 // DeserializeInternalField() expects to be called in the main world |
| 364 // with |document| being HTMLDocument. |
| 365 CHECK_EQ(payload.raw_size, static_cast<int>(sizeof(InternalFieldType))); |
| 366 InternalFieldType type = |
| 367 *reinterpret_cast<const InternalFieldType*>(payload.data); |
| 368 |
| 369 const WrapperTypeInfo* wrapper_type_info = FieldTypeToWrapperTypeInfo(type); |
| 370 switch (type) { |
| 371 case InternalFieldType::kNodeType: |
| 372 case InternalFieldType::kDocumentType: |
| 373 case InternalFieldType::kHTMLDocumentType: { |
| 374 CHECK_EQ(index, kV8DOMWrapperTypeIndex); |
| 375 object->SetAlignedPointerInInternalField( |
| 376 index, const_cast<WrapperTypeInfo*>(wrapper_type_info)); |
| 377 return; |
| 378 } |
| 379 case InternalFieldType::kHTMLDocumentObject: { |
| 380 // The below code handles window.document on the main world. |
| 381 CHECK_EQ(index, kV8DOMWrapperObjectIndex); |
| 382 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 383 DataForDeserializer* data = static_cast<DataForDeserializer*>(ptr); |
| 384 ScriptWrappable* document = data->document; |
| 385 DCHECK(document); |
| 386 |
| 387 // Make reference from wrapper to document |
| 388 object->SetAlignedPointerInInternalField(index, document); |
| 389 // Make reference from document to wrapper |
| 390 CHECK(document->SetWrapper(isolate, wrapper_type_info, object)); |
| 391 WrapperTypeInfo::WrapperCreated(); |
| 392 return; |
| 393 } |
| 394 case InternalFieldType::kNone: |
| 395 NOTREACHED(); |
| 396 return; |
| 397 } |
| 398 |
| 399 NOTREACHED(); |
| 400 } |
| 401 |
| 402 bool V8ContextSnapshot::CanCreateContextFromSnapshot( |
| 403 v8::Isolate* isolate, |
| 404 const DOMWrapperWorld& world, |
| 405 Document* document) { |
| 406 DCHECK(document); |
| 407 if (V8PerIsolateData::From(isolate)->GetV8ContextSnapshotMode() != |
| 408 V8PerIsolateData::V8ContextSnapshotMode::kUseSnapshot) { |
| 409 return false; |
| 410 } |
| 411 |
| 412 // When creating a context for the main world from snapshot, we also need a |
| 413 // HTMLDocument instance. If typeof window.document is not HTMLDocument, e.g. |
| 414 // SVGDocument or XMLDocument, we can't create contexts from the snapshot. |
| 415 return !world.IsMainWorld() || document->IsHTMLDocument(); |
| 416 } |
| 417 |
| 418 void V8ContextSnapshot::EnsureInterfaceTemplatesForWorld( |
| 419 v8::Isolate* isolate, |
| 420 const DOMWrapperWorld& world) { |
| 421 V8PerIsolateData* data = V8PerIsolateData::From(isolate); |
| 422 |
| 423 // A snapshot has some interface templates in it. The first |
| 424 // |kSnapshotInterfaceSize| templates are for the main world, and the |
| 425 // remaining templates are for isolated worlds. |
| 426 const int index_offset = world.IsMainWorld() ? 0 : kSnapshotInterfaceSize; |
| 427 |
| 428 for (size_t i = 0; i < kSnapshotInterfaceSize; ++i) { |
| 429 auto& snapshot_interface = g_snapshot_interfaces[i]; |
| 430 const WrapperTypeInfo* wrapper_type_info = |
| 431 snapshot_interface.wrapper_type_info; |
| 432 v8::Local<v8::FunctionTemplate> interface_template = |
| 433 v8::FunctionTemplate::FromSnapshot(isolate, index_offset + i) |
| 434 .ToLocalChecked(); |
| 435 snapshot_interface.install_function(isolate, world, interface_template); |
| 436 CHECK(!interface_template.IsEmpty()); |
| 437 data->SetInterfaceTemplate(world, wrapper_type_info, interface_template); |
| 438 } |
| 439 } |
| 440 |
| 441 void V8ContextSnapshot::TakeSnapshotForWorld(v8::SnapshotCreator* creator, |
| 442 const DOMWrapperWorld& world) { |
| 443 v8::Isolate* isolate = creator->GetIsolate(); |
| 444 CHECK_EQ(isolate, v8::Isolate::GetCurrent()); |
| 445 |
| 446 // Function templates |
| 447 v8::HandleScope handleScope(isolate); |
| 448 std::array<v8::Local<v8::FunctionTemplate>, kSnapshotInterfaceSize> |
| 449 interface_templates; |
| 450 v8::Local<v8::FunctionTemplate> window_template; |
| 451 for (size_t i = 0; i < kSnapshotInterfaceSize; ++i) { |
| 452 const WrapperTypeInfo* wrapper_type_info = |
| 453 g_snapshot_interfaces[i].wrapper_type_info; |
| 454 v8::Local<v8::FunctionTemplate> interface_template = |
| 455 wrapper_type_info->domTemplate(isolate, world); |
| 456 CHECK(!interface_template.IsEmpty()); |
| 457 interface_templates[i] = interface_template; |
| 458 if (V8Window::wrapperTypeInfo.Equals(wrapper_type_info)) { |
| 459 window_template = interface_template; |
| 460 } |
| 461 } |
| 462 CHECK(!window_template.IsEmpty()); |
| 463 |
| 464 v8::Local<v8::ObjectTemplate> window_instance_template = |
| 465 window_template->InstanceTemplate(); |
| 466 CHECK(!window_instance_template.IsEmpty()); |
| 467 |
| 468 v8::Local<v8::Context> context; |
| 469 { |
| 470 V8PerIsolateData::UseCounterDisabledScope use_counter_disabled( |
| 471 V8PerIsolateData::From(isolate)); |
| 472 context = v8::Context::New(isolate, nullptr, window_instance_template); |
| 473 } |
| 474 CHECK(!context.IsEmpty()); |
| 475 |
| 476 // For the main world context, we need to prepare a HTMLDocument wrapper and |
| 477 // set it to window.documnt. |
| 478 if (world.IsMainWorld()) { |
| 479 v8::Context::Scope scope(context); |
| 480 v8::Local<v8::Object> document_wrapper = CreatePlainWrapper( |
| 481 isolate, world, context, &V8HTMLDocument::wrapperTypeInfo); |
| 482 int indices[] = {kV8DOMWrapperObjectIndex, kV8DOMWrapperTypeIndex}; |
| 483 void* values[] = {nullptr, const_cast<WrapperTypeInfo*>( |
| 484 &V8HTMLDocument::wrapperTypeInfo)}; |
| 485 document_wrapper->SetAlignedPointerInInternalFields( |
| 486 WTF_ARRAY_LENGTH(indices), indices, values); |
| 487 |
| 488 // Set the cached accessor for window.document. |
| 489 CHECK(V8PrivateProperty::GetWindowDocumentCachedAccessor(isolate).Set( |
| 490 context->Global(), document_wrapper)); |
| 491 } |
| 492 |
| 493 for (auto& interface_template : interface_templates) { |
| 494 creator->AddTemplate(interface_template); |
| 495 } |
| 496 creator->AddContext(context, SerializeInternalField); |
| 497 |
| 498 V8PerIsolateData::From(isolate)->ClearPersistentsForV8ContextSnapshot(); |
| 499 } |
| 500 |
| 501 } // namespace blink |
OLD | NEW |