Web.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright (c) 2022 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.Threading.Tasks;
  16. using UnityEngine;
  17. namespace Vuplex.WebView {
  18. /// <summary>
  19. /// `Web` is the top-level static class for the 3D WebView plugin.
  20. /// It contains static methods for configuring the module and creating resources.
  21. /// </summary>
  22. /// <seealso cref="WebViewPrefab"/>
  23. /// <seealso cref="CanvasWebViewPrefab"/>
  24. /// <seealso cref="IWebView"/>
  25. public static class Web {
  26. /// <summary>
  27. /// Returns the ICookieManager for managing HTTP cookies, or `null` if ICookieManager
  28. /// isn't supported on the current platform.
  29. /// </summary>
  30. /// <remarks>
  31. /// ICookieManager is supported by all of the 3D WebView packages except for:
  32. /// - 3D WebView for Android with Gecko Engine
  33. /// - 2D WebView for WebGL
  34. /// </remarks>
  35. public static ICookieManager CookieManager {
  36. get { return _pluginFactory.GetPlugin().CookieManager; }
  37. }
  38. /// <summary>
  39. /// Gets the default 3D WebView plugin type among those
  40. /// installed for the current platform.
  41. /// </summary>
  42. public static WebPluginType DefaultPluginType {
  43. get { return _pluginFactory.GetPlugin().Type; }
  44. }
  45. /// <summary>
  46. /// Clears all data that persists between webview instances,
  47. /// including cookies, storage, and cached resources.
  48. /// </summary>
  49. /// <example>
  50. /// <code>
  51. /// void Awake() {
  52. /// Web.ClearAllData();
  53. /// }
  54. /// </code>
  55. /// </example>
  56. /// <remarks>
  57. /// Important notes:
  58. /// <list type="bullet">
  59. /// <item>On Windows and macOS, this method cannot be executed while the Chromium browser process is running. So, you will likely need to call it from Awake() to ensure that it's executed before Chromium is started. Alternatively, you can manually terminate Chromium prior to calling this method using StandaloneWebView.TerminateBrowserProcess().</item>
  60. /// <item>On Universal Windows Platform, this method is unable to clear cookies due to a UWP limitation.</item>
  61. /// </list>
  62. /// </remarks>
  63. /// <seealso cref="StandaloneWebView.DeleteAllCookies"/>
  64. public static void ClearAllData() {
  65. _pluginFactory.GetPlugin().ClearAllData();
  66. }
  67. /// <summary>
  68. /// Creates a new webview in a platform-agnostic way. After a webview
  69. /// is created, it must be initialized by calling its Init() method.
  70. /// </summary>
  71. /// <remarks>
  72. /// Note that WebViewPrefab takes care of creating and managing
  73. /// an IWebView instance for you, so you only need to call this method directly
  74. /// if you need to create an IWebView instance outside of a prefab
  75. /// (for example, to connect it to your own custom GameObject).
  76. /// </remarks>
  77. /// <example>
  78. /// <code>
  79. /// var webView = Web.CreateWebView();
  80. /// // Initialize the webview to 600px x 300px.
  81. /// await webView.Init(600, 300);
  82. /// webView.LoadUrl("https://vuplex.com");
  83. /// // Set the Material attached to this GameObject to display the webview.
  84. /// GetComponent&lt;Renderer&gt;().material = webView.CreateMaterial();
  85. /// </code>
  86. /// </example>
  87. public static IWebView CreateWebView() {
  88. return _pluginFactory.GetPlugin().CreateWebView();
  89. }
  90. /// <summary>
  91. /// Like CreateWebView(), except an array of preferred plugin types can be
  92. /// provided to override which 3D WebView plugin is used in the case where
  93. /// multiple plugins are installed for the same build platform.
  94. /// </summary>
  95. /// <remarks>
  96. /// Currently, Android is the only platform that supports multiple 3D WebView
  97. /// plugins: WebPluginType.Android and WebPluginType.AndroidGecko. If both
  98. /// plugins are installed in the same project, WebPluginType.AndroidGecko will be used by default.
  99. /// However, you can override this to force WebPluginType.Android to be used instead by passing
  100. /// `new WebPluginType[] { WebPluginType.Android }`.
  101. /// </remarks>
  102. public static IWebView CreateWebView(WebPluginType[] preferredPlugins) {
  103. return _pluginFactory.GetPlugin(preferredPlugins).CreateWebView();
  104. }
  105. /// <summary>
  106. /// Enables [remote debugging](https://support.vuplex.com/articles/how-to-debug-web-content).
  107. /// </summary>
  108. /// <remarks>
  109. /// On Windows and macOS, this method can only be called prior to initializing any webviews.
  110. /// </remarks>
  111. /// <example>
  112. /// <code>
  113. /// void Awake() {
  114. /// Web.EnableRemoteDebugging();
  115. /// }
  116. /// </code>
  117. /// </example>
  118. public static void EnableRemoteDebugging() {
  119. _pluginFactory.GetPlugin().EnableRemoteDebugging();
  120. }
  121. /// <summary>
  122. /// By default, browsers block sites from autoplaying video with audio,
  123. /// but this method can be used to enable autoplay.
  124. /// </summary>
  125. /// <example>
  126. /// <code>
  127. /// void Awake() {
  128. /// Web.SetAutoplayEnabled(true);
  129. /// }
  130. /// </code>
  131. /// </example>
  132. /// <remarks>
  133. /// Important notes:
  134. /// <list type="bullet">
  135. /// <item>
  136. /// On Windows and macOS, this method cannot be executed while the Chromium browser process is running. So, you will likely need to call it from Awake() to ensure that it's executed before Chromium is started. Alternatively, you can manually terminate Chromium prior to calling this method using StandaloneWebView.TerminateBrowserProcess().
  137. /// </item>
  138. /// <item>
  139. /// This method works for every package except for 3D WebView for UWP,
  140. /// because the underlying UWP WebView control doesn't allow autoplaying
  141. /// video with audio.
  142. /// </item>
  143. /// </list>
  144. /// </remarks>
  145. public static void SetAutoplayEnabled(bool enabled) {
  146. _pluginFactory.GetPlugin().SetAutoplayEnabled(enabled);
  147. }
  148. /// <summary>
  149. /// By default, web pages cannot access the device's
  150. /// camera or microphone via JavaScript, but this method can
  151. /// be used to grant **all web pages** access to the camera and microphone.
  152. /// This is useful, for example, to enable WebRTC support. Note that on
  153. /// Android, iOS, and UWP, [additional project configuration is needed in order to enable
  154. /// permission for the camera and microphone](https://support.vuplex.com/articles/webrtc).
  155. /// </summary>
  156. /// <remarks>
  157. /// Important notes:
  158. /// <list type="bullet">
  159. /// <item>
  160. /// On Windows and macOS, this method cannot be executed while the Chromium browser process is running. So, you will likely need to call it from Awake() to ensure that it's executed before Chromium is started. Alternatively, you can manually terminate Chromium prior to calling this method using StandaloneWebView.TerminateBrowserProcess().
  161. /// </item>
  162. /// <item>
  163. /// On iOS, enabling the camera and microphone is only supported in iOS 14.3 or newer
  164. /// and is only supported in Native 2D Mode.
  165. /// </item>
  166. /// </list>
  167. /// </remarks>
  168. /// <example>
  169. /// <code>
  170. /// void Awake() {
  171. /// Web.SetCameraAndMicrophoneEnabled(true);
  172. /// }
  173. /// </code>
  174. /// </example>
  175. public static void SetCameraAndMicrophoneEnabled(bool enabled) {
  176. _pluginFactory.GetPlugin().SetCameraAndMicrophoneEnabled(enabled);
  177. }
  178. /// <summary>
  179. /// By default, browsers block https URLs with invalid SSL certificates
  180. /// from being loaded, but this method can be used to disable those
  181. /// certificate errors.
  182. /// </summary>
  183. /// <example>
  184. /// <code>
  185. /// void Awake() {
  186. /// Web.SetIgnoreCertificateErrors(true);
  187. /// }
  188. /// </code>
  189. /// </example>
  190. /// <remarks>
  191. /// Important notes:
  192. /// <list type="bullet">
  193. /// <item>
  194. /// On Windows and macOS, this method cannot be executed while the Chromium browser process is running. So, you will likely need to call it from Awake() to ensure that it's executed before Chromium is started. Alternatively, you can manually terminate Chromium prior to calling this method using StandaloneWebView.TerminateBrowserProcess().
  195. /// </item>
  196. /// <item>
  197. /// This method works for every package except for 3D WebView for UWP.
  198. /// For UWP, certificates must be [whitelisted in the Package.appxmanifest file](https://www.suchan.cz/2015/10/displaying-https-page-with-invalid-certificate-in-uwp-webview/).
  199. /// </item>
  200. /// </list>
  201. /// </remarks>
  202. public static void SetIgnoreCertificateErrors(bool ignore) {
  203. _pluginFactory.GetPlugin().SetIgnoreCertificateErrors(ignore);
  204. }
  205. /// <summary>
  206. /// Controls whether data like cookies, localStorage, and cached resources
  207. /// is persisted between webview instances. The default is `true`, but this
  208. /// can be set to `false` to achieve an "incognito mode".
  209. /// </summary>
  210. /// <remarks>
  211. /// On Windows and macOS, this method cannot be executed while the Chromium browser process is running. So, you will likely need to call it from Awake() to ensure that it's executed before Chromium is started. Alternatively, you can manually terminate Chromium prior to calling this method using StandaloneWebView.TerminateBrowserProcess().
  212. /// </remarks>
  213. /// <example>
  214. /// <code>
  215. /// void Awake() {
  216. /// Web.SetStorageEnabled(false);
  217. /// }
  218. /// </code>
  219. /// </example>
  220. public static void SetStorageEnabled(bool enabled) {
  221. _pluginFactory.GetPlugin().SetStorageEnabled(enabled);
  222. }
  223. /// <summary>
  224. /// Globally configures all webviews to use a mobile or desktop
  225. /// [User-Agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent).
  226. /// By default, webviews use the browser engine's default User-Agent, but you
  227. /// can force them to use a mobile User-Agent by calling `Web.SetUserAgent(true)` or a
  228. /// desktop User-Agent with `Web.SetUserAgent(false)`.
  229. /// </summary>
  230. /// <remarks>
  231. /// On Windows and macOS, this method cannot be executed while the Chromium browser process is running. So, you will likely need to call it from Awake() to ensure that it's executed before Chromium is started. Alternatively, you can manually terminate Chromium prior to calling this method using StandaloneWebView.TerminateBrowserProcess().
  232. /// </remarks>
  233. /// <example>
  234. /// <code>
  235. /// void Awake() {
  236. /// // Use a desktop User-Agent.
  237. /// Web.SetUserAgent(false);
  238. /// }
  239. /// </code>
  240. /// </example>
  241. /// <seealso cref="IWithSettableUserAgent"/>
  242. public static void SetUserAgent(bool mobile) {
  243. _pluginFactory.GetPlugin().SetUserAgent(mobile);
  244. }
  245. /// <summary>
  246. /// Globally configures all webviews to use a custom
  247. /// [User-Agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent).
  248. /// </summary>
  249. /// <remarks>
  250. /// On Windows and macOS, this method cannot be executed while the Chromium browser process is running. So, you will likely need to call it from Awake() to ensure that it's executed before Chromium is started. Alternatively, you can manually terminate Chromium prior to calling this method using StandaloneWebView.TerminateBrowserProcess().
  251. /// </remarks>
  252. /// <example>
  253. /// <code>
  254. /// void Awake() {
  255. /// // Use FireFox's User-Agent.
  256. /// Web.SetUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0");
  257. /// }
  258. /// </code>
  259. /// </example>
  260. /// <seealso cref="IWithSettableUserAgent"/>
  261. public static void SetUserAgent(string userAgent) {
  262. _pluginFactory.GetPlugin().SetUserAgent(userAgent);
  263. }
  264. static internal void SetPluginFactory(WebPluginFactory pluginFactory) => _pluginFactory = pluginFactory;
  265. static WebPluginFactory _pluginFactory = new WebPluginFactory();
  266. #region Obsolete APIs
  267. const string CreateMaterialMessage = "Web.CreateMaterial() is now deprecated in v4. Please use IWebView.CreateMaterial() instead: https://developer.vuplex.com/webview/IWebView#CreateMaterial";
  268. const string CreateTextureMessage = "Web.CreateTexture() has been removed in v4 because IWebView instances now automatically create their own textures. For more details, please see this article: https://support.vuplex.com/articles/v4-changes#init";
  269. // Added in v1.0, deprecated in v3.16.
  270. [Obsolete(CreateMaterialMessage)]
  271. public static Task<Material> CreateMaterial() {
  272. var taskSource = new TaskCompletionSource<Material>();
  273. _pluginFactory.GetPlugin().CreateMaterial(taskSource.SetResult);
  274. return taskSource.Task;
  275. }
  276. // Added in v3.8, deprecated in v4.0.
  277. [Obsolete(CreateMaterialMessage)]
  278. public static void CreateMaterial(Action<Material> callback) => _pluginFactory.GetPlugin().CreateMaterial(callback);
  279. // Added in v3.10, removed in v4.0.
  280. [Obsolete(CreateTextureMessage, true)]
  281. public static Task<Texture2D> CreateTexture(int width, int height) { return null; }
  282. // Added in v1.0, deprecated in v3.16, removed in v4.0.
  283. [Obsolete(CreateTextureMessage, true)]
  284. public static void CreateTexture(float width, float height, Action<Texture2D> callback) {}
  285. // Added in v1.0, removed in v4.0.
  286. [Obsolete("Web.CreateVideoMaterial() has been removed. Please use IWithFallbackVideo.CreateVideoMaterial() instead: https://developer.vuplex.com/webview/IWithFallbackVideo#CreateVideoMaterial", true)]
  287. public static void CreateVideoMaterial(Action<Material> callback) {}
  288. // Added in v3.10, deprecated in v3.14, removed in v4.0.
  289. [Obsolete("Web.SetTouchScreenKeyboardEnabled() has been removed. Please use the NativeOnScreenKeyboardEnabled property of WebViewPrefab / CanvasWebViewPrefab or the IWithNativeOnScreenKeyboard interface instead: https://developer.vuplex.com/webview/WebViewPrefab#NativeOnScreenKeyboardEnabled", true)]
  290. public static void SetTouchScreenKeyboardEnabled(bool enabled) {}
  291. #endregion
  292. }
  293. }