// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved. #pragma once #include "CoreMinimal.h" #include "Widgets/SLeafWidget.h" #include "Widgets/DeclarativeSyntaxSupport.h" #include "AndroidWebBrowserWindow.h" #include "AndroidWebBrowserDialog.h" #include "AndroidJava.h" #include class SAndroidWebBrowserWidget : public SLeafWidget { SLATE_BEGIN_ARGS(SAndroidWebBrowserWidget) : _InitialURL("about:blank") { } SLATE_ARGUMENT(FString, InitialURL); SLATE_ARGUMENT(TSharedPtr, WebBrowserWindow); SLATE_END_ARGS() public: void Construct(const FArguments& Args); virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyClippingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override; virtual FVector2D ComputeDesiredSize(float) const override; void ExecuteJavascript(const FString& Script); void LoadURL(const FString& NewURL); void LoadString(const FString& Content, const FString& BaseUrl); void StopLoad(); void Reload(); void Close(); void GoBack(); void GoForward(); bool CanGoBack(); bool CanGoForward(); // WebViewClient callbacks jbyteArray HandleShouldInterceptRequest(jstring JUrl); bool HandleShouldOverrideUrlLoading(jstring JUrl); bool HandleJsDialog(EWebBrowserDialogType Type, jstring JUrl, jstring MessageText, jobject ResultCallback) { TSharedPtr Dialog(new FWebBrowserDialog(Type, MessageText, ResultCallback)); return HandleJsDialog(Dialog); } bool HandleJsPrompt(jstring JUrl, jstring MessageText, jstring DefaultPrompt, jobject ResultCallback) { TSharedPtr Dialog(new FWebBrowserDialog(MessageText, DefaultPrompt, ResultCallback)); return HandleJsDialog(Dialog); } void HandleReceivedTitle(jstring JTitle); void HandlePageLoad(jstring JUrl, bool bIsLoading, int InHistorySize, int InHistoryPosition); void HandleReceivedError(jint ErrorCode, jstring JMessage, jstring JUrl); // Helper to get the native widget pointer from a Java callback. // Jobj can either be a WebViewControl, a WebViewControl.ViewClient or WebViewControl.ChromeClient instance static SAndroidWebBrowserWidget* GetWidgetPtr(JNIEnv* JEnv, jobject Jobj) { jclass Class = JEnv->GetObjectClass(Jobj); jmethodID JMethod = JEnv->GetMethodID(Class, "GetNativePtr", "()J"); check(JMethod != nullptr); return reinterpret_cast(JEnv->CallLongMethod(Jobj, JMethod)); } protected: bool HandleJsDialog(TSharedPtr& Dialog); int HistorySize; int HistoryPosition; // mutable to allow calling JWebView_Update from inside OnPaint (which is const) mutable TOptional JWebView; TOptional JWebView_Update; TOptional JWebView_ExecuteJavascript; TOptional JWebView_LoadURL; TOptional JWebView_LoadString; TOptional JWebView_StopLoad; TOptional JWebView_Reload; TOptional JWebView_Close; TOptional JWebView_GoBackOrForward; TWeakPtr WebBrowserWindowPtr; };