diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefBrowserUnmanagedWrapper.h index 4e9293a7c..e9874ff3b 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserUnmanagedWrapper.h @@ -44,47 +44,43 @@ namespace CefSharp delete waithandle; } - void EvaluateScriptCallback(int64 frameId, CefString script, double timeout) + Object^ EvaluateScriptCallback(int64 frameId, CefString script) { - // TODO: Do something about the timeout... - auto frame = _cefBrowser->GetFrame(frameId); CefRefPtr context = frame->GetV8Context(); if (context.get() && context->Enter()) { - EvaluateScriptInContext(context, script); - context->Exit(); + try + { + return EvaluateScriptInContext(context, script); + } + finally + { + context->Exit(); + } } - WaitHandle->Set(); + + return nullptr; } - void EvaluateScriptInContext(CefRefPtr context, CefString script) + Object^ EvaluateScriptInContext(CefRefPtr context, CefString script) { CefRefPtr result; CefRefPtr exception; - EvaluateScriptExceptionMessage = nullptr; - bool success = context->Eval(script, result, exception); if (success) { - try - { - EvaluateScriptResult = TypeUtils::ConvertFromCef(result); - } - catch (Exception^ ex) - { - EvaluateScriptExceptionMessage = ex->Message; - } + return TypeUtils::ConvertFromCef(result); } else if (exception.get()) { - EvaluateScriptExceptionMessage = StringUtils::ToClr(exception->GetMessage()); + throw gcnew ScriptException(StringUtils::ToClr(exception->GetMessage())); } else { - EvaluateScriptExceptionMessage = "Failed to evaluate script"; + return nullptr; } } diff --git a/CefSharp.BrowserSubprocess.Core/CefSubprocessWrapper.h b/CefSharp.BrowserSubprocess.Core/CefSubprocessWrapper.h index 8cf8c720d..7b7525905 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSubprocessWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefSubprocessWrapper.h @@ -12,11 +12,13 @@ #include "Stdafx.h" #include "CefBrowserUnmanagedWrapper.h" +#include "CefTaskScheduler.h" using namespace CefSharp::Internals; using namespace System; using namespace System::ServiceModel; using namespace System::Threading; +using namespace System::Threading::Tasks; namespace CefSharp { @@ -27,12 +29,12 @@ namespace CefSharp MCefRefPtr _unmanagedWrapper; public: - CefBrowserWrapper(CefRefPtr cefBrowser) : _cefBrowser(cefBrowser) { BrowserId = cefBrowser->GetIdentifier(); _unmanagedWrapper = new CefBrowserUnmanagedWrapper(cefBrowser); + RenderThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_RENDERER)); } virtual void DoDispose( bool disposing ) override @@ -42,27 +44,9 @@ namespace CefSharp CefBrowserBase::DoDispose( disposing ); } - virtual Object^ EvaluateScript(System::Int32 frameId, String^ script, double timeout) override + virtual Object^ DoEvaluateScript(System::Int64 frameId, String^ script) override { - // TODO: Could we do something genericly useful here using C++ lambdas? To avoid having to make a lot of of these... - // TODO: DON'T USE AUTORESETEVENT STUPIDITY! Even though the code below compiles & runs correctly, it deadlocks the - // thread from which the request came, which is very, very stupid, especially since V8 and Chromium are built - // with asynchrony in mind. Instead, we should re-think this API to utilize WCF callbacks instead: - // http://idunno.org/archive/2008/05/29/wcf-callbacks-a-beginners-guide.aspx - // That feels much more like 2013, and not 1994... :) - // TODO: How about concurrency? One way to easily resolve it is to new() up something unique here and use that to - // invoke the method. - CefBrowserUnmanagedWrapper* unmanagedWrapper = _unmanagedWrapper.get(); - CefPostTask(TID_RENDERER, NewCefRunnableMethod(unmanagedWrapper, - &CefBrowserUnmanagedWrapper::EvaluateScriptCallback, frameId, StringUtils::ToNative(script), timeout)); - unmanagedWrapper->WaitHandle->WaitOne(); - - if (static_cast(unmanagedWrapper->EvaluateScriptExceptionMessage) != nullptr) - { - throw gcnew FaultException(unmanagedWrapper->EvaluateScriptExceptionMessage); - } - - return unmanagedWrapper->EvaluateScriptResult; + return _unmanagedWrapper->EvaluateScriptCallback(frameId, StringUtils::ToNative(script)); } }; } diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs index 87dd87dff..31d82dc9a 100644 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ b/CefSharp.BrowserSubprocess/CefRenderProcess.cs @@ -56,11 +56,9 @@ namespace CefSharp.BrowserSubprocess public Task EvaluateScript(int frameId, string script, double timeout) { - return Task.Factory.StartNew(() => - { - var result = Browser.EvaluateScript(frameId, script, timeout); - return result; - }, TaskCreationOptions.AttachedToParent); + var task = Browser.EvaluateScript(frameId, script, timeout); + + return task; } public override IBrowserProcess CreateBrowserProxy() diff --git a/CefSharp/CefBrowserBase.cs b/CefSharp/CefBrowserBase.cs index e921d8d26..854af6de7 100644 --- a/CefSharp/CefBrowserBase.cs +++ b/CefSharp/CefBrowserBase.cs @@ -2,12 +2,23 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. +using System.Threading.Tasks; + namespace CefSharp { public abstract class CefBrowserBase : DisposableResource { public int BrowserId { get; set; } + public TaskFactory RenderThreadTaskFactory { get; protected set; } - public abstract object EvaluateScript(int frameId, string script, double timeout); + public Task EvaluateScript(long frameId, string script, double timeout) + { + return RenderThreadTaskFactory.StartNew(() => + { + return DoEvaluateScript(frameId, script); + }, TaskCreationOptions.AttachedToParent); + } + + protected abstract object DoEvaluateScript(long frameId, string script); } }