CEFInterfaceJSStructDeserializerBackend.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Engine/Source/Runtime/WebBrowser/Private/CEF/CEFJSStructDeserializerBackend.cpp
  2. #include "CEF/CEFInterfaceJSStructDeserializerBackend.h"
  3. #if WITH_CEF3
  4. #include "UObject/EnumProperty.h"
  5. #include "UObject/TextProperty.h"
  6. #include "WebInterfaceJSFunction.h"
  7. /* Internal helpers
  8. *****************************************************************************/
  9. namespace {
  10. template<typename ValueType, typename ContainerType, typename KeyType>
  11. ValueType GetNumeric(CefRefPtr<ContainerType> Container, KeyType Key)
  12. {
  13. switch(Container->GetType(Key))
  14. {
  15. case VTYPE_BOOL:
  16. return static_cast<ValueType>(Container->GetBool(Key));
  17. case VTYPE_INT:
  18. return static_cast<ValueType>(Container->GetInt(Key));
  19. case VTYPE_DOUBLE:
  20. return static_cast<ValueType>(Container->GetDouble(Key));
  21. case VTYPE_STRING:
  22. case VTYPE_DICTIONARY:
  23. case VTYPE_LIST:
  24. case VTYPE_NULL:
  25. case VTYPE_BINARY:
  26. default:
  27. return static_cast<ValueType>(0);
  28. }
  29. }
  30. template<typename ContainerType, typename KeyType>
  31. void AssignTokenFromContainer(ContainerType Container, KeyType Key, EStructDeserializerBackendTokens& OutToken, FString& PropertyName, TSharedPtr<ICefInterfaceContainerWalker>& Retval)
  32. {
  33. switch (Container->GetType(Key))
  34. {
  35. case VTYPE_NULL:
  36. case VTYPE_BOOL:
  37. case VTYPE_INT:
  38. case VTYPE_DOUBLE:
  39. case VTYPE_STRING:
  40. OutToken = EStructDeserializerBackendTokens::Property;
  41. break;
  42. case VTYPE_DICTIONARY:
  43. {
  44. CefRefPtr<CefDictionaryValue> Dictionary = Container->GetDictionary(Key);
  45. if (Dictionary->GetType("$type") == VTYPE_STRING )
  46. {
  47. OutToken = EStructDeserializerBackendTokens::Property;
  48. }
  49. else
  50. {
  51. TSharedPtr<ICefInterfaceContainerWalker> NewWalker(new FCefInterfaceDictionaryValueWalker(Retval, Dictionary));
  52. Retval = NewWalker->GetNextToken(OutToken, PropertyName);
  53. }
  54. break;
  55. }
  56. case VTYPE_LIST:
  57. {
  58. TSharedPtr<ICefInterfaceContainerWalker> NewWalker(new FCefInterfaceListValueWalker(Retval, Container->GetList(Key)));
  59. Retval = NewWalker->GetNextToken(OutToken, PropertyName);
  60. break;
  61. }
  62. case VTYPE_BINARY:
  63. case VTYPE_INVALID:
  64. default:
  65. OutToken = EStructDeserializerBackendTokens::Error;
  66. break;
  67. }
  68. }
  69. /**
  70. * Gets a pointer to object of the given property.
  71. *
  72. * @param Property The property to get.
  73. * @param Outer The property that contains the property to be get, if any.
  74. * @param Data A pointer to the memory holding the property's data.
  75. * @param ArrayIndex The index of the element to set (if the property is an array).
  76. * @return A pointer to the object represented by the property, null otherwise..
  77. * @see ClearPropertyValue
  78. */
  79. void* GetPropertyValuePtr( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex )
  80. {
  81. check(Property);
  82. if (FArrayProperty* ArrayProperty = CastField<FArrayProperty>(Outer))
  83. {
  84. if (ArrayProperty->Inner != Property)
  85. {
  86. return nullptr;
  87. }
  88. FScriptArrayHelper ArrayHelper(ArrayProperty, ArrayProperty->template ContainerPtrToValuePtr<void>(Data));
  89. int32 Index = ArrayHelper.AddValue();
  90. return ArrayHelper.GetRawPtr(Index);
  91. }
  92. if (ArrayIndex >= Property->ArrayDim)
  93. {
  94. return nullptr;
  95. }
  96. return Property->template ContainerPtrToValuePtr<void>(Data, ArrayIndex);
  97. }
  98. /**
  99. * Sets the value of the given property.
  100. *
  101. * @param Property The property to set.
  102. * @param Outer The property that contains the property to be set, if any.
  103. * @param Data A pointer to the memory holding the property's data.
  104. * @param ArrayIndex The index of the element to set (if the property is an array).
  105. * @return true on success, false otherwise.
  106. * @see ClearPropertyValue
  107. */
  108. template<typename PropertyType, typename ValueType>
  109. bool SetPropertyValue( PropertyType* Property, FProperty* Outer, void* Data, int32 ArrayIndex, const ValueType& Value )
  110. {
  111. if (void* Ptr = GetPropertyValuePtr(Property, Outer, Data, ArrayIndex))
  112. {
  113. *(ValueType*)Ptr = Value;
  114. return true;
  115. }
  116. return false;
  117. }
  118. template<typename PropertyType, typename ContainerType, typename KeyType>
  119. bool ReadNumericProperty(FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
  120. {
  121. typedef typename PropertyType::TCppType TCppType;
  122. if (PropertyType* TypedProperty = CastField<PropertyType>(Property))
  123. {
  124. return SetPropertyValue(TypedProperty, Outer, Data, ArrayIndex, GetNumeric<TCppType>(Container, Key));
  125. }
  126. else
  127. {
  128. return false;
  129. }
  130. }
  131. template<typename ContainerType, typename KeyType>
  132. bool ReadBoolProperty(FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
  133. {
  134. if (FBoolProperty* BoolProperty = CastField<FBoolProperty>(Property))
  135. {
  136. return SetPropertyValue(BoolProperty, Outer, Data, ArrayIndex, GetNumeric<int>(Container, Key)!=0);
  137. }
  138. return false;
  139. }
  140. template<typename ContainerType, typename KeyType>
  141. bool ReadJSFunctionProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
  142. {
  143. if (Container->GetType(Key) != VTYPE_DICTIONARY || !Property->IsA<FStructProperty>())
  144. {
  145. return false;
  146. }
  147. CefRefPtr<CefDictionaryValue> Dictionary = Container->GetDictionary(Key);
  148. FStructProperty* StructProperty = CastField<FStructProperty>(Property);
  149. if ( !StructProperty || StructProperty->Struct != FWebInterfaceJSFunction::StaticStruct())
  150. {
  151. return false;
  152. }
  153. FGuid CallbackID;
  154. if (!FGuid::Parse(FString(WCHAR_TO_TCHAR(Dictionary->GetString("$id").ToWString().c_str())), CallbackID))
  155. {
  156. // Invalid GUID
  157. return false;
  158. }
  159. FWebInterfaceJSFunction CallbackObject(Scripting, CallbackID);
  160. return SetPropertyValue(StructProperty, Outer, Data, ArrayIndex, CallbackObject);
  161. }
  162. template<typename ContainerType, typename KeyType>
  163. bool ReadStringProperty(FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
  164. {
  165. if (Container->GetType(Key) == VTYPE_STRING)
  166. {
  167. FString StringValue = WCHAR_TO_TCHAR(Container->GetString(Key).ToWString().c_str());
  168. if (FStrProperty* StrProperty = CastField<FStrProperty>(Property))
  169. {
  170. return SetPropertyValue(StrProperty, Outer, Data, ArrayIndex, StringValue);
  171. }
  172. if (FNameProperty* NameProperty = CastField<FNameProperty>(Property))
  173. {
  174. return SetPropertyValue(NameProperty, Outer, Data, ArrayIndex, FName(*StringValue));
  175. }
  176. if (FTextProperty* TextProperty = CastField<FTextProperty>(Property))
  177. {
  178. return SetPropertyValue(TextProperty, Outer, Data, ArrayIndex, FText::FromString(StringValue));
  179. }
  180. if (FByteProperty* ByteProperty = CastField<FByteProperty>(Property))
  181. {
  182. if (!ByteProperty->Enum)
  183. {
  184. return false;
  185. }
  186. int32 Index = ByteProperty->Enum->GetIndexByNameString(StringValue);
  187. if (Index == INDEX_NONE)
  188. {
  189. return false;
  190. }
  191. return SetPropertyValue(ByteProperty, Outer, Data, ArrayIndex, (uint8)ByteProperty->Enum->GetValueByIndex(Index));
  192. }
  193. if (FEnumProperty* EnumProperty = CastField<FEnumProperty>(Property))
  194. {
  195. int32 Index = EnumProperty->GetEnum()->GetIndexByNameString(StringValue);
  196. if (Index == INDEX_NONE)
  197. {
  198. return false;
  199. }
  200. if (void* ElementPtr = GetPropertyValuePtr(EnumProperty, Outer, Data, ArrayIndex))
  201. {
  202. EnumProperty->GetUnderlyingProperty()->SetIntPropertyValue(ElementPtr, EnumProperty->GetEnum()->GetValueByIndex(Index));
  203. return true;
  204. }
  205. return false;
  206. }
  207. }
  208. return false;
  209. }
  210. template<typename ContainerType, typename KeyType>
  211. bool ReadProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex, CefRefPtr<ContainerType> Container, KeyType Key )
  212. {
  213. return ReadBoolProperty(Property, Outer, Data, ArrayIndex, Container, Key)
  214. || ReadStringProperty(Property, Outer, Data, ArrayIndex, Container, Key)
  215. || ReadNumericProperty<FByteProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
  216. || ReadNumericProperty<FInt8Property>(Property, Outer, Data, ArrayIndex, Container, Key)
  217. || ReadNumericProperty<FInt16Property>(Property, Outer, Data, ArrayIndex, Container, Key)
  218. || ReadNumericProperty<FIntProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
  219. || ReadNumericProperty<FInt64Property>(Property, Outer, Data, ArrayIndex, Container, Key)
  220. || ReadNumericProperty<FUInt16Property>(Property, Outer, Data, ArrayIndex, Container, Key)
  221. || ReadNumericProperty<FUInt32Property>(Property, Outer, Data, ArrayIndex, Container, Key)
  222. || ReadNumericProperty<FUInt64Property>(Property, Outer, Data, ArrayIndex, Container, Key)
  223. || ReadNumericProperty<FFloatProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
  224. || ReadNumericProperty<FDoubleProperty>(Property, Outer, Data, ArrayIndex, Container, Key)
  225. || ReadJSFunctionProperty(Scripting, Property, Outer, Data, ArrayIndex, Container, Key);
  226. }
  227. }
  228. TSharedPtr<ICefInterfaceContainerWalker> FCefInterfaceListValueWalker::GetNextToken(EStructDeserializerBackendTokens& OutToken, FString& PropertyName)
  229. {
  230. TSharedPtr<ICefInterfaceContainerWalker> Retval = SharedThis(this);
  231. Index++;
  232. if (Index == -1)
  233. {
  234. OutToken = EStructDeserializerBackendTokens::ArrayStart;
  235. }
  236. else if ( Index < List->GetSize() )
  237. {
  238. AssignTokenFromContainer(List, Index, OutToken, PropertyName, Retval);
  239. PropertyName = FString();
  240. }
  241. else
  242. {
  243. OutToken = EStructDeserializerBackendTokens::ArrayEnd;
  244. Retval = Parent;
  245. }
  246. return Retval;
  247. }
  248. bool FCefInterfaceListValueWalker::ReadProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex)
  249. {
  250. return ::ReadProperty(Scripting, Property, Outer, Data, ArrayIndex, List, Index);
  251. }
  252. TSharedPtr<ICefInterfaceContainerWalker> FCefInterfaceDictionaryValueWalker::GetNextToken(EStructDeserializerBackendTokens& OutToken, FString& PropertyName)
  253. {
  254. TSharedPtr<ICefInterfaceContainerWalker> Retval = SharedThis(this);
  255. Index++;
  256. if (Index == -1)
  257. {
  258. OutToken = EStructDeserializerBackendTokens::StructureStart;
  259. }
  260. else if ( Index < Keys.size() )
  261. {
  262. AssignTokenFromContainer(Dictionary, Keys[Index], OutToken, PropertyName, Retval);
  263. PropertyName = WCHAR_TO_TCHAR(Keys[Index].ToWString().c_str());
  264. }
  265. else
  266. {
  267. OutToken = EStructDeserializerBackendTokens::StructureEnd;
  268. Retval = Parent;
  269. }
  270. return Retval;
  271. }
  272. bool FCefInterfaceDictionaryValueWalker::ReadProperty(TSharedPtr<FCEFInterfaceJSScripting> Scripting, FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex)
  273. {
  274. return ::ReadProperty(Scripting, Property, Outer, Data, ArrayIndex, Dictionary, Keys[Index]);
  275. }
  276. /* IStructDeserializerBackend interface
  277. *****************************************************************************/
  278. const FString& FCEFInterfaceJSStructDeserializerBackend::GetCurrentPropertyName() const
  279. {
  280. return CurrentPropertyName;
  281. }
  282. FString FCEFInterfaceJSStructDeserializerBackend::GetDebugString() const
  283. {
  284. return CurrentPropertyName;
  285. }
  286. const FString& FCEFInterfaceJSStructDeserializerBackend::GetLastErrorMessage() const
  287. {
  288. return CurrentPropertyName;
  289. }
  290. bool FCEFInterfaceJSStructDeserializerBackend::GetNextToken( EStructDeserializerBackendTokens& OutToken )
  291. {
  292. if (Walker.IsValid())
  293. {
  294. Walker = Walker->GetNextToken(OutToken, CurrentPropertyName);
  295. return true;
  296. }
  297. else
  298. {
  299. return false;
  300. }
  301. }
  302. bool FCEFInterfaceJSStructDeserializerBackend::ReadProperty( FProperty* Property, FProperty* Outer, void* Data, int32 ArrayIndex )
  303. {
  304. return Walker->ReadProperty(Scripting, Property, Outer, Data, ArrayIndex);
  305. }
  306. void FCEFInterfaceJSStructDeserializerBackend::SkipArray()
  307. {
  308. EStructDeserializerBackendTokens Token;
  309. int32 depth = 1;
  310. while (GetNextToken(Token) && depth > 0)
  311. {
  312. switch (Token)
  313. {
  314. case EStructDeserializerBackendTokens::ArrayEnd:
  315. depth --;
  316. break;
  317. case EStructDeserializerBackendTokens::ArrayStart:
  318. depth ++;
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. }
  325. void FCEFInterfaceJSStructDeserializerBackend::SkipStructure()
  326. {
  327. EStructDeserializerBackendTokens Token;
  328. int32 depth = 1;
  329. while (GetNextToken(Token) && depth > 0)
  330. {
  331. switch (Token)
  332. {
  333. case EStructDeserializerBackendTokens::StructureEnd:
  334. depth --;
  335. break;
  336. case EStructDeserializerBackendTokens::StructureStart:
  337. depth ++;
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. }
  344. #endif