WebInterfaceJSScripting.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Engine/Source/Runtime/WebBrowser/Private/WebJSScripting.h
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Misc/Guid.h"
  5. #include "WebInterfaceJSFunction.h"
  6. #include "UObject/GCObject.h"
  7. class Error;
  8. /**
  9. * Implements handling of bridging UObjects client side with JavaScript renderer side.
  10. */
  11. class FWebInterfaceJSScripting
  12. : public FGCObject
  13. {
  14. public:
  15. FWebInterfaceJSScripting(bool bInJSBindingToLoweringEnabled)
  16. : BaseGuid(FGuid::NewGuid())
  17. , bJSBindingToLoweringEnabled(bInJSBindingToLoweringEnabled)
  18. {}
  19. virtual void BindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true) =0;
  20. virtual void UnbindUObject(const FString& Name, UObject* Object = nullptr, bool bIsPermanent = true) =0;
  21. virtual void InvokeJSFunction(FGuid FunctionId, int32 ArgCount, FWebInterfaceJSParam Arguments[], bool bIsError=false) =0;
  22. virtual void InvokeJSErrorResult(FGuid FunctionId, const FString& Error) =0;
  23. FString GetBindingName(const FString& Name, UObject* Object) const
  24. {
  25. return bJSBindingToLoweringEnabled ? Name.ToLower() : Name;
  26. }
  27. FString GetBindingName(const FFieldVariant& Property) const
  28. {
  29. return bJSBindingToLoweringEnabled ? Property.GetName().ToLower() : Property.GetName();
  30. }
  31. public:
  32. // FGCObject API
  33. virtual void AddReferencedObjects( FReferenceCollector& Collector ) override
  34. {
  35. // Ensure bound UObjects are not garbage collected as long as this object is valid.
  36. for (auto& Binding : BoundObjects)
  37. {
  38. Collector.AddReferencedObject(Binding.Key);
  39. }
  40. }
  41. virtual FString GetReferencerName() const override
  42. {
  43. return TEXT("FWebInterfaceJSScripting");
  44. }
  45. protected:
  46. // Creates a reversible memory addres -> psuedo-guid mapping.
  47. // This is done by xoring the address with the first 64 bits of a base guid owned by the instance.
  48. // Used to identify UObjects from the render process withough exposing internal pointers.
  49. FGuid PtrToGuid(UObject* Ptr)
  50. {
  51. FGuid Guid = BaseGuid;
  52. if (Ptr == nullptr)
  53. {
  54. Guid.Invalidate();
  55. }
  56. else
  57. {
  58. UPTRINT IntPtr = reinterpret_cast<UPTRINT>(Ptr);
  59. if (sizeof(UPTRINT) > 4)
  60. {
  61. Guid[0] ^= (static_cast<uint64>(IntPtr) >> 32);
  62. }
  63. Guid[1] ^= IntPtr & 0xFFFFFFFF;
  64. }
  65. return Guid;
  66. }
  67. // In addition to reversing the mapping, it verifies that we are currently holding on to an instance of that UObject
  68. UObject* GuidToPtr(const FGuid& Guid)
  69. {
  70. UPTRINT IntPtr = 0;
  71. if (sizeof(UPTRINT) > 4)
  72. {
  73. IntPtr = static_cast<UPTRINT>(static_cast<uint64>(Guid[0] ^ BaseGuid[0]) << 32);
  74. }
  75. IntPtr |= (Guid[1] ^ BaseGuid[1]) & 0xFFFFFFFF;
  76. UObject* Result = reinterpret_cast<UObject*>(IntPtr);
  77. if (BoundObjects.Contains(Result))
  78. {
  79. return Result;
  80. }
  81. else
  82. {
  83. return nullptr;
  84. }
  85. }
  86. void RetainBinding(UObject* Object)
  87. {
  88. if (BoundObjects.Contains(Object))
  89. {
  90. if(!BoundObjects[Object].bIsPermanent)
  91. {
  92. BoundObjects[Object].Refcount++;
  93. }
  94. }
  95. else
  96. {
  97. BoundObjects.Add(Object, {false, 1});
  98. }
  99. }
  100. void ReleaseBinding(UObject* Object)
  101. {
  102. if (BoundObjects.Contains(Object))
  103. {
  104. auto& Binding = BoundObjects[Object];
  105. if(!Binding.bIsPermanent)
  106. {
  107. Binding.Refcount--;
  108. if (Binding.Refcount <= 0)
  109. {
  110. BoundObjects.Remove(Object);
  111. }
  112. }
  113. }
  114. }
  115. struct ObjectBinding
  116. {
  117. bool bIsPermanent;
  118. int32 Refcount;
  119. };
  120. /** Private data */
  121. FGuid BaseGuid;
  122. /** UObjects currently visible on the renderer side. */
  123. TMap<UObject*, ObjectBinding> BoundObjects;
  124. /** Reverse lookup for permanent bindings */
  125. TMap<FString, UObject*> PermanentUObjectsByName;
  126. /** The to-lowering option enable for the binding names. */
  127. const bool bJSBindingToLoweringEnabled;
  128. };