From d6661df3443755ffab79062fbb0d5b56e894fd0d Mon Sep 17 00:00:00 2001 From: David Serfozo Date: Tue, 7 Jul 2015 19:26:56 +0200 Subject: [PATCH] removed unnecessary code from previous js callback implementation --- .../CefBrowserWrapper.cpp | 10 --- .../CefBrowserWrapper.h | 6 +- .../CefRenderProcess.cs | 80 +------------------ CefSharp/CefSharp.csproj | 3 - .../Internals/BrowserProcessServiceHost.cs | 23 ------ CefSharp/Internals/IBrowserProcess.cs | 2 +- CefSharp/Internals/IRenderProcess.cs | 22 ----- .../JavascriptCallbackEndpointBehavior.cs | 1 - .../Internals/JavascriptCallbackFactory.cs | 19 ----- CefSharp/Internals/JavascriptCallbackProxy.cs | 58 -------------- .../Internals/JavascriptCallbackSurrogate.cs | 2 +- 11 files changed, 5 insertions(+), 221 deletions(-) delete mode 100644 CefSharp/Internals/IRenderProcess.cs delete mode 100644 CefSharp/Internals/JavascriptCallbackFactory.cs delete mode 100644 CefSharp/Internals/JavascriptCallbackProxy.cs diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp index f89b213e1..17a105cbe 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp @@ -31,16 +31,6 @@ namespace CefSharp } } - JavascriptResponse^ CefBrowserWrapper::DoCallback(System::Int64 callbackId, array^ parameters) - { - return _callbackRegistry->Execute(callbackId, parameters); - } - - void CefBrowserWrapper::DestroyJavascriptCallback(Int64 id) - { - _callbackRegistry->Deregister(id); - } - JavascriptCallbackRegistry^ CefBrowserWrapper::CallbackRegistry::get() { return _callbackRegistry; diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h index 9c20cd855..ead63a594 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h @@ -45,7 +45,7 @@ namespace CefSharp property bool IsPopup; // This allows us to create the WCF proxies back to our parent process. - property DuplexChannelFactory^ ChannelFactory; + property ChannelFactory^ ChannelFactory; // The serialized registered object data waiting to be used. property JavascriptRootObject^ JavascriptRootObject; @@ -58,9 +58,5 @@ namespace CefSharp // The WCF proxy to the parent process. property IBrowserProcess^ BrowserProcess; - - JavascriptResponse^ DoCallback(System::Int64 callbackId, array^ parameters); - - void DestroyJavascriptCallback(Int64 id); }; } diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs index 839c1b965..e4bbf4cc4 100644 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ b/CefSharp.BrowserSubprocess/CefRenderProcess.cs @@ -13,7 +13,7 @@ using TaskExtensions = CefSharp.Internals.TaskExtensions; namespace CefSharp.BrowserSubprocess { [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults=true)] - public class CefRenderProcess : CefSubProcess, IRenderProcess + public class CefRenderProcess : CefSubProcess { private int? parentBrowserId; private List browsers = new List(); @@ -56,8 +56,7 @@ namespace CefSharp.BrowserSubprocess var binding = BrowserProcessServiceHost.CreateBinding(); - var channelFactory = new DuplexChannelFactory( - this, + var channelFactory = new ChannelFactory( binding, new EndpointAddress(serviceName) ); @@ -112,80 +111,5 @@ namespace CefSharp.BrowserSubprocess browser.BrowserProcess = null; browser.JavascriptRootObject = null; } - - public IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state) - { - var tcs = new TaskCompletionSource(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)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 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; - } } } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 1b20f23c0..660456b35 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -117,8 +117,6 @@ - - @@ -189,7 +187,6 @@ - diff --git a/CefSharp/Internals/BrowserProcessServiceHost.cs b/CefSharp/Internals/BrowserProcessServiceHost.cs index bdf69ee17..bd490ca0b 100644 --- a/CefSharp/Internals/BrowserProcessServiceHost.cs +++ b/CefSharp/Internals/BrowserProcessServiceHost.cs @@ -78,29 +78,6 @@ namespace CefSharp.Internals } } - internal Task JavascriptCallback(int browserId, long id, object[] parameters, TimeSpan? timeout) - { - var operationContextTask = operationContextTaskCompletionSource.Task; - return operationContextTask.ContinueWith(t => - { - var context = t.Result; - var renderProcess = context.GetCallbackChannel(); - var asyncResult = renderProcess.BeginJavascriptCallbackAsync(browserId, id, parameters, timeout, null, null); - return Task.Factory.FromAsync(asyncResult, renderProcess.EndJavascriptCallbackAsync); - }).Unwrap(); - } - - internal void DestroyJavascriptCallback(int browserId, long id) - { - var operationContextTask = operationContextTaskCompletionSource.Task; - operationContextTask.ContinueWith(t => - { - var context = t.Result; - var renderProcess = context.GetCallbackChannel(); - renderProcess.DestroyJavascriptCallback(browserId, id); - }); - } - protected override void OnClosed() { base.OnClosed(); diff --git a/CefSharp/Internals/IBrowserProcess.cs b/CefSharp/Internals/IBrowserProcess.cs index e60dc96e8..3583b0847 100644 --- a/CefSharp/Internals/IBrowserProcess.cs +++ b/CefSharp/Internals/IBrowserProcess.cs @@ -7,7 +7,7 @@ using System.ServiceModel; namespace CefSharp.Internals { - [ServiceContract(CallbackContract = typeof(IRenderProcess), SessionMode = SessionMode.Required)] + [ServiceContract(SessionMode = SessionMode.Required)] [ServiceKnownType(typeof(object[]))] [ServiceKnownType(typeof(Dictionary))] [ServiceKnownType(typeof(JavascriptObject))] diff --git a/CefSharp/Internals/IRenderProcess.cs b/CefSharp/Internals/IRenderProcess.cs deleted file mode 100644 index 4b232314f..000000000 --- a/CefSharp/Internals/IRenderProcess.cs +++ /dev/null @@ -1,22 +0,0 @@ -// 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. - -using System; -using System.ServiceModel; -using System.Threading.Tasks; - -namespace CefSharp.Internals -{ - [ServiceContract] - public interface IRenderProcess - { - [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); - } -} diff --git a/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs b/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs index 6d4d6c0db..854438053 100644 --- a/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs +++ b/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs @@ -16,7 +16,6 @@ namespace CefSharp.Internals private static readonly List Methods = new List { ReflectionUtils.GetMethodName(p => p.CallMethod(0, null, null)), - ReflectionUtils.GetMethodName(p => p.BeginJavascriptCallbackAsync(0, 0, null, null, null, null)).Substring(5), }; private readonly IJavascriptCallbackFactory callbackFactory; diff --git a/CefSharp/Internals/JavascriptCallbackFactory.cs b/CefSharp/Internals/JavascriptCallbackFactory.cs deleted file mode 100644 index cf197be0e..000000000 --- a/CefSharp/Internals/JavascriptCallbackFactory.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; - -namespace CefSharp.Internals -{ - public class JavascriptCallbackFactory : IJavascriptCallbackFactory - { - private readonly WeakReference browserProcessServiceHost; - - public JavascriptCallbackFactory(WeakReference browserProcessServiceHost) - { - this.browserProcessServiceHost = browserProcessServiceHost; - } - - public IJavascriptCallback Create(JavascriptCallback callback) - { - return new JavascriptCallbackProxy(callback.Id, callback.BrowserId, browserProcessServiceHost); - } - } -} diff --git a/CefSharp/Internals/JavascriptCallbackProxy.cs b/CefSharp/Internals/JavascriptCallbackProxy.cs deleted file mode 100644 index 916198c2c..000000000 --- a/CefSharp/Internals/JavascriptCallbackProxy.cs +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright © 2010-2015 The CefSharp Project. All rights reserved. -// -// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. - -using System; -using System.Runtime.Serialization; -using System.ServiceModel; -using System.Threading.Tasks; - -namespace CefSharp.Internals -{ - /// - /// JavascriptCallbackProxy is a wrapper class around the WCF BrowserProcess. - /// When the render process detects a function as param or return type it will add a - /// that identifies the function. On the server side - /// WCF Transforms into - /// using the class. - /// - [DataContract] - internal sealed class JavascriptCallbackProxy : DisposableResource, IJavascriptCallback - { - private readonly long id; - private readonly int browserId; - private readonly WeakReference browserProcessWeakReference; - - public JavascriptCallbackProxy(long id, int browserId, WeakReference browserProcessWeakReference) - { - this.id = id; - this.browserId = browserId; - this.browserProcessWeakReference = browserProcessWeakReference; - } - - public Task ExecuteAsync(params object[] parms) - { - if (IsDisposed) - { - throw new ObjectDisposedException("JavascriptCallbackProxy is already disposed."); - } - - var browserProcess = (BrowserProcessServiceHost)browserProcessWeakReference.Target; - if (browserProcess == null) - { - throw new ObjectDisposedException("BrowserProcessServiceHost is already disposed."); - } - - return browserProcess.JavascriptCallback(browserId, id, parms, null); - } - - protected override void DoDispose(bool isDisposing) - { - var browserProcess = (BrowserProcessServiceHost)browserProcessWeakReference.Target; - if (!IsDisposed && browserProcess != null && browserProcess.State == CommunicationState.Opened) - { - browserProcess.DestroyJavascriptCallback(browserId, id); - } - } - } -} diff --git a/CefSharp/Internals/JavascriptCallbackSurrogate.cs b/CefSharp/Internals/JavascriptCallbackSurrogate.cs index f0ca870b4..f62b0490e 100644 --- a/CefSharp/Internals/JavascriptCallbackSurrogate.cs +++ b/CefSharp/Internals/JavascriptCallbackSurrogate.cs @@ -23,7 +23,7 @@ namespace CefSharp.Internals { if (type == typeof (JavascriptCallback)) { - return typeof (JavascriptCallbackProxy); + return typeof (IJavascriptCallback); } return type; }