From d0b50a77bffc6f09c8bb4631b47c30675b6714e0 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Sat, 25 Nov 2023 14:41:46 +1000 Subject: [PATCH] Core - Basic ArrayBuffer support for EvaluateScriptAsync --- .../Serialization/V8Serialization.cpp | 4 +++ .../Serialization/ObjectsSerialization.cpp | 4 +++ .../Internals/Serialization/Primitives.cpp | 36 ++++++++++++++++++- .../Internals/Serialization/Primitives.h | 17 +++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp index 19cfa3578d..307ef0b0d4 100644 --- a/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp +++ b/CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp @@ -71,6 +71,10 @@ namespace CefSharp { SetCefTime(list, index, obj->GetDateValue().val); } + else if (obj->IsArrayBuffer()) + { + SetArrayBuffer(list, index, obj->GetArrayBufferByteLength(), obj->GetArrayBufferData()); + } else if (obj->IsArray()) { int arrLength = obj->GetArrayLength(); diff --git a/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp b/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp index 0c911e99f8..8f2175b6ab 100644 --- a/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp +++ b/CefSharp.Core.Runtime/Internals/Serialization/ObjectsSerialization.cpp @@ -38,6 +38,10 @@ namespace CefSharp auto cefTime = GetCefTime(list, index); result = CefTimeUtils::FromBaseTimeToDateTime(cefTime.val); } + else if (IsArrayBuffer(list, index)) + { + result = GetArrayBuffer(list, index); + } else if (IsJsCallback(list, index) && javascriptCallbackFactory != nullptr) { auto jsCallbackDto = GetJsCallback(list, index); diff --git a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp index 40adb0e076..127df727b6 100644 --- a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp +++ b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.cpp @@ -19,7 +19,8 @@ namespace CefSharp { INT64, CEFTIME, - JSCALLBACK + JSCALLBACK, + ARRAYBUFFER }; template @@ -91,6 +92,39 @@ namespace CefSharp { return IsType(PrimitiveType::CEFTIME, list, index); } + + template + void SetArrayBuffer(const CefRefPtr& list, TIndex index, const size_t& size, const void* value) + { + const auto src = static_cast(value); + + auto dest = new uint8_t[size + 1]; + dest[0] = static_cast(PrimitiveType::ARRAYBUFFER); + memcpy(&dest[1], src, size); + + list->SetBinary(index, CefBinaryValue::Create(dest, size + 1)); + } + + template + cli::array^ GetArrayBuffer(const CefRefPtr& list, TIndex index) + { + auto binaryValue = list->GetBinary(index); + auto size = binaryValue->GetSize() - 1; + + auto bufferByte = gcnew cli::array(static_cast(size)); + pin_ptr src = &bufferByte[0]; // pin pointer to first element in arr + + binaryValue->GetData(static_cast(src), size, 1); + + return bufferByte; + } + + template + bool IsArrayBuffer(const CefRefPtr& list, TIndex index) + { + return IsType(PrimitiveType::ARRAYBUFFER, list, index); + } + template void SetJsCallback(const CefRefPtr& list, TIndex index, JavascriptCallback^ value) { diff --git a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h index b160165975..04aad9b7f4 100644 --- a/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h +++ b/CefSharp.Core.Runtime/Internals/Serialization/Primitives.h @@ -26,6 +26,13 @@ namespace CefSharp template bool IsCefTime(const CefRefPtr& list, TIndex index); + template + void SetArrayBuffer(const CefRefPtr& list, TIndex index, const size_t& size, const void* value); + template + cli::array^ GetArrayBuffer(const CefRefPtr& list, TIndex index); + template + bool IsArrayBuffer(const CefRefPtr& list, TIndex index); + template void SetJsCallback(const CefRefPtr& list, TIndex index, JavascriptCallback^ value); template @@ -53,6 +60,16 @@ namespace CefSharp template bool IsCefTime(const CefRefPtr& list, int index); template bool IsCefTime(const CefRefPtr& list, CefString index); + template void SetArrayBuffer(const CefRefPtr& list, int index, const size_t& size, const void* value); + template void SetArrayBuffer(const CefRefPtr& list, size_t index, const size_t& size, const void* value); + template void SetArrayBuffer(const CefRefPtr& list, CefString index, const size_t& size, const void* value); + template cli::array^ GetArrayBuffer(const CefRefPtr& list, int index); + template cli::array^ GetArrayBuffer(const CefRefPtr& list, size_t index); + template cli::array^ GetArrayBuffer(const CefRefPtr& list, CefString index); + template bool IsArrayBuffer(const CefRefPtr& list, size_t index); + template bool IsArrayBuffer(const CefRefPtr& list, int index); + template bool IsArrayBuffer(const CefRefPtr& list, CefString index); + template void SetJsCallback(const CefRefPtr& list, int index, JavascriptCallback^ value); template void SetJsCallback(const CefRefPtr& list, size_t index, JavascriptCallback^ value); template void SetJsCallback(const CefRefPtr& list, CefString index, JavascriptCallback^ value);