SWebInterfaceBrowserView.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // Engine/Source/Runtime/WebBrowser/Public/SWebBrowserView.h
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Widgets/DeclarativeSyntaxSupport.h"
  5. #include "Input/PopupMethodReply.h"
  6. #include "Widgets/SWidget.h"
  7. #include "Widgets/SCompoundWidget.h"
  8. #include "Framework/Application/IMenu.h"
  9. #include "Framework/SlateDelegates.h"
  10. #include "Widgets/SViewport.h"
  11. #include "IWebInterfaceBrowserSingleton.h"
  12. class FWebInterfaceBrowserViewport;
  13. class IWebInterfaceBrowserAdapter;
  14. class IWebInterfaceBrowserDialog;
  15. class IWebInterfaceBrowserPopupFeatures;
  16. class IWebInterfaceBrowserWindow;
  17. struct FWebNavigationRequest;
  18. enum class EWebInterfaceBrowserDialogEventResponse;
  19. enum class EWebInterfaceBrowserDocumentState;
  20. enum class EWebInterfaceBrowserConsoleLogSeverity;
  21. DECLARE_DELEGATE_RetVal_TwoParams(bool, FOnBeforePopupDelegate, FString, FString);
  22. DECLARE_DELEGATE_RetVal_TwoParams(bool, FOnCreateWindowDelegate, const TWeakPtr<IWebInterfaceBrowserWindow>&, const TWeakPtr<IWebInterfaceBrowserPopupFeatures>&);
  23. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnCloseWindowDelegate, const TWeakPtr<IWebInterfaceBrowserWindow>&);
  24. DECLARE_DELEGATE_RetVal_OneParam(TSharedPtr<IToolTip>, FOnCreateToolTip, const FText&);
  25. DECLARE_DELEGATE_FourParams(FOnConsoleMessageDelegate, const FString& /*Message*/, const FString& /*Source*/, int32 /*Line*/, EWebInterfaceBrowserConsoleLogSeverity /*Severity*/);
  26. #if WITH_CEF3
  27. typedef SViewport SWebInterfaceBrowserWidget;
  28. #else
  29. typedef SWidget SWebInterfaceBrowserWidget;
  30. #endif
  31. class WEBBROWSERUI_API SWebInterfaceBrowserView
  32. : public SCompoundWidget
  33. {
  34. public:
  35. DECLARE_DELEGATE_RetVal_TwoParams(bool, FOnBeforeBrowse, const FString& /*Url*/, const FWebNavigationRequest& /*Request*/)
  36. DECLARE_DELEGATE_RetVal_ThreeParams(bool, FOnLoadUrl, const FString& /*Method*/, const FString& /*Url*/, FString& /* Response */)
  37. DECLARE_DELEGATE_RetVal_OneParam(EWebInterfaceBrowserDialogEventResponse, FOnShowDialog, const TWeakPtr<IWebInterfaceBrowserDialog>&);
  38. DECLARE_DELEGATE_RetVal(bool, FOnSuppressContextMenu);
  39. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnDragWindow, const FPointerEvent& /* MouseEvent */);
  40. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnUnhandledKeyDown, const FKeyEvent& /*KeyEvent*/);
  41. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnUnhandledKeyUp, const FKeyEvent& /*KeyEvent*/);
  42. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnUnhandledKeyChar, const FCharacterEvent& /*CharacterEvent*/);
  43. SLATE_BEGIN_ARGS(SWebInterfaceBrowserView)
  44. : _InitialURL(TEXT("https://www.google.com"))
  45. , _ShowErrorMessage(true)
  46. , _SupportsTransparency(false)
  47. , _InterceptLoadRequests(false)
  48. , _SupportsThumbMouseButtonNavigation(true)
  49. , _BackgroundColor(255,255,255,255)
  50. , _BrowserFrameRate(24)
  51. , _PopupMenuMethod(TOptional<EPopupMethod>())
  52. , _ContextSettings()
  53. , _AltRetryDomains(TArray<FString>())
  54. , _ViewportSize(FVector2D::ZeroVector)
  55. { }
  56. /** A reference to the parent window. */
  57. SLATE_ARGUMENT(TSharedPtr<SWindow>, ParentWindow)
  58. /** URL that the browser will initially navigate to. */
  59. SLATE_ARGUMENT(FString, InitialURL)
  60. /** Optional string to load contents as a web page. */
  61. SLATE_ARGUMENT(TOptional<FString>, ContentsToLoad)
  62. /** Whether to show an error message in case of loading errors. */
  63. SLATE_ARGUMENT(bool, ShowErrorMessage)
  64. /** Should this browser window support transparency. */
  65. SLATE_ARGUMENT(bool, SupportsTransparency)
  66. /** Should this browser window intercept resource loading requests. If false the BrowserContext will instead. */
  67. SLATE_ARGUMENT(bool, InterceptLoadRequests)
  68. /** Whether to allow forward and back navigation via the mouse thumb buttons. */
  69. SLATE_ARGUMENT(bool, SupportsThumbMouseButtonNavigation)
  70. /** Opaque background color used before a document is loaded and when no document color is specified. */
  71. SLATE_ARGUMENT(FColor, BackgroundColor)
  72. /** The frames per second rate that the browser will attempt to use. */
  73. SLATE_ARGUMENT(int, BrowserFrameRate)
  74. /** Override the popup menu method used for popup menus. If not set, parent widgets will be queried instead. */
  75. SLATE_ARGUMENT(TOptional<EPopupMethod>, PopupMenuMethod)
  76. /** Override the default global context settings for this specific window. If not set, the global default will be used. */
  77. SLATE_ARGUMENT(TOptional<FInterfaceBrowserContextSettings>, ContextSettings)
  78. /** Domains to retry if original domain cannot connect. */
  79. SLATE_ARGUMENT(TArray<FString>, AltRetryDomains)
  80. /** Desired size of the web browser viewport. */
  81. SLATE_ATTRIBUTE(FVector2D, ViewportSize);
  82. /** Called when document loading completed. */
  83. SLATE_EVENT(FSimpleDelegate, OnLoadCompleted)
  84. /** Called when document loading failed. */
  85. SLATE_EVENT(FSimpleDelegate, OnLoadError)
  86. /** Called when document loading started. */
  87. SLATE_EVENT(FSimpleDelegate, OnLoadStarted)
  88. /** Called when document title changed. */
  89. SLATE_EVENT(FOnTextChanged, OnTitleChanged)
  90. /** Called when the Url changes. */
  91. SLATE_EVENT(FOnTextChanged, OnUrlChanged)
  92. /** Called before a popup window happens */
  93. SLATE_EVENT(FOnBeforePopupDelegate, OnBeforePopup)
  94. /** Called when the browser requests the creation of a new window */
  95. SLATE_EVENT(FOnCreateWindowDelegate, OnCreateWindow)
  96. /** Called when a browser window close event is detected */
  97. SLATE_EVENT(FOnCloseWindowDelegate, OnCloseWindow)
  98. /** Called before browser navigation. */
  99. SLATE_EVENT(FOnBeforeBrowse, OnBeforeNavigation)
  100. /** Called to allow bypassing page content on load. */
  101. SLATE_EVENT(FOnLoadUrl, OnLoadUrl)
  102. /** Called when the browser needs to show a dialog to the user. */
  103. SLATE_EVENT(FOnShowDialog, OnShowDialog)
  104. /** Called to dismiss any dialogs shown via OnShowDialog. */
  105. SLATE_EVENT(FSimpleDelegate, OnDismissAllDialogs)
  106. /** Called to allow supression of the browser context menu. */
  107. SLATE_EVENT(FOnSuppressContextMenu, OnSuppressContextMenu);
  108. /** Called to allow overriding of ToolTip widget construction. */
  109. SLATE_EVENT(FOnCreateToolTip, OnCreateToolTip)
  110. /** Called when drag is detected in a web page area tagged as a drag region. */
  111. SLATE_EVENT(FOnDragWindow, OnDragWindow)
  112. /** Called to allow the handling of any key down events not handled by the browser. */
  113. SLATE_EVENT(FOnUnhandledKeyDown, OnUnhandledKeyDown)
  114. /** Called to allow the handling of any key up events not handled by the browser. */
  115. SLATE_EVENT(FOnUnhandledKeyUp, OnUnhandledKeyUp)
  116. /** Called to allow the handling of any key char events not handled by the browser. */
  117. SLATE_EVENT(FOnUnhandledKeyChar, OnUnhandledKeyChar)
  118. /** Called for each console message */
  119. SLATE_EVENT(FOnConsoleMessageDelegate, OnConsoleMessage)
  120. SLATE_END_ARGS()
  121. /** Default constructor. */
  122. SWebInterfaceBrowserView();
  123. ~SWebInterfaceBrowserView();
  124. virtual bool SupportsKeyboardFocus() const override {return true;}
  125. /**
  126. * Construct the widget.
  127. *
  128. * @param InArgs Declaration from which to construct the widget.
  129. */
  130. void Construct(const FArguments& InArgs, const TSharedPtr<IWebInterfaceBrowserWindow>& InWebBrowserWindow = nullptr);
  131. virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;
  132. /**
  133. * Load the specified URL.
  134. *
  135. * @param NewURL New URL to load.
  136. */
  137. void LoadURL(FString NewURL);
  138. /**
  139. * Load a string as data to create a web page.
  140. *
  141. * @param Contents String to load.
  142. * @param DummyURL Dummy URL for the page.
  143. */
  144. void LoadString(FString Contents, FString DummyURL);
  145. /** Reload the current page. */
  146. void Reload();
  147. /** Stop loading the page. */
  148. void StopLoad();
  149. /** Get the current title of the web page. */
  150. FText GetTitleText() const;
  151. /**
  152. * Gets the currently loaded URL.
  153. *
  154. * @return The URL, or empty string if no document is loaded.
  155. */
  156. FString GetUrl() const;
  157. /**
  158. * Gets the URL that appears in the address bar, this may not be the URL that is currently loaded in the frame.
  159. *
  160. * @return The address bar URL.
  161. */
  162. FText GetAddressBarUrlText() const;
  163. /** Whether the document finished loading. */
  164. bool IsLoaded() const;
  165. /** Whether the document is currently being loaded. */
  166. bool IsLoading() const;
  167. /** Whether the browser widget is done initializing. */
  168. bool IsInitialized() const;
  169. /** Execute javascript on the current window */
  170. void ExecuteJavascript(const FString& ScriptText);
  171. /**
  172. * Gets the source of the main frame as raw HTML.
  173. *
  174. * This method has to be called asynchronously by passing a callback function, which will be called at a later point when the
  175. * result is ready.
  176. * @param Callback A callable that takes a single string reference for handling the result.
  177. */
  178. void GetSource(TFunction<void (const FString&)> Callback) const ;
  179. /**
  180. * Expose a UObject instance to the browser runtime.
  181. * Properties and Functions will be accessible from JavaScript side.
  182. * As all communication with the rendering procesis asynchronous, return values (both for properties and function results) are wrapped into JS Future objects.
  183. *
  184. * @param Name The name of the object. The object will show up as window.ue.{Name} on the javascript side. If there is an existing object of the same name, this object will replace it. If bIsPermanent is false and there is an existing permanent binding, the permanent binding will be restored when the temporary one is removed.
  185. * @param Object The object instance.
  186. * @param bIsPermanent If true, the object will be visible to all pages loaded through this browser widget, otherwise, it will be deleted when navigating away from the current page. Non-permanent bindings should be registered from inside an OnLoadStarted event handler in order to be available before JS code starts loading.
  187. */
  188. void BindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true);
  189. /**
  190. * Remove an existing script binding registered by BindUObject.
  191. *
  192. * @param Name The name of the object to remove.
  193. * @param Object The object will only be removed if it is the same object as the one passed in.
  194. * @param bIsPermanent Must match the bIsPermanent argument passed to BindUObject.
  195. */
  196. void UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true);
  197. void BindAdapter(const TSharedRef<IWebInterfaceBrowserAdapter>& Adapter);
  198. void UnbindAdapter(const TSharedRef<IWebInterfaceBrowserAdapter>& Adapter);
  199. void BindInputMethodSystem(ITextInputMethodSystem* TextInputMethodSystem);
  200. void UnbindInputMethodSystem();
  201. /** Returns true if the browser can navigate backwards. */
  202. bool CanGoBack() const;
  203. /** Navigate backwards. */
  204. void GoBack();
  205. /** Returns true if the browser can navigate forwards. */
  206. bool CanGoForward() const;
  207. /** Navigate forwards. */
  208. void GoForward();
  209. /** Set parent SWindow for this browser. */
  210. void SetParentWindow(TSharedPtr<SWindow> Window);
  211. /** Update the underlying browser widget to match the KB focus in slate.
  212. This is used to work around a CEF bug that loses focus state on navigations*/
  213. void SetBrowserKeyboardFocus();
  214. /** Close the underlying browser object before we destruct this view.
  215. This will block until that object is fully destroyed.
  216. Calling this is optional, CEF has object lifetime requirements that mean on shutdown you must destroy browsers before exit.*/
  217. void CloseBrowser();
  218. private:
  219. void SetupParentWindowHandlers();
  220. /** Callback for document loading state changes. */
  221. void HandleBrowserWindowDocumentStateChanged(EWebInterfaceBrowserDocumentState NewState);
  222. /** Callback to tell slate we want to update the contents of the web view based on changes inside the view. */
  223. void HandleBrowserWindowNeedsRedraw();
  224. /** Callback for document title changes. */
  225. void HandleTitleChanged(FString NewTitle);
  226. /** Callback for loaded url changes. */
  227. void HandleUrlChanged(FString NewUrl);
  228. /** Callback for showing browser tool tips. */
  229. void HandleToolTip(FString ToolTipText);
  230. /**
  231. * A delegate that is executed prior to browser navigation.
  232. *
  233. * @return true if the navigation was handled an no further action should be taken by the browser, false if the browser should handle.
  234. */
  235. bool HandleBeforeNavigation(const FString& Url, const FWebNavigationRequest& Request);
  236. bool HandleLoadUrl(const FString& Method, const FString& Url, FString& OutResponse);
  237. /**
  238. * A delegate that is executed when the browser requests window creation.
  239. *
  240. * @return true if if the window request was handled, false if the browser requesting the new window should be closed.
  241. */
  242. bool HandleCreateWindow(const TWeakPtr<IWebInterfaceBrowserWindow>& NewBrowserWindow, const TWeakPtr<IWebInterfaceBrowserPopupFeatures>& PopupFeatures);
  243. /**
  244. * A delegate that is executed when closing the browser window.
  245. *
  246. * @return true if if the window close was handled, false otherwise.
  247. */
  248. bool HandleCloseWindow(const TWeakPtr<IWebInterfaceBrowserWindow>& BrowserWindow);
  249. /** Callback for showing dialogs to the user */
  250. EWebInterfaceBrowserDialogEventResponse HandleShowDialog(const TWeakPtr<IWebInterfaceBrowserDialog>& DialogParams);
  251. /** Callback for dismissing any dialogs previously shown */
  252. void HandleDismissAllDialogs();
  253. /** Callback for popup window permission */
  254. bool HandleBeforePopup(FString URL, FString Target);
  255. /** Callback for showing a popup menu */
  256. void HandleShowPopup(const FIntRect& PopupSize);
  257. /** Callback for hiding the popup menu */
  258. void HandleDismissPopup();
  259. /** Callback from the popup menu notifiying it has been dismissed */
  260. void HandleMenuDismissed(TSharedRef<IMenu>);
  261. virtual FPopupMethodReply OnQueryPopupMethod() const override
  262. {
  263. return PopupMenuMethod.IsSet()
  264. ? FPopupMethodReply::UseMethod(PopupMenuMethod.GetValue())
  265. : FPopupMethodReply::Unhandled();
  266. }
  267. void HandleWindowDeactivated();
  268. void HandleWindowActivated();
  269. bool UnhandledKeyDown(const FKeyEvent& KeyEvent);
  270. bool UnhandledKeyUp(const FKeyEvent& KeyEvent);
  271. bool UnhandledKeyChar(const FCharacterEvent& CharacterEvent);
  272. bool HandleDrag(const FPointerEvent& MouseEvent);
  273. void HandleConsoleMessage(const FString& Message, const FString& Source, int32 Line, EWebInterfaceBrowserConsoleLogSeverity Serverity);
  274. TOptional<FSlateRenderTransform> GetPopupRenderTransform() const;
  275. private:
  276. /** Interface for dealing with a web browser window. */
  277. TSharedPtr<IWebInterfaceBrowserWindow> BrowserWindow;
  278. /** The slate window that contains this widget. This must be stored weak otherwise we create a circular reference. */
  279. mutable TWeakPtr<SWindow> SlateParentWindowPtr;
  280. /** Viewport interface for rendering the web page. */
  281. TSharedPtr<FWebInterfaceBrowserViewport> BrowserViewport;
  282. /** Viewport interface for rendering popup menus. */
  283. TSharedPtr<FWebInterfaceBrowserViewport> MenuViewport;
  284. /** The implementation dependent widget that renders the browser contents. */
  285. TSharedPtr<SWebInterfaceBrowserWidget> BrowserWidget;
  286. TArray<TSharedRef<IWebInterfaceBrowserAdapter>> Adapters;
  287. /**
  288. * An interface pointer to a menu object presenting a popup.
  289. * Pointer is null when a popup is not visible.
  290. */
  291. TWeakPtr<IMenu> PopupMenuPtr;
  292. /** Can be set to override the popup menu method used for popup menus. If not set, parent widgets will be queried instead. */
  293. TOptional<EPopupMethod> PopupMenuMethod;
  294. /** The url that appears in the address bar which can differ from the url of the loaded page */
  295. FText AddressBarUrl;
  296. /** A delegate that is invoked when document loading completed. */
  297. FSimpleDelegate OnLoadCompleted;
  298. /** A delegate that is invoked when document loading failed. */
  299. FSimpleDelegate OnLoadError;
  300. /** A delegate that is invoked when document loading started. */
  301. FSimpleDelegate OnLoadStarted;
  302. /** A delegate that is invoked when document title changed. */
  303. FOnTextChanged OnTitleChanged;
  304. /** A delegate that is invoked when document address changed. */
  305. FOnTextChanged OnUrlChanged;
  306. /** A delegate that is invoked when the browser attempts to pop up a new window */
  307. FOnBeforePopupDelegate OnBeforePopup;
  308. /** A delegate that is invoked when the browser requests a UI window for another browser it spawned */
  309. FOnCreateWindowDelegate OnCreateWindow;
  310. /** A delegate that is invoked when a window close event is detected */
  311. FOnCloseWindowDelegate OnCloseWindow;
  312. /** A delegate that is invoked prior to browser navigation */
  313. FOnBeforeBrowse OnBeforeNavigation;
  314. /** A delegate that is invoked when loading a resource, allowing the application to provide contents directly */
  315. FOnLoadUrl OnLoadUrl;
  316. /** A delegate that is invoked when when the browser needs to present a dialog to the user */
  317. FOnShowDialog OnShowDialog;
  318. /** A delegate that is invoked when when the browser needs to dismiss all dialogs */
  319. FSimpleDelegate OnDismissAllDialogs;
  320. FOnSuppressContextMenu OnSuppressContextMenu;
  321. /** A delegate that is invoked when when the browser wishes to create a tooltip */
  322. FOnCreateToolTip OnCreateToolTip;
  323. /** A delegate that is invoked when the browser detects drag event in within drag region */
  324. FOnDragWindow OnDragWindow;
  325. /** A delegate for handling key down events not handled by browser. */
  326. FOnUnhandledKeyDown OnUnhandledKeyDown;
  327. /** A delegate for handling key up events not handled by browser. */
  328. FOnUnhandledKeyUp OnUnhandledKeyUp;
  329. /** A delegate for handling key char events not handled by browser. */
  330. FOnUnhandledKeyChar OnUnhandledKeyChar;
  331. /** A delegate that is invoked for each console message */
  332. FOnConsoleMessageDelegate OnConsoleMessage;
  333. protected:
  334. bool HandleSuppressContextMenu();
  335. };