From bf33f0bc7ce07a2b68996153e17884e824fd6aab Mon Sep 17 00:00:00 2001 From: David Serfozo Date: Sat, 27 Jun 2015 16:06:47 +0200 Subject: [PATCH 01/24] V8 serialization utils added --- .../CefSharp.BrowserSubprocess.Core.vcxproj | 4 + ...arp.BrowserSubprocess.Core.vcxproj.filters | 12 ++ .../Serialization/V8Serialization.cpp | 85 ++++++++++ .../Serialization/V8Serialization.h | 25 +++ CefSharp.Core/CefSharp.Core.vcxproj | 4 + CefSharp.Core/CefSharp.Core.vcxproj.filters | 12 ++ .../Internals/Serialization/Primitives.cpp | 155 ++++++++++++++++++ .../Internals/Serialization/Primitives.h | 37 +++++ .../Serialization/V8Serialization.cpp | 93 +++++++++++ .../Internals/Serialization/V8Serialization.h | 24 +++ CefSharp/CefSharp.csproj | 1 + .../Internals/IJavascriptCallbackFactory.cs | 14 ++ 12 files changed, 466 insertions(+) create mode 100644 CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp create mode 100644 CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h create mode 100644 CefSharp.Core/Internals/Serialization/Primitives.cpp create mode 100644 CefSharp.Core/Internals/Serialization/Primitives.h create mode 100644 CefSharp.Core/Internals/Serialization/V8Serialization.cpp create mode 100644 CefSharp.Core/Internals/Serialization/V8Serialization.h create mode 100644 CefSharp/Internals/IJavascriptCallbackFactory.cs diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj index 2e2ce536a..3c11f2d64 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj @@ -155,6 +155,7 @@ + @@ -171,9 +172,11 @@ + + @@ -193,6 +196,7 @@ Create + diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters index 5550541d5..5c744c4a0 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters @@ -68,6 +68,12 @@ Header Files + + Header Files + + + Header Files + @@ -112,6 +118,12 @@ Source Files + + Source Files + + + Source Files + diff --git a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp new file mode 100644 index 000000000..90d86ac6b --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp @@ -0,0 +1,85 @@ +// 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. + +#include "Stdafx.h" +#include "V8Serialization.h" +#include "JavascriptCallbackRegistry.h" +#include "../CefSharp.Core/Internals/Serialization/Primitives.h" + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + template + void SerializeV8Object(CefRefPtr obj, CefRefPtr list, TIndex index, JavascriptCallbackRegistry^ callbackRegistry) + { + if (obj->IsNull() || obj->IsUndefined()) + { + list->SetNull(index); + } + else if (obj->IsBool()) + list->SetBool(index, obj->GetBoolValue()); + else if (obj->IsInt()) + list->SetInt(index, obj->GetIntValue()); + else if (obj->IsDouble()) + list->SetDouble(index, obj->GetDoubleValue()); + else if (obj->IsString()) + list->SetString(index, obj->GetStringValue()); + else if (obj->IsDate()) + SetCefTime(obj->GetDateValue(), list, index); + else if (obj->IsArray()) + { + int arrLength = obj->GetArrayLength(); + std::vector keys; + if (arrLength > 0 && obj->GetKeys(keys)) + { + auto array = CefListValue::Create(); + for (int i = 0; i < arrLength; i++) + { + SerializeV8Object(obj->GetValue(keys[i]), array, i, callbackRegistry); + } + + list->SetList(index, array); + } + else + { + list->SetNull(index); + } + } + else if (obj->IsFunction()) + { + auto context = CefV8Context::GetCurrentContext(); + auto jsCallback = callbackRegistry->Register(context, obj); + SetJsCallback(jsCallback, list, index); + } + else if (obj->IsObject()) + { + std::vector keys; + if (obj->GetKeys(keys) && keys.size() > 0) + { + auto result = CefDictionaryValue::Create(); + for (int i = 0; i < keys.size(); i++) + { + auto p_keyStr = StringUtils::ToClr(keys[i].ToString()); + if ((obj->HasValue(keys[i])) && (!p_keyStr->StartsWith("__"))) + { + SerializeV8Object(obj->GetValue(keys[i]), result, keys[i], callbackRegistry); + } + } + list->SetDictionary(index, result); + } + } + else + { + list->SetNull(index); + } + } + + template void SerializeV8Object(CefRefPtr value, CefRefPtr list, int index, JavascriptCallbackRegistry^ callbackRegistry); + template void SerializeV8Object(CefRefPtr value, CefRefPtr list, CefString index, JavascriptCallbackRegistry^ callbackRegistry); + } + } +} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h new file mode 100644 index 000000000..d5b7a69a3 --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h @@ -0,0 +1,25 @@ +// 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. + +#pragma once + +#include "include/cef_v8.h" + +namespace CefSharp +{ + namespace Internals + { + ref class JavascriptCallbackRegistry; + + namespace Serialization + { + //Functions to sserialize data to be sent to the browser process. + + //Serializes a V8 structure into a given index of a CefListValue or CefDictionaryValue + //JavascriptCallbackRegistry should be passed to save V8Values with function types + template + void SerializeV8Object(CefRefPtr value, CefRefPtr list, TIndex index, JavascriptCallbackRegistry^ callbackRegistry); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index b19988af5..0471d257d 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -230,6 +230,8 @@ + + Create @@ -272,9 +274,11 @@ + + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index 42ee67e83..6c28533b6 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -59,6 +59,12 @@ Source Files + + Source Files + + + Source Files + @@ -181,6 +187,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/CefSharp.Core/Internals/Serialization/Primitives.cpp b/CefSharp.Core/Internals/Serialization/Primitives.cpp new file mode 100644 index 000000000..ce2cc91ef --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/Primitives.cpp @@ -0,0 +1,155 @@ +// 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 "Primitives.h" + +#include "include/cef_app.h" + +using namespace std; + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + enum class PrimitiveType : unsigned char + { + INT64, + CEFTIME, + JSCALLBACK + }; + + template + bool IsType(PrimitiveType type, CefRefPtr list, TIndex index) + { + auto result = list->GetType(index) == VTYPE_BINARY; + if (result) + { + underlying_type::type typeRead; + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&typeRead, sizeof(underlying_type::type), 0); + result = typeRead == static_cast::type>(type); + } + return result; + } + + template + void SetInt64(const int64 &value, CefRefPtr list, TIndex index) + { + unsigned char mem[1 + sizeof(int64)]; + mem[0] = static_cast(PrimitiveType::INT64); + memcpy(reinterpret_cast(mem + 1), &value, sizeof(int64)); + + auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem)); + list->SetBinary(index, binaryValue); + } + + template + int64 GetInt64(CefRefPtr list, TIndex index) + { + int64 result; + + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&result, sizeof(int64), 1); + + return result; + } + + template + bool IsInt64(CefRefPtr list, TIndex index) + { + return IsType(PrimitiveType::INT64, list, index); + } + + template + void SetCefTime(const CefTime &value, CefRefPtr list, TIndex index) + { + auto doubleT = value.GetDoubleT(); + unsigned char mem[1 + sizeof(double)]; + mem[0] = static_cast(PrimitiveType::CEFTIME); + memcpy(reinterpret_cast(mem + 1), &doubleT, sizeof(double)); + + auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem)); + list->SetBinary(index, binaryValue); + } + + template + CefTime GetCefTime(CefRefPtr list, TIndex index) + { + double doubleT; + + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&doubleT, sizeof(double), 1); + + return CefTime(doubleT); + } + + template + bool IsCefTime(CefRefPtr list, TIndex index) + { + return IsType(PrimitiveType::CEFTIME, list, index); + } + template + void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, TIndex index) + { + auto id = value->Id; + auto browserId = value->BrowserId; + + unsigned char mem[1 + sizeof(int) + sizeof(int64)]; + mem[0] = static_cast(PrimitiveType::JSCALLBACK); + memcpy(reinterpret_cast(mem + 1), &browserId, sizeof(int)); + memcpy(reinterpret_cast(mem + 1 + sizeof(int)), &id, sizeof(int64)); + + auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem)); + list->SetBinary(index, binaryValue); + } + + template + JavascriptCallback^ GetJsCallback(CefRefPtr list, TIndex index) + { + auto result = gcnew JavascriptCallback(); + int64 id; + int browserId; + + auto binaryValue = list->GetBinary(index); + binaryValue->GetData(&browserId, sizeof(int), 1); + binaryValue->GetData(&id, sizeof(int64), 1 + sizeof(int)); + + result->Id = id; + result->BrowserId = browserId; + + return result; + } + + template + bool IsJsCallback(CefRefPtr list, TIndex index) + { + return IsType(PrimitiveType::JSCALLBACK, list, index); + } + + template void SetInt64(const int64 &value, CefRefPtr list, int index); + template void SetInt64(const int64 &value, CefRefPtr list, CefString index); + template int64 GetInt64(CefRefPtr list, int index); + template int64 GetInt64(CefRefPtr list, CefString index); + template bool IsInt64(CefRefPtr list, int index); + template bool IsInt64(CefRefPtr list, CefString index); + + template void SetCefTime(const CefTime &value, CefRefPtr list, int index); + template void SetCefTime(const CefTime &value, CefRefPtr list, CefString index); + template CefTime GetCefTime(CefRefPtr list, int index); + template CefTime GetCefTime(CefRefPtr list, CefString index); + template bool IsCefTime(CefRefPtr list, int index); + template bool IsCefTime(CefRefPtr list, CefString index); + + template void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, int index); + template void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, CefString index); + template JavascriptCallback^ GetJsCallback(CefRefPtr list, int index); + template JavascriptCallback^ GetJsCallback(CefRefPtr list, CefString index); + template bool IsJsCallback(CefRefPtr list, int index); + template bool IsJsCallback(CefRefPtr list, CefString index); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/Primitives.h b/CefSharp.Core/Internals/Serialization/Primitives.h new file mode 100644 index 000000000..e451ae6ff --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/Primitives.h @@ -0,0 +1,37 @@ +// 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 + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + //Functions to serialize/deserialize specific types into CefBinaryValue + + template + void SetInt64(const int64 &value, CefRefPtr list, TIndex index); + template + int64 GetInt64(CefRefPtr list, TIndex index); + template + bool IsInt64(CefRefPtr list, TIndex index); + + template + void SetCefTime(const CefTime &value, CefRefPtr list, TIndex index); + template + CefTime GetCefTime(CefRefPtr list, TIndex index); + template + bool IsCefTime(CefRefPtr list, TIndex index); + + template + void SetJsCallback(JavascriptCallback^ value, CefRefPtr list, TIndex index); + template + JavascriptCallback^ GetJsCallback(CefRefPtr list, TIndex index); + template + bool IsJsCallback(CefRefPtr list, TIndex index); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp new file mode 100644 index 000000000..d0e91f587 --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp @@ -0,0 +1,93 @@ +// 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 "V8Serialization.h" +#include "Primitives.h" + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + template + Object^ DeserializeV8Object(CefRefPtr list, TIndex index, IJavascriptCallbackFactory^ javascriptCallbackFactory) + { + Object^ result = nullptr; + auto type = list->GetType(index); + + if (type == VTYPE_BOOL) + { + result = list->GetBool(index); + } + else if (type == VTYPE_INT) + { + result = list->GetInt(index); + } + else if (IsInt64(list, index)) + { + result = GetInt64(list, index); + } + else if (IsCefTime(list, index)) + { + auto cefTime = GetCefTime(list, index); + result = ConvertCefTimeToDateTime(cefTime); + } + else if (IsJsCallback(list, index) && javascriptCallbackFactory != nullptr) + { + auto jsCallbackDto = GetJsCallback(list, index); + result = javascriptCallbackFactory->Create(jsCallbackDto); + } + else if (type == VTYPE_DOUBLE) + { + result = list->GetDouble(index); + } + else if (type == VTYPE_STRING) + { + result = StringUtils::ToClr(list->GetString(index)); + } + else if (type == VTYPE_LIST) + { + auto subList = list->GetList(index); + auto array = gcnew List(subList->GetSize()); + for (auto i = 0; i < subList->GetSize(); i++) + { + array->Add(DeserializeV8Object(subList, i, javascriptCallbackFactory)); + } + result = array; + } + else if (type == VTYPE_DICTIONARY) + { + auto dict = gcnew Dictionary(); + auto subDict = list->GetDictionary(index); + std::vector keys; + subDict->GetKeys(keys); + + for (auto i = 0; i < keys.size(); i++) + { + dict->Add(StringUtils::ToClr(keys[i]), DeserializeV8Object(subDict, keys[i], javascriptCallbackFactory)); + } + + result = dict; + } + + return result; + } + + DateTime ConvertCefTimeToDateTime(CefTime time) + { + auto epoch = time.GetDoubleT(); + if (epoch == 0) + { + return DateTime::MinValue; + } + return DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime(); + } + + template Object^ DeserializeV8Object(CefRefPtr list, int index, IJavascriptCallbackFactory^ javascriptCallbackFactory); + template Object^ DeserializeV8Object(CefRefPtr list, CefString index, IJavascriptCallbackFactory^ javascriptCallbackFactory); + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.h b/CefSharp.Core/Internals/Serialization/V8Serialization.h new file mode 100644 index 000000000..714846891 --- /dev/null +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.h @@ -0,0 +1,24 @@ +// 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 + +namespace CefSharp +{ + namespace Internals + { + namespace Serialization + { + //Functions to deserialize data returned from the render process. + + //Deserializes data into Object from a given index of a CefListValue or CefDictionaryValue + //IJavascriptCallbackFactory implementation should be passed to allow creation of valid javascript callbacks + template + Object^ DeserializeV8Object(CefRefPtr list, TIndex index, IJavascriptCallbackFactory^ javascriptCallbackFactory); + + //Converts CefTime to DateTime + DateTime ConvertCefTimeToDateTime(CefTime time); + } + } +} \ No newline at end of file diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 1864dcf7b..b0b6140a4 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -112,6 +112,7 @@ Code + diff --git a/CefSharp/Internals/IJavascriptCallbackFactory.cs b/CefSharp/Internals/IJavascriptCallbackFactory.cs new file mode 100644 index 000000000..f9804e31d --- /dev/null +++ b/CefSharp/Internals/IJavascriptCallbackFactory.cs @@ -0,0 +1,14 @@ +// 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. + +namespace CefSharp.Internals +{ + /// + /// Interface to convert a JavascriptCallback dto to a callable implementation. + /// + public interface IJavascriptCallbackFactory + { + IJavascriptCallback Create(JavascriptCallback callback); + } +} From 7588259964b7454f0378b3b33231d6df577ce070 Mon Sep 17 00:00:00 2001 From: David Serfozo Date: Sat, 27 Jun 2015 17:01:51 +0200 Subject: [PATCH 02/24] added process message handlers and code to call them on both sides --- .../CefAppUnmanagedWrapper.cpp | 17 ++++++++++ .../CefAppUnmanagedWrapper.h | 8 +++++ CefSharp.Core/CefSharp.Core.vcxproj | 2 ++ CefSharp.Core/CefSharp.Core.vcxproj.filters | 6 ++++ CefSharp.Core/Internals/ClientAdapter.cpp | 18 +++++++++++ CefSharp.Core/Internals/ClientAdapter.h | 9 ++++++ CefSharp.Core/Internals/Messaging/Messages.h | 23 +++++++++++++ .../Messaging/ProcessMessageDelegate.h | 32 +++++++++++++++++++ 8 files changed, 115 insertions(+) create mode 100644 CefSharp.Core/Internals/Messaging/Messages.h create mode 100644 CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index ac8deba8d..cf88a23de 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -82,4 +82,21 @@ namespace CefSharp return wrapper; }; + + bool CefAppUnmanagedWrapper::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) + { + bool handled = false; + + for (ProcessMessageDelegateSet::iterator it = _processMessageDelegates.begin(); it != _processMessageDelegates.end() && !handled; it++) + { + handled = (*it)->OnProcessMessageReceived(browser, source_process, message); + } + + return handled; + }; + + void CefAppUnmanagedWrapper::AddProcessMessageDelegate(CefRefPtr processMessageDelegate) + { + _processMessageDelegates.insert(processMessageDelegate); + }; } \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index 2d4c179e4..e83c91a84 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -10,15 +10,20 @@ #include "include/cef_base.h" #include "CefBrowserWrapper.h" +#include "../CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h" using namespace System::Collections::Generic; namespace CefSharp { + using namespace Internals::Messaging; + // This class is the native subprocess level CEF object wrapper. private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler { private: + ProcessMessageDelegateSet _processMessageDelegates; + gcroot^> _onBrowserCreated; gcroot^> _onBrowserDestroyed; gcroot^> _browserWrappers; @@ -44,6 +49,9 @@ namespace CefSharp virtual DECL void OnBrowserDestroyed(CefRefPtr browser) OVERRIDE; virtual DECL void OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) OVERRIDE; virtual DECL void OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) OVERRIDE; + virtual DECL bool OnProcessMessageReceived(CefRefPtr< CefBrowser > browser, CefProcessId source_process, CefRefPtr< CefProcessMessage > message) OVERRIDE; + + void AddProcessMessageDelegate(CefRefPtr processMessageDelegate); IMPLEMENT_REFCOUNTING(CefAppUnmanagedWrapper); }; diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index 0471d257d..03df538bb 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -267,6 +267,7 @@ + @@ -279,6 +280,7 @@ + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index 6c28533b6..ff4ad11d0 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -193,6 +193,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index c2b846b5d..379c27a21 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -798,5 +798,23 @@ namespace CefSharp handler->OnDownloadUpdated(browserWrapper, TypeConversion::FromNative(download_item), callbackWrapper); } } + + + bool ClientAdapter::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) + { + bool handled = false; + + for (ProcessMessageDelegateSet::iterator it = _processMessageDelegates.begin(); it != _processMessageDelegates.end() && !handled; it++) + { + handled = (*it)->OnProcessMessageReceived(browser, source_process, message); + } + + return handled; + } + + void ClientAdapter::AddProcessMessageDelegate(CefRefPtr processMessageDelegate) + { + _processMessageDelegates.insert(processMessageDelegate); + } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index bda148803..d9bbdd11a 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -10,6 +10,7 @@ #include "include/cef_client.h" #include "include/cef_render_process_handler.h" #include "include/internal/cef_types.h" +#include "Internals/Messaging/ProcessMessageDelegate.h" using namespace System; @@ -17,6 +18,8 @@ namespace CefSharp { namespace Internals { + using namespace Messaging; + private class ClientAdapter : public CefClient, public CefLifeSpanHandler, public CefLoadHandler, @@ -35,6 +38,7 @@ namespace CefSharp gcroot _browserControl; HWND _browserHwnd; CefRefPtr _cefBrowser; + ProcessMessageDelegateSet _processMessageDelegates; gcroot^> _popupBrowsers; gcroot _tooltip; gcroot _browserAdapter; @@ -82,6 +86,8 @@ namespace CefSharp virtual DECL CefRefPtr GetDialogHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetDragHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetGeolocationHandler() OVERRIDE { return this; } + virtual DECL bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) OVERRIDE; + // CefLifeSpanHandler virtual DECL bool OnBeforePopup(CefRefPtr browser, CefRefPtr frame, @@ -158,6 +164,9 @@ namespace CefSharp virtual void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item, CefRefPtr callback) OVERRIDE; + void AddProcessMessageDelegate(CefRefPtr processMessageDelegate); + + IMPLEMENT_REFCOUNTING(ClientAdapter); }; } diff --git a/CefSharp.Core/Internals/Messaging/Messages.h b/CefSharp.Core/Internals/Messaging/Messages.h new file mode 100644 index 000000000..05a5e80fe --- /dev/null +++ b/CefSharp.Core/Internals/Messaging/Messages.h @@ -0,0 +1,23 @@ +// 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" + +namespace CefSharp +{ + namespace Internals + { + namespace Messaging + { + //contains process message names for all handled messages + + //Message containing a script to be evaluated + const CefString kEvaluateJavascript = "EvaluateJavascript"; + //Message containing the result for a given evaluation + const CefString kEvaluateJavascriptDone = "EvaluateJavascriptDone"; + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h b/CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h new file mode 100644 index 000000000..5e0fb347e --- /dev/null +++ b/CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h @@ -0,0 +1,32 @@ +// 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 "include\cef_base.h" +#include "include\cef_app.h" + +namespace CefSharp +{ + namespace Internals + { + namespace Messaging + { + //Specific process message handler classes can inherit from this one + class ProcessMessageDelegate : public virtual CefBase + { + public: + //Will be called for uhandled received process messages. + //Returns false if message wasn't handled, true if it was + virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) = 0; + + IMPLEMENT_REFCOUNTING(ProcessMessageDelegate); + }; + + typedef std::set> ProcessMessageDelegateSet; + } + } +} \ No newline at end of file From cc8a07c21f71b23d8129f31d8debed1458b3322a Mon Sep 17 00:00:00 2001 From: David Serfozo Date: Sat, 27 Jun 2015 18:24:32 +0200 Subject: [PATCH 03/24] added evaluatescriptasync message handlers and rerouted evalscript to cef ipc --- .../CefAppUnmanagedWrapper.cpp | 7 +- .../CefAppUnmanagedWrapper.h | 8 ++ .../CefBrowserWrapper.cpp | 10 ++ .../CefBrowserWrapper.h | 9 ++ .../CefSharp.BrowserSubprocess.Core.vcxproj | 2 + ...arp.BrowserSubprocess.Core.vcxproj.filters | 6 + .../Messaging/EvaluateScriptDelegate.cpp | 112 ++++++++++++++++++ .../Messaging/EvaluateScriptDelegate.h | 34 ++++++ CefSharp.Core/CefSharp.Core.vcxproj | 2 + CefSharp.Core/CefSharp.Core.vcxproj.filters | 6 + CefSharp.Core/Internals/ClientAdapter.cpp | 10 ++ CefSharp.Core/Internals/ClientAdapter.h | 15 ++- .../Messaging/EvaluateScriptDoneDelegate.cpp | 90 ++++++++++++++ .../Messaging/EvaluateScriptDoneDelegate.h | 32 +++++ CefSharp.Core/ManagedCefBrowserAdapter.cpp | 18 +-- CefSharp/CefSharp.csproj | 1 + CefSharp/Internals/PendingTaskRepository.cs | 78 ++++++++++++ 17 files changed, 423 insertions(+), 17 deletions(-) create mode 100644 CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp create mode 100644 CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h create mode 100644 CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp create mode 100644 CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h create mode 100644 CefSharp/Internals/PendingTaskRepository.cs diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index cf88a23de..6598ba493 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -71,6 +71,11 @@ namespace CefSharp CefBrowserWrapper^ CefAppUnmanagedWrapper::FindBrowserWrapper(CefRefPtr browser, bool mustExist) { auto browserId = browser->GetIdentifier(); + return FindBrowserWrapper(browserId, mustExist); + }; + + CefBrowserWrapper^ CefAppUnmanagedWrapper::FindBrowserWrapper(int browserId, bool mustExist) + { CefBrowserWrapper^ wrapper = nullptr; _browserWrappers->TryGetValue(browserId, wrapper); @@ -81,7 +86,7 @@ namespace CefSharp } return wrapper; - }; + } bool CefAppUnmanagedWrapper::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) { diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index e83c91a84..c0c4100de 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -10,6 +10,7 @@ #include "include/cef_base.h" #include "CefBrowserWrapper.h" +#include "Messaging/EvaluateScriptDelegate.h" #include "../CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h" using namespace System::Collections::Generic; @@ -22,11 +23,15 @@ namespace CefSharp private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler { private: + friend EvaluateScriptDelegate; + ProcessMessageDelegateSet _processMessageDelegates; gcroot^> _onBrowserCreated; gcroot^> _onBrowserDestroyed; gcroot^> _browserWrappers; + + CefBrowserWrapper^ FindBrowserWrapper(int browserId, bool mustExist); CefBrowserWrapper^ FindBrowserWrapper(CefRefPtr browser, bool mustExist); public: @@ -35,6 +40,9 @@ namespace CefSharp _onBrowserCreated = onBrowserCreated; _onBrowserDestroyed = onBrowserDestoryed; _browserWrappers = gcnew Dictionary(); + + //Register evaluate script request handler + AddProcessMessageDelegate(new EvaluateScriptDelegate(this)); } ~CefAppUnmanagedWrapper() diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp index 7f415274f..04fb9316d 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp @@ -87,6 +87,16 @@ namespace CefSharp _callbackRegistry->Deregister(id); } + CefRefPtr CefBrowserWrapper::GetWrapped() + { + return _cefBrowser.get(); + } + + JavascriptCallbackRegistry^ CefBrowserWrapper::CallbackRegistry::get() + { + return _callbackRegistry; + } + CefBrowserWrapper::!CefBrowserWrapper() { _cefBrowser = nullptr; diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h index 7c5b85a9a..af95ad8b4 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h @@ -30,6 +30,15 @@ namespace CefSharp JavascriptCallbackRegistry^ _callbackRegistry; JavascriptRootObjectWrapper^ _javascriptRootObjectWrapper; + internal: + //used by EvaluateScriptDelegate + CefRefPtr GetWrapped(); + + property JavascriptCallbackRegistry^ CallbackRegistry + { + CefSharp::Internals::JavascriptCallbackRegistry^ get(); + } + public: CefBrowserWrapper(CefRefPtr cefBrowser); !CefBrowserWrapper(); diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj index 3c11f2d64..35bfc0f7d 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj @@ -162,6 +162,7 @@ + @@ -182,6 +183,7 @@ + diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters index 5c744c4a0..0958c2a4e 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters @@ -74,6 +74,9 @@ Header Files + + Header Files + @@ -124,6 +127,9 @@ Source Files + + Source Files + diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp new file mode 100644 index 000000000..95a0a9276 --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp @@ -0,0 +1,112 @@ +// 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. + +#include "stdafx.h" +#include "../CefSharp.Core/Internals/Messaging/Messages.h" +#include "../CefSharp.Core/Internals/Serialization/Primitives.h" + +#include "Serialization/V8Serialization.h" +#include "../CefAppUnmanagedWrapper.h" +#include "EvaluateScriptDelegate.h" + +namespace CefSharp +{ + namespace Internals + { + using namespace Serialization; + + namespace Messaging + { + EvaluateScriptDelegate::EvaluateScriptDelegate(CefRefPtr appUnmanagedWrapper) + :_appUnmanagedWrapper(appUnmanagedWrapper) + { + + } + + bool EvaluateScriptDelegate::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) + { + auto handled = false; + auto name = message->GetName(); + if (name == kEvaluateJavascript) + { + auto argList = message->GetArgumentList(); + auto browserId = argList->GetInt(0); + auto frameId = argList->GetInt(1); + auto callbackId = GetInt64(argList, 2); + auto script = argList->GetString(3); + + auto response = EvaluateScript(browserId, frameId, callbackId, script); + if (response.get()) + { + browser->SendProcessMessage(source_process, response); + } + + handled = true; + } + + return handled; + } + + CefRefPtr EvaluateScriptDelegate::EvaluateScript(int browserId, int frameId, int64 callbackId, CefString script) + { + CefRefPtr result; + auto browserWrapper = _appUnmanagedWrapper->FindBrowserWrapper(browserId, true); + auto browser = browserWrapper->GetWrapped(); + auto frame = browser->GetFrame(frameId); + if (browser.get() && frame.get()) + { + result = EvaluateScriptInFrame(frame, callbackId, script, browserWrapper->CallbackRegistry); + } + else + { + //TODO handle error + } + return result; + } + + CefRefPtr EvaluateScriptDelegate::EvaluateScriptInFrame(CefRefPtr frame, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry) + { + CefRefPtr result; + auto context = frame->GetV8Context(); + + if (context.get() && context->Enter()) + { + try + { + result = EvaluateScriptInContext(context, callbackId, script, callbackRegistry); + } + finally + { + context->Exit(); + } + } + + return result; + } + + CefRefPtr EvaluateScriptDelegate::EvaluateScriptInContext(CefRefPtr context, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry) + { + CefRefPtr result; + CefRefPtr exception; + auto success = context->Eval(script, result, exception); + auto response = CefProcessMessage::Create(kEvaluateJavascriptDone); + auto argList = response->GetArgumentList(); + + argList->SetBool(0, success); + SetInt64(callbackId, argList, 1); + if (success) + { + SerializeV8Object(result, argList, 2, callbackRegistry); + } + else + { + argList->SetString(2, exception->GetMessage()); + } + + return response; + } + + } + } +} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h new file mode 100644 index 000000000..fd3891951 --- /dev/null +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h @@ -0,0 +1,34 @@ +// 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. + +#pragma once + +#include "../CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h" + +namespace CefSharp +{ + class CefAppUnmanagedWrapper; + + namespace Internals + { + namespace Messaging + { + //This class handles incoming evaluate script messages and responses to them after fulfillment. + class EvaluateScriptDelegate : public ProcessMessageDelegate + { + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(EvaluateScriptDelegate); + + CefRefPtr _appUnmanagedWrapper; + + CefRefPtr EvaluateScript(int browserId, int frameId, int64 callbackId, CefString script); + CefRefPtr EvaluateScriptInFrame(CefRefPtr frame, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry); + CefRefPtr EvaluateScriptInContext(CefRefPtr context, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry); + public: + EvaluateScriptDelegate(CefRefPtr appUnmanagedWrapper); + virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) override; + }; + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index 03df538bb..d48771ed2 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -219,6 +219,7 @@ + @@ -245,6 +246,7 @@ + diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index ff4ad11d0..5348e7be7 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -65,6 +65,9 @@ Source Files + + Source Files + @@ -199,6 +202,9 @@ Header Files + + Header Files + diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 379c27a21..4e49c9e87 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -812,6 +812,16 @@ namespace CefSharp return handled; } + Task^ ClientAdapter::EvaluateScriptAsync(int browserId, int frameId, String^ script, Nullable timeout) + { + Task^ result = nullptr; + if (_cefBrowser.get()) + { + result = _evalScriptDoneDelegate->EvaluateScriptAsync(_cefBrowser, browserId, frameId, script, timeout); + } + return result; + } + void ClientAdapter::AddProcessMessageDelegate(CefRefPtr processMessageDelegate) { _processMessageDelegates.insert(processMessageDelegate); diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index d9bbdd11a..5b9f1667c 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -11,6 +11,7 @@ #include "include/cef_render_process_handler.h" #include "include/internal/cef_types.h" #include "Internals/Messaging/ProcessMessageDelegate.h" +#include "Internals/Messaging/EvaluateScriptDoneDelegate.h" using namespace System; @@ -38,10 +39,16 @@ namespace CefSharp gcroot _browserControl; HWND _browserHwnd; CefRefPtr _cefBrowser; + //eval script handler + CefRefPtr _evalScriptDoneDelegate; + ProcessMessageDelegateSet _processMessageDelegates; gcroot^> _popupBrowsers; gcroot _tooltip; gcroot _browserAdapter; + //contains in-progress eval script tasks + gcroot^> _pendingTaskRepository; + void ThrowUnknownPopupBrowser(String^ context) { @@ -54,8 +61,12 @@ namespace CefSharp ClientAdapter(IWebBrowserInternal^ browserControl, IBrowserAdapter^ browserAdapter) : _browserControl(browserControl), _popupBrowsers(gcnew Dictionary()), + _pendingTaskRepository(gcnew PendingTaskRepository()), _browserAdapter(browserAdapter) { + //create eval script message handler + _evalScriptDoneDelegate = new EvaluateScriptDoneDelegate(_pendingTaskRepository); + AddProcessMessageDelegate(_evalScriptDoneDelegate); } ~ClientAdapter() @@ -164,8 +175,10 @@ namespace CefSharp virtual void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item, CefRefPtr callback) OVERRIDE; - void AddProcessMessageDelegate(CefRefPtr processMessageDelegate); + //sends out an eval script request to the render process + Task^ EvaluateScriptAsync(int browserId, int frameId, String^ script, Nullable timeout); + void AddProcessMessageDelegate(CefRefPtr processMessageDelegate); IMPLEMENT_REFCOUNTING(ClientAdapter); }; diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp new file mode 100644 index 000000000..35f25bf90 --- /dev/null +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp @@ -0,0 +1,90 @@ +// 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 "Messages.h" +#include "../Serialization/Primitives.h" +#include "../Serialization/V8Serialization.h" +#include "EvaluateScriptDoneDelegate.h" + +namespace CefSharp +{ + namespace Internals + { + using namespace Serialization; + + namespace Messaging + { + EvaluateScriptDoneDelegate::EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks) + :_pendingTasks(pendingTasks) + { + + } + + Task^ EvaluateScriptDoneDelegate::EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int frameId, String^ script, Nullable timeout) + { + TaskCompletionSource^ completionSource = nullptr; + //create a new taskcompletionsource based on wether we do or do not have a timeout + auto callbackId = timeout.HasValue ? _pendingTasks->CreatePendingTaskWithTimeout(completionSource, timeout.Value) : + _pendingTasks->CreatePendingTask(completionSource); + + auto message = CefProcessMessage::Create(kEvaluateJavascript); + auto argList = message->GetArgumentList(); + argList->SetInt(0, browserId); + argList->SetInt(1, frameId); + SetInt64(callbackId, argList, 2); + argList->SetString(3, StringUtils::ToNative(script)); + + cefBrowser->SendProcessMessage(CefProcessId::PID_RENDERER, message); + + return completionSource->Task; + } + + bool EvaluateScriptDoneDelegate::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) + { + auto handled = false; + auto name = message->GetName(); + if (name == kEvaluateJavascriptDone) + { + auto argList = message->GetArgumentList(); + auto success = argList->GetBool(0); + auto callbackId = GetInt64(argList, 1); + + FinishTask(callbackId, success, argList); + + handled = true; + } + + return handled; + } + + void EvaluateScriptDoneDelegate::FinishTask(int64 callbackId, bool success, CefRefPtr message) + { + auto pendingTask = _pendingTasks->RemovePendingTask(callbackId); + if (pendingTask != nullptr) + { + pendingTask->SetResult(CreateResponse(success, message)); + } + } + + JavascriptResponse^ EvaluateScriptDoneDelegate::CreateResponse(bool success, CefRefPtr message) + { + auto result = gcnew JavascriptResponse(); + result->Success = success; + + if (success) + { + //pass null as IJavascriptCallbackFactory for now + result->Result = DeserializeV8Object(message, 2, nullptr); + } + else + { + result->Message = StringUtils::ToClr(message->GetString(2)); + } + + return result; + } + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h new file mode 100644 index 000000000..e9b3f2efc --- /dev/null +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h @@ -0,0 +1,32 @@ +// 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 "ProcessMessageDelegate.h" + +namespace CefSharp +{ + namespace Internals + { + namespace Messaging + { + //This class will handle result messages of eval script calls + class EvaluateScriptDoneDelegate : public ProcessMessageDelegate + { + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(EvaluateScriptDoneDelegate); + + gcroot^> _pendingTasks; + + void FinishTask(int64 callbackId, bool success, CefRefPtr message); + JavascriptResponse^ CreateResponse(bool success, CefRefPtr message); + public: + EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks); + Task^ EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int frameId, String^ script, Nullable timeout); + virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) override; + }; + } + } +} \ No newline at end of file diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index bcc744df2..0f0058f50 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -39,8 +39,6 @@ void ManagedCefBrowserAdapter::LoadUrl(String^ address) void ManagedCefBrowserAdapter::OnAfterBrowserCreated(int browserId) { - _wcfEnabled = Cef::WcfEnabled; - if (_wcfEnabled) { _browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browserId); @@ -291,12 +289,7 @@ Task^ ManagedCefBrowserAdapter::EvaluateScriptAsync(int bro throw gcnew ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32::MaxValue); } - if (!_wcfEnabled) - { - throw gcnew InvalidOperationException("To wait for javascript code set WcfEnabled true in CefSettings during initialization."); - } - - return _browserProcessServiceHost->EvaluateScriptAsync(browserId, frameId, script, timeout); + return _clientAdapter->EvaluateScriptAsync(browserId, frameId, script, timeout); } Task^ ManagedCefBrowserAdapter::EvaluateScriptAsync(String^ script, Nullable timeout) @@ -306,14 +299,9 @@ Task^ ManagedCefBrowserAdapter::EvaluateScriptAsync(String^ throw gcnew ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32::MaxValue); } - if (!_wcfEnabled) - { - throw gcnew InvalidOperationException("To wait for javascript code set WcfEnabled true in CefSettings during initialization."); - } - auto browser = _clientAdapter->GetCefBrowser(); - if (_browserProcessServiceHost == nullptr || browser == nullptr) + if (browser == nullptr) { return nullptr; } @@ -325,7 +313,7 @@ Task^ ManagedCefBrowserAdapter::EvaluateScriptAsync(String^ return nullptr; } - return _browserProcessServiceHost->EvaluateScriptAsync(browser->GetIdentifier(), frame->GetIdentifier(), script, timeout); + return _clientAdapter->EvaluateScriptAsync(browser->GetIdentifier(), frame->GetIdentifier(), script, timeout); } void ManagedCefBrowserAdapter::CreateBrowser(BrowserSettings^ browserSettings, IntPtr sourceHandle, String^ address) diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index b0b6140a4..a175ee06b 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -118,6 +118,7 @@ + diff --git a/CefSharp/Internals/PendingTaskRepository.cs b/CefSharp/Internals/PendingTaskRepository.cs new file mode 100644 index 000000000..a15be3233 --- /dev/null +++ b/CefSharp/Internals/PendingTaskRepository.cs @@ -0,0 +1,78 @@ +// 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.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; + +namespace CefSharp.Internals.Messaging +{ + /// + /// Class to store TaskCompletionSources indexed by a unique id. + /// + /// The type of the result produced by the tasks held. + public sealed class PendingTaskRepository + { + private readonly ConcurrentDictionary> pendingTasks = + new ConcurrentDictionary>(); + //should only be accessed by Interlocked.Increment + private long lastId = 0; + + /// + /// Creates a new pending task. + /// + /// The newly created + /// The unique id of the newly created pending task. + public long CreatePendingTask(out TaskCompletionSource completionSource) + { + completionSource = new TaskCompletionSource(); + return SaveCompletionSource(completionSource); + } + + /// + /// Creates a new pending task with a timeout. + /// + /// The maximum running time of the task. + /// The newly created + /// The unique id of the newly created pending task. + public long CreatePendingTaskWithTimeout(out TaskCompletionSource completionSource, TimeSpan timeout) + { + var taskCompletionSource = new TaskCompletionSource(); + completionSource = taskCompletionSource; + + var id = SaveCompletionSource(completionSource); + Timer timer = null; + timer = new Timer(state => + { + timer.Dispose(); + RemovePendingTask(id); + taskCompletionSource.TrySetCanceled(); + }, null, timeout, TimeSpan.FromMilliseconds(-1)); + + return id; + } + + /// + /// Gets and removed pending task by id. + /// + /// Unique id of the pending task. + /// + /// The associated with the given id. + /// + public TaskCompletionSource RemovePendingTask(long id) + { + TaskCompletionSource result; + pendingTasks.TryRemove(id, out result); + return result; + } + + private long SaveCompletionSource(TaskCompletionSource completionSource) + { + var id = Interlocked.Increment(ref lastId); + pendingTasks.TryAdd(id, completionSource); + return id; + } + } +} \ No newline at end of file From 762d94942a0fcd38760db0c327e50cd04e7dcdaf Mon Sep 17 00:00:00 2001 From: David Serfozo Date: Sat, 27 Jun 2015 18:42:56 +0200 Subject: [PATCH 04/24] added jscallback support for new evaluatescript implementation --- CefSharp.Core/Internals/ClientAdapter.cpp | 3 +++ CefSharp.Core/Internals/ClientAdapter.h | 10 ++++++++-- .../Messaging/EvaluateScriptDoneDelegate.cpp | 18 ++++++++++-------- .../Messaging/EvaluateScriptDoneDelegate.h | 7 ++++--- CefSharp.Core/ManagedCefBrowserAdapter.cpp | 12 ++++++++++++ CefSharp.Core/ManagedCefBrowserAdapter.h | 5 +++++ CefSharp/CefSharp.csproj | 1 + .../Internals/BrowserProcessServiceHost.cs | 2 ++ CefSharp/Internals/IBrowserAdapter.cs | 1 + .../Internals/JavascriptCallbackFactory.cs | 19 +++++++++++++++++++ .../Internals/JavascriptCallbackSurrogate.cs | 8 +++----- 11 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 CefSharp/Internals/JavascriptCallbackFactory.cs diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 4e49c9e87..770a24a55 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -100,6 +100,9 @@ namespace CefSharp if (static_cast(_browserAdapter) != nullptr) { _browserAdapter->OnAfterBrowserCreated(browser->GetIdentifier()); + //save callback factory for this browser + //it's only going to be present after browseradapter is initialized + _javascriptCallbackFactories->Add(browser->GetIdentifier(), _browserAdapter->JavascriptCallbackFactory); } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 5b9f1667c..123784fec 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -48,7 +48,8 @@ namespace CefSharp gcroot _browserAdapter; //contains in-progress eval script tasks gcroot^> _pendingTaskRepository; - + //contains js callback factories for each browser + gcroot^> _javascriptCallbackFactories; void ThrowUnknownPopupBrowser(String^ context) { @@ -62,16 +63,21 @@ namespace CefSharp _browserControl(browserControl), _popupBrowsers(gcnew Dictionary()), _pendingTaskRepository(gcnew PendingTaskRepository()), + _javascriptCallbackFactories(gcnew Dictionary()), _browserAdapter(browserAdapter) { //create eval script message handler - _evalScriptDoneDelegate = new EvaluateScriptDoneDelegate(_pendingTaskRepository); + _evalScriptDoneDelegate = new EvaluateScriptDoneDelegate(_pendingTaskRepository, _javascriptCallbackFactories); AddProcessMessageDelegate(_evalScriptDoneDelegate); } ~ClientAdapter() { CloseAllPopups(true); + + //this will dispose the repository and cancel all pending tasks + delete _pendingTaskRepository; + _browserControl = nullptr; _browserHwnd = nullptr; _cefBrowser = NULL; diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp index 35f25bf90..eb8f22fd3 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp @@ -16,8 +16,8 @@ namespace CefSharp namespace Messaging { - EvaluateScriptDoneDelegate::EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks) - :_pendingTasks(pendingTasks) + EvaluateScriptDoneDelegate::EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks, Dictionary^ callbackFactories) + :_pendingTasks(pendingTasks), _callbackFactories(callbackFactories) { } @@ -51,7 +51,10 @@ namespace CefSharp auto success = argList->GetBool(0); auto callbackId = GetInt64(argList, 1); - FinishTask(callbackId, success, argList); + IJavascriptCallbackFactory^ callbackFactory; + _callbackFactories->TryGetValue(browser->GetIdentifier(), callbackFactory); + + FinishTask(callbackId, success, argList, callbackFactory); handled = true; } @@ -59,24 +62,23 @@ namespace CefSharp return handled; } - void EvaluateScriptDoneDelegate::FinishTask(int64 callbackId, bool success, CefRefPtr message) + void EvaluateScriptDoneDelegate::FinishTask(int64 callbackId, bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory) { auto pendingTask = _pendingTasks->RemovePendingTask(callbackId); if (pendingTask != nullptr) { - pendingTask->SetResult(CreateResponse(success, message)); + pendingTask->SetResult(CreateResponse(success, message, callbackFactory)); } } - JavascriptResponse^ EvaluateScriptDoneDelegate::CreateResponse(bool success, CefRefPtr message) + JavascriptResponse^ EvaluateScriptDoneDelegate::CreateResponse(bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory) { auto result = gcnew JavascriptResponse(); result->Success = success; if (success) { - //pass null as IJavascriptCallbackFactory for now - result->Result = DeserializeV8Object(message, 2, nullptr); + result->Result = DeserializeV8Object(message, 2, callbackFactory); } else { diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h index e9b3f2efc..8db0acb62 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h @@ -18,12 +18,13 @@ namespace CefSharp private: DISALLOW_IMPLICIT_CONSTRUCTORS(EvaluateScriptDoneDelegate); + gcroot^> _callbackFactories; gcroot^> _pendingTasks; - void FinishTask(int64 callbackId, bool success, CefRefPtr message); - JavascriptResponse^ CreateResponse(bool success, CefRefPtr message); + void FinishTask(int64 callbackId, bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory); + JavascriptResponse^ CreateResponse(bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory); public: - EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks); + EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks, Dictionary^ callbackFactories); Task^ EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int frameId, String^ script, Nullable timeout); virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) override; }; diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index 0f0058f50..ef43c91f9 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -39,6 +39,8 @@ void ManagedCefBrowserAdapter::LoadUrl(String^ address) void ManagedCefBrowserAdapter::OnAfterBrowserCreated(int browserId) { + _wcfEnabled = Cef::WcfEnabled; + if (_wcfEnabled) { _browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browserId); @@ -559,3 +561,13 @@ IBrowser^ ManagedCefBrowserAdapter::GetBrowser() { return _browserWrapper; } + +IJavascriptCallbackFactory^ ManagedCefBrowserAdapter::JavascriptCallbackFactory::get() +{ + IJavascriptCallbackFactory^ result = nullptr; + if (_browserProcessServiceHost != nullptr) + { + result = _browserProcessServiceHost->JavascriptCallbackFactory; + } + return result; +} diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.h b/CefSharp.Core/ManagedCefBrowserAdapter.h index e662c741d..7eb9d8e1f 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.h +++ b/CefSharp.Core/ManagedCefBrowserAdapter.h @@ -165,5 +165,10 @@ namespace CefSharp /// /// Gets the current instance or null virtual IBrowser^ GetBrowser(); + + virtual property IJavascriptCallbackFactory^ JavascriptCallbackFactory + { + CefSharp::Internals::IJavascriptCallbackFactory^ get(); + } }; } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index a175ee06b..9c253140e 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -114,6 +114,7 @@ + diff --git a/CefSharp/Internals/BrowserProcessServiceHost.cs b/CefSharp/Internals/BrowserProcessServiceHost.cs index 23f57b7c9..9748fcc90 100644 --- a/CefSharp/Internals/BrowserProcessServiceHost.cs +++ b/CefSharp/Internals/BrowserProcessServiceHost.cs @@ -15,6 +15,7 @@ namespace CefSharp.Internals { private const long OneHundredAndTwentyEightMegaBytesInBytes = 128*1024*1024; + public IJavascriptCallbackFactory JavascriptCallbackFactory { get; private set; } public JavascriptObjectRepository JavascriptObjectRepository { get; private set; } private TaskCompletionSource operationContextTaskCompletionSource = new TaskCompletionSource(); @@ -22,6 +23,7 @@ namespace CefSharp.Internals : base(typeof(BrowserProcessService), new Uri[0]) { JavascriptObjectRepository = javascriptObjectRepository; + JavascriptCallbackFactory = new JavascriptCallbackFactory(new WeakReference(this)); var serviceName = RenderprocessClientFactory.GetServiceName(parentProcessId, browserId); diff --git a/CefSharp/Internals/IBrowserAdapter.cs b/CefSharp/Internals/IBrowserAdapter.cs index 312a96757..2c256a78f 100644 --- a/CefSharp/Internals/IBrowserAdapter.cs +++ b/CefSharp/Internals/IBrowserAdapter.cs @@ -13,6 +13,7 @@ namespace CefSharp.Internals /// public interface IBrowserAdapter { + IJavascriptCallbackFactory JavascriptCallbackFactory { get; } Task EvaluateScriptAsync(string script, TimeSpan? timeout); Task EvaluateScriptAsync(int browserId, Int64 frameId, string script, TimeSpan? timeout); void OnAfterBrowserCreated(int browserId); diff --git a/CefSharp/Internals/JavascriptCallbackFactory.cs b/CefSharp/Internals/JavascriptCallbackFactory.cs new file mode 100644 index 000000000..cf197be0e --- /dev/null +++ b/CefSharp/Internals/JavascriptCallbackFactory.cs @@ -0,0 +1,19 @@ +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/JavascriptCallbackSurrogate.cs b/CefSharp/Internals/JavascriptCallbackSurrogate.cs index ae2007ec3..29c72e773 100644 --- a/CefSharp/Internals/JavascriptCallbackSurrogate.cs +++ b/CefSharp/Internals/JavascriptCallbackSurrogate.cs @@ -10,13 +10,11 @@ using System.Runtime.Serialization; namespace CefSharp.Internals { - internal sealed class JavascriptCallbackSurrogate : IDataContractSurrogate + internal sealed class JavascriptCallbackSurrogate : JavascriptCallbackFactory, IDataContractSurrogate { - private readonly WeakReference browserProcessWeakReference; - public JavascriptCallbackSurrogate(WeakReference browserProcessWeakReference) + :base(browserProcessWeakReference) { - this.browserProcessWeakReference = browserProcessWeakReference; } public Type GetDataContractType(Type type) @@ -39,7 +37,7 @@ namespace CefSharp.Internals var dto = obj as JavascriptCallback; if (dto != null) { - result = new JavascriptCallbackProxy(dto.Id, dto.BrowserId, browserProcessWeakReference); + result = Create(dto); } return result; } From 93a61c14423f2349c0f7c32facb3d57a09c1095e Mon Sep 17 00:00:00 2001 From: David Serfozo Date: Sat, 27 Jun 2015 18:56:21 +0200 Subject: [PATCH 05/24] removed unneeded eval script related methods --- .../CefBrowserWrapper.cpp | 46 ------------- .../CefBrowserWrapper.h | 4 -- .../CefRenderProcess.cs | 66 ------------------- .../Internals/BrowserProcessServiceHost.cs | 14 ---- CefSharp/Internals/IRenderProcess.cs | 5 -- .../JavascriptCallbackEndpointBehavior.cs | 1 - 6 files changed, 136 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp index 04fb9316d..11f4172c3 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp @@ -31,52 +31,6 @@ namespace CefSharp } } - JavascriptResponse^ CefBrowserWrapper::EvaluateScriptInContext(CefRefPtr context, CefString script) - { - CefRefPtr result; - CefRefPtr exception; - JavascriptResponse^ response = gcnew JavascriptResponse(); - - response->Success = context->Eval(script, result, exception); - if (response->Success) - { - if (result->IsFunction()) - { - response->Result = _callbackRegistry->Register(context, result); - } - else - { - response->Result = TypeUtils::ConvertFromCef(result); - } - } - else if (exception.get()) - { - response->Message = StringUtils::ToClr(exception->GetMessage()); - } - - return response; - } - - JavascriptResponse^ CefBrowserWrapper::DoEvaluateScript(System::Int64 frameId, String^ script) - { - auto frame = _cefBrowser->GetFrame(frameId); - CefRefPtr context = frame->GetV8Context(); - - if (context.get() && context->Enter()) - { - try - { - return EvaluateScriptInContext(context, StringUtils::ToNative(script)); - } - finally - { - context->Exit(); - } - } - - return nullptr; - } - JavascriptResponse^ CefBrowserWrapper::DoCallback(System::Int64 callbackId, array^ parameters) { return _callbackRegistry->Execute(callbackId, parameters); diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h index af95ad8b4..832d7d090 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h @@ -62,10 +62,6 @@ namespace CefSharp // The WCF proxy to the parent process. property IBrowserProcess^ BrowserProcess; - JavascriptResponse^ EvaluateScriptInContext(CefRefPtr context, CefString script); - - virtual JavascriptResponse^ DoEvaluateScript(System::Int64 frameId, String^ script); - JavascriptResponse^ DoCallback(System::Int64 callbackId, array^ parameters); void DestroyJavascriptCallback(Int64 id); diff --git a/CefSharp.BrowserSubprocess/CefRenderProcess.cs b/CefSharp.BrowserSubprocess/CefRenderProcess.cs index 5232a6e96..839c1b965 100644 --- a/CefSharp.BrowserSubprocess/CefRenderProcess.cs +++ b/CefSharp.BrowserSubprocess/CefRenderProcess.cs @@ -113,72 +113,6 @@ namespace CefSharp.BrowserSubprocess browser.JavascriptRootObject = null; } - public Task EvaluateScriptAsync(int browserId, long frameId, string script, 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.DoEvaluateScript(frameId, script); - - return response; - } - catch (Exception ex) - { - return new JavascriptResponse - { - Success = false, - Message = ex.ToString() - }; - } - }, TaskCreationOptions.AttachedToParent); - - return timeout.HasValue ? task.WithTimeout(timeout.Value) : task; - } - - public IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state) - { - var tcs = new TaskCompletionSource(state); - var task = EvaluateScriptAsync(browserId, frameId, script, 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 EndEvaluateScriptAsync(IAsyncResult result) - { - return ((Task)result).Result; - } - public IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state) { var tcs = new TaskCompletionSource(state); diff --git a/CefSharp/Internals/BrowserProcessServiceHost.cs b/CefSharp/Internals/BrowserProcessServiceHost.cs index 9748fcc90..0e458760a 100644 --- a/CefSharp/Internals/BrowserProcessServiceHost.cs +++ b/CefSharp/Internals/BrowserProcessServiceHost.cs @@ -51,20 +51,6 @@ namespace CefSharp.Internals operationContextTaskCompletionSource.SetResult(operationContext); } - public Task EvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout) - { - var operationContextTask = operationContextTaskCompletionSource.Task; - // Use TaskScheduler.Default to ensure that the work occurs - // on a background thread. - return operationContextTask.ContinueWith(t => - { - var context = t.Result; - var renderProcess = context.GetCallbackChannel(); - var asyncResult = renderProcess.BeginEvaluateScriptAsync(browserId, frameId, script, timeout, null, null); - return Task.Factory.FromAsync(asyncResult, renderProcess.EndEvaluateScriptAsync); - }, TaskScheduler.Default).Unwrap(); - } - protected override void OnClose(TimeSpan timeout) { var task = operationContextTaskCompletionSource.Task; diff --git a/CefSharp/Internals/IRenderProcess.cs b/CefSharp/Internals/IRenderProcess.cs index 8b2c70a68..4b232314f 100644 --- a/CefSharp/Internals/IRenderProcess.cs +++ b/CefSharp/Internals/IRenderProcess.cs @@ -11,11 +11,6 @@ namespace CefSharp.Internals [ServiceContract] public interface IRenderProcess { - [OperationContract(AsyncPattern=true)] - IAsyncResult BeginEvaluateScriptAsync(int browserId, long frameId, string script, TimeSpan? timeout, AsyncCallback callback, object state); - - JavascriptResponse EndEvaluateScriptAsync(IAsyncResult result); - [OperationContract(AsyncPattern = true)] IAsyncResult BeginJavascriptCallbackAsync(int browserId, long callbackId, object[] parameters, TimeSpan? timeout, AsyncCallback callback, object state); diff --git a/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs b/CefSharp/Internals/JavascriptCallbackEndpointBehavior.cs index ad4a8be12..36a50624c 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.BeginEvaluateScriptAsync(0, 0, null, null, null, null)).Substring(5), ReflectionUtils.GetMethodName(p => p.BeginJavascriptCallbackAsync(0, 0, null, null, null, null)).Substring(5), }; From b9e3100994791b5a96934f0bc093e7e995fcbded Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 11:25:57 +1000 Subject: [PATCH 06/24] Simplify PendingTaskRepository TaskCompletionSource creation - use KeyValuePair --- .../Messaging/EvaluateScriptDoneDelegate.cpp | 10 ++-- CefSharp/Internals/PendingTaskRepository.cs | 48 +++++++------------ 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp index eb8f22fd3..5de9163fb 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp @@ -24,21 +24,19 @@ namespace CefSharp Task^ EvaluateScriptDoneDelegate::EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int frameId, String^ script, Nullable timeout) { - TaskCompletionSource^ completionSource = nullptr; - //create a new taskcompletionsource based on wether we do or do not have a timeout - auto callbackId = timeout.HasValue ? _pendingTasks->CreatePendingTaskWithTimeout(completionSource, timeout.Value) : - _pendingTasks->CreatePendingTask(completionSource); + //create a new taskcompletionsource + auto idAndComplectionSource = _pendingTasks->CreatePendingTask(timeout); auto message = CefProcessMessage::Create(kEvaluateJavascript); auto argList = message->GetArgumentList(); argList->SetInt(0, browserId); argList->SetInt(1, frameId); - SetInt64(callbackId, argList, 2); + SetInt64(idAndComplectionSource.Key, argList, 2); argList->SetString(3, StringUtils::ToNative(script)); cefBrowser->SendProcessMessage(CefProcessId::PID_RENDERER, message); - return completionSource->Task; + return idAndComplectionSource.Value->Task; } bool EvaluateScriptDoneDelegate::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) diff --git a/CefSharp/Internals/PendingTaskRepository.cs b/CefSharp/Internals/PendingTaskRepository.cs index a15be3233..5d07e7a6a 100644 --- a/CefSharp/Internals/PendingTaskRepository.cs +++ b/CefSharp/Internals/PendingTaskRepository.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -18,40 +19,32 @@ namespace CefSharp.Internals.Messaging private readonly ConcurrentDictionary> pendingTasks = new ConcurrentDictionary>(); //should only be accessed by Interlocked.Increment - private long lastId = 0; - - /// - /// Creates a new pending task. - /// - /// The newly created - /// The unique id of the newly created pending task. - public long CreatePendingTask(out TaskCompletionSource completionSource) - { - completionSource = new TaskCompletionSource(); - return SaveCompletionSource(completionSource); - } + private long lastId; /// /// Creates a new pending task with a timeout. /// /// The maximum running time of the task. - /// The newly created - /// The unique id of the newly created pending task. - public long CreatePendingTaskWithTimeout(out TaskCompletionSource completionSource, TimeSpan timeout) + /// The unique id of the newly created pending task and the newly created . + public KeyValuePair> CreatePendingTask(TimeSpan? timeout = null) { var taskCompletionSource = new TaskCompletionSource(); - completionSource = taskCompletionSource; - var id = SaveCompletionSource(completionSource); - Timer timer = null; - timer = new Timer(state => + var id = Interlocked.Increment(ref lastId); + pendingTasks.TryAdd(id, taskCompletionSource); + + if (timeout.HasValue) { - timer.Dispose(); - RemovePendingTask(id); - taskCompletionSource.TrySetCanceled(); - }, null, timeout, TimeSpan.FromMilliseconds(-1)); + Timer timer = null; + timer = new Timer(state => + { + timer.Dispose(); + RemovePendingTask(id); + taskCompletionSource.TrySetCanceled(); + }, null, timeout.Value, TimeSpan.FromMilliseconds(-1)); + } - return id; + return new KeyValuePair>(id, taskCompletionSource); } /// @@ -67,12 +60,5 @@ namespace CefSharp.Internals.Messaging pendingTasks.TryRemove(id, out result); return result; } - - private long SaveCompletionSource(TaskCompletionSource completionSource) - { - var id = Interlocked.Increment(ref lastId); - pendingTasks.TryAdd(id, completionSource); - return id; - } } } \ No newline at end of file From d188382d20989a812902ea0ac6613a6a5006bbd3 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 11:34:43 +1000 Subject: [PATCH 07/24] Move PendingTaskRepository into matching namespace (resharper cleanup) - there is no messaging folder --- CefSharp/Internals/PendingTaskRepository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CefSharp/Internals/PendingTaskRepository.cs b/CefSharp/Internals/PendingTaskRepository.cs index 5d07e7a6a..97eb769e2 100644 --- a/CefSharp/Internals/PendingTaskRepository.cs +++ b/CefSharp/Internals/PendingTaskRepository.cs @@ -8,7 +8,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace CefSharp.Internals.Messaging +namespace CefSharp.Internals { /// /// Class to store TaskCompletionSources indexed by a unique id. From 6d28f97a1423e6b0493044711b3292c9b6372cc4 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 11:36:29 +1000 Subject: [PATCH 08/24] Prefer using statements directly after inclues - they get a little lost if we put them further down --- .../Internals/Messaging/EvaluateScriptDoneDelegate.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp index 5de9163fb..67b47979d 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp @@ -7,13 +7,13 @@ #include "../Serialization/Primitives.h" #include "../Serialization/V8Serialization.h" #include "EvaluateScriptDoneDelegate.h" + +using namespace CefSharp::Internals::Serialization; namespace CefSharp { namespace Internals { - using namespace Serialization; - namespace Messaging { EvaluateScriptDoneDelegate::EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks, Dictionary^ callbackFactories) From 03a893bdac1f2b16f51714d8fe15e2980d043ecc Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 11:42:25 +1000 Subject: [PATCH 09/24] Inline some of the EvaluateScriptDoneDelegate methods - as they're not used in multiple places it's easier to debug this way --- .../Messaging/EvaluateScriptDoneDelegate.cpp | 44 +++++++------------ .../Messaging/EvaluateScriptDoneDelegate.h | 3 -- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp index 67b47979d..3a051f428 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp @@ -52,39 +52,29 @@ namespace CefSharp IJavascriptCallbackFactory^ callbackFactory; _callbackFactories->TryGetValue(browser->GetIdentifier(), callbackFactory); - FinishTask(callbackId, success, argList, callbackFactory); + auto pendingTask = _pendingTasks->RemovePendingTask(callbackId); + if (pendingTask != nullptr) + { + auto response = gcnew JavascriptResponse(); + response->Success = success; + + if (success) + { + response->Result = DeserializeV8Object(argList, 2, callbackFactory); + } + else + { + response->Message = StringUtils::ToClr(argList->GetString(2)); + } + + pendingTask->SetResult(response); + } handled = true; } return handled; } - - void EvaluateScriptDoneDelegate::FinishTask(int64 callbackId, bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory) - { - auto pendingTask = _pendingTasks->RemovePendingTask(callbackId); - if (pendingTask != nullptr) - { - pendingTask->SetResult(CreateResponse(success, message, callbackFactory)); - } - } - - JavascriptResponse^ EvaluateScriptDoneDelegate::CreateResponse(bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory) - { - auto result = gcnew JavascriptResponse(); - result->Success = success; - - if (success) - { - result->Result = DeserializeV8Object(message, 2, callbackFactory); - } - else - { - result->Message = StringUtils::ToClr(message->GetString(2)); - } - - return result; - } } } } \ No newline at end of file diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h index 8db0acb62..03a894645 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h @@ -20,9 +20,6 @@ namespace CefSharp gcroot^> _callbackFactories; gcroot^> _pendingTasks; - - void FinishTask(int64 callbackId, bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory); - JavascriptResponse^ CreateResponse(bool success, CefRefPtr message, IJavascriptCallbackFactory^ callbackFactory); public: EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks, Dictionary^ callbackFactories); Task^ EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int frameId, String^ script, Nullable timeout); From f8954dcffea3a47143d12d34b01c75bfeb6dfcdd Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 11:49:12 +1000 Subject: [PATCH 10/24] Rename messaging keys to include Request/Response suffix Would be nice to move them into a class so they're not just out there on their own - whilst it's valid c++, it's not very .net --- .../Messaging/EvaluateScriptDelegate.cpp | 4 ++-- .../Internals/Messaging/EvaluateScriptDoneDelegate.cpp | 4 ++-- CefSharp.Core/Internals/Messaging/Messages.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp index 95a0a9276..7ae2f10b0 100644 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp @@ -28,7 +28,7 @@ namespace CefSharp { auto handled = false; auto name = message->GetName(); - if (name == kEvaluateJavascript) + if (name == kEvaluateJavascriptRequest) { auto argList = message->GetArgumentList(); auto browserId = argList->GetInt(0); @@ -90,7 +90,7 @@ namespace CefSharp CefRefPtr result; CefRefPtr exception; auto success = context->Eval(script, result, exception); - auto response = CefProcessMessage::Create(kEvaluateJavascriptDone); + auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse); auto argList = response->GetArgumentList(); argList->SetBool(0, success); diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp index 3a051f428..6dff6a26d 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp @@ -27,7 +27,7 @@ namespace CefSharp //create a new taskcompletionsource auto idAndComplectionSource = _pendingTasks->CreatePendingTask(timeout); - auto message = CefProcessMessage::Create(kEvaluateJavascript); + auto message = CefProcessMessage::Create(kEvaluateJavascriptRequest); auto argList = message->GetArgumentList(); argList->SetInt(0, browserId); argList->SetInt(1, frameId); @@ -43,7 +43,7 @@ namespace CefSharp { auto handled = false; auto name = message->GetName(); - if (name == kEvaluateJavascriptDone) + if (name == kEvaluateJavascriptResponse) { auto argList = message->GetArgumentList(); auto success = argList->GetBool(0); diff --git a/CefSharp.Core/Internals/Messaging/Messages.h b/CefSharp.Core/Internals/Messaging/Messages.h index 05a5e80fe..dad7864dc 100644 --- a/CefSharp.Core/Internals/Messaging/Messages.h +++ b/CefSharp.Core/Internals/Messaging/Messages.h @@ -15,9 +15,9 @@ namespace CefSharp //contains process message names for all handled messages //Message containing a script to be evaluated - const CefString kEvaluateJavascript = "EvaluateJavascript"; + const CefString kEvaluateJavascriptRequest = "EvaluateJavascriptRequest"; //Message containing the result for a given evaluation - const CefString kEvaluateJavascriptDone = "EvaluateJavascriptDone"; + const CefString kEvaluateJavascriptResponse = "EvaluateJavascriptDoneResponse"; } } } \ No newline at end of file From 0a220f2f96c302f559abf55050f8fd999767eaeb Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 11:57:16 +1000 Subject: [PATCH 11/24] Inline most of the methods in EvaluateScriptDelegate - much easier to step through now (with the CefRefPtrs, it's a lot harder to step into a function than normal) --- .../Messaging/EvaluateScriptDelegate.cpp | 110 +++++++----------- .../Messaging/EvaluateScriptDelegate.h | 4 - 2 files changed, 44 insertions(+), 70 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp index 7ae2f10b0..fd7319fb9 100644 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp @@ -9,13 +9,13 @@ #include "Serialization/V8Serialization.h" #include "../CefAppUnmanagedWrapper.h" #include "EvaluateScriptDelegate.h" + +using namespace CefSharp::Internals::Serialization; namespace CefSharp { namespace Internals { - using namespace Serialization; - namespace Messaging { EvaluateScriptDelegate::EvaluateScriptDelegate(CefRefPtr appUnmanagedWrapper) @@ -24,7 +24,7 @@ namespace CefSharp } - bool EvaluateScriptDelegate::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) + bool EvaluateScriptDelegate::OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) { auto handled = false; auto name = message->GetName(); @@ -36,10 +36,48 @@ namespace CefSharp auto callbackId = GetInt64(argList, 2); auto script = argList->GetString(3); - auto response = EvaluateScript(browserId, frameId, callbackId, script); - if (response.get()) + auto browserWrapper = _appUnmanagedWrapper->FindBrowserWrapper(browserId, true); + auto browser = browserWrapper->GetWrapped(); + auto frame = browser->GetFrame(frameId); + if (browser.get() && frame.get()) { - browser->SendProcessMessage(source_process, response); + auto context = frame->GetV8Context(); + + if (context.get() && context->Enter()) + { + try + { + CefRefPtr result; + CefRefPtr exception; + auto success = context->Eval(script, result, exception); + auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse); + auto argList = response->GetArgumentList(); + + argList->SetBool(0, success); + SetInt64(callbackId, argList, 1); + if (success) + { + SerializeV8Object(result, argList, 2, browserWrapper->CallbackRegistry); + } + else + { + argList->SetString(2, exception->GetMessage()); + } + + if (response.get()) + { + browser->SendProcessMessage(sourceProcessId, response); + } + } + finally + { + context->Exit(); + } + } + } + else + { + //TODO handle error } handled = true; @@ -47,66 +85,6 @@ namespace CefSharp return handled; } - - CefRefPtr EvaluateScriptDelegate::EvaluateScript(int browserId, int frameId, int64 callbackId, CefString script) - { - CefRefPtr result; - auto browserWrapper = _appUnmanagedWrapper->FindBrowserWrapper(browserId, true); - auto browser = browserWrapper->GetWrapped(); - auto frame = browser->GetFrame(frameId); - if (browser.get() && frame.get()) - { - result = EvaluateScriptInFrame(frame, callbackId, script, browserWrapper->CallbackRegistry); - } - else - { - //TODO handle error - } - return result; - } - - CefRefPtr EvaluateScriptDelegate::EvaluateScriptInFrame(CefRefPtr frame, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry) - { - CefRefPtr result; - auto context = frame->GetV8Context(); - - if (context.get() && context->Enter()) - { - try - { - result = EvaluateScriptInContext(context, callbackId, script, callbackRegistry); - } - finally - { - context->Exit(); - } - } - - return result; - } - - CefRefPtr EvaluateScriptDelegate::EvaluateScriptInContext(CefRefPtr context, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry) - { - CefRefPtr result; - CefRefPtr exception; - auto success = context->Eval(script, result, exception); - auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse); - auto argList = response->GetArgumentList(); - - argList->SetBool(0, success); - SetInt64(callbackId, argList, 1); - if (success) - { - SerializeV8Object(result, argList, 2, callbackRegistry); - } - else - { - argList->SetString(2, exception->GetMessage()); - } - - return response; - } - } } } \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h index fd3891951..4b59f5dbf 100644 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h @@ -21,10 +21,6 @@ namespace CefSharp DISALLOW_IMPLICIT_CONSTRUCTORS(EvaluateScriptDelegate); CefRefPtr _appUnmanagedWrapper; - - CefRefPtr EvaluateScript(int browserId, int frameId, int64 callbackId, CefString script); - CefRefPtr EvaluateScriptInFrame(CefRefPtr frame, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry); - CefRefPtr EvaluateScriptInContext(CefRefPtr context, int64 callbackId, CefString script, JavascriptCallbackRegistry^ callbackRegistry); public: EvaluateScriptDelegate(CefRefPtr appUnmanagedWrapper); virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) override; From d21808b5823bdf53fc969019969f700573a8eb69 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 11:58:42 +1000 Subject: [PATCH 12/24] Rename variable to be more .net like --- .../Messaging/EvaluateScriptDelegate.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h index 4b59f5dbf..264d9859c 100644 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h @@ -23,7 +23,7 @@ namespace CefSharp CefRefPtr _appUnmanagedWrapper; public: EvaluateScriptDelegate(CefRefPtr appUnmanagedWrapper); - virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) override; + virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) override; }; } } From af39be37bb6ad41f260faed8718d54d1ccd02762 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 12:00:33 +1000 Subject: [PATCH 13/24] Remove FindBrowserWrapper overload - not really required --- .../CefAppUnmanagedWrapper.cpp | 12 +++--------- .../CefAppUnmanagedWrapper.h | 1 - 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index 6598ba493..553b521f7 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -32,7 +32,7 @@ namespace CefSharp void CefAppUnmanagedWrapper::OnBrowserDestroyed(CefRefPtr browser) { - auto wrapper = FindBrowserWrapper(browser, false); + auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), false); if (wrapper != nullptr) { @@ -44,7 +44,7 @@ namespace CefSharp void CefAppUnmanagedWrapper::OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) { - auto wrapper = FindBrowserWrapper(browser, true); + auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), true); if (wrapper->JavascriptRootObject != nullptr) { @@ -59,7 +59,7 @@ namespace CefSharp void CefAppUnmanagedWrapper::OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) { - auto wrapper = FindBrowserWrapper(browser, true); + auto wrapper = FindBrowserWrapper(browser->GetIdentifier(), true); if (wrapper->JavascriptRootObjectWrapper != nullptr) { @@ -68,12 +68,6 @@ namespace CefSharp } }; - CefBrowserWrapper^ CefAppUnmanagedWrapper::FindBrowserWrapper(CefRefPtr browser, bool mustExist) - { - auto browserId = browser->GetIdentifier(); - return FindBrowserWrapper(browserId, mustExist); - }; - CefBrowserWrapper^ CefAppUnmanagedWrapper::FindBrowserWrapper(int browserId, bool mustExist) { CefBrowserWrapper^ wrapper = nullptr; diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index c0c4100de..932427d8a 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -32,7 +32,6 @@ namespace CefSharp gcroot^> _browserWrappers; CefBrowserWrapper^ FindBrowserWrapper(int browserId, bool mustExist); - CefBrowserWrapper^ FindBrowserWrapper(CefRefPtr browser, bool mustExist); public: CefAppUnmanagedWrapper(Action^ onBrowserCreated, Action^ onBrowserDestoryed) From a40590405a127ac201efc2310a290b600b7f312a Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 12:01:41 +1000 Subject: [PATCH 14/24] Remove AddProcessMessageDelegate method from CefAppUnmanagedWrapper - not required --- CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp | 5 ----- CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h | 4 +--- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index 553b521f7..ea70c9bb3 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -93,9 +93,4 @@ namespace CefSharp return handled; }; - - void CefAppUnmanagedWrapper::AddProcessMessageDelegate(CefRefPtr processMessageDelegate) - { - _processMessageDelegates.insert(processMessageDelegate); - }; } \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index 932427d8a..a6c8ef90f 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -41,7 +41,7 @@ namespace CefSharp _browserWrappers = gcnew Dictionary(); //Register evaluate script request handler - AddProcessMessageDelegate(new EvaluateScriptDelegate(this)); + _processMessageDelegates.insert(new EvaluateScriptDelegate(this)); } ~CefAppUnmanagedWrapper() @@ -58,8 +58,6 @@ namespace CefSharp virtual DECL void OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) OVERRIDE; virtual DECL bool OnProcessMessageReceived(CefRefPtr< CefBrowser > browser, CefProcessId source_process, CefRefPtr< CefProcessMessage > message) OVERRIDE; - void AddProcessMessageDelegate(CefRefPtr processMessageDelegate); - IMPLEMENT_REFCOUNTING(CefAppUnmanagedWrapper); }; } From ccbc97ffc4e418830ea43c24957d91fa8c8e58d0 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 12:03:01 +1000 Subject: [PATCH 15/24] Minor code formatting --- CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp | 4 ++-- CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index ea70c9bb3..dae6fa26c 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -82,13 +82,13 @@ namespace CefSharp return wrapper; } - bool CefAppUnmanagedWrapper::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) + bool CefAppUnmanagedWrapper::OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) { bool handled = false; for (ProcessMessageDelegateSet::iterator it = _processMessageDelegates.begin(); it != _processMessageDelegates.end() && !handled; it++) { - handled = (*it)->OnProcessMessageReceived(browser, source_process, message); + handled = (*it)->OnProcessMessageReceived(browser, sourceProcessId, message); } return handled; diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index a6c8ef90f..bfeae1825 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -56,7 +56,7 @@ namespace CefSharp virtual DECL void OnBrowserDestroyed(CefRefPtr browser) OVERRIDE; virtual DECL void OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) OVERRIDE; virtual DECL void OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) OVERRIDE; - virtual DECL bool OnProcessMessageReceived(CefRefPtr< CefBrowser > browser, CefProcessId source_process, CefRefPtr< CefProcessMessage > message) OVERRIDE; + virtual DECL bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) OVERRIDE; IMPLEMENT_REFCOUNTING(CefAppUnmanagedWrapper); }; From 53221506f207479e8c622e8213bba28745762b5e Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 12:33:42 +1000 Subject: [PATCH 16/24] Remove redundant AddProcessMessageDelegate method from ClientAdapter --- CefSharp.Core/Internals/ClientAdapter.cpp | 5 ----- CefSharp.Core/Internals/ClientAdapter.h | 5 ++--- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 770a24a55..5ef661ca6 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -824,10 +824,5 @@ namespace CefSharp } return result; } - - void ClientAdapter::AddProcessMessageDelegate(CefRefPtr processMessageDelegate) - { - _processMessageDelegates.insert(processMessageDelegate); - } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 123784fec..cf7496c3c 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -68,7 +68,8 @@ namespace CefSharp { //create eval script message handler _evalScriptDoneDelegate = new EvaluateScriptDoneDelegate(_pendingTaskRepository, _javascriptCallbackFactories); - AddProcessMessageDelegate(_evalScriptDoneDelegate); + + _processMessageDelegates.insert(_evalScriptDoneDelegate); } ~ClientAdapter() @@ -184,8 +185,6 @@ namespace CefSharp //sends out an eval script request to the render process Task^ EvaluateScriptAsync(int browserId, int frameId, String^ script, Nullable timeout); - void AddProcessMessageDelegate(CefRefPtr processMessageDelegate); - IMPLEMENT_REFCOUNTING(ClientAdapter); }; } From 833db43a1d02a79d0dd7c0500603991b8ffdd78d Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 12:38:54 +1000 Subject: [PATCH 17/24] FrameId is actually a 64bit int --- .../Messaging/EvaluateScriptDelegate.cpp | 2 +- CefSharp.Core/Internals/ClientAdapter.cpp | 2 +- CefSharp.Core/Internals/ClientAdapter.h | 2 +- .../Internals/Messaging/EvaluateScriptDoneDelegate.cpp | 4 ++-- .../Internals/Messaging/EvaluateScriptDoneDelegate.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp index fd7319fb9..5221ab00b 100644 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp @@ -32,7 +32,7 @@ namespace CefSharp { auto argList = message->GetArgumentList(); auto browserId = argList->GetInt(0); - auto frameId = argList->GetInt(1); + auto frameId = GetInt64(argList, 1); auto callbackId = GetInt64(argList, 2); auto script = argList->GetString(3); diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 5ef661ca6..f71cf7f41 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -815,7 +815,7 @@ namespace CefSharp return handled; } - Task^ ClientAdapter::EvaluateScriptAsync(int browserId, int frameId, String^ script, Nullable timeout) + Task^ ClientAdapter::EvaluateScriptAsync(int browserId, int64 frameId, String^ script, Nullable timeout) { Task^ result = nullptr; if (_cefBrowser.get()) diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index cf7496c3c..710fb3c5d 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -183,7 +183,7 @@ namespace CefSharp CefRefPtr callback) OVERRIDE; //sends out an eval script request to the render process - Task^ EvaluateScriptAsync(int browserId, int frameId, String^ script, Nullable timeout); + Task^ EvaluateScriptAsync(int browserId, int64 frameId, String^ script, Nullable timeout); IMPLEMENT_REFCOUNTING(ClientAdapter); }; diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp index 6dff6a26d..5d029b08e 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp @@ -22,7 +22,7 @@ namespace CefSharp } - Task^ EvaluateScriptDoneDelegate::EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int frameId, String^ script, Nullable timeout) + Task^ EvaluateScriptDoneDelegate::EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int64 frameId, String^ script, Nullable timeout) { //create a new taskcompletionsource auto idAndComplectionSource = _pendingTasks->CreatePendingTask(timeout); @@ -30,7 +30,7 @@ namespace CefSharp auto message = CefProcessMessage::Create(kEvaluateJavascriptRequest); auto argList = message->GetArgumentList(); argList->SetInt(0, browserId); - argList->SetInt(1, frameId); + SetInt64(frameId, argList, 1); SetInt64(idAndComplectionSource.Key, argList, 2); argList->SetString(3, StringUtils::ToNative(script)); diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h index 03a894645..25e17bcb1 100644 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h +++ b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h @@ -22,7 +22,7 @@ namespace CefSharp gcroot^> _pendingTasks; public: EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks, Dictionary^ callbackFactories); - Task^ EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int frameId, String^ script, Nullable timeout); + Task^ EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int64 frameId, String^ script, Nullable timeout); virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) override; }; } From 819dfd54f9cb1864ab7e860a3b516dfdf0fdfa86 Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 26 Jun 2015 11:19:51 +1000 Subject: [PATCH 18/24] Move WcfEnabled option into CefSharpSettings class Reduce code duplication - move EvaluateScriptAsync in WebBrowserExtensions --- CefSharp.Core/Cef.h | 13 ------ CefSharp.Core/CefSettings.h | 8 ---- CefSharp.Core/Internals/CefSharpApp.h | 2 +- CefSharp.Core/ManagedCefBrowserAdapter.cpp | 40 +------------------ CefSharp.Core/ManagedCefBrowserAdapter.h | 5 +-- CefSharp.Example/CefExample.cs | 2 +- CefSharp.OffScreen/ChromiumWebBrowser.cs | 5 --- CefSharp.WinForms/ChromiumWebBrowser.cs | 10 ----- .../Views/BrowserTabView.xaml.cs | 2 +- CefSharp.Wpf/ChromiumWebBrowser.cs | 10 ----- CefSharp/CefSharp.csproj | 1 + CefSharp/CefSharpSettings.cs | 21 ++++++++++ CefSharp/IWebBrowser.cs | 10 ----- CefSharp/Internals/IBrowserAdapter.cs | 2 - CefSharp/WebBrowserExtensions.cs | 20 ++++++++++ 15 files changed, 48 insertions(+), 103 deletions(-) create mode 100644 CefSharp/CefSharpSettings.cs diff --git a/CefSharp.Core/Cef.h b/CefSharp.Core/Cef.h index 63c24de9d..676e1e0bc 100644 --- a/CefSharp.Core/Cef.h +++ b/CefSharp.Core/Cef.h @@ -33,7 +33,6 @@ namespace CefSharp private: static Object^ _sync; - static bool _wcfEnabled = false; static bool _initialized = false; static HashSet^ _disposables; @@ -75,14 +74,6 @@ namespace CefSharp _disposables->Remove(item); } - static property bool WcfEnabled - { - bool get() - { - return _wcfEnabled; - } - } - /// Gets a value that indicates whether CefSharp is initialized. /// true if CefSharp is initialized; otherwise, false. static property bool IsInitialized @@ -191,10 +182,6 @@ namespace CefSharp } _initialized = success; - if (success) - { - _wcfEnabled = cefSettings->WcfEnabled; - } if (_initialized && shutdownOnProcessExit) { diff --git a/CefSharp.Core/CefSettings.h b/CefSharp.Core/CefSettings.h index 8b6471ad1..d3e889864 100644 --- a/CefSharp.Core/CefSettings.h +++ b/CefSharp.Core/CefSettings.h @@ -30,19 +30,11 @@ namespace CefSharp BrowserSubprocessPath = "CefSharp.BrowserSubprocess.exe"; _cefCustomSchemes = gcnew List(); _cefCommandLineArgs = gcnew Dictionary(); - WcfEnabled = true; } !CefSettings() { delete _cefSettings; } ~CefSettings() { delete _cefSettings; } - /// - /// WCF is used by JavascriptBinding and EvaluateScriptAsync. - /// Disabling effectively disables both of these features. - /// Defaults to true - /// - virtual property bool WcfEnabled; - virtual property IEnumerable^ CefCustomSchemes { IEnumerable^ get() { return _cefCustomSchemes; } diff --git a/CefSharp.Core/Internals/CefSharpApp.h b/CefSharp.Core/Internals/CefSharpApp.h index e023272f2..59b8e791e 100644 --- a/CefSharp.Core/Internals/CefSharpApp.h +++ b/CefSharp.Core/Internals/CefSharpApp.h @@ -45,7 +45,7 @@ namespace CefSharp virtual void OnBeforeChildProcessLaunch(CefRefPtr command_line) OVERRIDE { - if (_cefSettings->WcfEnabled) + if (CefSharpSettings::WcfEnabled) { command_line->AppendArgument(StringUtils::ToNative(CefSharpArguments::WcfEnabledArgument)); } diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index ef43c91f9..246f03375 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -39,9 +39,7 @@ void ManagedCefBrowserAdapter::LoadUrl(String^ address) void ManagedCefBrowserAdapter::OnAfterBrowserCreated(int browserId) { - _wcfEnabled = Cef::WcfEnabled; - - if (_wcfEnabled) + if (CefSharpSettings::WcfEnabled) { _browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browserId); //NOTE: Attempt to solve timing issue where browser is opened and rapidly disposed. In some cases a call to Open throws @@ -284,40 +282,6 @@ void ManagedCefBrowserAdapter::OnMouseWheel(int x, int y, int deltaX, int deltaY } } -Task^ ManagedCefBrowserAdapter::EvaluateScriptAsync(int browserId, Int64 frameId, String^ script, Nullable timeout) -{ - if (timeout.HasValue && timeout.Value.TotalMilliseconds > UInt32::MaxValue) - { - throw gcnew ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32::MaxValue); - } - - return _clientAdapter->EvaluateScriptAsync(browserId, frameId, script, timeout); -} - -Task^ ManagedCefBrowserAdapter::EvaluateScriptAsync(String^ script, Nullable timeout) -{ - if (timeout.HasValue && timeout.Value.TotalMilliseconds > UInt32::MaxValue) - { - throw gcnew ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32::MaxValue); - } - - auto browser = _clientAdapter->GetCefBrowser(); - - if (browser == nullptr) - { - return nullptr; - } - - auto frame = browser->GetMainFrame(); - - if (frame == nullptr) - { - return nullptr; - } - - return _clientAdapter->EvaluateScriptAsync(browser->GetIdentifier(), frame->GetIdentifier(), script, timeout); -} - void ManagedCefBrowserAdapter::CreateBrowser(BrowserSettings^ browserSettings, IntPtr sourceHandle, String^ address) { HWND hwnd = static_cast(sourceHandle.ToPointer()); @@ -371,7 +335,7 @@ void ManagedCefBrowserAdapter::NotifyScreenInfoChanged() void ManagedCefBrowserAdapter::RegisterJsObject(String^ name, Object^ object, bool lowerCaseJavascriptNames) { - if (!_wcfEnabled) + if (!CefSharpSettings::WcfEnabled) { throw gcnew InvalidOperationException("To enable synchronous JS bindings set WcfEnabled true in CefSettings during initialization."); } diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.h b/CefSharp.Core/ManagedCefBrowserAdapter.h index 7eb9d8e1f..494de58aa 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.h +++ b/CefSharp.Core/ManagedCefBrowserAdapter.h @@ -34,7 +34,6 @@ namespace CefSharp JavascriptObjectRepository^ _javaScriptObjectRepository; IBrowser^ _browserWrapper; bool _isDisposed; - bool _wcfEnabled; private: // Private keyboard functions: @@ -79,7 +78,7 @@ namespace CefSharp delete _browserWrapper; _browserWrapper = nullptr; - if (_wcfEnabled) + if (CefSharpSettings::WcfEnabled) { _browserProcessServiceHost->Close(); _browserProcessServiceHost = nullptr; @@ -108,8 +107,6 @@ namespace CefSharp void OnMouseMove(int x, int y, bool mouseLeave, CefEventFlags modifiers); void OnMouseButton(int x, int y, int mouseButtonType, bool mouseUp, int clickCount, CefEventFlags modifiers); void OnMouseWheel(int x, int y, int deltaX, int deltaY); - virtual Task^ EvaluateScriptAsync(int browserId, Int64 frameId, String^ script, Nullable timeout); - virtual Task^ EvaluateScriptAsync(String^ script, Nullable timeout); void Resize(int width, int height); void NotifyMoveOrResizeStarted(); void NotifyScreenInfoChanged(); diff --git a/CefSharp.Example/CefExample.cs b/CefSharp.Example/CefExample.cs index 6a8c7fc96..a8e4fa914 100644 --- a/CefSharp.Example/CefExample.cs +++ b/CefSharp.Example/CefExample.cs @@ -29,7 +29,7 @@ namespace CefSharp.Example var settings = new CefSettings(); settings.RemoteDebuggingPort = 8088; //This will disable WCF (Effectively disabling JavascriptBinding and EvaluateScriptAsync) - //settings.WcfEnabled = false; + //CefSharpSettings.WcfEnabled = false; //The location where cache data will be stored on disk. If empty an in-memory cache will be used for some features and a temporary disk cache for others. //HTML5 databases such as localStorage will only persist across sessions if a cache path is specified. settings.CachePath = "cache"; diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index d326e08b8..5bfd68ff0 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -228,11 +228,6 @@ namespace CefSharp.OffScreen return completionSource.Task; } - public Task EvaluateScriptAsync(string script, TimeSpan? timeout = null) - { - return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout); - } - public void Load(string url) { Address = url; diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index a4dc05225..b9d2a41f2 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -155,16 +155,6 @@ namespace CefSharp.WinForms managedCefBrowserAdapter.RegisterJsObject(name, objectToBind, camelCaseJavascriptNames); } - public Task EvaluateScriptAsync(string script) - { - return EvaluateScriptAsync(script, timeout: null); - } - - public Task EvaluateScriptAsync(string script, TimeSpan? timeout) - { - return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout); - } - public void SendMouseWheelEvent(int x, int y, int deltaX, int deltaY) { managedCefBrowserAdapter.OnMouseWheel(x, y, deltaX, deltaY); diff --git a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs index fed3bfbfc..0f18a4ea3 100644 --- a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs +++ b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs @@ -15,7 +15,7 @@ namespace CefSharp.Wpf.Example.Views InitializeComponent(); browser.RequestHandler = new RequestHandler(); - if (Cef.WcfEnabled) + if (CefSharpSettings.WcfEnabled) { browser.RegisterJsObject("bound", new BoundObject()); } diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index c8da90ab7..3e0e8c041 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -1292,16 +1292,6 @@ namespace CefSharp.Wpf managedCefBrowserAdapter.RegisterJsObject(name, objectToBind, camelCaseJavascriptNames); } - public Task EvaluateScriptAsync(string script) - { - return EvaluateScriptAsync(script, timeout: null); - } - - public Task EvaluateScriptAsync(string script, TimeSpan? timeout) - { - return managedCefBrowserAdapter.EvaluateScriptAsync(script, timeout); - } - /// /// Raises Rendering event /// diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 9c253140e..ee33d38b5 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -92,6 +92,7 @@ + diff --git a/CefSharp/CefSharpSettings.cs b/CefSharp/CefSharpSettings.cs new file mode 100644 index 000000000..22d7d7f76 --- /dev/null +++ b/CefSharp/CefSharpSettings.cs @@ -0,0 +1,21 @@ +// 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. + +namespace CefSharp +{ + public static class CefSharpSettings + { + static CefSharpSettings() + { + WcfEnabled = true; + } + + /// + /// WCF is used by JavascriptBinding and EvaluateScriptAsync. + /// Disabling effectively disables both of these features. + /// Defaults to true + /// + public static bool WcfEnabled { get; set; } + } +} diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index a4353011a..b753373bc 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -3,7 +3,6 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; -using System.Threading.Tasks; namespace CefSharp { @@ -60,15 +59,6 @@ namespace CefSharp /// camel case the javascript names of properties/methods, defaults to true void RegisterJsObject(string name, object objectToBind, bool camelCaseJavascriptNames = true); - /// - /// Execute some Javascript code in the context of this WebBrowser, and return the result of the evaluation - /// in an Async fashion - /// - /// The Javascript code that should be executed. - /// The timeout after which the Javascript code execution should be aborted. - /// A Task that can be awaited to perform the script execution - Task EvaluateScriptAsync(string script, TimeSpan? timeout = null); - /// /// Implement and assign to handle dialog events. /// diff --git a/CefSharp/Internals/IBrowserAdapter.cs b/CefSharp/Internals/IBrowserAdapter.cs index 2c256a78f..c9c651d0e 100644 --- a/CefSharp/Internals/IBrowserAdapter.cs +++ b/CefSharp/Internals/IBrowserAdapter.cs @@ -14,8 +14,6 @@ namespace CefSharp.Internals public interface IBrowserAdapter { IJavascriptCallbackFactory JavascriptCallbackFactory { get; } - Task EvaluateScriptAsync(string script, TimeSpan? timeout); - Task EvaluateScriptAsync(int browserId, Int64 frameId, string script, TimeSpan? timeout); void OnAfterBrowserCreated(int browserId); IBrowser GetBrowser(); } diff --git a/CefSharp/WebBrowserExtensions.cs b/CefSharp/WebBrowserExtensions.cs index f61825b44..ebc25c17b 100644 --- a/CefSharp/WebBrowserExtensions.cs +++ b/CefSharp/WebBrowserExtensions.cs @@ -538,6 +538,26 @@ namespace CefSharp cefBrowser.AddWordToDictionary(word); } + public static Task EvaluateScriptAsync(this IWebBrowser browser, string script, TimeSpan? timeout = null) + { + if (timeout.HasValue && timeout.Value.TotalMilliseconds > UInt32.MaxValue) + { + throw new ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32.MaxValue); + } + + if (!CefSharpSettings.WcfEnabled) + { + throw new InvalidOperationException("To wait for javascript code set WcfEnabled true in CefSettings during initialization."); + } + + using (var frame = browser.GetMainFrame()) + { + ThrowExceptionIfFrameNull(frame); + + return frame.EvaluateScriptAsync(script, timeout); + } + } + private static void ThrowExceptionIfFrameNull(IFrame frame) { if (frame == null) From 2129c1907ef215e36d32a420cbc8f4273d0a0b55 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 14:03:10 +1000 Subject: [PATCH 19/24] Remove IBrowserAdapter from Frame and Browser wrappers - now directly cast CefClient to ClientAdapter so can execute js --- CefSharp.Core/Internals/CefFrameWrapper.cpp | 15 ++++---- CefSharp.Core/Internals/CefFrameWrapper.h | 6 ++-- .../Internals/CefSharpBrowserWrapper.cpp | 8 ++--- .../Internals/CefSharpBrowserWrapper.h | 6 ++-- CefSharp.Core/Internals/ClientAdapter.cpp | 34 +++++++++---------- CefSharp.Core/ManagedCefBrowserAdapter.cpp | 10 +++--- CefSharp.Core/SchemeHandlerFactoryWrapper.h | 4 +-- CefSharp/WebBrowserExtensions.cs | 5 --- 8 files changed, 40 insertions(+), 48 deletions(-) diff --git a/CefSharp.Core/Internals/CefFrameWrapper.cpp b/CefSharp.Core/Internals/CefFrameWrapper.cpp index 9a523ff08..2e59f9834 100644 --- a/CefSharp.Core/Internals/CefFrameWrapper.cpp +++ b/CefSharp.Core/Internals/CefFrameWrapper.cpp @@ -175,11 +175,12 @@ void CefFrameWrapper::ExecuteJavaScriptAsync(String^ code, String^ scriptUrl, in Task^ CefFrameWrapper::EvaluateScriptAsync(String^ script, Nullable timeout) { ThrowIfDisposed(); - if (_browserAdapter == nullptr) - { - throw gcnew InvalidOperationException(gcnew String(L"IBrowserAdapter is NULL, EvaluateScriptAsync MUST NOT be called from the render process!")); - } - return _browserAdapter->EvaluateScriptAsync(_frame->GetBrowser()->GetIdentifier(), _frame->GetIdentifier(), script, timeout); + + auto host = _frame->GetBrowser()->GetHost(); + + auto client = static_cast(host->GetClient().get()); + + return client->EvaluateScriptAsync(_frame->GetBrowser()->GetIdentifier(), _frame->GetIdentifier(), script, timeout); } /// @@ -242,7 +243,7 @@ IFrame^ CefFrameWrapper::Parent::get() auto parent = _frame->GetParent(); if (parent != nullptr && _parentFrame != nullptr) { - _parentFrame = gcnew CefFrameWrapper(parent, _browserAdapter); + _parentFrame = gcnew CefFrameWrapper(parent); } else if (parent == nullptr) { @@ -286,7 +287,7 @@ IBrowser^ CefFrameWrapper::Browser::get() return _owningBrowser; } - _owningBrowser = gcnew CefSharpBrowserWrapper(_frame->GetBrowser(), _browserAdapter); + _owningBrowser = gcnew CefSharpBrowserWrapper(_frame->GetBrowser()); return _owningBrowser; } diff --git a/CefSharp.Core/Internals/CefFrameWrapper.h b/CefSharp.Core/Internals/CefFrameWrapper.h index 45fecf694..07d5abd4d 100644 --- a/CefSharp.Core/Internals/CefFrameWrapper.h +++ b/CefSharp.Core/Internals/CefFrameWrapper.h @@ -24,15 +24,14 @@ namespace CefSharp { private: MCefRefPtr _frame; - IBrowserAdapter^ _browserAdapter; IFrame^ _parentFrame; IBrowser^ _owningBrowser; Object^ _syncRoot; bool _disposed; internal: - CefFrameWrapper::CefFrameWrapper(CefRefPtr &frame, IBrowserAdapter^ browserAdapter) - : _frame(frame), _browserAdapter(browserAdapter), _disposed(false), + CefFrameWrapper::CefFrameWrapper(CefRefPtr &frame) + : _frame(frame), _disposed(false), _parentFrame(nullptr), _owningBrowser(nullptr), _syncRoot(gcnew Object()) { } @@ -46,7 +45,6 @@ namespace CefSharp { this->!CefFrameWrapper(); - _browserAdapter = nullptr; delete _parentFrame; delete _owningBrowser; _parentFrame = nullptr; diff --git a/CefSharp.Core/Internals/CefSharpBrowserWrapper.cpp b/CefSharp.Core/Internals/CefSharpBrowserWrapper.cpp index 1605c96ce..87cc4c30f 100644 --- a/CefSharp.Core/Internals/CefSharpBrowserWrapper.cpp +++ b/CefSharp.Core/Internals/CefSharpBrowserWrapper.cpp @@ -154,7 +154,7 @@ IFrame^ CefSharpBrowserWrapper::MainFrame::get() { ThrowIfDisposed(); auto frame = _browser->GetMainFrame(); - return gcnew CefFrameWrapper(frame, _browserAdapter); + return gcnew CefFrameWrapper(frame); } /// @@ -164,7 +164,7 @@ IFrame^ CefSharpBrowserWrapper::MainFrame::get() IFrame^ CefSharpBrowserWrapper::FocusedFrame::get() { ThrowIfDisposed(); - return gcnew CefFrameWrapper(_browser->GetFocusedFrame(), _browserAdapter); + return gcnew CefFrameWrapper(_browser->GetFocusedFrame()); } /// @@ -174,7 +174,7 @@ IFrame^ CefSharpBrowserWrapper::FocusedFrame::get() IFrame^ CefSharpBrowserWrapper::GetFrame(Int64 identifier) { ThrowIfDisposed(); - return gcnew CefFrameWrapper(_browser->GetFrame(identifier), _browserAdapter); + return gcnew CefFrameWrapper(_browser->GetFrame(identifier)); } /// @@ -184,7 +184,7 @@ IFrame^ CefSharpBrowserWrapper::GetFrame(Int64 identifier) IFrame^ CefSharpBrowserWrapper::GetFrame(String^ name) { ThrowIfDisposed(); - return gcnew CefFrameWrapper(_browser->GetFrame(StringUtils::ToNative(name)), _browserAdapter); + return gcnew CefFrameWrapper(_browser->GetFrame(StringUtils::ToNative(name))); } /// diff --git a/CefSharp.Core/Internals/CefSharpBrowserWrapper.h b/CefSharp.Core/Internals/CefSharpBrowserWrapper.h index 2c2587f41..a1b0d9379 100644 --- a/CefSharp.Core/Internals/CefSharpBrowserWrapper.h +++ b/CefSharp.Core/Internals/CefSharpBrowserWrapper.h @@ -17,13 +17,12 @@ namespace CefSharp { private: MCefRefPtr _browser; - IBrowserAdapter^ _browserAdapter; IBrowserHost^ _browserHost; bool _disposed; internal: - CefSharpBrowserWrapper::CefSharpBrowserWrapper(CefRefPtr &browser, IBrowserAdapter^ browserAdapter) - : _browser(browser), _browserAdapter(browserAdapter), _disposed(false) + CefSharpBrowserWrapper::CefSharpBrowserWrapper(CefRefPtr &browser) + : _browser(browser), _disposed(false) { } @@ -36,7 +35,6 @@ namespace CefSharp ~CefSharpBrowserWrapper() { this->!CefSharpBrowserWrapper(); - _browserAdapter = nullptr; delete _browserHost; _browserHost = nullptr; _disposed = true; diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index f71cf7f41..3af45dee8 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -71,7 +71,7 @@ namespace CefSharp bool createdWrapper = false; IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); auto result = handler->OnBeforePopup( _browserControl, browserWrapper, %frameWrapper, StringUtils::ToClr(target_url), @@ -83,7 +83,7 @@ namespace CefSharp { if (browser->IsPopup()) { - auto browserWrapper = gcnew CefSharpBrowserWrapper(browser, _browserAdapter); + auto browserWrapper = gcnew CefSharpBrowserWrapper(browser); // Add to the list of popup browsers. _popupBrowsers->Add(browser->GetIdentifier(), browserWrapper); auto handler = _browserControl->PopupHandler; @@ -315,14 +315,14 @@ namespace CefSharp if (popupHandler != nullptr) { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnFrameLoadStart(_browserControl, gcnew FrameLoadStartEventArgs(browserWrapper, %frameWrapper)); } } else { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); _browserControl->OnFrameLoadStart(gcnew FrameLoadStartEventArgs(browserWrapper, %frameWrapper)); } } @@ -336,13 +336,13 @@ namespace CefSharp { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnFrameLoadEnd(_browserControl, gcnew FrameLoadEndEventArgs(browserWrapper, %frameWrapper, httpStatusCode)); } } else { - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); _browserControl->OnFrameLoadEnd(gcnew FrameLoadEndEventArgs(browserWrapper, %frameWrapper, httpStatusCode)); } @@ -356,14 +356,14 @@ namespace CefSharp if (popupHandler != nullptr) { IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnLoadError(_browserControl, browserWrapper, gcnew LoadErrorEventArgs(%frameWrapper, static_cast(errorCode), StringUtils::ToClr(errorText), StringUtils::ToClr(failedUrl))); } } else { - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); _browserControl->OnLoadError(%frameWrapper, (CefErrorCode)errorCode, StringUtils::ToClr(errorText), StringUtils::ToClr(failedUrl)); } } @@ -380,7 +380,7 @@ namespace CefSharp IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); CefRequestWrapper requestWrapper(request); return popupHandler->OnBeforeBrowse(_browserControl, browserWrapper, %requestWrapper, isRedirect, %frameWrapper); } @@ -393,7 +393,7 @@ namespace CefSharp } IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); CefRequestWrapper requestWrapper(request); return handler->OnBeforeBrowse(_browserControl, browserWrapper, %frameWrapper, %requestWrapper, isRedirect); @@ -488,7 +488,7 @@ namespace CefSharp { auto managedNewUrl = StringUtils::ToClr(newUrl); IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true);; - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); popupHandler->OnResourceRedirect(_browserControl, browserWrapper, %frameWrapper, managedNewUrl); @@ -501,7 +501,7 @@ namespace CefSharp if (handler != nullptr) { auto managedNewUrl = StringUtils::ToClr(newUrl); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); handler->OnResourceRedirect(_browserControl, %frameWrapper, managedNewUrl); @@ -536,7 +536,7 @@ namespace CefSharp } IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()); - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto requestWrapper = gcnew CefRequestWrapper(request); auto resourceHandler = factory->GetResourceHandler(_browserControl, browserWrapper, frameWrapper, requestWrapper); @@ -570,7 +570,7 @@ namespace CefSharp auto popupHandler = _browserControl->PopupHandler; if (popupHandler != nullptr) { - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto requestWrapper = gcnew CefRequestWrapper(request); IBrowser^ browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), true); @@ -581,7 +581,7 @@ namespace CefSharp } else { - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), false); auto requestWrapper = gcnew CefRequestWrapper(request); auto requestCallback = gcnew CefRequestCallbackWrapper(callback, frameWrapper, requestWrapper); @@ -601,7 +601,7 @@ namespace CefSharp } auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()); - auto frameWrapper = gcnew CefFrameWrapper(frame, _browserAdapter); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto callbackWrapper = gcnew CefAuthCallbackWrapper(callback, frameWrapper); return handler->GetAuthCredentials( @@ -618,7 +618,7 @@ namespace CefSharp // Context menu params CefContextMenuParamsWrapper contextMenuParamsWrapper(params); - CefFrameWrapper frameWrapper(frame, _browserAdapter); + CefFrameWrapper frameWrapper(frame); auto result = handler->OnBeforeContextMenu(_browserControl, %frameWrapper, %contextMenuParamsWrapper); if (!result) { diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index 246f03375..298649bd7 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -54,7 +54,7 @@ void ManagedCefBrowserAdapter::OnAfterBrowserCreated(int browserId) auto browser = _clientAdapter->GetCefBrowser(); if (browser != nullptr) { - _browserWrapper = gcnew CefSharpBrowserWrapper(browser, this); + _browserWrapper = gcnew CefSharpBrowserWrapper(browser); } if (_webBrowserInternal != nullptr) @@ -459,7 +459,7 @@ IFrame^ ManagedCefBrowserAdapter::GetMainFrame() { return nullptr; } - return gcnew CefFrameWrapper(browser->GetMainFrame(), this); + return gcnew CefFrameWrapper(browser->GetMainFrame()); } /// @@ -474,7 +474,7 @@ IFrame^ ManagedCefBrowserAdapter::GetFocusedFrame() return nullptr; } - return gcnew CefFrameWrapper(browser->GetFocusedFrame(), this); + return gcnew CefFrameWrapper(browser->GetFocusedFrame()); } /// @@ -494,7 +494,7 @@ IFrame^ ManagedCefBrowserAdapter::GetFrame(System::Int64 identifier) return nullptr; } - return gcnew CefFrameWrapper(result, this); + return gcnew CefFrameWrapper(result); } /// @@ -514,7 +514,7 @@ IFrame^ ManagedCefBrowserAdapter::GetFrame(String^ name) return nullptr; } - return gcnew CefFrameWrapper(result, this); + return gcnew CefFrameWrapper(result); } /// diff --git a/CefSharp.Core/SchemeHandlerFactoryWrapper.h b/CefSharp.Core/SchemeHandlerFactoryWrapper.h index 6edef9f81..b728b4942 100644 --- a/CefSharp.Core/SchemeHandlerFactoryWrapper.h +++ b/CefSharp.Core/SchemeHandlerFactoryWrapper.h @@ -32,8 +32,8 @@ namespace CefSharp // NOTE: NOT supplying a browser adapter here means they currently // can't issue a JavaScript ExecuteScriptAsync call from any IFrame generated via browserWrapper // and IFrame itself. - auto browserWrapper = gcnew CefSharpBrowserWrapper(browser, nullptr); - auto frameWrapper = gcnew CefFrameWrapper(frame, nullptr); + auto browserWrapper = gcnew CefSharpBrowserWrapper(browser); + auto frameWrapper = gcnew CefFrameWrapper(frame); auto requestWrapper = gcnew CefRequestWrapper(request); auto handler = _factory->Create(browserWrapper, frameWrapper, StringUtils::ToClr(schemeName), requestWrapper); diff --git a/CefSharp/WebBrowserExtensions.cs b/CefSharp/WebBrowserExtensions.cs index ebc25c17b..dafe1c10f 100644 --- a/CefSharp/WebBrowserExtensions.cs +++ b/CefSharp/WebBrowserExtensions.cs @@ -545,11 +545,6 @@ namespace CefSharp throw new ArgumentOutOfRangeException("timeout", "Timeout greater than Maximum allowable value of " + UInt32.MaxValue); } - if (!CefSharpSettings.WcfEnabled) - { - throw new InvalidOperationException("To wait for javascript code set WcfEnabled true in CefSettings during initialization."); - } - using (var frame = browser.GetMainFrame()) { ThrowExceptionIfFrameNull(frame); From 192f6e7783ddcb6a317a49cd17be5f2d9ce16dda Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 14:04:08 +1000 Subject: [PATCH 20/24] Make FindBrowserWrapper public - preferable to using friend which --- CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index bfeae1825..4a383478a 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -14,24 +14,19 @@ #include "../CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h" using namespace System::Collections::Generic; +using namespace CefSharp::Internals::Messaging; namespace CefSharp { - using namespace Internals::Messaging; - // This class is the native subprocess level CEF object wrapper. private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler { private: - friend EvaluateScriptDelegate; - ProcessMessageDelegateSet _processMessageDelegates; gcroot^> _onBrowserCreated; gcroot^> _onBrowserDestroyed; gcroot^> _browserWrappers; - - CefBrowserWrapper^ FindBrowserWrapper(int browserId, bool mustExist); public: CefAppUnmanagedWrapper(Action^ onBrowserCreated, Action^ onBrowserDestoryed) @@ -51,6 +46,8 @@ namespace CefSharp delete _onBrowserDestroyed; } + CefBrowserWrapper^ FindBrowserWrapper(int browserId, bool mustExist); + virtual DECL CefRefPtr GetRenderProcessHandler() OVERRIDE; virtual DECL void OnBrowserCreated(CefRefPtr browser) OVERRIDE; virtual DECL void OnBrowserDestroyed(CefRefPtr browser) OVERRIDE; From 02ddbd702ba7b397ed600cacaa4f929f79156dce Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 14:08:24 +1000 Subject: [PATCH 21/24] Change GetWrapped to Browser - consistent with other code e.g. cefframewrapper --- CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp | 2 +- CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h | 5 ++++- .../Messaging/EvaluateScriptDelegate.cpp | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp index 11f4172c3..3ecdff657 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp @@ -41,7 +41,7 @@ namespace CefSharp _callbackRegistry->Deregister(id); } - CefRefPtr CefBrowserWrapper::GetWrapped() + CefRefPtr CefBrowserWrapper::Browser::get() { return _cefBrowser.get(); } diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h index 832d7d090..ee73c9df3 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h @@ -32,7 +32,10 @@ namespace CefSharp internal: //used by EvaluateScriptDelegate - CefRefPtr GetWrapped(); + property CefRefPtr Browser + { + CefRefPtr get(); + } property JavascriptCallbackRegistry^ CallbackRegistry { diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp index 5221ab00b..f3d3f90d1 100644 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp +++ b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp @@ -37,7 +37,7 @@ namespace CefSharp auto script = argList->GetString(3); auto browserWrapper = _appUnmanagedWrapper->FindBrowserWrapper(browserId, true); - auto browser = browserWrapper->GetWrapped(); + auto browser = browserWrapper->Browser; auto frame = browser->GetFrame(frameId); if (browser.get() && frame.get()) { From ddd7ec168794c7aba0e6d5e6b6af08595e3551ec Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 14:14:12 +1000 Subject: [PATCH 22/24] Cleanup usings --- CefSharp/Internals/IBrowserAdapter.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/CefSharp/Internals/IBrowserAdapter.cs b/CefSharp/Internals/IBrowserAdapter.cs index c9c651d0e..1dda9138c 100644 --- a/CefSharp/Internals/IBrowserAdapter.cs +++ b/CefSharp/Internals/IBrowserAdapter.cs @@ -2,9 +2,6 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. -using System; -using System.Threading.Tasks; - namespace CefSharp.Internals { /// From 7090ac9cda3b375953ce5cb581e4323e1b4df578 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 15:08:53 +1000 Subject: [PATCH 23/24] For now simplify message handing - remove delegates Will look at implementing a managed class to handle/route messages later --- .../CefAppUnmanagedWrapper.cpp | 67 +++++++++++++- .../CefAppUnmanagedWrapper.h | 7 -- .../CefSharp.BrowserSubprocess.Core.vcxproj | 2 - ...arp.BrowserSubprocess.Core.vcxproj.filters | 6 -- .../Messaging/EvaluateScriptDelegate.cpp | 90 ------------------- .../Messaging/EvaluateScriptDelegate.h | 30 ------- CefSharp.Core/CefSharp.Core.vcxproj | 3 - CefSharp.Core/CefSharp.Core.vcxproj.filters | 9 -- CefSharp.Core/Internals/ClientAdapter.cpp | 58 +++++++++--- CefSharp.Core/Internals/ClientAdapter.h | 12 +-- .../Messaging/EvaluateScriptDoneDelegate.cpp | 80 ----------------- .../Messaging/EvaluateScriptDoneDelegate.h | 30 ------- .../Messaging/ProcessMessageDelegate.h | 32 ------- 13 files changed, 112 insertions(+), 314 deletions(-) delete mode 100644 CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp delete mode 100644 CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h delete mode 100644 CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp delete mode 100644 CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h delete mode 100644 CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index dae6fa26c..f97c7e721 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -8,10 +8,15 @@ #include "CefBrowserWrapper.h" #include "CefAppUnmanagedWrapper.h" #include "JavascriptRootObjectWrapper.h" +#include "Serialization\V8Serialization.h" +#include "..\CefSharp.Core\Internals\Messaging\Messages.h" +#include "..\CefSharp.Core\Internals\Serialization\Primitives.h" using namespace System; using namespace System::Diagnostics; using namespace System::Collections::Generic; +using namespace CefSharp::Internals::Messaging; +using namespace CefSharp::Internals::Serialization; namespace CefSharp { @@ -84,11 +89,65 @@ namespace CefSharp bool CefAppUnmanagedWrapper::OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) { - bool handled = false; - - for (ProcessMessageDelegateSet::iterator it = _processMessageDelegates.begin(); it != _processMessageDelegates.end() && !handled; it++) + auto handled = false; + auto name = message->GetName(); + if (name == kEvaluateJavascriptRequest) { - handled = (*it)->OnProcessMessageReceived(browser, sourceProcessId, message); + auto argList = message->GetArgumentList(); + auto browserId = argList->GetInt(0); + auto frameId = GetInt64(argList, 1); + auto callbackId = GetInt64(argList, 2); + auto script = argList->GetString(3); + + if(browser->GetIdentifier() != browserId) + { + //TODO: Re-add lookup + } + + auto browserWrapper = FindBrowserWrapper(browserId, true); + auto frame = browser->GetFrame(frameId); + if (frame.get()) + { + auto context = frame->GetV8Context(); + + if (context.get() && context->Enter()) + { + try + { + CefRefPtr result; + CefRefPtr exception; + auto success = context->Eval(script, result, exception); + auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse); + auto argList = response->GetArgumentList(); + + argList->SetBool(0, success); + SetInt64(callbackId, argList, 1); + if (success) + { + SerializeV8Object(result, argList, 2, browserWrapper->CallbackRegistry); + } + else + { + argList->SetString(2, exception->GetMessage()); + } + + if (response.get()) + { + browser->SendProcessMessage(sourceProcessId, response); + } + } + finally + { + context->Exit(); + } + } + } + else + { + //TODO handle error + } + + handled = true; } return handled; diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h index 4a383478a..c04091d44 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h @@ -10,11 +10,8 @@ #include "include/cef_base.h" #include "CefBrowserWrapper.h" -#include "Messaging/EvaluateScriptDelegate.h" -#include "../CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h" using namespace System::Collections::Generic; -using namespace CefSharp::Internals::Messaging; namespace CefSharp { @@ -22,7 +19,6 @@ namespace CefSharp private class CefAppUnmanagedWrapper : CefApp, CefRenderProcessHandler { private: - ProcessMessageDelegateSet _processMessageDelegates; gcroot^> _onBrowserCreated; gcroot^> _onBrowserDestroyed; @@ -34,9 +30,6 @@ namespace CefSharp _onBrowserCreated = onBrowserCreated; _onBrowserDestroyed = onBrowserDestoryed; _browserWrappers = gcnew Dictionary(); - - //Register evaluate script request handler - _processMessageDelegates.insert(new EvaluateScriptDelegate(this)); } ~CefAppUnmanagedWrapper() diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj index 35bfc0f7d..3c11f2d64 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj @@ -162,7 +162,6 @@ - @@ -183,7 +182,6 @@ - diff --git a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters index 0958c2a4e..5c744c4a0 100644 --- a/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters +++ b/CefSharp.BrowserSubprocess.Core/CefSharp.BrowserSubprocess.Core.vcxproj.filters @@ -74,9 +74,6 @@ Header Files - - Header Files - @@ -127,9 +124,6 @@ Source Files - - Source Files - diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp deleted file mode 100644 index f3d3f90d1..000000000 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.cpp +++ /dev/null @@ -1,90 +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. - -#include "stdafx.h" -#include "../CefSharp.Core/Internals/Messaging/Messages.h" -#include "../CefSharp.Core/Internals/Serialization/Primitives.h" - -#include "Serialization/V8Serialization.h" -#include "../CefAppUnmanagedWrapper.h" -#include "EvaluateScriptDelegate.h" - -using namespace CefSharp::Internals::Serialization; - -namespace CefSharp -{ - namespace Internals - { - namespace Messaging - { - EvaluateScriptDelegate::EvaluateScriptDelegate(CefRefPtr appUnmanagedWrapper) - :_appUnmanagedWrapper(appUnmanagedWrapper) - { - - } - - bool EvaluateScriptDelegate::OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) - { - auto handled = false; - auto name = message->GetName(); - if (name == kEvaluateJavascriptRequest) - { - auto argList = message->GetArgumentList(); - auto browserId = argList->GetInt(0); - auto frameId = GetInt64(argList, 1); - auto callbackId = GetInt64(argList, 2); - auto script = argList->GetString(3); - - auto browserWrapper = _appUnmanagedWrapper->FindBrowserWrapper(browserId, true); - auto browser = browserWrapper->Browser; - auto frame = browser->GetFrame(frameId); - if (browser.get() && frame.get()) - { - auto context = frame->GetV8Context(); - - if (context.get() && context->Enter()) - { - try - { - CefRefPtr result; - CefRefPtr exception; - auto success = context->Eval(script, result, exception); - auto response = CefProcessMessage::Create(kEvaluateJavascriptResponse); - auto argList = response->GetArgumentList(); - - argList->SetBool(0, success); - SetInt64(callbackId, argList, 1); - if (success) - { - SerializeV8Object(result, argList, 2, browserWrapper->CallbackRegistry); - } - else - { - argList->SetString(2, exception->GetMessage()); - } - - if (response.get()) - { - browser->SendProcessMessage(sourceProcessId, response); - } - } - finally - { - context->Exit(); - } - } - } - else - { - //TODO handle error - } - - handled = true; - } - - return handled; - } - } - } -} \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h b/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h deleted file mode 100644 index 264d9859c..000000000 --- a/CefSharp.BrowserSubprocess.Core/Messaging/EvaluateScriptDelegate.h +++ /dev/null @@ -1,30 +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. - -#pragma once - -#include "../CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h" - -namespace CefSharp -{ - class CefAppUnmanagedWrapper; - - namespace Internals - { - namespace Messaging - { - //This class handles incoming evaluate script messages and responses to them after fulfillment. - class EvaluateScriptDelegate : public ProcessMessageDelegate - { - private: - DISALLOW_IMPLICIT_CONSTRUCTORS(EvaluateScriptDelegate); - - CefRefPtr _appUnmanagedWrapper; - public: - EvaluateScriptDelegate(CefRefPtr appUnmanagedWrapper); - virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId sourceProcessId, CefRefPtr message) override; - }; - } - } -} \ No newline at end of file diff --git a/CefSharp.Core/CefSharp.Core.vcxproj b/CefSharp.Core/CefSharp.Core.vcxproj index d48771ed2..4859ba5e4 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj +++ b/CefSharp.Core/CefSharp.Core.vcxproj @@ -219,7 +219,6 @@ - @@ -246,7 +245,6 @@ - @@ -282,7 +280,6 @@ - diff --git a/CefSharp.Core/CefSharp.Core.vcxproj.filters b/CefSharp.Core/CefSharp.Core.vcxproj.filters index 5348e7be7..5bb8eb369 100644 --- a/CefSharp.Core/CefSharp.Core.vcxproj.filters +++ b/CefSharp.Core/CefSharp.Core.vcxproj.filters @@ -65,9 +65,6 @@ Source Files - - Source Files - @@ -196,15 +193,9 @@ Header Files - - Header Files - Header Files - - Header Files - diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 3af45dee8..fede990d9 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -18,6 +18,12 @@ #include "CefAuthCallbackWrapper.h" #include "CefJSDialogCallbackWrapper.h" #include "CefRequestCallbackWrapper.h" +#include "Serialization\Primitives.h" +#include "Serialization\V8Serialization.h" +#include "Messaging\Messages.h" + +using namespace CefSharp::Internals::Messaging; +using namespace CefSharp::Internals::Serialization; namespace CefSharp { @@ -805,11 +811,36 @@ namespace CefSharp bool ClientAdapter::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) { - bool handled = false; - - for (ProcessMessageDelegateSet::iterator it = _processMessageDelegates.begin(); it != _processMessageDelegates.end() && !handled; it++) + auto handled = false; + auto name = message->GetName(); + if (name == kEvaluateJavascriptResponse) { - handled = (*it)->OnProcessMessageReceived(browser, source_process, message); + auto argList = message->GetArgumentList(); + auto success = argList->GetBool(0); + auto callbackId = GetInt64(argList, 1); + + IJavascriptCallbackFactory^ callbackFactory; + _javascriptCallbackFactories->TryGetValue(browser->GetIdentifier(), callbackFactory); + + auto pendingTask = _pendingTaskRepository->RemovePendingTask(callbackId); + if (pendingTask != nullptr) + { + auto response = gcnew JavascriptResponse(); + response->Success = success; + + if (success) + { + response->Result = DeserializeV8Object(argList, 2, callbackFactory); + } + else + { + response->Message = StringUtils::ToClr(argList->GetString(2)); + } + + pendingTask->SetResult(response); + } + + handled = true; } return handled; @@ -817,12 +848,19 @@ namespace CefSharp Task^ ClientAdapter::EvaluateScriptAsync(int browserId, int64 frameId, String^ script, Nullable timeout) { - Task^ result = nullptr; - if (_cefBrowser.get()) - { - result = _evalScriptDoneDelegate->EvaluateScriptAsync(_cefBrowser, browserId, frameId, script, timeout); - } - return result; + //create a new taskcompletionsource + auto idAndComplectionSource = _pendingTaskRepository->CreatePendingTask(timeout); + + auto message = CefProcessMessage::Create(kEvaluateJavascriptRequest); + auto argList = message->GetArgumentList(); + argList->SetInt(0, browserId); + SetInt64(frameId, argList, 1); + SetInt64(idAndComplectionSource.Key, argList, 2); + argList->SetString(3, StringUtils::ToNative(script)); + + _cefBrowser->SendProcessMessage(CefProcessId::PID_RENDERER, message); + + return idAndComplectionSource.Value->Task; } } } diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 710fb3c5d..4985591da 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -10,8 +10,6 @@ #include "include/cef_client.h" #include "include/cef_render_process_handler.h" #include "include/internal/cef_types.h" -#include "Internals/Messaging/ProcessMessageDelegate.h" -#include "Internals/Messaging/EvaluateScriptDoneDelegate.h" using namespace System; @@ -19,8 +17,6 @@ namespace CefSharp { namespace Internals { - using namespace Messaging; - private class ClientAdapter : public CefClient, public CefLifeSpanHandler, public CefLoadHandler, @@ -39,10 +35,7 @@ namespace CefSharp gcroot _browserControl; HWND _browserHwnd; CefRefPtr _cefBrowser; - //eval script handler - CefRefPtr _evalScriptDoneDelegate; - ProcessMessageDelegateSet _processMessageDelegates; gcroot^> _popupBrowsers; gcroot _tooltip; gcroot _browserAdapter; @@ -66,10 +59,7 @@ namespace CefSharp _javascriptCallbackFactories(gcnew Dictionary()), _browserAdapter(browserAdapter) { - //create eval script message handler - _evalScriptDoneDelegate = new EvaluateScriptDoneDelegate(_pendingTaskRepository, _javascriptCallbackFactories); - - _processMessageDelegates.insert(_evalScriptDoneDelegate); + } ~ClientAdapter() diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp deleted file mode 100644 index 5d029b08e..000000000 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.cpp +++ /dev/null @@ -1,80 +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. - -#include "Stdafx.h" -#include "Messages.h" -#include "../Serialization/Primitives.h" -#include "../Serialization/V8Serialization.h" -#include "EvaluateScriptDoneDelegate.h" - -using namespace CefSharp::Internals::Serialization; - -namespace CefSharp -{ - namespace Internals - { - namespace Messaging - { - EvaluateScriptDoneDelegate::EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks, Dictionary^ callbackFactories) - :_pendingTasks(pendingTasks), _callbackFactories(callbackFactories) - { - - } - - Task^ EvaluateScriptDoneDelegate::EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int64 frameId, String^ script, Nullable timeout) - { - //create a new taskcompletionsource - auto idAndComplectionSource = _pendingTasks->CreatePendingTask(timeout); - - auto message = CefProcessMessage::Create(kEvaluateJavascriptRequest); - auto argList = message->GetArgumentList(); - argList->SetInt(0, browserId); - SetInt64(frameId, argList, 1); - SetInt64(idAndComplectionSource.Key, argList, 2); - argList->SetString(3, StringUtils::ToNative(script)); - - cefBrowser->SendProcessMessage(CefProcessId::PID_RENDERER, message); - - return idAndComplectionSource.Value->Task; - } - - bool EvaluateScriptDoneDelegate::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) - { - auto handled = false; - auto name = message->GetName(); - if (name == kEvaluateJavascriptResponse) - { - auto argList = message->GetArgumentList(); - auto success = argList->GetBool(0); - auto callbackId = GetInt64(argList, 1); - - IJavascriptCallbackFactory^ callbackFactory; - _callbackFactories->TryGetValue(browser->GetIdentifier(), callbackFactory); - - auto pendingTask = _pendingTasks->RemovePendingTask(callbackId); - if (pendingTask != nullptr) - { - auto response = gcnew JavascriptResponse(); - response->Success = success; - - if (success) - { - response->Result = DeserializeV8Object(argList, 2, callbackFactory); - } - else - { - response->Message = StringUtils::ToClr(argList->GetString(2)); - } - - pendingTask->SetResult(response); - } - - handled = true; - } - - return handled; - } - } - } -} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h b/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h deleted file mode 100644 index 25e17bcb1..000000000 --- a/CefSharp.Core/Internals/Messaging/EvaluateScriptDoneDelegate.h +++ /dev/null @@ -1,30 +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. - -#pragma once - -#include "ProcessMessageDelegate.h" - -namespace CefSharp -{ - namespace Internals - { - namespace Messaging - { - //This class will handle result messages of eval script calls - class EvaluateScriptDoneDelegate : public ProcessMessageDelegate - { - private: - DISALLOW_IMPLICIT_CONSTRUCTORS(EvaluateScriptDoneDelegate); - - gcroot^> _callbackFactories; - gcroot^> _pendingTasks; - public: - EvaluateScriptDoneDelegate(PendingTaskRepository^ pendingTasks, Dictionary^ callbackFactories); - Task^ EvaluateScriptAsync(CefRefPtr cefBrowser, int browserId, int64 frameId, String^ script, Nullable timeout); - virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) override; - }; - } - } -} \ No newline at end of file diff --git a/CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h b/CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h deleted file mode 100644 index 5e0fb347e..000000000 --- a/CefSharp.Core/Internals/Messaging/ProcessMessageDelegate.h +++ /dev/null @@ -1,32 +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. - -#pragma once - -#include - -#include "include\cef_base.h" -#include "include\cef_app.h" - -namespace CefSharp -{ - namespace Internals - { - namespace Messaging - { - //Specific process message handler classes can inherit from this one - class ProcessMessageDelegate : public virtual CefBase - { - public: - //Will be called for uhandled received process messages. - //Returns false if message wasn't handled, true if it was - virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) = 0; - - IMPLEMENT_REFCOUNTING(ProcessMessageDelegate); - }; - - typedef std::set> ProcessMessageDelegateSet; - } - } -} \ No newline at end of file From 81a220275356525ec8064b49c4ffd854f9b78edf Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 29 Jun 2015 15:35:30 +1000 Subject: [PATCH 24/24] Throw exception if browser Id's don't match Use the provided browser object rather than get the wrapped one - should be the same --- CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp | 2 +- CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp | 5 ----- CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h | 6 ------ 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index f97c7e721..8f9606f00 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -101,7 +101,7 @@ namespace CefSharp if(browser->GetIdentifier() != browserId) { - //TODO: Re-add lookup + throw gcnew InvalidOperationException(String::Format("Request BrowserId : {0} does not match browser Id : {1}", browserId, browser->GetIdentifier())); } auto browserWrapper = FindBrowserWrapper(browserId, true); diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp index 3ecdff657..f89b213e1 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.cpp @@ -41,11 +41,6 @@ namespace CefSharp _callbackRegistry->Deregister(id); } - CefRefPtr CefBrowserWrapper::Browser::get() - { - return _cefBrowser.get(); - } - JavascriptCallbackRegistry^ CefBrowserWrapper::CallbackRegistry::get() { return _callbackRegistry; diff --git a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h index ee73c9df3..9c20cd855 100644 --- a/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h +++ b/CefSharp.BrowserSubprocess.Core/CefBrowserWrapper.h @@ -31,12 +31,6 @@ namespace CefSharp JavascriptRootObjectWrapper^ _javascriptRootObjectWrapper; internal: - //used by EvaluateScriptDelegate - property CefRefPtr Browser - { - CefRefPtr get(); - } - property JavascriptCallbackRegistry^ CallbackRegistry { CefSharp::Internals::JavascriptCallbackRegistry^ get();