From 4de8dca04b2d06c93e40bdb3a8dc6627990f5ebd Mon Sep 17 00:00:00 2001 From: amaitland Date: Wed, 18 Nov 2015 09:17:16 +1000 Subject: [PATCH] Resolves #1407 More the DeserializeV8Object call to within the context->Enter so a V8Object can be created Change callback to use a struct as it's return type to demo the solution to this bug Add missing list->SetDictionary call in V8Serialization.cpp --- .../CefAppUnmanagedWrapper.cpp | 16 ++++++++++------ .../Serialization/V8Serialization.cpp | 2 +- .../Internals/Serialization/V8Serialization.cpp | 1 + CefSharp.Example/BoundObject.cs | 4 +++- CefSharp.Example/CallbackResponseStruct.cs | 17 +++++++++++++++++ CefSharp.Example/CefSharp.Example.csproj | 1 + CefSharp.Example/Resources/BindingTest.html | 2 +- 7 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 CefSharp.Example/CallbackResponseStruct.cs diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index 59a021301..c6fd4bac3 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -251,12 +251,6 @@ 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++) - { - params.push_back(DeserializeV8Object(parameterList, static_cast(i))); - } auto callbackWrapper = callbackRegistry->FindWrapper(jsCallbackId); if (callbackWrapper == nullptr) @@ -272,6 +266,16 @@ namespace CefSharp { try { + auto parameterList = argList->GetList(3); + CefV8ValueList params; + + //Needs to be called within the context as for Dictionary (mapped to struct) + //a V8Object will be created + for (CefV8ValueList::size_type i = 0; i < parameterList->GetSize(); i++) + { + params.push_back(DeserializeV8Object(parameterList, static_cast(i))); + } + result = value->ExecuteFunction(nullptr, params); success = result.get() != nullptr; diff --git a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp index 5d7ea022e..da6733c60 100644 --- a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp +++ b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp @@ -160,7 +160,7 @@ namespace CefSharp auto size = subDict->GetSize(); std::vector keys; subDict->GetKeys(keys); - result = CefV8Value::CreateArray(size); + result = CefV8Value::CreateObject(nullptr); for (auto i = 0; i < size; i++) { result->SetValue(keys[i], DeserializeV8Object(subDict, keys[i]), V8_PROPERTY_ATTRIBUTE_NONE); diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp index 1ba239e05..6e5c76a3c 100644 --- a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp @@ -126,6 +126,7 @@ namespace CefSharp auto fieldVal = fields[i]->GetValue(obj); SerializeV8SimpleObject(fieldVal, subDict, strFieldName, seen); } + list->SetDictionary(index, subDict); } else { diff --git a/CefSharp.Example/BoundObject.cs b/CefSharp.Example/BoundObject.cs index ca9f878ac..a5e75e25a 100644 --- a/CefSharp.Example/BoundObject.cs +++ b/CefSharp.Example/BoundObject.cs @@ -56,7 +56,9 @@ namespace CefSharp.Example using (javascriptCallback) { - await javascriptCallback.ExecuteAsync("This callback from C# was delayed " + taskDelay + "ms"); + //NOTE: Classes are not supported, simple structs are + var response = new CallbackResponseStruct("This callback from C# was delayed " + taskDelay + "ms"); + await javascriptCallback.ExecuteAsync(response); } }); } diff --git a/CefSharp.Example/CallbackResponseStruct.cs b/CefSharp.Example/CallbackResponseStruct.cs new file mode 100644 index 000000000..0a549daf8 --- /dev/null +++ b/CefSharp.Example/CallbackResponseStruct.cs @@ -0,0 +1,17 @@ +using System; +// Copyright © 2010-2015 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +namespace CefSharp.Example +{ + public struct CallbackResponseStruct + { + public string Response; + + public CallbackResponseStruct(string response) + { + Response = response; + } + } +} diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index eb810d1fd..b08d9a62f 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -92,6 +92,7 @@ + diff --git a/CefSharp.Example/Resources/BindingTest.html b/CefSharp.Example/Resources/BindingTest.html index ae63fc4c7..0caedff02 100644 --- a/CefSharp.Example/Resources/BindingTest.html +++ b/CefSharp.Example/Resources/BindingTest.html @@ -96,7 +96,7 @@ function callback(s) { var result = document.getElementById('cbresult'); - result.innerText += "Callback: " + s + "" + Date(); + result.innerText += "Callback: " + s.Response + "" + Date(); } function testCallback()