Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UniWebViewSafeBrowsing.cs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // UniWebViewSafeBrowsing.cs
  3. // Created by Wang Wei(@onevcat) on 2020-07-18.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. using UnityEngine;
  18. using System;
  19. /// <summary>
  20. /// UniWebView Safe Browsing provides a way for browsing the web content in a more browser-like way, such as Safari on
  21. /// iOS and Chrome on Android.
  22. ///
  23. /// This class wraps `SFSafariViewController` on iOS and "Custom Tabs" on Android. It shares cookies, auto-fill
  24. /// completion and other more data with the browser on device. Most of permissions are also built-in supported. You can
  25. /// use this class for some tasks that are limited for a normal web view, such as using Apple Pay or Progressive Web
  26. /// Apps (PWA).
  27. ///
  28. /// You create a `UniWebViewSafeBrowsing` instance by calling the static `UniWebViewSafeBrowsing.Create` method with a
  29. /// destination URL. You cannot change this URL once the instance is created. To show the safe browsing, call `Show` on
  30. /// the instance. The web content will be displayed in full screen with a toolbar containing the loaded URL, as well
  31. /// as some basic controls like Go Back, Go Forward and Done.
  32. ///
  33. /// Browsing web content in `UniWebViewSafeBrowsing` is only supported on iOS and Android. There is no such component in
  34. /// Unity Editor. Creating and showing a `UniWebViewSafeBrowsing` on Unity Editor will fall back to open the URL in
  35. /// external browser by using Unity's `Application.OpenURL`.
  36. ///
  37. /// </summary>
  38. public class UniWebViewSafeBrowsing: UnityEngine.Object {
  39. /// <summary>
  40. /// Delegate for safe browsing finish event.
  41. /// </summary>
  42. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  43. public delegate void OnSafeBrowsingFinishedDelegate(UniWebViewSafeBrowsing browsing);
  44. /// <summary>
  45. /// Raised when user dismisses safe browsing by tapping the Done button or Back button.
  46. ///
  47. /// The dismissed safe browsing instance will be invalid after this event being raised, and you should not use
  48. /// it for another browsing purpose. Instead, create a new one for a new browsing session.
  49. ///
  50. /// This event will not happen in Unity Editor, because the whole `UniWebViewSafeBrowsing` will fall back to an
  51. /// external browser.
  52. /// </summary>
  53. public event OnSafeBrowsingFinishedDelegate OnSafeBrowsingFinished;
  54. private string id = Guid.NewGuid().ToString();
  55. private UniWebViewNativeListener listener;
  56. // This is only for editor, to open the url in system browser.
  57. private string url;
  58. /// <summary>
  59. /// Whether the safe browsing mode is supported in current runtime or not.
  60. ///
  61. /// If supported, the safe browsing mode will be used when `Show` is called on a `UniWebViewSafeBrowsing` instance.
  62. /// Otherwise, the system default browser will be used to open the page when `Show` is called.
  63. ///
  64. /// This property always returns `true` on iOS runtime platform. On Android, it depends on whether there is an Intent
  65. /// can handle the safe browsing request. Usually it is provided by Chrome. If there is no Intent can open the URL
  66. /// in safe browsing mode, this property will return `false`.
  67. ///
  68. /// To use this API on Android when you set your Target SDK to Android 11 or later, you need to declare the correct
  69. /// intent query explicitly in your AndroidManifest.xml, to follow the Package Visibility
  70. /// (https://developer.android.com/about/versions/11/privacy/package-visibility):
  71. ///
  72. /// ```xml
  73. /// <queries>
  74. /// <intent>
  75. /// <action android:name="android.support.customtabs.action.CustomTabsService" />
  76. /// </intent>
  77. /// </queries>
  78. /// ```
  79. /// </summary>
  80. /// <returns>
  81. /// Returns `true` if the safe browsing mode is supported and the page will be opened in safe browsing
  82. /// mode. Otherwise, `false`.
  83. /// </returns>
  84. public static bool IsSafeBrowsingSupported {
  85. get {
  86. #if UNITY_EDITOR
  87. return false;
  88. #elif UNITY_IOS
  89. return true;
  90. #elif UNITY_ANDROID
  91. return UniWebViewInterface.IsSafeBrowsingSupported();
  92. #else
  93. return false;
  94. #endif
  95. }
  96. }
  97. /// <summary>
  98. /// Creates a new `UniWebViewSafeBrowsing` instance with a given URL.
  99. /// </summary>
  100. /// <param name="url">The URL to navigate to. The URL must use the `http` or `https` scheme.</param>
  101. /// <returns>A newly created `UniWebViewSafeBrowsing` instance.</returns>
  102. public static UniWebViewSafeBrowsing Create(string url) {
  103. var safeBrowsing = new UniWebViewSafeBrowsing();
  104. if (!UniWebViewHelper.IsEditor) {
  105. safeBrowsing.listener.safeBrowsing = safeBrowsing;
  106. safeBrowsing.Init(url);
  107. }
  108. safeBrowsing.url = url;
  109. return safeBrowsing;
  110. }
  111. /// <summary>
  112. /// Shows the safe browsing content above current screen.
  113. /// </summary>
  114. public void Show() {
  115. if (UniWebViewSafeBrowsing.IsSafeBrowsingSupported) {
  116. UniWebViewInterface.SafeBrowsingShow(listener.Name);
  117. } else {
  118. if (!UniWebViewHelper.IsEditor) {
  119. UniWebViewLogger.Instance.Critical(@"UniWebViewSafeBrowsing.Show is called but the current device does
  120. not support Safe Browsing.
  121. This might be due to Chrome or any other processing app is not installed, or the manifest file not
  122. configured correctly. Check SafeBrowsing Mode guide for more: https://docs.uniwebview.com/guide/safe-browsing.html");
  123. }
  124. Application.OpenURL(url);
  125. }
  126. }
  127. /// <summary>
  128. /// Dismisses the safe browsing component.
  129. ///
  130. /// This method only works on iOS. On Android, there is no way to dismiss the safe browsing component
  131. /// programatically as the result of the limitation from the native (Android) side.
  132. /// </summary>
  133. public void Dismiss() {
  134. #if UNITY_IOS && !UNITY_EDITOR
  135. UniWebViewInterface.SafeBrowsingDismiss(listener.Name);
  136. #endif
  137. }
  138. /// <summary>
  139. /// Sets the color for toolbar background in the safe browsing component. The changes are ignored after `Show`
  140. /// method is called.
  141. /// </summary>
  142. /// <param name="color">The color to tint the toolbar.</param>
  143. public void SetToolbarColor(Color color) {
  144. if (!UniWebViewHelper.IsEditor) {
  145. UniWebViewInterface.SafeBrowsingSetToolbarColor(listener.Name, color.r, color.g, color.b);
  146. }
  147. }
  148. /// <summary>
  149. /// Sets the color for toolbar controls in the safe browsing component. The changes are ignored after `Show` method
  150. /// is called.
  151. ///
  152. /// This method only works on iOS. On Android, the controls color is determined by system to keep a reasonable
  153. /// contrast, based on the toolbar background color you provided in `SetToolbarColor`.
  154. /// </summary>
  155. /// <param name="color">The color to tint the controls on toolbar.</param>
  156. public void SetToolbarItemColor(Color color) {
  157. #if UNITY_IOS && !UNITY_EDITOR
  158. UniWebViewInterface.SafeBrowsingSetToolbarItemColor(listener.Name, color.r, color.g, color.b);
  159. #endif
  160. }
  161. private UniWebViewSafeBrowsing() {
  162. if (!UniWebViewHelper.IsEditor) {
  163. var listenerObject = new GameObject(id);
  164. listener = listenerObject.AddComponent<UniWebViewNativeListener>();
  165. UniWebViewNativeListener.AddListener(listener);
  166. }
  167. }
  168. private void Init(string url) {
  169. UniWebViewInterface.SafeBrowsingInit(listener.Name, url);
  170. }
  171. internal void InternalSafeBrowsingFinished() {
  172. if (OnSafeBrowsingFinished != null) {
  173. OnSafeBrowsingFinished(this);
  174. }
  175. UniWebViewNativeListener.RemoveListener(listener.Name);
  176. Destroy(listener.gameObject);
  177. }
  178. }