IWebInterfaceBrowserWindow.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Engine/Source/Runtime/WebBrowser/Public/IWebBrowserWindow.h
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Input/CursorReply.h"
  5. #include "Input/Reply.h"
  6. #include "Widgets/SWindow.h"
  7. #include "SWebInterfaceBrowser.h"
  8. class Error;
  9. class FSlateShaderResource;
  10. class IWebInterfaceBrowserDialog;
  11. class IWebInterfaceBrowserPopupFeatures;
  12. enum class EWebInterfaceBrowserDialogEventResponse;
  13. enum class EWebInterfaceBrowserDocumentState
  14. {
  15. Completed,
  16. Error,
  17. Loading,
  18. NoDocument
  19. };
  20. enum class EWebInterfaceBrowserConsoleLogSeverity
  21. {
  22. Default,
  23. Verbose,
  24. Debug,
  25. Info,
  26. Warning,
  27. Error,
  28. Fatal
  29. };
  30. enum class EWebInterfaceTransitionSource
  31. {
  32. Unknown,
  33. /** Source is a link click or the JavaScript window.open function. */
  34. Link,
  35. /** Source is some other "explicit" navigation action such as creating a new browser or using the LoadURL function. */
  36. Explicit,
  37. /** Source is a subframe navigation. */
  38. AutoSubframe,
  39. /** Source is a subframe navigation explicitly requested by the user. */
  40. ManualSubframe,
  41. /** Source is a form submission by the user. */
  42. FormSubmit,
  43. /** Source is a "reload" of the page via the Reload function */
  44. Reload
  45. };
  46. enum class EWebInterfaceTransitionSourceQualifier
  47. {
  48. Unknown,
  49. /** Attempted to visit a URL but was blocked. */
  50. Blocked,
  51. /** Used the Forward or Back function to navigate among browsing history. */
  52. ForwardBack,
  53. /** The beginning of a navigation chain. */
  54. ChainStart,
  55. /** The last transition in a redirect chain. */
  56. ChainEnd,
  57. /** Redirects caused by JavaScript or a meta refresh tag on the page. */
  58. ClientRedirect,
  59. /** Used to test whether a transition involves a redirect. */
  60. ServerRedirect
  61. };
  62. struct FWebNavigationRequest
  63. {
  64. bool bIsRedirect;
  65. bool bIsMainFrame;
  66. bool bIsExplicitTransition;
  67. EWebInterfaceTransitionSource TransitionSource;
  68. EWebInterfaceTransitionSourceQualifier TransitionSourceQualifier;
  69. };
  70. /**
  71. * Interface for dealing with a Web Browser window
  72. */
  73. class IWebInterfaceBrowserWindow
  74. {
  75. public:
  76. /**
  77. * Load the specified URL
  78. *
  79. * @param NewURL New URL to load
  80. */
  81. virtual void LoadURL(FString NewURL) = 0;
  82. /**
  83. * Load a string as data to create a web page
  84. *
  85. * @param Contents String to load
  86. * @param DummyURL Dummy URL for the page
  87. */
  88. virtual void LoadString(FString Contents, FString DummyURL) = 0;
  89. /**
  90. * Set the desired size of the web browser viewport
  91. *
  92. * @param WindowSize Desired viewport size
  93. */
  94. virtual void SetViewportSize(FIntPoint WindowSize, FIntPoint WindowPos = FIntPoint::NoneValue) = 0;
  95. /**
  96. * Gets the current size of the web browser viewport if available, FIntPoint::NoneValue otherwise
  97. *
  98. * @param WindowSize Desired viewport size
  99. */
  100. virtual FIntPoint GetViewportSize() const = 0;
  101. /**
  102. * Gets interface to the texture representation of the browser
  103. *
  104. * @param bISpopup Whether to return the popup menu texture instead of the main browser window.
  105. * @return A slate shader resource that can be rendered
  106. */
  107. virtual FSlateShaderResource* GetTexture(bool bIsPopup = false) = 0;
  108. /**
  109. * Checks whether the web browser is valid and ready for use
  110. */
  111. virtual bool IsValid() const = 0;
  112. /**
  113. * Checks whether the web browser has finished loaded the initial page.
  114. */
  115. virtual bool IsInitialized() const = 0;
  116. /**
  117. * Checks whether the web browser is currently being shut down
  118. */
  119. virtual bool IsClosing() const = 0;
  120. /** Gets the loading state of the current document. */
  121. virtual EWebInterfaceBrowserDocumentState GetDocumentLoadingState() const = 0;
  122. /**
  123. * Gets the current title of the Browser page
  124. */
  125. virtual FString GetTitle() const = 0;
  126. /**
  127. * Gets the currently loaded URL.
  128. *
  129. * @return The URL, or empty string if no document is loaded.
  130. */
  131. virtual FString GetUrl() const = 0;
  132. /**
  133. * Gets the source of the main frame as raw HTML.
  134. *
  135. * This method has to be called asynchronously by passing a callback function, which will be called at a later point when the
  136. * result is ready.
  137. * @param Callback A callable that takes a single string reference for handling the result.
  138. */
  139. virtual void GetSource(TFunction<void (const FString&)> Callback) const = 0;
  140. /**
  141. * Notify the browser that a key has been pressed
  142. *
  143. * @param InKeyEvent Key event
  144. */
  145. virtual bool OnKeyDown(const FKeyEvent& InKeyEvent) = 0;
  146. /**
  147. * Notify the browser that a key has been released
  148. *
  149. * @param InKeyEvent Key event
  150. */
  151. virtual bool OnKeyUp(const FKeyEvent& InKeyEvent) = 0;
  152. /**
  153. * Notify the browser of a character event
  154. *
  155. * @param InCharacterEvent Character event
  156. */
  157. virtual bool OnKeyChar(const FCharacterEvent& InCharacterEvent) = 0;
  158. /**
  159. * Notify the browser that a mouse button was pressed within it
  160. *
  161. * @param MyGeometry The Geometry of the browser
  162. * @param MouseEvent Information about the input event
  163. * @param bIsPopup True if the coordinates are relative to a popup menu window, otherwise false.
  164. *
  165. * @return FReply::Handled() if the mouse event was handled, FReply::Unhandled() oterwise
  166. */
  167. virtual FReply OnMouseButtonDown(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent, bool bIsPopup) = 0;
  168. /**
  169. * Notify the browser that a mouse button was released within it
  170. *
  171. * @param MyGeometry The Geometry of the browser
  172. * @param MouseEvent Information about the input event
  173. * @param bIsPopup True if the coordinates are relative to a popup menu window, otherwise false.
  174. *
  175. * @return FReply::Handled() if the mouse event was handled, FReply::Unhandled() oterwise
  176. */
  177. virtual FReply OnMouseButtonUp(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent, bool bIsPopup) = 0;
  178. /**
  179. * Notify the browser of a double click event
  180. *
  181. * @param MyGeometry The Geometry of the browser
  182. * @param MouseEvent Information about the input event
  183. * @param bIsPopup True if the coordinates are relative to a popup menu window, otherwise false.
  184. *
  185. * @return FReply::Handled() if the mouse event was handled, FReply::Unhandled() oterwise
  186. */
  187. virtual FReply OnMouseButtonDoubleClick(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent, bool bIsPopup) = 0;
  188. /**
  189. * Notify the browser that a mouse moved within it
  190. *
  191. * @param MyGeometry The Geometry of the browser
  192. * @param MouseEvent Information about the input event
  193. * @param bIsPopup True if the coordinates are relative to a popup menu window, otherwise false.
  194. *
  195. * @return FReply::Handled() if the mouse event was handled, FReply::Unhandled() oterwise
  196. */
  197. virtual FReply OnMouseMove(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent, bool bIsPopup) = 0;
  198. /**
  199. * Notify the browser that a mouse has left the window
  200. *
  201. * @param MouseEvent Information about the input event
  202. */
  203. virtual void OnMouseLeave(const FPointerEvent& MouseEvent) = 0;
  204. /**
  205. * Sets whether mouse wheel events should be handled by the window
  206. */
  207. virtual void SetSupportsMouseWheel(bool bValue) = 0;
  208. /**
  209. * Returns whether mouse wheel events should be handled by the window
  210. */
  211. virtual bool GetSupportsMouseWheel() const = 0;
  212. /**
  213. * Called when the mouse wheel is spun
  214. *
  215. * @param MyGeometry The Geometry of the browser
  216. * @param MouseEvent Information about the input event
  217. * @param bIsPopup True if the coordinates are relative to a popup menu window, otherwise false.
  218. *
  219. * @return FReply::Handled() if the mouse event was handled, FReply::Unhandled() oterwise
  220. */
  221. virtual FReply OnMouseWheel(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent, bool bIsPopup) = 0;
  222. /**
  223. * Called when a touch gesture is performed.
  224. *
  225. * @param MyGeometry The Geometry of the browser
  226. * @param GestureEvent Information about the input event
  227. * @param bIsPopup True if the coordinates are relative to a popup menu window, otherwise false.
  228. *
  229. * @return FReply::Handled() if the mouse event was handled, FReply::Unhandled() oterwise
  230. */
  231. virtual FReply OnTouchGesture(const FGeometry& MyGeometry, const FPointerEvent& GestureEvent, bool bIsPopup) = 0;
  232. /**
  233. * The system asks each widget under the mouse to provide a cursor. This event is bubbled.
  234. *
  235. * @return FCursorReply::Unhandled() if the event is not handled; return FCursorReply::Cursor() otherwise.
  236. */
  237. virtual FCursorReply OnCursorQuery(const FGeometry& MyGeometry, const FPointerEvent& CursorEvent) = 0;
  238. /**
  239. * Called when browser receives/loses focus
  240. * @param SetFocus Whether the window gained or lost focus.
  241. * @param bIsPopup True if the coordinates are relative to a popup menu window, otherwise false.
  242. */
  243. virtual void OnFocus(bool SetFocus, bool bIsPopup) = 0;
  244. /** Called when Capture lost */
  245. virtual void OnCaptureLost() = 0;
  246. /**
  247. * Returns true if the browser can navigate backwards.
  248. */
  249. virtual bool CanGoBack() const = 0;
  250. /** Navigate backwards. */
  251. virtual void GoBack() = 0;
  252. /**
  253. * Returns true if the browser can navigate forwards.
  254. */
  255. virtual bool CanGoForward() const = 0;
  256. /** Navigate forwards. */
  257. virtual void GoForward() = 0;
  258. /**
  259. * Returns true if the browser is currently loading.
  260. */
  261. virtual bool IsLoading() const = 0;
  262. /** Reload the current page. */
  263. virtual void Reload() = 0;
  264. /** Stop loading the page. */
  265. virtual void StopLoad() = 0;
  266. /** Execute Javascript on the page. */
  267. virtual void ExecuteJavascript(const FString& Script) = 0;
  268. /**
  269. * Close this window so that it can no longer be used.
  270. *
  271. * @param bForce Designates whether the web browser close should be forced.
  272. * @param bBlockTillClosed Don't return until this browser object is fully closed.
  273. */
  274. virtual void CloseBrowser(bool bForce, bool bBlockTillClosed = false) = 0;
  275. /**
  276. * Expose a UObject instance to the browser runtime.
  277. * Properties and Functions will be accessible from JavaScript side.
  278. * As all communication with the rendering procesis asynchronous, return values (both for properties and function results) are wrapped into JS Future objects.
  279. *
  280. * @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.
  281. * @param Object The object instance.
  282. * @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.
  283. */
  284. virtual void BindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true) = 0;
  285. /**
  286. * Remove an existing script binding registered by BindUObject.
  287. *
  288. * @param Name The name of the object to remove.
  289. * @param Object The object will only be removed if it is the same object as the one passed in.
  290. * @param bIsPermanent Must match the bIsPermanent argument passed to BindUObject.
  291. */
  292. virtual void UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true) = 0;
  293. virtual void BindInputMethodSystem(ITextInputMethodSystem* TextInputMethodSystem) {}
  294. virtual void UnbindInputMethodSystem() {}
  295. /**
  296. * Get current load error.
  297. *
  298. * @return an error code if the last page load resulted in an error, otherwise 0.
  299. */
  300. virtual int GetLoadError() = 0;
  301. /**
  302. * Disable or enable web view.
  303. *
  304. * @param bValue Setting this to true will prevent any updates from the background web browser.
  305. */
  306. virtual void SetIsDisabled(bool bValue) = 0;
  307. /**
  308. * Get parent SWindow for this window
  309. */
  310. virtual TSharedPtr<class SWindow> GetParentWindow() const = 0;
  311. /**
  312. * Set parent SWindow for this window
  313. */
  314. virtual void SetParentWindow(TSharedPtr<class SWindow> Window) = 0;
  315. virtual void CheckTickActivity() {};
  316. public:
  317. /** A delegate that is invoked when the loading state of a document changed. */
  318. DECLARE_EVENT_OneParam(IWebInterfaceBrowserWindow, FOnDocumentStateChanged, EWebInterfaceBrowserDocumentState /*NewState*/);
  319. virtual FOnDocumentStateChanged& OnDocumentStateChanged() = 0;
  320. /** A delegate to allow callbacks when a browser title changes. */
  321. DECLARE_EVENT_OneParam(IWebInterfaceBrowserWindow, FOnTitleChanged, FString /*NewTitle*/);
  322. virtual FOnTitleChanged& OnTitleChanged() = 0;
  323. /** A delegate to allow callbacks when a frame url changes. */
  324. DECLARE_EVENT_OneParam(IWebInterfaceBrowserWindow, FOnUrlChanged, FString /*NewUrl*/);
  325. virtual FOnUrlChanged& OnUrlChanged() = 0;
  326. /** A delegate to allow callbacks when a frame url changes. */
  327. DECLARE_EVENT_OneParam(IWebInterfaceBrowserWindow, FOnToolTip, FString /*ToolTipText*/);
  328. virtual FOnToolTip& OnToolTip() = 0;
  329. /** A delegate that is invoked when the off-screen window has been repainted and requires an update. */
  330. DECLARE_EVENT(IWebInterfaceBrowserWindow, FOnNeedsRedraw)
  331. virtual FOnNeedsRedraw& OnNeedsRedraw() = 0;
  332. /** A delegate that is invoked prior to browser navigation. */
  333. DECLARE_DELEGATE_RetVal_TwoParams(bool, FOnBeforeBrowse, const FString& /*Url*/, const FWebNavigationRequest& /*Request*/)
  334. virtual FOnBeforeBrowse& OnBeforeBrowse() = 0;
  335. /** A delegate that is invoked to allow user code to override the contents of a Url. */
  336. DECLARE_DELEGATE_RetVal_ThreeParams(bool, FOnLoadUrl, const FString& /*Method*/, const FString& /*Url*/, FString& /*OutBody*/)
  337. virtual FOnLoadUrl& OnLoadUrl() = 0;
  338. /** A delegate that is invoked when a popup window is attempting to open. */
  339. DECLARE_DELEGATE_RetVal_TwoParams(bool, FOnBeforePopupDelegate, FString, FString);
  340. virtual FOnBeforePopupDelegate& OnBeforePopup() = 0;
  341. /** A delegate that is invoked before the browser loads a resource. Its primary purpose is to inject headers into the request. */
  342. typedef TMap<FString, FString> FRequestHeaders;
  343. DECLARE_DELEGATE_FourParams(FOnBeforeResourceLoadDelegate, FString /*Url*/, FString /*ResourceType*/, FRequestHeaders& /*AdditionalHeaders*/, const bool /*AllowUserCredentials*/);
  344. virtual FOnBeforeResourceLoadDelegate& OnBeforeResourceLoad() = 0;
  345. /** A delegate that is invoked on completion of browser resource loads. Its primary purpose is to allow response to failures. */
  346. DECLARE_DELEGATE_FourParams(FOnResourceLoadCompleteDelegate, FString /*Url*/, FString /*ResourceType*/, FString /*RequestStatus*/, int64 /*ContentLength*/);
  347. virtual FOnResourceLoadCompleteDelegate& OnResourceLoadComplete() = 0;
  348. /** A delegate that is invoked for each console message */
  349. DECLARE_DELEGATE_FourParams(FOnConsoleMessageDelegate, const FString& /*Message*/, const FString& /*Source*/, int32 /*Line*/, EWebInterfaceBrowserConsoleLogSeverity /*severity*/);
  350. virtual FOnConsoleMessageDelegate& OnConsoleMessage() = 0;
  351. /** A delegate that is invoked when an existing browser requests creation of a new browser window. */
  352. DECLARE_DELEGATE_RetVal_TwoParams(bool, FOnCreateWindow, const TWeakPtr<IWebInterfaceBrowserWindow>& /*NewBrowserWindow*/, const TWeakPtr<IWebInterfaceBrowserPopupFeatures>& /* PopupFeatures*/)
  353. virtual FOnCreateWindow& OnCreateWindow() = 0;
  354. /** A delegate that is invoked when closing created popup windows. */
  355. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnCloseWindow, const TWeakPtr<IWebInterfaceBrowserWindow>& /*BrowserWindow*/)
  356. virtual FOnCloseWindow& OnCloseWindow() = 0;
  357. /** A delegate that is invoked when the browser needs to show a popup menu. */
  358. DECLARE_EVENT_OneParam(IWebInterfaceBrowserWindow, FOnShowPopup, const FIntRect& /*PopupSize*/)
  359. virtual FOnShowPopup& OnShowPopup() = 0;
  360. /** A delegate that is invoked when the browser no longer wants to show the popup menu. */
  361. DECLARE_EVENT(IWebInterfaceBrowserWindow, FOnDismissPopup)
  362. virtual FOnDismissPopup& OnDismissPopup() = 0;
  363. /** A delegate that is invoked when the browser needs to show a dialog. */
  364. DECLARE_DELEGATE_RetVal_OneParam(EWebInterfaceBrowserDialogEventResponse, FOnShowDialog, const TWeakPtr<IWebInterfaceBrowserDialog>& /*DialogParams*/)
  365. virtual FOnShowDialog& OnShowDialog() = 0;
  366. /** A delegate that is invoked when the browser needs to dismiss and reset all dialogs. */
  367. DECLARE_DELEGATE(FOnDismissAllDialogs)
  368. virtual FOnDismissAllDialogs& OnDismissAllDialogs() = 0;
  369. /** Should return true if this dialog wants to suppress the context menu */
  370. DECLARE_DELEGATE_RetVal(bool, FOnSuppressContextMenu);
  371. virtual FOnSuppressContextMenu& OnSuppressContextMenu() = 0;
  372. /** A delegate that is invoked for each key down event not handled by the browser, return true if event is handled to prevent it from bubbling up. */
  373. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnUnhandledKeyDown, const FKeyEvent& /*KeyEvent*/);
  374. virtual FOnUnhandledKeyDown& OnUnhandledKeyDown() = 0;
  375. /** A delegate that is invoked for each up down event not handled by the browser, return true if event is handled to prevent it from bubbling up. */
  376. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnUnhandledKeyUp, const FKeyEvent& /*KeyEvent*/);
  377. virtual FOnUnhandledKeyUp& OnUnhandledKeyUp() = 0;
  378. /** A delegate that is invoked for each key char event not handled by the browser, return true if event is handled to prevent it from bubbling up. */
  379. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnUnhandledKeyChar, const FCharacterEvent& /*CharacterEvent*/);
  380. virtual FOnUnhandledKeyChar& OnUnhandledKeyChar() = 0;
  381. /** A delegate that is invoked when drag is detected in an area specified as a drag region on the web page. */
  382. DECLARE_DELEGATE_RetVal_OneParam(bool, FOnDragWindow, const FPointerEvent& /*MouseEvent*/)
  383. virtual FOnDragWindow& OnDragWindow() = 0;
  384. /** A delegate that is invoked to check the visibility of the native browser */
  385. DECLARE_DELEGATE_RetVal(bool, FOnCheckVisibility);
  386. virtual FOnCheckVisibility& OnCheckVisibility()
  387. {
  388. return OnCheckVisibilityDelegate;
  389. }
  390. virtual bool CheckVisibility()
  391. {
  392. return !OnCheckVisibilityDelegate.IsBound() || OnCheckVisibilityDelegate.Execute();
  393. }
  394. protected:
  395. /** Virtual Destructor. */
  396. virtual ~IWebInterfaceBrowserWindow() { };
  397. private:
  398. /** Delegate for veritying the window's visibility */
  399. FOnCheckVisibility OnCheckVisibilityDelegate;
  400. };