mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 10:22:55 +00:00
added wcf operations to do javascript callbacks
This commit is contained in:
@@ -77,6 +77,16 @@ namespace CefSharp
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
JavascriptResponse^ CefBrowserWrapper::DoCallback(System::Int64 callbackId, array<Object^>^ parameters)
|
||||
{
|
||||
return _callbackRegistry->Execute(callbackId, parameters);
|
||||
}
|
||||
|
||||
void CefBrowserWrapper::DestroyJavascriptCallback(Int64 id)
|
||||
{
|
||||
_callbackRegistry->RemoveWrapper(id);
|
||||
}
|
||||
|
||||
CefBrowserWrapper::~CefBrowserWrapper()
|
||||
{
|
||||
_cefBrowser = nullptr;
|
||||
|
||||
@@ -49,5 +49,9 @@ namespace CefSharp
|
||||
virtual void DoDispose(bool disposing) override;
|
||||
|
||||
virtual JavascriptResponse^ DoEvaluateScript(System::Int64 frameId, String^ script);
|
||||
|
||||
JavascriptResponse^ DoCallback(System::Int64 callbackId, array<Object^>^ parameters);
|
||||
|
||||
void DestroyJavascriptCallback(Int64 id);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,20 +8,20 @@ using TaskExtensions = CefSharp.Internals.TaskExtensions;
|
||||
|
||||
namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults=true)]
|
||||
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults = true)]
|
||||
public class CefRenderProcess : CefSubProcess, IRenderProcess
|
||||
{
|
||||
private int? parentBrowserId;
|
||||
private List<CefBrowserWrapper> browsers = new List<CefBrowserWrapper>();
|
||||
|
||||
public CefRenderProcess(IEnumerable<string> args)
|
||||
public CefRenderProcess(IEnumerable<string> args)
|
||||
: base(args)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
protected override void DoDispose(bool isDisposing)
|
||||
{
|
||||
foreach(var browser in browsers)
|
||||
foreach (var browser in browsers)
|
||||
{
|
||||
browser.Dispose();
|
||||
}
|
||||
@@ -55,7 +55,7 @@ namespace CefSharp.BrowserSubprocess
|
||||
this,
|
||||
binding,
|
||||
new EndpointAddress(serviceName)
|
||||
);
|
||||
);
|
||||
|
||||
channelFactory.Open();
|
||||
|
||||
@@ -80,7 +80,7 @@ namespace CefSharp.BrowserSubprocess
|
||||
browser.ChannelFactory = channelFactory;
|
||||
browser.BrowserProcess = browserProcess;
|
||||
}
|
||||
catch(Exception)
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -137,7 +137,6 @@ namespace CefSharp.BrowserSubprocess
|
||||
Message = ex.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
}, TaskCreationOptions.AttachedToParent);
|
||||
|
||||
return timeout.HasValue ? task.WithTimeout(timeout.Value) : task;
|
||||
@@ -174,5 +173,80 @@ namespace CefSharp.BrowserSubprocess
|
||||
{
|
||||
return ((Task<JavascriptResponse>)result).Result;
|
||||
}
|
||||
|
||||
public IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<JavascriptResponse>(state);
|
||||
var task = JavascriptCallbackAsync(browserId, callbackId, parameters, timeout);
|
||||
task.ContinueWith(t =>
|
||||
{
|
||||
if (t.IsFaulted)
|
||||
{
|
||||
tcs.TrySetException(t.Exception.InnerExceptions);
|
||||
}
|
||||
else if (t.IsCanceled)
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.TrySetResult(t.Result);
|
||||
}
|
||||
|
||||
if (callback != null)
|
||||
{
|
||||
callback(tcs.Task);
|
||||
}
|
||||
});
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public JavascriptResponse EndJavascriptCallbackAsync(IAsyncResult result)
|
||||
{
|
||||
return ((Task<JavascriptResponse>)result).Result;
|
||||
}
|
||||
|
||||
public void DestroyJavascriptCallback(int browserId, long id)
|
||||
{
|
||||
var browser = browsers.FirstOrDefault(x => x.BrowserId == browserId);
|
||||
if (browser != null)
|
||||
{
|
||||
browser.DestroyJavascriptCallback(id);
|
||||
}
|
||||
}
|
||||
|
||||
private Task<JavascriptResponse> JavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout)
|
||||
{
|
||||
var factory = RenderThreadTaskFactory;
|
||||
var browser = browsers.FirstOrDefault(x => x.BrowserId == browserId);
|
||||
if (browser == null)
|
||||
{
|
||||
return TaskExtensions.FromResult(new JavascriptResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Format("Browser with Id {0} not found in Render Sub Process.", browserId)
|
||||
});
|
||||
}
|
||||
|
||||
var task = factory.StartNew(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = browser.DoCallback(callbackId, parameters);
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new JavascriptResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = ex.ToString()
|
||||
};
|
||||
}
|
||||
}, TaskCreationOptions.AttachedToParent);
|
||||
|
||||
return timeout.HasValue ? task.WithTimeout(timeout.Value) : task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,11 @@
|
||||
<Compile Include="CefDirtyRect.cs" />
|
||||
<Compile Include="CefFileDialogMode.cs" />
|
||||
<Compile Include="DefaultResourceHandlerFactory.cs" />
|
||||
<Compile Include="Internals\JavascriptCallback.cs" />
|
||||
<Compile Include="Internals\JavascriptCallbackDto.cs" />
|
||||
<Compile Include="Internals\JavascriptCallbackEndpointBehavior.cs" />
|
||||
<Compile Include="Internals\JavascriptCallbackSurrogate.cs" />
|
||||
<Compile Include="Internals\ReflectionUtils.cs" />
|
||||
<Compile Include="IResourceHandlerFactory.cs" />
|
||||
<Compile Include="MouseEvent.cs" />
|
||||
<Compile Include="DragOperationsMask.cs" />
|
||||
|
||||
@@ -9,11 +9,19 @@ using System.Threading.Tasks;
|
||||
namespace CefSharp.Internals
|
||||
{
|
||||
[ServiceContract]
|
||||
public interface IRenderProcess
|
||||
public interface IRenderProcess
|
||||
{
|
||||
[OperationContract(AsyncPattern=true)]
|
||||
[OperationContract(AsyncPattern = true)]
|
||||
IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state);
|
||||
|
||||
JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result);
|
||||
|
||||
[OperationContract(AsyncPattern = true)]
|
||||
IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state);
|
||||
|
||||
JavascriptResponse EndJavascriptCallbackAsync(IAsyncResult result);
|
||||
|
||||
[OperationContract]
|
||||
void DestroyJavascriptCallback(int browserId, long id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user