Skip to content

Commit 913310f

Browse files
committed
Fixed a crash in V8 bindings when the call to Frame.LoadUrl caused redirect to a different
origin. For example a redirect from "file://" to "http://" or from "data:text/html" to "file://" (Issue 130).
1 parent cea737c commit 913310f

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

cefpython/cef3/subprocess/cefpython_app.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,25 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
479479
"invalid data [1]");
480480
return;
481481
}
482+
482483
// A context must be explicitly entered before creating a
483484
// V8 Object, Array, Function or Date asynchronously.
484-
bool enteredContext = false;
485-
if (!context->IsSame(CefV8Context::GetCurrentContext())) {
486-
enteredContext = true;
485+
// NOTE: you cannot call CefV8Context::GetEnteredContext
486+
// or GetCurrentContext when CefV8Context::InContext
487+
// returns false, as it will result in crashes.
488+
bool didEnterContext = false;
489+
if (!CefV8Context::InContext()) {
490+
if (!context->IsValid()) {
491+
// BUG in CEF (Issue 130), the "context" provided by CEF may
492+
// not be valid. May be a timing issue. Or may be caused by
493+
// a redirect to a different origin and that creates a new
494+
// renderer process.
495+
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED:"\
496+
" V8 context provided by CEF is invalid");
497+
return;
498+
}
487499
context->Enter();
500+
didEnterContext = true;
488501
}
489502
CefRefPtr<CefDictionaryValue> functions = \
490503
jsBindings->GetDictionary("functions");
@@ -498,7 +511,7 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
498511
&& objects->IsValid())) {
499512
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \
500513
"invalid data [2]");
501-
if (enteredContext)
514+
if (didEnterContext)
502515
context->Exit();
503516
return;
504517
}
@@ -510,7 +523,7 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
510523
if (!functions->GetKeys(functionsVector)) {
511524
DebugLog("Renderer: DoJavascriptBindingsForFrame(): " \
512525
"functions->GetKeys() FAILED");
513-
if (enteredContext)
526+
if (didEnterContext)
514527
context->Exit();
515528
return;
516529
}
@@ -529,7 +542,7 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
529542
if (!v8Properties->GetKeys(v8Keys)) {
530543
DebugLog("DoJavascriptBindingsForFrame() FAILED: " \
531544
"v8Properties->GetKeys() failed");
532-
if (enteredContext)
545+
if (didEnterContext)
533546
context->Exit();
534547
return;
535548
}
@@ -544,7 +557,7 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
544557
if (!objects->GetKeys(objectsVector)) {
545558
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \
546559
"objects->GetKeys() failed");
547-
if (enteredContext)
560+
if (didEnterContext)
548561
context->Exit();
549562
return;
550563
}
@@ -557,7 +570,7 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
557570
if (!(objects->GetType(objectName) == VTYPE_DICTIONARY)) {
558571
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \
559572
"objects->GetType() != VTYPE_DICTIONARY");
560-
if (enteredContext)
573+
if (didEnterContext)
561574
context->Exit();
562575
return;
563576
}
@@ -567,7 +580,7 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
567580
if (!(methods->IsValid() && methods->GetKeys(methodsVector))) {
568581
DebugLog("Renderer: DoJavascriptBindingsForFrame() FAILED: " \
569582
"methods->GetKeys() failed");
570-
if (enteredContext)
583+
if (didEnterContext)
571584
context->Exit();
572585
return;
573586
}
@@ -584,6 +597,6 @@ void CefPythonApp::DoJavascriptBindingsForFrame(CefRefPtr<CefBrowser> browser,
584597
}
585598
}
586599
// END.
587-
if (enteredContext)
600+
if (didEnterContext)
588601
context->Exit();
589602
}

0 commit comments

Comments
 (0)