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