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