Skip to content

Commit 792dcd2

Browse files
committed
Merge branch 'JsBinding_WIP' of https://github.com/bjarteskogoy/CefSharp into JsBinding/Wip2
2 parents 86dc296 + 17b56e6 commit 792dcd2

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

CefSharp.BrowserSubprocess/CefRenderProcess.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,37 @@ public Task<JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId,
132132

133133
return timeout.HasValue ? task.WithTimeout(timeout.Value) : task;
134134
}
135+
136+
public IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state)
137+
{
138+
var tcs = new TaskCompletionSource<JavascriptResponse>(state);
139+
var task = EvaluateScriptAsync(browserId, frameId, script, timeout);
140+
task.ContinueWith(t =>
141+
{
142+
if (t.IsFaulted)
143+
{
144+
tcs.TrySetException(t.Exception.InnerExceptions);
145+
}
146+
else if (t.IsCanceled)
147+
{
148+
tcs.TrySetCanceled();
149+
}
150+
else
151+
{
152+
tcs.TrySetResult(t.Result);
153+
}
154+
155+
if (callback != null)
156+
{
157+
callback(tcs.Task);
158+
}
159+
});
160+
return tcs.Task;
161+
}
162+
163+
public JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result)
164+
{
165+
return ((Task<JavascriptResponse>)result).Result;
166+
}
135167
}
136168
}

CefSharp/Internals/BrowserProcessServiceHost.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ public void SetOperationContext(OperationContext operationContext)
5151
public Task<JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout)
5252
{
5353
var operationContextTask = operationContextTaskCompletionSource.Task;
54-
5554
return operationContextTask.ContinueWith(t =>
5655
{
5756
var context = t.Result;
5857
var renderProcess = context.GetCallbackChannel<IRenderProcess>();
59-
return renderProcess.EvaluateScriptAsync(browserId, frameId, script, timeout);
58+
return Task.Factory.FromAsync<JavascriptResponse>(renderProcess.BeginEvaluateScriptAsync(browserId, frameId, script, timeout, null, null), renderProcess.EndEvaluateScriptAsync);
6059
}).Unwrap();
6160
}
6261

CefSharp/Internals/IRenderProcess.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace CefSharp.Internals
1111
[ServiceContract]
1212
public interface IRenderProcess
1313
{
14-
[OperationContract]
15-
Task<JavascriptResponse> EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout);
14+
[OperationContract(AsyncPattern=true)]
15+
IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state);
16+
17+
JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result);
1618
}
1719
}

0 commit comments

Comments
 (0)