Skip to content

Commit

Permalink
Allow calling managed methods from objects bound with RegisterAsyncJs…
Browse files Browse the repository at this point in the history
…Object that return arrays of structs (#1981)

* Fixed problem when calling CefV8Value::CreateArray. Needs prior call to V8 context enter
* Call callback dispose in case of failure
* Support serialization of struct arrays to JS side
* Fixed formatting
  • Loading branch information
João Neves authored and amaitland committed Mar 13, 2017
1 parent 26574eb commit bc83d6c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
43 changes: 35 additions & 8 deletions CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ namespace CefSharp
{
auto frameId = GetInt64(argList, 0);
auto callbackId = GetInt64(argList, 1);

JavascriptRootObjectWrapper^ rootObjectWrapper;
browserWrapper->JavascriptRootObjectWrappers->TryGetValue(frameId, rootObjectWrapper);

Expand All @@ -417,17 +417,44 @@ namespace CefSharp
JavascriptAsyncMethodCallback^ callback;
if (rootObjectWrapper->TryGetAndRemoveMethodCallback(callbackId, callback))
{
auto success = argList->GetBool(2);
if (success)

try
{
callback->Success(DeserializeV8Object(argList, 3));
auto frame = browser->GetFrame(frameId);
if (frame.get())
{
auto context = frame->GetV8Context();

if (context.get() && context->Enter())
{
try
{
auto success = argList->GetBool(2);
if (success)
{
callback->Success(DeserializeV8Object(argList, 3));
}
else
{
callback->Fail(argList->GetString(3));
}
}
finally
{
context->Exit();
}
}
else
{
callback->Fail("Unable to Enter Context");
}
}
}
else
finally
{
callback->Fail(argList->GetString(3));
//dispose
delete callback;
}
//dispose
delete callback;
}
}
handled = true;
Expand Down
14 changes: 14 additions & 0 deletions CefSharp.Example/AsyncBoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace CefSharp.Example
{
public struct JsObject
{
public string Value;
}

public class AsyncBoundObject
{
//We expect an exception here, so tell VS to ignore
Expand All @@ -33,5 +38,14 @@ public void DoSomething()
{
Thread.Sleep(1000);
}

public JsObject[] ObjectArray(string name)
{
return new[]
{
new JsObject() { Value = "Item1" },
new JsObject() { Value = "Item2" }
};
}
}
}
12 changes: 12 additions & 0 deletions CefSharp.Example/Resources/BindingTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,23 @@
writeAsyncResult(call, end);
});
}

function asyncObjectArray()
{
var call = "Async call (ObjectArray): " + Date();
boundAsync.objectArray('CefSharp').then(function (res)
{
var end = "Result: [ " + res.map(function (item) { return item.Value }) + " ] (" + Date() + ")";
writeAsyncResult(call, end);
});
}

asyncError();
asyncDivOk();
asyncDivFail();
asyncDoSomething();
asyncHello();
asyncObjectArray();
</script>
</p>
<p>
Expand Down
4 changes: 2 additions & 2 deletions CefSharp/Internals/JavascriptObjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
}
catch (Exception e)
{
throw new InvalidOperationException("Could not execute method: " + name + "(" + String.Join(", ", parameters) + ")" + " - Missing Parameters: " + missingParams, e);
throw new InvalidOperationException("Could not execute method: " + name + "(" + String.Join(", ", parameters) + ") " + (missingParams > 0 ? "- Missing Parameters: " + missingParams : ""), e);
}

if(result != null && IsComplexType(result.GetType()))
Expand Down Expand Up @@ -397,7 +397,7 @@ private static bool IsComplexType(Type type)
baseType = Nullable.GetUnderlyingType(type);
}

if (baseType == null || baseType.Namespace.StartsWith("System"))
if (baseType == null || baseType.IsArray || baseType.Namespace.StartsWith("System"))
{
return false;
}
Expand Down

0 comments on commit bc83d6c

Please sign in to comment.