diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index 64f3d5bd2..fa584b737 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -155,6 +155,7 @@ namespace CefSharp result = value->ExecuteFunction(nullptr, params); success = result.get() != nullptr; + response = CefProcessMessage::Create(kJavascriptCallbackResponse); //we need to do this here to be able to store the v8context if (success) { @@ -166,8 +167,6 @@ namespace CefSharp exception = value->GetException(); } } - - response = CefProcessMessage::Create(kJavascriptCallbackResponse); } if (response.get()) diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index 94f9dcf95..fb2b4f0e3 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -19,6 +19,7 @@ namespace CefSharp private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler { private: + //Simple v8context enter/exit to avoid code duplication struct V8ContextScope { V8ContextScope(CefRefPtr context) diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index 3c8727ae2..446216d82 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -227,6 +227,8 @@ + + @@ -267,6 +269,8 @@ + + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index 055c55c71..5bba52895 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -65,6 +65,12 @@ Source Files + + Source Files + + + Source Files + @@ -194,6 +200,12 @@ Header Files + Header Files + + + Header Files + + Header Files diff --git a/CefSharp.Core/Internals/JavascriptCallbackImpl.cpp b/CefSharp.Core/Internals/JavascriptCallbackImpl.cpp new file mode 100644 index 000000000..eaa8413a6 --- /dev/null +++ b/CefSharp.Core/Internals/JavascriptCallbackImpl.cpp @@ -0,0 +1,96 @@ +// 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. + +#include "Stdafx.h" +#include "JavascriptCallbackImpl.h" +#include "Messaging/Messages.h" +#include "Serialization/Primitives.h" +#include "Serialization/V8Serialization.h" + +namespace CefSharp +{ + namespace Internals + { + using namespace Messaging; + using namespace Serialization; + + JavascriptCallbackImpl::JavascriptCallbackImpl(JavascriptCallback^ callback, PendingTaskRepository^ pendingTasks, WeakReference^ browserWrapper) + :_callback(callback), _pendingTasks(pendingTasks) + { + _browserWrapper = browserWrapper; + } + + Task^ JavascriptCallbackImpl::ExecuteAsync(array^ parameters) + { + auto browser = GetBrowser(); + if (browser.get()) + { + auto doneCallback = _pendingTasks->CreatePendingTask(Nullable()); + auto callbackMessage = CreateCallMessage(doneCallback.Key, parameters); + browser->SendProcessMessage(CefProcessId::PID_RENDERER, callbackMessage); + + return doneCallback.Value->Task; + } + else + { + throw gcnew InvalidOperationException("Browser instance is null."); + } + } + + CefRefPtr JavascriptCallbackImpl::CreateCallMessage(int64 doneCallbackId, array^ parameters) + { + auto result = CefProcessMessage::Create(kJavascriptCallbackRequest); + auto browser = GetBrowser(); + auto argList = result->GetArgumentList(); + argList->SetInt(0, browser->GetIdentifier()); + SetInt64(_callback->Id, argList, 1); + SetInt64(doneCallbackId, argList, 2); + auto paramList = CefListValue::Create(); + for (int i = 0; i < parameters->Length; i++) + { + auto param = parameters[i]; + SerializeV8Object(param, paramList, i); + } + argList->SetList(3, paramList); + return result; + } + + CefRefPtr JavascriptCallbackImpl::CreateDestroyMessage() + { + auto result = CefProcessMessage::Create(kJavascriptCallbackDestroyRequest); + auto argList = result->GetArgumentList(); + SetInt64(_callback->Id, argList, 0); + return result; + } + + CefRefPtr JavascriptCallbackImpl::GetBrowser() + { + CefRefPtr result; + if (_browserWrapper->IsAlive) + { + auto wrapper = static_cast(_browserWrapper->Target); + result = wrapper == nullptr ? nullptr : wrapper->Browser.get(); + } + return result; + } + + void JavascriptCallbackImpl::DisposedGuard() + { + if (_disposed) + { + throw gcnew ObjectDisposedException("JavascriptCallbackImpl"); + } + } + + JavascriptCallbackImpl::!JavascriptCallbackImpl() + { + auto browser = GetBrowser(); + if (browser.get()) + { + browser->SendProcessMessage(CefProcessId::PID_RENDERER, CreateDestroyMessage()); + } + _disposed = true; + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/JavascriptCallbackImpl.h b/CefSharp.Core/Internals/JavascriptCallbackImpl.h new file mode 100644 index 000000000..9221d7c0e --- /dev/null +++ b/CefSharp.Core/Internals/JavascriptCallbackImpl.h @@ -0,0 +1,40 @@ +// 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. + +#pragma once + +#include "include/cef_base.h" +#include "include/cef_app.h" + +#include "CefSharpBrowserWrapper.h" + +using namespace System; +using namespace System::Threading::Tasks; + +namespace CefSharp +{ + namespace Internals + { + ref class JavascriptCallbackImpl : public IJavascriptCallback + { + private: + WeakReference^ _browserWrapper; + JavascriptCallback^ _callback; + PendingTaskRepository^ _pendingTasks; + bool _disposed; + + CefRefPtr CreateCallMessage(int64 doneCallbackId, array^ parameters); + CefRefPtr CreateDestroyMessage(); + CefRefPtr GetBrowser(); + void DisposedGuard(); + public: + JavascriptCallbackImpl(JavascriptCallback^ callback, PendingTaskRepository^ pendingTasks, WeakReference^ browser); + + virtual Task^ ExecuteAsync(array^ parameters) override; + + ~JavascriptCallbackImpl() { this->!JavascriptCallbackImpl(); } + !JavascriptCallbackImpl(); + }; + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/JavascriptCallbackImplFactory.cpp b/CefSharp.Core/Internals/JavascriptCallbackImplFactory.cpp new file mode 100644 index 000000000..42dcc120b --- /dev/null +++ b/CefSharp.Core/Internals/JavascriptCallbackImplFactory.cpp @@ -0,0 +1,29 @@ +// 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. + +#include "Stdafx.h" +#include "CefSharpBrowserWrapper.h" +#include "JavascriptCallbackImpl.h" +#include "JavascriptCallbackImplFactory.h" + +namespace CefSharp +{ + namespace Internals + { + JavascriptCallbackImplFactory::JavascriptCallbackImplFactory(PendingTaskRepository^ pendingTasks) + :_pendingTasks(pendingTasks) + { + } + + void JavascriptCallbackImplFactory::BrowserWrapper::set(CefSharpBrowserWrapper^ browserWrapper) + { + _browserWrapper = gcnew WeakReference(browserWrapper); + } + + IJavascriptCallback^ JavascriptCallbackImplFactory::Create(JavascriptCallback^ callback) + { + return gcnew JavascriptCallbackImpl(callback, _pendingTasks, _browserWrapper); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/JavascriptCallbackImplFactory.h b/CefSharp.Core/Internals/JavascriptCallbackImplFactory.h new file mode 100644 index 000000000..eb7917082 --- /dev/null +++ b/CefSharp.Core/Internals/JavascriptCallbackImplFactory.h @@ -0,0 +1,31 @@ +// 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. + +#pragma once + +using namespace System; + +namespace CefSharp +{ + namespace Internals + { + ref class CefSharpBrowserWrapper; + + ref class JavascriptCallbackImplFactory : public IJavascriptCallbackFactory + { + private: + WeakReference^ _browserWrapper; + PendingTaskRepository^ _pendingTasks; + public: + JavascriptCallbackImplFactory(PendingTaskRepository^ pendingTasks); + + property CefSharpBrowserWrapper^ BrowserWrapper + { + void set(CefSharpBrowserWrapper^ browserWrapper); + }; + + virtual IJavascriptCallback^ Create(JavascriptCallback^ callback); + }; + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp index 8975ceb09..874b35001 100644 --- a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp @@ -163,7 +163,7 @@ namespace CefSharp throw gcnew NotSupportedException("Complex types cannot be serialized to Cef lists"); } - seen->Pop(obj); + seen->Pop(); } DateTime ConvertCefTimeToDateTime(CefTime time) @@ -185,6 +185,8 @@ namespace CefSharp template Object^ DeserializeV8Object(CefRefPtr list, int index, IJavascriptCallbackFactory^ javascriptCallbackFactory); template Object^ DeserializeV8Object(CefRefPtr list, CefString index, IJavascriptCallbackFactory^ javascriptCallbackFactory); + template void SerializeV8Object(Object^ obj, CefRefPtr list, int index); + template void SerializeV8Object(Object^ obj, CefRefPtr list, CefString index); } } } \ No newline at end of file