mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
This should fix #1336 (IJavascriptCallback.ExecuteAsync never returns when callback no longer exists) and likely fixes as-of-yet undiscovered related callback problems.
The code previously in CefAppUnmanagedWrapper.cpp's OnProcessMessageReceived method did not check for a nullptr callbackWrapper resulting in an exception. Also note that previously the success and reponse variables were not initialized by default which I believe to have been a mistake after following all of the if..else code paths (most importantly some error conditions would never be returned back). I added a new test to BindingTest.html entitled "Test Disposed Callback" which makes the same 1.5 second delayed callback that "Test Callback" does, but then immediately navigate the page to www.google.com. Without the fix in CefAppUnmanagedWrapper.cpp the browser's renderer process will die in about 10 seconds.
This commit is contained in:
@@ -158,10 +158,10 @@ namespace CefSharp
|
||||
//these messages are roughly handled the same way
|
||||
if (name == kEvaluateJavascriptRequest || name == kJavascriptCallbackRequest)
|
||||
{
|
||||
bool success;
|
||||
bool success = false;
|
||||
CefRefPtr<CefV8Value> result;
|
||||
CefString errorMessage;
|
||||
CefRefPtr<CefProcessMessage> response;
|
||||
CefRefPtr<CefProcessMessage> response = CefProcessMessage::Create(kEvaluateJavascriptResponse);
|
||||
//both messages have the frameId stored at 0 and callbackId stored at index 1
|
||||
auto frameId = GetInt64(argList, 0);
|
||||
int64 callbackId = GetInt64(argList, 1);
|
||||
@@ -171,15 +171,12 @@ namespace CefSharp
|
||||
auto callbackRegistry = rootObjectWrapper == nullptr ? nullptr : rootObjectWrapper->CallbackRegistry;
|
||||
if (callbackRegistry == nullptr)
|
||||
{
|
||||
success = false;
|
||||
errorMessage = StringUtils::ToNative("Frame " + frameId + " is no longer available, most likely the Frame has been Disposed.");
|
||||
}
|
||||
else if (name == kEvaluateJavascriptRequest)
|
||||
{
|
||||
auto script = argList->GetString(2);
|
||||
|
||||
response = CefProcessMessage::Create(kEvaluateJavascriptResponse);
|
||||
|
||||
auto frame = browser->GetFrame(frameId);
|
||||
if (frame.get())
|
||||
{
|
||||
@@ -228,47 +225,50 @@ namespace CefSharp
|
||||
params.push_back(DeserializeV8Object(parameterList, static_cast<int>(i)));
|
||||
}
|
||||
|
||||
response = CefProcessMessage::Create(kJavascriptCallbackResponse);
|
||||
|
||||
auto callbackWrapper = callbackRegistry->FindWrapper(jsCallbackId);
|
||||
auto context = callbackWrapper->GetContext();
|
||||
auto value = callbackWrapper->GetValue();
|
||||
|
||||
if (context.get() && context->Enter())
|
||||
if (callbackWrapper == nullptr)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = value->ExecuteFunction(nullptr, params);
|
||||
success = result.get() != nullptr;
|
||||
|
||||
//we need to do this here to be able to store the v8context
|
||||
if (success)
|
||||
{
|
||||
auto responseArgList = response->GetArgumentList();
|
||||
SerializeV8Object(result, responseArgList, 2, callbackRegistry);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto exception = value->GetException();
|
||||
if (exception.get())
|
||||
{
|
||||
errorMessage = exception->GetMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
context->Exit();
|
||||
}
|
||||
errorMessage = "Unable to find callbackWrapper";
|
||||
}
|
||||
else
|
||||
{
|
||||
errorMessage = "Unable to Enter Context";
|
||||
}
|
||||
}
|
||||
auto context = callbackWrapper->GetContext();
|
||||
auto value = callbackWrapper->GetValue();
|
||||
|
||||
if (context.get() && context->Enter())
|
||||
{
|
||||
try
|
||||
{
|
||||
result = value->ExecuteFunction(nullptr, params);
|
||||
success = result.get() != nullptr;
|
||||
|
||||
//we need to do this here to be able to store the v8context
|
||||
if (success)
|
||||
{
|
||||
auto responseArgList = response->GetArgumentList();
|
||||
SerializeV8Object(result, responseArgList, 2, callbackRegistry);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto exception = value->GetException();
|
||||
if (exception.get())
|
||||
{
|
||||
errorMessage = exception->GetMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
context->Exit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
errorMessage = "Unable to Enter Context";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (response.get())
|
||||
{
|
||||
auto responseArgList = response->GetArgumentList();
|
||||
responseArgList->SetBool(0, success);
|
||||
SetInt64(callbackId, responseArgList, 1);
|
||||
@@ -277,7 +277,6 @@ namespace CefSharp
|
||||
responseArgList->SetString(2, errorMessage);
|
||||
}
|
||||
browser->SendProcessMessage(sourceProcessId, response);
|
||||
}
|
||||
|
||||
handled = true;
|
||||
}
|
||||
|
||||
@@ -112,6 +112,32 @@
|
||||
<span id="cbresult"></span>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Disposed Javscript Callback Test (navigates to www.google.com before callback fires)
|
||||
<br />
|
||||
<script type="text/javascript">
|
||||
function disposedCallback(s) {
|
||||
// This callback should be disposed and should not be called
|
||||
window.alert("This callback should not have been called");
|
||||
|
||||
var result = document.getElementById('disposedcbresult');
|
||||
result.innerText += "Callback: " + s + "" + Date();
|
||||
}
|
||||
|
||||
function testDisposedCallback() {
|
||||
bound.testCallback(callback);
|
||||
|
||||
var result = document.getElementById('disposedcbresult');
|
||||
result.innerText = "The function has returned: " + Date() + "\n";
|
||||
|
||||
window.location.assign("http://www.google.com");
|
||||
}
|
||||
</script>
|
||||
<button onclick="testDisposedCallback()">Test Disposed Callback</button>
|
||||
<br />
|
||||
<span id="disposedcbresult"></span>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Result of calling bound.repeat("hi ", 5) =
|
||||
<script type="text/javascript">
|
||||
|
||||
Reference in New Issue
Block a user