Skip to content

Commit

Permalink
V8 serialization utils added
Browse files Browse the repository at this point in the history
  • Loading branch information
arsher committed Jun 27, 2015
1 parent b92cdfd commit bf33f0b
Show file tree
Hide file tree
Showing 12 changed files with 466 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<ItemGroup>
<ClInclude Include="..\CefSharp.Core\Internals\MCefRefPtr.h" />
<ClInclude Include="..\CefSharp.Core\Internals\ReportUnhandledExceptions.h" />
<ClInclude Include="..\CefSharp.Core\Internals\Serialization\Primitives.h" />
<ClInclude Include="..\CefSharp.Core\Internals\StringUtils.h" />
<ClInclude Include="CefAppUnmanagedWrapper.h" />
<ClInclude Include="CefAppWrapper.h" />
Expand All @@ -171,9 +172,11 @@
<ClInclude Include="JavascriptRootObjectWrapper.h" />
<ClInclude Include="Stdafx.h" />
<ClInclude Include="TypeUtils.h" />
<ClInclude Include="Serialization\V8Serialization.h" />
<ClInclude Include="vcclr_local.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\CefSharp.Core\Internals\Serialization\Primitives.cpp" />
<ClCompile Include="..\CefSharp.Core\Internals\StringUtils.cpp" />
<ClCompile Include="AssemblyInfo.cpp" />
<ClCompile Include="CefAppUnmanagedWrapper.cpp" />
Expand All @@ -193,6 +196,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="TypeUtils.cpp" />
<ClCompile Include="Serialization\V8Serialization.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CefSharp\CefSharp.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
<ClInclude Include="..\CefSharp.Core\Internals\ReportUnhandledExceptions.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\CefSharp.Core\Internals\Serialization\Primitives.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Serialization\V8Serialization.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp">
Expand Down Expand Up @@ -112,6 +118,12 @@
<ClCompile Include="JavascriptCallbackWrapper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\CefSharp.Core\Internals\Serialization\Primitives.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Serialization\V8Serialization.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
85 changes: 85 additions & 0 deletions CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.cpp
Original file line number Diff line number Diff line change
@@ -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<typename TList, typename TIndex>
void SerializeV8Object(CefRefPtr<CefV8Value> obj, CefRefPtr<TList> 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<CefString> 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<CefString> 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<CefV8Value> value, CefRefPtr<CefListValue> list, int index, JavascriptCallbackRegistry^ callbackRegistry);
template void SerializeV8Object(CefRefPtr<CefV8Value> value, CefRefPtr<CefDictionaryValue> list, CefString index, JavascriptCallbackRegistry^ callbackRegistry);
}
}
}
25 changes: 25 additions & 0 deletions CefSharp.BrowserSubprocess.Core/Serialization/V8Serialization.h
Original file line number Diff line number Diff line change
@@ -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<typename TList, typename TIndex>
void SerializeV8Object(CefRefPtr<CefV8Value> value, CefRefPtr<TList> list, TIndex index, JavascriptCallbackRegistry^ callbackRegistry);
}
}
}
4 changes: 4 additions & 0 deletions CefSharp.Core/CefSharp.Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@
<ClCompile Include="Internals\StringVisitor.cpp" />
<ClCompile Include="ManagedCefBrowserAdapter.cpp" />
<ClCompile Include="NativeMethodWrapper.cpp" />
<ClCompile Include="Internals\Serialization\V8Serialization.cpp" />
<ClCompile Include="Internals\Serialization\Primitives.cpp" />
<ClCompile Include="ResourceHandlerWrapper.cpp" />
<ClCompile Include="Stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
Expand Down Expand Up @@ -272,9 +274,11 @@
<ClInclude Include="Internals\StringVisitor.h" />
<ClInclude Include="Internals\TypeConversion.h" />
<ClInclude Include="NativeMethodWrapper.h" />
<ClInclude Include="Internals\Serialization\V8Serialization.h" />
<ClInclude Include="PaintElementType.h" />
<ClInclude Include="ManagedCefBrowserAdapter.h" />
<ClInclude Include="CefSettings.h" />
<ClInclude Include="Internals\Serialization\Primitives.h" />
<ClInclude Include="SchemeHandlerFactoryWrapper.h" />
<ClInclude Include="ResourceHandlerWrapper.h" />
<ClInclude Include="CookieAsyncWrapper.h" />
Expand Down
12 changes: 12 additions & 0 deletions CefSharp.Core/CefSharp.Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<ClCompile Include="Internals\CefBrowserHostWrapper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Internals\Serialization\V8Serialization.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Internals\Serialization\Primitives.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vcclr_local.h">
Expand Down Expand Up @@ -181,6 +187,12 @@
<ClInclude Include="Internals\CefResponseWrapper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\Serialization\V8Serialization.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Internals\Serialization\Primitives.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefSharpBrowserWrapper.h">
Expand Down
155 changes: 155 additions & 0 deletions CefSharp.Core/Internals/Serialization/Primitives.cpp
Original file line number Diff line number Diff line change
@@ -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<typename TList, typename TIndex>
bool IsType(PrimitiveType type, CefRefPtr<TList> list, TIndex index)
{
auto result = list->GetType(index) == VTYPE_BINARY;
if (result)
{
underlying_type<PrimitiveType>::type typeRead;
auto binaryValue = list->GetBinary(index);
binaryValue->GetData(&typeRead, sizeof(underlying_type<PrimitiveType>::type), 0);
result = typeRead == static_cast<underlying_type<PrimitiveType>::type>(type);
}
return result;
}

template<typename TList, typename TIndex>
void SetInt64(const int64 &value, CefRefPtr<TList> list, TIndex index)
{
unsigned char mem[1 + sizeof(int64)];
mem[0] = static_cast<unsigned char>(PrimitiveType::INT64);
memcpy(reinterpret_cast<void*>(mem + 1), &value, sizeof(int64));

auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem));
list->SetBinary(index, binaryValue);
}

template<typename TList, typename TIndex>
int64 GetInt64(CefRefPtr<TList> list, TIndex index)
{
int64 result;

auto binaryValue = list->GetBinary(index);
binaryValue->GetData(&result, sizeof(int64), 1);

return result;
}

template<typename TList, typename TIndex>
bool IsInt64(CefRefPtr<TList> list, TIndex index)
{
return IsType(PrimitiveType::INT64, list, index);
}

template<typename TList, typename TIndex>
void SetCefTime(const CefTime &value, CefRefPtr<TList> list, TIndex index)
{
auto doubleT = value.GetDoubleT();
unsigned char mem[1 + sizeof(double)];
mem[0] = static_cast<unsigned char>(PrimitiveType::CEFTIME);
memcpy(reinterpret_cast<void*>(mem + 1), &doubleT, sizeof(double));

auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem));
list->SetBinary(index, binaryValue);
}

template<typename TList, typename TIndex>
CefTime GetCefTime(CefRefPtr<TList> list, TIndex index)
{
double doubleT;

auto binaryValue = list->GetBinary(index);
binaryValue->GetData(&doubleT, sizeof(double), 1);

return CefTime(doubleT);
}

template<typename TList, typename TIndex>
bool IsCefTime(CefRefPtr<TList> list, TIndex index)
{
return IsType(PrimitiveType::CEFTIME, list, index);
}
template<typename TList, typename TIndex>
void SetJsCallback(JavascriptCallback^ value, CefRefPtr<TList> list, TIndex index)
{
auto id = value->Id;
auto browserId = value->BrowserId;

unsigned char mem[1 + sizeof(int) + sizeof(int64)];
mem[0] = static_cast<unsigned char>(PrimitiveType::JSCALLBACK);
memcpy(reinterpret_cast<void*>(mem + 1), &browserId, sizeof(int));
memcpy(reinterpret_cast<void*>(mem + 1 + sizeof(int)), &id, sizeof(int64));

auto binaryValue = CefBinaryValue::Create(mem, sizeof(mem));
list->SetBinary(index, binaryValue);
}

template<typename TList, typename TIndex>
JavascriptCallback^ GetJsCallback(CefRefPtr<TList> 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<typename TList, typename TIndex>
bool IsJsCallback(CefRefPtr<TList> list, TIndex index)
{
return IsType(PrimitiveType::JSCALLBACK, list, index);
}

template void SetInt64(const int64 &value, CefRefPtr<CefListValue> list, int index);
template void SetInt64(const int64 &value, CefRefPtr<CefDictionaryValue> list, CefString index);
template int64 GetInt64(CefRefPtr<CefListValue> list, int index);
template int64 GetInt64(CefRefPtr<CefDictionaryValue> list, CefString index);
template bool IsInt64(CefRefPtr<CefListValue> list, int index);
template bool IsInt64(CefRefPtr<CefDictionaryValue> list, CefString index);

template void SetCefTime(const CefTime &value, CefRefPtr<CefListValue> list, int index);
template void SetCefTime(const CefTime &value, CefRefPtr<CefDictionaryValue> list, CefString index);
template CefTime GetCefTime(CefRefPtr<CefListValue> list, int index);
template CefTime GetCefTime(CefRefPtr<CefDictionaryValue> list, CefString index);
template bool IsCefTime(CefRefPtr<CefListValue> list, int index);
template bool IsCefTime(CefRefPtr<CefDictionaryValue> list, CefString index);

template void SetJsCallback(JavascriptCallback^ value, CefRefPtr<CefListValue> list, int index);
template void SetJsCallback(JavascriptCallback^ value, CefRefPtr<CefDictionaryValue> list, CefString index);
template JavascriptCallback^ GetJsCallback(CefRefPtr<CefListValue> list, int index);
template JavascriptCallback^ GetJsCallback(CefRefPtr<CefDictionaryValue> list, CefString index);
template bool IsJsCallback(CefRefPtr<CefListValue> list, int index);
template bool IsJsCallback(CefRefPtr<CefDictionaryValue> list, CefString index);
}
}
}
Loading

0 comments on commit bf33f0b

Please sign in to comment.