Files
UnrealEngineUWP/Engine/Source/Runtime/WebBrowser/Private/CEF/CEFJSScripting.cpp
Ben Marsh 4ba423868f Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3209340 on 2016/11/23 by Ben.Marsh

	Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.

	Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.

	  * Every header now includes everything it needs to compile.
	        * There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
	        * There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
	  * Every .cpp file includes its matching .h file first.
	        * This helps validate that each header is including everything it needs to compile.
	  * No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
	        * You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
	        * There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
	  * No engine code explicitly includes a precompiled header any more.
	        * We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
	        * PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.

	Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.

[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00

354 lines
11 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "CEF/CEFJSScripting.h"
#if WITH_CEF3
#include "WebJSScripting.h"
#include "WebJSFunction.h"
#include "CEFWebBrowserWindow.h"
#include "CEFJSStructSerializerBackend.h"
#include "CEFJSStructDeserializerBackend.h"
#include "StructSerializer.h"
#include "StructDeserializer.h"
// Internal utility function(s)
namespace
{
template<typename DestContainerType, typename SrcContainerType, typename DestKeyType, typename SrcKeyType>
bool CopyContainerValue(DestContainerType DestContainer, SrcContainerType SrcContainer, DestKeyType DestKey, SrcKeyType SrcKey )
{
switch (SrcContainer->GetType(SrcKey))
{
case VTYPE_NULL:
return DestContainer->SetNull(DestKey);
case VTYPE_BOOL:
return DestContainer->SetBool(DestKey, SrcContainer->GetBool(SrcKey));
case VTYPE_INT:
return DestContainer->SetInt(DestKey, SrcContainer->GetInt(SrcKey));
case VTYPE_DOUBLE:
return DestContainer->SetDouble(DestKey, SrcContainer->GetDouble(SrcKey));
case VTYPE_STRING:
return DestContainer->SetString(DestKey, SrcContainer->GetString(SrcKey));
case VTYPE_BINARY:
return DestContainer->SetBinary(DestKey, SrcContainer->GetBinary(SrcKey));
case VTYPE_DICTIONARY:
return DestContainer->SetDictionary(DestKey, SrcContainer->GetDictionary(SrcKey));
case VTYPE_LIST:
return DestContainer->SetList(DestKey, SrcContainer->GetList(SrcKey));
case VTYPE_INVALID:
default:
return false;
}
}
}
CefRefPtr<CefDictionaryValue> FCEFJSScripting::ConvertStruct(UStruct* TypeInfo, const void* StructPtr)
{
FCEFJSStructSerializerBackend Backend (SharedThis(this));
FStructSerializer::Serialize(StructPtr, *TypeInfo, Backend);
CefRefPtr<CefDictionaryValue> Result = CefDictionaryValue::Create();
Result->SetString("$type", "struct");
Result->SetString("$ue4Type", *TypeInfo->GetName());
Result->SetDictionary("$value", Backend.GetResult());
return Result;
}
CefRefPtr<CefDictionaryValue> FCEFJSScripting::ConvertObject(UObject* Object)
{
CefRefPtr<CefDictionaryValue> Result = CefDictionaryValue::Create();
RetainBinding(Object);
UClass* Class = Object->GetClass();
CefRefPtr<CefListValue> MethodNames = CefListValue::Create();
int32 MethodIndex = 0;
for (TFieldIterator<UFunction> FunctionIt(Class, EFieldIteratorFlags::IncludeSuper); FunctionIt; ++FunctionIt)
{
UFunction* Function = *FunctionIt;
MethodNames->SetString(MethodIndex++, *Function->GetName());
}
Result->SetString("$type", "uobject");
Result->SetString("$id", *PtrToGuid(Object).ToString(EGuidFormats::Digits));
Result->SetList("$methods", MethodNames);
return Result;
}
bool FCEFJSScripting::OnProcessMessageReceived(CefRefPtr<CefBrowser> Browser, CefProcessId SourceProcess, CefRefPtr<CefProcessMessage> Message)
{
bool Result = false;
FString MessageName = Message->GetName().ToWString().c_str();
if (MessageName == TEXT("UE::ExecuteUObjectMethod"))
{
Result = HandleExecuteUObjectMethodMessage(Message->GetArgumentList());
}
else if (MessageName == TEXT("UE::ReleaseUObject"))
{
Result = HandleReleaseUObjectMessage(Message->GetArgumentList());
}
return Result;
}
void FCEFJSScripting::SendProcessMessage(CefRefPtr<CefProcessMessage> Message)
{
if (IsValid() )
{
InternalCefBrowser->SendProcessMessage(PID_RENDERER, Message);
}
}
CefRefPtr<CefDictionaryValue> FCEFJSScripting::GetPermanentBindings()
{
CefRefPtr<CefDictionaryValue> Result = CefDictionaryValue::Create();
for(auto& Entry : PermanentUObjectsByName)
{
Result->SetDictionary(*Entry.Key, ConvertObject(Entry.Value));
}
return Result;
}
void FCEFJSScripting::BindUObject(const FString& Name, UObject* Object, bool bIsPermanent)
{
CefRefPtr<CefDictionaryValue> Converted = ConvertObject(Object);
if (bIsPermanent)
{
// Each object can only have one permanent binding
if (BoundObjects[Object].bIsPermanent)
{
return;
}
// Existing permanent objects must be removed first
if (PermanentUObjectsByName.Contains(Name))
{
return;
}
BoundObjects[Object]={true, -1};
PermanentUObjectsByName.Add(Name, Object);
}
CefRefPtr<CefProcessMessage> SetValueMessage = CefProcessMessage::Create(TEXT("UE::SetValue"));
CefRefPtr<CefListValue>MessageArguments = SetValueMessage->GetArgumentList();
CefRefPtr<CefDictionaryValue> Value = CefDictionaryValue::Create();
Value->SetString("name", *Name);
Value->SetDictionary("value", Converted);
Value->SetBool("permanent", bIsPermanent);
MessageArguments->SetDictionary(0, Value);
SendProcessMessage(SetValueMessage);
}
void FCEFJSScripting::UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent)
{
if (bIsPermanent)
{
// If overriding an existing permanent object, make it non-permanent
if (PermanentUObjectsByName.Contains(Name) && (Object == nullptr || PermanentUObjectsByName[Name] == Object))
{
Object = PermanentUObjectsByName.FindAndRemoveChecked(Name);
BoundObjects.Remove(Object);
return;
}
else
{
return;
}
}
CefRefPtr<CefProcessMessage> DeleteValueMessage = CefProcessMessage::Create(TEXT("UE::DeleteValue"));
CefRefPtr<CefListValue>MessageArguments = DeleteValueMessage->GetArgumentList();
CefRefPtr<CefDictionaryValue> Info = CefDictionaryValue::Create();
Info->SetString("name", *Name);
Info->SetString("id", *PtrToGuid(Object).ToString(EGuidFormats::Digits));
Info->SetBool("permanent", bIsPermanent);
MessageArguments->SetDictionary(0, Info);
SendProcessMessage(DeleteValueMessage);
}
bool FCEFJSScripting::HandleReleaseUObjectMessage(CefRefPtr<CefListValue> MessageArguments)
{
FGuid ObjectKey;
// Message arguments are Name, Value, bGlobal
if (MessageArguments->GetSize() != 1 || MessageArguments->GetType(0) != VTYPE_STRING)
{
// Wrong message argument types or count
return false;
}
if (!FGuid::Parse(FString(MessageArguments->GetString(0).ToWString().c_str()), ObjectKey))
{
// Invalid GUID
return false;
}
UObject* Object = GuidToPtr(ObjectKey);
if ( Object == nullptr )
{
// Invalid object
return false;
}
ReleaseBinding(Object);
return true;
}
bool FCEFJSScripting::HandleExecuteUObjectMethodMessage(CefRefPtr<CefListValue> MessageArguments)
{
FGuid ObjectKey;
// Message arguments are Name, Value, bGlobal
if (MessageArguments->GetSize() != 4
|| MessageArguments->GetType(0) != VTYPE_STRING
|| MessageArguments->GetType(1) != VTYPE_STRING
|| MessageArguments->GetType(2) != VTYPE_STRING
|| MessageArguments->GetType(3) != VTYPE_LIST
)
{
// Wrong message argument types or count
return false;
}
if (!FGuid::Parse(FString(MessageArguments->GetString(0).ToWString().c_str()), ObjectKey))
{
// Invalid GUID
return false;
}
// Get the promise callback and use that to report any results from executing this function.
FGuid ResultCallbackId;
if (!FGuid::Parse(FString(MessageArguments->GetString(2).ToWString().c_str()), ResultCallbackId))
{
// Invalid GUID
return false;
}
UObject* Object = GuidToPtr(ObjectKey);
if (Object == nullptr)
{
// Unknown uobject id
InvokeJSErrorResult(ResultCallbackId, TEXT("Unknown UObject ID"));
return true;
}
FName MethodName = MessageArguments->GetString(1).ToWString().c_str();
UFunction* Function = Object->FindFunction(MethodName);
if (!Function)
{
InvokeJSErrorResult(ResultCallbackId, TEXT("Unknown UObject Function"));
return true;
}
// Coerce arguments to function arguments.
uint16 ParamsSize = Function->ParmsSize;
TArray<uint8> Params;
UProperty* ReturnParam = nullptr;
UProperty* PromiseParam = nullptr;
if (ParamsSize > 0)
{
// Convert cef argument list to a dictionary, so we can use FStructDeserializer to convert it for us
CefRefPtr<CefDictionaryValue> NamedArgs = CefDictionaryValue::Create();
int32 CurrentArg = 0;
CefRefPtr<CefListValue> CefArgs = MessageArguments->GetList(3);
for ( TFieldIterator<UProperty> It(Function); It; ++It )
{
UProperty* Param = *It;
if (Param->PropertyFlags & CPF_Parm)
{
if (Param->PropertyFlags & CPF_ReturnParm)
{
ReturnParam = Param;
}
else
{
UStructProperty *StructProperty = Cast<UStructProperty>(Param);
if (StructProperty && StructProperty->Struct->IsChildOf(FWebJSResponse::StaticStruct()))
{
PromiseParam = Param;
}
else
{
CopyContainerValue(NamedArgs, CefArgs, CefString(*Param->GetName()), CurrentArg);
CurrentArg++;
}
}
}
}
// UFunction is a subclass of UStruct, so we can treat the arguments as a struct for deserialization
Params.AddUninitialized(ParamsSize);
Function->InitializeStruct(Params.GetData());
FCEFJSStructDeserializerBackend Backend = FCEFJSStructDeserializerBackend(SharedThis(this), NamedArgs);
FStructDeserializer::Deserialize(Params.GetData(), *Function, Backend);
}
if (PromiseParam)
{
FWebJSResponse* PromisePtr = PromiseParam->ContainerPtrToValuePtr<FWebJSResponse>(Params.GetData());
if (PromisePtr)
{
*PromisePtr = FWebJSResponse(SharedThis(this), ResultCallbackId);
}
}
Object->ProcessEvent(Function, Params.GetData());
CefRefPtr<CefListValue> Results = CefListValue::Create();
if ( ! PromiseParam ) // If PromiseParam is set, we assume that the UFunction will ensure it is called with the result
{
if ( ReturnParam )
{
FStructSerializerPolicies ReturnPolicies;
ReturnPolicies.PropertyFilter = [&](const UProperty* CandidateProperty, const UProperty* ParentProperty)
{
return ParentProperty != nullptr || CandidateProperty == ReturnParam;
};
FCEFJSStructSerializerBackend ReturnBackend(SharedThis(this));
FStructSerializer::Serialize(Params.GetData(), *Function, ReturnBackend, ReturnPolicies);
CefRefPtr<CefDictionaryValue> ResultDict = ReturnBackend.GetResult();
// Extract the single return value from the serialized dictionary to an array
CopyContainerValue(Results, ResultDict, 0, CefString(*ReturnParam->GetName()));
}
InvokeJSFunction(ResultCallbackId, Results, false);
}
return true;
}
void FCEFJSScripting::UnbindCefBrowser()
{
InternalCefBrowser = nullptr;
}
void FCEFJSScripting::InvokeJSErrorResult(FGuid FunctionId, const FString& Error)
{
CefRefPtr<CefListValue> FunctionArguments = CefListValue::Create();
FunctionArguments->SetString(0, *Error);
InvokeJSFunction(FunctionId, FunctionArguments, true);
}
void FCEFJSScripting::InvokeJSFunction(FGuid FunctionId, int32 ArgCount, FWebJSParam Arguments[], bool bIsError)
{
CefRefPtr<CefListValue> FunctionArguments = CefListValue::Create();
for ( int32 i=0; i<ArgCount; i++)
{
SetConverted(FunctionArguments, i, Arguments[i]);
}
InvokeJSFunction(FunctionId, FunctionArguments, bIsError);
}
void FCEFJSScripting::InvokeJSFunction(FGuid FunctionId, const CefRefPtr<CefListValue>& FunctionArguments, bool bIsError)
{
CefRefPtr<CefProcessMessage> Message = CefProcessMessage::Create(TEXT("UE::ExecuteJSFunction"));
CefRefPtr<CefListValue> MessageArguments = Message->GetArgumentList();
MessageArguments->SetString(0, *FunctionId.ToString(EGuidFormats::Digits));
MessageArguments->SetList(1, FunctionArguments);
MessageArguments->SetBool(2, bIsError);
SendProcessMessage(Message);
}
#endif