From 03f2be66ea268a1ea285899f56f55b81e5a25044 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Thu, 8 Jun 2023 07:20:21 -0300 Subject: [PATCH] [wasm][debugging] Enable debugging when Device Toolbar is enabled and Iphone is selected (#86920) * Enable debugging when Device Toolbar is enabled and Iphone is selected * unify browser detection Co-authored-by: pavelsavara --- src/mono/wasm/runtime/loader/blazor/_Polyfill.ts | 24 ++---------------------- src/mono/wasm/runtime/loader/polyfills.ts | 16 ++++++++++++++-- src/mono/wasm/runtime/loader/run.ts | 6 +++--- src/mono/wasm/runtime/scheduling.ts | 14 ++------------ src/mono/wasm/runtime/types/internal.ts | 3 +++ 5 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/mono/wasm/runtime/loader/blazor/_Polyfill.ts b/src/mono/wasm/runtime/loader/blazor/_Polyfill.ts index 4eeefe2..ea9b13b 100644 --- a/src/mono/wasm/runtime/loader/blazor/_Polyfill.ts +++ b/src/mono/wasm/runtime/loader/blazor/_Polyfill.ts @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. import type { BootJsonData } from "../../types/blazor"; +import { loaderHelpers } from "../globals"; let testAnchor: HTMLAnchorElement; export function toAbsoluteUri(relativeUri: string): string { @@ -16,26 +17,5 @@ export function hasDebuggingEnabled(bootConfig: BootJsonData): boolean { const hasReferencedPdbs = !!bootConfig.resources.pdb; const debugBuild = bootConfig.debugBuild; - const navigatorUA = navigator as MonoNavigatorUserAgent; - const brands = navigatorUA.userAgentData && navigatorUA.userAgentData.brands; - const currentBrowserIsChromeOrEdge = brands - ? brands.some(b => b.brand === "Google Chrome" || b.brand === "Microsoft Edge" || b.brand === "Chromium") - : (window as any).chrome; - - return (hasReferencedPdbs || debugBuild) && (currentBrowserIsChromeOrEdge || navigator.userAgent.includes("Firefox")); -} - -// can be removed once userAgentData is part of lib.dom.d.ts -declare interface MonoNavigatorUserAgent extends Navigator { - readonly userAgentData: MonoUserAgentData; -} - -declare interface MonoUserAgentData { - readonly brands: ReadonlyArray; - readonly platform: string; -} - -declare interface MonoUserAgentDataBrandVersion { - brand?: string; - version?: string; + return (hasReferencedPdbs || debugBuild) && (loaderHelpers.isChromium || loaderHelpers.isFirefox); } \ No newline at end of file diff --git a/src/mono/wasm/runtime/loader/polyfills.ts b/src/mono/wasm/runtime/loader/polyfills.ts index fb5c4bc..5a6a6d5 100644 --- a/src/mono/wasm/runtime/loader/polyfills.ts +++ b/src/mono/wasm/runtime/loader/polyfills.ts @@ -1,11 +1,11 @@ import type { DotnetModuleInternal } from "../types/internal"; -import { INTERNAL, ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, loaderHelpers } from "./globals"; +import { INTERNAL, ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, loaderHelpers, ENVIRONMENT_IS_WEB } from "./globals"; let node_fs: any | undefined = undefined; let node_url: any | undefined = undefined; -export async function init_polyfills(module: DotnetModuleInternal): Promise { +export async function detect_features_and_polyfill(module: DotnetModuleInternal): Promise { loaderHelpers.scriptUrl = normalizeFileUrl(/* webpackIgnore: true */import.meta.url); loaderHelpers.scriptDirectory = normalizeDirectoryUrl(loaderHelpers.scriptUrl); @@ -21,6 +21,18 @@ export async function init_polyfills(module: DotnetModuleInternal): Promise 0) { + loaderHelpers.isChromium = brands.some((b: any) => b.brand === "Google Chrome" || b.brand === "Microsoft Edge" || b.brand === "Chromium"); + } + else if (navigator.userAgent) { + loaderHelpers.isChromium = navigator.userAgent.includes("Chrome"); + loaderHelpers.isFirefox = navigator.userAgent.includes("Firefox"); + } + } + if (ENVIRONMENT_IS_NODE) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore: diff --git a/src/mono/wasm/runtime/loader/run.ts b/src/mono/wasm/runtime/loader/run.ts index 973f530..a147f9f 100644 --- a/src/mono/wasm/runtime/loader/run.ts +++ b/src/mono/wasm/runtime/loader/run.ts @@ -9,7 +9,7 @@ import { deep_merge_config, deep_merge_module, mono_wasm_load_config } from "./c import { mono_exit } from "./exit"; import { setup_proxy_console } from "./logging"; import { resolve_asset_path, start_asset_download } from "./assets"; -import { init_polyfills } from "./polyfills"; +import { detect_features_and_polyfill } from "./polyfills"; import { runtimeHelpers, loaderHelpers } from "./globals"; import { init_globalization } from "./icu"; import { setupPreloadChannelToMainThread } from "./worker"; @@ -391,7 +391,7 @@ function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModu } async function createEmscriptenMain(): Promise { - await init_polyfills(module); + await detect_features_and_polyfill(module); if (!module.configSrc && (!module.config || Object.keys(module.config).length === 0 || !module.config.assets)) { // if config file location nor assets are provided @@ -425,7 +425,7 @@ async function createEmscriptenMain(): Promise { } async function createEmscriptenWorker(): Promise { - await init_polyfills(module); + await detect_features_and_polyfill(module); setupPreloadChannelToMainThread(); diff --git a/src/mono/wasm/runtime/scheduling.ts b/src/mono/wasm/runtime/scheduling.ts index 7b0755b..b0ce9c0 100644 --- a/src/mono/wasm/runtime/scheduling.ts +++ b/src/mono/wasm/runtime/scheduling.ts @@ -2,23 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. import cwraps from "./cwraps"; +import { loaderHelpers } from "./globals"; let spread_timers_maximum = 0; -export let isChromium = false; let pump_count = 0; -if (globalThis.navigator) { - const nav: any = globalThis.navigator; - if (nav.userAgentData && nav.userAgentData.brands) { - isChromium = nav.userAgentData.brands.some((i: any) => i.brand == "Chromium"); - } - else if (nav.userAgent) { - isChromium = nav.userAgent.includes("Chrome"); - } -} - export function prevent_timer_throttling(): void { - if (!isChromium) { + if (!loaderHelpers.isChromium) { return; } diff --git a/src/mono/wasm/runtime/types/internal.ts b/src/mono/wasm/runtime/types/internal.ts index f9a1623..5816f9a 100644 --- a/src/mono/wasm/runtime/types/internal.ts +++ b/src/mono/wasm/runtime/types/internal.ts @@ -131,6 +131,9 @@ export type LoaderHelpers = { out(message: string): void; err(message: string): void; getApplicationEnvironment?: (bootConfigResponse: Response) => string | null; + + isChromium: boolean, + isFirefox: boolean, } export type RuntimeHelpers = { config: MonoConfigInternal; -- 2.7.4