CEFInterfaceJSScripting.h 5.2 KB

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