| // Copyright 2026 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import {assert} from 'chai'; | |
| import {describe, it, setup} from '../../../test/api/mocha-interface.js'; | |
| import * as Common from '../../core/common/common.js'; | |
| import * as Host from '../../core/host/host.js'; | |
| import * as Platform from '../../core/platform/platform.js'; | |
| import * as TextUtils from '../../core/text_utils/text_utils.js'; | |
| import * as Workspace from '../workspace/workspace.js'; | |
| import * as Persistence from './persistence.js'; | |
| const {urlString} = Platform.DevToolsPath; | |
| class OverridePlatformFileSystem extends Persistence.PlatformFileSystem.PlatformFileSystem { | |
| readonly #files: Map<Platform.DevToolsPath.EncodedPathString, string>; | |
| readonly #createdFiles: string[]; | |
| constructor( | |
| path: Platform.DevToolsPath.UrlString, | |
| files: Map<Platform.DevToolsPath.EncodedPathString, string>, | |
| createdFiles: string[] = [], | |
| ) { | |
| super(path, Persistence.PlatformFileSystem.PlatformFileSystemType.OVERRIDES, false); | |
| this.#files = files; | |
| this.#createdFiles = createdFiles; | |
| } | |
| override async createFile( | |
| path: Platform.DevToolsPath.EncodedPathString, | |
| name: Platform.DevToolsPath.RawPathString|null, | |
| ): Promise<Platform.DevToolsPath.EncodedPathString|null> { | |
| const fileName = name || ('NewFile' as Platform.DevToolsPath.RawPathString); | |
| this.#createdFiles.push(`${path}/${fileName}`); | |
| const decodedPath = Common.ParsedURL.ParsedURL.encodedPathToRawPathString(path); | |
| const fullRawPath = Common.ParsedURL.ParsedURL.concatenate(decodedPath, '/', fileName); | |
| const encodedFilePath = Common.ParsedURL.ParsedURL.rawPathToEncodedPathString(fullRawPath); | |
| this.#files.set(encodedFilePath, ''); | |
| return encodedFilePath; | |
| } | |
| override setFileContent(path: Platform.DevToolsPath.EncodedPathString, content: string, _isBase64: boolean): void { | |
| const normalizedPath = (path.startsWith('/') ? path.slice(1) : path) as Platform.DevToolsPath.EncodedPathString; | |
| this.#files.set(normalizedPath, content); | |
| } | |
| override initialFilePaths(): Platform.DevToolsPath.EncodedPathString[] { | |
| return [...this.#files.keys()]; | |
| } | |
| override embedderPath(): Platform.DevToolsPath.RawPathString { | |
| return Common.ParsedURL.ParsedURL.urlToRawPathString(this.path(), Host.Platform.isWin()); | |
| } | |
| override supportsAutomapping(): boolean { | |
| return false; | |
| } | |
| #mimeTypeForPath(path: string): string { | |
| const extension = Common.ParsedURL.ParsedURL.extractExtension(path); | |
| return Common.ResourceType.ResourceType.mimeFromExtension(extension) || 'text/plain'; | |
| } | |
| override contentType(path: string): Common.ResourceType.ResourceType { | |
| return Common.ResourceType.ResourceType.fromMimeType(this.#mimeTypeForPath(path)); | |
| } | |
| override mimeFromPath(path: Platform.DevToolsPath.UrlString): string { | |
| return this.#mimeTypeForPath(path); | |
| } | |
| override tooltipForURL(url: Platform.DevToolsPath.UrlString): string { | |
| return url; | |
| } | |
| override async requestFileContent(path: Platform.DevToolsPath.EncodedPathString): | |
| Promise<TextUtils.ContentData.ContentDataOrError> { | |
| const normalizedPath = (path.startsWith('/') ? path.slice(1) : path) as Platform.DevToolsPath.EncodedPathString; | |
| const content = this.#files.get(normalizedPath); | |
| if (content === undefined) { | |
| return await super.requestFileContent(path); | |
| } | |
| return new TextUtils.ContentData.ContentData(content, /* isBase64 */ false, this.#mimeTypeForPath(normalizedPath)); | |
| } | |
| } | |
| describe('NetworkPersistenceManager API Test', () => { | |
| setup({ | |
| targetUrl: 'about:blank', | |
| }); | |
| for (const testPlatform of ['linux', 'windows'] as const) { | |
| it(`binds existing override files when an overrides project is added (${testPlatform})`, | |
| async ({inspectedPage, universe}) => { | |
| const originalPlatform = Host.Platform.platform(); | |
| try { | |
| Host.Platform.setPlatformForTests(testPlatform); | |
| const {persistence, workspace, isolatedFileSystemManager, settings} = universe; | |
| // Eagerly initialize NetworkPersistenceManager and FileSystemWorkspaceBinding | |
| // so they observe workspace changes. | |
| void universe.fileSystemWorkspaceBinding; | |
| const networkPersistenceManager = universe.networkPersistenceManager; | |
| const resourceUrl = urlString`${inspectedPage.domain()}/test/e2e/resources/empty.html`; | |
| const networkUISourceCodePromise = new Promise<Workspace.UISourceCode.UISourceCode>(resolve => { | |
| const listener = | |
| (event: Common.EventTarget.EventTargetEvent<Workspace.UISourceCode.UISourceCode>): void => { | |
| if (event.data.url() === resourceUrl) { | |
| workspace.removeEventListener(Workspace.Workspace.Events.UISourceCodeAdded, listener); | |
| resolve(event.data); | |
| } | |
| }; | |
| workspace.addEventListener(Workspace.Workspace.Events.UISourceCodeAdded, listener); | |
| }); | |
| await inspectedPage.goToResource('empty.html'); | |
| const networkUISourceCode = await networkUISourceCodePromise; | |
| assert.strictEqual(networkUISourceCode.project().type(), Workspace.Workspace.projectTypes.Network); | |
| const bindingPromise = persistence.once(Persistence.Persistence.Events.BindingCreated); | |
| const host = new URL(resourceUrl).host; | |
| assert.include(host, ':'); | |
| const baseDirectory = Host.Platform.isWin() ? host.replace(':', '%3A') : host; | |
| const relativeFilePath = Common.ParsedURL.ParsedURL.rawPathToEncodedPathString( | |
| `${baseDirectory}/test/e2e/resources/empty.html` as Platform.DevToolsPath.RawPathString, | |
| ); | |
| const fileSystemPath = urlString`file:///tmp`; | |
| const fileSystem = new OverridePlatformFileSystem( | |
| fileSystemPath, | |
| new Map([[relativeFilePath, 'New Content']]), | |
| ); | |
| isolatedFileSystemManager.addPlatformFileSystem(fileSystemPath, fileSystem); | |
| const enabledSetting = settings.resolve( | |
| Persistence.NetworkPersistenceManager.persistenceNetworkOverridesEnabledSettingDescriptor, | |
| ); | |
| enabledSetting.set(true); | |
| const binding = await bindingPromise; | |
| assert.isTrue(networkPersistenceManager.active()); | |
| assert.strictEqual(persistence.binding(networkUISourceCode), binding); | |
| assert.strictEqual(binding.network.url(), resourceUrl); | |
| const expectedHost = testPlatform === 'windows' ? host.replace(':', '%253A') : host; | |
| assert.strictEqual( | |
| binding.fileSystem.url(), | |
| `file:///tmp/${expectedHost}/test/e2e/resources/empty.html`, | |
| ); | |
| } finally { | |
| Host.Platform.setPlatformForTests(originalPlatform); | |
| } | |
| }); | |
| } | |
| it('saves overridden files without hash in filename on Linux and Windows', async ({inspectedPage, universe}) => { | |
| const {workspace, isolatedFileSystemManager, settings} = universe; | |
| void universe.fileSystemWorkspaceBinding; | |
| const networkPersistenceManager = universe.networkPersistenceManager; | |
| const createdFiles: string[] = []; | |
| const fileSystemPath = urlString`file:///tmp`; | |
| const fileSystem = new OverridePlatformFileSystem(fileSystemPath, new Map(), createdFiles); | |
| const projectChangedPromise = | |
| networkPersistenceManager.once(Persistence.NetworkPersistenceManager.Events.PROJECT_CHANGED); | |
| isolatedFileSystemManager.addPlatformFileSystem(fileSystemPath, fileSystem); | |
| await projectChangedPromise; | |
| const overridesUpdatedPromise = | |
| networkPersistenceManager.once(Persistence.NetworkPersistenceManager.Events.LOCAL_OVERRIDES_PROJECT_UPDATED); | |
| const enabledSetting = settings.resolve( | |
| Persistence.NetworkPersistenceManager.persistenceNetworkOverridesEnabledSettingDescriptor, | |
| ); | |
| enabledSetting.set(true); | |
| await overridesUpdatedPromise; | |
| assert.isTrue(networkPersistenceManager.active()); | |
| await inspectedPage.goToResource('empty.html'); | |
| await inspectedPage.evaluate(() => { | |
| const iframe = document.createElement('iframe'); | |
| iframe.id = 'test-iframe'; | |
| iframe.src = 'data:,'; | |
| document.body.appendChild(iframe); | |
| }); | |
| const overridesPrefix = `${inspectedPage.domain()}/devtools/overrides/`; | |
| async function testUrl(url: string): Promise<void> { | |
| let listener: ((event: Common.EventTarget.EventTargetEvent<Workspace.UISourceCode.UISourceCode>) => void)|null = | |
| null; | |
| try { | |
| const uiSourceCodePromise = new Promise<Workspace.UISourceCode.UISourceCode>(resolve => { | |
| listener = event => { | |
| const candidate = event.data; | |
| if (candidate.project().type() === Workspace.Workspace.projectTypes.Network && | |
| candidate.url().startsWith(overridesPrefix)) { | |
| resolve(candidate); | |
| } | |
| }; | |
| workspace.addEventListener(Workspace.Workspace.Events.UISourceCodeAdded, listener); | |
| }); | |
| await inspectedPage.evaluate(iframeUrl => { | |
| const iframe = document.getElementById('test-iframe') as HTMLIFrameElement; | |
| iframe.src = iframeUrl; | |
| }, url); | |
| const uiSourceCode = await uiSourceCodePromise; | |
| await networkPersistenceManager.saveUISourceCodeForOverrides(uiSourceCode); | |
| } finally { | |
| if (listener) { | |
| workspace.removeEventListener(Workspace.Workspace.Events.UISourceCodeAdded, listener); | |
| } | |
| } | |
| } | |
| const urlPaths = [ | |
| 'bar.js', | |
| 'a space/bar.js', | |
| 'bar.js?#hello', | |
| 'bar.js?params', | |
| 'bar.js?params&and=more&pa&ra?ms', | |
| 'bar2.js?params&and=more&pa&ra?ms#hello?with¶ms', | |
| 'no-extension', | |
| 'foo&with%20some*bad%5EC!h%7Ba...r,acter%%s/file&with?s%20t^rang@e~character%27S', | |
| '', | |
| ]; | |
| const host = new URL(inspectedPage.domain()).host; | |
| assert.include(host, ':'); | |
| const winHost = host.replace(':', '%253A'); | |
| const originalPlatform = Host.Platform.platform(); | |
| try { | |
| Host.Platform.setPlatformForTests('linux'); | |
| for (const urlPath of urlPaths) { | |
| await testUrl(`${inspectedPage.domain()}/devtools/overrides/resources/${urlPath}`); | |
| } | |
| Host.Platform.setPlatformForTests('windows'); | |
| for (const urlPath of urlPaths) { | |
| await testUrl(`${inspectedPage.domain()}/devtools/overrides/windows/${urlPath}`); | |
| } | |
| } finally { | |
| Host.Platform.setPlatformForTests(originalPlatform); | |
| } | |
| assert.deepEqual(createdFiles, [ | |
| `${host}/devtools/overrides/resources/bar.js`, | |
| `${host}/devtools/overrides/resources/a%252520space/bar.js`, | |
| `${host}/devtools/overrides/resources/bar.js?`, | |
| `${host}/devtools/overrides/resources/bar.js?params`, | |
| `${host}/devtools/overrides/resources/bar.js?params&and=more&pa&ra?ms`, | |
| `${host}/devtools/overrides/resources/bar2.js?params&and=more&pa&ra?ms`, | |
| `${host}/devtools/overrides/resources/no-extension`, | |
| `${host}/devtools/overrides/resources/` + | |
| 'foo&with%252520some%252Abad%25255EC!h%25257Ba...r,acter%2525%2525s/' + | |
| 'file&with?s%2520t%5Erang@e~character%2527S', | |
| `${host}/devtools/overrides/resources/index.html`, | |
| `${winHost}/devtools/overrides/windows/bar.js`, | |
| `${winHost}/devtools/overrides/windows/a%252520space/bar.js`, | |
| `${winHost}/devtools/overrides/windows/bar.js%3F`, | |
| `${winHost}/devtools/overrides/windows/bar.js%3Fparams`, | |
| `${winHost}/devtools/overrides/windows/bar.js%3Fparams&and=more&pa&ra%3Fms`, | |
| `${winHost}/devtools/overrides/windows/bar2.js%3Fparams&and=more&pa&ra%3Fms`, | |
| `${winHost}/devtools/overrides/windows/no-extension`, | |
| `${winHost}/devtools/overrides/windows/` + | |
| 'foo&with%252520some%252Abad%25255EC!h%25257Ba...r,acter%2525%2525s/' + | |
| 'file&with%3Fs%2520t%5Erang@e~character%2527S', | |
| `${winHost}/devtools/overrides/windows/index.html`, | |
| ]); | |
| }); | |
| }); |