diff --git a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp index 7d7c8b5fc..f3725e954 100644 --- a/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp +++ b/CefSharp.BrowserSubprocess.Core/Async/JavascriptAsyncMethodHandler.cpp @@ -29,8 +29,9 @@ namespace CefSharp } SetInt64(_objectId, argList, 0); - argList->SetString(1, StringUtils::ToNative(_method->JavascriptName)); - argList->SetList(2, params); + SetInt64(0, argList, 1); + argList->SetString(2, StringUtils::ToNative(_method->JavascriptName)); + argList->SetList(3, params); _browser->SendProcessMessage(CefProcessId::PID_BROWSER, request); return true; diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index ba5912126..df9068210 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -106,7 +106,7 @@ namespace CefSharp //Error handling for missing/closed browser if (browserWrapper == nullptr) { - if (name == kJavascriptCallbackDestroyRequest || name == kJavascriptRootObjectRequest) + if (name == kJavascriptCallbackDestroyRequest || name == kJavascriptRootObjectRequest || name == kJavascriptMethodCallResponse) { //If we can't find the browser wrapper then we'll just //ignore this as it's likely already been disposed of diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index bdb8bc23d..e7f5c3234 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -859,9 +859,10 @@ namespace CefSharp if (!browser->IsPopup()) { auto objectId = GetInt64(argList, 0); - auto methodName = StringUtils::ToClr(argList->GetString(1)); - auto arguments = argList->GetList(2); - auto methodInvocation = gcnew MethodInvocation(objectId, methodName); + auto callbackId = GetInt64(argList, 1); + auto methodName = StringUtils::ToClr(argList->GetString(2)); + auto arguments = argList->GetList(3); + auto methodInvocation = gcnew MethodInvocation(objectId, methodName, (callbackId > 0 ? Nullable(callbackId) : Nullable())); for (auto i = 0; i < arguments->GetSize(); i++) { methodInvocation->Parameters->Add(DeserializeV8Object(arguments, i, callbackFactory)); @@ -899,5 +900,25 @@ namespace CefSharp { return _pendingTaskRepository; } + + void ClientAdapter::MethodInvocationComplete(MethodInvocationResult^ result) + { + if (result->CallbackId.HasValue) + { + auto message = CefProcessMessage::Create(kJavascriptMethodCallResponse); + auto argList = message->GetArgumentList(); + SetInt64(result->CallbackId.Value, argList, 0); + argList->SetBool(1, result->Success); + if (result->Success) + { + SerializeV8Object(result->Result, argList, 2); + } + else + { + argList->SetString(2, StringUtils::ToNative(result->Message)); + } + _cefBrowser->SendProcessMessage(CefProcessId::PID_RENDERER, message); + } + } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 258fc5513..cb06082e8 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -81,6 +81,7 @@ namespace CefSharp CefRefPtr GetCefBrowser() { return _cefBrowser; } PendingTaskRepository^ GetPendingTaskRepository(); void CloseAllPopups(bool forceClose); + void MethodInvocationComplete(MethodInvocationResult^ result); // CefClient virtual DECL CefRefPtr GetLifeSpanHandler() OVERRIDE { return this; } diff --git a/CefSharp.Core/Internals/Messaging/Messages.h b/CefSharp.Core/Internals/Messaging/Messages.h index d96279ad1..3fdcc0c3f 100644 --- a/CefSharp.Core/Internals/Messaging/Messages.h +++ b/CefSharp.Core/Internals/Messaging/Messages.h @@ -24,8 +24,12 @@ namespace CefSharp const CefString kJavascriptCallbackDestroyRequest = "JavascriptCallbackDestroyRequest"; //Message containing the result of a given js function call const CefString kJavascriptCallbackResponse = "JavascriptCallbackDoneResponse"; + //Message containing a js root object for async bindings const CefString kJavascriptRootObjectRequest = "JavascriptRootObjectRequest"; + //Message from the render process to request a method invocation on a bound object const CefString kJavascriptMethodCallRequest = "JavascriptMethodCallRequest"; + //Message from the browser process containing the result of a bound method invocation + const CefString kJavascriptMethodCallResponse = "JavascriptMethodCallResponse"; } } } \ No newline at end of file diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index 80dde1068..b86e15dc4 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -4,6 +4,9 @@ #include "Stdafx.h" #include "Cef.h" +#include "Internals/Messaging/Messages.h" + +using namespace CefSharp::Internals::Messaging; bool ManagedCefBrowserAdapter::IsDisposed::get() { @@ -355,3 +358,12 @@ MethodRunnerQueue^ ManagedCefBrowserAdapter::MethodRunnerQueue::get() { return _methodRunnerQueue; } + +void ManagedCefBrowserAdapter::MethodInvocationComplete(Object^ sender, MethodInvocationCompleteArgs^ e) +{ + auto result = e->Result; + if (result->CallbackId.HasValue) + { + _clientAdapter->MethodInvocationComplete(result); + } +} diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.h b/CefSharp.Core/ManagedCefBrowserAdapter.h index 8a52ebcc6..b709de0b0 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.h +++ b/CefSharp.Core/ManagedCefBrowserAdapter.h @@ -48,6 +48,7 @@ namespace CefSharp int GetCefKeyboardModifiers(WPARAM wparam, LPARAM lparam); CefMouseEvent GetCefMouseEvent(MouseEvent^ mouseEvent); + void MethodInvocationComplete(Object^ sender, MethodInvocationCompleteArgs^ e); public: ManagedCefBrowserAdapter(IWebBrowserInternal^ webBrowserInternal, bool offScreenRendering) : _isDisposed(false) @@ -65,6 +66,7 @@ namespace CefSharp _javaScriptObjectRepository = gcnew CefSharp::Internals::JavascriptObjectRepository(); _javascriptCallbackFactory = gcnew CefSharp::Internals::JavascriptCallbackFactory(_clientAdapter->GetPendingTaskRepository()); _methodRunnerQueue = gcnew CefSharp::Internals::MethodRunnerQueue(_javaScriptObjectRepository); + _methodRunnerQueue->MethodInvocationComplete += gcnew EventHandler(this, &ManagedCefBrowserAdapter::MethodInvocationComplete); _methodRunnerQueue->Start(); } @@ -88,6 +90,7 @@ namespace CefSharp if (_methodRunnerQueue != nullptr) { + _methodRunnerQueue->MethodInvocationComplete -= gcnew EventHandler(this, &ManagedCefBrowserAdapter::MethodInvocationComplete); _methodRunnerQueue->Stop(); _methodRunnerQueue = nullptr; } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 7408b2794..00f8f736d 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -94,6 +94,8 @@ + + diff --git a/CefSharp/Internals/MethodInvocation.cs b/CefSharp/Internals/MethodInvocation.cs index fde1e39e4..997d61e93 100644 --- a/CefSharp/Internals/MethodInvocation.cs +++ b/CefSharp/Internals/MethodInvocation.cs @@ -10,6 +10,8 @@ namespace CefSharp.Internals { private readonly List parameters = new List(); + public long? CallbackId { get; private set; } + public long ObjectId { get; private set; } public string MethodName { get; private set; } @@ -20,7 +22,13 @@ namespace CefSharp.Internals } public MethodInvocation(long objectId, string methodName) + :this(objectId, methodName, null) { + } + + public MethodInvocation(long objectId, string methodName, long? callbackId) + { + CallbackId = callbackId; ObjectId = objectId; MethodName = methodName; } diff --git a/CefSharp/Internals/MethodInvocationCompleteArgs.cs b/CefSharp/Internals/MethodInvocationCompleteArgs.cs new file mode 100644 index 000000000..6d53ba276 --- /dev/null +++ b/CefSharp/Internals/MethodInvocationCompleteArgs.cs @@ -0,0 +1,14 @@ +using System; + +namespace CefSharp.Internals +{ + public sealed class MethodInvocationCompleteArgs : EventArgs + { + public MethodInvocationResult Result { get; private set; } + + public MethodInvocationCompleteArgs(MethodInvocationResult result) + { + Result = result; + } + } +} diff --git a/CefSharp/Internals/MethodInvocationResult.cs b/CefSharp/Internals/MethodInvocationResult.cs new file mode 100644 index 000000000..3aad588e9 --- /dev/null +++ b/CefSharp/Internals/MethodInvocationResult.cs @@ -0,0 +1,13 @@ +namespace CefSharp.Internals +{ + public sealed class MethodInvocationResult + { + public long? CallbackId { get; set; } + + public string Message { get; set; } + + public bool Success { get; set; } + + public object Result { get; set; } + } +} diff --git a/CefSharp/Internals/MethodRunnerQueue.cs b/CefSharp/Internals/MethodRunnerQueue.cs index 7651c0d52..5e81b9d1c 100644 --- a/CefSharp/Internals/MethodRunnerQueue.cs +++ b/CefSharp/Internals/MethodRunnerQueue.cs @@ -10,7 +10,9 @@ namespace CefSharp.Internals private readonly JavascriptObjectRepository repository; private readonly AutoResetEvent stopped = new AutoResetEvent(false); private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - private readonly BlockingCollection> queue = new BlockingCollection>(); + private readonly BlockingCollection> queue = new BlockingCollection>(); + + public event EventHandler MethodInvocationComplete; public MethodRunnerQueue(JavascriptObjectRepository repository) { @@ -25,6 +27,7 @@ namespace CefSharp.Internals { var task = queue.Take(cancellationTokenSource.Token); task.RunSynchronously(TaskScheduler.Current); + OnMethodInvocationComplete(task.Result); } stopped.Set(); }, TaskCreationOptions.LongRunning); @@ -38,13 +41,14 @@ namespace CefSharp.Internals public void Enqueue(MethodInvocation methodInvocation) { - var task = new Task(() => + var task = new Task(() => { object result; string exception; var success = repository.TryCallMethod(methodInvocation.ObjectId, methodInvocation.MethodName, methodInvocation.Parameters.ToArray(), out result, out exception); - return new BrowserProcessResponse + return new MethodInvocationResult { + CallbackId = methodInvocation.CallbackId, Message = exception, Result = result, Success = success @@ -52,5 +56,14 @@ namespace CefSharp.Internals }); queue.Add(task); } + + private void OnMethodInvocationComplete(MethodInvocationResult e) + { + var handler = MethodInvocationComplete; + if (handler != null) + { + handler(this, new MethodInvocationCompleteArgs(e)); + } + } } }