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 | |
Benedikt Meurer | cfc8cd6 | 2024-01-17 10:08:28 | [diff] [blame^] | 363 | export type Callable = (...args: any[]) => void; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 364 | |
| 365 | export interface EventSink<ListenerT extends Callable> extends PublicAPI.Chrome.DevTools.EventSink<ListenerT> { |
| 366 | _type: string; |
| 367 | _listeners: ListenerT[]; |
| 368 | _customDispatch: undefined|((this: EventSink<ListenerT>, request: {arguments: unknown[]}) => unknown); |
| 369 | |
| 370 | _fire(..._vararg: Parameters<ListenerT>): void; |
| 371 | _dispatch(request: {arguments: unknown[]}): void; |
| 372 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 373 | |
| 374 | export interface Network extends PublicAPI.Chrome.DevTools.Network { |
| 375 | addRequestHeaders(headers: {[key: string]: string}): void; |
| 376 | } |
| 377 | |
| 378 | export interface Request extends PublicAPI.Chrome.DevTools.Request, HAR.Log.EntryDTO { |
| 379 | _id: number; |
| 380 | } |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 381 | |
| 382 | export interface Panels extends PublicAPI.Chrome.DevTools.Panels { |
| 383 | get SearchAction(): {[key: string]: string}; |
| 384 | applyStyleSheet(styleSheet: string): void; |
| 385 | setOpenResourceHandler(callback?: (resource: PublicAPI.Chrome.DevTools.Resource, lineNumber: number) => unknown): |
| 386 | void; |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 387 | setThemeChangeHandler(callback?: (themeName: string) => unknown): void; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | export interface ExtensionView extends PublicAPI.Chrome.DevTools.ExtensionView { |
| 391 | _id: string|null; |
| 392 | } |
| 393 | |
| 394 | export interface ExtensionSidebarPane extends ExtensionView, PublicAPI.Chrome.DevTools.ExtensionSidebarPane { |
| 395 | setExpression( |
| 396 | expression: string, rootTitle?: string, evaluteOptions?: PrivateAPI.EvaluateOptions, |
| 397 | callback?: () => unknown): void; |
| 398 | } |
| 399 | |
| 400 | export interface PanelWithSidebar extends ExtensionView, PublicAPI.Chrome.DevTools.PanelWithSidebar { |
| 401 | _hostPanelName: string; |
| 402 | } |
| 403 | |
| 404 | export interface LanguageExtensions extends PublicAPI.Chrome.DevTools.LanguageExtensions { |
| 405 | _plugins: Map<PublicAPI.Chrome.DevTools.LanguageExtensionPlugin, MessagePort>; |
| 406 | } |
| 407 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 408 | export interface RecorderExtensions extends PublicAPI.Chrome.DevTools.RecorderExtensions { |
| 409 | _plugins: Map<PublicAPI.Chrome.DevTools.RecorderExtensionPlugin, MessagePort>; |
| 410 | } |
| 411 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 412 | export interface ExtensionPanel extends ExtensionView, PublicAPI.Chrome.DevTools.ExtensionPanel { |
| 413 | show(): void; |
| 414 | } |
| 415 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 416 | export interface RecorderView extends ExtensionView, PublicAPI.Chrome.DevTools.RecorderView {} |
| 417 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 418 | export interface Button extends PublicAPI.Chrome.DevTools.Button { |
| 419 | _id: string; |
| 420 | } |
| 421 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 422 | export type ResourceData = {url: string, type: string}; |
| 423 | export interface Resource extends PublicAPI.Chrome.DevTools.Resource { |
| 424 | _type: string; |
| 425 | _url: string; |
| 426 | |
| 427 | get type(): string; |
| 428 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 429 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 430 | |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 431 | self.injectedExtensionAPI = function( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 432 | extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
| 433 | testHook: (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 434 | injectedScriptId: number, targetWindowForTest?: Window): void { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 435 | const keysToForwardSet = new Set<number>(keysToForward); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 436 | const chrome = window.chrome || {}; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 437 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 438 | const devtools_descriptor = Object.getOwnPropertyDescriptor(chrome, 'devtools'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 439 | if (devtools_descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 440 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 441 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 442 | let userAction = false; |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 443 | let userRecorderAction = false; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 444 | |
| 445 | // Here and below, all constructors are private to API implementation. |
| 446 | // For a public type Foo, if internal fields are present, these are on |
| 447 | // a private FooImpl type, an instance of FooImpl is used in a closure |
| 448 | // by Foo consutrctor to re-bind publicly exported members to an instance |
| 449 | // of Foo. |
| 450 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 451 | function EventSinkImpl<ListenerT extends APIImpl.Callable>( |
| 452 | this: APIImpl.EventSink<ListenerT>, type: string, |
| 453 | customDispatch?: (this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 454 | this._type = type; |
| 455 | this._listeners = []; |
| 456 | this._customDispatch = customDispatch; |
| 457 | } |
| 458 | |
| 459 | EventSinkImpl.prototype = { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 460 | addListener: function<ListenerT extends APIImpl.Callable>(this: APIImpl.EventSink<ListenerT>, callback: ListenerT): |
| 461 | void { |
| 462 | if (typeof callback !== 'function') { |
| 463 | throw 'addListener: callback is not a function'; |
| 464 | } |
| 465 | if (this._listeners.length === 0) { |
| 466 | extensionServer.sendRequest({command: PrivateAPI.Commands.Subscribe, type: this._type}); |
| 467 | } |
| 468 | this._listeners.push(callback); |
| 469 | extensionServer.registerHandler('notify-' + this._type, this._dispatch.bind(this)); |
| 470 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 471 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 472 | removeListener: function<ListenerT extends APIImpl.Callable>( |
| 473 | this: APIImpl.EventSink<ListenerT>, callback: ListenerT): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 474 | const listeners = this._listeners; |
| 475 | |
| 476 | for (let i = 0; i < listeners.length; ++i) { |
| 477 | if (listeners[i] === callback) { |
| 478 | listeners.splice(i, 1); |
| 479 | break; |
| 480 | } |
| 481 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 482 | if (this._listeners.length === 0) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 483 | extensionServer.sendRequest({command: PrivateAPI.Commands.Unsubscribe, type: this._type}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 484 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 485 | }, |
| 486 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 487 | _fire: function<ListenerT extends APIImpl.Callable>( |
| 488 | this: APIImpl.EventSink<ListenerT>, ..._vararg: Parameters<ListenerT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 489 | const listeners = this._listeners.slice(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 490 | for (let i = 0; i < listeners.length; ++i) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 491 | listeners[i].apply(null, Array.from(arguments)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 492 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 493 | }, |
| 494 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 495 | _dispatch: function<ListenerT extends APIImpl.Callable>( |
| 496 | this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 497 | if (this._customDispatch) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 498 | this._customDispatch.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 499 | } else { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 500 | this._fire.apply(this, request.arguments as Parameters<ListenerT>); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 501 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 502 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 503 | }; |
| 504 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 505 | function Constructor<NewT extends APIImpl.Callable>(ctor: NewT): new (...args: Parameters<NewT>) => |
| 506 | ThisParameterType<NewT> { |
| 507 | return ctor as unknown as new (...args: Parameters<NewT>) => ThisParameterType<NewT>; |
| 508 | } |
| 509 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 510 | function InspectorExtensionAPI(this: APIImpl.InspectorExtensionAPI): void { |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 511 | this.inspectedWindow = new (Constructor(InspectedWindow))(); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 512 | this.panels = new (Constructor(Panels))(); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 513 | this.network = new (Constructor(Network))(); |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 514 | this.languageServices = new (Constructor(LanguageServicesAPI))(); |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 515 | this.recorder = new (Constructor(RecorderServicesAPI))(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 516 | defineDeprecatedProperty(this, 'webInspector', 'resources', 'network'); |
| 517 | } |
| 518 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 519 | function Network(this: APIImpl.Network): void { |
| 520 | function dispatchRequestEvent( |
| 521 | this: APIImpl.EventSink<(request: PublicAPI.Chrome.DevTools.Request) => unknown>, |
| 522 | message: {arguments: unknown[]}): void { |
| 523 | const request = message.arguments[1] as APIImpl.Request & {__proto__: APIImpl.Request}; |
| 524 | |
| 525 | request.__proto__ = new (Constructor(Request))(message.arguments[0] as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 526 | this._fire(request); |
| 527 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 528 | |
| 529 | this.onRequestFinished = |
| 530 | new (Constructor(EventSink))(PrivateAPI.Events.NetworkRequestFinished, dispatchRequestEvent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 531 | defineDeprecatedProperty(this, 'network', 'onFinished', 'onRequestFinished'); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 532 | |
| 533 | this.onNavigated = new (Constructor(EventSink))(PrivateAPI.Events.InspectedURLChanged); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 534 | } |
| 535 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 536 | (Network.prototype as Pick<APIImpl.Network, 'getHAR'|'addRequestHeaders'>) = { |
| 537 | getHAR: function(this: PublicAPI.Chrome.DevTools.Network, callback?: (harLog: Object) => unknown): void { |
| 538 | function callbackWrapper(response: unknown): void { |
| 539 | const result = |
| 540 | response as ({entries: Array<HAR.Log.EntryDTO&{__proto__?: APIImpl.Request, _requestId?: number}>}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 541 | const entries = (result && result.entries) || []; |
| 542 | for (let i = 0; i < entries.length; ++i) { |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 543 | entries[i].__proto__ = new (Constructor(Request))(entries[i]._requestId as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 544 | delete entries[i]._requestId; |
| 545 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 546 | callback && callback(result as Object); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 547 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 548 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetHAR}, callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 549 | }, |
| 550 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 551 | addRequestHeaders: function(headers: {[key: string]: string}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 552 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 553 | {command: PrivateAPI.Commands.AddRequestHeaders, headers: headers, extensionId: window.location.hostname}); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 554 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 555 | }; |
| 556 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 557 | function RequestImpl(this: APIImpl.Request, id: number): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 558 | this._id = id; |
| 559 | } |
| 560 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 561 | (RequestImpl.prototype as Pick<APIImpl.Request, 'getContent'>) = { |
| 562 | getContent: function(this: APIImpl.Request, callback?: (content: string, encoding: string) => unknown): void { |
| 563 | function callbackWrapper(response: unknown): void { |
| 564 | const {content, encoding} = response as {content: string, encoding: string}; |
| 565 | callback && callback(content, encoding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 566 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 567 | extensionServer.sendRequest( |
| 568 | {command: PrivateAPI.Commands.GetRequestContent, id: this._id}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 569 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | }; |
| 571 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 572 | function Panels(this: APIImpl.Panels): void { |
| 573 | const panels: {[key: string]: ElementsPanel|SourcesPanel} = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 574 | elements: new ElementsPanel(), |
| 575 | sources: new SourcesPanel(), |
| 576 | }; |
| 577 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 578 | function panelGetter(name: string): ElementsPanel|SourcesPanel { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 579 | return panels[name]; |
| 580 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 581 | for (const panel in panels) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 582 | Object.defineProperty(this, panel, {get: panelGetter.bind(null, panel), enumerable: true}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 583 | } |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 584 | this.applyStyleSheet = function(styleSheet: string): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 585 | extensionServer.sendRequest({command: PrivateAPI.Commands.ApplyStyleSheet, styleSheet: styleSheet}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 586 | }; |
| 587 | } |
| 588 | |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 589 | (Panels.prototype as |
| 590 | Pick<APIImpl.Panels, 'create'|'setOpenResourceHandler'|'openResource'|'SearchAction'|'setThemeChangeHandler'>) = { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 591 | create: function( |
| 592 | title: string, icon: string, page: string, |
| 593 | callback: (panel: PublicAPI.Chrome.DevTools.ExtensionPanel) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 594 | const id = 'extension-panel-' + extensionServer.nextObjectId(); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 595 | extensionServer.sendRequest( |
| 596 | {command: PrivateAPI.Commands.CreatePanel, id, title, page}, |
Philip Pfaffe | 3abccb4 | 2021-09-14 09:18:37 | [diff] [blame] | 597 | callback && ((): unknown => callback.call(this, new (Constructor(ExtensionPanel))(id)))); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 598 | }, |
| 599 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 600 | setOpenResourceHandler: function( |
| 601 | callback: (resource: PublicAPI.Chrome.DevTools.Resource, lineNumber: number) => unknown): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 602 | const hadHandler = extensionServer.hasHandler(PrivateAPI.Events.OpenResource); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 603 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 604 | function callbackWrapper(message: unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 605 | // Allow the panel to show itself when handling the event. |
| 606 | userAction = true; |
| 607 | try { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 608 | const {resource, lineNumber} = message as {resource: APIImpl.ResourceData, lineNumber: number}; |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 609 | if (canAccessResource(resource)) { |
| 610 | callback.call(null, new (Constructor(Resource))(resource), lineNumber); |
| 611 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 612 | } finally { |
| 613 | userAction = false; |
| 614 | } |
| 615 | } |
| 616 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 617 | if (!callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 618 | extensionServer.unregisterHandler(PrivateAPI.Events.OpenResource); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 619 | } else { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 620 | extensionServer.registerHandler(PrivateAPI.Events.OpenResource, callbackWrapper); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 621 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 622 | |
| 623 | // 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] | 624 | if (hadHandler === !callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 625 | extensionServer.sendRequest( |
| 626 | {command: PrivateAPI.Commands.SetOpenResourceHandler, 'handlerPresent': Boolean(callback)}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 627 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 628 | }, |
| 629 | |
Paul Lewis | ada689f | 2022-01-11 12:03:40 | [diff] [blame] | 630 | setThemeChangeHandler: function(callback: (themeName: string) => unknown): void { |
| 631 | const hadHandler = extensionServer.hasHandler(PrivateAPI.Events.ThemeChange); |
| 632 | |
| 633 | function callbackWrapper(message: unknown): void { |
| 634 | const {themeName} = message as {themeName: string}; |
| 635 | chrome.devtools.panels.themeName = themeName; |
| 636 | callback.call(null, themeName); |
| 637 | } |
| 638 | |
| 639 | if (!callback) { |
| 640 | extensionServer.unregisterHandler(PrivateAPI.Events.ThemeChange); |
| 641 | } else { |
| 642 | extensionServer.registerHandler(PrivateAPI.Events.ThemeChange, callbackWrapper); |
| 643 | } |
| 644 | |
| 645 | // Only send command if we either removed an existing handler or added handler and had none before. |
| 646 | if (hadHandler === !callback) { |
| 647 | extensionServer.sendRequest( |
| 648 | {command: PrivateAPI.Commands.SetThemeChangeHandler, 'handlerPresent': Boolean(callback)}); |
| 649 | } |
| 650 | }, |
| 651 | |
Philip Pfaffe | 140e543 | 2021-09-13 16:34:23 | [diff] [blame] | 652 | openResource: function( |
Kateryna Prokopenko | 9964ab1 | 2022-03-23 16:41:49 | [diff] [blame] | 653 | url: Platform.DevToolsPath.UrlString, lineNumber: number, columnNumber?: number, |
| 654 | _callback?: (response: unknown) => unknown): void { |
Philip Pfaffe | 140e543 | 2021-09-13 16:34:23 | [diff] [blame] | 655 | const callbackArg = extractCallbackArgument(arguments); |
| 656 | // Handle older API: |
| 657 | const columnNumberArg = typeof columnNumber === 'number' ? columnNumber : 0; |
| 658 | extensionServer.sendRequest( |
| 659 | {command: PrivateAPI.Commands.OpenResource, url, lineNumber, columnNumber: columnNumberArg}, callbackArg); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 660 | }, |
| 661 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 662 | get SearchAction(): {[key: string]: string} { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 663 | return { |
| 664 | CancelSearch: PrivateAPI.Panels.SearchAction.CancelSearch, |
| 665 | PerformSearch: PrivateAPI.Panels.SearchAction.PerformSearch, |
| 666 | NextSearchResult: PrivateAPI.Panels.SearchAction.NextSearchResult, |
| 667 | PreviousSearchResult: PrivateAPI.Panels.SearchAction.PreviousSearchResult, |
| 668 | }; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 669 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 670 | }; |
| 671 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 672 | function ExtensionViewImpl(this: APIImpl.ExtensionView, id: string|null): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 673 | this._id = id; |
| 674 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 675 | function dispatchShowEvent( |
| 676 | this: APIImpl.EventSink<(window?: Window) => unknown>, message: {arguments: unknown[]}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 677 | const frameIndex = message.arguments[0]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 678 | if (typeof frameIndex === 'number') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 679 | this._fire(window.parent.frames[frameIndex]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 680 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 681 | this._fire(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 682 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (id) { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 686 | this.onShown = new (Constructor(EventSink))(PrivateAPI.Events.ViewShown + id, dispatchShowEvent); |
| 687 | |
| 688 | this.onHidden = new (Constructor(EventSink))(PrivateAPI.Events.ViewHidden + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 692 | function PanelWithSidebarImpl(this: APIImpl.PanelWithSidebar, hostPanelName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 693 | ExtensionViewImpl.call(this, null); |
| 694 | this._hostPanelName = hostPanelName; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 695 | |
| 696 | this.onSelectionChanged = new (Constructor(EventSink))(PrivateAPI.Events.PanelObjectSelected + hostPanelName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 697 | } |
| 698 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 699 | (PanelWithSidebarImpl.prototype as Pick<APIImpl.PanelWithSidebar, 'createSidebarPane'>& |
| 700 | {__proto__: APIImpl.ExtensionView}) = { |
| 701 | createSidebarPane: function( |
| 702 | this: APIImpl.PanelWithSidebar, title: string, |
| 703 | callback?: (pane: PublicAPI.Chrome.DevTools.ExtensionSidebarPane) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 704 | const id = 'extension-sidebar-' + extensionServer.nextObjectId(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 705 | function callbackWrapper(): void { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 706 | callback && callback(new (Constructor(ExtensionSidebarPane))(id)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 707 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 708 | extensionServer.sendRequest( |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 709 | {command: PrivateAPI.Commands.CreateSidebarPane, panel: this._hostPanelName, id, title}, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 710 | callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 711 | }, |
| 712 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 713 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 714 | }; |
| 715 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 716 | function RecorderServicesAPIImpl(this: APIImpl.RecorderExtensions): void { |
| 717 | this._plugins = new Map(); |
| 718 | } |
| 719 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 720 | async function registerRecorderExtensionPluginImpl( |
| 721 | this: APIImpl.RecorderExtensions, plugin: PublicAPI.Chrome.DevTools.RecorderExtensionPlugin, pluginName: string, |
| 722 | mediaType?: string): Promise<void> { |
| 723 | if (this._plugins.has(plugin)) { |
| 724 | throw new Error(`Tried to register plugin '${pluginName}' twice`); |
| 725 | } |
| 726 | const channel = new MessageChannel(); |
| 727 | const port = channel.port1; |
| 728 | this._plugins.set(plugin, port); |
| 729 | port.onmessage = ({data}: MessageEvent<{requestId: number}&PrivateAPI.RecorderExtensionRequests>): void => { |
| 730 | const {requestId} = data; |
| 731 | dispatchMethodCall(data) |
| 732 | .then(result => port.postMessage({requestId, result})) |
| 733 | .catch(error => port.postMessage({requestId, error: {message: error.message}})); |
| 734 | }; |
| 735 | |
| 736 | async function dispatchMethodCall(request: PrivateAPI.RecorderExtensionRequests): Promise<unknown> { |
| 737 | switch (request.method) { |
| 738 | case PrivateAPI.RecorderExtensionPluginCommands.Stringify: |
| 739 | return (plugin as PublicAPI.Chrome.DevTools.RecorderExtensionExportPlugin) |
| 740 | .stringify(request.parameters.recording); |
| 741 | case PrivateAPI.RecorderExtensionPluginCommands.StringifyStep: |
| 742 | return (plugin as PublicAPI.Chrome.DevTools.RecorderExtensionExportPlugin) |
| 743 | .stringifyStep(request.parameters.step); |
| 744 | case PrivateAPI.RecorderExtensionPluginCommands.Replay: |
| 745 | try { |
| 746 | userAction = true; |
| 747 | userRecorderAction = true; |
| 748 | return (plugin as PublicAPI.Chrome.DevTools.RecorderExtensionReplayPlugin) |
| 749 | .replay(request.parameters.recording); |
| 750 | } finally { |
| 751 | userAction = false; |
| 752 | userRecorderAction = false; |
| 753 | } |
| 754 | default: |
| 755 | // @ts-expect-error |
| 756 | throw new Error(`'${request.method}' is not recognized`); |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 757 | } |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 758 | } |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 759 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 760 | const capabilities: PrivateAPI.RecordingExtensionPluginCapability[] = []; |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 761 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 762 | if ('stringify' in plugin && 'stringifyStep' in plugin) { |
| 763 | capabilities.push('export'); |
| 764 | } |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 765 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 766 | if ('replay' in plugin) { |
| 767 | capabilities.push('replay'); |
| 768 | } |
| 769 | |
| 770 | await new Promise<void>(resolve => { |
| 771 | extensionServer.sendRequest( |
| 772 | { |
| 773 | command: PrivateAPI.Commands.RegisterRecorderExtensionPlugin, |
| 774 | pluginName, |
| 775 | mediaType, |
| 776 | capabilities, |
| 777 | port: channel.port2, |
| 778 | }, |
| 779 | () => resolve(), [channel.port2]); |
| 780 | }); |
| 781 | } |
| 782 | |
| 783 | (RecorderServicesAPIImpl.prototype as Pick< |
| 784 | APIImpl.RecorderExtensions, |
| 785 | 'registerRecorderExtensionPlugin'|'unregisterRecorderExtensionPlugin'|'createView'>) = { |
| 786 | registerRecorderExtensionPlugin: registerRecorderExtensionPluginImpl, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 787 | unregisterRecorderExtensionPlugin: async function( |
| 788 | this: APIImpl.RecorderExtensions, plugin: PublicAPI.Chrome.DevTools.RecorderExtensionPlugin): Promise<void> { |
| 789 | const port = this._plugins.get(plugin); |
| 790 | if (!port) { |
| 791 | throw new Error('Tried to unregister a plugin that was not previously registered'); |
| 792 | } |
| 793 | this._plugins.delete(plugin); |
| 794 | port.postMessage({event: PrivateAPI.RecorderExtensionPluginEvents.UnregisteredRecorderExtensionPlugin}); |
| 795 | port.close(); |
| 796 | }, |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 797 | createView: async function(this: APIImpl.RecorderExtensions, title: string, pagePath: string): |
| 798 | Promise<PublicAPI.Chrome.DevTools.RecorderView> { |
| 799 | const id = 'recorder-extension-view-' + extensionServer.nextObjectId(); |
| 800 | await new Promise(resolve => { |
| 801 | extensionServer.sendRequest( |
| 802 | {command: PrivateAPI.Commands.CreateRecorderView, id, title, pagePath}, resolve); |
| 803 | }); |
| 804 | return new (Constructor(RecorderView))(id); |
| 805 | }, |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 806 | }; |
| 807 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 808 | function LanguageServicesAPIImpl(this: APIImpl.LanguageExtensions): void { |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 809 | this._plugins = new Map(); |
| 810 | } |
| 811 | |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 812 | (LanguageServicesAPIImpl.prototype as PublicAPI.Chrome.DevTools.LanguageExtensions) = { |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 813 | registerLanguageExtensionPlugin: async function( |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 814 | this: APIImpl.LanguageExtensions, plugin: PublicAPI.Chrome.DevTools.LanguageExtensionPlugin, pluginName: string, |
| 815 | supportedScriptTypes: PublicAPI.Chrome.DevTools.SupportedScriptTypes): Promise<void> { |
| 816 | if (this._plugins.has(plugin)) { |
| 817 | throw new Error(`Tried to register plugin '${pluginName}' twice`); |
| 818 | } |
| 819 | const channel = new MessageChannel(); |
| 820 | const port = channel.port1; |
| 821 | this._plugins.set(plugin, port); |
| 822 | port.onmessage = ({data}: MessageEvent<{requestId: number}&PrivateAPI.LanguageExtensionRequests>): void => { |
| 823 | const {requestId} = data; |
| 824 | console.time(`${requestId}: ${data.method}`); |
| 825 | dispatchMethodCall(data) |
| 826 | .then(result => port.postMessage({requestId, result})) |
| 827 | .catch(error => port.postMessage({requestId, error: {message: error.message}})) |
| 828 | .finally(() => console.timeEnd(`${requestId}: ${data.method}`)); |
| 829 | }; |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 830 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 831 | function dispatchMethodCall(request: PrivateAPI.LanguageExtensionRequests): Promise<unknown> { |
| 832 | switch (request.method) { |
| 833 | case PrivateAPI.LanguageExtensionPluginCommands.AddRawModule: |
| 834 | return plugin.addRawModule( |
| 835 | request.parameters.rawModuleId, request.parameters.symbolsURL, request.parameters.rawModule); |
| 836 | case PrivateAPI.LanguageExtensionPluginCommands.RemoveRawModule: |
| 837 | return plugin.removeRawModule(request.parameters.rawModuleId); |
| 838 | case PrivateAPI.LanguageExtensionPluginCommands.SourceLocationToRawLocation: |
| 839 | return plugin.sourceLocationToRawLocation(request.parameters.sourceLocation); |
| 840 | case PrivateAPI.LanguageExtensionPluginCommands.RawLocationToSourceLocation: |
| 841 | return plugin.rawLocationToSourceLocation(request.parameters.rawLocation); |
| 842 | case PrivateAPI.LanguageExtensionPluginCommands.GetScopeInfo: |
| 843 | return plugin.getScopeInfo(request.parameters.type); |
| 844 | case PrivateAPI.LanguageExtensionPluginCommands.ListVariablesInScope: |
| 845 | return plugin.listVariablesInScope(request.parameters.rawLocation); |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 846 | case PrivateAPI.LanguageExtensionPluginCommands.GetFunctionInfo: |
| 847 | return plugin.getFunctionInfo(request.parameters.rawLocation); |
| 848 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedFunctionRanges: |
| 849 | return plugin.getInlinedFunctionRanges(request.parameters.rawLocation); |
| 850 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedCalleesRanges: |
| 851 | return plugin.getInlinedCalleesRanges(request.parameters.rawLocation); |
| 852 | case PrivateAPI.LanguageExtensionPluginCommands.GetMappedLines: |
| 853 | if ('getMappedLines' in plugin) { |
| 854 | return plugin.getMappedLines(request.parameters.rawModuleId, request.parameters.sourceFileURL); |
| 855 | } |
| 856 | return Promise.resolve(undefined); |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 857 | case PrivateAPI.LanguageExtensionPluginCommands.FormatValue: |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 858 | if ('evaluate' in plugin && plugin.evaluate) { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 859 | return plugin.evaluate( |
| 860 | request.parameters.expression, request.parameters.context, request.parameters.stopId); |
| 861 | } |
| 862 | return Promise.resolve(undefined); |
| 863 | case PrivateAPI.LanguageExtensionPluginCommands.GetProperties: |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 864 | if ('getProperties' in plugin && plugin.getProperties) { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 865 | return plugin.getProperties(request.parameters.objectId); |
| 866 | } |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 867 | if (!('evaluate' in plugin && |
| 868 | 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] | 869 | return Promise.resolve(undefined); |
| 870 | } |
| 871 | break; |
| 872 | case PrivateAPI.LanguageExtensionPluginCommands.ReleaseObject: |
Philip Pfaffe | 889342a | 2022-07-08 06:18:47 | [diff] [blame] | 873 | if ('releaseObject' in plugin && plugin.releaseObject) { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 874 | return plugin.releaseObject(request.parameters.objectId); |
| 875 | } |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 876 | break; |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 877 | } |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 878 | throw new Error(`Unknown language plugin method ${request.method}`); |
| 879 | } |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 880 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 881 | await new Promise<void>(resolve => { |
| 882 | extensionServer.sendRequest( |
| 883 | { |
| 884 | command: PrivateAPI.Commands.RegisterLanguageExtensionPlugin, |
| 885 | pluginName, |
| 886 | port: channel.port2, |
| 887 | supportedScriptTypes, |
| 888 | }, |
| 889 | () => resolve(), [channel.port2]); |
| 890 | }); |
| 891 | }, |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 892 | |
Philip Pfaffe | c5d160e | 2021-07-20 10:53:32 | [diff] [blame] | 893 | unregisterLanguageExtensionPlugin: async function( |
| 894 | this: APIImpl.LanguageExtensions, plugin: PublicAPI.Chrome.DevTools.LanguageExtensionPlugin): Promise<void> { |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 895 | const port = this._plugins.get(plugin); |
| 896 | if (!port) { |
| 897 | throw new Error('Tried to unregister a plugin that was not previously registered'); |
| 898 | } |
| 899 | this._plugins.delete(plugin); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 900 | port.postMessage({event: PrivateAPI.LanguageExtensionPluginEvents.UnregisteredLanguageExtensionPlugin}); |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 901 | port.close(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 902 | }, |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 903 | |
| 904 | getWasmLinearMemory: async function( |
| 905 | this: APIImpl.LanguageExtensions, offset: number, length: number, stopId: number): Promise<ArrayBuffer> { |
| 906 | const result = await new Promise( |
| 907 | resolve => extensionServer.sendRequest( |
| 908 | {command: PrivateAPI.Commands.GetWasmLinearMemory, offset, length, stopId}, resolve)); |
| 909 | if (Array.isArray(result)) { |
| 910 | return new Uint8Array(result).buffer; |
| 911 | } |
| 912 | return new ArrayBuffer(0); |
| 913 | }, |
| 914 | getWasmLocal: async function( |
| 915 | this: APIImpl.LanguageExtensions, local: number, stopId: number): Promise<PublicAPI.Chrome.DevTools.WasmValue> { |
| 916 | return new Promise( |
| 917 | resolve => extensionServer.sendRequest({command: PrivateAPI.Commands.GetWasmLocal, local, stopId}, resolve)); |
| 918 | }, |
| 919 | getWasmGlobal: async function(this: APIImpl.LanguageExtensions, global: number, stopId: number): |
| 920 | Promise<PublicAPI.Chrome.DevTools.WasmValue> { |
| 921 | return new Promise( |
| 922 | resolve => |
| 923 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetWasmGlobal, global, stopId}, resolve)); |
| 924 | }, |
| 925 | getWasmOp: async function(this: APIImpl.LanguageExtensions, op: number, stopId: number): |
| 926 | Promise<PublicAPI.Chrome.DevTools.WasmValue> { |
| 927 | return new Promise( |
| 928 | resolve => extensionServer.sendRequest({command: PrivateAPI.Commands.GetWasmOp, op, stopId}, resolve)); |
| 929 | }, |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 930 | }; |
| 931 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 932 | function declareInterfaceClass<ImplT extends APIImpl.Callable>(implConstructor: ImplT): ( |
| 933 | this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>) => void { |
| 934 | return function(this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 935 | const impl = {__proto__: implConstructor.prototype}; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 936 | implConstructor.apply(impl, args); |
| 937 | populateInterfaceClass(this as {[key: string]: unknown}, impl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 938 | }; |
| 939 | } |
| 940 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 941 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 942 | function defineDeprecatedProperty(object: any, className: string, oldName: string, newName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 943 | let warningGiven = false; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 944 | function getter(): unknown { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 945 | if (!warningGiven) { |
| 946 | console.warn(className + '.' + oldName + ' is deprecated. Use ' + className + '.' + newName + ' instead'); |
| 947 | warningGiven = true; |
| 948 | } |
| 949 | return object[newName]; |
| 950 | } |
| 951 | object.__defineGetter__(oldName, getter); |
| 952 | } |
| 953 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 954 | function extractCallbackArgument(args: IArguments): ((...args: unknown[]) => unknown)|undefined { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 955 | const lastArgument = args[args.length - 1]; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 956 | return typeof lastArgument === 'function' ? lastArgument as (...args: unknown[]) => unknown : undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 957 | } |
| 958 | |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 959 | const LanguageServicesAPI = declareInterfaceClass(LanguageServicesAPIImpl); |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 960 | const RecorderServicesAPI = declareInterfaceClass(RecorderServicesAPIImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 961 | const Button = declareInterfaceClass(ButtonImpl); |
| 962 | const EventSink = declareInterfaceClass(EventSinkImpl); |
| 963 | const ExtensionPanel = declareInterfaceClass(ExtensionPanelImpl); |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 964 | const RecorderView = declareInterfaceClass(RecorderViewImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 965 | const ExtensionSidebarPane = declareInterfaceClass(ExtensionSidebarPaneImpl); |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 966 | const PanelWithSidebarClass = declareInterfaceClass(PanelWithSidebarImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 967 | const Request = declareInterfaceClass(RequestImpl); |
| 968 | const Resource = declareInterfaceClass(ResourceImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 969 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 970 | class ElementsPanel extends (Constructor(PanelWithSidebarClass)) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 971 | constructor() { |
| 972 | super('elements'); |
| 973 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 974 | } |
| 975 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 976 | class SourcesPanel extends (Constructor(PanelWithSidebarClass)) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 977 | constructor() { |
| 978 | super('sources'); |
| 979 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 980 | } |
| 981 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 982 | function ExtensionPanelImpl(this: APIImpl.ExtensionPanel, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 983 | ExtensionViewImpl.call(this, id); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 984 | |
| 985 | this.onSearch = new (Constructor(EventSink))(PrivateAPI.Events.PanelSearch + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 986 | } |
| 987 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 988 | (ExtensionPanelImpl.prototype as Pick<APIImpl.ExtensionPanel, 'createStatusBarButton'|'show'>& |
| 989 | {__proto__: APIImpl.ExtensionView}) = { |
| 990 | createStatusBarButton: function( |
| 991 | this: APIImpl.ExtensionPanel, iconPath: string, tooltipText: string, disabled: boolean): |
| 992 | PublicAPI.Chrome.DevTools.Button { |
| 993 | const id = 'button-' + extensionServer.nextObjectId(); |
| 994 | extensionServer.sendRequest({ |
| 995 | command: PrivateAPI.Commands.CreateToolbarButton, |
| 996 | panel: this._id as string, |
| 997 | id: id, |
| 998 | icon: iconPath, |
| 999 | tooltip: tooltipText, |
| 1000 | disabled: Boolean(disabled), |
| 1001 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1002 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1003 | return new (Constructor(Button))(id); |
| 1004 | }, |
| 1005 | |
| 1006 | show: function(this: APIImpl.ExtensionPanel): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1007 | if (!userAction) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1008 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1009 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1010 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1011 | extensionServer.sendRequest({command: PrivateAPI.Commands.ShowPanel, id: this._id as string}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1012 | }, |
| 1013 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1014 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1015 | }; |
| 1016 | |
Alex Rudenko | 0b8b888 | 2023-01-17 12:21:49 | [diff] [blame] | 1017 | function RecorderViewImpl(this: APIImpl.RecorderView, id: string): void { |
| 1018 | ExtensionViewImpl.call(this, id); |
| 1019 | } |
| 1020 | |
| 1021 | (RecorderViewImpl.prototype as Pick<APIImpl.RecorderView, 'show'>& {__proto__: APIImpl.ExtensionView}) = { |
| 1022 | show: function(this: APIImpl.RecorderView): void { |
| 1023 | if (!userAction || !userRecorderAction) { |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | extensionServer.sendRequest({command: PrivateAPI.Commands.ShowRecorderView, id: this._id as string}); |
| 1028 | }, |
| 1029 | |
| 1030 | __proto__: ExtensionViewImpl.prototype, |
| 1031 | }; |
| 1032 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1033 | function ExtensionSidebarPaneImpl(this: APIImpl.ExtensionSidebarPane, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1034 | ExtensionViewImpl.call(this, id); |
| 1035 | } |
| 1036 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1037 | (ExtensionSidebarPaneImpl.prototype as |
| 1038 | Pick<APIImpl.ExtensionSidebarPane, 'setHeight'|'setExpression'|'setObject'|'setPage'>& |
| 1039 | {__proto__: APIImpl.ExtensionView}) = { |
| 1040 | setHeight: function(this: APIImpl.ExtensionSidebarPane, height: string): void { |
| 1041 | extensionServer.sendRequest( |
| 1042 | {command: PrivateAPI.Commands.SetSidebarHeight, id: this._id as string, height: height}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1043 | }, |
| 1044 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1045 | setExpression: function( |
| 1046 | this: APIImpl.ExtensionSidebarPane, expression: string, rootTitle: string, |
| 1047 | evaluateOptions?: PrivateAPI.EvaluateOptions, _callback?: () => unknown): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1048 | extensionServer.sendRequest( |
| 1049 | { |
| 1050 | command: PrivateAPI.Commands.SetSidebarContent, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1051 | id: this._id as string, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1052 | expression: expression, |
| 1053 | rootTitle: rootTitle, |
| 1054 | evaluateOnPage: true, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1055 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : {}), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1056 | }, |
| 1057 | extractCallbackArgument(arguments)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1058 | }, |
| 1059 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1060 | setObject: function( |
| 1061 | this: APIImpl.ExtensionSidebarPane, jsonObject: string, rootTitle?: string, callback?: () => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1062 | extensionServer.sendRequest( |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1063 | { |
| 1064 | command: PrivateAPI.Commands.SetSidebarContent, |
| 1065 | id: this._id as string, |
| 1066 | expression: jsonObject, |
| 1067 | rootTitle: rootTitle, |
| 1068 | }, |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1069 | callback); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1070 | }, |
| 1071 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1072 | setPage: function(this: APIImpl.ExtensionSidebarPane, page: string): void { |
| 1073 | extensionServer.sendRequest({command: PrivateAPI.Commands.SetSidebarPage, id: this._id as string, page: page}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1074 | }, |
| 1075 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1076 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1077 | }; |
| 1078 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1079 | function ButtonImpl(this: APIImpl.Button, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1080 | this._id = id; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1081 | |
| 1082 | this.onClicked = new (Constructor(EventSink))(PrivateAPI.Events.ButtonClicked + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1083 | } |
| 1084 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1085 | (ButtonImpl.prototype as Pick<APIImpl.Button, 'update'>) = { |
| 1086 | update: function(this: APIImpl.Button, iconPath?: string, tooltipText?: string, disabled?: boolean): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1087 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1088 | command: PrivateAPI.Commands.UpdateButton, |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1089 | id: this._id, |
| 1090 | icon: iconPath, |
| 1091 | tooltip: tooltipText, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1092 | disabled: Boolean(disabled), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1093 | }); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1094 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1095 | }; |
| 1096 | |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1097 | function canAccessResource(resource: APIImpl.ResourceData): boolean { |
Danil Somsikov | b2e3705 | 2023-03-24 12:50:43 | [diff] [blame] | 1098 | try { |
| 1099 | return extensionInfo.allowFileAccess || (new URL(resource.url)).protocol !== 'file:'; |
| 1100 | } catch (e) { |
| 1101 | return false; |
| 1102 | } |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1103 | } |
| 1104 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1105 | function InspectedWindow(this: PublicAPI.Chrome.DevTools.InspectedWindow): void { |
| 1106 | function dispatchResourceEvent( |
| 1107 | this: APIImpl.EventSink<(resource: APIImpl.Resource) => unknown>, message: {arguments: unknown[]}): void { |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1108 | const resourceData = message.arguments[0] as APIImpl.ResourceData; |
| 1109 | if (!canAccessResource(resourceData)) { |
| 1110 | return; |
| 1111 | } |
| 1112 | this._fire(new (Constructor(Resource))(resourceData)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1113 | } |
| 1114 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1115 | function dispatchResourceContentEvent( |
| 1116 | this: APIImpl.EventSink<(resource: APIImpl.Resource, content: string) => unknown>, |
| 1117 | message: {arguments: unknown[]}): void { |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1118 | const resourceData = message.arguments[0] as APIImpl.ResourceData; |
| 1119 | if (!canAccessResource(resourceData)) { |
| 1120 | return; |
| 1121 | } |
| 1122 | this._fire(new (Constructor(Resource))(resourceData), message.arguments[1] as string); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1123 | } |
| 1124 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1125 | this.onResourceAdded = new (Constructor(EventSink))(PrivateAPI.Events.ResourceAdded, dispatchResourceEvent); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1126 | this.onResourceContentCommitted = |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1127 | new (Constructor(EventSink))(PrivateAPI.Events.ResourceContentCommitted, dispatchResourceContentEvent); |
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 | (InspectedWindow.prototype as Pick<PublicAPI.Chrome.DevTools.InspectedWindow, 'reload'|'eval'|'getResources'>) = { |
| 1131 | reload: function(optionsOrUserAgent: { |
| 1132 | ignoreCache?: boolean, |
| 1133 | injectedScript?: string, |
| 1134 | userAgent?: string, |
| 1135 | }): void { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1136 | let options: { |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1137 | ignoreCache?: boolean, |
| 1138 | injectedScript?: string, |
| 1139 | userAgent?: string, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1140 | }|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1141 | if (typeof optionsOrUserAgent === 'object') { |
| 1142 | options = optionsOrUserAgent; |
| 1143 | } else if (typeof optionsOrUserAgent === 'string') { |
| 1144 | options = {userAgent: optionsOrUserAgent}; |
| 1145 | console.warn( |
| 1146 | 'Passing userAgent as string parameter to inspectedWindow.reload() is deprecated. ' + |
| 1147 | 'Use inspectedWindow.reload({ userAgent: value}) instead.'); |
| 1148 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1149 | extensionServer.sendRequest({command: PrivateAPI.Commands.Reload, options: options}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1150 | }, |
| 1151 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1152 | eval: function( |
| 1153 | expression: string, |
Benedikt Meurer | 6428bce | 2023-09-15 10:47:33 | [diff] [blame] | 1154 | evaluateOptions: {scriptExecutionContext?: string, frameURL?: string, useContentScriptContext?: boolean}): |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1155 | Object | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1156 | null { |
| 1157 | const callback = extractCallbackArgument(arguments); |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1158 | function callbackWrapper(result: unknown): void { |
| 1159 | const {isError, isException, value} = result as { |
| 1160 | isError?: boolean, |
| 1161 | isException?: boolean, value: unknown, |
| 1162 | }; |
| 1163 | if (isError || isException) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1164 | callback && callback(undefined, result); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1165 | } else { |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1166 | callback && callback(value); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1167 | } |
| 1168 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1169 | extensionServer.sendRequest( |
| 1170 | { |
| 1171 | command: PrivateAPI.Commands.EvaluateOnInspectedPage, |
| 1172 | expression: expression, |
| 1173 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : undefined), |
| 1174 | }, |
| 1175 | callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1176 | return null; |
| 1177 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1178 | |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1179 | getResources: function(callback?: (resources: PublicAPI.Chrome.DevTools.Resource[]) => unknown): void { |
| 1180 | function wrapResource(resourceData: APIImpl.ResourceData): APIImpl.Resource { |
| 1181 | return new (Constructor(Resource))(resourceData); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1182 | } |
Philip Pfaffe | ddf60d2 | 2021-06-25 12:35:41 | [diff] [blame] | 1183 | function callbackWrapper(resources: unknown): void { |
Danil Somsikov | f8035f0 | 2023-07-17 14:05:41 | [diff] [blame] | 1184 | callback && callback((resources as APIImpl.ResourceData[]).filter(canAccessResource).map(wrapResource)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1185 | } |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1186 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetPageResources}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1187 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1188 | }; |
| 1189 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1190 | function ResourceImpl(this: APIImpl.Resource, resourceData: APIImpl.ResourceData): void { |
Danil Somsikov | f8035f0 | 2023-07-17 14:05:41 | [diff] [blame] | 1191 | if (!canAccessResource(resourceData)) { |
Danil Somsikov | 5621083 | 2022-10-05 13:17:58 | [diff] [blame] | 1192 | throw new Error('Resource access not allowed'); |
| 1193 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1194 | this._url = resourceData.url; |
| 1195 | this._type = resourceData.type; |
| 1196 | } |
| 1197 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1198 | (ResourceImpl.prototype as Pick<APIImpl.Resource, 'url'|'type'|'getContent'|'setContent'>) = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1199 | get url(): string { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1200 | return (this as APIImpl.Resource)._url; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1201 | }, |
| 1202 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1203 | get type(): string { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1204 | return (this as APIImpl.Resource)._type; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1205 | }, |
| 1206 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1207 | getContent: function(this: APIImpl.Resource, callback?: (content: string, encoding: string) => unknown): void { |
| 1208 | function callbackWrapper(response: unknown): void { |
| 1209 | const {content, encoding} = response as {content: string, encoding: string}; |
| 1210 | callback && callback(content, encoding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1211 | } |
| 1212 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1213 | extensionServer.sendRequest( |
| 1214 | {command: PrivateAPI.Commands.GetResourceContent, url: this._url}, callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1215 | }, |
| 1216 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1217 | setContent: function( |
| 1218 | this: APIImpl.Resource, content: string, commit: boolean, callback: (error?: Object) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1219 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1220 | {command: PrivateAPI.Commands.SetResourceContent, url: this._url, content: content, commit: commit}, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 1221 | callback as (response: unknown) => unknown); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1222 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1223 | }; |
| 1224 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1225 | function getTabId(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1226 | return inspectedTabId; |
| 1227 | } |
| 1228 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1229 | let keyboardEventRequestQueue: KeyboardEventInit&{eventType: string}[] = []; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1230 | let forwardTimer: number|null = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1231 | function forwardKeyboardEvent(event: KeyboardEvent): void { |
Jan Scheffler | e7d7bb1 | 2019-10-24 09:18:52 | [diff] [blame] | 1232 | // Check if the event should be forwarded. |
| 1233 | // This is a workaround for crbug.com/923338. |
| 1234 | const focused = document.activeElement; |
| 1235 | if (focused) { |
Benedikt Meurer | 5d508f6 | 2022-12-06 14:37:38 | [diff] [blame] | 1236 | const isInput = |
| 1237 | focused.nodeName === 'INPUT' || focused.nodeName === 'TEXTAREA' || (focused as HTMLElement).isContentEditable; |
Jan Scheffler | e7d7bb1 | 2019-10-24 09:18:52 | [diff] [blame] | 1238 | if (isInput && !(event.ctrlKey || event.altKey || event.metaKey)) { |
| 1239 | return; |
| 1240 | } |
| 1241 | } |
| 1242 | |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1243 | let modifiers = 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1244 | if (event.shiftKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1245 | modifiers |= 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1246 | } |
| 1247 | if (event.ctrlKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1248 | modifiers |= 2; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1249 | } |
| 1250 | if (event.altKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1251 | modifiers |= 4; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1252 | } |
| 1253 | if (event.metaKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1254 | modifiers |= 8; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1255 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1256 | const num = (event.keyCode & 255) | (modifiers << 8); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1257 | // We only care about global hotkeys, not about random text |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1258 | if (!keysToForwardSet.has(num)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1259 | return; |
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 | event.preventDefault(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1262 | const requestPayload = { |
| 1263 | eventType: event.type, |
| 1264 | ctrlKey: event.ctrlKey, |
| 1265 | altKey: event.altKey, |
| 1266 | metaKey: event.metaKey, |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1267 | shiftKey: event.shiftKey, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1268 | // @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] | 1269 | keyIdentifier: event.keyIdentifier, |
| 1270 | key: event.key, |
| 1271 | code: event.code, |
| 1272 | location: event.location, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1273 | keyCode: event.keyCode, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1274 | }; |
| 1275 | keyboardEventRequestQueue.push(requestPayload); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1276 | if (!forwardTimer) { |
Tim van der Lippe | 6bcbe0f | 2022-01-11 13:10:31 | [diff] [blame] | 1277 | forwardTimer = window.setTimeout(forwardEventQueue, 0); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1278 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1279 | } |
| 1280 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1281 | function forwardEventQueue(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1282 | forwardTimer = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1283 | extensionServer.sendRequest( |
| 1284 | {command: PrivateAPI.Commands.ForwardKeyboardEvent, entries: keyboardEventRequestQueue}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1285 | keyboardEventRequestQueue = []; |
| 1286 | } |
| 1287 | |
| 1288 | document.addEventListener('keydown', forwardKeyboardEvent, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1289 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1290 | function ExtensionServerClient(this: APIImpl.ExtensionServerClient, targetWindow: Window): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1291 | this._callbacks = {}; |
| 1292 | this._handlers = {}; |
| 1293 | this._lastRequestId = 0; |
| 1294 | this._lastObjectId = 0; |
| 1295 | |
| 1296 | this.registerHandler('callback', this._onCallback.bind(this)); |
| 1297 | |
| 1298 | const channel = new MessageChannel(); |
| 1299 | this._port = channel.port1; |
| 1300 | this._port.addEventListener('message', this._onMessage.bind(this), false); |
| 1301 | this._port.start(); |
| 1302 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1303 | targetWindow.postMessage('registerExtension', '*', [channel.port2]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1304 | } |
| 1305 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1306 | (ExtensionServerClient.prototype as Pick< |
| 1307 | APIImpl.ExtensionServerClient, |
| 1308 | 'sendRequest'|'hasHandler'|'registerHandler'|'unregisterHandler'|'nextObjectId'|'_registerCallback'| |
| 1309 | '_onCallback'|'_onMessage'>) = { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 1310 | sendRequest: function<ResponseT>( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1311 | this: APIImpl.ExtensionServerClient, message: PrivateAPI.ServerRequests, |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 1312 | callback?: (response: ResponseT) => unknown, transfers?: Transferable[]): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1313 | if (typeof callback === 'function') { |
Philip Pfaffe | 6ed0126 | 2022-07-06 10:41:39 | [diff] [blame] | 1314 | (message as PrivateAPI.ExtensionServerRequestMessage).requestId = |
| 1315 | this._registerCallback(callback as (response: unknown) => unknown); |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1316 | } |
| 1317 | // @ts-expect-error |
| 1318 | this._port.postMessage(message, transfers); |
| 1319 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1320 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1321 | hasHandler: function(this: APIImpl.ExtensionServerClient, command: string): boolean { |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1322 | return Boolean(this._handlers[command]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1323 | }, |
| 1324 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1325 | registerHandler: function( |
| 1326 | this: APIImpl.ExtensionServerClient, command: string, handler: (request: {arguments: unknown[]}) => unknown): |
| 1327 | void { |
| 1328 | this._handlers[command] = handler; |
| 1329 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1330 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1331 | unregisterHandler: function(this: APIImpl.ExtensionServerClient, command: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1332 | delete this._handlers[command]; |
| 1333 | }, |
| 1334 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1335 | nextObjectId: function(this: APIImpl.ExtensionServerClient): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1336 | return injectedScriptId.toString() + '_' + ++this._lastObjectId; |
| 1337 | }, |
| 1338 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1339 | _registerCallback: function(this: APIImpl.ExtensionServerClient, callback: (response: unknown) => unknown): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1340 | const id = ++this._lastRequestId; |
| 1341 | this._callbacks[id] = callback; |
| 1342 | return id; |
| 1343 | }, |
| 1344 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1345 | _onCallback: function(this: APIImpl.ExtensionServerClient, request: {requestId: number, result: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1346 | if (request.requestId in this._callbacks) { |
| 1347 | const callback = this._callbacks[request.requestId]; |
| 1348 | delete this._callbacks[request.requestId]; |
| 1349 | callback(request.result); |
| 1350 | } |
| 1351 | }, |
| 1352 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1353 | _onMessage: function( |
| 1354 | this: APIImpl.ExtensionServerClient, |
| 1355 | event: MessageEvent<{command: string, requestId: number, arguments: unknown[]}>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1356 | const request = event.data; |
| 1357 | const handler = this._handlers[request.command]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1358 | if (handler) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1359 | handler.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1360 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1361 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1362 | }; |
| 1363 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1364 | function populateInterfaceClass(interfaze: {[key: string]: unknown}, implementation: {[key: string]: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1365 | for (const member in implementation) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1366 | if (member.charAt(0) === '_') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1367 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1368 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1369 | let descriptor: (PropertyDescriptor|undefined)|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1370 | // Traverse prototype chain until we find the owner. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1371 | for (let owner = implementation; owner && !descriptor; owner = owner.__proto__ as {[key: string]: unknown}) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1372 | descriptor = Object.getOwnPropertyDescriptor(owner, member); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1373 | } |
| 1374 | if (!descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1375 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1376 | } |
| 1377 | if (typeof descriptor.value === 'function') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1378 | interfaze[member] = descriptor.value.bind(implementation); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1379 | } else if (typeof descriptor.get === 'function') { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1380 | // @ts-expect-error |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1381 | interfaze.__defineGetter__(member, descriptor.get.bind(implementation)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1382 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1383 | Object.defineProperty(interfaze, member, descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1384 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1388 | const extensionServer = new (Constructor(ExtensionServerClient))(targetWindowForTest || window.parent); |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1389 | |
| 1390 | const coreAPI = new (Constructor(InspectorExtensionAPI))(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1391 | |
| 1392 | Object.defineProperty(chrome, 'devtools', {value: {}, enumerable: true}); |
| 1393 | |
| 1394 | // Only expose tabId on chrome.devtools.inspectedWindow, not webInspector.inspectedWindow. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1395 | // @ts-expect-error |
| 1396 | chrome.devtools!.inspectedWindow = {}; |
| 1397 | Object.defineProperty(chrome.devtools!.inspectedWindow, 'tabId', {get: getTabId}); |
| 1398 | // @ts-expect-error |
| 1399 | chrome.devtools!.inspectedWindow.__proto__ = coreAPI.inspectedWindow; |
| 1400 | chrome.devtools!.network = coreAPI.network; |
| 1401 | chrome.devtools!.panels = coreAPI.panels; |
| 1402 | chrome.devtools!.panels.themeName = themeName; |
| 1403 | chrome.devtools!.languageServices = coreAPI.languageServices; |
Alex Rudenko | a385082 | 2022-05-24 07:34:22 | [diff] [blame] | 1404 | chrome.devtools!.recorder = coreAPI.recorder; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1405 | |
| 1406 | // default to expose experimental APIs for now. |
| 1407 | if (extensionInfo.exposeExperimentalAPIs !== false) { |
| 1408 | chrome.experimental = chrome.experimental || {}; |
| 1409 | chrome.experimental.devtools = chrome.experimental.devtools || {}; |
| 1410 | |
| 1411 | const properties = Object.getOwnPropertyNames(coreAPI); |
| 1412 | for (let i = 0; i < properties.length; ++i) { |
| 1413 | const descriptor = Object.getOwnPropertyDescriptor(coreAPI, properties[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1414 | if (descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1415 | Object.defineProperty(chrome.experimental.devtools, properties[i], descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1416 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1417 | } |
| 1418 | chrome.experimental.devtools.inspectedWindow = chrome.devtools.inspectedWindow; |
| 1419 | } |
| 1420 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1421 | if (extensionInfo.exposeWebInspectorNamespace) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1422 | window.webInspector = coreAPI; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1423 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1424 | testHook(extensionServer, coreAPI); |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1425 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1426 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1427 | self.buildExtensionAPIInjectedScript = function( |
| 1428 | extensionInfo: { |
| 1429 | startPage: string, |
| 1430 | name: string, |
| 1431 | exposeExperimentalAPIs: boolean, |
| 1432 | }, |
| 1433 | inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1434 | testHook: |
| 1435 | ((extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown)| |
| 1436 | undefined): string { |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 1437 | const argumentsJSON = |
| 1438 | [extensionInfo, inspectedTabId || null, themeName, keysToForward].map(_ => JSON.stringify(_)).join(','); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1439 | if (!testHook) { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1440 | testHook = (): void => {}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1441 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1442 | return '(function(injectedScriptId){ ' + |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1443 | '(' + self.injectedExtensionAPI.toString() + ')(' + argumentsJSON + ',' + testHook + ', injectedScriptId);' + |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1444 | '})'; |
Tim van der Lippe | 29fab47 | 2019-08-15 14:46:48 | [diff] [blame] | 1445 | }; |