OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2008, 2009, 2011 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 are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 24 matching lines...) Expand all Loading... | |
35 #include "bindings/core/v8/ScriptController.h" | 35 #include "bindings/core/v8/ScriptController.h" |
36 #include "bindings/core/v8/ToV8ForCore.h" | 36 #include "bindings/core/v8/ToV8ForCore.h" |
37 #include "bindings/core/v8/V8BindingForCore.h" | 37 #include "bindings/core/v8/V8BindingForCore.h" |
38 #include "bindings/core/v8/V8DOMActivityLogger.h" | 38 #include "bindings/core/v8/V8DOMActivityLogger.h" |
39 #include "bindings/core/v8/V8DOMWrapper.h" | 39 #include "bindings/core/v8/V8DOMWrapper.h" |
40 #include "bindings/core/v8/V8GCForContextDispose.h" | 40 #include "bindings/core/v8/V8GCForContextDispose.h" |
41 #include "bindings/core/v8/V8HTMLDocument.h" | 41 #include "bindings/core/v8/V8HTMLDocument.h" |
42 #include "bindings/core/v8/V8Initializer.h" | 42 #include "bindings/core/v8/V8Initializer.h" |
43 #include "bindings/core/v8/V8PagePopupControllerBinding.h" | 43 #include "bindings/core/v8/V8PagePopupControllerBinding.h" |
44 #include "bindings/core/v8/V8PrivateProperty.h" | 44 #include "bindings/core/v8/V8PrivateProperty.h" |
45 #include "bindings/core/v8/V8SnapshotCreator.h" | |
45 #include "bindings/core/v8/V8Window.h" | 46 #include "bindings/core/v8/V8Window.h" |
46 #include "core/dom/Modulator.h" | 47 #include "core/dom/Modulator.h" |
47 #include "core/frame/LocalFrame.h" | 48 #include "core/frame/LocalFrame.h" |
48 #include "core/frame/LocalFrameClient.h" | 49 #include "core/frame/LocalFrameClient.h" |
49 #include "core/frame/csp/ContentSecurityPolicy.h" | 50 #include "core/frame/csp/ContentSecurityPolicy.h" |
50 #include "core/html/DocumentNameCollection.h" | 51 #include "core/html/DocumentNameCollection.h" |
51 #include "core/html/HTMLIFrameElement.h" | 52 #include "core/html/HTMLIFrameElement.h" |
52 #include "core/inspector/MainThreadDebugger.h" | 53 #include "core/inspector/MainThreadDebugger.h" |
53 #include "core/loader/FrameLoader.h" | 54 #include "core/loader/FrameLoader.h" |
54 #include "core/origin_trials/OriginTrialContext.h" | 55 #include "core/origin_trials/OriginTrialContext.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 context->AllowCodeGenerationFromStrings(csp->AllowEval( | 151 context->AllowCodeGenerationFromStrings(csp->AllowEval( |
151 0, SecurityViolationReportingPolicy::kSuppressReporting)); | 152 0, SecurityViolationReportingPolicy::kSuppressReporting)); |
152 context->SetErrorMessageForCodeGenerationFromStrings( | 153 context->SetErrorMessageForCodeGenerationFromStrings( |
153 V8String(GetIsolate(), csp->EvalDisabledErrorMessage())); | 154 V8String(GetIsolate(), csp->EvalDisabledErrorMessage())); |
154 } else { | 155 } else { |
155 UpdateActivityLogger(); | 156 UpdateActivityLogger(); |
156 origin = world_->IsolatedWorldSecurityOrigin(); | 157 origin = world_->IsolatedWorldSecurityOrigin(); |
157 SetSecurityToken(origin); | 158 SetSecurityToken(origin); |
158 } | 159 } |
159 | 160 |
160 MainThreadDebugger::Instance()->ContextCreated(script_state_.Get(), | 161 { |
161 GetFrame(), origin); | 162 TRACE_EVENT1("v8", "notification", "isMainWindow", |
162 GetFrame()->Loader().Client()->DidCreateScriptContext(context, | 163 GetFrame()->IsMainFrame()); |
163 world_->GetWorldId()); | 164 MainThreadDebugger::Instance()->ContextCreated(script_state_.Get(), |
164 // If conditional features for window have been queued before the V8 context | 165 GetFrame(), origin); |
165 // was ready, then inject them into the context now | 166 GetFrame()->Loader().Client()->DidCreateScriptContext(context, |
166 if (world_->IsMainWorld()) { | 167 world_->GetWorldId()); |
167 InstallConditionalFeaturesOnWindow(script_state_.Get()); | 168 // If conditional features for window have been queued before the V8 context |
169 // was ready, then inject them into the context now | |
170 if (world_->IsMainWorld()) { | |
171 InstallConditionalFeaturesOnWindow(script_state_.Get()); | |
172 GetFrame()->Loader().DispatchDidClearWindowObjectInMainWorld(); | |
173 } | |
168 } | 174 } |
169 | |
170 if (world_->IsMainWorld()) | |
171 GetFrame()->Loader().DispatchDidClearWindowObjectInMainWorld(); | |
172 } | 175 } |
173 | 176 |
177 namespace { | |
178 | |
179 struct DataForDeserializer { | |
180 STACK_ALLOCATED(); | |
181 Member<Document> document; | |
182 }; | |
183 | |
184 const WrapperTypeInfo* FieldTypeToWrapperTypeInfo( | |
185 V8SnapshotCreator::FieldType type) { | |
186 switch (type) { | |
187 case V8SnapshotCreator::kNodeType: | |
188 return &V8Node::wrapperTypeInfo; | |
189 case V8SnapshotCreator::kDocumentType: | |
190 return &V8Document::wrapperTypeInfo; | |
191 case V8SnapshotCreator::kHTMLDocumentType: | |
192 return &V8HTMLDocument::wrapperTypeInfo; | |
193 case V8SnapshotCreator::kHTMLDocumentObject: | |
194 return &V8HTMLDocument::wrapperTypeInfo; | |
195 case V8SnapshotCreator::kNone: | |
196 NOTREACHED(); | |
197 break; | |
198 } | |
199 NOTREACHED(); | |
200 return nullptr; | |
201 } | |
202 | |
203 void deserialize(v8::Local<v8::Object> wrapper, | |
204 int index, | |
205 v8::StartupData payload, | |
206 void* ptr) { | |
207 CHECK_EQ(payload.raw_size, | |
208 static_cast<int>(sizeof(V8SnapshotCreator::FieldType))); | |
209 V8SnapshotCreator::FieldType type = | |
210 *reinterpret_cast<const V8SnapshotCreator::FieldType*>(payload.data); | |
211 | |
212 const WrapperTypeInfo* wrapper_type_info = FieldTypeToWrapperTypeInfo(type); | |
213 switch (type) { | |
214 case V8SnapshotCreator::kNodeType: | |
215 case V8SnapshotCreator::kDocumentType: | |
216 case V8SnapshotCreator::kHTMLDocumentType: { | |
217 CHECK_EQ(index, kV8DOMWrapperTypeIndex); | |
218 wrapper->SetAlignedPointerInInternalField( | |
219 index, const_cast<WrapperTypeInfo*>(wrapper_type_info)); | |
220 wrapper_type_info->WrapperCreated(); | |
221 break; | |
222 } | |
223 case V8SnapshotCreator::kHTMLDocumentObject: { | |
224 CHECK_EQ(index, kV8DOMWrapperObjectIndex); | |
225 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
226 DataForDeserializer* data = static_cast<DataForDeserializer*>(ptr); | |
227 ScriptWrappable* document = data->document; | |
228 | |
229 // Make reference from wrapper to document | |
230 wrapper->SetAlignedPointerInInternalField(index, document); | |
231 auto per_isolate_data = V8PerIsolateData::From(isolate); | |
232 per_isolate_data->GetScriptWrappableVisitor()->RegisterV8Reference( | |
Michael Lippautz
2017/05/04 17:30:23
Line 232 is not needed. SetWrapper will take care
peria
2017/05/09 03:19:15
Done.
The issue I asked you had gone with your cha
| |
233 std::make_pair(const_cast<WrapperTypeInfo*>(wrapper_type_info), | |
234 document)); | |
235 // Make reference from document to wrapper | |
236 CHECK(document->SetWrapper(isolate, wrapper_type_info, wrapper)); | |
237 break; | |
238 } | |
239 case V8SnapshotCreator::kNone: | |
240 NOTREACHED(); | |
241 break; | |
242 } | |
243 } | |
244 | |
245 } // namespace | |
246 | |
174 void LocalWindowProxy::CreateContext() { | 247 void LocalWindowProxy::CreateContext() { |
175 // Create a new v8::Context with the window object as the global object | 248 TRACE_EVENT1("v8", "LocalWindowProxy::CreateContext", "isMainWindow", |
176 // (aka the inner global). Reuse the outer global proxy if it already exists. | 249 GetFrame()->IsMainFrame()); |
177 v8::Local<v8::ObjectTemplate> global_template = | 250 |
178 V8Window::domTemplate(GetIsolate(), *world_)->InstanceTemplate(); | 251 if (V8PerIsolateData::From(GetIsolate())->UseSnapshot()) { |
179 CHECK(!global_template.IsEmpty()); | 252 // To store function templates into V8PerIsolateData. |
253 v8::Local<v8::FunctionTemplate> eventTargetFunctionTemplate = | |
254 V8EventTarget::domTemplate(GetIsolate(), World()); | |
255 CHECK(!eventTargetFunctionTemplate.IsEmpty()); | |
256 v8::Local<v8::FunctionTemplate> windowFunctionTemplate = | |
257 V8Window::domTemplate(GetIsolate(), World()); | |
258 CHECK(!windowFunctionTemplate.IsEmpty()); | |
259 v8::Local<v8::FunctionTemplate> nodeFunctionTemplate = | |
260 V8Node::domTemplate(GetIsolate(), World()); | |
261 CHECK(!nodeFunctionTemplate.IsEmpty()); | |
262 v8::Local<v8::FunctionTemplate> documentFunctionTemplate = | |
263 V8Document::domTemplate(GetIsolate(), World()); | |
264 CHECK(!documentFunctionTemplate.IsEmpty()); | |
265 v8::Local<v8::FunctionTemplate> htmlDocumentFunctionTemplate = | |
266 V8HTMLDocument::domTemplate(GetIsolate(), World()); | |
267 CHECK(!htmlDocumentFunctionTemplate.IsEmpty()); | |
268 } | |
180 | 269 |
181 Vector<const char*> extension_names; | 270 Vector<const char*> extension_names; |
182 // Dynamically tell v8 about our extensions now. | 271 // Dynamically tell v8 about our extensions now. |
183 if (GetFrame()->Loader().Client()->AllowScriptExtensions()) { | 272 if (GetFrame()->Loader().Client()->AllowScriptExtensions()) { |
184 const V8Extensions& extensions = ScriptController::RegisteredExtensions(); | 273 const V8Extensions& extensions = ScriptController::RegisteredExtensions(); |
185 extension_names.ReserveInitialCapacity(extensions.size()); | 274 extension_names.ReserveInitialCapacity(extensions.size()); |
186 for (const auto* extension : extensions) | 275 for (const auto* extension : extensions) |
187 extension_names.push_back(extension->name()); | 276 extension_names.push_back(extension->name()); |
188 } | 277 } |
189 v8::ExtensionConfiguration extension_configuration(extension_names.size(), | 278 v8::ExtensionConfiguration extension_configuration(extension_names.size(), |
190 extension_names.data()); | 279 extension_names.data()); |
191 | 280 |
192 v8::Local<v8::Context> context; | 281 v8::Local<v8::Context> context; |
193 { | 282 { |
283 V8PerIsolateData* per_isolate_data = V8PerIsolateData::From(GetIsolate()); | |
194 V8PerIsolateData::UseCounterDisabledScope use_counter_disabled( | 284 V8PerIsolateData::UseCounterDisabledScope use_counter_disabled( |
195 V8PerIsolateData::From(GetIsolate())); | 285 V8PerIsolateData::From(GetIsolate())); |
196 context = | 286 TRACE_EVENT1("v8", "contextCreation", "isMainWindow", |
197 v8::Context::New(GetIsolate(), &extension_configuration, | 287 GetFrame()->IsMainFrame()); |
198 global_template, global_proxy_.NewLocal(GetIsolate())); | 288 |
289 v8::Local<v8::Object> global_proxy = global_proxy_.NewLocal(GetIsolate()); | |
290 Document* document = GetFrame()->GetDocument(); | |
291 | |
292 if (per_isolate_data->UseSnapshot() && | |
293 (!World().IsMainWorld() || (document && document->IsHTMLDocument()))) { | |
294 const int index = World().IsMainWorld() ? 0 : 1; | |
295 DataForDeserializer data{document}; | |
296 CHECK(v8::Context::FromSnapshot( | |
297 GetIsolate(), index, | |
298 v8::DeserializeInternalFieldsCallback(&deserialize, &data), | |
299 &extension_configuration, global_proxy) | |
300 .ToLocal(&context)); | |
301 } | |
302 | |
303 if (context.IsEmpty()) { | |
304 v8::Local<v8::ObjectTemplate> global_template = | |
305 V8Window::domTemplate(GetIsolate(), *world_)->InstanceTemplate(); | |
306 CHECK(!global_template.IsEmpty()); | |
307 context = v8::Context::New(GetIsolate(), &extension_configuration, | |
308 global_template, global_proxy); | |
309 } | |
199 } | 310 } |
200 CHECK(!context.IsEmpty()); | 311 CHECK(!context.IsEmpty()); |
201 | 312 |
202 #if DCHECK_IS_ON() | 313 #if DCHECK_IS_ON() |
203 DidAttachGlobalObject(); | 314 DidAttachGlobalObject(); |
204 #endif | 315 #endif |
205 | 316 |
206 script_state_ = ScriptState::Create(context, world_); | 317 script_state_ = ScriptState::Create(context, world_); |
207 | 318 |
208 DCHECK(lifecycle_ == Lifecycle::kContextIsUninitialized || | 319 DCHECK(lifecycle_ == Lifecycle::kContextIsUninitialized || |
209 lifecycle_ == Lifecycle::kGlobalObjectIsDetached); | 320 lifecycle_ == Lifecycle::kGlobalObjectIsDetached); |
210 lifecycle_ = Lifecycle::kContextIsInitialized; | 321 lifecycle_ = Lifecycle::kContextIsInitialized; |
211 DCHECK(script_state_->ContextIsValid()); | 322 DCHECK(script_state_->ContextIsValid()); |
212 } | 323 } |
213 | 324 |
214 void LocalWindowProxy::SetupWindowPrototypeChain() { | 325 void LocalWindowProxy::SetupWindowPrototypeChain() { |
326 TRACE_EVENT1("v8", "LocalWindowProxy::setupWindowPrototypeChain", | |
327 "isMainWindow", GetFrame()->IsMainFrame()); | |
328 | |
215 // Associate the window wrapper object and its prototype chain with the | 329 // Associate the window wrapper object and its prototype chain with the |
216 // corresponding native DOMWindow object. | 330 // corresponding native DOMWindow object. |
217 DOMWindow* window = GetFrame()->DomWindow(); | 331 DOMWindow* window = GetFrame()->DomWindow(); |
218 const WrapperTypeInfo* wrapper_type_info = window->GetWrapperTypeInfo(); | 332 const WrapperTypeInfo* wrapper_type_info = window->GetWrapperTypeInfo(); |
219 v8::Local<v8::Context> context = script_state_->GetContext(); | 333 v8::Local<v8::Context> context = script_state_->GetContext(); |
220 | 334 |
221 // The global proxy object. Note this is not the global object. | 335 // The global proxy object. Note this is not the global object. |
222 v8::Local<v8::Object> global_proxy = context->Global(); | 336 v8::Local<v8::Object> global_proxy = context->Global(); |
223 CHECK(global_proxy_ == global_proxy); | 337 CHECK(global_proxy_ == global_proxy); |
224 V8DOMWrapper::SetNativeInfo(GetIsolate(), global_proxy, wrapper_type_info, | 338 V8DOMWrapper::SetNativeInfo(GetIsolate(), global_proxy, wrapper_type_info, |
(...skipping 16 matching lines...) Expand all Loading... | |
241 V8DOMWrapper::SetNativeInfo(GetIsolate(), window_prototype, wrapper_type_info, | 355 V8DOMWrapper::SetNativeInfo(GetIsolate(), window_prototype, wrapper_type_info, |
242 window); | 356 window); |
243 | 357 |
244 // The named properties object of Window interface. | 358 // The named properties object of Window interface. |
245 v8::Local<v8::Object> window_properties = | 359 v8::Local<v8::Object> window_properties = |
246 window_prototype->GetPrototype().As<v8::Object>(); | 360 window_prototype->GetPrototype().As<v8::Object>(); |
247 CHECK(!window_properties.IsEmpty()); | 361 CHECK(!window_properties.IsEmpty()); |
248 V8DOMWrapper::SetNativeInfo(GetIsolate(), window_properties, | 362 V8DOMWrapper::SetNativeInfo(GetIsolate(), window_properties, |
249 wrapper_type_info, window); | 363 wrapper_type_info, window); |
250 | 364 |
365 if (V8PerIsolateData::From(GetIsolate())->UseSnapshot()) { | |
366 v8::Local<v8::Function> window_interface = | |
367 V8Window::domTemplate(GetIsolate(), World())->GetFunction(); | |
368 V8Window::installV8WindowRuntimeEnabledFunction( | |
369 GetIsolate(), World(), window_wrapper, window_prototype, | |
370 window_interface); | |
371 } | |
372 | |
251 // TODO(keishi): Remove installPagePopupController and implement | 373 // TODO(keishi): Remove installPagePopupController and implement |
252 // PagePopupController in another way. | 374 // PagePopupController in another way. |
253 V8PagePopupControllerBinding::InstallPagePopupController(context, | 375 V8PagePopupControllerBinding::InstallPagePopupController(context, |
254 window_wrapper); | 376 window_wrapper); |
255 } | 377 } |
256 | 378 |
257 void LocalWindowProxy::UpdateDocumentProperty() { | 379 void LocalWindowProxy::UpdateDocumentProperty() { |
258 DCHECK(world_->IsMainWorld()); | 380 DCHECK(world_->IsMainWorld()); |
381 TRACE_EVENT1("v8", "LocalWindowProxy::updateDocumentProperty", "isMainWindow", | |
382 GetFrame()->IsMainFrame()); | |
259 | 383 |
260 ScriptState::Scope scope(script_state_.Get()); | 384 ScriptState::Scope scope(script_state_.Get()); |
261 v8::Local<v8::Context> context = script_state_->GetContext(); | 385 v8::Local<v8::Context> context = script_state_->GetContext(); |
262 v8::Local<v8::Value> document_wrapper = | 386 v8::Local<v8::Value> document_wrapper = |
263 ToV8(GetFrame()->GetDocument(), context->Global(), GetIsolate()); | 387 ToV8(GetFrame()->GetDocument(), context->Global(), GetIsolate()); |
264 DCHECK(document_wrapper->IsObject()); | 388 DCHECK(document_wrapper->IsObject()); |
389 | |
390 if (V8PerIsolateData::From(GetIsolate())->UseSnapshot()) { | |
391 v8::Local<v8::Object> wrapper = document_wrapper.As<v8::Object>(); | |
392 v8::Local<v8::Object> document_prototype = wrapper->GetPrototype() | |
393 .As<v8::Object>() | |
394 ->GetPrototype() | |
395 .As<v8::Object>(); | |
396 V8Document::installRuntimeEnabledFeatures(GetIsolate(), World(), wrapper, | |
397 document_prototype, | |
398 v8::Local<v8::Function>()); | |
399 V8Document::preparePrototypeAndInterfaceObject( | |
400 context, World(), document_prototype, v8::Local<v8::Function>(), | |
401 v8::Local<v8::FunctionTemplate>()); | |
402 } | |
403 | |
265 // Update the cached accessor for window.document. | 404 // Update the cached accessor for window.document. |
266 CHECK(V8PrivateProperty::GetWindowDocumentCachedAccessor(GetIsolate()) | 405 CHECK(V8PrivateProperty::GetWindowDocumentCachedAccessor(GetIsolate()) |
267 .Set(context->Global(), document_wrapper)); | 406 .Set(context->Global(), document_wrapper)); |
268 } | 407 } |
269 | 408 |
270 void LocalWindowProxy::UpdateActivityLogger() { | 409 void LocalWindowProxy::UpdateActivityLogger() { |
271 script_state_->PerContextData()->SetActivityLogger( | 410 script_state_->PerContextData()->SetActivityLogger( |
272 V8DOMActivityLogger::ActivityLogger( | 411 V8DOMActivityLogger::ActivityLogger( |
273 world_->GetWorldId(), GetFrame()->GetDocument() | 412 world_->GetWorldId(), GetFrame()->GetDocument() |
274 ? GetFrame()->GetDocument()->baseURI() | 413 ? GetFrame()->GetDocument()->baseURI() |
(...skipping 23 matching lines...) Expand all Loading... | |
298 context->UseDefaultSecurityToken(); | 437 context->UseDefaultSecurityToken(); |
299 return; | 438 return; |
300 } | 439 } |
301 | 440 |
302 if (world_->IsIsolatedWorld()) { | 441 if (world_->IsIsolatedWorld()) { |
303 SecurityOrigin* frame_security_origin = | 442 SecurityOrigin* frame_security_origin = |
304 GetFrame()->GetDocument()->GetSecurityOrigin(); | 443 GetFrame()->GetDocument()->GetSecurityOrigin(); |
305 String frame_security_token = frame_security_origin->ToString(); | 444 String frame_security_token = frame_security_origin->ToString(); |
306 // We need to check the return value of domainWasSetInDOM() on the | 445 // We need to check the return value of domainWasSetInDOM() on the |
307 // frame's SecurityOrigin because, if that's the case, only | 446 // frame's SecurityOrigin because, if that's the case, only |
308 // SecurityOrigin::m_domain would have been modified. | 447 // SecurityOrigin::domain_ would have been modified. |
309 // m_domain is not used by SecurityOrigin::toString(), so we would end | 448 // domain_ is not used by SecurityOrigin::toString(), so we would end |
310 // up generating the same token that was already set. | 449 // up generating the same token that was already set. |
311 if (frame_security_origin->DomainWasSetInDOM() || | 450 if (frame_security_origin->DomainWasSetInDOM() || |
312 frame_security_token.IsEmpty() || frame_security_token == "null") { | 451 frame_security_token.IsEmpty() || frame_security_token == "null") { |
313 context->UseDefaultSecurityToken(); | 452 context->UseDefaultSecurityToken(); |
314 return; | 453 return; |
315 } | 454 } |
316 token = frame_security_token + token; | 455 token = frame_security_token + token; |
317 } | 456 } |
318 | 457 |
319 // NOTE: V8 does identity comparison in fast path, must use a symbol | 458 // NOTE: V8 does identity comparison in fast path, must use a symbol |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 | 588 |
450 SetSecurityToken(origin); | 589 SetSecurityToken(origin); |
451 } | 590 } |
452 | 591 |
453 LocalWindowProxy::LocalWindowProxy(v8::Isolate* isolate, | 592 LocalWindowProxy::LocalWindowProxy(v8::Isolate* isolate, |
454 LocalFrame& frame, | 593 LocalFrame& frame, |
455 RefPtr<DOMWrapperWorld> world) | 594 RefPtr<DOMWrapperWorld> world) |
456 : WindowProxy(isolate, frame, std::move(world)) {} | 595 : WindowProxy(isolate, frame, std::move(world)) {} |
457 | 596 |
458 } // namespace blink | 597 } // namespace blink |
OLD | NEW |