chrome-devtools-mcp 0.0.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +6 -3
  2. package/build/node_modules/chrome-devtools-frontend/front_end/core/common/Settings.js +3 -32
  3. package/build/node_modules/chrome-devtools-frontend/front_end/core/i18n/i18n.js +35 -8
  4. package/build/node_modules/chrome-devtools-frontend/front_end/core/root/Runtime.js +4 -1
  5. package/build/node_modules/chrome-devtools-frontend/front_end/generated/InspectorBackendCommands.js +4 -4
  6. package/build/node_modules/chrome-devtools-frontend/front_end/generated/SupportedCSSProperties.js +12 -0
  7. package/build/node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/data_formatters/PerformanceTraceFormatter.js +366 -0
  8. package/build/node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/performance/AICallTree.js +366 -0
  9. package/build/node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/performance/AIContext.js +64 -0
  10. package/build/node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/performance/AIQueries.js +105 -0
  11. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/CSSWorkspaceBinding.js +243 -0
  12. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/CompilerScriptMapping.js +407 -0
  13. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/ContentProviderBasedProject.js +128 -0
  14. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/DebuggerLanguagePlugins.js +992 -0
  15. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/DebuggerWorkspaceBinding.js +574 -0
  16. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/DefaultScriptMapping.js +112 -0
  17. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/FileUtils.js +186 -0
  18. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/LiveLocation.js +60 -0
  19. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/NetworkProject.js +107 -0
  20. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/PresentationConsoleMessageHelper.js +244 -0
  21. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/ResourceMapping.js +473 -0
  22. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/ResourceScriptMapping.js +399 -0
  23. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/ResourceUtils.js +87 -0
  24. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/SASSSourceMapping.js +181 -0
  25. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/StylesSourceMapping.js +268 -0
  26. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/TempFile.js +55 -0
  27. package/build/node_modules/chrome-devtools-frontend/front_end/models/bindings/bindings.js +20 -0
  28. package/build/node_modules/chrome-devtools-frontend/front_end/models/crux-manager/CrUXManager.js +283 -0
  29. package/build/node_modules/chrome-devtools-frontend/front_end/models/crux-manager/crux-manager.js +4 -0
  30. package/build/node_modules/chrome-devtools-frontend/front_end/models/emulation/DeviceModeModel.js +775 -0
  31. package/build/node_modules/chrome-devtools-frontend/front_end/models/emulation/EmulatedDevices.js +1706 -0
  32. package/build/node_modules/chrome-devtools-frontend/front_end/models/emulation/emulation.js +6 -0
  33. package/build/node_modules/chrome-devtools-frontend/front_end/models/formatter/FormatterWorkerPool.js +131 -0
  34. package/build/node_modules/chrome-devtools-frontend/front_end/models/formatter/ScriptFormatter.js +77 -0
  35. package/build/node_modules/chrome-devtools-frontend/front_end/models/formatter/formatter.js +6 -0
  36. package/build/node_modules/chrome-devtools-frontend/front_end/models/geometry/GeometryImpl.js +347 -0
  37. package/build/node_modules/chrome-devtools-frontend/front_end/models/geometry/geometry.js +4 -0
  38. package/build/node_modules/chrome-devtools-frontend/front_end/models/source_map_scopes/NamesResolver.js +626 -0
  39. package/build/node_modules/chrome-devtools-frontend/front_end/models/source_map_scopes/ScopeChainModel.js +59 -0
  40. package/build/node_modules/chrome-devtools-frontend/front_end/models/source_map_scopes/ScopeTreeCache.js +32 -0
  41. package/build/node_modules/chrome-devtools-frontend/front_end/models/source_map_scopes/source_map_scopes.js +7 -0
  42. package/build/node_modules/chrome-devtools-frontend/front_end/models/stack_trace/StackTrace.js +4 -0
  43. package/build/node_modules/chrome-devtools-frontend/front_end/models/stack_trace/StackTraceImpl.js +67 -0
  44. package/build/node_modules/chrome-devtools-frontend/front_end/models/stack_trace/StackTraceModel.js +97 -0
  45. package/build/node_modules/chrome-devtools-frontend/front_end/models/stack_trace/Trie.js +113 -0
  46. package/build/node_modules/chrome-devtools-frontend/front_end/models/stack_trace/stack_trace.js +5 -0
  47. package/build/node_modules/chrome-devtools-frontend/front_end/models/stack_trace/stack_trace_impl.js +7 -0
  48. package/build/node_modules/chrome-devtools-frontend/front_end/models/text_utils/TextUtils.js +23 -0
  49. package/build/node_modules/chrome-devtools-frontend/front_end/models/trace/Processor.js +1 -1
  50. package/build/node_modules/chrome-devtools-frontend/front_end/models/trace/helpers/Trace.js +1 -1
  51. package/build/node_modules/chrome-devtools-frontend/front_end/models/trace/insights/DocumentLatency.js +5 -4
  52. package/build/node_modules/chrome-devtools-frontend/front_end/models/trace_source_maps_resolver/SourceMapsResolver.js +199 -0
  53. package/build/node_modules/chrome-devtools-frontend/front_end/models/trace_source_maps_resolver/trace_source_maps_resolver.js +4 -0
  54. package/build/node_modules/chrome-devtools-frontend/front_end/models/workspace/FileManager.js +64 -0
  55. package/build/node_modules/chrome-devtools-frontend/front_end/models/workspace/IgnoreListManager.js +511 -0
  56. package/build/node_modules/chrome-devtools-frontend/front_end/models/workspace/SearchConfig.js +113 -0
  57. package/build/node_modules/chrome-devtools-frontend/front_end/models/workspace/UISourceCode.js +563 -0
  58. package/build/node_modules/chrome-devtools-frontend/front_end/models/workspace/WorkspaceImpl.js +204 -0
  59. package/build/node_modules/chrome-devtools-frontend/front_end/models/workspace/workspace.js +9 -0
  60. package/build/src/McpContext.js +24 -9
  61. package/build/src/McpResponse.js +3 -3
  62. package/build/src/browser.js +3 -1
  63. package/build/src/index.js +1 -1
  64. package/build/src/tools/input.js +7 -7
  65. package/build/src/tools/performance.js +29 -2
  66. package/build/src/tools/screenshot.js +1 -1
  67. package/build/src/tools/script.js +40 -14
  68. package/build/src/trace-processing/parse.js +26 -22
  69. package/package.json +9 -7
@@ -0,0 +1,243 @@
1
+ // Copyright 2014 The Chromium Authors
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+ import * as Common from '../../core/common/common.js';
5
+ import * as Platform from '../../core/platform/platform.js';
6
+ import * as SDK from '../../core/sdk/sdk.js';
7
+ import { LiveLocationWithPool, } from './LiveLocation.js';
8
+ import { SASSSourceMapping } from './SASSSourceMapping.js';
9
+ import { StylesSourceMapping } from './StylesSourceMapping.js';
10
+ let cssWorkspaceBindingInstance;
11
+ export class CSSWorkspaceBinding {
12
+ #resourceMapping;
13
+ #modelToInfo;
14
+ #liveLocationPromises;
15
+ constructor(resourceMapping, targetManager) {
16
+ this.#resourceMapping = resourceMapping;
17
+ this.#modelToInfo = new Map();
18
+ targetManager.observeModels(SDK.CSSModel.CSSModel, this);
19
+ this.#liveLocationPromises = new Set();
20
+ }
21
+ static instance(opts = { forceNew: null, resourceMapping: null, targetManager: null }) {
22
+ const { forceNew, resourceMapping, targetManager } = opts;
23
+ if (!cssWorkspaceBindingInstance || forceNew) {
24
+ if (!resourceMapping || !targetManager) {
25
+ throw new Error(`Unable to create CSSWorkspaceBinding: resourceMapping and targetManager must be provided: ${new Error().stack}`);
26
+ }
27
+ cssWorkspaceBindingInstance = new CSSWorkspaceBinding(resourceMapping, targetManager);
28
+ }
29
+ return cssWorkspaceBindingInstance;
30
+ }
31
+ static removeInstance() {
32
+ cssWorkspaceBindingInstance = undefined;
33
+ }
34
+ get modelToInfo() {
35
+ return this.#modelToInfo;
36
+ }
37
+ getCSSModelInfo(cssModel) {
38
+ return this.#modelToInfo.get(cssModel);
39
+ }
40
+ modelAdded(cssModel) {
41
+ this.#modelToInfo.set(cssModel, new ModelInfo(cssModel, this.#resourceMapping));
42
+ }
43
+ modelRemoved(cssModel) {
44
+ this.getCSSModelInfo(cssModel).dispose();
45
+ this.#modelToInfo.delete(cssModel);
46
+ }
47
+ /**
48
+ * The promise returned by this function is resolved once all *currently*
49
+ * pending LiveLocations are processed.
50
+ */
51
+ async pendingLiveLocationChangesPromise() {
52
+ await Promise.all(this.#liveLocationPromises);
53
+ }
54
+ recordLiveLocationChange(promise) {
55
+ void promise.then(() => {
56
+ this.#liveLocationPromises.delete(promise);
57
+ });
58
+ this.#liveLocationPromises.add(promise);
59
+ }
60
+ async updateLocations(header) {
61
+ const updatePromise = this.getCSSModelInfo(header.cssModel()).updateLocations(header);
62
+ this.recordLiveLocationChange(updatePromise);
63
+ await updatePromise;
64
+ }
65
+ createLiveLocation(rawLocation, updateDelegate, locationPool) {
66
+ const locationPromise = this.getCSSModelInfo(rawLocation.cssModel()).createLiveLocation(rawLocation, updateDelegate, locationPool);
67
+ this.recordLiveLocationChange(locationPromise);
68
+ return locationPromise;
69
+ }
70
+ propertyRawLocation(cssProperty, forName) {
71
+ const style = cssProperty.ownerStyle;
72
+ if (!style || style.type !== SDK.CSSStyleDeclaration.Type.Regular || !style.styleSheetId) {
73
+ return null;
74
+ }
75
+ const header = style.cssModel().styleSheetHeaderForId(style.styleSheetId);
76
+ if (!header) {
77
+ return null;
78
+ }
79
+ const range = forName ? cssProperty.nameRange() : cssProperty.valueRange();
80
+ if (!range) {
81
+ return null;
82
+ }
83
+ const lineNumber = range.startLine;
84
+ const columnNumber = range.startColumn;
85
+ return new SDK.CSSModel.CSSLocation(header, header.lineNumberInSource(lineNumber), header.columnNumberInSource(lineNumber, columnNumber));
86
+ }
87
+ propertyUILocation(cssProperty, forName) {
88
+ const rawLocation = this.propertyRawLocation(cssProperty, forName);
89
+ if (!rawLocation) {
90
+ return null;
91
+ }
92
+ return this.rawLocationToUILocation(rawLocation);
93
+ }
94
+ rawLocationToUILocation(rawLocation) {
95
+ return this.getCSSModelInfo(rawLocation.cssModel()).rawLocationToUILocation(rawLocation);
96
+ }
97
+ uiLocationToRawLocations(uiLocation) {
98
+ const rawLocations = [];
99
+ for (const modelInfo of this.#modelToInfo.values()) {
100
+ rawLocations.push(...modelInfo.uiLocationToRawLocations(uiLocation));
101
+ }
102
+ return rawLocations;
103
+ }
104
+ }
105
+ export class ModelInfo {
106
+ #eventListeners;
107
+ #resourceMapping;
108
+ #stylesSourceMapping;
109
+ #sassSourceMapping;
110
+ #locations;
111
+ #unboundLocations;
112
+ constructor(cssModel, resourceMapping) {
113
+ this.#eventListeners = [
114
+ cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetAdded, event => {
115
+ void this.styleSheetAdded(event);
116
+ }, this),
117
+ cssModel.addEventListener(SDK.CSSModel.Events.StyleSheetRemoved, event => {
118
+ void this.styleSheetRemoved(event);
119
+ }, this),
120
+ ];
121
+ this.#resourceMapping = resourceMapping;
122
+ this.#stylesSourceMapping = new StylesSourceMapping(cssModel, resourceMapping.workspace);
123
+ const sourceMapManager = cssModel.sourceMapManager();
124
+ this.#sassSourceMapping = new SASSSourceMapping(cssModel.target(), sourceMapManager, resourceMapping.workspace);
125
+ this.#locations = new Platform.MapUtilities.Multimap();
126
+ this.#unboundLocations = new Platform.MapUtilities.Multimap();
127
+ }
128
+ get locations() {
129
+ return this.#locations;
130
+ }
131
+ async createLiveLocation(rawLocation, updateDelegate, locationPool) {
132
+ const location = new LiveLocation(rawLocation, this, updateDelegate, locationPool);
133
+ const header = rawLocation.header();
134
+ if (header) {
135
+ location.setHeader(header);
136
+ this.#locations.set(header, location);
137
+ await location.update();
138
+ }
139
+ else {
140
+ this.#unboundLocations.set(rawLocation.url, location);
141
+ }
142
+ return location;
143
+ }
144
+ disposeLocation(location) {
145
+ const header = location.header();
146
+ if (header) {
147
+ this.#locations.delete(header, location);
148
+ }
149
+ else {
150
+ this.#unboundLocations.delete(location.url, location);
151
+ }
152
+ }
153
+ updateLocations(header) {
154
+ const promises = [];
155
+ for (const location of this.#locations.get(header)) {
156
+ promises.push(location.update());
157
+ }
158
+ return Promise.all(promises);
159
+ }
160
+ async styleSheetAdded(event) {
161
+ const header = event.data;
162
+ if (!header.sourceURL) {
163
+ return;
164
+ }
165
+ const promises = [];
166
+ for (const location of this.#unboundLocations.get(header.sourceURL)) {
167
+ location.setHeader(header);
168
+ this.#locations.set(header, location);
169
+ promises.push(location.update());
170
+ }
171
+ await Promise.all(promises);
172
+ this.#unboundLocations.deleteAll(header.sourceURL);
173
+ }
174
+ async styleSheetRemoved(event) {
175
+ const header = event.data;
176
+ const promises = [];
177
+ for (const location of this.#locations.get(header)) {
178
+ location.setHeader(header);
179
+ this.#unboundLocations.set(location.url, location);
180
+ promises.push(location.update());
181
+ }
182
+ await Promise.all(promises);
183
+ this.#locations.deleteAll(header);
184
+ }
185
+ addSourceMap(sourceUrl, sourceMapUrl) {
186
+ this.#stylesSourceMapping.addSourceMap(sourceUrl, sourceMapUrl);
187
+ }
188
+ rawLocationToUILocation(rawLocation) {
189
+ let uiLocation = null;
190
+ uiLocation = uiLocation || this.#sassSourceMapping.rawLocationToUILocation(rawLocation);
191
+ uiLocation = uiLocation || this.#stylesSourceMapping.rawLocationToUILocation(rawLocation);
192
+ uiLocation = uiLocation || this.#resourceMapping.cssLocationToUILocation(rawLocation);
193
+ return uiLocation;
194
+ }
195
+ uiLocationToRawLocations(uiLocation) {
196
+ let rawLocations = this.#sassSourceMapping.uiLocationToRawLocations(uiLocation);
197
+ if (rawLocations.length) {
198
+ return rawLocations;
199
+ }
200
+ rawLocations = this.#stylesSourceMapping.uiLocationToRawLocations(uiLocation);
201
+ if (rawLocations.length) {
202
+ return rawLocations;
203
+ }
204
+ return this.#resourceMapping.uiLocationToCSSLocations(uiLocation);
205
+ }
206
+ dispose() {
207
+ Common.EventTarget.removeEventListeners(this.#eventListeners);
208
+ this.#stylesSourceMapping.dispose();
209
+ this.#sassSourceMapping.dispose();
210
+ }
211
+ }
212
+ export class LiveLocation extends LiveLocationWithPool {
213
+ url;
214
+ #lineNumber;
215
+ #columnNumber;
216
+ #info;
217
+ #header;
218
+ constructor(rawLocation, info, updateDelegate, locationPool) {
219
+ super(updateDelegate, locationPool);
220
+ this.url = rawLocation.url;
221
+ this.#lineNumber = rawLocation.lineNumber;
222
+ this.#columnNumber = rawLocation.columnNumber;
223
+ this.#info = info;
224
+ this.#header = null;
225
+ }
226
+ header() {
227
+ return this.#header;
228
+ }
229
+ setHeader(header) {
230
+ this.#header = header;
231
+ }
232
+ async uiLocation() {
233
+ if (!this.#header) {
234
+ return null;
235
+ }
236
+ const rawLocation = new SDK.CSSModel.CSSLocation(this.#header, this.#lineNumber, this.#columnNumber);
237
+ return CSSWorkspaceBinding.instance().rawLocationToUILocation(rawLocation);
238
+ }
239
+ dispose() {
240
+ super.dispose();
241
+ this.#info.disposeLocation(this);
242
+ }
243
+ }
@@ -0,0 +1,407 @@
1
+ // Copyright 2012 The Chromium Authors
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+ import * as Common from '../../core/common/common.js';
5
+ import * as Platform from '../../core/platform/platform.js';
6
+ import * as SDK from '../../core/sdk/sdk.js';
7
+ import * as TextUtils from '../text_utils/text_utils.js';
8
+ import * as Workspace from '../workspace/workspace.js';
9
+ import { ContentProviderBasedProject } from './ContentProviderBasedProject.js';
10
+ import { NetworkProject } from './NetworkProject.js';
11
+ /**
12
+ * The `CompilerScriptMapping` maps script entities from source maps to scripts and vice versa.
13
+ * It is part of the {@link DebuggerWorkspaceBinding} and operates on top of the
14
+ * {@link SDK.SourceMapManager.SourceMapManager}.
15
+ *
16
+ * The `CompilerScriptMapping` maintains a list of {@link ContentProviderBasedProject}s, in which it
17
+ * creates the `UISourceCode`s for the source-mapped entities. The mapping is implemented in various
18
+ * layers:
19
+ *
20
+ * - `#sourceMapToProject` holds a mapping of all the attached `SourceMap`s to their respective
21
+ * `ContentBasedProviderBasedProject`s. When resolving raw to UI locations this is the first
22
+ * place to check.
23
+ * - `#uiSourceCodeToSourceMaps` maps every `UISourceCode` created herein to the `SourceMap` that
24
+ * it originated from. This is the authoritative source of information: The `#projects` might
25
+ * contain `UISourceCode` objects that were created from this `CompilerScriptMapping`, but which
26
+ * have become stale, and `#uiSourceCodeToSourceMaps` represents these as having no source maps.
27
+ *
28
+ * @see {@link SDK.SourceMap.SourceMap}
29
+ * @see {@link SDK.SourceMapManager.SourceMapManager}
30
+ */
31
+ export class CompilerScriptMapping {
32
+ #sourceMapManager;
33
+ #debuggerWorkspaceBinding;
34
+ #stubUISourceCodes = new Map();
35
+ #stubProject;
36
+ #eventListeners;
37
+ #projects = new Map();
38
+ #sourceMapToProject = new Map();
39
+ #uiSourceCodeToSourceMaps = new Platform.MapUtilities.Multimap();
40
+ constructor(debuggerModel, workspace, debuggerWorkspaceBinding) {
41
+ this.#sourceMapManager = debuggerModel.sourceMapManager();
42
+ this.#debuggerWorkspaceBinding = debuggerWorkspaceBinding;
43
+ this.#stubProject = new ContentProviderBasedProject(workspace, 'jsSourceMaps:stub:' + debuggerModel.target().id(), Workspace.Workspace.projectTypes.Service, '', true /* isServiceProject */);
44
+ this.#eventListeners = [
45
+ this.#sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapWillAttach, this.sourceMapWillAttach, this),
46
+ this.#sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapFailedToAttach, this.sourceMapFailedToAttach, this),
47
+ this.#sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapAttached, this.sourceMapAttached, this),
48
+ this.#sourceMapManager.addEventListener(SDK.SourceMapManager.Events.SourceMapDetached, this.sourceMapDetached, this),
49
+ ];
50
+ }
51
+ setFunctionRanges(uiSourceCode, ranges) {
52
+ for (const sourceMap of this.#uiSourceCodeToSourceMaps.get(uiSourceCode)) {
53
+ sourceMap.augmentWithScopes(uiSourceCode.url(), ranges);
54
+ }
55
+ }
56
+ addStubUISourceCode(script) {
57
+ const stubUISourceCode = this.#stubProject.addContentProvider(Common.ParsedURL.ParsedURL.concatenate(script.sourceURL, ':sourcemap'), TextUtils.StaticContentProvider.StaticContentProvider.fromString(script.sourceURL, Common.ResourceType.resourceTypes.Script, '\n\n\n\n\n// Please wait a bit.\n// Compiled script is not shown while source map is being loaded!'), 'text/javascript');
58
+ this.#stubUISourceCodes.set(script, stubUISourceCode);
59
+ }
60
+ removeStubUISourceCode(script) {
61
+ const uiSourceCode = this.#stubUISourceCodes.get(script);
62
+ this.#stubUISourceCodes.delete(script);
63
+ if (uiSourceCode) {
64
+ this.#stubProject.removeUISourceCode(uiSourceCode.url());
65
+ }
66
+ }
67
+ getLocationRangesForSameSourceLocation(rawLocation) {
68
+ const debuggerModel = rawLocation.debuggerModel;
69
+ const script = rawLocation.script();
70
+ if (!script) {
71
+ return [];
72
+ }
73
+ const sourceMap = this.#sourceMapManager.sourceMapForClient(script);
74
+ if (!sourceMap) {
75
+ return [];
76
+ }
77
+ // Find the source location for the raw location.
78
+ const { lineNumber, columnNumber } = script.rawLocationToRelativeLocation(rawLocation);
79
+ const entry = sourceMap.findEntry(lineNumber, columnNumber);
80
+ if (!entry?.sourceURL) {
81
+ return [];
82
+ }
83
+ const project = this.#sourceMapToProject.get(sourceMap);
84
+ if (!project) {
85
+ return [];
86
+ }
87
+ const uiSourceCode = project.uiSourceCodeForURL(entry.sourceURL);
88
+ if (!uiSourceCode) {
89
+ return [];
90
+ }
91
+ // Check that the reverse is also true and we have a binding for this |uiSourceCode|
92
+ // that is referred to from the |sourceMap| of the |script|.
93
+ if (!this.#uiSourceCodeToSourceMaps.hasValue(uiSourceCode, sourceMap)) {
94
+ return [];
95
+ }
96
+ // Map the source location back to raw location ranges.
97
+ const ranges = sourceMap.findReverseRanges(entry.sourceURL, entry.sourceLineNumber, entry.sourceColumnNumber);
98
+ return ranges.map(({ startLine, startColumn, endLine, endColumn }) => {
99
+ const start = script.relativeLocationToRawLocation({ lineNumber: startLine, columnNumber: startColumn });
100
+ const end = script.relativeLocationToRawLocation({ lineNumber: endLine, columnNumber: endColumn });
101
+ return {
102
+ start: debuggerModel.createRawLocation(script, start.lineNumber, start.columnNumber),
103
+ end: debuggerModel.createRawLocation(script, end.lineNumber, end.columnNumber),
104
+ };
105
+ });
106
+ }
107
+ uiSourceCodeForURL(url, isContentScript) {
108
+ const projectType = isContentScript ? Workspace.Workspace.projectTypes.ContentScripts : Workspace.Workspace.projectTypes.Network;
109
+ for (const project of this.#projects.values()) {
110
+ if (project.type() !== projectType) {
111
+ continue;
112
+ }
113
+ const uiSourceCode = project.uiSourceCodeForURL(url);
114
+ if (uiSourceCode) {
115
+ return uiSourceCode;
116
+ }
117
+ }
118
+ return null;
119
+ }
120
+ /**
121
+ * Resolves the source-mapped entity mapped from the given `rawLocation` if any. If the `rawLocation`
122
+ * does not point into a script with a source map, `null` is returned from here, while if the source
123
+ * map for the script is currently being loaded, a stub `UISourceCode` is returned meanwhile. Otherwise,
124
+ * if the script has a source map entry for the position identified by the `rawLocation`, this method
125
+ * will compute the location in the source-mapped file by a reverse lookup on the source map.
126
+ *
127
+ * If `rawLocation` points to a script with a source map managed by this `CompilerScriptMapping`, which
128
+ * is stale (because it was overridden by a more recent mapping), `null` will be returned.
129
+ *
130
+ * @param rawLocation script location.
131
+ * @returns the {@link Workspace.UISourceCode.UILocation} for the `rawLocation` if any.
132
+ */
133
+ rawLocationToUILocation(rawLocation) {
134
+ const script = rawLocation.script();
135
+ if (!script) {
136
+ return null;
137
+ }
138
+ const { lineNumber, columnNumber } = script.rawLocationToRelativeLocation(rawLocation);
139
+ const stubUISourceCode = this.#stubUISourceCodes.get(script);
140
+ if (stubUISourceCode) {
141
+ return new Workspace.UISourceCode.UILocation(stubUISourceCode, lineNumber, columnNumber);
142
+ }
143
+ const sourceMap = this.#sourceMapManager.sourceMapForClient(script);
144
+ if (!sourceMap) {
145
+ return null;
146
+ }
147
+ const project = this.#sourceMapToProject.get(sourceMap);
148
+ if (!project) {
149
+ return null;
150
+ }
151
+ const entry = sourceMap.findEntry(lineNumber, columnNumber, rawLocation.inlineFrameIndex);
152
+ if (!entry?.sourceURL) {
153
+ return null;
154
+ }
155
+ const uiSourceCode = project.uiSourceCodeForURL(entry.sourceURL);
156
+ if (!uiSourceCode) {
157
+ return null;
158
+ }
159
+ // Check that the reverse is also true and we have a binding for this `uiSourceCode`
160
+ // that is referred to from the `sourceMap` of the `script`.
161
+ if (!this.#uiSourceCodeToSourceMaps.hasValue(uiSourceCode, sourceMap)) {
162
+ return null;
163
+ }
164
+ return uiSourceCode.uiLocation(entry.sourceLineNumber, entry.sourceColumnNumber);
165
+ }
166
+ /**
167
+ * Resolves a location within a source mapped entity managed by the `CompilerScriptMapping`
168
+ * to its script locations. If the `uiSourceCode` does not belong to this mapping or if its
169
+ * mapping is stale, an empty list will be returned.
170
+ *
171
+ * A single UI location can map to multiple different {@link SDK.DebuggerModel.RawLocation}s,
172
+ * and these raw locations don't even need to belong to the same script (e.g. multiple bundles
173
+ * can reference the same shared source file in case of code splitting, and locations within
174
+ * this shared source file will then resolve to locations in all the bundles).
175
+ *
176
+ * @param uiSourceCode the source mapped entity.
177
+ * @param lineNumber the line number in terms of the {@link uiSourceCode}.
178
+ * @param columnNumber the column number in terms of the {@link uiSourceCode}.
179
+ * @returns a list of raw locations that correspond to the UI location.
180
+ */
181
+ uiLocationToRawLocations(uiSourceCode, lineNumber, columnNumber) {
182
+ const locations = [];
183
+ for (const sourceMap of this.#uiSourceCodeToSourceMaps.get(uiSourceCode)) {
184
+ const entry = sourceMap.sourceLineMapping(uiSourceCode.url(), lineNumber, columnNumber);
185
+ if (!entry) {
186
+ continue;
187
+ }
188
+ const script = this.#sourceMapManager.clientForSourceMap(sourceMap);
189
+ if (!script) {
190
+ continue;
191
+ }
192
+ const location = script.relativeLocationToRawLocation(entry);
193
+ locations.push(script.debuggerModel.createRawLocation(script, location.lineNumber, location.columnNumber));
194
+ }
195
+ return locations;
196
+ }
197
+ uiLocationRangeToRawLocationRanges(uiSourceCode, textRange) {
198
+ if (!this.#uiSourceCodeToSourceMaps.has(uiSourceCode)) {
199
+ return null;
200
+ }
201
+ const ranges = [];
202
+ for (const sourceMap of this.#uiSourceCodeToSourceMaps.get(uiSourceCode)) {
203
+ const script = this.#sourceMapManager.clientForSourceMap(sourceMap);
204
+ if (!script) {
205
+ continue;
206
+ }
207
+ for (const scriptTextRange of sourceMap.reverseMapTextRanges(uiSourceCode.url(), textRange)) {
208
+ const startLocation = script.relativeLocationToRawLocation(scriptTextRange.start);
209
+ const endLocation = script.relativeLocationToRawLocation(scriptTextRange.end);
210
+ const start = script.debuggerModel.createRawLocation(script, startLocation.lineNumber, startLocation.columnNumber);
211
+ const end = script.debuggerModel.createRawLocation(script, endLocation.lineNumber, endLocation.columnNumber);
212
+ ranges.push({ start, end });
213
+ }
214
+ }
215
+ return ranges;
216
+ }
217
+ translateRawFramesStep(_rawFrames, _translatedFrames) {
218
+ // TODO(crbug.com/433162438): Implement source map stack trace translation.
219
+ return false;
220
+ }
221
+ /**
222
+ * Computes the set of line numbers which are source-mapped to a script within the
223
+ * given {@link uiSourceCode}.
224
+ *
225
+ * @param uiSourceCode the source mapped entity.
226
+ * @returns a set of source-mapped line numbers or `null` if the {@link uiSourceCode}
227
+ * is not provided by this {@link CompilerScriptMapping} instance.
228
+ */
229
+ getMappedLines(uiSourceCode) {
230
+ if (!this.#uiSourceCodeToSourceMaps.has(uiSourceCode)) {
231
+ return null;
232
+ }
233
+ const mappedLines = new Set();
234
+ for (const sourceMap of this.#uiSourceCodeToSourceMaps.get(uiSourceCode)) {
235
+ for (const entry of sourceMap.mappings()) {
236
+ if (entry.sourceURL !== uiSourceCode.url()) {
237
+ continue;
238
+ }
239
+ mappedLines.add(entry.sourceLineNumber);
240
+ }
241
+ }
242
+ return mappedLines;
243
+ }
244
+ /**
245
+ * Invoked by the {@link SDK.SourceMapManager.SourceMapManager} whenever it starts loading the
246
+ * source map for a given {@link SDK.Script.Script}. The `CompilerScriptMapping` will set up a
247
+ * {@link Workspace.UISourceCode.UISourceCode} stub for the time that the source map is being
248
+ * loaded to avoid showing the generated code when the DevTools front-end is stopped early (for
249
+ * example on a breakpoint).
250
+ *
251
+ * @param event holds the {@link SDK.Script.Script} whose source map is being loaded.
252
+ */
253
+ sourceMapWillAttach(event) {
254
+ const { client: script } = event.data;
255
+ // Create stub UISourceCode for the time source mapping is being loaded.
256
+ this.addStubUISourceCode(script);
257
+ void this.#debuggerWorkspaceBinding.updateLocations(script);
258
+ if (Workspace.IgnoreListManager.IgnoreListManager.instance().isUserIgnoreListedURL(script.sourceURL, { isContentScript: script.isContentScript() })) {
259
+ this.#sourceMapManager.cancelAttachSourceMap(script);
260
+ }
261
+ }
262
+ /**
263
+ * Invoked by the {@link SDK.SourceMapManager.SourceMapManager} after an attempt to load the
264
+ * source map for a given {@link SDK.Script.Script} failed. The `CompilerScriptMapping` will
265
+ * remove the {@link Workspace.UISourceCode.UISourceCode} stub, and from this time on won't
266
+ * report any mappings for the `client` script.
267
+ *
268
+ * @param event holds the {@link SDK.Script.Script} whose source map failed to load.
269
+ */
270
+ sourceMapFailedToAttach(event) {
271
+ const { client: script } = event.data;
272
+ this.removeStubUISourceCode(script);
273
+ void this.#debuggerWorkspaceBinding.updateLocations(script);
274
+ }
275
+ /**
276
+ * Invoked by the {@link SDK.SourceMapManager.SourceMapManager} after an attempt to load the
277
+ * source map for a given {@link SDK.Script.Script} succeeded. The `CompilerScriptMapping` will
278
+ * now create {@link Workspace.UISourceCode.UISourceCode}s for all the sources mentioned in the
279
+ * `sourceMap`.
280
+ *
281
+ * In case of a conflict this method creates a new {@link Workspace.UISourceCode.UISourceCode}
282
+ * and copies over all the mappings from the other source maps that were registered for the
283
+ * same URL and which are compatible (agree on the content and ignore-list hint for the given
284
+ * URL). If they are considered incompatible, the original `UISourceCode` will simply be
285
+ * removed and a new mapping will be established.
286
+ *
287
+ * @param event holds the {@link SDK.Script.Script} and its {@link SDK.SourceMap.SourceMap}.
288
+ */
289
+ sourceMapAttached(event) {
290
+ const { client: script, sourceMap } = event.data;
291
+ const scripts = new Set([script]);
292
+ this.removeStubUISourceCode(script);
293
+ const target = script.target();
294
+ const projectId = `jsSourceMaps:${script.isContentScript() ? 'extensions' : ''}:${target.id()}`;
295
+ let project = this.#projects.get(projectId);
296
+ if (!project) {
297
+ const projectType = script.isContentScript() ? Workspace.Workspace.projectTypes.ContentScripts :
298
+ Workspace.Workspace.projectTypes.Network;
299
+ project = new ContentProviderBasedProject(this.#stubProject.workspace(), projectId, projectType, /* displayName */ '', /* isServiceProject */ false);
300
+ NetworkProject.setTargetForProject(project, target);
301
+ this.#projects.set(projectId, project);
302
+ }
303
+ this.#sourceMapToProject.set(sourceMap, project);
304
+ for (const url of sourceMap.sourceURLs()) {
305
+ const contentType = Common.ResourceType.resourceTypes.SourceMapScript;
306
+ const uiSourceCode = project.createUISourceCode(url, contentType);
307
+ if (sourceMap.hasIgnoreListHint(url)) {
308
+ uiSourceCode.markKnownThirdParty();
309
+ }
310
+ const content = sourceMap.embeddedContentByURL(url);
311
+ const contentProvider = content !== null ?
312
+ TextUtils.StaticContentProvider.StaticContentProvider.fromString(url, contentType, content) :
313
+ new SDK.CompilerSourceMappingContentProvider.CompilerSourceMappingContentProvider(url, contentType, script.createPageResourceLoadInitiator());
314
+ let metadata = null;
315
+ if (content !== null) {
316
+ const encoder = new TextEncoder();
317
+ metadata = new Workspace.UISourceCode.UISourceCodeMetadata(null, encoder.encode(content).length);
318
+ }
319
+ const mimeType = Common.ResourceType.ResourceType.mimeFromURL(url) ?? contentType.canonicalMimeType();
320
+ this.#uiSourceCodeToSourceMaps.set(uiSourceCode, sourceMap);
321
+ NetworkProject.setInitialFrameAttribution(uiSourceCode, script.frameId);
322
+ // Check if there was already an `UISourceCode` for the given `url`, and if so, discard
323
+ // the previous one. While it would be possible to keep the previous one and just add
324
+ // the new mapping (from the `sourceMap`) to it as long as there's no conflict, this
325
+ // doesn't really work with the way the `BreakpointManager` and other parts of the front-end
326
+ // work, which only listen for additions/removals of `UISourceCode`s, since there's no
327
+ // notion of a 'UISourceCodeChanged` event (yet).
328
+ //
329
+ // Therefore, unless we discard any previous `UISourceCode` for the `url` and publish the
330
+ // new `uiSourceCode`, the `BreakpointManager` will not restore / set breakpoints in newly
331
+ // added scripts in case of code-splitting, since it won't find out about these new mappings.
332
+ // By removing and (re)adding a `UISourceCode` for the `url` we effectively force restoration
333
+ // of breakpoints.
334
+ const otherUISourceCode = project.uiSourceCodeForURL(url);
335
+ if (otherUISourceCode !== null) {
336
+ // Copy the existing source mappings from the `otherUISourceCode` over as long as
337
+ // they are compatible with the `sourceMap` wrt. `url`. While doing so, also clean
338
+ // up the `otherUISourceCode` (in particular its frame attributions).
339
+ for (const otherSourceMap of this.#uiSourceCodeToSourceMaps.get(otherUISourceCode)) {
340
+ this.#uiSourceCodeToSourceMaps.delete(otherUISourceCode, otherSourceMap);
341
+ const otherScript = this.#sourceMapManager.clientForSourceMap(otherSourceMap);
342
+ if (!otherScript) {
343
+ continue;
344
+ }
345
+ NetworkProject.removeFrameAttribution(otherUISourceCode, otherScript.frameId);
346
+ if (sourceMap.compatibleForURL(url, otherSourceMap)) {
347
+ this.#uiSourceCodeToSourceMaps.set(uiSourceCode, otherSourceMap);
348
+ NetworkProject.addFrameAttribution(uiSourceCode, otherScript.frameId);
349
+ }
350
+ scripts.add(otherScript);
351
+ }
352
+ project.removeUISourceCode(url);
353
+ }
354
+ project.addUISourceCodeWithProvider(uiSourceCode, contentProvider, metadata, mimeType);
355
+ }
356
+ void Promise.all([...scripts].map(script => this.#debuggerWorkspaceBinding.updateLocations(script)))
357
+ .then(() => this.sourceMapAttachedForTest(sourceMap));
358
+ }
359
+ /**
360
+ * Invoked by the {@link SDK.SourceMapManager.SourceMapManager} when the source map for a given
361
+ * {@link SDK.Script.Script} is removed, which could be either because the target is execution
362
+ * context was destroyed, or the user manually asked to replace the source map for a given
363
+ * script.
364
+ *
365
+ * @param event holds the {@link SDK.Script.Script} and {@link SDK.SourceMap.SourceMap} that
366
+ * should be detached.
367
+ */
368
+ sourceMapDetached(event) {
369
+ const { client: script, sourceMap } = event.data;
370
+ const project = this.#sourceMapToProject.get(sourceMap);
371
+ if (!project) {
372
+ return;
373
+ }
374
+ // Remove all the files in the `project` that (still) belong to the `sourceMap`.
375
+ // In case of conflicts or overrides (for example due to HMR), not all the files
376
+ // that were originally provided by the `sourceMap` might still belong to it.
377
+ for (const uiSourceCode of project.uiSourceCodes()) {
378
+ if (this.#uiSourceCodeToSourceMaps.delete(uiSourceCode, sourceMap)) {
379
+ NetworkProject.removeFrameAttribution(uiSourceCode, script.frameId);
380
+ if (!this.#uiSourceCodeToSourceMaps.has(uiSourceCode)) {
381
+ project.removeUISourceCode(uiSourceCode.url());
382
+ }
383
+ }
384
+ }
385
+ this.#sourceMapToProject.delete(sourceMap);
386
+ void this.#debuggerWorkspaceBinding.updateLocations(script);
387
+ }
388
+ scriptsForUISourceCode(uiSourceCode) {
389
+ const scripts = [];
390
+ for (const sourceMap of this.#uiSourceCodeToSourceMaps.get(uiSourceCode)) {
391
+ const script = this.#sourceMapManager.clientForSourceMap(sourceMap);
392
+ if (script) {
393
+ scripts.push(script);
394
+ }
395
+ }
396
+ return scripts;
397
+ }
398
+ sourceMapAttachedForTest(_sourceMap) {
399
+ }
400
+ dispose() {
401
+ Common.EventTarget.removeEventListeners(this.#eventListeners);
402
+ for (const project of this.#projects.values()) {
403
+ project.dispose();
404
+ }
405
+ this.#stubProject.dispose();
406
+ }
407
+ }