| /* |
| * Copyright (C) 2012 Google Inc. All rights reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions are |
| * met: |
| * |
| * * Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * * Redistributions in binary form must reproduce the above |
| * copyright notice, this list of conditions and the following disclaimer |
| * in the documentation and/or other materials provided with the |
| * distribution. |
| * * Neither the name of Google Inc. nor the names of its |
| * contributors may be used to endorse or promote products derived from |
| * this software without specific prior written permission. |
| * |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| */ |
| |
| import * as Common from '../../core/common/common.js'; |
| import * as SDK from '../../core/sdk/sdk.js'; |
| import * as Workspace from '../workspace/workspace.js'; |
| |
| import {ContentProviderBasedProject} from './ContentProviderBasedProject.js'; |
| import type {SourceMapping} from './CSSWorkspaceBinding.js'; |
| import {CSSWorkspaceBinding} from './CSSWorkspaceBinding.js'; |
| import {NetworkProject} from './NetworkProject.js'; |
| |
| export class SASSSourceMapping implements SourceMapping { |
| private readonly sourceMapManager: SDK.SourceMapManager.SourceMapManager<SDK.CSSStyleSheetHeader.CSSStyleSheetHeader>; |
| private readonly project: ContentProviderBasedProject; |
| private readonly eventListeners: Common.EventTarget.EventDescriptor[]; |
| private readonly bindings: Map<string, Binding>; |
| |
| constructor( |
| target: SDK.Target.Target, |
| sourceMapManager: SDK.SourceMapManager.SourceMapManager<SDK.CSSStyleSheetHeader.CSSStyleSheetHeader>, |
| workspace: Workspace.Workspace.WorkspaceImpl) { |
| this.sourceMapManager = sourceMapManager; |
| this.project = new ContentProviderBasedProject( |
| workspace, 'cssSourceMaps:' + target.id(), Workspace.Workspace.projectTypes.Network, '', |
| false /* isServiceProject */); |
| NetworkProject.setTargetForProject(this.project, target); |
| |
| this.bindings = new Map(); |
| |
| this.eventListeners = [ |
| this.sourceMapManager.addEventListener( |
| SDK.SourceMapManager.Events.SourceMapAttached, |
| event => { |
| this.sourceMapAttached(event); |
| }, |
| this), |
| this.sourceMapManager.addEventListener( |
| SDK.SourceMapManager.Events.SourceMapDetached, |
| event => { |
| this.sourceMapDetached(event); |
| }, |
| this), |
| ]; |
| } |
| |
| private sourceMapAttachedForTest(_sourceMap: SDK.SourceMap.SourceMap|null): void { |
| } |
| |
| private async sourceMapAttached(event: Common.EventTarget.EventTargetEvent): Promise<void> { |
| const header = (event.data.client as SDK.CSSStyleSheetHeader.CSSStyleSheetHeader); |
| const sourceMap = (event.data.sourceMap as SDK.SourceMap.TextSourceMap); |
| const project = this.project; |
| const bindings = this.bindings; |
| for (const sourceURL of sourceMap.sourceURLs()) { |
| let binding = bindings.get(sourceURL); |
| if (!binding) { |
| binding = new Binding(project, sourceURL); |
| bindings.set(sourceURL, binding); |
| } |
| binding.addSourceMap(sourceMap, header.frameId); |
| } |
| await CSSWorkspaceBinding.instance().updateLocations(header); |
| this.sourceMapAttachedForTest(sourceMap); |
| } |
| |
| private async sourceMapDetached(event: Common.EventTarget.EventTargetEvent): Promise<void> { |
| const header = (event.data.client as SDK.CSSStyleSheetHeader.CSSStyleSheetHeader); |
| const sourceMap = (event.data.sourceMap as SDK.SourceMap.TextSourceMap); |
| const bindings = this.bindings; |
| for (const sourceURL of sourceMap.sourceURLs()) { |
| const binding = bindings.get(sourceURL); |
| if (binding) { |
| binding.removeSourceMap(sourceMap, header.frameId); |
| if (!binding.getUiSourceCode()) { |
| bindings.delete(sourceURL); |
| } |
| } |
| } |
| await CSSWorkspaceBinding.instance().updateLocations(header); |
| } |
| |
| rawLocationToUILocation(rawLocation: SDK.CSSModel.CSSLocation): Workspace.UISourceCode.UILocation|null { |
| const header = rawLocation.header(); |
| if (!header) { |
| return null; |
| } |
| const sourceMap = this.sourceMapManager.sourceMapForClient(header); |
| if (!sourceMap) { |
| return null; |
| } |
| let {lineNumber, columnNumber} = rawLocation; |
| // If the source map maps the origin (line:0, column:0) but the CSS header is inline (in a HTML doc), |
| // then adjust the line and column numbers. |
| if (sourceMap.mapsOrigin() && header.isInline) { |
| lineNumber -= header.startLine; |
| if (lineNumber === 0) { |
| columnNumber -= header.startColumn; |
| } |
| } |
| const entry = sourceMap.findEntry(lineNumber, columnNumber); |
| if (!entry || !entry.sourceURL) { |
| return null; |
| } |
| const uiSourceCode = this.project.uiSourceCodeForURL(entry.sourceURL); |
| if (!uiSourceCode) { |
| return null; |
| } |
| return uiSourceCode.uiLocation(entry.sourceLineNumber, entry.sourceColumnNumber); |
| } |
| |
| uiLocationToRawLocations(uiLocation: Workspace.UISourceCode.UILocation): SDK.CSSModel.CSSLocation[] { |
| // TODO(crbug.com/1153123): Revisit the `columnNumber || 0` and also preserve `undefined` for source maps? |
| const {uiSourceCode, lineNumber, columnNumber = 0} = uiLocation; |
| const binding = uiSourceCodeToBinding.get(uiSourceCode); |
| if (!binding) { |
| return []; |
| } |
| const locations: SDK.CSSModel.CSSLocation[] = []; |
| for (const sourceMap of binding.getReferringSourceMaps()) { |
| const entries = sourceMap.findReverseEntries(uiSourceCode.url(), lineNumber, columnNumber); |
| for (const header of this.sourceMapManager.clientsForSourceMap(sourceMap)) { |
| locations.push( |
| ...entries.map(entry => new SDK.CSSModel.CSSLocation(header, entry.lineNumber, entry.columnNumber))); |
| } |
| } |
| return locations; |
| } |
| |
| dispose(): void { |
| Common.EventTarget.removeEventListeners(this.eventListeners); |
| this.project.dispose(); |
| } |
| } |
| |
| const uiSourceCodeToBinding = new WeakMap<Workspace.UISourceCode.UISourceCode, Binding>(); |
| |
| class Binding { |
| private readonly project: ContentProviderBasedProject; |
| private readonly url: string; |
| referringSourceMaps: SDK.SourceMap.TextSourceMap[]; |
| private readonly activeSourceMap?: SDK.SourceMap.TextSourceMap|null; |
| uiSourceCode: Workspace.UISourceCode.UISourceCode|null; |
| |
| constructor(project: ContentProviderBasedProject, url: string) { |
| this.project = project; |
| this.url = url; |
| |
| this.referringSourceMaps = []; |
| this.uiSourceCode = null; |
| } |
| |
| private recreateUISourceCodeIfNeeded(frameId: string): void { |
| const sourceMap = this.referringSourceMaps[this.referringSourceMaps.length - 1]; |
| |
| const contentProvider = |
| sourceMap.sourceContentProvider(this.url, Common.ResourceType.resourceTypes.SourceMapStyleSheet); |
| const newUISourceCode = this.project.createUISourceCode(this.url, contentProvider.contentType()); |
| uiSourceCodeToBinding.set(newUISourceCode, this); |
| const mimeType = |
| Common.ResourceType.ResourceType.mimeFromURL(this.url) || contentProvider.contentType().canonicalMimeType(); |
| const embeddedContent = sourceMap.embeddedContentByURL(this.url); |
| const metadata = typeof embeddedContent === 'string' ? |
| new Workspace.UISourceCode.UISourceCodeMetadata(null, embeddedContent.length) : |
| null; |
| |
| if (this.uiSourceCode) { |
| NetworkProject.cloneInitialFrameAttribution(this.uiSourceCode, newUISourceCode); |
| this.project.removeFile(this.uiSourceCode.url()); |
| } else { |
| NetworkProject.setInitialFrameAttribution(newUISourceCode, frameId); |
| } |
| this.uiSourceCode = newUISourceCode; |
| this.project.addUISourceCodeWithProvider(this.uiSourceCode, contentProvider, metadata, mimeType); |
| } |
| |
| addSourceMap(sourceMap: SDK.SourceMap.TextSourceMap, frameId: string): void { |
| if (this.uiSourceCode) { |
| NetworkProject.addFrameAttribution(this.uiSourceCode, frameId); |
| } |
| this.referringSourceMaps.push(sourceMap); |
| this.recreateUISourceCodeIfNeeded(frameId); |
| } |
| |
| removeSourceMap(sourceMap: SDK.SourceMap.TextSourceMap, frameId: string): void { |
| const uiSourceCode = (this.uiSourceCode as Workspace.UISourceCode.UISourceCode); |
| NetworkProject.removeFrameAttribution(uiSourceCode, frameId); |
| const lastIndex = this.referringSourceMaps.lastIndexOf(sourceMap); |
| if (lastIndex !== -1) { |
| this.referringSourceMaps.splice(lastIndex, 1); |
| } |
| if (!this.referringSourceMaps.length) { |
| this.project.removeFile(uiSourceCode.url()); |
| this.uiSourceCode = null; |
| } else { |
| this.recreateUISourceCodeIfNeeded(frameId); |
| } |
| } |
| |
| getReferringSourceMaps(): Array<SDK.SourceMap.TextSourceMap> { |
| return this.referringSourceMaps; |
| } |
| |
| getUiSourceCode(): Workspace.UISourceCode.UISourceCode|null { |
| return this.uiSourceCode; |
| } |
| } |