Skip to content

Commit fe98124

Browse files
authored
Async JavascriptBinding - Add support for returning Task (cefsharp#2771)
* Async JavascriptBinding - Add support for returning Task Remove Parallel Task execution code from MethodRunnerQueue Add ParallelMethodRunnerQueue - For the new code path the Func is executed, if the Result is a generic Task we wait for it's completion - If the task is not generic then we return immediately This is only enabled when CefSharpSettings.ConcurrentTaskExecution = true; * MethodRunnerQueue - Display error message when method returns Task * ParallelMethodRunnerQueue - Rename to ConcurrentMethodRunnerQueue * MethodRunnerQueue - Improve formatting of error message when method returns Task * Async JavascriptBinding Return Task - Add Basic xUnit Test Very quick test, needs to be refined a little. The basics are working as expected. * Add Binding Test Async Task Example to the WPF Example menu * Async JSB Task - Add additional tests and improve error messaging
1 parent a1eec28 commit fe98124

19 files changed

Lines changed: 588 additions & 71 deletions

‎CefSharp.Core/ManagedCefBrowserAdapter.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ JavascriptObjectRepository^ ManagedCefBrowserAdapter::JavascriptObjectRepository
140140
return _javaScriptObjectRepository;
141141
}
142142

143-
MethodRunnerQueue^ ManagedCefBrowserAdapter::MethodRunnerQueue::get()
143+
IMethodRunnerQueue^ ManagedCefBrowserAdapter::MethodRunnerQueue::get()
144144
{
145145
return _methodRunnerQueue;
146146
}

‎CefSharp.Core/ManagedCefBrowserAdapter.h‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace CefSharp
3333
IWebBrowserInternal^ _webBrowserInternal;
3434
JavascriptObjectRepository^ _javaScriptObjectRepository;
3535
JavascriptCallbackFactory^ _javascriptCallbackFactory;
36-
MethodRunnerQueue^ _methodRunnerQueue;
36+
IMethodRunnerQueue^ _methodRunnerQueue;
3737
IBrowser^ _browserWrapper;
3838
bool _isDisposed;
3939

@@ -61,7 +61,16 @@ namespace CefSharp
6161
_webBrowserInternal = webBrowserInternal;
6262
_javaScriptObjectRepository = gcnew CefSharp::Internals::JavascriptObjectRepository();
6363
_javascriptCallbackFactory = gcnew CefSharp::Internals::JavascriptCallbackFactory(_clientAdapter->GetPendingTaskRepository());
64-
_methodRunnerQueue = gcnew CefSharp::Internals::MethodRunnerQueue(_javaScriptObjectRepository);
64+
65+
if (CefSharpSettings::ConcurrentTaskExecution)
66+
{
67+
_methodRunnerQueue = gcnew CefSharp::Internals::ConcurrentMethodRunnerQueue(_javaScriptObjectRepository);
68+
}
69+
else
70+
{
71+
_methodRunnerQueue = gcnew CefSharp::Internals::MethodRunnerQueue(_javaScriptObjectRepository);
72+
}
73+
6574
_methodRunnerQueue->MethodInvocationComplete += gcnew EventHandler<MethodInvocationCompleteArgs^>(this, &ManagedCefBrowserAdapter::MethodInvocationComplete);
6675
_methodRunnerQueue->Start();
6776
}
@@ -126,9 +135,9 @@ namespace CefSharp
126135
CefSharp::Internals::JavascriptObjectRepository^ get();
127136
}
128137

129-
virtual property MethodRunnerQueue^ MethodRunnerQueue
138+
virtual property IMethodRunnerQueue^ MethodRunnerQueue
130139
{
131-
CefSharp::Internals::MethodRunnerQueue^ get();
140+
CefSharp::Internals::IMethodRunnerQueue^ get();
132141
}
133142
};
134143
}

‎CefSharp.Example/CefExample.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class CefExample
1717
public const string DefaultUrl = BaseUrl + "/home.html";
1818
public const string BindingTestUrl = BaseUrl + "/BindingTest.html";
1919
public const string BindingTestSingleUrl = BaseUrl + "/BindingTestSingle.html";
20+
public const string BindingTestsAsyncTaskUrl = BaseUrl + "/BindingTestsAsyncTask.html";
2021
public const string LegacyBindingTestUrl = BaseUrl + "/LegacyBindingTest.html";
2122
public const string PostMessageTestUrl = BaseUrl + "/PostMessageTest.html";
2223
public const string PluginsTestUrl = BaseUrl + "/plugins.html";
@@ -188,7 +189,8 @@ public static void Init(AbstractCefSettings settings, IBrowserProcessHandler bro
188189
//This must be set before Cef.Initialized is called
189190
CefSharpSettings.FocusedNodeChangedEnabled = true;
190191

191-
//Experimental option where bound async methods are queued on TaskScheduler.Default.
192+
//Async Javascript Binding - methods are queued on TaskScheduler.Default.
193+
//Set this to true to when you have methods that return Task<T>
192194
//CefSharpSettings.ConcurrentTaskExecution = true;
193195

194196
//Legacy Binding Behaviour doesn't work for cross-site navigation (navigating to a different domain)

‎CefSharp.Example/CefSharp.Example.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<Content Include="Resources\assets\js\shBrushCSharp.js" />
156156
<None Include="Resources\assets\js\shBrushJScript.js" />
157157
<Content Include="Resources\assets\js\shCore.js" />
158+
<Content Include="Resources\BindingTestsAsyncTask.html" />
158159
<Content Include="Resources\BindingTestSingle.html" />
159160
<Content Include="Resources\DragDropCursorsTest.html" />
160161
<Content Include="Resources\JavascriptCallbackTest.html" />

‎CefSharp.Example/CefSharpSchemeHandlerFactory.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ static CefSharpSchemeHandlerFactory()
5454
{ "/Recaptcha.html", Resources.Recaptcha },
5555
{ "/UnicodeExampleGreaterThan32kb.html", Resources.UnicodeExampleGreaterThan32kb },
5656
{ "/UnocodeExampleEqualTo32kb.html", Resources.UnocodeExampleEqualTo32kb },
57-
{ "/JavascriptCallbackTest.html", Resources.JavascriptCallbackTest }
57+
{ "/JavascriptCallbackTest.html", Resources.JavascriptCallbackTest },
58+
{ "/BindingTestsAsyncTask.html", Resources.BindingTestsAsyncTask }
5859
};
5960
}
6061

‎CefSharp.Example/JavascriptBinding/AsyncBoundObject.cs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
using System.Collections;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9+
using System.Linq;
10+
using System.Net;
911
using System.Text;
1012
using System.Threading;
13+
using System.Threading.Tasks;
1114

1215
namespace CefSharp.Example.JavascriptBinding
1316
{
@@ -147,5 +150,47 @@ public Dictionary<string, IDictionary> MethodReturnsDictionary3()
147150
{"data", MethodReturnsDictionary2()}
148151
};
149152
}
153+
154+
//The Following Test methods can only be used when
155+
//CefSharpSettings.ConcurrentTaskExecution = true;
156+
//There is a seperate set of QUnit tests for these
157+
158+
public Task<string> ReturnTaskStringAsync()
159+
{
160+
return Task.FromResult(nameof(ReturnTaskStringAsync));
161+
}
162+
163+
public async void VoidReturnAsync()
164+
{
165+
await Task.Delay(1000);
166+
167+
Debug.WriteLine("Delayed 1 second.");
168+
}
169+
170+
public async Task<string> AsyncWaitTwoSeconds(string str)
171+
{
172+
await Task.Delay(2000);
173+
174+
return str;
175+
}
176+
177+
public async Task<string[]> AsyncDownloadFileAndSplitOnNewLines(string url)
178+
{
179+
var webClient = new WebClient();
180+
var download = await webClient.DownloadStringTaskAsync(new Uri(url));
181+
182+
var lines = download.Split('\n').Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();
183+
184+
return lines;
185+
}
186+
187+
//We expect an exception here, so tell VS to ignore
188+
[DebuggerHidden]
189+
public async Task<string> AsyncThrowException()
190+
{
191+
await Task.Delay(2000);
192+
193+
throw new Exception("Expected Exception");
194+
}
150195
}
151196
}

‎CefSharp.Example/Properties/Resources.Designer.cs‎

Lines changed: 39 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎CefSharp.Example/Properties/Resources.resx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@
217217
<data name="JavascriptCallbackTest" type="System.Resources.ResXFileRef, System.Windows.Forms">
218218
<value>..\Resources\JavascriptCallbackTest.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
219219
</data>
220+
<data name="BindingTestsAsyncTask" type="System.Resources.ResXFileRef, System.Windows.Forms">
221+
<value>..\Resources\BindingTestsAsyncTask.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
222+
</data>
220223
<data name="PostMessageTest" type="System.Resources.ResXFileRef, System.Windows.Forms">
221224
<value>..\Resources\PostMessageTest.html;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
222225
</data>
Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,68 @@
1-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
22
<html>
3-
<head>
4-
<title>Binding Test</title>
5-
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.4.1.css">
6-
</head>
7-
<body>
8-
<div id="qunit"></div>
9-
<div id="qunit-fixture"></div>
10-
<script src="https://code.jquery.com/qunit/qunit-2.4.1.js"></script>
11-
12-
<script type="text/javascript">
3+
<head>
4+
<title>Binding Test</title>
5+
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.4.1.css">
6+
</head>
7+
<body>
8+
<div>
9+
These tests require CefSharpSettings.ConcurrentTaskExecution = true;
10+
Which by default is set to false
11+
</div>
12+
13+
<div id="qunit"></div>
14+
<div id="qunit-fixture"></div>
15+
<script src="https://code.jquery.com/qunit/qunit-2.4.1.js"></script>
16+
17+
<script type="text/javascript">
1318
(async () =>
1419
{
15-
await CefSharp.BindObjectAsync("boundAsync", "bound");
20+
await CefSharp.BindObjectAsync("boundAsync");
1621

17-
QUnit.test( "Struct Test:", function( assert )
22+
QUnit.test("ReturnTaskStringAsync:", function (assert)
1823
{
19-
var asyncCallback = assert.async();
24+
let asyncCallback = assert.async();
2025

21-
//Returns a Struct
22-
boundAsync.returnObject('CefSharp Struct Test').then(function (actualResult)
26+
boundAsync.returnTaskStringAsync().then(function (actualResult)
2327
{
24-
const expectedResult = 'CefSharp Struct Test';
28+
const expectedResult = 'ReturnTaskStringAsync';
2529

26-
assert.equal(expectedResult, actualResult.Value, "Return class " + expectedResult);
30+
assert.equal(expectedResult, actualResult, "Return string " + expectedResult);
2731

2832
asyncCallback();
29-
});
33+
});
3034
});
3135

32-
QUnit.test( "Class Test:", function( assert )
36+
QUnit.test("VoidReturnAsync Test:", function (assert)
3337
{
34-
var asyncCallback = assert.async();
38+
let asyncCallback = assert.async();
3539

36-
//Returns a class
37-
boundAsync.returnClass('CefSharp Class Test').then(function (actualResult)
40+
boundAsync.voidReturnAsync().then(function (actualResult)
3841
{
39-
const expectedResult = 'CefSharp Class Test';
42+
const expectedResult = null;
4043

41-
assert.equal(expectedResult, actualResult.Value, "Return class " + expectedResult);
44+
assert.equal(expectedResult, actualResult, "Return null");
4245

4346
asyncCallback();
44-
});
47+
});
4548
});
4649

50+
QUnit.test("AsyncWaitTwoSeconds Test:", function (assert)
51+
{
52+
let asyncCallback = assert.async();
53+
54+
const expectedResult = "This method waited two seconds";
55+
56+
boundAsync.asyncWaitTwoSeconds(expectedResult).then(function (actualResult)
57+
{
58+
assert.equal(expectedResult, actualResult, "Return string " + expectedResult);
59+
60+
asyncCallback();
61+
});
62+
});
4763

4864
})();
49-
</script>
65+
</script>
5066

51-
</body>
67+
</body>
5268
</html>

0 commit comments

Comments
 (0)