chrome-devtools-frontend 1.0.1515988 → 1.0.1518653

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 (122) hide show
  1. package/docs/checklist/README.md +2 -2
  2. package/docs/checklist/javascript.md +1 -1
  3. package/docs/contributing/README.md +1 -1
  4. package/docs/contributing/settings-experiments-features.md +9 -8
  5. package/docs/cookbook/devtools_on_devtools.md +2 -2
  6. package/docs/cookbook/localization.md +10 -10
  7. package/docs/devtools-protocol.md +9 -8
  8. package/docs/ecosystem/automatic_workspace_folders.md +3 -3
  9. package/docs/get_the_code.md +0 -2
  10. package/docs/styleguide/ux/components.md +166 -85
  11. package/docs/styleguide/ux/numbers.md +3 -4
  12. package/front_end/core/common/README.md +13 -12
  13. package/front_end/core/host/GdpClient.ts +16 -1
  14. package/front_end/core/host/UserMetrics.ts +8 -2
  15. package/front_end/core/root/Runtime.ts +13 -0
  16. package/front_end/core/sdk/CSSMatchedStyles.ts +5 -1
  17. package/front_end/entrypoints/main/MainImpl.ts +6 -3
  18. package/front_end/generated/InspectorBackendCommands.js +10 -7
  19. package/front_end/generated/SupportedCSSProperties.js +21 -7
  20. package/front_end/generated/protocol-mapping.d.ts +16 -1
  21. package/front_end/generated/protocol-proxy-api.d.ts +13 -1
  22. package/front_end/generated/protocol.ts +95 -0
  23. package/front_end/models/ai_assistance/agents/PerformanceAgent.ts +170 -54
  24. package/front_end/models/ai_assistance/data_formatters/PerformanceInsightFormatter.snapshot.txt +14 -181
  25. package/front_end/models/ai_assistance/data_formatters/PerformanceInsightFormatter.ts +13 -315
  26. package/front_end/models/ai_assistance/data_formatters/PerformanceTraceFormatter.snapshot.txt +224 -50
  27. package/front_end/models/ai_assistance/data_formatters/PerformanceTraceFormatter.ts +310 -11
  28. package/front_end/models/ai_assistance/performance/AIContext.ts +15 -2
  29. package/front_end/models/ai_code_completion/AiCodeCompletion.ts +41 -19
  30. package/front_end/models/badges/Badge.ts +8 -3
  31. package/front_end/models/badges/CodeWhispererBadge.ts +2 -4
  32. package/front_end/models/badges/StarterBadge.ts +2 -2
  33. package/front_end/models/badges/UserBadges.ts +59 -6
  34. package/front_end/models/formatter/FormatterWorkerPool.ts +3 -3
  35. package/front_end/models/javascript_metadata/NativeFunctions.js +1 -1
  36. package/front_end/models/trace/README.md +28 -1
  37. package/front_end/models/trace/handlers/UserTimingsHandler.ts +1 -1
  38. package/front_end/models/trace/helpers/Trace.ts +99 -43
  39. package/front_end/models/trace/types/TraceEvents.ts +9 -0
  40. package/front_end/panels/accessibility/ARIAAttributesView.ts +113 -191
  41. package/front_end/panels/accessibility/AccessibilityNodeView.ts +9 -9
  42. package/front_end/panels/accessibility/AccessibilitySubPane.ts +6 -4
  43. package/front_end/panels/accessibility/accessibilityProperties.css +2 -0
  44. package/front_end/panels/ai_assistance/AiAssistancePanel.ts +16 -2
  45. package/front_end/panels/ai_assistance/components/ChatView.ts +9 -10
  46. package/front_end/panels/ai_assistance/components/PerformanceAgentMarkdownRenderer.ts +42 -0
  47. package/front_end/panels/common/AiCodeCompletionDisclaimer.ts +32 -9
  48. package/front_end/panels/common/AiCodeCompletionSummaryToolbar.ts +7 -1
  49. package/front_end/panels/common/BadgeNotification.ts +67 -15
  50. package/front_end/panels/common/GdpSignUpDialog.ts +18 -9
  51. package/front_end/panels/console/ConsolePrompt.ts +1 -1
  52. package/front_end/panels/console/ConsoleView.ts +6 -2
  53. package/front_end/panels/elements/ComputedStyleWidget.ts +1 -2
  54. package/front_end/panels/elements/ElementsPanel.ts +4 -0
  55. package/front_end/panels/elements/ElementsTreeElement.ts +18 -0
  56. package/front_end/panels/elements/ElementsTreeOutline.ts +13 -0
  57. package/front_end/panels/elements/LayoutPane.ts +1 -1
  58. package/front_end/panels/elements/StylePropertyTreeElement.ts +21 -6
  59. package/front_end/panels/media/TickingFlameChart.ts +1 -1
  60. package/front_end/panels/network/NetworkLogView.ts +5 -1
  61. package/front_end/panels/profiler/HeapSnapshotView.ts +34 -19
  62. package/front_end/panels/search/SearchResultsPane.ts +126 -145
  63. package/front_end/panels/search/SearchView.ts +43 -59
  64. package/front_end/panels/settings/components/SyncSection.ts +16 -8
  65. package/front_end/panels/sources/AiCodeCompletionPlugin.ts +6 -1
  66. package/front_end/panels/sources/OutlineQuickOpen.ts +3 -1
  67. package/front_end/panels/sources/SourcesPanel.ts +3 -0
  68. package/front_end/panels/timeline/AppenderUtils.ts +2 -2
  69. package/front_end/panels/timeline/ExtensionTrackAppender.ts +13 -4
  70. package/front_end/panels/timeline/GPUTrackAppender.ts +2 -1
  71. package/front_end/panels/timeline/InteractionsTrackAppender.ts +5 -1
  72. package/front_end/panels/timeline/LayoutShiftsTrackAppender.ts +2 -1
  73. package/front_end/panels/timeline/ThreadAppender.ts +12 -3
  74. package/front_end/panels/timeline/TimelineFlameChartDataProvider.ts +9 -4
  75. package/front_end/panels/timeline/TimelinePanel.ts +3 -2
  76. package/front_end/panels/timeline/TimelineUIUtils.ts +18 -12
  77. package/front_end/panels/timeline/TimingsTrackAppender.ts +6 -1
  78. package/front_end/panels/timeline/components/CPUThrottlingSelector.ts +95 -82
  79. package/front_end/panels/timeline/components/LiveMetricsView.ts +2 -2
  80. package/front_end/panels/timeline/components/cpuThrottlingSelector.css +17 -15
  81. package/front_end/panels/timeline/components/insights/BaseInsightComponent.ts +3 -0
  82. package/front_end/third_party/chromium/README.chromium +1 -1
  83. package/front_end/third_party/codemirror.next/chunk/codemirror.js +1 -1
  84. package/front_end/third_party/codemirror.next/chunk/codemirror.js.map +1 -1
  85. package/front_end/third_party/codemirror.next/codemirror.next.d.ts +6 -9
  86. package/front_end/third_party/codemirror.next/package.json +2 -1
  87. package/front_end/third_party/diff/README.chromium +1 -0
  88. package/front_end/third_party/puppeteer/README.chromium +2 -2
  89. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/bidi/core/Realm.d.ts +2 -2
  90. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/cdp/Accessibility.js +0 -20
  91. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/cdp/Accessibility.js.map +1 -1
  92. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/generated/version.d.ts +1 -1
  93. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/generated/version.js +1 -1
  94. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/injected/injected.d.ts +1 -1
  95. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/revisions.d.ts +1 -1
  96. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/revisions.js +1 -1
  97. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/revisions.js.map +1 -1
  98. package/front_end/third_party/puppeteer/package/lib/cjs/puppeteer/util/Mutex.d.ts +2 -2
  99. package/front_end/third_party/puppeteer/package/lib/es5-iife/puppeteer-core-browser.js +2 -23
  100. package/front_end/third_party/puppeteer/package/lib/esm/puppeteer/cdp/Accessibility.js +0 -20
  101. package/front_end/third_party/puppeteer/package/lib/esm/puppeteer/cdp/Accessibility.js.map +1 -1
  102. package/front_end/third_party/puppeteer/package/lib/esm/puppeteer/generated/version.d.ts +1 -1
  103. package/front_end/third_party/puppeteer/package/lib/esm/puppeteer/generated/version.js +1 -1
  104. package/front_end/third_party/puppeteer/package/lib/esm/puppeteer/revisions.d.ts +1 -1
  105. package/front_end/third_party/puppeteer/package/lib/esm/puppeteer/revisions.js +1 -1
  106. package/front_end/third_party/puppeteer/package/lib/esm/puppeteer/revisions.js.map +1 -1
  107. package/front_end/third_party/puppeteer/package/package.json +1 -1
  108. package/front_end/third_party/puppeteer/package/src/cdp/Accessibility.ts +1 -21
  109. package/front_end/third_party/puppeteer/package/src/generated/version.ts +1 -1
  110. package/front_end/third_party/puppeteer/package/src/revisions.ts +1 -1
  111. package/front_end/ui/components/text_editor/config.ts +36 -8
  112. package/front_end/ui/components/tooltips/Tooltip.ts +71 -34
  113. package/front_end/ui/legacy/README.md +33 -24
  114. package/front_end/ui/legacy/SearchableView.ts +19 -26
  115. package/front_end/ui/legacy/TextPrompt.ts +166 -1
  116. package/front_end/ui/legacy/Treeoutline.ts +16 -2
  117. package/front_end/ui/legacy/UIUtils.ts +15 -2
  118. package/front_end/ui/legacy/XElement.ts +0 -43
  119. package/front_end/ui/legacy/components/perf_ui/FlameChart.ts +20 -4
  120. package/front_end/ui/visual_logging/KnownContextValues.ts +24 -6
  121. package/front_end/ui/visual_logging/README.md +43 -27
  122. package/package.json +1 -1
@@ -4,9 +4,10 @@
4
4
 
5
5
  import * as Common from '../../core/common/common.js';
6
6
  import * as Host from '../../core/host/host.js';
7
+ import * as Root from '../../core/root/root.js';
7
8
 
8
9
  import {AiExplorerBadge} from './AiExplorerBadge.js';
9
- import type {Badge, BadgeAction, BadgeActionEvents, BadgeContext} from './Badge.js';
10
+ import type {Badge, BadgeAction, BadgeActionEvents, BadgeContext, TriggerOptions} from './Badge.js';
10
11
  import {CodeWhispererBadge} from './CodeWhispererBadge.js';
11
12
  import {DOMDetectiveBadge} from './DOMDetectiveBadge.js';
12
13
  import {SpeedsterBadge} from './SpeedsterBadge.js';
@@ -22,6 +23,10 @@ export interface EventTypes {
22
23
  [Events.BADGE_TRIGGERED]: Badge;
23
24
  }
24
25
 
26
+ const SNOOZE_TIME_MS = 24 * 60 * 60 * 1000; // 24 hours
27
+ const MAX_SNOOZE_COUNT = 3;
28
+ const DELAY_BEFORE_TRIGGER = 1500;
29
+
25
30
  let userBadgesInstance: UserBadges|undefined = undefined;
26
31
  export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
27
32
  readonly #badgeActionEventTarget = new Common.ObjectWrapper.ObjectWrapper<BadgeActionEvents>();
@@ -29,6 +34,10 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
29
34
  #receiveBadgesSetting: Common.Settings.Setting<Boolean>;
30
35
  #allBadges: Badge[];
31
36
 
37
+ #starterBadgeSnoozeCount: Common.Settings.Setting<number>;
38
+ #starterBadgeLastSnoozedTimestamp: Common.Settings.Setting<number>;
39
+ #starterBadgeDismissed: Common.Settings.Setting<boolean>;
40
+
32
41
  static readonly BADGE_REGISTRY: BadgeClass[] = [
33
42
  StarterBadge,
34
43
  SpeedsterBadge,
@@ -41,8 +50,19 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
41
50
  super();
42
51
 
43
52
  this.#receiveBadgesSetting = Common.Settings.Settings.instance().moduleSetting('receive-gdp-badges');
53
+ if (Host.GdpClient.getGdpProfilesEnterprisePolicy() ===
54
+ Root.Runtime.GdpProfilesEnterprisePolicyValue.ENABLED_WITHOUT_BADGES) {
55
+ this.#receiveBadgesSetting.set(false);
56
+ }
44
57
  this.#receiveBadgesSetting.addChangeListener(this.#reconcileBadges, this);
45
58
 
59
+ this.#starterBadgeSnoozeCount = Common.Settings.Settings.instance().createSetting(
60
+ 'starter-badge-snooze-count', 0, Common.Settings.SettingStorageType.SYNCED);
61
+ this.#starterBadgeLastSnoozedTimestamp = Common.Settings.Settings.instance().createSetting(
62
+ 'starter-badge-last-snoozed-timestamp', 0, Common.Settings.SettingStorageType.SYNCED);
63
+ this.#starterBadgeDismissed = Common.Settings.Settings.instance().createSetting(
64
+ 'starter-badge-dismissed', false, Common.Settings.SettingStorageType.SYNCED);
65
+
46
66
  this.#allBadges = UserBadges.BADGE_REGISTRY.map(badgeCtor => new badgeCtor({
47
67
  onTriggerBadge: this.#onTriggerBadge.bind(this),
48
68
  badgeActionEventTarget: this.#badgeActionEventTarget,
@@ -60,6 +80,15 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
60
80
  return await this.#reconcileBadges();
61
81
  }
62
82
 
83
+ snoozeStarterBadge(): void {
84
+ this.#starterBadgeSnoozeCount.set(this.#starterBadgeSnoozeCount.get() + 1);
85
+ this.#starterBadgeLastSnoozedTimestamp.set(Date.now());
86
+ }
87
+
88
+ dismissStarterBadge(): void {
89
+ this.#starterBadgeDismissed.set(true);
90
+ }
91
+
63
92
  recordAction(action: BadgeAction): void {
64
93
  // `Common.ObjectWrapper.ObjectWrapper` does not allow passing unions to
65
94
  // the `dispatchEventToListeners` and `action` in this case is a union.
@@ -70,7 +99,8 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
70
99
  this.#badgeActionEventTarget.dispatchEventToListeners(action);
71
100
  }
72
101
 
73
- async #onTriggerBadge(badge: Badge): Promise<void> {
102
+ async #onTriggerBadge(badge: Badge, opts?: TriggerOptions): Promise<void> {
103
+ const triggerTime = Date.now();
74
104
  let shouldAwardBadge = false;
75
105
  // By default, we award non-starter badges directly when they are triggered.
76
106
  if (!badge.isStarterBadge) {
@@ -79,7 +109,8 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
79
109
  const gdpProfile = await Host.GdpClient.GdpClient.instance().getProfile();
80
110
  const receiveBadgesSettingEnabled = Boolean(this.#receiveBadgesSetting.get());
81
111
  // If there is a GDP profile and the user has enabled receiving badges, we award the starter badge as well.
82
- if (gdpProfile && receiveBadgesSettingEnabled) {
112
+ if (gdpProfile && receiveBadgesSettingEnabled && !this.#isStarterBadgeDismissed() &&
113
+ !this.#isStarterBadgeSnoozed()) {
83
114
  shouldAwardBadge = true;
84
115
  }
85
116
  }
@@ -92,7 +123,12 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
92
123
  }
93
124
  }
94
125
 
95
- this.dispatchEventToListeners(Events.BADGE_TRIGGERED, badge);
126
+ const timeElapsedAfterTriggerCall = Date.now() - triggerTime;
127
+ // We want to add exactly 1.5 second delay between the trigger action & the notification.
128
+ const delay = opts?.immediate ? 0 : Math.max(DELAY_BEFORE_TRIGGER - timeElapsedAfterTriggerCall, 0);
129
+ setTimeout(() => {
130
+ this.dispatchEventToListeners(Events.BADGE_TRIGGERED, badge);
131
+ }, delay);
96
132
  }
97
133
 
98
134
  #deactivateAllBadges(): void {
@@ -101,7 +137,17 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
101
137
  });
102
138
  }
103
139
 
104
- // TODO(ergunsh): Implement starter badge dismissal, snooze count & timestamp checks.
140
+ #isStarterBadgeDismissed(): boolean {
141
+ return this.#starterBadgeDismissed.get();
142
+ }
143
+
144
+ #isStarterBadgeSnoozed(): boolean {
145
+ const snoozeCount = this.#starterBadgeSnoozeCount.get();
146
+ const lastSnoozed = this.#starterBadgeLastSnoozedTimestamp.get();
147
+ const snoozedRecently = (Date.now() - lastSnoozed) < SNOOZE_TIME_MS;
148
+ return snoozeCount >= MAX_SNOOZE_COUNT || snoozedRecently;
149
+ }
150
+
105
151
  async #reconcileBadges(): Promise<void> {
106
152
  const syncInfo = await new Promise<Host.InspectorFrontendHostAPI.SyncInformation>(
107
153
  resolve => Host.InspectorFrontendHost.InspectorFrontendHostInstance.getSyncInformation(resolve));
@@ -111,6 +157,12 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
111
157
  return;
112
158
  }
113
159
 
160
+ if (!Host.GdpClient.isGdpProfilesAvailable() ||
161
+ Host.GdpClient.getGdpProfilesEnterprisePolicy() !== Root.Runtime.GdpProfilesEnterprisePolicyValue.ENABLED) {
162
+ this.#deactivateAllBadges();
163
+ return;
164
+ }
165
+
114
166
  const [gdpProfile, isEligibleToCreateProfile] = await Promise.all([
115
167
  Host.GdpClient.GdpClient.instance().getProfile(),
116
168
  Host.GdpClient.GdpClient.instance().isEligibleToCreateProfile(),
@@ -148,7 +200,8 @@ export class UserBadges extends Common.ObjectWrapper.ObjectWrapper<EventTypes> {
148
200
  continue;
149
201
  }
150
202
 
151
- const shouldActivateStarterBadge = badge.isStarterBadge && isEligibleToCreateProfile;
203
+ const shouldActivateStarterBadge = badge.isStarterBadge && isEligibleToCreateProfile &&
204
+ !this.#isStarterBadgeDismissed() && !this.#isStarterBadgeSnoozed();
152
205
  const shouldActivateActivityBasedBadge =
153
206
  !badge.isStarterBadge && Boolean(gdpProfile) && receiveBadgesSettingEnabled;
154
207
  if (shouldActivateStarterBadge || shouldActivateActivityBasedBadge) {
@@ -7,8 +7,6 @@ import * as FormatterActions from '../../entrypoints/formatter_worker/FormatterA
7
7
 
8
8
  export {DefinitionKind, type ScopeTreeNode} from '../../entrypoints/formatter_worker/FormatterActions.js';
9
9
 
10
- const MAX_WORKERS = Math.max(2, navigator.hardwareConcurrency - 1);
11
-
12
10
  let formatterWorkerPoolInstance: FormatterWorkerPool;
13
11
 
14
12
  export class FormatterWorkerPool {
@@ -37,12 +35,14 @@ export class FormatterWorkerPool {
37
35
  }
38
36
 
39
37
  private processNextTask(): void {
38
+ const maxWorkers = Math.max(2, navigator.hardwareConcurrency - 1);
39
+
40
40
  if (!this.taskQueue.length) {
41
41
  return;
42
42
  }
43
43
 
44
44
  let freeWorker = [...this.workerTasks.keys()].find(worker => !this.workerTasks.get(worker));
45
- if (!freeWorker && this.workerTasks.size < MAX_WORKERS) {
45
+ if (!freeWorker && this.workerTasks.size < maxWorkers) {
46
46
  freeWorker = this.createWorker();
47
47
  }
48
48
  if (!freeWorker) {
@@ -8470,7 +8470,7 @@ export const NativeFunctions = [
8470
8470
  },
8471
8471
  {
8472
8472
  name: "submitPrintJob",
8473
- signatures: [["job_name","document","attributes"]]
8473
+ signatures: [["job_name","document_data","attributes"]]
8474
8474
  },
8475
8475
  {
8476
8476
  name: "PushEvent",
@@ -31,7 +31,7 @@ This folder contains the new trace engine that was first implemented for the Per
31
31
 
32
32
 
33
33
  ┌──────────────────▼─────────────────┐
34
- │const data = model.parsedTrace()│
34
+ │const data = model.parsedTrace()
35
35
  └────────────────────────────────────┘
36
36
  ```
37
37
 
@@ -98,3 +98,30 @@ The object returned from `parsedTrace()` is an object of key-value pairs where e
98
98
  // and so on for each enabled Handler
99
99
  }
100
100
  ```
101
+
102
+ ## Pairing begin & end events
103
+
104
+ Note: this detail is not useful if you are using the Trace Engine, but it is if you are working on it.
105
+
106
+ Trace events are often emitted as `begin` & `end` events to represent the lifetime of the event. These have the `b` and `e` phase.
107
+
108
+ When we find these events, we often try to pair them into what we call a "Synthetic" event. This is a trace event that doesn't exist in the raw trace, but one that we create to make it easier to deal with. This means we can represent a `b` & `e` pair as a single event rather than pass two events around.
109
+
110
+ When we pair these events, we look for an ID. Some events have a top level `id` field, others have a nested `id2.local` field (this is for various historical reasons). `getSyntheticId` in `Trace.Helpers` takes care of this, and tries to account for potential collisions by appending a few other pieces of metadata onto the ID.
111
+
112
+ This approach worked well until July 2025 when an upstream change in Perfetto [https://chromium.googlesource.com/external/github.com/google/perfetto.git/+/aef636b27ffbf379fd722e7798030da2c5c4d699] meant that Perfetto will try to minimise the amount of unique IDs it uses. A consequence of this change is that IDs can be reused by consecutive, non-overlapping events.
113
+
114
+ For example, take the following set of events:
115
+
116
+ ```
117
+ === E1 === === E2 ===
118
+ ```
119
+
120
+ These could have the same ID, because they do not overlap.
121
+
122
+ Whereas these events will have different IDs because otherwise you cannot reliably pair them up:
123
+
124
+ ```
125
+ === E1 ===
126
+ === E2 ===
127
+ ```
@@ -223,9 +223,9 @@ export async function finalize(): Promise<void> {
223
223
 
224
224
  export function data(): UserTimingsData {
225
225
  return {
226
+ consoleTimings: syntheticEvents.filter(e => e.cat === 'blink.console') as Types.Events.SyntheticConsoleTimingPair[],
226
227
  performanceMeasures: syntheticEvents.filter(e => e.cat === 'blink.user_timing') as
227
228
  Types.Events.SyntheticUserTimingPair[],
228
- consoleTimings: syntheticEvents.filter(e => e.cat === 'blink.console') as Types.Events.SyntheticConsoleTimingPair[],
229
229
  performanceMarks: performanceMarkEvents,
230
230
  timestampEvents,
231
231
  measureTraceByTraceId,
@@ -12,6 +12,7 @@ import {SyntheticEventsManager} from './SyntheticEvents.js';
12
12
  import {eventTimingsMicroSeconds} from './Timing.js';
13
13
 
14
14
  interface MatchingPairableAsyncEvents {
15
+ syntheticId: string;
15
16
  begin: Types.Events.PairableAsyncBegin|null;
16
17
  end: Types.Events.PairableAsyncEnd|null;
17
18
  instant?: Types.Events.PairableAsyncInstant[];
@@ -290,62 +291,113 @@ export function makeProfileCall(
290
291
  }
291
292
 
292
293
  /**
293
- * Matches beginning events with PairableAsyncEnd and PairableAsyncInstant (ASYNC_NESTABLE_INSTANT)
294
- * if provided, though currently only coming from Animations. Traces may contain multiple instant events so we need to
295
- * account for that.
294
+ * Matches beginning events with PairableAsyncEnd and PairableAsyncInstant
295
+ * if provided. Traces may contain multiple instant events so we need to
296
+ * account for that. Additionally we have seen cases where we might only have a
297
+ * begin event & instant event(s), with no end event. So we account for that
298
+ * situation also.
296
299
  *
297
- * @returns Map of the animation's ID to it's matching events.
300
+ * You might also like to read the models/trace/README.md which has some
301
+ * documentation on trace IDs. This is important as Perfetto will reuse trace
302
+ * IDs when emitting events (if they do not overlap). This means it's not as
303
+ * simple as grouping events by IDs. Instead, we group begin & instant events
304
+ * by ID as we find them. When we find end events, we then pop any matching
305
+ * begin/instant events off the stack and group those. That way, if we meet the
306
+ * same ID later on it doesn't cause us collisions.
307
+ *
308
+ * @returns An array of all the matched event groups, along with their ID. Note
309
+ * that two event groups can have the same ID if they were non-overlapping
310
+ * events. You cannot rely on ID being unique across a trace. The returned set
311
+ * of groups are NOT SORTED in any order.
298
312
  */
299
- export function matchEvents(unpairedEvents: Types.Events.PairableAsync[]): Map<string, MatchingPairableAsyncEvents> {
313
+ function matchEvents(unpairedEvents: Types.Events.PairableAsync[]): MatchingPairableAsyncEvents[] {
314
+ sortTraceEventsInPlace(unpairedEvents);
300
315
  // map to store begin and end of the event
301
- const matchedPairs = new Map<string, MatchingPairableAsyncEvents>();
316
+ const matches: MatchingPairableAsyncEvents[] = [];
302
317
 
303
- // looking for start and end
318
+ const beginEventsById = new Map<string, Types.Events.PairableAsyncBegin[]>();
319
+ const instantEventsById = new Map<string, Types.Events.PairableAsyncInstant[]>();
304
320
  for (const event of unpairedEvents) {
305
- const syntheticId = getSyntheticId(event);
306
- if (syntheticId === undefined) {
321
+ const id = getSyntheticId(event);
322
+ if (id === undefined) {
307
323
  continue;
308
324
  }
309
- // Create a synthetic id to prevent collisions across categories.
310
- // Console timings can be dispatched with the same id, so use the
311
- // event name as well to generate unique ids.
312
- const otherEventsWithID = Platform.MapUtilities.getWithDefault(matchedPairs, syntheticId, () => {
313
- return {begin: null, end: null, instant: []};
314
- });
315
-
316
- const isStartEvent = event.ph === Types.Events.Phase.ASYNC_NESTABLE_START;
317
- const isEndEvent = event.ph === Types.Events.Phase.ASYNC_NESTABLE_END;
318
- const isInstantEvent = event.ph === Types.Events.Phase.ASYNC_NESTABLE_INSTANT;
319
-
320
- if (isStartEvent) {
321
- otherEventsWithID.begin = event as Types.Events.PairableAsyncBegin;
322
- } else if (isEndEvent) {
323
- otherEventsWithID.end = event as Types.Events.PairableAsyncEnd;
324
- } else if (isInstantEvent) {
325
- if (!otherEventsWithID.instant) {
326
- otherEventsWithID.instant = [];
325
+ if (Types.Events.isPairableAsyncBegin(event)) {
326
+ const existingEvents = beginEventsById.get(id) ?? [];
327
+ existingEvents.push(event);
328
+ beginEventsById.set(id, existingEvents);
329
+ } else if (Types.Events.isPairableAsyncInstant(event)) {
330
+ const existingEvents = instantEventsById.get(id) ?? [];
331
+ existingEvents.push(event);
332
+ instantEventsById.set(id, existingEvents);
333
+ } else if (Types.Events.isPairableAsyncEnd(event)) {
334
+ // Find matching begin event by ID
335
+ const beginEventsWithMatchingId = beginEventsById.get(id) ?? [];
336
+ const beginEvent = beginEventsWithMatchingId.pop();
337
+ if (!beginEvent) {
338
+ continue;
327
339
  }
328
- otherEventsWithID.instant.push(event as Types.Events.PairableAsyncInstant);
340
+ const instantEventsWithMatchingId = instantEventsById.get(id) ?? [];
341
+ // Find all instant events after the begin event ts.
342
+ const instantEventsForThisGroup: Types.Events.PairableAsyncInstant[] = [];
343
+ while (instantEventsWithMatchingId.length > 0) {
344
+ if (instantEventsWithMatchingId[0].ts >= beginEvent.ts) {
345
+ const event = instantEventsWithMatchingId.pop();
346
+ if (event) {
347
+ instantEventsForThisGroup.push(event);
348
+ }
349
+ } else {
350
+ break;
351
+ }
352
+ }
353
+ const matchingGroup: MatchingPairableAsyncEvents = {
354
+ begin: beginEvent,
355
+ end: event,
356
+ instant: instantEventsForThisGroup,
357
+ syntheticId: id,
358
+ };
359
+ matches.push(matchingGroup);
329
360
  }
330
361
  }
331
- return matchedPairs;
362
+
363
+ // At this point we know we have paired up all the Begin & End & Instant
364
+ // events. But it is possible to see only begin & instant events with the
365
+ // same ID, and no end event. So now we do a second pass through our begin
366
+ // events to find any that did not have an end event. If we find some
367
+ // instant events for the begin event, we create a new group.
368
+ // Also, because there were no end events, we know that the IDs will be
369
+ // unique now; e.g. each key in the map should have no more than one item in
370
+ // it.
371
+ for (const [id, beginEvents] of beginEventsById) {
372
+ const beginEvent = beginEvents.pop();
373
+ if (!beginEvent) {
374
+ continue;
375
+ }
376
+ const matchingInstantEvents = instantEventsById.get(id);
377
+ if (matchingInstantEvents?.length) {
378
+ matches.push({
379
+ syntheticId: id,
380
+ begin: beginEvent,
381
+ end: null,
382
+ instant: matchingInstantEvents,
383
+ });
384
+ }
385
+ }
386
+
387
+ return matches;
332
388
  }
333
389
 
334
- function getSyntheticId(event: Types.Events.PairableAsync): string|undefined {
390
+ export function getSyntheticId(event: Types.Events.PairableAsync): string|undefined {
335
391
  const id = extractId(event);
336
392
  return id && `${event.cat}:${id}:${event.name}`;
337
393
  }
338
394
 
339
- export function createSortedSyntheticEvents<T extends Types.Events.PairableAsync>(
340
- matchedPairs: Map<string, {
341
- begin: Types.Events.PairableAsyncBegin | null,
342
- end: Types.Events.PairableAsyncEnd | null,
343
- instant?: Types.Events.PairableAsyncInstant[],
344
- }>,
345
- syntheticEventCallback?: (syntheticEvent: Types.Events.SyntheticEventPair<T>) => void,
395
+ function createSortedSyntheticEvents<T extends Types.Events.PairableAsync>(
396
+ matchedPairs: MatchingPairableAsyncEvents[],
346
397
  ): Array<Types.Events.SyntheticEventPair<T>> {
347
398
  const syntheticEvents: Array<Types.Events.SyntheticEventPair<T>> = [];
348
- for (const [id, eventsTriplet] of matchedPairs.entries()) {
399
+ for (const eventsTriplet of matchedPairs) {
400
+ const id = eventsTriplet.syntheticId;
349
401
  const beginEvent = eventsTriplet.begin;
350
402
  const endEvent = eventsTriplet.end;
351
403
  const instantEvents = eventsTriplet.instant;
@@ -399,17 +451,21 @@ export function createSortedSyntheticEvents<T extends Types.Events.PairableAsync
399
451
  // crbug.com/1472375
400
452
  continue;
401
453
  }
402
- syntheticEventCallback?.(event);
403
454
  syntheticEvents.push(event);
404
455
  }
405
- return syntheticEvents.sort((a, b) => a.ts - b.ts);
456
+ sortTraceEventsInPlace(syntheticEvents);
457
+ return syntheticEvents;
406
458
  }
407
459
 
408
- export function createMatchedSortedSyntheticEvents<T extends Types.Events.PairableAsync>(
409
- unpairedAsyncEvents: T[], syntheticEventCallback?: (syntheticEvent: Types.Events.SyntheticEventPair<T>) => void):
460
+ /**
461
+ * Groups up sets of async events into synthetic events.
462
+ * @param unpairedAsyncEvents the raw array of begin, end and async instant
463
+ * events. These MUST be sorted in timestamp ASC order.
464
+ */
465
+ export function createMatchedSortedSyntheticEvents<T extends Types.Events.PairableAsync>(unpairedAsyncEvents: T[]):
410
466
  Array<Types.Events.SyntheticEventPair<T>> {
411
467
  const matchedPairs = matchEvents(unpairedAsyncEvents);
412
- const syntheticEvents = createSortedSyntheticEvents<T>(matchedPairs, syntheticEventCallback);
468
+ const syntheticEvents = createSortedSyntheticEvents<T>(matchedPairs);
413
469
  return syntheticEvents;
414
470
  }
415
471
 
@@ -1358,6 +1358,15 @@ export interface PairableAsyncInstant extends PairableAsync {
1358
1358
  export interface PairableAsyncEnd extends PairableAsync {
1359
1359
  ph: Phase.ASYNC_NESTABLE_END;
1360
1360
  }
1361
+ export function isPairableAsyncBegin(e: Event): e is PairableAsyncBegin {
1362
+ return e.ph === Phase.ASYNC_NESTABLE_START;
1363
+ }
1364
+ export function isPairableAsyncEnd(e: Event): e is PairableAsyncEnd {
1365
+ return e.ph === Phase.ASYNC_NESTABLE_END;
1366
+ }
1367
+ export function isPairableAsyncInstant(e: Event): e is PairableAsyncInstant {
1368
+ return e.ph === Phase.ASYNC_NESTABLE_INSTANT;
1369
+ }
1361
1370
 
1362
1371
  export interface AnimationFrame extends PairableAsync {
1363
1372
  name: Name.ANIMATION_FRAME;