WebInterfaceBrowserSingleton.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Engine/Source/Runtime/WebBrowser/Private/WebBrowserSingleton.h
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Containers/Ticker.h"
  5. #include "IWebInterfaceBrowserSingleton.h"
  6. #if WITH_CEF3
  7. #if PLATFORM_WINDOWS
  8. #include "Windows/WindowsHWrapper.h"
  9. #include "Windows/AllowWindowsPlatformTypes.h"
  10. #include "Windows/AllowWindowsPlatformAtomics.h"
  11. #endif
  12. #pragma push_macro("OVERRIDE")
  13. #undef OVERRIDE // cef headers provide their own OVERRIDE macro
  14. THIRD_PARTY_INCLUDES_START
  15. #if PLATFORM_APPLE
  16. PRAGMA_DISABLE_DEPRECATION_WARNINGS
  17. #endif
  18. #include "include/internal/cef_ptr.h"
  19. #include "include/cef_request_context.h"
  20. #if PLATFORM_APPLE
  21. PRAGMA_ENABLE_DEPRECATION_WARNINGS
  22. #endif
  23. THIRD_PARTY_INCLUDES_END
  24. #pragma pop_macro("OVERRIDE")
  25. #if PLATFORM_WINDOWS
  26. #include "Windows/HideWindowsPlatformAtomics.h"
  27. #include "Windows/HideWindowsPlatformTypes.h"
  28. #endif
  29. #include "CEF/CEFInterfaceSchemeHandler.h"
  30. #include "CEF/CEFInterfaceResourceContextHandler.h"
  31. class CefListValue;
  32. class FCEFInterfaceBrowserApp;
  33. class FCEFWebInterfaceBrowserWindow;
  34. #endif
  35. class IWebInterfaceBrowserCookieManager;
  36. class IWebInterfaceBrowserWindow;
  37. struct FWebInterfaceBrowserWindowInfo;
  38. struct FWebInterfaceBrowserInitSettings;
  39. class UMaterialInterface;
  40. PRAGMA_DISABLE_DEPRECATION_WARNINGS
  41. /**
  42. * Implementation of singleton class that takes care of general web browser tasks
  43. */
  44. class FWebInterfaceBrowserSingleton
  45. : public IWebInterfaceBrowserSingleton
  46. , public FTSTickerObjectBase
  47. {
  48. public:
  49. /** Constructor. */
  50. FWebInterfaceBrowserSingleton(const FWebInterfaceBrowserInitSettings& WebBrowserInitSettings);
  51. /** Virtual destructor. */
  52. virtual ~FWebInterfaceBrowserSingleton();
  53. /**
  54. * Gets the Current Locale Code in the format CEF expects
  55. *
  56. * @return Locale code as either "xx" or "xx-YY"
  57. */
  58. static FString GetCurrentLocaleCode();
  59. virtual FString ApplicationCacheDir() const override;
  60. public:
  61. // IWebBrowserSingleton Interface
  62. virtual TSharedRef<IWebInterfaceBrowserWindowFactory> GetWebBrowserWindowFactory() const override;
  63. TSharedPtr<IWebInterfaceBrowserWindow> CreateBrowserWindow(
  64. TSharedPtr<FCEFWebInterfaceBrowserWindow>& BrowserWindowParent,
  65. TSharedPtr<FWebInterfaceBrowserWindowInfo>& BrowserWindowInfo) override;
  66. TSharedPtr<IWebInterfaceBrowserWindow> CreateBrowserWindow(const FCreateInterfaceBrowserWindowSettings& Settings) override;
  67. #if BUILD_EMBEDDED_APP
  68. TSharedPtr<IWebInterfaceBrowserWindow> CreateNativeBrowserProxy() override;
  69. #endif
  70. virtual TSharedPtr<IWebInterfaceBrowserCookieManager> GetCookieManager() const override
  71. {
  72. return DefaultCookieManager;
  73. }
  74. virtual TSharedPtr<IWebInterfaceBrowserCookieManager> GetCookieManager(TOptional<FString> ContextId) const override;
  75. virtual bool RegisterContext(const FInterfaceBrowserContextSettings& Settings) override;
  76. virtual bool UnregisterContext(const FString& ContextId) override;
  77. virtual bool RegisterSchemeHandlerFactory(FString Scheme, FString Domain, IWebInterfaceBrowserSchemeHandlerFactory* WebBrowserSchemeHandlerFactory) override;
  78. virtual bool UnregisterSchemeHandlerFactory(IWebInterfaceBrowserSchemeHandlerFactory* WebBrowserSchemeHandlerFactory) override;
  79. virtual bool IsDevToolsShortcutEnabled() override
  80. {
  81. return bDevToolsShortcutEnabled;
  82. }
  83. virtual void SetDevToolsShortcutEnabled(bool Value) override
  84. {
  85. bDevToolsShortcutEnabled = Value;
  86. }
  87. virtual void SetJSBindingToLoweringEnabled(bool bEnabled) override
  88. {
  89. bJSBindingsToLoweringEnabled = bEnabled;
  90. }
  91. virtual void ClearOldCacheFolders(const FString& CachePathRoot, const FString& CachePrefix) override;
  92. /** Set a reference to UWebBrowser's default material*/
  93. virtual void SetDefaultMaterial(UMaterialInterface* InDefaultMaterial) override
  94. {
  95. DefaultMaterial = InDefaultMaterial;
  96. }
  97. /** Set a reference to UWebBrowser's translucent material*/
  98. virtual void SetDefaultTranslucentMaterial(UMaterialInterface* InDefaultMaterial) override
  99. {
  100. DefaultTranslucentMaterial = InDefaultMaterial;
  101. }
  102. /** Get a reference to UWebBrowser's default material*/
  103. virtual UMaterialInterface* GetDefaultMaterial() override
  104. {
  105. return DefaultMaterial;
  106. }
  107. /** Get a reference to UWebBrowser's translucent material*/
  108. virtual UMaterialInterface* GetDefaultTranslucentMaterial() override
  109. {
  110. return DefaultTranslucentMaterial;
  111. }
  112. public:
  113. // FTSTickerObjectBase Interface
  114. virtual bool Tick(float DeltaTime) override;
  115. #if WITH_CEF3
  116. /** Return true if this URL will support adding an Authorization header to it */
  117. bool URLRequestAllowsCredentials(const FString& URL);
  118. #endif
  119. private:
  120. TSharedPtr<IWebInterfaceBrowserCookieManager> DefaultCookieManager;
  121. #if WITH_CEF3
  122. /** When new render processes are created, send all permanent variable bindings to them. */
  123. void HandleRenderProcessCreated(CefRefPtr<CefListValue> ExtraInfo);
  124. /** Helper function to generate the CEF build unique name for the cache_path */
  125. FString GenerateWebCacheFolderName(const FString &InputPath);
  126. /** Helper function that blocks until the CEF task queue has processed a posted task, flushing the queue */
  127. void WaitForTaskQueueFlush();
  128. /** Pointer to the CEF App implementation */
  129. CefRefPtr<FCEFInterfaceBrowserApp> CEFBrowserApp;
  130. TMap<FString, CefRefPtr<CefRequestContext>> RequestContexts;
  131. TMap<FString, CefRefPtr<FCEFInterfaceResourceContextHandler>> RequestResourceHandlers;
  132. FCefInterfaceSchemeHandlerFactories SchemeHandlerFactories;
  133. bool bAllowCEF;
  134. bool bTaskFinished;
  135. #endif
  136. /** List of currently existing browser windows */
  137. #if WITH_CEF3
  138. TArray<TWeakPtr<FCEFWebInterfaceBrowserWindow>> WindowInterfaces;
  139. #endif
  140. /** Critical section for thread safe modification of WindowInterfaces array. */
  141. FCriticalSection WindowInterfacesCS;
  142. TSharedRef<IWebInterfaceBrowserWindowFactory> WebBrowserWindowFactory;
  143. bool bDevToolsShortcutEnabled;
  144. bool bJSBindingsToLoweringEnabled;
  145. bool bAppIsFocused;
  146. #if WITH_CEF3
  147. /** Did CEF successfully initialize itself */
  148. bool bCEFInitialized;
  149. #endif
  150. /** Reference to UWebBrowser's default material*/
  151. UMaterialInterface* DefaultMaterial;
  152. /** Reference to UWebBrowser's translucent material*/
  153. UMaterialInterface* DefaultTranslucentMaterial;
  154. };
  155. PRAGMA_ENABLE_DEPRECATION_WARNINGS
  156. #if WITH_CEF3
  157. class CefCookieManager;
  158. class FCefWebInterfaceCookieManagerFactory
  159. {
  160. public:
  161. static TSharedRef<IWebInterfaceBrowserCookieManager> Create(
  162. const CefRefPtr<CefCookieManager>& CookieManager);
  163. };
  164. #endif