Path: blob/main/src/vs/platform/browserView/common/browserViewUri.ts
4777 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Schemas } from '../../../base/common/network.js';6import { URI } from '../../../base/common/uri.js';7import { generateUuid } from '../../../base/common/uuid.js';89/**10* Helper for creating and parsing browser view URIs.11*/12export namespace BrowserViewUri {1314export const scheme = Schemas.vscodeBrowser;1516/**17* Creates a resource URI for a browser view with the given URL.18* Optionally accepts an ID; if not provided, a new UUID is generated.19*/20export function forUrl(url: string | undefined, id?: string): URI {21const viewId = id ?? generateUuid();22return URI.from({23scheme,24path: `/${viewId}`,25query: url ? `url=${encodeURIComponent(url)}` : undefined26});27}2829/**30* Parses a browser view resource URI to extract the ID and URL.31*/32export function parse(resource: URI): { id: string; url: string } | undefined {33if (resource.scheme !== scheme) {34return undefined;35}3637// Remove leading slash if present38const id = resource.path.startsWith('/') ? resource.path.substring(1) : resource.path;39if (!id) {40return undefined;41}4243const url = resource.query ? new URLSearchParams(resource.query).get('url') ?? '' : '';4445return { id, url };46}4748/**49* Extracts the ID from a browser view resource URI.50*/51export function getId(resource: URI): string | undefined {52return parse(resource)?.id;53}5455/**56* Extracts the URL from a browser view resource URI.57*/58export function getUrl(resource: URI): string | undefined {59return parse(resource)?.url;60}61}626364