Proposed solution to #1354

The problem seems to be that OnContextCreated is never called as the page has no javascript code on it.
Keep the same callbackRegistry logic for kJavascriptCallbackRequest as without a context there cannot be a callback
For kEvaluateJavascriptRequest we now manually create a callback registry for the frame if required. Once the script is executed OnContextCreated is called, the wrapper creation is ignored and the context should be released as per normal in OnContextReleased
This commit is contained in:
amaitland
2015-10-26 11:41:19 +10:00
parent fdceb5c202
commit cc6eaa65d6
@@ -176,15 +176,24 @@ namespace CefSharp
auto frameId = GetInt64(argList, 0);
int64 callbackId = GetInt64(argList, 1);
JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);
auto callbackRegistry = rootObjectWrapper == nullptr ? nullptr : rootObjectWrapper->CallbackRegistry;
if (callbackRegistry == nullptr)
{
errorMessage = StringUtils::ToNative("Frame " + frameId + " is no longer available, most likely the Frame has been Disposed.");
}
else if (name == kEvaluateJavascriptRequest)
if (name == kEvaluateJavascriptRequest)
{
JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);
//NOTE: In the rare case when when OnContextCreated hasn't been called we need to manually create the rootObjectWrapper
//It appears that OnContextCreated is only called for pages that have javascript on them, which makes sense
//as without javascript there is no need for a context.
if (rootObjectWrapper == nullptr)
{
rootObjectWrapper = gcnew JavascriptRootObjectWrapper(browser->GetIdentifier(), browserWrapper->BrowserProcess);
browserWrapper->JavascriptRootObjectWrappers->TryAdd(frameId, rootObjectWrapper);
}
auto callbackRegistry = rootObjectWrapper->CallbackRegistry;
auto script = argList->GetString(2);
auto frame = browser->GetFrame(frameId);
@@ -227,51 +236,61 @@ namespace CefSharp
}
else
{
auto jsCallbackId = GetInt64(argList, 2);
auto parameterList = argList->GetList(3);
CefV8ValueList params;
for (CefV8ValueList::size_type i = 0; i < parameterList->GetSize(); i++)
JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);
auto callbackRegistry = rootObjectWrapper == nullptr ? nullptr : rootObjectWrapper->CallbackRegistry;
if (callbackRegistry == nullptr)
{
params.push_back(DeserializeV8Object(parameterList, static_cast<int>(i)));
}
auto callbackWrapper = callbackRegistry->FindWrapper(jsCallbackId);
if (callbackWrapper == nullptr)
{
errorMessage = "Unable to find callbackWrapper";
errorMessage = StringUtils::ToNative("The callback registry for Frame " + frameId + " is no longer available, most likely the Frame has been Disposed.");
}
else
{
auto context = callbackWrapper->GetContext();
auto value = callbackWrapper->GetValue();
if (context.get() && context->Enter())
auto jsCallbackId = GetInt64(argList, 2);
auto parameterList = argList->GetList(3);
CefV8ValueList params;
for (CefV8ValueList::size_type i = 0; i < parameterList->GetSize(); i++)
{
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();
errorMessage = StringUtils::CreateExceptionString(exception);
}
}
finally
{
context->Exit();
}
params.push_back(DeserializeV8Object(parameterList, static_cast<int>(i)));
}
auto callbackWrapper = callbackRegistry->FindWrapper(jsCallbackId);
if (callbackWrapper == nullptr)
{
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();
errorMessage = StringUtils::CreateExceptionString(exception);
}
}
finally
{
context->Exit();
}
}
else
{
errorMessage = "Unable to Enter Context";
}
}
}
}