Files
cefsharp/CefSharp.Core.Runtime/Internals/Serialization/V8Serialization.cpp
T
Alex Maitland 8a367fbdc4 Refactor CefSharp.Core into CefSharp.Core.Runtime (#3311)
* Net Core - Rename CefSharp.Core.dll to CefSharp.Core.Runtime.dll

Partial rename, only Net Core, folder not renamed

* Net Core - Rename CefSharp.Core.RefAssembly to CefSharp.Core.netcore

Remove GenApi

* Core - Rename CefDragDataWrapper to DragData

Move into CefSharp.Core namespace

* WinForms/WPF/OffScreen - Migrate from GitLink command line to Nuget package

* Net Core - Refactor to have CefSharp.Core.dll contain only public Api

* Net Core - Remove CefSharp.Core.RefAssembly

* Net Core - Change CefSharp.Core.netcore output folder

* Net Core - Restructure nuget packages

* Net Core - Add Cefsharp.Core.Runtime.RefAssembly

* Net Core - Hide CLI/C++ classes from intellisense

Make sure users don't attempt to load them directly

* Rename CefSharp.Core to CefSharp.Core.Runtime

* Core - Restructure Net 4.5.2 packages to use CefSharp.Core.dll anycpu variant

Attempt to load CefSharp.Core.Runtime at runtime rather than having to use msbuild to copy the correct version

* Rename CefSharp.Core.netcore to CefSharp.Core

* WPF/WinForms/OffScreen - Change from x86/64 to AnyCPU

As they are all managed assemblies they can target AnyCPU.
Includes CefSharp.dll

* Convert RequestContextBuilder from C++ to C#

Now part of the CefSharp.Core PublicApi

* Update version number to 87.1.11

* Migrate more of the public Api to C#

* Net Core - Basic restructure complete

* Net Core - ModuleInitializer (Doesn't work yet)

* Remove direct references to BrowserSettings

* Net Core - ModuleInitializer load CefShar.Core.Runtime.dl

* Net Core - Load libcef.dll via CLR Module initializer

If no RID is specified then we can load libcef.dll using the module initializer

* Add version to CefSharp.Core

* Remove dependency on CefSharp.Core.Runtime
Rewrite common targets

* AnyCPU app.config transform

Improve AnyCPU support

* Improve Net Core 3 support
Only delete CefSharp.Core.Runtime.dll when AnyCPU

* Nuget - Add CefSharp.Core.Runtime reference when TargetFramework = NetCore

* Fix Typos

Based on #3306

* Net Core - Rename CefSharp.Core.Runtime RefAssembly source file

* Net Full - Generate CefSharp.Core.Runtime Ref Assembly

For now the powershell build script generates the .cs file based on a x86 release build.ps1
It's not possible to directly install GenApi as it requires a Sdk style project

* Net Core - Old packages copy files to required folders

* Test - Install newer .Net Compiler and set Lang Version to 7.3

* Net Core - Exclude Net 452 Runtime generated reference source

* Core - Add Refactoring TODO

* Ref Assembly - Generate source as part of build

- Move ref assembly source generate into GenerateRefAssemblySource.ps1
- Call before project build

Runs locally, see if Appveyor has a problem with the powershell script execution

* Core - Add more factory methods to create instances of managed wrappers

* Net Core - Make Initialzier properties internal

Not quite sure what the public API should look like as yet, so making internal for now.
2020-12-16 10:47:34 +10:00

227 lines
9.1 KiB
C++

// Copyright © 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 "include\cef_values.h"
#include "include\cef_parser.h"
#include "V8Serialization.h"
#include "Primitives.h"
using namespace System::Collections::Generic;
namespace
{
String^ GetJavascriptName(System::Reflection::MemberInfo^ memberInfo, IJavascriptNameConverter^ nameConverter)
{
if (nameConverter == nullptr)
{
return memberInfo->Name;
}
return nameConverter->ConvertReturnedObjectPropertyAndFieldToNameJavascript(memberInfo);
}
}
namespace CefSharp
{
namespace Internals
{
namespace Serialization
{
template<typename TList, typename TIndex>
void SerializeV8Object(const CefRefPtr<TList>& list, const TIndex& index, Object^ obj, IJavascriptNameConverter^ nameConverter)
{
// Collection of ancestors to currently serialised object.
// This enables prevention of endless loops due to cycles in graphs where
// a child references one of its ancestors.
auto ancestors = gcnew HashSet<Object^>();
SerializeV8SimpleObject(list, index, obj, ancestors, nameConverter);
}
template<typename TList, typename TIndex>
void SerializeV8SimpleObject(const CefRefPtr<TList>& list, const TIndex& index, Object^ obj, HashSet<Object^>^ ancestors, IJavascriptNameConverter^ nameConverter)
{
list->SetNull(index);
if (obj == nullptr || ancestors->Contains(obj))
{
return;
}
ancestors->Add(obj);
auto type = obj->GetType();
Type^ underlyingType = Nullable::GetUnderlyingType(type);
if (underlyingType != nullptr) type = underlyingType;
if (type == Boolean::typeid)
{
list->SetBool(index, safe_cast<bool>(obj));
}
else if (type == Int32::typeid)
{
list->SetInt(index, safe_cast<int>(obj));
}
else if (type == String::typeid)
{
list->SetString(index, StringUtils::ToNative(safe_cast<String^>(obj)));
}
else if (type == Double::typeid)
{
list->SetDouble(index, safe_cast<double>(obj));
}
else if (type == Decimal::typeid)
{
list->SetDouble(index, Convert::ToDouble(obj));
}
else if (type == SByte::typeid)
{
list->SetInt(index, Convert::ToInt32(obj));
}
else if (type == Int16::typeid)
{
list->SetInt(index, Convert::ToInt32(obj));
}
else if (type == Int64::typeid)
{
list->SetDouble(index, Convert::ToDouble(obj));
}
else if (type == Byte::typeid)
{
list->SetInt(index, Convert::ToInt32(obj));
}
else if (type == UInt16::typeid)
{
list->SetInt(index, Convert::ToInt32(obj));
}
else if (type == UInt32::typeid)
{
list->SetDouble(index, Convert::ToDouble(obj));
}
else if (type == UInt64::typeid)
{
list->SetDouble(index, Convert::ToDouble(obj));
}
else if (type == Single::typeid)
{
list->SetDouble(index, Convert::ToDouble(obj));
}
else if (type == Char::typeid)
{
list->SetInt(index, Convert::ToInt32(obj));
}
else if (type == DateTime::typeid)
{
SetCefTime(list, index, ConvertDateTimeToCefTime(safe_cast<DateTime>(obj)));
}
// Serialize enum to sbyte, short, int, long, byte, ushort, uint, ulong (check type of enum)
else if (type->IsEnum)
{
auto subType = System::Enum::GetUnderlyingType(type);
if (subType == SByte::typeid ||
subType == Int16::typeid ||
subType == Int32::typeid ||
subType == Byte::typeid ||
subType == UInt16::typeid)
{
list->SetInt(index, Convert::ToInt32(obj));
}
else if (subType == Int64::typeid ||
subType == UInt32::typeid ||
subType == UInt64::typeid)
{
list->SetDouble(index, Convert::ToDouble(obj));
}
else
{
//Unexpected type, just convert it to a string
list->SetString(index, StringUtils::ToNative(Convert::ToString(obj)));
}
}
// Serialize dictionary to CefDictionary (key,value pairs)
else if (System::Collections::IDictionary::typeid->IsAssignableFrom(type))
{
auto subDict = CefDictionaryValue::Create();
auto dict = (System::Collections::IDictionary^) obj;
for each (System::Collections::DictionaryEntry kvp in dict)
{
auto fieldName = StringUtils::ToNative(Convert::ToString(kvp.Key));
SerializeV8SimpleObject(subDict, fieldName, kvp.Value, ancestors, nameConverter);
}
list->SetDictionary(index, subDict);
}
else if (System::Collections::IEnumerable::typeid->IsAssignableFrom(type))
{
auto subList = CefListValue::Create();
auto enumerable = (System::Collections::IEnumerable^) obj;
int i = 0;
for each (Object^ arrObj in enumerable)
{
SerializeV8SimpleObject(subList, i, arrObj, ancestors, nameConverter);
i++;
}
list->SetList(index, subList);
}
else if (CefSharp::Web::JsonString::typeid->IsAssignableFrom(type))
{
auto jsonString = (CefSharp::Web::JsonString^) obj;
//Tried to use CefParseJSONAndReturnError, keeps returning error when
//CefParseJson works for the same string, so must be a CEF bug
auto jsonValue = CefParseJSON(StringUtils::ToNative(jsonString->Json),
cef_json_parser_options_t::JSON_PARSER_ALLOW_TRAILING_COMMAS);
if (jsonValue.get())
{
list->SetValue(index, jsonValue);
}
else
{
list->SetString(index, CefString("V8Serialization - Unable to parse JSON"));
}
}
// Serialize class/structs to CefDictionary (key,value pairs)
else if (!type->IsPrimitive && !type->IsEnum)
{
auto fields = type->GetFields();
auto subDict = CefDictionaryValue::Create();
for (int i = 0; i < fields->Length; i++)
{
auto fieldName = GetJavascriptName(fields[i], nameConverter);
auto fieldValue = fields[i]->GetValue(obj);
SerializeV8SimpleObject(subDict, StringUtils::ToNative(fieldName), fieldValue, ancestors, nameConverter);
}
auto properties = type->GetProperties();
for (int i = 0; i < properties->Length; i++)
{
auto propertyName = GetJavascriptName(properties[i], nameConverter);
auto propertyValue = properties[i]->GetValue(obj);
SerializeV8SimpleObject(subDict, StringUtils::ToNative(propertyName), propertyValue, ancestors, nameConverter);
}
list->SetDictionary(index, subDict);
}
else
{
list->SetString(index, StringUtils::ToNative("Unable to serialize Type - " + obj->GetType()->ToString()));
}
ancestors->Remove(obj);
}
CefTime ConvertDateTimeToCefTime(DateTime dateTime)
{
auto timeSpan = dateTime.ToUniversalTime() - DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc);
return CefTime(timeSpan.TotalSeconds);
}
}
}
}