UniWebViewSafeBrowsing.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. using Object = UnityEngine.Object;
  20. /// <summary>
  21. /// UniWebView Safe Browsing provides a way for browsing the web content in a more browser-like way, such as Safari on
  22. /// iOS and Chrome on Android.
  23. ///
  24. /// This class wraps `SFSafariViewController` on iOS and "Custom Tabs" on Android. It shares cookies, auto-fill
  25. /// completion and other more data with the browser on device. Most of permissions are also built-in supported. You can
  26. /// use this class for some tasks that are limited for a normal web view, such as using Apple Pay or Progressive Web
  27. /// Apps (PWA).
  28. ///
  29. /// You create a `UniWebViewSafeBrowsing` instance by calling the static `UniWebViewSafeBrowsing.Create` method with a
  30. /// destination URL. You cannot change this URL once the instance is created. To show the safe browsing, call `Show` on
  31. /// the instance. The web content will be displayed in full screen with a toolbar containing the loaded URL, as well
  32. /// as some basic controls like Go Back, Go Forward and Done.
  33. ///
  34. /// Browsing web content in `UniWebViewSafeBrowsing` is only supported on iOS and Android. There is no such component in
  35. /// Unity Editor. Creating and showing a `UniWebViewSafeBrowsing` on Unity Editor will fall back to open the URL in
  36. /// external browser by using Unity's `Application.OpenURL`.
  37. ///
  38. /// </summary>
  39. public class UniWebViewSafeBrowsing {
  40. public enum ColorScheme
  41. {
  42. System = 0,
  43. Light = 1,
  44. Dark = 2
  45. }
  46. public enum ActivityHeightResizeBehavior
  47. {
  48. Default = 0,
  49. Resizable = 1,
  50. Fixed = 2
  51. }
  52. /// <summary>
  53. /// Delegate for safe browsing finish event.
  54. /// </summary>
  55. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  56. public delegate void OnSafeBrowsingFinishedDelegate(UniWebViewSafeBrowsing browsing);
  57. /// <summary>
  58. /// Raised when user dismisses safe browsing by tapping the Done button or Back button.
  59. ///
  60. /// The dismissed safe browsing instance will be invalid after this event being raised, and you should not use
  61. /// it for another browsing purpose. Instead, create a new one for a new browsing session.
  62. ///
  63. /// This event will not happen in Unity Editor, because the whole `UniWebViewSafeBrowsing` will fall back to an
  64. /// external browser.
  65. /// </summary>
  66. public event OnSafeBrowsingFinishedDelegate OnSafeBrowsingFinished;
  67. /// <summary>
  68. /// Delegate for safe browsing close event with metadata payload.
  69. /// </summary>
  70. /// <param name="browsing">The `UniWebViewSafeBrowsing` instance that raised the event.</param>
  71. /// <param name="metadata">Structured metadata describing the native callback.</param>
  72. public delegate void OnSafeBrowsingClosedDelegate(UniWebViewSafeBrowsing browsing, UniWebViewSafeBrowsingEventMetadata metadata);
  73. /// <summary>
  74. /// Raised together with <see cref="OnSafeBrowsingFinished"/> but providing metadata.
  75. /// </summary>
  76. public event OnSafeBrowsingClosedDelegate OnSafeBrowsingClosed;
  77. /// <summary>
  78. /// Delegate for safe browsing started event.
  79. /// </summary>
  80. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  81. /// <param name="metadata">Structured metadata describing the native callback.</param>
  82. public delegate void OnSafeBrowsingNavigationStartedDelegate(UniWebViewSafeBrowsing browsing, UniWebViewSafeBrowsingEventMetadata metadata);
  83. /// <summary>
  84. /// Raised when website starts page loading in safe browsing
  85. ///
  86. /// This event will not happen in Unity Editor or on iOS, because the native API does not expose a navigation-start callback there.
  87. /// </summary>
  88. public event OnSafeBrowsingNavigationStartedDelegate OnSafeBrowsingNavigationStarted;
  89. /// <summary>
  90. /// Delegate for safe browsing finished page loading event.
  91. /// </summary>
  92. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  93. /// <param name="metadata">Structured metadata describing the native callback.</param>
  94. public delegate void OnSafeBrowsingNavigationFinishedDelegate(UniWebViewSafeBrowsing browsing, UniWebViewSafeBrowsingEventMetadata metadata);
  95. /// <summary>
  96. /// Raised when website finishes loading in safe browsing
  97. ///
  98. /// This event will not happen in Unity Editor.
  99. /// </summary>
  100. public event OnSafeBrowsingNavigationFinishedDelegate OnSafeBrowsingNavigationFinished;
  101. /// <summary>
  102. /// Delegate for safe browsing finished loading event.
  103. /// </summary>
  104. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  105. /// <param name="metadata">Structured metadata describing the native callback.</param>
  106. public delegate void OnSafeBrowsingNavigationFailedDelegate(UniWebViewSafeBrowsing browsing, UniWebViewSafeBrowsingEventMetadata metadata);
  107. /// <summary>
  108. /// Raised if the website errors while loading
  109. ///
  110. /// This event will not happen in Unity Editor.
  111. /// </summary>
  112. public event OnSafeBrowsingNavigationFailedDelegate OnSafeBrowsingNavigationFailed;
  113. /// <summary>
  114. /// Delegate for safe browsing warmup complete.
  115. /// </summary>
  116. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  117. /// <param name="metadata">Structured metadata describing the native callback.</param>
  118. public delegate void OnSafeBrowsingWarmupCompleteDelegate(UniWebViewSafeBrowsing browsing, UniWebViewSafeBrowsingEventMetadata metadata);
  119. /// <summary>
  120. /// Raised when the safe browser warmup is complete.
  121. ///
  122. /// Android only. Requires AndroidX Browser 1.8.0+.
  123. /// Note this indicates the browser process warmup callback, not that the page finished loading.
  124. ///
  125. /// This event will not happen in Unity Editor.
  126. /// </summary>
  127. public event OnSafeBrowsingWarmupCompleteDelegate OnSafeBrowsingWarmupComplete;
  128. /// <summary>
  129. /// Delegate for safe browsing minimized.
  130. /// </summary>
  131. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  132. /// <param name="metadata">Structured metadata describing the native callback.</param>
  133. public delegate void OnSafeBrowsingMinimizedDelegate(UniWebViewSafeBrowsing browsing, UniWebViewSafeBrowsingEventMetadata metadata);
  134. /// <summary>
  135. /// Raised when the safe browser is minimized.
  136. ///
  137. /// Android only. Requires AndroidX Browser 1.8.0+.
  138. ///
  139. /// This event will not happen in Unity Editor.
  140. /// </summary>
  141. public event OnSafeBrowsingMinimizedDelegate OnSafeBrowsingMinimized;
  142. /// <summary>
  143. /// Delegate for safe browsing unminimized.
  144. /// </summary>
  145. /// <param name="browsing">The `UniWebViewSafeBrowsing` object raised this event.</param>
  146. /// <param name="metadata">Structured metadata describing the native callback.</param>
  147. public delegate void OnSafeBrowsingUnminimizedDelegate(UniWebViewSafeBrowsing browsing, UniWebViewSafeBrowsingEventMetadata metadata);
  148. /// <summary>
  149. /// Raised when the safe browser is restored from minimized state.
  150. ///
  151. /// Android only. Requires AndroidX Browser 1.8.0+.
  152. ///
  153. /// This event will not happen in Unity Editor.
  154. /// </summary>
  155. public event OnSafeBrowsingUnminimizedDelegate OnSafeBrowsingUnminimized;
  156. private string id = Guid.NewGuid().ToString();
  157. private UniWebViewNativeListener listener;
  158. private bool isDisposed;
  159. #if UNITY_IOS && !UNITY_EDITOR
  160. private bool hasShown;
  161. #endif
  162. // This is only for editor, to open the url in system browser.
  163. private string url;
  164. /// <summary>
  165. /// Whether the safe browsing mode is supported in current runtime or not.
  166. ///
  167. /// If supported, the safe browsing mode will be used when `Show` is called on a `UniWebViewSafeBrowsing` instance.
  168. /// Otherwise, the system default browser will be used to open the page when `Show` is called.
  169. ///
  170. /// This property always returns `true` on iOS runtime platform. On Android, it depends on whether there is an Intent
  171. /// can handle the safe browsing request. Usually it is provided by Chrome. If there is no Intent can open the URL
  172. /// in safe browsing mode, this property will return `false`.
  173. ///
  174. /// To use this API on Android when you set your Target SDK to Android 11 or later, you need to declare the correct
  175. /// intent query explicitly in your AndroidManifest.xml, to follow the Package Visibility
  176. /// (https://developer.android.com/about/versions/11/privacy/package-visibility):
  177. ///
  178. /// ```xml
  179. /// <queries>
  180. /// <intent>
  181. /// <action android:name="android.support.customtabs.action.CustomTabsService" />
  182. /// </intent>
  183. /// </queries>
  184. /// ```
  185. /// </summary>
  186. /// <returns>
  187. /// Returns `true` if the safe browsing mode is supported and the page will be opened in safe browsing
  188. /// mode. Otherwise, `false`.
  189. /// </returns>
  190. public static bool IsSafeBrowsingSupported {
  191. get {
  192. #if UNITY_EDITOR
  193. return false;
  194. #elif UNITY_IOS
  195. return true;
  196. #elif UNITY_ANDROID
  197. return UniWebViewInterface.IsSafeBrowsingSupported();
  198. #else
  199. return false;
  200. #endif
  201. }
  202. }
  203. /// <summary>
  204. /// Returns the Android Custom Tabs provider package name that UniWebView Safe Browsing would use.
  205. ///
  206. /// This is Android-only. It returns `null` if Safe Browsing is not supported or no provider can be resolved.
  207. /// </summary>
  208. /// <returns>The resolved Custom Tabs provider package name on Android; otherwise, `null`.</returns>
  209. public static string GetSafeBrowsingCustomTabsProviderPackageName()
  210. {
  211. #if UNITY_ANDROID && !UNITY_EDITOR
  212. return UniWebViewInterface.GetSafeBrowsingCustomTabsProviderPackageName();
  213. #else
  214. return null;
  215. #endif
  216. }
  217. /// <summary>
  218. /// Sets the preferred browsers for Custom Tabs in order of preference.
  219. /// This allows developers to specify which browsers should be preferred when multiple
  220. /// Custom Tabs providers are available on the device.
  221. ///
  222. /// This setting affects both SafeBrowsing and AuthenticationSession functionality.
  223. ///
  224. /// Browser Selection Priority (Android):
  225. /// 1. User-defined preferred packages (highest priority) - checked in order
  226. /// 2. Default browser if it's Chromium-based (Chrome, Edge, etc.)
  227. /// 3. Default browser if it supports Custom Tabs (even non-Chromium)
  228. /// 4. Any Chromium-based browser (only when no user preference is set)
  229. /// 5. Any available Custom Tabs provider (last resort)
  230. ///
  231. /// This prioritization helps avoid browsers with incomplete Custom Tabs implementations
  232. /// (such as Firefox, which may not trigger onNavigationEvent callbacks properly).
  233. ///
  234. /// On iOS, this method has no effect as Safari is always used for safe browsing.
  235. /// </summary>
  236. /// <param name="packages">Array of browser package names in order of preference. Common package names include:
  237. /// - "com.android.chrome" (Chrome)
  238. /// - "com.brave.browser" (Brave Browser)
  239. /// - "com.opera.browser" (Opera Browser)
  240. /// - "com.microsoft.emmx" (Microsoft Edge)
  241. /// - "com.sec.android.app.sbrowser" (Samsung Internet)
  242. /// </param>
  243. public static void SetPreferredCustomTabsBrowsers(string[] packages) {
  244. #if UNITY_ANDROID && !UNITY_EDITOR
  245. UniWebViewInterface.SetPreferredCustomTabsBrowsers(packages);
  246. #endif
  247. }
  248. /// <summary>
  249. /// Creates a new `UniWebViewSafeBrowsing` instance with a given URL.
  250. /// </summary>
  251. /// <param name="url">The URL to navigate to. The URL must use the `http` or `https` scheme.</param>
  252. /// <returns>A newly created `UniWebViewSafeBrowsing` instance.</returns>
  253. public static UniWebViewSafeBrowsing Create(string url) {
  254. var safeBrowsing = new UniWebViewSafeBrowsing();
  255. if (!UniWebViewHelper.IsEditor) {
  256. safeBrowsing.listener.safeBrowsing = safeBrowsing;
  257. safeBrowsing.Init(url);
  258. }
  259. safeBrowsing.url = url;
  260. return safeBrowsing;
  261. }
  262. /// <summary>
  263. /// Shows the safe browsing content above current screen.
  264. /// </summary>
  265. public void Show() {
  266. if (UniWebViewSafeBrowsing.IsSafeBrowsingSupported) {
  267. if (!EnsureNativeInvocationAvailable(nameof(Show))) {
  268. return;
  269. }
  270. #if UNITY_IOS && !UNITY_EDITOR
  271. hasShown = true;
  272. #endif
  273. UniWebViewInterface.SafeBrowsingShow(listener.Name);
  274. } else {
  275. if (!UniWebViewHelper.IsEditor) {
  276. UniWebViewLogger.Instance.Critical(@"UniWebViewSafeBrowsing.Show is called but the current device does
  277. not support Safe Browsing.
  278. This might be due to Chrome or any other processing app is not installed, or the manifest file not
  279. configured correctly. Check SafeBrowsing Mode guide for more: https://docs.uniwebview.com/guide/safe-browsing.html");
  280. }
  281. Application.OpenURL(url);
  282. }
  283. }
  284. /// <summary>
  285. /// Dismisses the safe browsing component.
  286. ///
  287. /// This method only works on iOS. On Android, there is no way to dismiss the safe browsing component
  288. /// programatically as the result of the limitation from the native (Android) side.
  289. /// </summary>
  290. public void Dismiss() {
  291. #if UNITY_IOS && !UNITY_EDITOR
  292. if (!EnsureNativeInvocationAvailable(nameof(Dismiss))) {
  293. return;
  294. }
  295. UniWebViewInterface.SafeBrowsingDismiss(listener.Name);
  296. #endif
  297. }
  298. /// <summary>
  299. /// Invalidates this safe browsing instance and releases underlying native resources without waiting for a native close event.
  300. /// </summary>
  301. public void Invalidate() {
  302. if (isDisposed) {
  303. UniWebViewLogger.Instance.Debug("Safe Browsing instance already invalidated.");
  304. return;
  305. }
  306. #if UNITY_IOS && !UNITY_EDITOR
  307. if (!EnsureNativeInvocationAvailable(nameof(Invalidate))) {
  308. return;
  309. }
  310. UniWebViewInterface.SafeBrowsingInvalidate(listener.Name);
  311. if (!hasShown) {
  312. CleanupSafeBrowsing();
  313. }
  314. #else
  315. if (!UniWebViewHelper.IsEditor) {
  316. if (!EnsureNativeInvocationAvailable(nameof(Invalidate))) {
  317. return;
  318. }
  319. UniWebViewInterface.SafeBrowsingInvalidate(listener.Name);
  320. }
  321. CleanupSafeBrowsing();
  322. #endif
  323. }
  324. /// <summary>
  325. /// Sets the color for toolbar background in the safe browsing component. The changes are ignored after `Show`
  326. /// method is called.
  327. /// </summary>
  328. /// <param name="color">The color to tint the toolbar.</param>
  329. public void SetToolbarColor(Color color)
  330. {
  331. if (!UniWebViewHelper.IsEditor)
  332. {
  333. if (!EnsureNativeInvocationAvailable(nameof(SetToolbarColor)))
  334. {
  335. return;
  336. }
  337. UniWebViewInterface.SafeBrowsingSetToolbarColor(listener.Name, color.r, color.g, color.b);
  338. }
  339. }
  340. /// <summary>
  341. /// Sets the color scheme applied to the Custom Tab UI. Ignored after `Show`.
  342. ///
  343. /// Android only. This method does nothing on iOS.
  344. /// </summary>
  345. /// <param name="colorScheme">Specify a specific color scheme for the browsing component.</param>
  346. public void SetColorScheme(ColorScheme colorScheme)
  347. {
  348. #if UNITY_ANDROID && !UNITY_EDITOR
  349. if (!EnsureNativeInvocationAvailable(nameof(SetColorScheme)))
  350. {
  351. return;
  352. }
  353. UniWebViewInterface.SafeBrowsingSetColorScheme(listener.Name, (int)colorScheme);
  354. #endif
  355. }
  356. /// <summary>
  357. /// Changes the target URL before the tab is shown (handy for prefetch/warm-up). Ignored after `Show`.
  358. ///
  359. /// Android only. This method does nothing on iOS.
  360. /// </summary>
  361. /// <param name="url">url to switch to</param>
  362. public void ChangeUrl(string url)
  363. {
  364. #if UNITY_ANDROID && !UNITY_EDITOR
  365. if (!EnsureNativeInvocationAvailable(nameof(ChangeUrl)))
  366. {
  367. return;
  368. }
  369. UniWebViewInterface.SafeBrowsingChangeUrl(listener.Name, url);
  370. #endif
  371. }
  372. /// <summary>
  373. /// Sets the secondary (bottom) toolbar color. Ignored after `Show`.
  374. ///
  375. /// Android only. This method does nothing on iOS.
  376. /// </summary>
  377. /// <param name="color">The color to tint the secondary toolbar.</param>
  378. public void SetSecondaryToolbarColor(Color color)
  379. {
  380. #if UNITY_ANDROID && !UNITY_EDITOR
  381. if (!EnsureNativeInvocationAvailable(nameof(SetSecondaryToolbarColor)))
  382. {
  383. return;
  384. }
  385. UniWebViewInterface.SafeBrowsingSetSecondaryToolbarColor(listener.Name, color.r, color.g, color.b);
  386. #endif
  387. }
  388. /// <summary>
  389. /// Sets the navigation bar color. Ignored after `Show`.
  390. ///
  391. /// Android only. This method does nothing on iOS.
  392. /// </summary>
  393. /// <param name="color">The color to tint the navigation toolbar.</param>
  394. public void SetNavigationBarColor(Color color)
  395. {
  396. #if UNITY_ANDROID && !UNITY_EDITOR
  397. if (!EnsureNativeInvocationAvailable(nameof(SetNavigationBarColor)))
  398. {
  399. return;
  400. }
  401. UniWebViewInterface.SafeBrowsingSetNavigationBarColor(listener.Name, color.r, color.g, color.b);
  402. #endif
  403. }
  404. /// <summary>
  405. /// Sets the navigation bar divider color. Ignored after `Show`.
  406. ///
  407. /// Android only. This method does nothing on iOS.
  408. /// </summary>
  409. /// <param name="color">The color to tint the navigation toolbar.</param>
  410. public void SetNavigationBarDividerColor(Color color)
  411. {
  412. #if UNITY_ANDROID && !UNITY_EDITOR
  413. if (!EnsureNativeInvocationAvailable(nameof(SetNavigationBarDividerColor)))
  414. {
  415. return;
  416. }
  417. UniWebViewInterface.SafeBrowsingSetNavigationBarDividerColor(listener.Name, color.r, color.g, color.b);
  418. #endif
  419. }
  420. /// <summary>
  421. /// Sets the toolbar corner radius in dp. Ignored after `Show`.
  422. ///
  423. /// Android only. This method does nothing on iOS.
  424. /// </summary>
  425. /// <param name="cornerRadiusDp">Toolbar corner radius dp.</param>
  426. public void SetToolbarCornerRadiusDp(int cornerRadiusDp)
  427. {
  428. #if UNITY_ANDROID && !UNITY_EDITOR
  429. if (!EnsureNativeInvocationAvailable(nameof(SetToolbarCornerRadiusDp)))
  430. {
  431. return;
  432. }
  433. UniWebViewInterface.SafeBrowsingSetToolbarCornerRadiusDp(listener.Name, cornerRadiusDp);
  434. #endif
  435. }
  436. /// <summary>
  437. /// Sets the Custom Tab Activity's initial height in pixels with a resize behavior.
  438. ///
  439. /// Android only. This method does nothing on iOS.
  440. /// </summary>
  441. /// <param name="initialHeightPx">Partial custom tab initial height in pixels.</param>
  442. /// <param name="resizeBehavior">Resize behavior applied to the partial custom tab (`Default`, `Resizable`, or `Fixed`).</param>
  443. public void SetInitialHeightPx(int initialHeightPx, ActivityHeightResizeBehavior resizeBehavior = ActivityHeightResizeBehavior.Fixed)
  444. {
  445. #if UNITY_ANDROID && !UNITY_EDITOR
  446. if (!EnsureNativeInvocationAvailable(nameof(SetInitialHeightPx)))
  447. {
  448. return;
  449. }
  450. UniWebViewInterface.SafeBrowsingSetInitialHeightPx(listener.Name, initialHeightPx, (int)resizeBehavior);
  451. #endif
  452. }
  453. /// <summary>
  454. /// Sets the Custom Tab Activity's initial width in pixels. Defaults to no resize.
  455. ///
  456. /// Android only. Requires AndroidX Browser 1.8.0+. This method does nothing on iOS.
  457. /// </summary>
  458. /// <param name="initialWidthPx">partial custom tab initial width in pixels</param>
  459. public void SetInitialWidthPx(int initialWidthPx)
  460. {
  461. #if UNITY_ANDROID && !UNITY_EDITOR
  462. if (!EnsureNativeInvocationAvailable(nameof(SetInitialWidthPx)))
  463. {
  464. return;
  465. }
  466. UniWebViewInterface.SafeBrowsingSetInitialWidthPx(listener.Name, initialWidthPx);
  467. #endif
  468. }
  469. /// <summary>
  470. /// Enables or disables share menu items. Ignored after `Show`.
  471. ///
  472. /// Android only. This method does nothing on iOS.
  473. /// </summary>
  474. /// <param name="enable">enabling sharing with other browsers</param>
  475. public void SetShareMenuItemEnabled(bool enable)
  476. {
  477. #if UNITY_ANDROID && !UNITY_EDITOR
  478. if (!EnsureNativeInvocationAvailable(nameof(SetShareMenuItemEnabled)))
  479. {
  480. return;
  481. }
  482. UniWebViewInterface.SafeBrowsingSetShareMenuItemEnabled(listener.Name, enable);
  483. #endif
  484. }
  485. /// <summary>
  486. /// Enables URL bar hiding on scroll. Ignored after `Show`.
  487. ///
  488. /// Android only. This method does nothing on iOS.
  489. /// </summary>
  490. /// <param name="enable">enables url hiding on scroll down</param>
  491. public void SetUrlBarHidingEnabled(bool enable)
  492. {
  493. #if UNITY_ANDROID && !UNITY_EDITOR
  494. if (!EnsureNativeInvocationAvailable(nameof(SetUrlBarHidingEnabled)))
  495. {
  496. return;
  497. }
  498. UniWebViewInterface.SafeBrowsingSetUrlBarHidingEnabled(listener.Name, enable);
  499. #endif
  500. }
  501. /// <summary>
  502. /// Sends redirects to the system default handler when enabled. Ignored after `Show`.
  503. ///
  504. /// Android only. Requires AndroidX Browser 1.7.0+. This method does nothing on iOS.
  505. /// </summary>
  506. /// <param name="enable">enables sending to external handler for links tapped.</param>
  507. public void SetSendToExternalDefaultHandlerEnabled(bool enable)
  508. {
  509. #if UNITY_ANDROID && !UNITY_EDITOR
  510. if (!EnsureNativeInvocationAvailable(nameof(SetSendToExternalDefaultHandlerEnabled)))
  511. {
  512. return;
  513. }
  514. UniWebViewInterface.SafeBrowsingSetSendToExternalDefaultHandlerEnabled(listener.Name, enable);
  515. #endif
  516. }
  517. /// <summary>
  518. /// Toggles the maximization button for partial custom tabs. Ignored after `Show`.
  519. ///
  520. /// Android only. Requires AndroidX Browser 1.8.0+. This method does nothing on iOS.
  521. /// </summary>
  522. /// <param name="enable">enables whether users can maximize webview when its a partial one.</param>
  523. public void SetMaximizationEnabled(bool enable)
  524. {
  525. #if UNITY_ANDROID && !UNITY_EDITOR
  526. if (!EnsureNativeInvocationAvailable(nameof(SetMaximizationEnabled)))
  527. {
  528. return;
  529. }
  530. UniWebViewInterface.SafeBrowsingSetMaximizationEnabled(listener.Name, enable);
  531. #endif
  532. }
  533. /// <summary>
  534. /// Toggles the download button in the overflow menu. Ignored after `Show`.
  535. ///
  536. /// Android only. Requires AndroidX Browser 1.7.0+. This method does nothing on iOS.
  537. /// </summary>
  538. /// <param name="enable">enables whether users can maximize webview when its a partial one.</param>
  539. public void SetDownloadButtonEnabled(bool enable)
  540. {
  541. #if UNITY_ANDROID && !UNITY_EDITOR
  542. if (!EnsureNativeInvocationAvailable(nameof(SetDownloadButtonEnabled)))
  543. {
  544. return;
  545. }
  546. UniWebViewInterface.SafeBrowsingSetDownloadButtonEnabled(listener.Name, enable);
  547. #endif
  548. }
  549. /// <summary>
  550. /// Toggles the bookmarks button in the overflow menu. Enabled by default. Ignored after `Show`.
  551. ///
  552. /// Android only. Requires AndroidX Browser 1.7.0+. This method does nothing on iOS.
  553. /// </summary>
  554. /// <param name="enable">enables bookmark button</param>
  555. public void SetBookmarksButtonEnabled(bool enable)
  556. {
  557. #if UNITY_ANDROID && !UNITY_EDITOR
  558. if (!EnsureNativeInvocationAvailable(nameof(SetBookmarksButtonEnabled)))
  559. {
  560. return;
  561. }
  562. UniWebViewInterface.SafeBrowsingSetBookmarksButtonEnabled(listener.Name, enable);
  563. #endif
  564. }
  565. /// <summary>
  566. /// Allows background interaction when a partial Custom Tab is shown. Ignored after `Show`.
  567. ///
  568. /// Android only. Requires AndroidX Browser 1.7.0+. This method does nothing on iOS.
  569. /// </summary>
  570. /// <param name="enable">enables background interactions while partial webview is showing</param>
  571. public void SetBackgroundInteractionEnabled(bool enable)
  572. {
  573. #if UNITY_ANDROID && !UNITY_EDITOR
  574. if (!EnsureNativeInvocationAvailable(nameof(SetBackgroundInteractionEnabled)))
  575. {
  576. return;
  577. }
  578. UniWebViewInterface.SafeBrowsingSetBackgroundInteractionEnabled(listener.Name, enable);
  579. #endif
  580. }
  581. /// <summary>
  582. /// Warms up the browser process asynchronously to speed up later `Show`. Can be called multiple times.
  583. ///
  584. /// Android only. This method does nothing on iOS.
  585. /// </summary>
  586. /// <param name="enable">enable warmup</param>
  587. public void SetWarmup(bool enable)
  588. {
  589. #if UNITY_ANDROID && !UNITY_EDITOR
  590. if (!EnsureNativeInvocationAvailable(nameof(SetWarmup)))
  591. {
  592. return;
  593. }
  594. UniWebViewInterface.SafeBrowsingSetWarmup(listener.Name, enable);
  595. #endif
  596. }
  597. /// Uses Custom Tabs prefetch (mayLaunchUrl). Ignored after `Show`.
  598. ///
  599. /// Android only. This method does nothing on iOS.
  600. /// If `alternativeUrl` is provided, it will be used for prefetching instead of the current URL.
  601. public void SetPrefetch(bool enable, string alternativeUrl = null)
  602. {
  603. #if UNITY_ANDROID && !UNITY_EDITOR
  604. if (!EnsureNativeInvocationAvailable(nameof(SetPrefetch)))
  605. {
  606. return;
  607. }
  608. UniWebViewInterface.SafeBrowsingSetPrefetch(listener.Name, enable, alternativeUrl);
  609. #endif
  610. }
  611. /// <summary>
  612. /// Sets the color for toolbar controls in the safe browsing component. The changes are ignored after `Show` method
  613. /// is called.
  614. ///
  615. /// This method only works on iOS. On Android, the controls color is determined by system to keep a reasonable
  616. /// contrast, based on the toolbar background color you provided in `SetToolbarColor`.
  617. /// </summary>
  618. /// <param name="color">The color to tint the controls on toolbar.</param>
  619. public void SetToolbarItemColor(Color color) {
  620. #if UNITY_IOS && !UNITY_EDITOR
  621. if (!EnsureNativeInvocationAvailable(nameof(SetToolbarItemColor)))
  622. {
  623. return;
  624. }
  625. UniWebViewInterface.SafeBrowsingSetToolbarItemColor(listener.Name, color.r, color.g, color.b);
  626. #endif
  627. }
  628. private UniWebViewSafeBrowsing() {
  629. if (!UniWebViewHelper.IsEditor) {
  630. var listenerObject = new GameObject(id);
  631. listener = listenerObject.AddComponent<UniWebViewNativeListener>();
  632. UniWebViewNativeListener.AddListener(listener);
  633. }
  634. }
  635. private void Init(string url) {
  636. UniWebViewInterface.SafeBrowsingInit(listener.Name, url);
  637. }
  638. internal void InternalSafeBrowsingEvent(string metadata) {
  639. var parsed = UniWebViewSafeBrowsingEventMetadata.FromRaw(
  640. metadata,
  641. UniWebViewSafeBrowsingEventMetadata.EventKind.Unknown
  642. );
  643. if (!string.IsNullOrEmpty(parsed.Raw)) {
  644. UniWebViewLogger.Instance.Verbose("Safe Browsing metadata (" + parsed.Kind + "): " + parsed.Raw);
  645. }
  646. switch (parsed.Kind) {
  647. case UniWebViewSafeBrowsingEventMetadata.EventKind.NavigationStarted:
  648. if (OnSafeBrowsingNavigationStarted != null) {
  649. OnSafeBrowsingNavigationStarted(this, parsed);
  650. }
  651. break;
  652. case UniWebViewSafeBrowsingEventMetadata.EventKind.NavigationFinished:
  653. if (OnSafeBrowsingNavigationFinished != null) {
  654. OnSafeBrowsingNavigationFinished(this, parsed);
  655. }
  656. break;
  657. case UniWebViewSafeBrowsingEventMetadata.EventKind.NavigationFailed:
  658. if (OnSafeBrowsingNavigationFailed != null) {
  659. OnSafeBrowsingNavigationFailed(this, parsed);
  660. }
  661. break;
  662. case UniWebViewSafeBrowsingEventMetadata.EventKind.Minimized:
  663. if (OnSafeBrowsingMinimized != null) {
  664. OnSafeBrowsingMinimized(this, parsed);
  665. }
  666. break;
  667. case UniWebViewSafeBrowsingEventMetadata.EventKind.Unminimized:
  668. if (OnSafeBrowsingUnminimized != null) {
  669. OnSafeBrowsingUnminimized(this, parsed);
  670. }
  671. break;
  672. case UniWebViewSafeBrowsingEventMetadata.EventKind.WarmupComplete:
  673. if (OnSafeBrowsingWarmupComplete != null) {
  674. OnSafeBrowsingWarmupComplete(this, parsed);
  675. }
  676. break;
  677. case UniWebViewSafeBrowsingEventMetadata.EventKind.TabHidden:
  678. if (OnSafeBrowsingClosed != null) {
  679. OnSafeBrowsingClosed(this, parsed);
  680. }
  681. if (OnSafeBrowsingFinished != null) {
  682. OnSafeBrowsingFinished(this);
  683. }
  684. CleanupSafeBrowsing();
  685. break;
  686. default:
  687. UniWebViewLogger.Instance.Debug("Unknown Safe Browsing event metadata: " + parsed.Raw);
  688. break;
  689. }
  690. }
  691. private bool EnsureNativeInvocationAvailable(string action)
  692. {
  693. if (isDisposed || listener == null)
  694. {
  695. UniWebViewLogger.Instance.Critical($"Safe Browsing instance already disposed. Skip {action}.");
  696. return false;
  697. }
  698. return true;
  699. }
  700. private void CleanupSafeBrowsing() {
  701. if (isDisposed) {
  702. return;
  703. }
  704. isDisposed = true;
  705. if (listener == null) {
  706. return;
  707. }
  708. UniWebViewNativeListener.RemoveListener(listener.Name);
  709. Object.Destroy(listener.gameObject);
  710. listener = null;
  711. }
  712. }