Finish importing #289, script is now executed as a task using CefTaskScheduler

This commit is contained in:
amaitland
2014-08-29 12:32:52 +10:00
parent 5faa2a2e5a
commit ca4cd6b9a5
4 changed files with 35 additions and 46 deletions
@@ -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<CefV8Context> 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<CefV8Context> context, CefString script)
Object^ EvaluateScriptInContext(CefRefPtr<CefV8Context> context, CefString script)
{
CefRefPtr<CefV8Value> result;
CefRefPtr<CefV8Exception> 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;
}
}
@@ -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<CefBrowserUnmanagedWrapper> _unmanagedWrapper;
public:
CefBrowserWrapper(CefRefPtr<CefBrowser> 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<String^>(unmanagedWrapper->EvaluateScriptExceptionMessage) != nullptr)
{
throw gcnew FaultException(unmanagedWrapper->EvaluateScriptExceptionMessage);
}
return unmanagedWrapper->EvaluateScriptResult;
return _unmanagedWrapper->EvaluateScriptCallback(frameId, StringUtils::ToNative(script));
}
};
}
@@ -56,11 +56,9 @@ namespace CefSharp.BrowserSubprocess
public Task<object> EvaluateScript(int frameId, string script, double timeout)
{
return Task<object>.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()
+12 -1
View File
@@ -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<object> EvaluateScript(long frameId, string script, double timeout)
{
return RenderThreadTaskFactory.StartNew(() =>
{
return DoEvaluateScript(frameId, script);
}, TaskCreationOptions.AttachedToParent);
}
protected abstract object DoEvaluateScript(long frameId, string script);
}
}