CEFInterfaceJSScripting.h
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Engine/Source/Runtime/WebBrowser/Private/CEF/CEFJSScripting.h
#pragma once
#include "CoreMinimal.h"
#if WITH_CEF3
#include "WebInterfaceJSFunction.h"
#include "WebInterfaceJSScripting.h"
#if PLATFORM_WINDOWS
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/AllowWindowsPlatformAtomics.h"
#endif
#pragma push_macro("OVERRIDE")
#undef OVERRIDE // cef headers provide their own OVERRIDE macro
THIRD_PARTY_INCLUDES_START
#if PLATFORM_APPLE
PRAGMA_DISABLE_DEPRECATION_WARNINGS
#endif
#include "include/cef_client.h"
#include "include/cef_values.h"
#if PLATFORM_APPLE
PRAGMA_ENABLE_DEPRECATION_WARNINGS
#endif
THIRD_PARTY_INCLUDES_END
#pragma pop_macro("OVERRIDE")
#if PLATFORM_WINDOWS
#include "Windows/HideWindowsPlatformAtomics.h"
#include "Windows/HideWindowsPlatformTypes.h"
#endif
#endif
class Error;
class FWebInterfaceJSScripting;
struct FWebInterfaceJSParam;
#if WITH_CEF3
/**
* Implements handling of bridging UObjects client side with JavaScript renderer side.
*/
class FCEFInterfaceJSScripting
: public FWebInterfaceJSScripting
, public TSharedFromThis<FCEFInterfaceJSScripting>
{
public:
FCEFInterfaceJSScripting(CefRefPtr<CefBrowser> Browser, bool bJSBindingToLoweringEnabled)
: FWebInterfaceJSScripting(bJSBindingToLoweringEnabled)
, InternalCefBrowser(Browser)
{}
void UnbindCefBrowser();
virtual void BindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true) override;
virtual void UnbindUObject(const FString& Name, UObject* Object = nullptr, bool bIsPermanent = true) override;
/**
* Called when a message was received from the renderer process.
*
* @param Browser The CefBrowser for this window.
* @param SourceProcess The process id of the sender of the message. Currently always PID_RENDERER.
* @param Message The actual message.
* @return true if the message was handled, else false.
*/
bool OnProcessMessageReceived(CefRefPtr<CefBrowser> Browser, CefProcessId SourceProcess, CefRefPtr<CefProcessMessage> Message);
/**
* Sends a message to the renderer process.
* See https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage#markdown-header-inter-process-communication-ipc for more information.
*
* @param Message the message to send to the renderer process
*/
void SendProcessMessage(CefRefPtr<CefProcessMessage> Message);
CefRefPtr<CefDictionaryValue> ConvertStruct(UStruct* TypeInfo, const void* StructPtr);
CefRefPtr<CefDictionaryValue> ConvertObject(UObject* Object);
// Works for CefListValue and CefDictionaryValues
template<typename ContainerType, typename KeyType>
bool SetConverted(CefRefPtr<ContainerType> Container, KeyType Key, FWebInterfaceJSParam& Param)
{
switch (Param.Tag)
{
case FWebInterfaceJSParam::PTYPE_NULL:
return Container->SetNull(Key);
case FWebInterfaceJSParam::PTYPE_BOOL:
return Container->SetBool(Key, Param.BoolValue);
case FWebInterfaceJSParam::PTYPE_DOUBLE:
return Container->SetDouble(Key, Param.DoubleValue);
case FWebInterfaceJSParam::PTYPE_INT:
return Container->SetInt(Key, Param.IntValue);
case FWebInterfaceJSParam::PTYPE_STRING:
{
CefString ConvertedString = TCHAR_TO_WCHAR(**Param.StringValue);
return Container->SetString(Key, ConvertedString);
}
case FWebInterfaceJSParam::PTYPE_OBJECT:
{
if (Param.ObjectValue == nullptr)
{
return Container->SetNull(Key);
}
else
{
CefRefPtr<CefDictionaryValue> ConvertedObject = ConvertObject(Param.ObjectValue);
return Container->SetDictionary(Key, ConvertedObject);
}
}
case FWebInterfaceJSParam::PTYPE_STRUCT:
{
CefRefPtr<CefDictionaryValue> ConvertedStruct = ConvertStruct(Param.StructValue->GetTypeInfo(), Param.StructValue->GetData());
return Container->SetDictionary(Key, ConvertedStruct);
}
case FWebInterfaceJSParam::PTYPE_ARRAY:
{
CefRefPtr<CefListValue> ConvertedArray = CefListValue::Create();
for(int i=0; i < Param.ArrayValue->Num(); ++i)
{
SetConverted(ConvertedArray, i, (*Param.ArrayValue)[i]);
}
return Container->SetList(Key, ConvertedArray);
}
case FWebInterfaceJSParam::PTYPE_MAP:
{
CefRefPtr<CefDictionaryValue> ConvertedMap = CefDictionaryValue::Create();
for(auto& Pair : *Param.MapValue)
{
SetConverted(ConvertedMap, TCHAR_TO_WCHAR(*Pair.Key), Pair.Value);
}
return Container->SetDictionary(Key, ConvertedMap);
}
default:
return false;
}
}
CefRefPtr<CefDictionaryValue> GetPermanentBindings();
void InvokeJSFunction(FGuid FunctionId, int32 ArgCount, FWebInterfaceJSParam Arguments[], bool bIsError=false) override;
void InvokeJSFunction(FGuid FunctionId, const CefRefPtr<CefListValue>& FunctionArguments, bool bIsError=false);
void InvokeJSErrorResult(FGuid FunctionId, const FString& Error) override;
private:
bool ConvertStructArgImpl(uint8* Args, FProperty* Param, CefRefPtr<CefListValue> List, int32 Index);
bool IsValid()
{
return InternalCefBrowser.get() != nullptr;
}
/** Message handling helpers */
bool HandleExecuteUObjectMethodMessage(CefRefPtr<CefListValue> MessageArguments);
bool HandleReleaseUObjectMessage(CefRefPtr<CefListValue> MessageArguments);
/** Pointer to the CEF Browser for this window. */
CefRefPtr<CefBrowser> InternalCefBrowser;
};
#endif