From a64f25193fa9f14445db84ca38028a49910e2b2e Mon Sep 17 00:00:00 2001
From: Greg Martin
Date: Sun, 18 Oct 2015 14:47:54 -0700
Subject: [PATCH] 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.
---
.../CefAppUnmanagedWrapper.cpp | 81 +++++++++----------
CefSharp.Example/Resources/BindingTest.html | 26 ++++++
2 files changed, 66 insertions(+), 41 deletions(-)
diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
index 1a671ca78..fe71c3577 100644
--- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
+++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
@@ -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 result;
CefString errorMessage;
- CefRefPtr response;
+ CefRefPtr 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(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;
}
diff --git a/CefSharp.Example/Resources/BindingTest.html b/CefSharp.Example/Resources/BindingTest.html
index 5e7d2342c..cd1efc035 100644
--- a/CefSharp.Example/Resources/BindingTest.html
+++ b/CefSharp.Example/Resources/BindingTest.html
@@ -112,6 +112,32 @@
+
+ Disposed Javscript Callback Test (navigates to www.google.com before callback fires)
+
+
+
+
+
+