// // UniWebView.Event.cs // Created by Wang Wei (@onevcat) on 2025-04-19. // // This file is a part of UniWebView Project (https://uniwebview.com) // By purchasing the asset, you are allowed to use this code in as many as projects // you want, only if you publish the final products under the name of the same account // used for the purchase. // // This asset and all corresponding files (such as source code) are provided on an // “as is” basis, without warranty of any kind, express of implied, including but not // limited to the warranties of merchantability, fitness for a particular purpose, and // noninfringement. In no event shall the authors or copyright holders be liable for any // claim, damages or other liability, whether in action of contract, tort or otherwise, // arising from, out of or in connection with the software or the use of other dealing in the software. // using System; using UnityEngine; // UniWebView Event related part public partial class UniWebView { /// /// Delegate for page started event. /// /// The web view component which raises this event. /// The url which the web view is about to load. public delegate void PageStartedDelegate(UniWebView webView, string url); /// /// Raised when the web view starts loading a url. /// /// This event will be invoked for both url loading with `Load` method or by a link navigating from page. /// public event PageStartedDelegate OnPageStarted; /// /// Delegate for page committed event. /// /// The web view component which raises this event. /// The url which the web view has started receiving content for. public delegate void PageCommittedDelegate(UniWebView webView, string url); /// /// Raised when the web view receives response from the server and starts receiving web content. /// /// This event will be invoked when the web view has confirmed the response is a web page and /// has started to receive and process the web content. This happens after `OnPageStarted` but /// before `OnPageFinished`. /// /// This is an ideal place to inject JavaScript code at the earliest possible moment when a page starts loading. /// Note that JavaScript execution is asynchronous - it may complete after the page finishes loading. /// For most cases, it is recommended to use `OnPageFinished` event instead, which ensures the page is fully loaded. /// public event PageCommittedDelegate OnPageCommitted; /// /// Delegate for page finished event. /// /// The web view component which raises this event. /// HTTP status code received from response. /// The url which the web view loaded. public delegate void PageFinishedDelegate(UniWebView webView, int statusCode, string url); /// /// Raised when the web view finished to load a url successfully. /// /// This method will be invoked when a valid response received from the url, regardless of the response status. /// If a url loading fails before reaching to the server and getting a response, `OnLoadingErrorReceived` will be /// raised instead. /// public event PageFinishedDelegate OnPageFinished; /// /// Delegate for page loading error received event. /// /// The web view component which raises this event. /// /// The error code which indicates the error type. /// It can be different from systems and platforms. /// /// The error message which indicates the error. /// The payload received from native side, which contains the error information, such as the failing URL, in its `Extra`. public delegate void LoadingErrorReceivedDelegate( UniWebView webView, int errorCode, string errorMessage, UniWebViewNativeResultPayload payload); /// /// Raised when an error encountered during the loading process. /// Such as the "host not found" error or "no Internet connection" error will raise this event. /// /// public event LoadingErrorReceivedDelegate OnLoadingErrorReceived; /// /// Delegate for page progress changed event. /// /// The web view component which raises this event. /// A value indicates the loading progress of current page. It is a value between 0.0f and 1.0f. public delegate void PageProgressChangedDelegate(UniWebView webView, float progress); /// /// Raised when the loading progress value changes in current web view. /// public event PageProgressChangedDelegate OnPageProgressChanged; /// /// Delegate for message received event. /// /// The web view component which raises this event. /// Message received from web view. public delegate void MessageReceivedDelegate(UniWebView webView, UniWebViewMessage message); /// /// Raised when a message from web view is received. /// /// Generally, the message comes from a navigation to /// a scheme which is observed by current web view. You could use `AddUrlScheme` and /// `RemoveUrlScheme` to manipulate the scheme list. /// /// "uniwebview://" scheme is default in the list, so a clicking on link starting with "uniwebview://" /// will raise this event, if it is not removed. /// public event MessageReceivedDelegate OnMessageReceived; /// /// Delegate for should close event. /// /// The web view component which raises this event. /// Whether the web view should be closed and destroyed. public delegate bool ShouldCloseDelegate(UniWebView webView); /// /// Raised when the web view is about to close itself. /// /// This event is raised when the users close the web view by Back button on Android, Done button on iOS, /// or Close button on Unity Editor. It gives a chance to make final decision whether the web view should /// be closed and destroyed. You can also clean all related resources you created (such as a reference to /// the web view) in this event. /// public event ShouldCloseDelegate OnShouldClose; /// /// Delegate for orientation changed event. /// /// The web view component which raises this event. /// The screen orientation for current state. public delegate void OrientationChangedDelegate(UniWebView webView, ScreenOrientation orientation); /// /// Raised when the screen orientation is changed. It is a good time to set the web view frame if you /// need to support multiple orientations in your game. /// public event OrientationChangedDelegate OnOrientationChanged; /// /// Delegate for content loading terminated event. /// /// The web view component which raises this event. public delegate void OnWebContentProcessTerminatedDelegate(UniWebView webView); /// /// On iOS, raise when the system calls `webViewWebContentProcessDidTerminate` method. On Android, raise when the /// system calls `onRenderProcessGone` method. /// /// It is usually due to a low memory or the render process crashes when loading the web content. When this happens, /// the web view will leave you a blank white screen. /// /// Usually you should close the web view and clean all the resource since there is no good way to restore. In some /// cases, you can also try to free as much as memory and do a page `Reload`. /// public event OnWebContentProcessTerminatedDelegate OnWebContentProcessTerminated; /// /// Delegate for file download task starting event. /// /// The web view component which raises this event. /// The remote URL of this download task. This is also the download URL for the task. /// The file name which user chooses to use. public delegate void FileDownloadStarted(UniWebView webView, string remoteUrl, string fileName); /// /// Raised when a file download task starts. /// public event FileDownloadStarted OnFileDownloadStarted; /// /// Delegate for file download task finishing event. /// /// The web view component which raises this event. /// /// The error code of the download task result. Value `0` means the download finishes without a problem. /// Any other non-`0` value indicates an issue. The detail meaning of the error code depends on system. /// On iOS, it is usually the `errorCode` of the received `NSURLError`. On Android, the value usually represents /// an `ERROR_*` value in `DownloadManager`. /// /// The remote URL of this download task. /// /// The file path of the downloaded file. On iOS, the downloader file is in a temporary folder of your app sandbox. /// On Android, it is in the "Download" folder of your app. /// public delegate void FileDownloadFinished(UniWebView webView, int errorCode, string remoteUrl, string diskPath); /// /// Raised when a file download task finishes with either an error or success. /// public event FileDownloadFinished OnFileDownloadFinished; /// /// Delegate for capturing snapshot finished event. /// /// The web view component which raises this event. /// /// The error code of the event. If the snapshot is captured and stored without a problem, the error code is 0. /// Any other number indicates an error happened. In most cases, the screenshot capturing only fails due to lack /// of disk storage. /// /// /// An accessible disk path to the captured snapshot image. If an error happens, it is an empty string. /// public delegate void CaptureSnapshotFinished(UniWebView webView, int errorCode, string diskPath); /// /// Raised when an image captured and stored in a cache path on disk. /// public event CaptureSnapshotFinished OnCaptureSnapshotFinished; /// /// Delegate for multiple window opening event. /// /// The web view component which opens the new multiple (pop-up) window. /// The identifier of the opened new window. public delegate void MultipleWindowOpenedDelegate(UniWebView webView, string multipleWindowId); /// /// Raised when a new window is opened. This happens when you enable the `SetSupportMultipleWindows` and open a /// new pop-up window. /// public event MultipleWindowOpenedDelegate OnMultipleWindowOpened; /// /// Delegate for popup window opening event. /// /// The web view component which opens the popup window. /// The popup window handle. public delegate void PopupWindowOpenedDelegate(UniWebView webView, UniWebViewPopup popup); /// /// Raised when a popup window is opened. This provides a lightweight popup handle. /// public event PopupWindowOpenedDelegate OnPopupWindowOpened; /// /// Delegate for multiple window closing event. /// /// The web view component which closes the multiple window. /// The identifier of the closed window. public delegate void MultipleWindowClosedDelegate(UniWebView webView, string multipleWindowId); /// /// Raised when the multiple window is closed. This happens when the pop-up window is closed by navigation operation /// or by a invocation of `close()` on the page. /// public event MultipleWindowClosedDelegate OnMultipleWindowClosed; /// /// Delegate for popup window closing event. /// /// The web view component which closes the popup window. /// The popup window handle. public delegate void PopupWindowClosedDelegate(UniWebView webView, UniWebViewPopup popup); /// /// Raised when a popup window is closed. /// public event PopupWindowClosedDelegate OnPopupWindowClosed; /// /// Delegate for external activity result received event. /// /// The web view component which raises this event. /// The result returned from the external Android activity. public delegate void ExternalActivityResultReceivedDelegate( UniWebView webView, UniWebViewExternalActivityResult result); /// /// Raised when an external Android activity, launched from the web view via a custom URL scheme /// (such as `upi://` for payment apps), finishes and returns a result. /// /// When a web page navigates to a non-HTTP URL scheme that matches an installed app, UniWebView /// launches that app using Android's `startActivityForResult`. When the external app calls /// `setResult` and finishes, this event delivers the result back to your Unity code. /// /// This event is only available on Android. On iOS and macOS, external app communication uses /// URL schemes and deep links instead (handle via `Application.deepLinkActivated`). /// public event ExternalActivityResultReceivedDelegate OnExternalActivityResultReceived; /// /// Delegate for channel message received event. /// /// The web view component which raises this event. /// The channel message received from JavaScript Bridge. /// A response object that will be sent back to JavaScript. For sync calls, this is the immediate response. For async calls that don't need a response, this can be null. public delegate UniWebViewChannelMessageResponse ChannelMessageReceivedDelegate(UniWebView webView, UniWebViewChannelMessage message); /// /// Raised when a channel message from JavaScript Bridge is received. /// /// This event is triggered when JavaScript code calls window.uniwebview.send(), /// window.uniwebview.call(), or window.uniwebview.request(). The handler should /// return a UniWebViewChannelMessageResponse for sync calls or when immediate response /// is needed. For async messages that need delayed response, use message.Respond() method. /// public event ChannelMessageReceivedDelegate OnChannelMessageReceived; /* ////////////////////////////////////////////////////// // Internal Listener Interface ////////////////////////////////////////////////////// */ internal void InternalOnPageStarted(string url) { if (OnPageStarted != null) { OnPageStarted(this, url); } } internal void InternalOnShowTransitionFinished(string identifier) { Action action; if (actions.TryGetValue(identifier, out action)) { action(); actions.Remove(identifier); } } internal void InternalOnHideTransitionFinished(string identifier) { Action action; if (actions.TryGetValue(identifier, out action)) { action(); actions.Remove(identifier); } } internal void InternalOnAnimateToFinished(string identifier) { Action action; if (actions.TryGetValue(identifier, out action)) { action(); actions.Remove(identifier); } } internal void InternalOnGeneralCallback(string identifier) { Action action; if (actions.TryGetValue(identifier, out action)) { action(); actions.Remove(identifier); } } internal void InternalOnAddJavaScriptFinished(UniWebViewNativeResultPayload payload) { Action action; var identifier = payload.identifier; if (payloadActions.TryGetValue(identifier, out action)) { action(payload); payloadActions.Remove(identifier); } } internal void InternalOnEvalJavaScriptFinished(UniWebViewNativeResultPayload payload) { Action action; var identifier = payload.identifier; if (payloadActions.TryGetValue(identifier, out action)) { action(payload); payloadActions.Remove(identifier); } } internal void InternalOnPageFinished(UniWebViewNativeResultPayload payload) { if (OnPageFinished != null) { int code = -1; if (int.TryParse(payload.resultCode, out code)) { OnPageFinished(this, code, payload.data); } else { UniWebViewLogger.Instance.Critical("Invalid status code received: " + payload.resultCode); } } } internal void InternalOnPageCommitted(string url) { if (OnPageCommitted != null) { OnPageCommitted(this, url); } } internal void InternalOnPageErrorReceived(UniWebViewNativeResultPayload payload) { if (OnLoadingErrorReceived != null) { if (int.TryParse(payload.resultCode, out var code)) { OnLoadingErrorReceived(this, code, payload.data, payload); } else { UniWebViewLogger.Instance.Critical("Invalid error code received: " + payload.resultCode); } } else if (OnPageErrorReceived != null) { if (int.TryParse(payload.resultCode, out var code)) { OnPageErrorReceived(this, code, payload.data); } else { UniWebViewLogger.Instance.Critical("Invalid error code received: " + payload.resultCode); } } } internal void InternalOnPageProgressChanged(float progress) { if (OnPageProgressChanged != null) { OnPageProgressChanged(this, progress); } } internal void InternalOnMessageReceived(string result) { if (OnMessageReceived != null) { var message = new UniWebViewMessage(result); OnMessageReceived(this, message); } } internal void InternalOnShouldClose() { if (OnShouldClose != null) { var shouldClose = OnShouldClose(this); if (shouldClose) { Destroy(this); } } else { Destroy(this); } } internal void InternalOnWebContentProcessDidTerminate() { if (OnWebContentProcessTerminated != null) { OnWebContentProcessTerminated(this); } } internal void InternalOnMultipleWindowOpened(string multiWindowId) { var popup = GetOrCreatePopupWindow(multiWindowId); if (OnMultipleWindowOpened != null) { OnMultipleWindowOpened(this, multiWindowId); } if (OnPopupWindowOpened != null) { OnPopupWindowOpened(this, popup); } } internal void InternalOnMultipleWindowClosed(string multiWindowId) { HandlePopupWindowClosed(multiWindowId); } internal void InternalOnFileDownloadStarted(UniWebViewNativeResultPayload payload) { if (OnFileDownloadStarted != null) { OnFileDownloadStarted(this, payload.identifier, payload.data); } } internal void InternalOnFileDownloadFinished(UniWebViewNativeResultPayload payload) { if (OnFileDownloadFinished != null) { int errorCode = int.TryParse(payload.resultCode, out errorCode) ? errorCode : -1; OnFileDownloadFinished(this, errorCode, payload.identifier, payload.data); } } internal void InternalOnCaptureSnapshotFinished(UniWebViewNativeResultPayload payload) { if (OnCaptureSnapshotFinished != null) { int errorCode = int.TryParse(payload.resultCode, out errorCode) ? errorCode : -1; OnCaptureSnapshotFinished(this, errorCode, payload.data); } } internal void InternalOnIntentResultReceived(UniWebViewNativeResultPayload payload) { if (OnExternalActivityResultReceived != null) { int resultCode = int.TryParse(payload.resultCode, out resultCode) ? resultCode : 0; var result = new UniWebViewExternalActivityResult(payload.identifier, resultCode, payload.data); OnExternalActivityResultReceived(this, result); } } internal void InternalOnSnapshotRenderingStarted(string identifier) { if (string.IsNullOrEmpty(identifier)) { return; } if (!actions.TryGetValue(identifier, out var action)) { return; } action(); actions.Remove(identifier); } internal static void InternalCookieOperation(UniWebViewNativeResultPayload payload) { var identifier = payload.identifier; if (!globalPayloadActions.TryGetValue(identifier, out var action)) { return; } action(payload); globalPayloadActions.Remove(identifier); } }