WebBase.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Copyright aXiuShen. All Rights Reserved.
  2. #include "WebBase.h"
  3. #include "Async/Async.h"
  4. #include "WebViewObject.h"
  5. #include "Widgets/Layout/SBox.h"
  6. #include "Widgets/Text/STextBlock.h"
  7. #include "Async/TaskGraphInterfaces.h"
  8. #include "UObject/ConstructorHelpers.h"
  9. #include "Misc/ConfigCacheIni.h"
  10. #if WITH_EDITOR
  11. #include "Materials/MaterialInterface.h"
  12. #include "Materials/MaterialExpressionMaterialFunctionCall.h"
  13. #include "Materials/MaterialExpressionTextureSample.h"
  14. #include "Materials/MaterialExpressionTextureSampleParameter2D.h"
  15. #include "Materials/MaterialFunction.h"
  16. #include "Factories/MaterialFactoryNew.h"
  17. //#include "AssetRegistryModule.h"
  18. #include "PackageHelperFunctions.h"
  19. #endif
  20. #include "cefcorelib.h"
  21. #ifdef WEBVIEW_CUSTOMIZED_CORE
  22. #include "SCefBrowser.h"
  23. #endif
  24. #ifdef USING_WEBBROWSER
  25. #include "SProxyWeb.h"
  26. #endif
  27. #define LOCTEXT_NAMESPACE "WebBase"
  28. /////////////////////////////////////////////////////
  29. // UWebBase
  30. void UHtmlHeaders::ExistAppend(const FString& Key, const FString& Value) {
  31. if (!Headers.Contains(Key))return;
  32. FString &V= Headers[Key];
  33. V = V + TEXT(" ") +Value;
  34. }
  35. void UHtmlHeaders::Replace(const FString& Key, const FString& Value) {
  36. if (!Headers.Contains(Key)) {
  37. Headers.Add(Key, Value);
  38. return;
  39. }
  40. Headers[Key] = Value;
  41. }
  42. UWebBase::UWebBase(const FObjectInitializer& ObjectInitializer)
  43. : Super(ObjectInitializer)
  44. , styleText(FTextBlockStyle::GetDefault())
  45. , ColorBackground(255, 255, 255, 254)
  46. , _Pixel(8, 4)
  47. , _Zoom(1.0f)
  48. //, jsWindow(TEXT("ue"))
  49. {
  50. CefWidget = nullptr;
  51. ProxyWidget = nullptr;
  52. WebWidget = nullptr;
  53. _Touch = false;
  54. bIsVariable = true;
  55. SetVisibility(ESlateVisibility::SelfHitTestInvisible);
  56. FString category(TEXT("ue"));
  57. FString object(TEXT("interface"));
  58. GConfig->GetString(TEXT("WebView"), TEXT("category"), category, GGameIni);
  59. GConfig->GetString(TEXT("WebView"), TEXT("object"), object, GGameIni);
  60. jsWindow = FString::Printf(TEXT("%s.%s"),*category,*object);
  61. styleText.ColorAndOpacity = FSlateColor(FLinearColor(0.0f, 0.0f, 0.0f));
  62. styleText.Font.Size = 20;
  63. bIsVariable = true;
  64. eKeyboradModeTransparency = WebView_Keyboard_Mode::WebView_Keyboard_Mode_Blend;
  65. }
  66. void UWebBase::LoadURL(const FString& NewURL,FString PostData, bool need_response)
  67. {
  68. #ifdef USING_WEBBROWSER
  69. if (ProxyWidget)ProxyWidget->LoadURL(NewURL);
  70. #else
  71. if (CefWidget)CefWidget->LoadURL(NewURL, PostData, need_response);
  72. #endif
  73. }
  74. void UWebBase::LoadString(const FString& NewURL, const FString& Content)
  75. {
  76. #ifdef USING_WEBBROWSER
  77. if (ProxyWidget)ProxyWidget->LoadString(NewURL, Content);
  78. #else
  79. if (CefWidget)CefWidget->LoadString(NewURL, Content);
  80. #endif
  81. }
  82. void UWebBase::ExecuteJavascript(const FString& ScriptText)
  83. {
  84. #ifdef USING_WEBBROWSER
  85. if (ProxyWidget)ProxyWidget->ExecuteJavascript(ScriptText);
  86. #else
  87. if (CefWidget)CefWidget->ExecuteJavascript(ScriptText);
  88. #endif
  89. }
  90. void UWebBase::CallJsonStr(const FString& Function, const FString& Data)
  91. {
  92. if (Function.IsEmpty())
  93. return;
  94. FString TextScript;
  95. #ifndef USING_WEBBROWSER
  96. if (!CefWidget || CefWidget->CallJsonStr(Function, Data))return;
  97. #endif
  98. if (Data.Len() >= 2) {
  99. TextScript = FString::Printf(TEXT("%s['%s'](%s)"),
  100. *jsWindow, *Function, *Data);
  101. }
  102. else {
  103. TextScript = FString::Printf(TEXT("%s['%s']()"),
  104. *jsWindow, *Function);
  105. }
  106. #ifdef USING_WEBBROWSER
  107. if(ProxyWidget)ProxyWidget->ExecuteJavascript(TextScript);
  108. #else
  109. if(CefWidget)CefWidget->ExecuteJavascript(TextScript);
  110. #endif
  111. }
  112. void UWebBase::CallParams(const FString& Function, const TArray<FString>& Params) {
  113. if (Function.IsEmpty())
  114. return;
  115. FString strParam;
  116. for (auto& parm: Params) {
  117. if (!strParam.IsEmpty())strParam.Append(TEXT(","));
  118. strParam.Append(TEXT("\'")).Append(parm).Append(TEXT("\'"));
  119. }
  120. FString TextScript;
  121. TextScript = FString::Printf(TEXT("%s['%s'](%s)"),
  122. *jsWindow, *Function, *strParam);
  123. #ifdef USING_WEBBROWSER
  124. if (ProxyWidget)ProxyWidget->ExecuteJavascript(TextScript);
  125. #else
  126. if (CefWidget)CefWidget->ExecuteJavascript(TextScript);
  127. #endif
  128. }
  129. FString UWebBase::GetUrl() const {
  130. #ifndef USING_WEBBROWSER
  131. if (CefWidget)CefWidget->GetUrl();
  132. #endif
  133. return FString();
  134. }
  135. /** Reload the current page. */
  136. void UWebBase::Reload() {
  137. #ifndef USING_WEBBROWSER
  138. if (CefWidget)CefWidget->Reload();
  139. #endif
  140. }
  141. bool UWebBase::Isloaded() {
  142. #ifndef USING_WEBBROWSER
  143. if (CefWidget)return CefWidget->Isloaded();
  144. #endif
  145. return true;
  146. }
  147. void UWebBase::ZoomLevel(float zoom) const {
  148. #ifndef USING_WEBBROWSER
  149. if (CefWidget)CefWidget->ZoomLevel(zoom);
  150. #endif
  151. }
  152. void UWebBase::Silent(bool onoff) {
  153. #ifndef USING_WEBBROWSER
  154. if (CefWidget)CefWidget->Silent(onoff);
  155. #endif
  156. }
  157. void UWebBase::WebPixel(FIntPoint pixel) const {
  158. #ifndef USING_WEBBROWSER
  159. if (CefWidget)CefWidget->WebPixel(pixel);
  160. #endif
  161. }
  162. void UWebBase::BindUObject(const FString& VarName, UObject* Object, bool bIsPermanent) {
  163. #ifndef USING_WEBBROWSER
  164. if (CefWidget)CefWidget->BindUObject(VarName, Object, bIsPermanent);
  165. #else
  166. if (ProxyWidget)ProxyWidget->BindUObject(VarName, Object, bIsPermanent);
  167. #endif
  168. }
  169. void UWebBase::UnbindUObject(const FString& Name, UObject* Object, bool bIsPermanent) {
  170. #ifndef USING_WEBBROWSER
  171. if (CefWidget)CefWidget->UnbindUObject(Name, Object, bIsPermanent);
  172. #else
  173. if (ProxyWidget)ProxyWidget->UnbindUObject(Name, Object, bIsPermanent);
  174. #endif
  175. }
  176. void UWebBase::BeginDestroy() {
  177. #ifndef USING_WEBBROWSER
  178. if (CefWidget) {
  179. CefWidget->Close();
  180. CefWidget->SetCanTick(false);
  181. CefWidget.Reset();
  182. }
  183. #else
  184. if (ProxyWidget) {
  185. ProxyWidget->SetCanTick(false);
  186. ProxyWidget.Reset();
  187. }
  188. #endif
  189. CefWidget.Reset();
  190. Super::BeginDestroy();
  191. }
  192. TSharedRef<SWidget> UWebBase::RebuildWidget() {
  193. if (IsDesignTime() || IsDefaultSubobject()) {
  194. return SNew(SBox)
  195. .HAlign(HAlign_Center)
  196. .VAlign(VAlign_Center)
  197. [
  198. SNew(STextBlock)
  199. .Text(LOCTEXT("WebView", "WebView"))
  200. ];
  201. }
  202. if (OnPreReBuild.IsBound())OnPreReBuild.Broadcast();
  203. #ifndef USING_WEBBROWSER
  204. CefWidget = SNew(SCefBrowser)
  205. .ShowAddressBar(addressShow)
  206. //.InitialURL(urlInitial)
  207. .BackgroundColor(ColorBackground)
  208. .ShowControls(controlShow)
  209. .RightKeyPopup(RightKeyPopup)
  210. .BrowserFrameRate(RateFrame)
  211. .TextStyle(styleText)
  212. .EnableMouseTransparency(bEnableMouseTransparency)
  213. .SwitchInputMethod(SwitchInputMethod)
  214. .ViewportSize(GetDesiredSize())
  215. .Pixel(_Pixel)
  216. .zoom(_Zoom)
  217. .Touch(_Touch)
  218. .downloadTip(downloadTip)
  219. .Visibility(EVisibility::SelfHitTestInvisible)
  220. .OnUrlChanged_UObject(this, &UWebBase::HandleOnUrlChanged)
  221. .OnBeforePopup_UObject(this, &UWebBase::HandleOnBeforePopup)
  222. .OnPostResponse_UObject(this, &UWebBase::HandleOnPostResponse)
  223. .OnWebError_UObject(this, &UWebBase::HandleOnWebError)
  224. .OnResourceLoad_UObject(this, &UWebBase::HandleOnResourceLoad)
  225. .OnJsStr_UObject(this, &UWebBase::HandleAsyn)
  226. .OnLoadState_UObject(this, &UWebBase::HandleOnLoadState)
  227. .OnDownloadComplete_UObject(this, &UWebBase::HandleOnDownloadTip);
  228. CefWidget->KeyboardMode(webview::toInner(eKeyboradModeTransparency));
  229. WebWidget = CefWidget;
  230. #else
  231. ProxyWidget = SNew(SProxyWeb)
  232. .ShowAddressBar(addressShow)
  233. //.InitialURL(urlInitial)
  234. .BackgroundColor(ColorBackground)
  235. .ShowControls(controlShow)
  236. .RightKeyPopup(RightKeyPopup)
  237. .BrowserFrameRate(RateFrame)
  238. .TextStyle(styleText)
  239. .EnableMouseTransparency(bEnableMouseTransparency)
  240. .SwitchInputMethod(SwitchInputMethod)
  241. .ViewportSize(GetDesiredSize())
  242. .Pixel(_Pixel)
  243. .zoom(_Zoom)
  244. .downloadTip(downloadTip)
  245. .Visibility(EVisibility::SelfHitTestInvisible)
  246. .OnUrlChanged_UObject(this, &UWebBase::HandleOnUrlChanged)
  247. .OnBeforePopup_UObject(this, &UWebBase::HandleOnBeforePopup)
  248. .OnLoadState_UObject(this, &UWebBase::HandleOnLoadState)
  249. .OnDownloadComplete_UObject(this, &UWebBase::HandleOnDownloadTip);
  250. WebWidget = ProxyWidget;
  251. #endif
  252. _ViewObject = NewObject<UWebViewObject>();// 隔离JS和UE4之间的数据。
  253. if (_ViewObject) {
  254. _ViewObject->SetUMG(this);
  255. BindUObject("$receive", _ViewObject);
  256. }
  257. #ifndef USING_WEBBROWSER
  258. CefWidget->LoadURL(urlInitial);
  259. #else
  260. ProxyWidget->LoadURL(urlInitial);
  261. #endif
  262. return WebWidget.ToSharedRef();
  263. }
  264. bool UWebBase::Asyn(const FString& Name, FString& Data, const FString& Callback) {
  265. if (!OnJsEventStr.IsBound())return false;
  266. OnJsEventStr.Broadcast(Name, Data, Callback);
  267. return true;
  268. }
  269. void UWebBase::StopRender(bool hidden) {
  270. #ifndef USING_WEBBROWSER
  271. if(CefWidget)CefWidget->StopRender(hidden);
  272. #endif
  273. }
  274. void UWebBase::HandleOnUrlChanged(const FText& InText) {
  275. if(OnUrlChanged.IsBound()) OnUrlChanged.Broadcast(InText);
  276. }
  277. void UWebBase::HandleOnLoadState(const int state) {
  278. if (OnLoadState.IsBound()) OnLoadState.Broadcast(state);
  279. }
  280. bool UWebBase::HandleOnBeforePopup(FString URL, FString Frame) {
  281. if (!OnBeforePopup.IsBound()) {// 如果没有绑定事件则自动跳转URL
  282. LoadURL(URL);
  283. return true;
  284. }
  285. OnBeforePopup.Broadcast(URL, Frame);
  286. return true;
  287. }
  288. void UWebBase::ShowAddress(bool show) {
  289. #ifndef USING_WEBBROWSER
  290. if (CefWidget)CefWidget->ShowAddress(show);
  291. #endif
  292. }
  293. void UWebBase::ReopenRender(FString NewURL) {
  294. #ifndef USING_WEBBROWSER
  295. if (CefWidget)CefWidget->ReopenRender(NewURL);
  296. #endif
  297. }
  298. void UWebBase::ShowDevTools() {
  299. #ifndef USING_WEBBROWSER
  300. if (CefWidget)CefWidget->ShowDevTools();
  301. #endif
  302. }
  303. void UWebBase::HandleOnDownloadTip(FString URL, FString File) {
  304. if (!OnDownloadComplete.IsBound()) return;
  305. OnDownloadComplete.Broadcast(URL, File);
  306. }
  307. void UWebBase::HandleOnPostResponse(const FString& URL, const FString& PostResponse) {
  308. if (!OnPostResponse.IsBound()) return;
  309. OnPostResponse.Broadcast(URL, PostResponse);
  310. }
  311. void UWebBase::HandleOnWebError(const FString& Url, const FString& Desc, const FString& Source, const int line) {
  312. if (!OnWebError.IsBound()) return;
  313. OnWebError.Broadcast(Desc, Source, line);
  314. }
  315. void UWebBase::HandleAsyn(const FString& Name, const FString& Data, const FString& Callback) {
  316. FString Json = Data;
  317. Asyn(Name, Json, Callback);
  318. }
  319. bool UWebBase::HandleOnResourceLoad(FString URL, int ResourceType, TMap<FString, FString>& HtmlHeaders) {
  320. if (!OnBeforeRequest.IsBound()) return false;
  321. UHtmlHeaders* Headers = NewObject<UHtmlHeaders>(this);
  322. Headers->Headers = HtmlHeaders;
  323. OnBeforeRequest.Broadcast(URL, ResourceType, Headers);
  324. HtmlHeaders = Headers->Headers;
  325. return true;
  326. }
  327. void UWebBase::ReleaseSlateResources(bool bReleaseChildren) {
  328. #ifndef USING_WEBBROWSER
  329. if (CefWidget) {
  330. CefWidget->StopRender(false);
  331. CefWidget->Close();
  332. }
  333. #endif
  334. WebWidget.Reset();
  335. if (_ViewObject)_ViewObject = nullptr;
  336. }
  337. void UWebBase::KeyboardMode(WebView_Keyboard_Mode KeyMode) {
  338. eKeyboradModeTransparency = KeyMode;
  339. #ifndef USING_WEBBROWSER
  340. if (CefWidget)CefWidget->KeyboardMode(webview::toInner(eKeyboradModeTransparency));
  341. #endif
  342. }
  343. void UWebBase::GoBack() {
  344. #ifndef USING_WEBBROWSER
  345. if (CefWidget)CefWidget->GoBack();
  346. #else
  347. if (ProxyWidget)ProxyWidget->GoBack();
  348. #endif
  349. }
  350. void UWebBase::GoForward() {
  351. #ifndef USING_WEBBROWSER
  352. if (CefWidget)CefWidget->GoForward();
  353. #else
  354. if (ProxyWidget)ProxyWidget->GoForward();
  355. #endif
  356. }
  357. bool UWebBase::CanGoBack() {
  358. #ifndef USING_WEBBROWSER
  359. if (CefWidget)CefWidget->CanGoBack();
  360. #else
  361. if (ProxyWidget)ProxyWidget->CanGoBack();
  362. #endif
  363. return false;
  364. }
  365. bool UWebBase::CanGoForward() {
  366. #ifndef USING_WEBBROWSER
  367. if (CefWidget)CefWidget->CanGoForward();
  368. #else
  369. if (ProxyWidget)ProxyWidget->CanGoForward();
  370. #endif
  371. return false;
  372. }
  373. void UWebBase::SetImitateInput(const FImitateInput& ImitateInput) {
  374. #ifndef USING_WEBBROWSER
  375. if (CefWidget)CefWidget->SetImitateInput(webview::toInner(ImitateInput));
  376. #endif
  377. }
  378. #undef LOCTEXT_NAMESPACE