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

Side by Side Diff: Source/bindings/v8/ScriptController.cpp

Issue 306853002: Replace Context::Scope with ScriptState::Scope (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 { 245 {
246 ScriptableDocumentParser* parser = m_frame->document()->scriptableDocumentPa rser(); 246 ScriptableDocumentParser* parser = m_frame->document()->scriptableDocumentPa rser();
247 if (parser) 247 if (parser)
248 return parser->textPosition(); 248 return parser->textPosition();
249 return TextPosition::minimumPosition(); 249 return TextPosition::minimumPosition();
250 } 250 }
251 251
252 // Create a V8 object with an interceptor of NPObjectPropertyGetter. 252 // Create a V8 object with an interceptor of NPObjectPropertyGetter.
253 void ScriptController::bindToWindowObject(LocalFrame* frame, const String& key, NPObject* object) 253 void ScriptController::bindToWindowObject(LocalFrame* frame, const String& key, NPObject* object)
254 { 254 {
255 v8::HandleScope handleScope(m_isolate); 255 ScriptState* scriptState = ScriptState::forMainWorld(frame);
256 256 if (scriptState->contextIsEmpty())
257 v8::Handle<v8::Context> v8Context = toV8Context(m_isolate, frame, DOMWrapper World::mainWorld());
258 if (v8Context.IsEmpty())
259 return; 257 return;
260 258
261 v8::Context::Scope scope(v8Context); 259 ScriptState::Scope scope(scriptState);
262
263 v8::Handle<v8::Object> value = createV8ObjectForNPObject(object, 0, m_isolat e); 260 v8::Handle<v8::Object> value = createV8ObjectForNPObject(object, 0, m_isolat e);
264 261
265 // Attach to the global object. 262 // Attach to the global object.
266 v8::Handle<v8::Object> global = v8Context->Global(); 263 scriptState->context()->Global()->Set(v8String(m_isolate, key), value);
267 global->Set(v8String(m_isolate, key), value);
268 } 264 }
269 265
270 void ScriptController::enableEval() 266 void ScriptController::enableEval()
271 { 267 {
272 if (!m_windowShell->isContextInitialized()) 268 if (!m_windowShell->isContextInitialized())
273 return; 269 return;
274 v8::HandleScope handleScope(m_isolate); 270 v8::HandleScope handleScope(m_isolate);
275 m_windowShell->context()->AllowCodeGenerationFromStrings(true); 271 m_windowShell->context()->AllowCodeGenerationFromStrings(true);
276 } 272 }
277 273
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 } 352 }
357 353
358 static NPObject* createNoScriptObject() 354 static NPObject* createNoScriptObject()
359 { 355 {
360 notImplemented(); 356 notImplemented();
361 return 0; 357 return 0;
362 } 358 }
363 359
364 static NPObject* createScriptObject(LocalFrame* frame, v8::Isolate* isolate) 360 static NPObject* createScriptObject(LocalFrame* frame, v8::Isolate* isolate)
365 { 361 {
366 v8::HandleScope handleScope(isolate); 362 ScriptState* scriptState = ScriptState::forMainWorld(frame);
367 v8::Handle<v8::Context> v8Context = toV8Context(isolate, frame, DOMWrapperWo rld::mainWorld()); 363 if (scriptState->contextIsEmpty())
368 if (v8Context.IsEmpty())
369 return createNoScriptObject(); 364 return createNoScriptObject();
370 365
371 v8::Context::Scope scope(v8Context); 366 ScriptState::Scope scope(scriptState);
372 DOMWindow* window = frame->domWindow(); 367 DOMWindow* window = frame->domWindow();
373 v8::Handle<v8::Value> global = toV8(window, v8Context->Global(), v8Context-> GetIsolate()); 368 v8::Handle<v8::Value> global = toV8(window, scriptState->context()->Global() , scriptState->isolate());
374 ASSERT(global->IsObject()); 369 ASSERT(global->IsObject());
375
376 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(global), windo w, isolate); 370 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(global), windo w, isolate);
377 } 371 }
378 372
379 NPObject* ScriptController::windowScriptNPObject() 373 NPObject* ScriptController::windowScriptNPObject()
380 { 374 {
381 if (m_windowScriptNPObject) 375 if (m_windowScriptNPObject)
382 return m_windowScriptNPObject; 376 return m_windowScriptNPObject;
383 377
384 if (canExecuteScripts(NotAboutToExecuteScript)) { 378 if (canExecuteScripts(NotAboutToExecuteScript)) {
385 // JavaScript is enabled, so there is a JavaScript window object. 379 // JavaScript is enabled, so there is a JavaScript window object.
386 // Return an NPObject bound to the window object. 380 // Return an NPObject bound to the window object.
387 m_windowScriptNPObject = createScriptObject(m_frame, m_isolate); 381 m_windowScriptNPObject = createScriptObject(m_frame, m_isolate);
388 _NPN_RegisterObject(m_windowScriptNPObject, 0); 382 _NPN_RegisterObject(m_windowScriptNPObject, 0);
389 } else { 383 } else {
390 // JavaScript is not enabled, so we cannot bind the NPObject to the 384 // JavaScript is not enabled, so we cannot bind the NPObject to the
391 // JavaScript window object. Instead, we create an NPObject of a 385 // JavaScript window object. Instead, we create an NPObject of a
392 // different class, one which is not bound to a JavaScript object. 386 // different class, one which is not bound to a JavaScript object.
393 m_windowScriptNPObject = createNoScriptObject(); 387 m_windowScriptNPObject = createNoScriptObject();
394 } 388 }
395 return m_windowScriptNPObject; 389 return m_windowScriptNPObject;
396 } 390 }
397 391
398 NPObject* ScriptController::createScriptObjectForPluginElement(HTMLPlugInElement * plugin) 392 NPObject* ScriptController::createScriptObjectForPluginElement(HTMLPlugInElement * plugin)
399 { 393 {
400 // Can't create NPObjects when JavaScript is disabled. 394 // Can't create NPObjects when JavaScript is disabled.
401 if (!canExecuteScripts(NotAboutToExecuteScript)) 395 if (!canExecuteScripts(NotAboutToExecuteScript))
402 return createNoScriptObject(); 396 return createNoScriptObject();
403 397
404 v8::HandleScope handleScope(m_isolate); 398 ScriptState* scriptState = ScriptState::forMainWorld(m_frame);
405 v8::Handle<v8::Context> v8Context = toV8Context(m_isolate, m_frame, DOMWrapp erWorld::mainWorld()); 399 if (scriptState->contextIsEmpty())
406 if (v8Context.IsEmpty())
407 return createNoScriptObject(); 400 return createNoScriptObject();
408 v8::Context::Scope scope(v8Context);
409 401
402 ScriptState::Scope scope(scriptState);
410 DOMWindow* window = m_frame->domWindow(); 403 DOMWindow* window = m_frame->domWindow();
411 v8::Handle<v8::Value> v8plugin = toV8(plugin, v8Context->Global(), v8Context ->GetIsolate()); 404 v8::Handle<v8::Value> v8plugin = toV8(plugin, scriptState->context()->Global (), scriptState->isolate());
412 if (!v8plugin->IsObject()) 405 if (!v8plugin->IsObject())
413 return createNoScriptObject(); 406 return createNoScriptObject();
414 407
415 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(v8plugin), win dow, v8Context->GetIsolate()); 408 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(v8plugin), win dow, scriptState->isolate());
416 } 409 }
417 410
418 void ScriptController::clearWindowShell() 411 void ScriptController::clearWindowShell()
419 { 412 {
420 double start = currentTime(); 413 double start = currentTime();
421 // V8 binding expects ScriptController::clearWindowShell only be called 414 // V8 binding expects ScriptController::clearWindowShell only be called
422 // when a frame is loading a new page. This creates a new context for the ne w page. 415 // when a frame is loading a new page. This creates a new context for the ne w page.
423 m_windowShell->clearForNavigation(); 416 m_windowShell->clearForNavigation();
424 for (IsolatedWorldMap::iterator iter = m_isolatedWorlds.begin(); iter != m_i solatedWorlds.end(); ++iter) 417 for (IsolatedWorldMap::iterator iter = m_isolatedWorlds.begin(); iter != m_i solatedWorlds.end(); ++iter)
425 iter->value->clearForNavigation(); 418 iter->value->clearForNavigation();
426 clearScriptObjects(); 419 clearScriptObjects();
427 blink::Platform::current()->histogramCustomCounts("WebCore.ScriptController. clearWindowShell", (currentTime() - start) * 1000, 0, 10000, 50); 420 blink::Platform::current()->histogramCustomCounts("WebCore.ScriptController. clearWindowShell", (currentTime() - start) * 1000, 0, 10000, 50);
428 } 421 }
429 422
430 void ScriptController::setCaptureCallStackForUncaughtExceptions(bool value) 423 void ScriptController::setCaptureCallStackForUncaughtExceptions(bool value)
431 { 424 {
432 v8::V8::SetCaptureStackTraceForUncaughtExceptions(value, ScriptCallStack::ma xCallStackSizeToCapture, stackTraceOptions); 425 v8::V8::SetCaptureStackTraceForUncaughtExceptions(value, ScriptCallStack::ma xCallStackSizeToCapture, stackTraceOptions);
433 } 426 }
434 427
435 void ScriptController::collectIsolatedContexts(Vector<std::pair<ScriptState*, Se curityOrigin*> >& result) 428 void ScriptController::collectIsolatedContexts(Vector<std::pair<ScriptState*, Se curityOrigin*> >& result)
436 { 429 {
437 v8::HandleScope handleScope(m_isolate); 430 v8::HandleScope handleScope(m_isolate);
adamk 2014/05/30 19:59:22 Is this HandleScope still needed?
haraken 2014/06/02 01:14:50 Removed.
438 for (IsolatedWorldMap::iterator it = m_isolatedWorlds.begin(); it != m_isola tedWorlds.end(); ++it) { 431 for (IsolatedWorldMap::iterator it = m_isolatedWorlds.begin(); it != m_isola tedWorlds.end(); ++it) {
439 V8WindowShell* isolatedWorldShell = it->value.get(); 432 V8WindowShell* isolatedWorldShell = it->value.get();
440 SecurityOrigin* origin = isolatedWorldShell->world().isolatedWorldSecuri tyOrigin(); 433 SecurityOrigin* origin = isolatedWorldShell->world().isolatedWorldSecuri tyOrigin();
441 if (!origin) 434 if (!origin)
442 continue; 435 continue;
443 v8::Local<v8::Context> v8Context = isolatedWorldShell->context(); 436 if (!isolatedWorldShell->isContextInitialized())
444 if (v8Context.IsEmpty())
445 continue; 437 continue;
446 ScriptState* scriptState = ScriptState::from(v8Context); 438 result.append(std::pair<ScriptState*, SecurityOrigin*>(isolatedWorldShel l->scriptState(), origin));
447 result.append(std::pair<ScriptState*, SecurityOrigin*>(scriptState, orig in));
448 } 439 }
449 } 440 }
450 441
451 bool ScriptController::setContextDebugId(int debugId) 442 bool ScriptController::setContextDebugId(int debugId)
452 { 443 {
453 ASSERT(debugId > 0); 444 ASSERT(debugId > 0);
454 if (!m_windowShell->isContextInitialized()) 445 if (!m_windowShell->isContextInitialized())
455 return false; 446 return false;
456 v8::HandleScope scope(m_isolate); 447 v8::HandleScope scope(m_isolate);
457 v8::Local<v8::Context> context = m_windowShell->context(); 448 v8::Local<v8::Context> context = m_windowShell->context();
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 557
567 ScriptValue ScriptController::evaluateScriptInMainWorld(const ScriptSourceCode& sourceCode, AccessControlStatus corsStatus, ExecuteScriptPolicy policy) 558 ScriptValue ScriptController::evaluateScriptInMainWorld(const ScriptSourceCode& sourceCode, AccessControlStatus corsStatus, ExecuteScriptPolicy policy)
568 { 559 {
569 if (policy == DoNotExecuteScriptWhenScriptsDisabled && !canExecuteScripts(Ab outToExecuteScript)) 560 if (policy == DoNotExecuteScriptWhenScriptsDisabled && !canExecuteScripts(Ab outToExecuteScript))
570 return ScriptValue(); 561 return ScriptValue();
571 562
572 String sourceURL = sourceCode.url(); 563 String sourceURL = sourceCode.url();
573 const String* savedSourceURL = m_sourceURL; 564 const String* savedSourceURL = m_sourceURL;
574 m_sourceURL = &sourceURL; 565 m_sourceURL = &sourceURL;
575 566
576 v8::HandleScope handleScope(m_isolate); 567 ScriptState* scriptState = ScriptState::forMainWorld(m_frame);
577 v8::Handle<v8::Context> v8Context = toV8Context(m_isolate, m_frame, DOMWrapp erWorld::mainWorld()); 568 if (scriptState->contextIsEmpty())
578 if (v8Context.IsEmpty())
579 return ScriptValue(); 569 return ScriptValue();
580 v8::Context::Scope scope(v8Context); 570
571 ScriptState::Scope scope(scriptState);
581 572
582 RefPtr<LocalFrame> protect(m_frame); 573 RefPtr<LocalFrame> protect(m_frame);
583 if (m_frame->loader().stateMachine()->isDisplayingInitialEmptyDocument()) 574 if (m_frame->loader().stateMachine()->isDisplayingInitialEmptyDocument())
584 m_frame->loader().didAccessInitialDocument(); 575 m_frame->loader().didAccessInitialDocument();
585 576
586 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio n::preprocess(m_frame, sourceCode); 577 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio n::preprocess(m_frame, sourceCode);
587 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma ybeProcessedSourceCode : sourceCode; 578 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma ybeProcessedSourceCode : sourceCode;
588 579
589 v8::Local<v8::Value> object = executeScriptAndReturnValue(v8Context, sourceC odeToCompile, corsStatus); 580 v8::Local<v8::Value> object = executeScriptAndReturnValue(scriptState->conte xt(), sourceCodeToCompile, corsStatus);
590 m_sourceURL = savedSourceURL; 581 m_sourceURL = savedSourceURL;
591 582
592 if (object.IsEmpty()) 583 if (object.IsEmpty())
593 return ScriptValue(); 584 return ScriptValue();
594 585
595 return ScriptValue(ScriptState::from(v8Context), object); 586 return ScriptValue(scriptState, object);
596 } 587 }
597 588
598 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results) 589 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results)
599 { 590 {
600 ASSERT(worldID > 0); 591 ASSERT(worldID > 0);
601 592
602 v8::HandleScope handleScope(m_isolate); 593 v8::HandleScope handleScope(m_isolate);
adamk 2014/05/30 19:59:22 Is this HandleScope still needed?
haraken 2014/06/02 01:14:50 Removed.
603 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(worldID , extensionGroup); 594 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(worldID , extensionGroup);
604 V8WindowShell* isolatedWorldShell = windowShell(*world); 595 V8WindowShell* isolatedWorldShell = windowShell(*world);
605 if (!isolatedWorldShell->isContextInitialized()) 596 if (!isolatedWorldShell->isContextInitialized())
606 return; 597 return;
607 598
608 v8::Local<v8::Context> context = isolatedWorldShell->context(); 599 ScriptState* scriptState = isolatedWorldShell->scriptState();
609 v8::Context::Scope contextScope(context); 600 ScriptState::Scope scope(scriptState);
610 v8::Local<v8::Array> resultArray = v8::Array::New(m_isolate, sources.size()) ; 601 v8::Local<v8::Array> resultArray = v8::Array::New(m_isolate, sources.size()) ;
611 602
612 for (size_t i = 0; i < sources.size(); ++i) { 603 for (size_t i = 0; i < sources.size(); ++i) {
613 v8::Local<v8::Value> evaluationResult = executeScriptAndReturnValue(cont ext, sources[i]); 604 v8::Local<v8::Value> evaluationResult = executeScriptAndReturnValue(scri ptState->context(), sources[i]);
614 if (evaluationResult.IsEmpty()) 605 if (evaluationResult.IsEmpty())
615 evaluationResult = v8::Local<v8::Value>::New(m_isolate, v8::Undefine d(m_isolate)); 606 evaluationResult = v8::Local<v8::Value>::New(m_isolate, v8::Undefine d(m_isolate));
616 resultArray->Set(i, evaluationResult); 607 resultArray->Set(i, evaluationResult);
617 } 608 }
618 609
619 if (results) { 610 if (results) {
620 for (size_t i = 0; i < resultArray->Length(); ++i) 611 for (size_t i = 0; i < resultArray->Length(); ++i)
621 results->append(ScriptValue(ScriptState::from(context), resultArray- >Get(i))); 612 results->append(ScriptValue(scriptState, resultArray->Get(i)));
622 } 613 }
623 } 614 }
624 615
625 } // namespace WebCore 616 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698