Skip to content

Commit

Permalink
[wasm][debugging] Enable debugging when Device Toolbar is enabled and…
Browse files Browse the repository at this point in the history
… Iphone is selected (#86920)

* Enable debugging when Device Toolbar is enabled and Iphone is selected
* unify browser detection
Co-authored-by: pavelsavara <pavel.savara@gmail.com>
  • Loading branch information
thaystg committed Jun 8, 2023
1 parent d3ea4a9 commit 03f2be6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
24 changes: 2 additions & 22 deletions src/mono/wasm/runtime/loader/blazor/_Polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<MonoUserAgentDataBrandVersion>;
readonly platform: string;
}

declare interface MonoUserAgentDataBrandVersion {
brand?: string;
version?: string;
return (hasReferencedPdbs || debugBuild) && (loaderHelpers.isChromium || loaderHelpers.isFirefox);
}
16 changes: 14 additions & 2 deletions src/mono/wasm/runtime/loader/polyfills.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
export async function detect_features_and_polyfill(module: DotnetModuleInternal): Promise<void> {

loaderHelpers.scriptUrl = normalizeFileUrl(/* webpackIgnore: true */import.meta.url);
loaderHelpers.scriptDirectory = normalizeDirectoryUrl(loaderHelpers.scriptUrl);
Expand All @@ -21,6 +21,18 @@ export async function init_polyfills(module: DotnetModuleInternal): Promise<void
loaderHelpers.err = console.error;
loaderHelpers.getApplicationEnvironment = module.getApplicationEnvironment;

if (ENVIRONMENT_IS_WEB && globalThis.navigator) {
const navigator: any = globalThis.navigator;
const brands = navigator.userAgentData && navigator.userAgentData.brands;
if (brands && brands.length > 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:
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -391,7 +391,7 @@ function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModu
}

async function createEmscriptenMain(): Promise<RuntimeAPI> {
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
Expand Down Expand Up @@ -425,7 +425,7 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
}

async function createEmscriptenWorker(): Promise<EmscriptenModuleInternal> {
await init_polyfills(module);
await detect_features_and_polyfill(module);

setupPreloadChannelToMainThread();

Expand Down
14 changes: 2 additions & 12 deletions src/mono/wasm/runtime/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 03f2be6

Please sign in to comment.