Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Kateryna Prokopenko | 6fa122c | 2022-05-06 08:54:47 | [diff] [blame] | 31 | import * as Platform from '../../core/platform/platform.js'; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 32 | import type * as PublicAPI from '../../../extension-api/ExtensionAPI'; // eslint-disable-line rulesdir/es_modules_import |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 33 | import type * as HAR from '../har/har.js'; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 34 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 35 | /* eslint-disable @typescript-eslint/naming-convention,@typescript-eslint/no-non-null-assertion */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 36 | export namespace PrivateAPI { |
| 37 | export namespace Panels { |
| 38 | export const enum SearchAction { |
| 39 | CancelSearch = 'cancelSearch', |
| 40 | PerformSearch = 'performSearch', |
| 41 | NextSearchResult = 'nextSearchResult', |
| 42 | PreviousSearchResult = 'previousSearchResult', |
| 43 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 44 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 45 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 46 | export const enum Events { |
| 47 | ButtonClicked = 'button-clicked-', |
| 48 | PanelObjectSelected = 'panel-objectSelected-', |
| 49 | InspectedURLChanged = 'inspected-url-changed', |
| 50 | NetworkRequestFinished = 'network-request-finished', |
| 51 | OpenResource = 'open-resource', |
| 52 | PanelSearch = 'panel-search-', |
| 53 | RecordingStarted = 'trace-recording-started-', |
| 54 | RecordingStopped = 'trace-recording-stopped-', |
| 55 | ResourceAdded = 'resource-added', |
| 56 | ResourceContentCommitted = 'resource-content-committed', |
| 57 | ViewShown = 'view-shown-', |
| 58 | ViewHidden = 'view-hidden,', |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 59 | ThemeChange = 'host-theme-change', |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 60 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 61 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 62 | export const enum Commands { |
| 63 | AddRequestHeaders = 'addRequestHeaders', |
| 64 | AddTraceProvider = 'addTraceProvider', |
| 65 | ApplyStyleSheet = 'applyStyleSheet', |
| 66 | CompleteTraceSession = 'completeTra.eSession', |
| 67 | CreatePanel = 'createPanel', |
| 68 | CreateSidebarPane = 'createSidebarPane', |
| 69 | CreateToolbarButton = 'createToolbarButton', |
| 70 | EvaluateOnInspectedPage = 'evaluateOnInspectedPage', |
| 71 | ForwardKeyboardEvent = '_forwardKeyboardEvent', |
| 72 | GetHAR = 'getHAR', |
| 73 | GetPageResources = 'getPageResources', |
| 74 | GetRequestContent = 'getRequestContent', |
| 75 | GetResourceContent = 'getResourceContent', |
| 76 | OpenResource = 'openResource', |
| 77 | Reload = 'Reload', |
| 78 | Subscribe = 'subscribe', |
| 79 | SetOpenResourceHandler = 'setOpenResourceHandler', |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 80 | SetThemeChangeHandler = 'setThemeChangeHandler', |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 81 | SetResourceContent = 'setResourceContent', |
| 82 | SetSidebarContent = 'setSidebarContent', |
| 83 | SetSidebarHeight = 'setSidebarHeight', |
| 84 | SetSidebarPage = 'setSidebarPage', |
| 85 | ShowPanel = 'showPanel', |
| 86 | Unsubscribe = 'unsubscribe', |
| 87 | UpdateButton = 'updateButton', |
| 88 | RegisterLanguageExtensionPlugin = 'registerLanguageExtensionPlugin', |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 89 | GetWasmLinearMemory = 'getWasmLinearMemory', |
| 90 | GetWasmLocal = 'getWasmLocal', |
| 91 | GetWasmGlobal = 'getWasmGlobal', |
| 92 | GetWasmOp = 'getWasmOp', |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 93 | RegisterRecorderExtensionPlugin = 'registerRecorderExtensionPlugin', |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 94 | CreateRecorderView = 'createRecorderView', |
| 95 | ShowRecorderView = 'showRecorderView', |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 96 | } |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 97 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 98 | export const enum LanguageExtensionPluginCommands { |
| 99 | AddRawModule = 'addRawModule', |
| 100 | RemoveRawModule = 'removeRawModule', |
| 101 | SourceLocationToRawLocation = 'sourceLocationToRawLocation', |
| 102 | RawLocationToSourceLocation = 'rawLocationToSourceLocation', |
| 103 | GetScopeInfo = 'getScopeInfo', |
| 104 | ListVariablesInScope = 'listVariablesInScope', |
| 105 | GetTypeInfo = 'getTypeInfo', |
| 106 | GetFormatter = 'getFormatter', |
| 107 | GetInspectableAddress = 'getInspectableAddress', |
| 108 | GetFunctionInfo = 'getFunctionInfo', |
| 109 | GetInlinedFunctionRanges = 'getInlinedFunctionRanges', |
| 110 | GetInlinedCalleesRanges = 'getInlinedCalleesRanges', |
| 111 | GetMappedLines = 'getMappedLines', |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 112 | FormatValue = 'formatValue', |
| 113 | GetProperties = 'getProperties', |
| 114 | ReleaseObject = 'releaseObject', |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 115 | } |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 116 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 117 | export const enum LanguageExtensionPluginEvents { |
| 118 | UnregisteredLanguageExtensionPlugin = 'unregisteredLanguageExtensionPlugin', |
| 119 | } |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 120 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 121 | export const enum RecorderExtensionPluginCommands { |
| 122 | Stringify = 'stringify', |
Alex Rudenko | 2aa04c0 | 2022-06-01 13:05:20 | [diff] [blame] | 123 | StringifyStep = 'stringifyStep', |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 124 | Replay = 'replay', |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | export const enum RecorderExtensionPluginEvents { |
| 128 | UnregisteredRecorderExtensionPlugin = 'unregisteredRecorderExtensionPlugin', |
| 129 | } |
| 130 | |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 131 | export interface EvaluateOptions { |
| 132 | frameURL?: string; |
| 133 | useContentScriptContext?: boolean; |
| 134 | scriptExecutionContext?: string; |
| 135 | } |
| 136 | |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 137 | type RegisterLanguageExtensionPluginRequest = { |
| 138 | command: Commands.RegisterLanguageExtensionPlugin, |
| 139 | pluginName: string, |
| 140 | port: MessagePort, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 141 | supportedScriptTypes: PublicAPI.Chrome.DevTools.SupportedScriptTypes, |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 142 | }; |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 143 | export type RecordingExtensionPluginCapability = 'export'|'replay'; |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 144 | type RegisterRecorderExtensionPluginRequest = { |
| 145 | command: Commands.RegisterRecorderExtensionPlugin, |
| 146 | pluginName: string, |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 147 | mediaType?: string, capabilities: RecordingExtensionPluginCapability[], port: MessagePort, |
| 148 | }; |
| 149 | type CreateRecorderViewRequest = { |
| 150 | command: Commands.CreateRecorderView, |
| 151 | id: string, |
| 152 | title: string, |
| 153 | pagePath: string, |
| 154 | }; |
| 155 | type ShowRecorderViewRequest = { |
| 156 | command: Commands.ShowRecorderView, |
| 157 | id: string, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 158 | }; |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 159 | type SubscribeRequest = {command: Commands.Subscribe, type: string}; |
| 160 | type UnsubscribeRequest = {command: Commands.Unsubscribe, type: string}; |
| 161 | type AddRequestHeadersRequest = { |
| 162 | command: Commands.AddRequestHeaders, |
| 163 | extensionId: string, |
| 164 | headers: {[key: string]: string}, |
| 165 | }; |
| 166 | type ApplyStyleSheetRequest = {command: Commands.ApplyStyleSheet, styleSheet: string}; |
| 167 | type CreatePanelRequest = {command: Commands.CreatePanel, id: string, title: string, page: string}; |
| 168 | type ShowPanelRequest = {command: Commands.ShowPanel, id: string}; |
| 169 | type CreateToolbarButtonRequest = { |
| 170 | command: Commands.CreateToolbarButton, |
| 171 | id: string, |
| 172 | icon: string, |
| 173 | panel: string, |
| 174 | tooltip?: string, |
| 175 | disabled?: boolean, |
| 176 | }; |
| 177 | type UpdateButtonRequest = |
| 178 | {command: Commands.UpdateButton, id: string, icon?: string, tooltip?: string, disabled?: boolean}; |
| 179 | type CompleteTraceSessionRequest = |
Kateryna Prokopenko | 6fa122c | 2022-05-06 08:54:47 | [diff] [blame] | 180 | {command: Commands.CompleteTraceSession, id: string, url: Platform.DevToolsPath.UrlString, timeOffset: number}; |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 181 | type CreateSidebarPaneRequest = {command: Commands.CreateSidebarPane, id: string, panel: string, title: string}; |
| 182 | type SetSidebarHeightRequest = {command: Commands.SetSidebarHeight, id: string, height: string}; |
| 183 | type SetSidebarContentRequest = { |
| 184 | command: Commands.SetSidebarContent, |
| 185 | id: string, |
| 186 | evaluateOnPage?: boolean, expression: string, |
| 187 | rootTitle?: string, |
| 188 | evaluateOptions?: EvaluateOptions, |
| 189 | }; |
| 190 | type SetSidebarPageRequest = {command: Commands.SetSidebarPage, id: string, page: string}; |
Kateryna Prokopenko | 9964ab1 | 2022-03-23 16:41:49 | [diff] [blame] | 191 | type OpenResourceRequest = |
| 192 | {command: Commands.OpenResource, url: Platform.DevToolsPath.UrlString, lineNumber: number, columnNumber: number}; |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 193 | type SetOpenResourceHandlerRequest = {command: Commands.SetOpenResourceHandler, handlerPresent: boolean}; |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 194 | type SetThemeChangeHandlerRequest = {command: Commands.SetThemeChangeHandler, handlerPresent: boolean}; |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 195 | type ReloadRequest = { |
| 196 | command: Commands.Reload, |
| 197 | options: null|{ |
| 198 | userAgent?: string, |
| 199 | injectedScript?: string, |
| 200 | ignoreCache?: boolean, |
| 201 | }, |
| 202 | }; |
| 203 | type EvaluateOnInspectedPageRequest = { |
| 204 | command: Commands.EvaluateOnInspectedPage, |
| 205 | expression: string, |
| 206 | evaluateOptions?: EvaluateOptions, |
| 207 | }; |
| 208 | type GetRequestContentRequest = {command: Commands.GetRequestContent, id: number}; |
| 209 | type GetResourceContentRequest = {command: Commands.GetResourceContent, url: string}; |
| 210 | type SetResourceContentRequest = |
| 211 | {command: Commands.SetResourceContent, url: string, content: string, commit: boolean}; |
| 212 | type AddTraceProviderRequest = |
| 213 | {command: Commands.AddTraceProvider, id: string, categoryName: string, categoryTooltip: string}; |
| 214 | type ForwardKeyboardEventRequest = { |
| 215 | command: Commands.ForwardKeyboardEvent, |
| 216 | entries: Array<KeyboardEventInit&{eventType: string}>, |
| 217 | }; |
| 218 | type GetHARRequest = {command: Commands.GetHAR}; |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 219 | type GetPageResourcesRequest = {command: Commands.GetPageResources}; |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 220 | type GetWasmLinearMemoryRequest = { |
| 221 | command: Commands.GetWasmLinearMemory, |
| 222 | offset: number, |
| 223 | length: number, |
| 224 | stopId: unknown, |
| 225 | }; |
| 226 | type GetWasmLocalRequest = { |
| 227 | command: Commands.GetWasmLocal, |
| 228 | local: number, |
| 229 | stopId: unknown, |
| 230 | }; |
| 231 | type GetWasmGlobalRequest = { |
| 232 | command: Commands.GetWasmGlobal, |
| 233 | global: number, |
| 234 | stopId: unknown, |
| 235 | }; |
| 236 | type GetWasmOpRequest = {command: Commands.GetWasmOp, op: number, stopId: unknown}; |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 237 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 238 | export type ServerRequests = ShowRecorderViewRequest|CreateRecorderViewRequest|RegisterRecorderExtensionPluginRequest| |
| 239 | RegisterLanguageExtensionPluginRequest|SubscribeRequest|UnsubscribeRequest|AddRequestHeadersRequest| |
| 240 | ApplyStyleSheetRequest|CreatePanelRequest|ShowPanelRequest|CreateToolbarButtonRequest|UpdateButtonRequest| |
| 241 | CompleteTraceSessionRequest|CreateSidebarPaneRequest|SetSidebarHeightRequest|SetSidebarContentRequest| |
| 242 | SetSidebarPageRequest|OpenResourceRequest|SetOpenResourceHandlerRequest|SetThemeChangeHandlerRequest| |
| 243 | ReloadRequest|EvaluateOnInspectedPageRequest|GetRequestContentRequest|GetResourceContentRequest| |
| 244 | SetResourceContentRequest|AddTraceProviderRequest|ForwardKeyboardEventRequest|GetHARRequest| |
| 245 | GetPageResourcesRequest|GetWasmLinearMemoryRequest|GetWasmLocalRequest|GetWasmGlobalRequest|GetWasmOpRequest; |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 246 | export type ExtensionServerRequestMessage = PrivateAPI.ServerRequests&{requestId?: number}; |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 247 | |
| 248 | type AddRawModuleRequest = { |
| 249 | method: LanguageExtensionPluginCommands.AddRawModule, |
| 250 | parameters: {rawModuleId: string, symbolsURL: string|undefined, rawModule: PublicAPI.Chrome.DevTools.RawModule}, |
| 251 | }; |
| 252 | type SourceLocationToRawLocationRequest = { |
| 253 | method: LanguageExtensionPluginCommands.SourceLocationToRawLocation, |
| 254 | parameters: {sourceLocation: PublicAPI.Chrome.DevTools.SourceLocation}, |
| 255 | }; |
| 256 | type RawLocationToSourceLocationRequest = { |
| 257 | method: LanguageExtensionPluginCommands.RawLocationToSourceLocation, |
| 258 | parameters: {rawLocation: PublicAPI.Chrome.DevTools.RawLocation}, |
| 259 | }; |
| 260 | type GetScopeInfoRequest = {method: LanguageExtensionPluginCommands.GetScopeInfo, parameters: {type: string}}; |
| 261 | type ListVariablesInScopeRequest = { |
| 262 | method: LanguageExtensionPluginCommands.ListVariablesInScope, |
| 263 | parameters: {rawLocation: PublicAPI.Chrome.DevTools.RawLocation}, |
| 264 | }; |
| 265 | type RemoveRawModuleRequest = { |
| 266 | method: LanguageExtensionPluginCommands.RemoveRawModule, |
| 267 | parameters: {rawModuleId: string}, |
| 268 | }; |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 269 | type GetFunctionInfoRequest = { |
| 270 | method: LanguageExtensionPluginCommands.GetFunctionInfo, |
| 271 | parameters: {rawLocation: PublicAPI.Chrome.DevTools.RawLocation}, |
| 272 | }; |
| 273 | type GetInlinedFunctionRangesRequest = { |
| 274 | method: LanguageExtensionPluginCommands.GetInlinedFunctionRanges, |
| 275 | parameters: {rawLocation: PublicAPI.Chrome.DevTools.RawLocation}, |
| 276 | }; |
| 277 | type GetInlinedCalleesRangesRequest = { |
| 278 | method: LanguageExtensionPluginCommands.GetInlinedCalleesRanges, |
| 279 | parameters: {rawLocation: PublicAPI.Chrome.DevTools.RawLocation}, |
| 280 | }; |
| 281 | type GetMappedLinesRequest = { |
| 282 | method: LanguageExtensionPluginCommands.GetMappedLines, |
| 283 | parameters: {rawModuleId: string, sourceFileURL: string}, |
| 284 | }; |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 285 | type FormatValueRequest = { |
| 286 | method: LanguageExtensionPluginCommands.FormatValue, |
| 287 | parameters: {expression: string, context: PublicAPI.Chrome.DevTools.RawLocation, stopId: number}, |
| 288 | }; |
| 289 | type GetPropertiesRequest = { |
| 290 | method: LanguageExtensionPluginCommands.GetProperties, |
| 291 | parameters: {objectId: PublicAPI.Chrome.DevTools.RemoteObjectId}, |
| 292 | }; |
| 293 | type ReleaseObjectRequest = { |
| 294 | method: LanguageExtensionPluginCommands.ReleaseObject, |
| 295 | parameters: {objectId: PublicAPI.Chrome.DevTools.RemoteObjectId}, |
| 296 | }; |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 297 | |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 298 | export type LanguageExtensionRequests = |
| 299 | AddRawModuleRequest|SourceLocationToRawLocationRequest|RawLocationToSourceLocationRequest|GetScopeInfoRequest| |
Philip Pfaffe | 8a3d59b | 2023-05-04 11:26:00 | [diff] [blame] | 300 | ListVariablesInScopeRequest|RemoveRawModuleRequest|GetFunctionInfoRequest|GetInlinedFunctionRangesRequest| |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 301 | GetInlinedCalleesRangesRequest|GetMappedLinesRequest|FormatValueRequest|GetPropertiesRequest|ReleaseObjectRequest; |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 302 | |
| 303 | type StringifyRequest = { |
| 304 | method: RecorderExtensionPluginCommands.Stringify, |
| 305 | parameters: {recording: Record<string, unknown>}, |
| 306 | }; |
| 307 | |
Alex Rudenko | 2aa04c0 | 2022-06-01 13:05:20 | [diff] [blame] | 308 | type StringifyStepRequest = { |
| 309 | method: RecorderExtensionPluginCommands.StringifyStep, |
| 310 | parameters: {step: Record<string, unknown>}, |
| 311 | }; |
| 312 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 313 | type ReplayRequest = { |
| 314 | method: RecorderExtensionPluginCommands.Replay, |
| 315 | parameters: {recording: Record<string, unknown>}, |
| 316 | }; |
| 317 | |
| 318 | export type RecorderExtensionRequests = StringifyRequest|StringifyStepRequest|ReplayRequest; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 319 | } |
| 320 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 321 | declare global { |
| 322 | interface Window { |
| 323 | injectedExtensionAPI: |
| 324 | (extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 325 | testHook: |
| 326 | (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 327 | injectedScriptId: number, targetWindow?: Window) => void; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 328 | buildExtensionAPIInjectedScript( |
| 329 | extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 330 | testHook: undefined|((extensionServer: unknown, extensionAPI: unknown) => unknown)): string; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 331 | chrome: PublicAPI.Chrome.DevTools.Chrome; |
| 332 | webInspector?: APIImpl.InspectorExtensionAPI; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | export type ExtensionDescriptor = { |
| 337 | startPage: string, |
| 338 | name: string, |
| 339 | exposeExperimentalAPIs: boolean, |
| 340 | exposeWebInspectorNamespace?: boolean, |
Danil Somsikov | 28cfaf3 | 2022-09-27 15:36:55 | [diff] [blame] | 341 | allowFileAccess?: boolean, |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 342 | }; |
| 343 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 344 | namespace APIImpl { |
| 345 | export interface InspectorExtensionAPI { |
| 346 | languageServices: PublicAPI.Chrome.DevTools.LanguageExtensions; |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 347 | recorder: PublicAPI.Chrome.DevTools.RecorderExtensions; |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 348 | timeline: Timeline; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 349 | network: PublicAPI.Chrome.DevTools.Network; |
| 350 | panels: PublicAPI.Chrome.DevTools.Panels; |
| 351 | inspectedWindow: PublicAPI.Chrome.DevTools.InspectedWindow; |
| 352 | } |
| 353 | |
| 354 | export interface ExtensionServerClient { |
| 355 | _callbacks: {[key: string]: (response: unknown) => unknown}; |
| 356 | _handlers: {[key: string]: (request: {arguments: unknown[]}) => unknown}; |
| 357 | _lastRequestId: number; |
| 358 | _lastObjectId: number; |
| 359 | _port: MessagePort; |
| 360 | |
| 361 | _onCallback(request: unknown): void; |
| 362 | _onMessage(event: MessageEvent<{command: string, requestId: number, arguments: unknown[]}>): void; |
| 363 | _registerCallback(callback: (response: unknown) => unknown): number; |
| 364 | registerHandler(command: string, handler: (request: {arguments: unknown[]}) => unknown): void; |
| 365 | unregisterHandler(command: string): void; |
| 366 | hasHandler(command: string): boolean; |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 367 | sendRequest<ResponseT>( |
| 368 | request: PrivateAPI.ServerRequests, callback?: ((response: ResponseT) => unknown), transfers?: unknown[]): void; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 369 | nextObjectId(): string; |
| 370 | } |
| 371 | |
| 372 | // We cannot use the stronger `unknown` type in place of `any` in the following type definition. The type is used as |
| 373 | // the right-hand side of `extends` in a few places, which doesn't narrow `unknown`. Without narrowing, overload |
| 374 | // resolution and meaningful type inference of arguments break, for example. |
Tim van der Lippe | 269e9ae | 2021-12-14 13:33:28 | [diff] [blame] | 375 | // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 376 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
Tim van der Lippe | 269e9ae | 2021-12-14 13:33:28 | [diff] [blame] | 377 | export type Callable = (...args: any) => void; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 378 | |
| 379 | export interface EventSink<ListenerT extends Callable> extends PublicAPI.Chrome.DevTools.EventSink<ListenerT> { |
| 380 | _type: string; |
| 381 | _listeners: ListenerT[]; |
| 382 | _customDispatch: undefined|((this: EventSink<ListenerT>, request: {arguments: unknown[]}) => unknown); |
| 383 | |
| 384 | _fire(..._vararg: Parameters<ListenerT>): void; |
| 385 | _dispatch(request: {arguments: unknown[]}): void; |
| 386 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 387 | |
| 388 | export interface Network extends PublicAPI.Chrome.DevTools.Network { |
| 389 | addRequestHeaders(headers: {[key: string]: string}): void; |
| 390 | } |
| 391 | |
| 392 | export interface Request extends PublicAPI.Chrome.DevTools.Request, HAR.Log.EntryDTO { |
| 393 | _id: number; |
| 394 | } |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 395 | |
| 396 | export interface Panels extends PublicAPI.Chrome.DevTools.Panels { |
| 397 | get SearchAction(): {[key: string]: string}; |
| 398 | applyStyleSheet(styleSheet: string): void; |
| 399 | setOpenResourceHandler(callback?: (resource: PublicAPI.Chrome.DevTools.Resource, lineNumber: number) => unknown): |
| 400 | void; |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 401 | setThemeChangeHandler(callback?: (themeName: string) => unknown): void; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | export interface ExtensionView extends PublicAPI.Chrome.DevTools.ExtensionView { |
| 405 | _id: string|null; |
| 406 | } |
| 407 | |
| 408 | export interface ExtensionSidebarPane extends ExtensionView, PublicAPI.Chrome.DevTools.ExtensionSidebarPane { |
| 409 | setExpression( |
| 410 | expression: string, rootTitle?: string, evaluteOptions?: PrivateAPI.EvaluateOptions, |
| 411 | callback?: () => unknown): void; |
| 412 | } |
| 413 | |
| 414 | export interface PanelWithSidebar extends ExtensionView, PublicAPI.Chrome.DevTools.PanelWithSidebar { |
| 415 | _hostPanelName: string; |
| 416 | } |
| 417 | |
| 418 | export interface LanguageExtensions extends PublicAPI.Chrome.DevTools.LanguageExtensions { |
| 419 | _plugins: Map<PublicAPI.Chrome.DevTools.LanguageExtensionPlugin, MessagePort>; |
| 420 | } |
| 421 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 422 | export interface RecorderExtensions extends PublicAPI.Chrome.DevTools.RecorderExtensions { |
| 423 | _plugins: Map<PublicAPI.Chrome.DevTools.RecorderExtensionPlugin, MessagePort>; |
| 424 | } |
| 425 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 426 | export interface ExtensionPanel extends ExtensionView, PublicAPI.Chrome.DevTools.ExtensionPanel { |
| 427 | show(): void; |
| 428 | } |
| 429 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 430 | export interface RecorderView extends ExtensionView, PublicAPI.Chrome.DevTools.RecorderView {} |
| 431 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 432 | export interface Button extends PublicAPI.Chrome.DevTools.Button { |
| 433 | _id: string; |
| 434 | } |
| 435 | |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 436 | export interface TraceSession { |
| 437 | _id: string; |
| 438 | |
| 439 | complete(url?: string, timeOffset?: number): void; |
| 440 | } |
| 441 | |
| 442 | export interface TraceProvider { |
| 443 | onRecordingStarted: EventSink<(session: TraceSession) => unknown>; |
| 444 | onRecordingStopped: EventSink<() => unknown>; |
| 445 | } |
| 446 | |
| 447 | export interface Timeline { |
| 448 | addTraceProvider(categoryName: string, categoryTooltip: string): TraceProvider; |
| 449 | } |
| 450 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 451 | export type ResourceData = {url: string, type: string}; |
| 452 | export interface Resource extends PublicAPI.Chrome.DevTools.Resource { |
| 453 | _type: string; |
| 454 | _url: string; |
| 455 | |
| 456 | get type(): string; |
| 457 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 458 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 459 | |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 460 | self.injectedExtensionAPI = function( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 461 | extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
| 462 | testHook: (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 463 | injectedScriptId: number, targetWindowForTest?: Window): void { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 464 | const keysToForwardSet = new Set<number>(keysToForward); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 465 | const chrome = window.chrome || {}; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 466 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 467 | const devtools_descriptor = Object.getOwnPropertyDescriptor(chrome, 'devtools'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 468 | if (devtools_descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 469 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 470 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 471 | let userAction = false; |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 472 | let userRecorderAction = false; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 473 | |
| 474 | // Here and below, all constructors are private to API implementation. |
| 475 | // For a public type Foo, if internal fields are present, these are on |
| 476 | // a private FooImpl type, an instance of FooImpl is used in a closure |
| 477 | // by Foo consutrctor to re-bind publicly exported members to an instance |
| 478 | // of Foo. |
| 479 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 480 | function EventSinkImpl<ListenerT extends APIImpl.Callable>( |
| 481 | this: APIImpl.EventSink<ListenerT>, type: string, |
| 482 | customDispatch?: (this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 483 | this._type = type; |
| 484 | this._listeners = []; |
| 485 | this._customDispatch = customDispatch; |
| 486 | } |
| 487 | |
| 488 | EventSinkImpl.prototype = { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 489 | addListener: function<ListenerT extends APIImpl.Callable>(this: APIImpl.EventSink<ListenerT>, callback: ListenerT): |
| 490 | void { |
| 491 | if (typeof callback !== 'function') { |
| 492 | throw 'addListener: callback is not a function'; |
| 493 | } |
| 494 | if (this._listeners.length === 0) { |
| 495 | extensionServer.sendRequest({command: PrivateAPI.Commands.Subscribe, type: this._type}); |
| 496 | } |
| 497 | this._listeners.push(callback); |
| 498 | extensionServer.registerHandler('notify-' + this._type, this._dispatch.bind(this)); |
| 499 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 500 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 501 | removeListener: function<ListenerT extends APIImpl.Callable>( |
| 502 | this: APIImpl.EventSink<ListenerT>, callback: ListenerT): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 503 | const listeners = this._listeners; |
| 504 | |
| 505 | for (let i = 0; i < listeners.length; ++i) { |
| 506 | if (listeners[i] === callback) { |
| 507 | listeners.splice(i, 1); |
| 508 | break; |
| 509 | } |
| 510 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 511 | if (this._listeners.length === 0) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 512 | extensionServer.sendRequest({command: PrivateAPI.Commands.Unsubscribe, type: this._type}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 513 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 514 | }, |
| 515 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 516 | _fire: function<ListenerT extends APIImpl.Callable>( |
| 517 | this: APIImpl.EventSink<ListenerT>, ..._vararg: Parameters<ListenerT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 518 | const listeners = this._listeners.slice(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 519 | for (let i = 0; i < listeners.length; ++i) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 520 | listeners[i].apply(null, Array.from(arguments)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 521 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 522 | }, |
| 523 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 524 | _dispatch: function<ListenerT extends APIImpl.Callable>( |
| 525 | this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 526 | if (this._customDispatch) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 527 | this._customDispatch.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 528 | } else { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 529 | this._fire.apply(this, request.arguments as Parameters<ListenerT>); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 530 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 531 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 532 | }; |
| 533 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 534 | function Constructor<NewT extends APIImpl.Callable>(ctor: NewT): new (...args: Parameters<NewT>) => |
| 535 | ThisParameterType<NewT> { |
| 536 | return ctor as unknown as new (...args: Parameters<NewT>) => ThisParameterType<NewT>; |
| 537 | } |
| 538 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 539 | function InspectorExtensionAPI(this: APIImpl.InspectorExtensionAPI): void { |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 540 | this.inspectedWindow = new (Constructor(InspectedWindow))(); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 541 | this.panels = new (Constructor(Panels))(); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 542 | this.network = new (Constructor(Network))(); |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 543 | this.timeline = new (Constructor(Timeline))(); |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 544 | this.languageServices = new (Constructor(LanguageServicesAPI))(); |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 545 | this.recorder = new (Constructor(RecorderServicesAPI))(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 546 | defineDeprecatedProperty(this, 'webInspector', 'resources', 'network'); |
| 547 | } |
| 548 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 549 | function Network(this: APIImpl.Network): void { |
| 550 | function dispatchRequestEvent( |
| 551 | this: APIImpl.EventSink<(request: PublicAPI.Chrome.DevTools.Request) => unknown>, |
| 552 | message: {arguments: unknown[]}): void { |
| 553 | const request = message.arguments[1] as APIImpl.Request & {__proto__: APIImpl.Request}; |
| 554 | |
| 555 | request.__proto__ = new (Constructor(Request))(message.arguments[0] as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 556 | this._fire(request); |
| 557 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 558 | |
| 559 | this.onRequestFinished = |
| 560 | new (Constructor(EventSink))(PrivateAPI.Events.NetworkRequestFinished, dispatchRequestEvent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 561 | defineDeprecatedProperty(this, 'network', 'onFinished', 'onRequestFinished'); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 562 | |
| 563 | this.onNavigated = new (Constructor(EventSink))(PrivateAPI.Events.InspectedURLChanged); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 564 | } |
| 565 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 566 | (Network.prototype as Pick<APIImpl.Network, 'getHAR'|'addRequestHeaders'>) = { |
| 567 | getHAR: function(this: PublicAPI.Chrome.DevTools.Network, callback?: (harLog: Object) => unknown): void { |
| 568 | function callbackWrapper(response: unknown): void { |
| 569 | const result = |
| 570 | response as ({entries: Array<HAR.Log.EntryDTO&{__proto__?: APIImpl.Request, _requestId?: number}>}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 571 | const entries = (result && result.entries) || []; |
| 572 | for (let i = 0; i < entries.length; ++i) { |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 573 | entries[i].__proto__ = new (Constructor(Request))(entries[i]._requestId as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 574 | delete entries[i]._requestId; |
| 575 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 576 | callback && callback(result as Object); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 577 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 578 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetHAR}, callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 579 | }, |
| 580 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 581 | addRequestHeaders: function(headers: {[key: string]: string}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 582 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 583 | {command: PrivateAPI.Commands.AddRequestHeaders, headers: headers, extensionId: window.location.hostname}); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 584 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 585 | }; |
| 586 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 587 | function RequestImpl(this: APIImpl.Request, id: number): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 588 | this._id = id; |
| 589 | } |
| 590 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 591 | (RequestImpl.prototype as Pick<APIImpl.Request, 'getContent'>) = { |
| 592 | getContent: function(this: APIImpl.Request, callback?: (content: string, encoding: string) => unknown): void { |
| 593 | function callbackWrapper(response: unknown): void { |
| 594 | const {content, encoding} = response as {content: string, encoding: string}; |
| 595 | callback && callback(content, encoding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 596 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 597 | extensionServer.sendRequest( |
| 598 | {command: PrivateAPI.Commands.GetRequestContent, id: this._id}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 599 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 600 | }; |
| 601 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 602 | function Panels(this: APIImpl.Panels): void { |
| 603 | const panels: {[key: string]: ElementsPanel|SourcesPanel} = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 604 | elements: new ElementsPanel(), |
| 605 | sources: new SourcesPanel(), |
| 606 | }; |
| 607 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 608 | function panelGetter(name: string): ElementsPanel|SourcesPanel { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 609 | return panels[name]; |
| 610 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 611 | for (const panel in panels) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 612 | Object.defineProperty(this, panel, {get: panelGetter.bind(null, panel), enumerable: true}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 613 | } |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 614 | this.applyStyleSheet = function(styleSheet: string): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 615 | extensionServer.sendRequest({command: PrivateAPI.Commands.ApplyStyleSheet, styleSheet: styleSheet}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 616 | }; |
| 617 | } |
| 618 | |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 619 | (Panels.prototype as |
| 620 | Pick<APIImpl.Panels, 'create'|'setOpenResourceHandler'|'openResource'|'SearchAction'|'setThemeChangeHandler'>) = { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 621 | create: function( |
| 622 | title: string, icon: string, page: string, |
| 623 | callback: (panel: PublicAPI.Chrome.DevTools.ExtensionPanel) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 624 | const id = 'extension-panel-' + extensionServer.nextObjectId(); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 625 | extensionServer.sendRequest( |
| 626 | {command: PrivateAPI.Commands.CreatePanel, id, title, page}, |
Philip Pfaffe | 3abccb4 | 2021-09-14 09:18:37 | [diff] [blame] | 627 | callback && ((): unknown => callback.call(this, new (Constructor(ExtensionPanel))(id)))); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 628 | }, |
| 629 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 630 | setOpenResourceHandler: function( |
| 631 | callback: (resource: PublicAPI.Chrome.DevTools.Resource, lineNumber: number) => unknown): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 632 | const hadHandler = extensionServer.hasHandler(PrivateAPI.Events.OpenResource); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 633 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 634 | function callbackWrapper(message: unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 635 | // Allow the panel to show itself when handling the event. |
| 636 | userAction = true; |
| 637 | try { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 638 | const {resource, lineNumber} = message as {resource: APIImpl.ResourceData, lineNumber: number}; |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 639 | if (canAccessResource(resource)) { |
| 640 | callback.call(null, new (Constructor(Resource))(resource), lineNumber); |
| 641 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 642 | } finally { |
| 643 | userAction = false; |
| 644 | } |
| 645 | } |
| 646 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 647 | if (!callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 648 | extensionServer.unregisterHandler(PrivateAPI.Events.OpenResource); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 649 | } else { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 650 | extensionServer.registerHandler(PrivateAPI.Events.OpenResource, callbackWrapper); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 651 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 652 | |
| 653 | // Only send command if we either removed an existing handler or added handler and had none before. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 654 | if (hadHandler === !callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 655 | extensionServer.sendRequest( |
| 656 | {command: PrivateAPI.Commands.SetOpenResourceHandler, 'handlerPresent': Boolean(callback)}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 657 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 658 | }, |
| 659 | |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 660 | setThemeChangeHandler: function(callback: (themeName: string) => unknown): void { |
| 661 | const hadHandler = extensionServer.hasHandler(PrivateAPI.Events.ThemeChange); |
| 662 | |
| 663 | function callbackWrapper(message: unknown): void { |
| 664 | const {themeName} = message as {themeName: string}; |
| 665 | chrome.devtools.panels.themeName = themeName; |
| 666 | callback.call(null, themeName); |
| 667 | } |
| 668 | |
| 669 | if (!callback) { |
| 670 | extensionServer.unregisterHandler(PrivateAPI.Events.ThemeChange); |
| 671 | } else { |
| 672 | extensionServer.registerHandler(PrivateAPI.Events.ThemeChange, callbackWrapper); |
| 673 | } |
| 674 | |
| 675 | // Only send command if we either removed an existing handler or added handler and had none before. |
| 676 | if (hadHandler === !callback) { |
| 677 | extensionServer.sendRequest( |
| 678 | {command: PrivateAPI.Commands.SetThemeChangeHandler, 'handlerPresent': Boolean(callback)}); |
| 679 | } |
| 680 | }, |
| 681 | |
Philip Pfaffe | 140e543 | 2021-09-13 16:34:23 | [diff] [blame] | 682 | openResource: function( |
Kateryna Prokopenko | 9964ab1 | 2022-03-23 16:41:49 | [diff] [blame] | 683 | url: Platform.DevToolsPath.UrlString, lineNumber: number, columnNumber?: number, |
| 684 | _callback?: (response: unknown) => unknown): void { |
Philip Pfaffe | 140e543 | 2021-09-13 16:34:23 | [diff] [blame] | 685 | const callbackArg = extractCallbackArgument(arguments); |
| 686 | // Handle older API: |
| 687 | const columnNumberArg = typeof columnNumber === 'number' ? columnNumber : 0; |
| 688 | extensionServer.sendRequest( |
| 689 | {command: PrivateAPI.Commands.OpenResource, url, lineNumber, columnNumber: columnNumberArg}, callbackArg); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 690 | }, |
| 691 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 692 | get SearchAction(): {[key: string]: string} { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 693 | return { |
| 694 | CancelSearch: PrivateAPI.Panels.SearchAction.CancelSearch, |
| 695 | PerformSearch: PrivateAPI.Panels.SearchAction.PerformSearch, |
| 696 | NextSearchResult: PrivateAPI.Panels.SearchAction.NextSearchResult, |
| 697 | PreviousSearchResult: PrivateAPI.Panels.SearchAction.PreviousSearchResult, |
| 698 | }; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 699 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 700 | }; |
| 701 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 702 | function ExtensionViewImpl(this: APIImpl.ExtensionView, id: string|null): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 703 | this._id = id; |
| 704 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 705 | function dispatchShowEvent( |
| 706 | this: APIImpl.EventSink<(window?: Window) => unknown>, message: {arguments: unknown[]}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 707 | const frameIndex = message.arguments[0]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 708 | if (typeof frameIndex === 'number') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 709 | this._fire(window.parent.frames[frameIndex]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 710 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 711 | this._fire(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 712 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | if (id) { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 716 | this.onShown = new (Constructor(EventSink))(PrivateAPI.Events.ViewShown + id, dispatchShowEvent); |
| 717 | |
| 718 | this.onHidden = new (Constructor(EventSink))(PrivateAPI.Events.ViewHidden + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 722 | function PanelWithSidebarImpl(this: APIImpl.PanelWithSidebar, hostPanelName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 723 | ExtensionViewImpl.call(this, null); |
| 724 | this._hostPanelName = hostPanelName; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 725 | |
| 726 | this.onSelectionChanged = new (Constructor(EventSink))(PrivateAPI.Events.PanelObjectSelected + hostPanelName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 727 | } |
| 728 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 729 | (PanelWithSidebarImpl.prototype as Pick<APIImpl.PanelWithSidebar, 'createSidebarPane'>& |
| 730 | {__proto__: APIImpl.ExtensionView}) = { |
| 731 | createSidebarPane: function( |
| 732 | this: APIImpl.PanelWithSidebar, title: string, |
| 733 | callback?: (pane: PublicAPI.Chrome.DevTools.ExtensionSidebarPane) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 734 | const id = 'extension-sidebar-' + extensionServer.nextObjectId(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 735 | function callbackWrapper(): void { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 736 | callback && callback(new (Constructor(ExtensionSidebarPane))(id)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 737 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 738 | extensionServer.sendRequest( |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 739 | {command: PrivateAPI.Commands.CreateSidebarPane, panel: this._hostPanelName, id, title}, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 740 | callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 741 | }, |
| 742 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 743 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 744 | }; |
| 745 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 746 | function RecorderServicesAPIImpl(this: APIImpl.RecorderExtensions): void { |
| 747 | this._plugins = new Map(); |
| 748 | } |
| 749 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 750 | async function registerRecorderExtensionPluginImpl( |
| 751 | this: APIImpl.RecorderExtensions, plugin: PublicAPI.Chrome.DevTools.RecorderExtensionPlugin, pluginName: string, |
| 752 | mediaType?: string): Promise<void> { |
| 753 | if (this._plugins.has(plugin)) { |
| 754 | throw new Error(`Tried to register plugin '${pluginName}' twice`); |
| 755 | } |
| 756 | const channel = new MessageChannel(); |
| 757 | const port = channel.port1; |
| 758 | this._plugins.set(plugin, port); |
| 759 | port.onmessage = ({data}: MessageEvent<{requestId: number}&PrivateAPI.RecorderExtensionRequests>): void => { |
| 760 | const {requestId} = data; |
| 761 | dispatchMethodCall(data) |
| 762 | .then(result => port.postMessage({requestId, result})) |
| 763 | .catch(error => port.postMessage({requestId, error: {message: error.message}})); |
| 764 | }; |
| 765 | |
| 766 | async function dispatchMethodCall(request: PrivateAPI.RecorderExtensionRequests): Promise<unknown> { |
| 767 | switch (request.method) { |
| 768 | case PrivateAPI.RecorderExtensionPluginCommands.Stringify: |
| 769 | return (plugin as PublicAPI.Chrome.DevTools.RecorderExtensionExportPlugin) |
| 770 | .stringify(request.parameters.recording); |
| 771 | case PrivateAPI.RecorderExtensionPluginCommands.StringifyStep: |
| 772 | return (plugin as PublicAPI.Chrome.DevTools.RecorderExtensionExportPlugin) |
| 773 | .stringifyStep(request.parameters.step); |
| 774 | case PrivateAPI.RecorderExtensionPluginCommands.Replay: |
| 775 | try { |
| 776 | userAction = true; |
| 777 | userRecorderAction = true; |
| 778 | return (plugin as PublicAPI.Chrome.DevTools.RecorderExtensionReplayPlugin) |
| 779 | .replay(request.parameters.recording); |
| 780 | } finally { |
| 781 | userAction = false; |
| 782 | userRecorderAction = false; |
| 783 | } |
| 784 | default: |
| 785 | // @ts-expect-error |
| 786 | throw new Error(`'${request.method}' is not recognized`); |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 787 | } |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 788 | } |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 789 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 790 | const capabilities: PrivateAPI.RecordingExtensionPluginCapability[] = []; |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 791 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 792 | if ('stringify' in plugin && 'stringifyStep' in plugin) { |
| 793 | capabilities.push('export'); |
| 794 | } |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 795 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 796 | if ('replay' in plugin) { |
| 797 | capabilities.push('replay'); |
| 798 | } |
| 799 | |
| 800 | await new Promise<void>(resolve => { |
| 801 | extensionServer.sendRequest( |
| 802 | { |
| 803 | command: PrivateAPI.Commands.RegisterRecorderExtensionPlugin, |
| 804 | pluginName, |
| 805 | mediaType, |
| 806 | capabilities, |
| 807 | port: channel.port2, |
| 808 | }, |
| 809 | () => resolve(), [channel.port2]); |
| 810 | }); |
| 811 | } |
| 812 | |
| 813 | (RecorderServicesAPIImpl.prototype as Pick< |
| 814 | APIImpl.RecorderExtensions, |
| 815 | 'registerRecorderExtensionPlugin'|'unregisterRecorderExtensionPlugin'|'createView'>) = { |
| 816 | registerRecorderExtensionPlugin: registerRecorderExtensionPluginImpl, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 817 | unregisterRecorderExtensionPlugin: async function( |
| 818 | this: APIImpl.RecorderExtensions, plugin: PublicAPI.Chrome.DevTools.RecorderExtensionPlugin): Promise<void> { |
| 819 | const port = this._plugins.get(plugin); |
| 820 | if (!port) { |
| 821 | throw new Error('Tried to unregister a plugin that was not previously registered'); |
| 822 | } |
| 823 | this._plugins.delete(plugin); |
| 824 | port.postMessage({event: PrivateAPI.RecorderExtensionPluginEvents.UnregisteredRecorderExtensionPlugin}); |
| 825 | port.close(); |
| 826 | }, |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 827 | createView: async function(this: APIImpl.RecorderExtensions, title: string, pagePath: string): |
| 828 | Promise<PublicAPI.Chrome.DevTools.RecorderView> { |
| 829 | const id = 'recorder-extension-view-' + extensionServer.nextObjectId(); |
| 830 | await new Promise(resolve => { |
| 831 | extensionServer.sendRequest( |
| 832 | {command: PrivateAPI.Commands.CreateRecorderView, id, title, pagePath}, resolve); |
| 833 | }); |
| 834 | return new (Constructor(RecorderView))(id); |
| 835 | }, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 836 | }; |
| 837 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 838 | function LanguageServicesAPIImpl(this: APIImpl.LanguageExtensions): void { |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 839 | this._plugins = new Map(); |
| 840 | } |
| 841 | |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 842 | (LanguageServicesAPIImpl.prototype as PublicAPI.Chrome.DevTools.LanguageExtensions) = { |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 843 | registerLanguageExtensionPlugin: async function( |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 844 | this: APIImpl.LanguageExtensions, plugin: PublicAPI.Chrome.DevTools.LanguageExtensionPlugin, pluginName: string, |
| 845 | supportedScriptTypes: PublicAPI.Chrome.DevTools.SupportedScriptTypes): Promise<void> { |
| 846 | if (this._plugins.has(plugin)) { |
| 847 | throw new Error(`Tried to register plugin '${pluginName}' twice`); |
| 848 | } |
| 849 | const channel = new MessageChannel(); |
| 850 | const port = channel.port1; |
| 851 | this._plugins.set(plugin, port); |
| 852 | port.onmessage = ({data}: MessageEvent<{requestId: number}&PrivateAPI.LanguageExtensionRequests>): void => { |
| 853 | const {requestId} = data; |
| 854 | console.time(`${requestId}: ${data.method}`); |
| 855 | dispatchMethodCall(data) |
| 856 | .then(result => port.postMessage({requestId, result})) |
| 857 | .catch(error => port.postMessage({requestId, error: {message: error.message}})) |
| 858 | .finally(() => console.timeEnd(`${requestId}: ${data.method}`)); |
| 859 | }; |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 860 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 861 | function dispatchMethodCall(request: PrivateAPI.LanguageExtensionRequests): Promise<unknown> { |
| 862 | switch (request.method) { |
| 863 | case PrivateAPI.LanguageExtensionPluginCommands.AddRawModule: |
| 864 | return plugin.addRawModule( |
| 865 | request.parameters.rawModuleId, request.parameters.symbolsURL, request.parameters.rawModule); |
| 866 | case PrivateAPI.LanguageExtensionPluginCommands.RemoveRawModule: |
| 867 | return plugin.removeRawModule(request.parameters.rawModuleId); |
| 868 | case PrivateAPI.LanguageExtensionPluginCommands.SourceLocationToRawLocation: |
| 869 | return plugin.sourceLocationToRawLocation(request.parameters.sourceLocation); |
| 870 | case PrivateAPI.LanguageExtensionPluginCommands.RawLocationToSourceLocation: |
| 871 | return plugin.rawLocationToSourceLocation(request.parameters.rawLocation); |
| 872 | case PrivateAPI.LanguageExtensionPluginCommands.GetScopeInfo: |
| 873 | return plugin.getScopeInfo(request.parameters.type); |
| 874 | case PrivateAPI.LanguageExtensionPluginCommands.ListVariablesInScope: |
| 875 | return plugin.listVariablesInScope(request.parameters.rawLocation); |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 876 | case PrivateAPI.LanguageExtensionPluginCommands.GetFunctionInfo: |
| 877 | return plugin.getFunctionInfo(request.parameters.rawLocation); |
| 878 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedFunctionRanges: |
| 879 | return plugin.getInlinedFunctionRanges(request.parameters.rawLocation); |
| 880 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedCalleesRanges: |
| 881 | return plugin.getInlinedCalleesRanges(request.parameters.rawLocation); |
| 882 | case PrivateAPI.LanguageExtensionPluginCommands.GetMappedLines: |
| 883 | if ('getMappedLines' in plugin) { |
| 884 | return plugin.getMappedLines(request.parameters.rawModuleId, request.parameters.sourceFileURL); |
| 885 | } |
| 886 | return Promise.resolve(undefined); |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 887 | case PrivateAPI.LanguageExtensionPluginCommands.FormatValue: |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 888 | if ('evaluate' in plugin && plugin.evaluate) { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 889 | return plugin.evaluate( |
| 890 | request.parameters.expression, request.parameters.context, request.parameters.stopId); |
| 891 | } |
| 892 | return Promise.resolve(undefined); |
| 893 | case PrivateAPI.LanguageExtensionPluginCommands.GetProperties: |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 894 | if ('getProperties' in plugin && plugin.getProperties) { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 895 | return plugin.getProperties(request.parameters.objectId); |
| 896 | } |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 897 | if (!('evaluate' in plugin && |
| 898 | plugin.evaluate)) { // If evalute is defined but the remote objects methods aren't, that's a bug |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 899 | return Promise.resolve(undefined); |
| 900 | } |
| 901 | break; |
| 902 | case PrivateAPI.LanguageExtensionPluginCommands.ReleaseObject: |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 903 | if ('releaseObject' in plugin && plugin.releaseObject) { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 904 | return plugin.releaseObject(request.parameters.objectId); |
| 905 | } |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 906 | break; |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 907 | } |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 908 | throw new Error(`Unknown language plugin method ${request.method}`); |
| 909 | } |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 910 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 911 | await new Promise<void>(resolve => { |
| 912 | extensionServer.sendRequest( |
| 913 | { |
| 914 | command: PrivateAPI.Commands.RegisterLanguageExtensionPlugin, |
| 915 | pluginName, |
| 916 | port: channel.port2, |
| 917 | supportedScriptTypes, |
| 918 | }, |
| 919 | () => resolve(), [channel.port2]); |
| 920 | }); |
| 921 | }, |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 922 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 923 | unregisterLanguageExtensionPlugin: async function( |
| 924 | this: APIImpl.LanguageExtensions, plugin: PublicAPI.Chrome.DevTools.LanguageExtensionPlugin): Promise<void> { |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 925 | const port = this._plugins.get(plugin); |
| 926 | if (!port) { |
| 927 | throw new Error('Tried to unregister a plugin that was not previously registered'); |
| 928 | } |
| 929 | this._plugins.delete(plugin); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 930 | port.postMessage({event: PrivateAPI.LanguageExtensionPluginEvents.UnregisteredLanguageExtensionPlugin}); |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 931 | port.close(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 932 | }, |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 933 | |
| 934 | getWasmLinearMemory: async function( |
| 935 | this: APIImpl.LanguageExtensions, offset: number, length: number, stopId: number): Promise<ArrayBuffer> { |
| 936 | const result = await new Promise( |
| 937 | resolve => extensionServer.sendRequest( |
| 938 | {command: PrivateAPI.Commands.GetWasmLinearMemory, offset, length, stopId}, resolve)); |
| 939 | if (Array.isArray(result)) { |
| 940 | return new Uint8Array(result).buffer; |
| 941 | } |
| 942 | return new ArrayBuffer(0); |
| 943 | }, |
| 944 | getWasmLocal: async function( |
| 945 | this: APIImpl.LanguageExtensions, local: number, stopId: number): Promise<PublicAPI.Chrome.DevTools.WasmValue> { |
| 946 | return new Promise( |
| 947 | resolve => extensionServer.sendRequest({command: PrivateAPI.Commands.GetWasmLocal, local, stopId}, resolve)); |
| 948 | }, |
| 949 | getWasmGlobal: async function(this: APIImpl.LanguageExtensions, global: number, stopId: number): |
| 950 | Promise<PublicAPI.Chrome.DevTools.WasmValue> { |
| 951 | return new Promise( |
| 952 | resolve => |
| 953 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetWasmGlobal, global, stopId}, resolve)); |
| 954 | }, |
| 955 | getWasmOp: async function(this: APIImpl.LanguageExtensions, op: number, stopId: number): |
| 956 | Promise<PublicAPI.Chrome.DevTools.WasmValue> { |
| 957 | return new Promise( |
| 958 | resolve => extensionServer.sendRequest({command: PrivateAPI.Commands.GetWasmOp, op, stopId}, resolve)); |
| 959 | }, |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 960 | }; |
| 961 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 962 | function declareInterfaceClass<ImplT extends APIImpl.Callable>(implConstructor: ImplT): ( |
| 963 | this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>) => void { |
| 964 | return function(this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 965 | const impl = {__proto__: implConstructor.prototype}; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 966 | implConstructor.apply(impl, args); |
| 967 | populateInterfaceClass(this as {[key: string]: unknown}, impl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 968 | }; |
| 969 | } |
| 970 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 971 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 972 | function defineDeprecatedProperty(object: any, className: string, oldName: string, newName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 973 | let warningGiven = false; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 974 | function getter(): unknown { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 975 | if (!warningGiven) { |
| 976 | console.warn(className + '.' + oldName + ' is deprecated. Use ' + className + '.' + newName + ' instead'); |
| 977 | warningGiven = true; |
| 978 | } |
| 979 | return object[newName]; |
| 980 | } |
| 981 | object.__defineGetter__(oldName, getter); |
| 982 | } |
| 983 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 984 | function extractCallbackArgument(args: IArguments): ((...args: unknown[]) => unknown)|undefined { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 985 | const lastArgument = args[args.length - 1]; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 986 | return typeof lastArgument === 'function' ? lastArgument as (...args: unknown[]) => unknown : undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 987 | } |
| 988 | |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 989 | const LanguageServicesAPI = declareInterfaceClass(LanguageServicesAPIImpl); |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 990 | const RecorderServicesAPI = declareInterfaceClass(RecorderServicesAPIImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 991 | const Button = declareInterfaceClass(ButtonImpl); |
| 992 | const EventSink = declareInterfaceClass(EventSinkImpl); |
| 993 | const ExtensionPanel = declareInterfaceClass(ExtensionPanelImpl); |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 994 | const RecorderView = declareInterfaceClass(RecorderViewImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 995 | const ExtensionSidebarPane = declareInterfaceClass(ExtensionSidebarPaneImpl); |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 996 | const PanelWithSidebarClass = declareInterfaceClass(PanelWithSidebarImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 997 | const Request = declareInterfaceClass(RequestImpl); |
| 998 | const Resource = declareInterfaceClass(ResourceImpl); |
| 999 | const TraceSession = declareInterfaceClass(TraceSessionImpl); |
| 1000 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1001 | class ElementsPanel extends (Constructor(PanelWithSidebarClass)) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 1002 | constructor() { |
| 1003 | super('elements'); |
| 1004 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1005 | } |
| 1006 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1007 | class SourcesPanel extends (Constructor(PanelWithSidebarClass)) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 1008 | constructor() { |
| 1009 | super('sources'); |
| 1010 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1011 | } |
| 1012 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1013 | function ExtensionPanelImpl(this: APIImpl.ExtensionPanel, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1014 | ExtensionViewImpl.call(this, id); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1015 | |
| 1016 | this.onSearch = new (Constructor(EventSink))(PrivateAPI.Events.PanelSearch + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1017 | } |
| 1018 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1019 | (ExtensionPanelImpl.prototype as Pick<APIImpl.ExtensionPanel, 'createStatusBarButton'|'show'>& |
| 1020 | {__proto__: APIImpl.ExtensionView}) = { |
| 1021 | createStatusBarButton: function( |
| 1022 | this: APIImpl.ExtensionPanel, iconPath: string, tooltipText: string, disabled: boolean): |
| 1023 | PublicAPI.Chrome.DevTools.Button { |
| 1024 | const id = 'button-' + extensionServer.nextObjectId(); |
| 1025 | extensionServer.sendRequest({ |
| 1026 | command: PrivateAPI.Commands.CreateToolbarButton, |
| 1027 | panel: this._id as string, |
| 1028 | id: id, |
| 1029 | icon: iconPath, |
| 1030 | tooltip: tooltipText, |
| 1031 | disabled: Boolean(disabled), |
| 1032 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1033 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1034 | return new (Constructor(Button))(id); |
| 1035 | }, |
| 1036 | |
| 1037 | show: function(this: APIImpl.ExtensionPanel): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1038 | if (!userAction) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1039 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1040 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1041 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1042 | extensionServer.sendRequest({command: PrivateAPI.Commands.ShowPanel, id: this._id as string}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1043 | }, |
| 1044 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1045 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1046 | }; |
| 1047 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 1048 | function RecorderViewImpl(this: APIImpl.RecorderView, id: string): void { |
| 1049 | ExtensionViewImpl.call(this, id); |
| 1050 | } |
| 1051 | |
| 1052 | (RecorderViewImpl.prototype as Pick<APIImpl.RecorderView, 'show'>& {__proto__: APIImpl.ExtensionView}) = { |
| 1053 | show: function(this: APIImpl.RecorderView): void { |
| 1054 | if (!userAction || !userRecorderAction) { |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | extensionServer.sendRequest({command: PrivateAPI.Commands.ShowRecorderView, id: this._id as string}); |
| 1059 | }, |
| 1060 | |
| 1061 | __proto__: ExtensionViewImpl.prototype, |
| 1062 | }; |
| 1063 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1064 | function ExtensionSidebarPaneImpl(this: APIImpl.ExtensionSidebarPane, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1065 | ExtensionViewImpl.call(this, id); |
| 1066 | } |
| 1067 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1068 | (ExtensionSidebarPaneImpl.prototype as |
| 1069 | Pick<APIImpl.ExtensionSidebarPane, 'setHeight'|'setExpression'|'setObject'|'setPage'>& |
| 1070 | {__proto__: APIImpl.ExtensionView}) = { |
| 1071 | setHeight: function(this: APIImpl.ExtensionSidebarPane, height: string): void { |
| 1072 | extensionServer.sendRequest( |
| 1073 | {command: PrivateAPI.Commands.SetSidebarHeight, id: this._id as string, height: height}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1074 | }, |
| 1075 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1076 | setExpression: function( |
| 1077 | this: APIImpl.ExtensionSidebarPane, expression: string, rootTitle: string, |
| 1078 | evaluateOptions?: PrivateAPI.EvaluateOptions, _callback?: () => unknown): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1079 | extensionServer.sendRequest( |
| 1080 | { |
| 1081 | command: PrivateAPI.Commands.SetSidebarContent, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1082 | id: this._id as string, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1083 | expression: expression, |
| 1084 | rootTitle: rootTitle, |
| 1085 | evaluateOnPage: true, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1086 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : {}), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1087 | }, |
| 1088 | extractCallbackArgument(arguments)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1089 | }, |
| 1090 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1091 | setObject: function( |
| 1092 | this: APIImpl.ExtensionSidebarPane, jsonObject: string, rootTitle?: string, callback?: () => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1093 | extensionServer.sendRequest( |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1094 | { |
| 1095 | command: PrivateAPI.Commands.SetSidebarContent, |
| 1096 | id: this._id as string, |
| 1097 | expression: jsonObject, |
| 1098 | rootTitle: rootTitle, |
| 1099 | }, |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1100 | callback); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1101 | }, |
| 1102 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1103 | setPage: function(this: APIImpl.ExtensionSidebarPane, page: string): void { |
| 1104 | extensionServer.sendRequest({command: PrivateAPI.Commands.SetSidebarPage, id: this._id as string, page: page}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1105 | }, |
| 1106 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1107 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1108 | }; |
| 1109 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1110 | function ButtonImpl(this: APIImpl.Button, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1111 | this._id = id; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1112 | |
| 1113 | this.onClicked = new (Constructor(EventSink))(PrivateAPI.Events.ButtonClicked + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1114 | } |
| 1115 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1116 | (ButtonImpl.prototype as Pick<APIImpl.Button, 'update'>) = { |
| 1117 | update: function(this: APIImpl.Button, iconPath?: string, tooltipText?: string, disabled?: boolean): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1118 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1119 | command: PrivateAPI.Commands.UpdateButton, |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1120 | id: this._id, |
| 1121 | icon: iconPath, |
| 1122 | tooltip: tooltipText, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1123 | disabled: Boolean(disabled), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1124 | }); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1125 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1126 | }; |
| 1127 | |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 1128 | function Timeline(this: APIImpl.Timeline): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1129 | } |
| 1130 | |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 1131 | (Timeline.prototype as Pick<APIImpl.Timeline, 'addTraceProvider'>) = { |
| 1132 | addTraceProvider: function(this: APIImpl.Timeline, categoryName: string, categoryTooltip: string): |
| 1133 | APIImpl.TraceProvider { |
| 1134 | const id = 'extension-trace-provider-' + extensionServer.nextObjectId(); |
| 1135 | extensionServer.sendRequest({ |
| 1136 | command: PrivateAPI.Commands.AddTraceProvider, |
| 1137 | id: id, |
| 1138 | categoryName: categoryName, |
| 1139 | categoryTooltip: categoryTooltip, |
| 1140 | }); |
| 1141 | |
| 1142 | return new (Constructor(TraceProvider))(id); |
| 1143 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1144 | }; |
| 1145 | |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 1146 | function TraceSessionImpl(this: APIImpl.TraceSession, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1147 | this._id = id; |
| 1148 | } |
| 1149 | |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 1150 | (TraceSessionImpl.prototype as Pick<APIImpl.TraceSession, 'complete'>) = { |
Kateryna Prokopenko | 6fa122c | 2022-05-06 08:54:47 | [diff] [blame] | 1151 | complete: function(this: APIImpl.TraceSession, url?: Platform.DevToolsPath.UrlString, timeOffset?: number): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1152 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1153 | command: PrivateAPI.Commands.CompleteTraceSession, |
| 1154 | id: this._id, |
Kateryna Prokopenko | 6fa122c | 2022-05-06 08:54:47 | [diff] [blame] | 1155 | url: url || Platform.DevToolsPath.EmptyUrlString, |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1156 | timeOffset: timeOffset || 0, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1157 | }); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1158 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1159 | }; |
| 1160 | |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 1161 | function TraceProvider(this: APIImpl.TraceProvider, id: string): void { |
| 1162 | function dispatchRecordingStarted( |
| 1163 | this: APIImpl.EventSink<APIImpl.Callable>, message: {arguments: unknown[]}): void { |
| 1164 | const sessionId = message.arguments[0] as string; |
| 1165 | |
| 1166 | this._fire(new (Constructor(TraceSession))(sessionId)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1167 | } |
| 1168 | |
Philip Pfaffe | 1c82721 | 2021-06-25 12:33:20 | [diff] [blame] | 1169 | this.onRecordingStarted = |
| 1170 | new (Constructor(EventSink))(PrivateAPI.Events.RecordingStarted + id, dispatchRecordingStarted); |
| 1171 | |
| 1172 | this.onRecordingStopped = new (Constructor(EventSink))(PrivateAPI.Events.RecordingStopped + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1173 | } |
| 1174 | |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1175 | function canAccessResource(resource: APIImpl.ResourceData): boolean { |
Danil Somsikov | b2e3705 | 2023-03-24 12:50:43 | [diff] [blame] | 1176 | try { |
| 1177 | return extensionInfo.allowFileAccess || (new URL(resource.url)).protocol !== 'file:'; |
| 1178 | } catch (e) { |
| 1179 | return false; |
| 1180 | } |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1181 | } |
| 1182 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1183 | function InspectedWindow(this: PublicAPI.Chrome.DevTools.InspectedWindow): void { |
| 1184 | function dispatchResourceEvent( |
| 1185 | this: APIImpl.EventSink<(resource: APIImpl.Resource) => unknown>, message: {arguments: unknown[]}): void { |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1186 | const resourceData = message.arguments[0] as APIImpl.ResourceData; |
| 1187 | if (!canAccessResource(resourceData)) { |
| 1188 | return; |
| 1189 | } |
| 1190 | this._fire(new (Constructor(Resource))(resourceData)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1191 | } |
| 1192 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1193 | function dispatchResourceContentEvent( |
| 1194 | this: APIImpl.EventSink<(resource: APIImpl.Resource, content: string) => unknown>, |
| 1195 | message: {arguments: unknown[]}): void { |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1196 | const resourceData = message.arguments[0] as APIImpl.ResourceData; |
| 1197 | if (!canAccessResource(resourceData)) { |
| 1198 | return; |
| 1199 | } |
| 1200 | this._fire(new (Constructor(Resource))(resourceData), message.arguments[1] as string); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1201 | } |
| 1202 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1203 | this.onResourceAdded = new (Constructor(EventSink))(PrivateAPI.Events.ResourceAdded, dispatchResourceEvent); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1204 | this.onResourceContentCommitted = |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1205 | new (Constructor(EventSink))(PrivateAPI.Events.ResourceContentCommitted, dispatchResourceContentEvent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1206 | } |
| 1207 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1208 | (InspectedWindow.prototype as Pick<PublicAPI.Chrome.DevTools.InspectedWindow, 'reload'|'eval'|'getResources'>) = { |
| 1209 | reload: function(optionsOrUserAgent: { |
| 1210 | ignoreCache?: boolean, |
| 1211 | injectedScript?: string, |
| 1212 | userAgent?: string, |
| 1213 | }): void { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1214 | let options: { |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1215 | ignoreCache?: boolean, |
| 1216 | injectedScript?: string, |
| 1217 | userAgent?: string, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1218 | }|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1219 | if (typeof optionsOrUserAgent === 'object') { |
| 1220 | options = optionsOrUserAgent; |
| 1221 | } else if (typeof optionsOrUserAgent === 'string') { |
| 1222 | options = {userAgent: optionsOrUserAgent}; |
| 1223 | console.warn( |
| 1224 | 'Passing userAgent as string parameter to inspectedWindow.reload() is deprecated. ' + |
| 1225 | 'Use inspectedWindow.reload({ userAgent: value}) instead.'); |
| 1226 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1227 | extensionServer.sendRequest({command: PrivateAPI.Commands.Reload, options: options}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1228 | }, |
| 1229 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1230 | eval: function( |
| 1231 | expression: string, |
Benedikt Meurer | 6428bce | 2023-09-15 10:47:33 | [diff] [blame^] | 1232 | evaluateOptions: {scriptExecutionContext?: string, frameURL?: string, useContentScriptContext?: boolean}): |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1233 | Object | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1234 | null { |
| 1235 | const callback = extractCallbackArgument(arguments); |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1236 | function callbackWrapper(result: unknown): void { |
| 1237 | const {isError, isException, value} = result as { |
| 1238 | isError?: boolean, |
| 1239 | isException?: boolean, value: unknown, |
| 1240 | }; |
| 1241 | if (isError || isException) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1242 | callback && callback(undefined, result); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1243 | } else { |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1244 | callback && callback(value); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1245 | } |
| 1246 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1247 | extensionServer.sendRequest( |
| 1248 | { |
| 1249 | command: PrivateAPI.Commands.EvaluateOnInspectedPage, |
| 1250 | expression: expression, |
| 1251 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : undefined), |
| 1252 | }, |
| 1253 | callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1254 | return null; |
| 1255 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1256 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1257 | getResources: function(callback?: (resources: PublicAPI.Chrome.DevTools.Resource[]) => unknown): void { |
| 1258 | function wrapResource(resourceData: APIImpl.ResourceData): APIImpl.Resource { |
| 1259 | return new (Constructor(Resource))(resourceData); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1260 | } |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1261 | function callbackWrapper(resources: unknown): void { |
Danil Somsikov | f8035f0 | 2023-07-17 14:05:41 | [diff] [blame] | 1262 | callback && callback((resources as APIImpl.ResourceData[]).filter(canAccessResource).map(wrapResource)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1263 | } |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1264 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetPageResources}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1265 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1266 | }; |
| 1267 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1268 | function ResourceImpl(this: APIImpl.Resource, resourceData: APIImpl.ResourceData): void { |
Danil Somsikov | f8035f0 | 2023-07-17 14:05:41 | [diff] [blame] | 1269 | if (!canAccessResource(resourceData)) { |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1270 | throw new Error('Resource access not allowed'); |
| 1271 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1272 | this._url = resourceData.url; |
| 1273 | this._type = resourceData.type; |
| 1274 | } |
| 1275 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1276 | (ResourceImpl.prototype as Pick<APIImpl.Resource, 'url'|'type'|'getContent'|'setContent'>) = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1277 | get url(): string { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1278 | return (this as APIImpl.Resource)._url; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1279 | }, |
| 1280 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1281 | get type(): string { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1282 | return (this as APIImpl.Resource)._type; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1283 | }, |
| 1284 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1285 | getContent: function(this: APIImpl.Resource, callback?: (content: string, encoding: string) => unknown): void { |
| 1286 | function callbackWrapper(response: unknown): void { |
| 1287 | const {content, encoding} = response as {content: string, encoding: string}; |
| 1288 | callback && callback(content, encoding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1289 | } |
| 1290 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1291 | extensionServer.sendRequest( |
| 1292 | {command: PrivateAPI.Commands.GetResourceContent, url: this._url}, callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1293 | }, |
| 1294 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1295 | setContent: function( |
| 1296 | this: APIImpl.Resource, content: string, commit: boolean, callback: (error?: Object) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1297 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1298 | {command: PrivateAPI.Commands.SetResourceContent, url: this._url, content: content, commit: commit}, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1299 | callback as (response: unknown) => unknown); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1300 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1301 | }; |
| 1302 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1303 | function getTabId(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1304 | return inspectedTabId; |
| 1305 | } |
| 1306 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1307 | let keyboardEventRequestQueue: KeyboardEventInit&{eventType: string}[] = []; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1308 | let forwardTimer: number|null = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1309 | function forwardKeyboardEvent(event: KeyboardEvent): void { |
Jan Scheffler | e7d7bb1 | 2019-10-24 09:18:52 | [diff] [blame] | 1310 | // Check if the event should be forwarded. |
| 1311 | // This is a workaround for crbug.com/923338. |
| 1312 | const focused = document.activeElement; |
| 1313 | if (focused) { |
Benedikt Meurer | 5d508f6 | 2022-12-06 14:37:38 | [diff] [blame] | 1314 | const isInput = |
| 1315 | focused.nodeName === 'INPUT' || focused.nodeName === 'TEXTAREA' || (focused as HTMLElement).isContentEditable; |
Jan Scheffler | e7d7bb1 | 2019-10-24 09:18:52 | [diff] [blame] | 1316 | if (isInput && !(event.ctrlKey || event.altKey || event.metaKey)) { |
| 1317 | return; |
| 1318 | } |
| 1319 | } |
| 1320 | |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1321 | let modifiers = 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1322 | if (event.shiftKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1323 | modifiers |= 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1324 | } |
| 1325 | if (event.ctrlKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1326 | modifiers |= 2; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1327 | } |
| 1328 | if (event.altKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1329 | modifiers |= 4; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1330 | } |
| 1331 | if (event.metaKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1332 | modifiers |= 8; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1333 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1334 | const num = (event.keyCode & 255) | (modifiers << 8); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1335 | // We only care about global hotkeys, not about random text |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1336 | if (!keysToForwardSet.has(num)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1337 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1338 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1339 | event.preventDefault(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1340 | const requestPayload = { |
| 1341 | eventType: event.type, |
| 1342 | ctrlKey: event.ctrlKey, |
| 1343 | altKey: event.altKey, |
| 1344 | metaKey: event.metaKey, |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1345 | shiftKey: event.shiftKey, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1346 | // @ts-expect-error keyIdentifier is a deprecated non-standard property that typescript doesn't know about. |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1347 | keyIdentifier: event.keyIdentifier, |
| 1348 | key: event.key, |
| 1349 | code: event.code, |
| 1350 | location: event.location, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1351 | keyCode: event.keyCode, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1352 | }; |
| 1353 | keyboardEventRequestQueue.push(requestPayload); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1354 | if (!forwardTimer) { |
Tim van der Lippe | 6bcbe0f | 2022-01-11 13:10:31 | [diff] [blame] | 1355 | forwardTimer = window.setTimeout(forwardEventQueue, 0); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1356 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1357 | } |
| 1358 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1359 | function forwardEventQueue(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1360 | forwardTimer = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1361 | extensionServer.sendRequest( |
| 1362 | {command: PrivateAPI.Commands.ForwardKeyboardEvent, entries: keyboardEventRequestQueue}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1363 | keyboardEventRequestQueue = []; |
| 1364 | } |
| 1365 | |
| 1366 | document.addEventListener('keydown', forwardKeyboardEvent, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1367 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1368 | function ExtensionServerClient(this: APIImpl.ExtensionServerClient, targetWindow: Window): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1369 | this._callbacks = {}; |
| 1370 | this._handlers = {}; |
| 1371 | this._lastRequestId = 0; |
| 1372 | this._lastObjectId = 0; |
| 1373 | |
| 1374 | this.registerHandler('callback', this._onCallback.bind(this)); |
| 1375 | |
| 1376 | const channel = new MessageChannel(); |
| 1377 | this._port = channel.port1; |
| 1378 | this._port.addEventListener('message', this._onMessage.bind(this), false); |
| 1379 | this._port.start(); |
| 1380 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1381 | targetWindow.postMessage('registerExtension', '*', [channel.port2]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1382 | } |
| 1383 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1384 | (ExtensionServerClient.prototype as Pick< |
| 1385 | APIImpl.ExtensionServerClient, |
| 1386 | 'sendRequest'|'hasHandler'|'registerHandler'|'unregisterHandler'|'nextObjectId'|'_registerCallback'| |
| 1387 | '_onCallback'|'_onMessage'>) = { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 1388 | sendRequest: function<ResponseT>( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1389 | this: APIImpl.ExtensionServerClient, message: PrivateAPI.ServerRequests, |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 1390 | callback?: (response: ResponseT) => unknown, transfers?: Transferable[]): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1391 | if (typeof callback === 'function') { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 1392 | (message as PrivateAPI.ExtensionServerRequestMessage).requestId = |
| 1393 | this._registerCallback(callback as (response: unknown) => unknown); |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1394 | } |
| 1395 | // @ts-expect-error |
| 1396 | this._port.postMessage(message, transfers); |
| 1397 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1398 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1399 | hasHandler: function(this: APIImpl.ExtensionServerClient, command: string): boolean { |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1400 | return Boolean(this._handlers[command]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1401 | }, |
| 1402 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1403 | registerHandler: function( |
| 1404 | this: APIImpl.ExtensionServerClient, command: string, handler: (request: {arguments: unknown[]}) => unknown): |
| 1405 | void { |
| 1406 | this._handlers[command] = handler; |
| 1407 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1408 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1409 | unregisterHandler: function(this: APIImpl.ExtensionServerClient, command: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1410 | delete this._handlers[command]; |
| 1411 | }, |
| 1412 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1413 | nextObjectId: function(this: APIImpl.ExtensionServerClient): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1414 | return injectedScriptId.toString() + '_' + ++this._lastObjectId; |
| 1415 | }, |
| 1416 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1417 | _registerCallback: function(this: APIImpl.ExtensionServerClient, callback: (response: unknown) => unknown): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1418 | const id = ++this._lastRequestId; |
| 1419 | this._callbacks[id] = callback; |
| 1420 | return id; |
| 1421 | }, |
| 1422 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1423 | _onCallback: function(this: APIImpl.ExtensionServerClient, request: {requestId: number, result: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1424 | if (request.requestId in this._callbacks) { |
| 1425 | const callback = this._callbacks[request.requestId]; |
| 1426 | delete this._callbacks[request.requestId]; |
| 1427 | callback(request.result); |
| 1428 | } |
| 1429 | }, |
| 1430 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1431 | _onMessage: function( |
| 1432 | this: APIImpl.ExtensionServerClient, |
| 1433 | event: MessageEvent<{command: string, requestId: number, arguments: unknown[]}>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1434 | const request = event.data; |
| 1435 | const handler = this._handlers[request.command]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1436 | if (handler) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1437 | handler.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1438 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1439 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1440 | }; |
| 1441 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1442 | function populateInterfaceClass(interfaze: {[key: string]: unknown}, implementation: {[key: string]: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1443 | for (const member in implementation) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1444 | if (member.charAt(0) === '_') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1445 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1446 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1447 | let descriptor: (PropertyDescriptor|undefined)|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1448 | // Traverse prototype chain until we find the owner. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1449 | for (let owner = implementation; owner && !descriptor; owner = owner.__proto__ as {[key: string]: unknown}) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1450 | descriptor = Object.getOwnPropertyDescriptor(owner, member); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1451 | } |
| 1452 | if (!descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1453 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1454 | } |
| 1455 | if (typeof descriptor.value === 'function') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1456 | interfaze[member] = descriptor.value.bind(implementation); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1457 | } else if (typeof descriptor.get === 'function') { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1458 | // @ts-expect-error |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1459 | interfaze.__defineGetter__(member, descriptor.get.bind(implementation)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1460 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1461 | Object.defineProperty(interfaze, member, descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1462 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1466 | const extensionServer = new (Constructor(ExtensionServerClient))(targetWindowForTest || window.parent); |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1467 | |
| 1468 | const coreAPI = new (Constructor(InspectorExtensionAPI))(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1469 | |
| 1470 | Object.defineProperty(chrome, 'devtools', {value: {}, enumerable: true}); |
| 1471 | |
| 1472 | // Only expose tabId on chrome.devtools.inspectedWindow, not webInspector.inspectedWindow. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1473 | // @ts-expect-error |
| 1474 | chrome.devtools!.inspectedWindow = {}; |
| 1475 | Object.defineProperty(chrome.devtools!.inspectedWindow, 'tabId', {get: getTabId}); |
| 1476 | // @ts-expect-error |
| 1477 | chrome.devtools!.inspectedWindow.__proto__ = coreAPI.inspectedWindow; |
| 1478 | chrome.devtools!.network = coreAPI.network; |
| 1479 | chrome.devtools!.panels = coreAPI.panels; |
| 1480 | chrome.devtools!.panels.themeName = themeName; |
| 1481 | chrome.devtools!.languageServices = coreAPI.languageServices; |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1482 | chrome.devtools!.recorder = coreAPI.recorder; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1483 | |
| 1484 | // default to expose experimental APIs for now. |
| 1485 | if (extensionInfo.exposeExperimentalAPIs !== false) { |
| 1486 | chrome.experimental = chrome.experimental || {}; |
| 1487 | chrome.experimental.devtools = chrome.experimental.devtools || {}; |
| 1488 | |
| 1489 | const properties = Object.getOwnPropertyNames(coreAPI); |
| 1490 | for (let i = 0; i < properties.length; ++i) { |
| 1491 | const descriptor = Object.getOwnPropertyDescriptor(coreAPI, properties[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1492 | if (descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1493 | Object.defineProperty(chrome.experimental.devtools, properties[i], descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1494 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1495 | } |
| 1496 | chrome.experimental.devtools.inspectedWindow = chrome.devtools.inspectedWindow; |
| 1497 | } |
| 1498 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1499 | if (extensionInfo.exposeWebInspectorNamespace) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1500 | window.webInspector = coreAPI; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1501 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1502 | testHook(extensionServer, coreAPI); |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1503 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1504 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1505 | self.buildExtensionAPIInjectedScript = function( |
| 1506 | extensionInfo: { |
| 1507 | startPage: string, |
| 1508 | name: string, |
| 1509 | exposeExperimentalAPIs: boolean, |
| 1510 | }, |
| 1511 | inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1512 | testHook: |
| 1513 | ((extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown)| |
| 1514 | undefined): string { |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 1515 | const argumentsJSON = |
| 1516 | [extensionInfo, inspectedTabId || null, themeName, keysToForward].map(_ => JSON.stringify(_)).join(','); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1517 | if (!testHook) { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1518 | testHook = (): void => {}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1519 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1520 | return '(function(injectedScriptId){ ' + |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1521 | '(' + self.injectedExtensionAPI.toString() + ')(' + argumentsJSON + ',' + testHook + ', injectedScriptId);' + |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1522 | '})'; |
Tim van der Lippe | 29fab47 | 2019-08-15 14:46:48 | [diff] [blame] | 1523 | }; |