WebBase.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright aXiuShen. All Rights Reserved.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Components/Widget.h"
  5. #include "Styling/SlateTypes.h"
  6. #include "Components/WidgetSwitcherSlot.h"
  7. #include "Containers/Map.h"
  8. #include "ImitateInput.h"
  9. #include "WebBase.generated.h"
  10. class UWebViewObject;
  11. UCLASS(BlueprintType, Blueprintable)
  12. class UHtmlHeaders : public UObject {
  13. GENERATED_BODY()
  14. public:
  15. UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Web View|HTML")
  16. TMap<FString, FString> Headers;
  17. UFUNCTION(BlueprintCallable, Category = "Web View")
  18. void ExistAppend(const FString& Key,const FString& Value);
  19. UFUNCTION(BlueprintCallable, Category = "Web View")
  20. void Replace(const FString& Key, const FString& Value);
  21. };
  22. /**
  23. * , BlueprintType, hidecategories = (Object)
  24. */
  25. //UCLASS()
  26. UCLASS(Abstract)
  27. class WEBVIEW_API UWebBase : public UWidget
  28. {
  29. GENERATED_UCLASS_BODY()
  30. public:
  31. DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnPreReBuild);
  32. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnStateLoad, int, state);
  33. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnUrlChanged, const FText&, Url);
  34. DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FOnJsStr, const FString&, Type,const FString&, JSON, const FString&, FuncName);
  35. DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnBeforePopup, const FString&, Url, const FString&, Frame);
  36. DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnDownloadComplete, const FString&, Url, const FString&, File);
  37. DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FOnWebError, const FString&, Desc, const FString&, Source ,const int,line);
  38. DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnPostResponse, const FString&, Url, const FString&, Response);
  39. /* ResourceType
  40. 0: Top level page.
  41. 1: Frame or iframe.
  42. 3: CSS stylesheet.
  43. 4: External script.
  44. 5: Image (jpg/gif/png/etc).
  45. 6: Font.
  46. 7: Some other subresource. This is the default type if the actual type is unknown.
  47. 8: Object (or embed) tag for a plugin, or a resource that a plugin requested.
  48. 9: Media resource.
  49. 10: Main resource of a dedicated worker.
  50. 11: Main resource of a shared worker.
  51. 12: Explicitly requested prefetch.
  52. 13: Favicon.
  53. 14: XMLHttpRequest.
  54. 15: A request for a <ping>
  55. 16: Main resource of a service worker.
  56. 17: A report of Content Security Policy violations.
  57. 18: A resource that a plugin requested.
  58. 19: A main-frame service worker navigation preload request.
  59. 20: A sub-frame service worker navigation preload request.
  60. */
  61. DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FOnBeforeRequest, const FString&, URL, int, ResourceType, UHtmlHeaders*, Headers);
  62. public:
  63. /** Called when loading stat changed */
  64. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  65. FOnStateLoad OnLoadState;
  66. /** Called when the Url changes. */
  67. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  68. FOnUrlChanged OnUrlChanged;
  69. // Called with ue.interface.broadcast(name, data) in the browser context.
  70. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  71. FOnJsStr OnJsEventStr;
  72. /** Called when a popup is about to spawn. */
  73. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  74. FOnBeforePopup OnBeforePopup;
  75. /** Called when a popup is about to spawn. */
  76. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  77. FOnDownloadComplete OnDownloadComplete;
  78. /** Called when a popup is about to spawn. */
  79. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  80. FOnPreReBuild OnPreReBuild;
  81. /** called when resouce load. */
  82. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  83. FOnBeforeRequest OnBeforeRequest;
  84. /** called when resouce load. */
  85. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  86. FOnWebError OnWebError;
  87. UPROPERTY(BlueprintAssignable, Category = "Web View|Event")
  88. FOnPostResponse OnPostResponse;
  89. /** this party is blueprint editor params */
  90. /** URL that the browser will initially navigate to. The URL should include the protocol, eg http:// */
  91. UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisplayName = "Initial URL"), Category = "Web View")
  92. FString urlInitial;
  93. /** Configure webpage mouse is transparency */
  94. UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisplayName = "Enable Mouse"), Category = "Web View|Transparency")
  95. bool bEnableMouseTransparency = true;
  96. /** Configure webpage mouse is transparency */
  97. UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisplayName = "Keyborad Mode"), Category = "Web View|Transparency")
  98. WebView_Keyboard_Mode eKeyboradModeTransparency ;
  99. /** Control and Editor show text style */
  100. UPROPERTY(EditAnywhere, meta = (DisplayName = "Text Style", UIMin = 0, UIMax = 1), Category = "Web View|Show Head")
  101. FTextBlockStyle styleText;
  102. /** popup a menu using right mouse in web page */
  103. UPROPERTY(EditAnywhere, meta = (DisplayName = "Show Popup Menu", UIMin = 0, UIMax = 1), Category = "Web View|Show Head")
  104. bool RightKeyPopup;
  105. /** Switch Input Method. */
  106. UPROPERTY(EditAnywhere, meta = (DisplayName = "Switch Input Method", UIMin = 0, UIMax = 1), Category = "Web View")
  107. bool SwitchInputMethod = false;
  108. /** Configure webpage flush frame rate */
  109. UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisplayName = "Frame Rate", ClampMin = 1, ClampMax = 60), Category = "Web View")
  110. int RateFrame = 20;
  111. /** Configure webpage is transparency */
  112. UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisplayName = "Background Color", ToolTip="transparent: A+js.A<255, "), Category = "Web View")
  113. FColor ColorBackground;
  114. /** Whether to show an address bar. */
  115. UPROPERTY(EditAnywhere, meta = (DisplayName = "Show Address", UIMin = 0, UIMax = 1), Category = "Web View|Show Head")
  116. bool addressShow = false;
  117. /** Whether to show standard controls like Back, Forward, Reload etc. */
  118. UPROPERTY(EditAnywhere, meta = (DisplayName = "Show Controls", UIMin = 0, UIMax = 1), Category = "Web View|Show Head")
  119. bool controlShow = false;
  120. /** When Download file Whether to show Tip Dialog. */
  121. UPROPERTY(EditAnywhere, meta = (DisplayName = "Download Tip", UIMin = 0, UIMax = 1), Category = "Web View|Show Head")
  122. bool downloadTip = true;
  123. /** set web page rendering pixel size, 8*4 default :The browser calculates the size itself */
  124. UPROPERTY(EditAnywhere, meta = (DisplayName = "Web Pixel", UIMin = 4, UIMax = 15360), Category = "Web View|Screen")
  125. FIntPoint _Pixel;
  126. /** Page Zoom Level. The value is consistent with that of chrome */
  127. UPROPERTY(EditAnywhere, meta = (DisplayName = "Zoom Level", ClampMin = 0.0, ClampMax = 5.0), Category = "Web View|Screen")
  128. float _Zoom;
  129. /** Page Zoom Level. The value is consistent with that of chrome */
  130. UPROPERTY(EditAnywhere, meta = (DisplayName = "Touch Screen"), Category = "Web View|Screen")
  131. bool _Touch;
  132. UPROPERTY(EditAnywhere, meta = (DisplayName = "Sync Parse Json", UIMin = 0, UIMax = 1), Category = "Web View")
  133. bool syncJson = true;
  134. protected:
  135. FString jsWindow;// for javescrit
  136. TSharedPtr<class SCefBrowser> CefWidget; // for slate core widget
  137. TSharedPtr<class SProxyWeb> ProxyWidget; // for slate core widget
  138. TSharedPtr<class SCompoundWidget> WebWidget; // for slate core widget
  139. private:
  140. UWebViewObject* _ViewObject;// 保存UE4与Js的通信数据
  141. public:
  142. /**
  143. * Executes a JavaScript string in the context of the web page
  144. * @param ScriptText JavaScript string to execute
  145. */
  146. UFUNCTION(BlueprintCallable, Category = "Web View")
  147. void ExecuteJavascript(const FString& ScriptText);
  148. /**
  149. * Load the specified URL
  150. * @param NewURL New URL to load
  151. * @param PostData arg1=val1&arg2=val2
  152. * notice: " don't must need.
  153. * @param need_response
  154. */
  155. UFUNCTION(BlueprintCallable, Category = "Web View", meta = (AdvancedDisplay = "PostData,need_response"))
  156. void LoadURL(const FString& NewURL, FString PostData=TEXT(""),bool need_response=false);
  157. /**
  158. * Load the specified content
  159. * @param DummyURL use to show
  160. * @param Content show html content
  161. * @param need_response
  162. */
  163. UFUNCTION(BlueprintCallable, Category = "Web View")
  164. void LoadString(const FString& DummyURL, const FString& Content);
  165. //UFUNCTION(BlueprintCallable, Category = "Web View")
  166. //void NavigationURL(FString NewURL,FString NaviHead);
  167. /** Reload the current page. */
  168. UFUNCTION(BlueprintCallable, Category = "Web View")
  169. void Reload();
  170. /**
  171. * check url is loaded succesed
  172. */
  173. UFUNCTION(BlueprintCallable, Category = "Web View")
  174. bool Isloaded();
  175. /**
  176. * Call javascript function
  177. * @param Function ue.interface.func
  178. * @param Data {"a":1,"b":"sdf"}
  179. */
  180. UFUNCTION(BlueprintCallable, Category = "Web View", meta = (AdvancedDisplay = "Data", AutoCreateRefTerm = "Data"))
  181. void CallJsonStr(const FString& Function, const FString& Data);
  182. /**
  183. * Call javascript function
  184. * @param Function ue.interface.func
  185. * @param Params
  186. */
  187. UFUNCTION(BlueprintCallable, Category = "Web View", meta = (AdvancedDisplay = "Params", AutoCreateRefTerm = "Params"))
  188. void CallParams(const FString& Function, const TArray<FString>& Params);
  189. /**
  190. * Expose a UObject instance to the browser runtime.
  191. * Properties and Functions will be accessible from JavaScript side.
  192. * As all communication with the rendering procesis asynchronous, return values (both for properties and function results) are wrapped into JS Future objects.
  193. * @param Name The name of the object.
  194. * The object will show up as window.ue4.{Name} on the javascript side.
  195. * 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.
  196. * @param Object The object instance.
  197. * @param bIsPermanent If true, the object will be visible to all pages loaded through this browser widget,
  198. * otherwise, it will be deleted when navigating away from the current page.
  199. * Non-permanent bindings should be registered from inside an OnLoadStarted event handler in order to be available before JS code starts loading.
  200. */
  201. UFUNCTION(BlueprintCallable, Category = "Web View", meta = (AdvancedDisplay = "Name|Object"))
  202. void BindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true);
  203. /**
  204. * Remove an existing script binding registered by BindUObject.
  205. * @param Name The name of the object to remove.
  206. * @param Object The object will only be removed if it is the same object as the one passed in.
  207. * @param bIsPermanent Must match the bIsPermanent argument passed to BindUObject.
  208. */
  209. UFUNCTION(BlueprintCallable, Category = "Web View", meta = (AdvancedDisplay = "Name|Object"))
  210. void UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent = true);
  211. /**
  212. * Gets the currently loaded URL.
  213. * @return The URL, or empty string if no document is loaded.
  214. */
  215. UFUNCTION(BlueprintCallable, Category = "Web View")
  216. FString GetUrl() const;
  217. /**
  218. * Set web page zoom level
  219. * @param zoom : between 0.25 and 5 default is 1
  220. * when Pixel was set,then zoom invalid
  221. */
  222. UFUNCTION(BlueprintCallable, Category = "Web View")
  223. void ZoomLevel(float zoom=1.0) const;
  224. /**
  225. * Set web page silent
  226. * @param zoom : true silent
  227. *
  228. */
  229. UFUNCTION(BlueprintCallable, Category = "Web View")
  230. void Silent(bool onoff=false);
  231. /**
  232. * Set web page zoom level
  233. * @param pixel : X between 8 and 15360, Y between 4 and 8640
  234. * when Pixel was set,then zoom invalid
  235. */
  236. UFUNCTION(BlueprintCallable, Category = "Web View")
  237. void WebPixel(FIntPoint pixel /*= FIntPoint(8,4)*/) const;
  238. /**
  239. * Set web show address
  240. * @ show : true:show false:hide
  241. */
  242. UFUNCTION(BlueprintCallable, Category = "Web View")
  243. void ShowAddress(bool show);
  244. /**
  245. * reopen a new render to replace old render.
  246. * @param NewURL New URL to load.
  247. * if NewURL is empty,will Assign old URL.
  248. */
  249. UFUNCTION(BlueprintCallable, Category = "Web View")
  250. void ReopenRender(FString NewURL="");
  251. /**
  252. * Show Dev Tools for debug web
  253. */
  254. UFUNCTION(BlueprintCallable, Category = "Web View")
  255. void ShowDevTools();
  256. /**
  257. * Show Dev Tools for debug web
  258. */
  259. UFUNCTION(BlueprintCallable, Category = "Web View")
  260. void KeyboardMode(WebView_Keyboard_Mode KeyMode);
  261. UFUNCTION(BlueprintCallable, Category = "Web View")
  262. void GoBack();
  263. UFUNCTION(BlueprintCallable, Category = "Web View")
  264. void GoForward();
  265. UFUNCTION(BlueprintCallable, Category = "Web View")
  266. bool CanGoBack();
  267. UFUNCTION(BlueprintCallable, Category = "Web View")
  268. bool CanGoForward();
  269. //FImitateInput ImitateInput
  270. UFUNCTION(BlueprintCallable, Category = "Web View")
  271. void SetImitateInput(const FImitateInput& ImitateInput);
  272. UFUNCTION(BlueprintCallable, Category = "Web View")
  273. void StopRender(bool hidden);
  274. public:
  275. virtual void BeginDestroy() override;
  276. //
  277. virtual bool Asyn(const FString& Name, FString& Data, const FString& Callback);
  278. //virtual void TickRenderResource() override;
  279. virtual void ReleaseSlateResources(bool bReleaseChildren);
  280. protected:
  281. virtual TSharedRef<SWidget> RebuildWidget() override;
  282. void HandleOnLoadState(const int state);
  283. void HandleOnUrlChanged(const FText& Text);
  284. bool HandleOnBeforePopup(FString URL, FString Frame);
  285. void HandleOnDownloadTip(FString URL, FString File);
  286. void HandleOnPostResponse(const FString& URL,const FString& File);
  287. void HandleOnWebError(const FString& Url, const FString& Desc, const FString& Source, const int line);
  288. void HandleAsyn(const FString& Name, const FString& Data, const FString& Callback);
  289. //typedef class SCefBrowser::TMap<FString, FString> RequestHeaders;
  290. bool HandleOnResourceLoad(FString URL, int ResourceType, TMap<FString, FString>& HtmlHeaders);
  291. };