EditorWebGLWebViewEmulator.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2024 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. #if UNITY_WEBGL && UNITY_EDITOR
  15. #pragma warning disable CS0618
  16. using System;
  17. using System.Collections;
  18. using UnityEngine;
  19. using UnityEngine.Networking;
  20. using Vuplex.WebView.Internal;
  21. namespace Vuplex.WebView {
  22. // Hooks into the partial methods of MockWebView and StandaloneWebView
  23. // in order to log WebGL-related warnings while running in the editor.
  24. //
  25. // - This file can't be moved to the /Editor folder because it must be
  26. // compiled into the same assembly as StandaloneWebView.cs and MockWebView.cs
  27. //
  28. // - Warnings aren't logged for Click(), Scroll(), or SendKey()
  29. // because those methods aren't normally called when interacting with the WebGL webview
  30. // in Native 2D Mode, but they are called for StandaloneWebView and MockWebView because
  31. // they don't implement IWithNative2DMode.
  32. // It's necessary to explicitly exclude the Linux editor because ConditionalCompilationUtility
  33. // doesn't execute when running in headless mode (i.e. for CI/CD).
  34. #if VUPLEX_STANDALONE && !UNITY_EDITOR_LINUX
  35. partial class StandaloneWebView {
  36. #else
  37. partial class MockWebView {
  38. #endif
  39. partial void OnCanGoBack() {
  40. _logIFrameContentAccessWarningIfNeeded("CanGoBack");
  41. WebGLWarnings.LogCanGoBackOrForwardWarning();
  42. }
  43. partial void OnCanGoForward() {
  44. _logIFrameContentAccessWarningIfNeeded("CanGoBack");
  45. WebGLWarnings.LogCanGoBackOrForwardWarning();
  46. }
  47. partial void OnCaptureScreenshot() => WebGLWarnings.LogCaptureScreenshotError();
  48. partial void OnCopy() => WebGLWarnings.LogCopyError();
  49. partial void OnCut() => WebGLWarnings.LogCutError();
  50. partial void OnExecuteJavaScript() => _logIFrameContentAccessWarningIfNeeded("ExecuteJavaScript");
  51. partial void OnGetRawTextureData() => WebGLWarnings.LogGetRawTextureDataError();
  52. partial void OnGoBack() => _logIFrameContentAccessWarningIfNeeded("GoBack");
  53. partial void OnGoForward() => _logIFrameContentAccessWarningIfNeeded("GoForward");
  54. partial void OnLoadHtml() {
  55. if (_ignoreNextLoadHtmlInvocation) {
  56. _ignoreNextLoadHtmlInvocation = false;
  57. return;
  58. }
  59. _iframeMayBeInaccessible = true;
  60. WebGLWarnings.LogLoadHtmlWarning();
  61. }
  62. partial void OnLoadUrl(string url) {
  63. if (url == null) {
  64. return;
  65. }
  66. var isHttpOrHttpsUrl = url.StartsWith("http://") || url.StartsWith("https://");
  67. // streaming-assets:// URLs will be from the same origin.
  68. _iframeMayBeInaccessible = isHttpOrHttpsUrl;
  69. if (isHttpOrHttpsUrl) {
  70. StartCoroutine(_checkForXFrameOptionsHeader(url));
  71. }
  72. }
  73. partial void OnPaste() => WebGLWarnings.LogPasteError();
  74. partial void OnZoomIn() => WebGLWarnings.LogZoomInError();
  75. partial void OnZoomOut() => WebGLWarnings.LogZoomOutError();
  76. bool _ignoreNextLoadHtmlInvocation;
  77. bool _iframeMayBeInaccessible;
  78. IEnumerator _checkForXFrameOptionsHeader(string url) {
  79. var request = UnityWebRequest.Get(url);
  80. yield return request.SendWebRequest();
  81. if (request.isNetworkError) {
  82. yield break;
  83. }
  84. var headers = request.GetResponseHeaders();
  85. if (headers == null) {
  86. yield break;
  87. }
  88. var headerName = "X-Frame-Options";
  89. string headerValue = null;
  90. headers.TryGetValue(headerName, out headerValue);
  91. if (headerValue == null) {
  92. headers.TryGetValue(headerName.ToLowerInvariant(), out headerValue);
  93. }
  94. if (headerValue == "DENY" || headerValue == "SAMEORIGIN") {
  95. var message = $"2D WebView for WebGL won't be able to load this URL ({url}) when running in the browser because the web page's server blocks it from being loaded in an <iframe> element by sending an \"X-Frame-Options: {headerValue}\" response header.";
  96. WebViewLogger.LogError(message + WebGLWarnings.GetArticleLinkText());
  97. #if VUPLEX_STANDALONE
  98. _ignoreNextLoadHtmlInvocation = true;
  99. var html = $"<p style='font-family: sans-serif; padding: 2em;'>{message.Replace("<", "&lt;").Replace(">", "&gt;")} For more info, please see this article: <a href='https://support.vuplex.com/articles/webgl-limitations'>https://support.vuplex.com/articles/webgl-limitations</a></p>";
  100. LoadHtml(html);
  101. #endif
  102. yield break;
  103. }
  104. }
  105. void _logIFrameContentAccessWarningIfNeeded(string methodName) {
  106. if (_iframeMayBeInaccessible) {
  107. WebGLWarnings.LogIFrameContentAccessWarning(methodName);
  108. }
  109. }
  110. }
  111. }
  112. #endif