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 |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 32 | import type * as HAR from '../har/har.js'; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 33 | |
| 34 | /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/naming-convention,@typescript-eslint/no-non-null-assertion */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 35 | export namespace PrivateAPI { |
| 36 | export namespace Panels { |
| 37 | export const enum SearchAction { |
| 38 | CancelSearch = 'cancelSearch', |
| 39 | PerformSearch = 'performSearch', |
| 40 | NextSearchResult = 'nextSearchResult', |
| 41 | PreviousSearchResult = 'previousSearchResult', |
| 42 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 43 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 45 | export const enum Events { |
| 46 | ButtonClicked = 'button-clicked-', |
| 47 | PanelObjectSelected = 'panel-objectSelected-', |
| 48 | InspectedURLChanged = 'inspected-url-changed', |
| 49 | NetworkRequestFinished = 'network-request-finished', |
| 50 | OpenResource = 'open-resource', |
| 51 | PanelSearch = 'panel-search-', |
| 52 | RecordingStarted = 'trace-recording-started-', |
| 53 | RecordingStopped = 'trace-recording-stopped-', |
| 54 | ResourceAdded = 'resource-added', |
| 55 | ResourceContentCommitted = 'resource-content-committed', |
| 56 | ViewShown = 'view-shown-', |
| 57 | ViewHidden = 'view-hidden,', |
| 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', |
| 62 | AddTraceProvider = 'addTraceProvider', |
| 63 | ApplyStyleSheet = 'applyStyleSheet', |
| 64 | CompleteTraceSession = 'completeTra.eSession', |
| 65 | CreatePanel = 'createPanel', |
| 66 | CreateSidebarPane = 'createSidebarPane', |
| 67 | CreateToolbarButton = 'createToolbarButton', |
| 68 | EvaluateOnInspectedPage = 'evaluateOnInspectedPage', |
| 69 | ForwardKeyboardEvent = '_forwardKeyboardEvent', |
| 70 | GetHAR = 'getHAR', |
| 71 | GetPageResources = 'getPageResources', |
| 72 | GetRequestContent = 'getRequestContent', |
| 73 | GetResourceContent = 'getResourceContent', |
| 74 | OpenResource = 'openResource', |
| 75 | Reload = 'Reload', |
| 76 | Subscribe = 'subscribe', |
| 77 | SetOpenResourceHandler = 'setOpenResourceHandler', |
| 78 | SetResourceContent = 'setResourceContent', |
| 79 | SetSidebarContent = 'setSidebarContent', |
| 80 | SetSidebarHeight = 'setSidebarHeight', |
| 81 | SetSidebarPage = 'setSidebarPage', |
| 82 | ShowPanel = 'showPanel', |
| 83 | Unsubscribe = 'unsubscribe', |
| 84 | UpdateButton = 'updateButton', |
| 85 | RegisterLanguageExtensionPlugin = 'registerLanguageExtensionPlugin', |
| 86 | } |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 87 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 88 | export const enum LanguageExtensionPluginCommands { |
| 89 | AddRawModule = 'addRawModule', |
| 90 | RemoveRawModule = 'removeRawModule', |
| 91 | SourceLocationToRawLocation = 'sourceLocationToRawLocation', |
| 92 | RawLocationToSourceLocation = 'rawLocationToSourceLocation', |
| 93 | GetScopeInfo = 'getScopeInfo', |
| 94 | ListVariablesInScope = 'listVariablesInScope', |
| 95 | GetTypeInfo = 'getTypeInfo', |
| 96 | GetFormatter = 'getFormatter', |
| 97 | GetInspectableAddress = 'getInspectableAddress', |
| 98 | GetFunctionInfo = 'getFunctionInfo', |
| 99 | GetInlinedFunctionRanges = 'getInlinedFunctionRanges', |
| 100 | GetInlinedCalleesRanges = 'getInlinedCalleesRanges', |
| 101 | GetMappedLines = 'getMappedLines', |
| 102 | } |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 103 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 104 | export const enum LanguageExtensionPluginEvents { |
| 105 | UnregisteredLanguageExtensionPlugin = 'unregisteredLanguageExtensionPlugin', |
| 106 | } |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 107 | |
| 108 | export interface EvaluateOptions { |
| 109 | frameURL?: string; |
| 110 | useContentScriptContext?: boolean; |
| 111 | scriptExecutionContext?: string; |
| 112 | } |
| 113 | |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 114 | type RegisterLanguageExtensionPluginRequest = { |
| 115 | command: Commands.RegisterLanguageExtensionPlugin, |
| 116 | pluginName: string, |
| 117 | port: MessagePort, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 118 | supportedScriptTypes: PublicAPI.Chrome.DevTools.SupportedScriptTypes, |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 119 | }; |
| 120 | type SubscribeRequest = {command: Commands.Subscribe, type: string}; |
| 121 | type UnsubscribeRequest = {command: Commands.Unsubscribe, type: string}; |
| 122 | type AddRequestHeadersRequest = { |
| 123 | command: Commands.AddRequestHeaders, |
| 124 | extensionId: string, |
| 125 | headers: {[key: string]: string}, |
| 126 | }; |
| 127 | type ApplyStyleSheetRequest = {command: Commands.ApplyStyleSheet, styleSheet: string}; |
| 128 | type CreatePanelRequest = {command: Commands.CreatePanel, id: string, title: string, page: string}; |
| 129 | type ShowPanelRequest = {command: Commands.ShowPanel, id: string}; |
| 130 | type CreateToolbarButtonRequest = { |
| 131 | command: Commands.CreateToolbarButton, |
| 132 | id: string, |
| 133 | icon: string, |
| 134 | panel: string, |
| 135 | tooltip?: string, |
| 136 | disabled?: boolean, |
| 137 | }; |
| 138 | type UpdateButtonRequest = |
| 139 | {command: Commands.UpdateButton, id: string, icon?: string, tooltip?: string, disabled?: boolean}; |
| 140 | type CompleteTraceSessionRequest = |
| 141 | {command: Commands.CompleteTraceSession, id: string, url: string, timeOffset: number}; |
| 142 | type CreateSidebarPaneRequest = {command: Commands.CreateSidebarPane, id: string, panel: string, title: string}; |
| 143 | type SetSidebarHeightRequest = {command: Commands.SetSidebarHeight, id: string, height: string}; |
| 144 | type SetSidebarContentRequest = { |
| 145 | command: Commands.SetSidebarContent, |
| 146 | id: string, |
| 147 | evaluateOnPage?: boolean, expression: string, |
| 148 | rootTitle?: string, |
| 149 | evaluateOptions?: EvaluateOptions, |
| 150 | }; |
| 151 | type SetSidebarPageRequest = {command: Commands.SetSidebarPage, id: string, page: string}; |
| 152 | type OpenResourceRequest = {command: Commands.OpenResource, url: string, lineNumber: number}; |
| 153 | type SetOpenResourceHandlerRequest = {command: Commands.SetOpenResourceHandler, handlerPresent: boolean}; |
| 154 | type ReloadRequest = { |
| 155 | command: Commands.Reload, |
| 156 | options: null|{ |
| 157 | userAgent?: string, |
| 158 | injectedScript?: string, |
| 159 | ignoreCache?: boolean, |
| 160 | }, |
| 161 | }; |
| 162 | type EvaluateOnInspectedPageRequest = { |
| 163 | command: Commands.EvaluateOnInspectedPage, |
| 164 | expression: string, |
| 165 | evaluateOptions?: EvaluateOptions, |
| 166 | }; |
| 167 | type GetRequestContentRequest = {command: Commands.GetRequestContent, id: number}; |
| 168 | type GetResourceContentRequest = {command: Commands.GetResourceContent, url: string}; |
| 169 | type SetResourceContentRequest = |
| 170 | {command: Commands.SetResourceContent, url: string, content: string, commit: boolean}; |
| 171 | type AddTraceProviderRequest = |
| 172 | {command: Commands.AddTraceProvider, id: string, categoryName: string, categoryTooltip: string}; |
| 173 | type ForwardKeyboardEventRequest = { |
| 174 | command: Commands.ForwardKeyboardEvent, |
| 175 | entries: Array<KeyboardEventInit&{eventType: string}>, |
| 176 | }; |
| 177 | type GetHARRequest = {command: Commands.GetHAR}; |
| 178 | type GetPageResourcesRequest = {command: Commands.GetPageResources}; |
| 179 | |
| 180 | export type ServerRequests = RegisterLanguageExtensionPluginRequest|SubscribeRequest|UnsubscribeRequest| |
| 181 | AddRequestHeadersRequest|ApplyStyleSheetRequest|CreatePanelRequest|ShowPanelRequest|CreateToolbarButtonRequest| |
| 182 | UpdateButtonRequest|CompleteTraceSessionRequest|CreateSidebarPaneRequest|SetSidebarHeightRequest| |
| 183 | SetSidebarContentRequest|SetSidebarPageRequest|OpenResourceRequest|SetOpenResourceHandlerRequest|ReloadRequest| |
| 184 | EvaluateOnInspectedPageRequest|GetRequestContentRequest|GetResourceContentRequest|SetResourceContentRequest| |
| 185 | AddTraceProviderRequest|ForwardKeyboardEventRequest|GetHARRequest|GetPageResourcesRequest; |
| 186 | export type ExtensionServerRequestMessage = PrivateAPI.ServerRequests&{requestId?: number}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 187 | } |
| 188 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 189 | declare global { |
| 190 | interface Window { |
| 191 | injectedExtensionAPI: |
| 192 | (extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 193 | testHook: |
| 194 | (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown, |
| 195 | injectedScriptId: number) => void; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 196 | buildExtensionAPIInjectedScript( |
| 197 | extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 198 | testHook: undefined|((extensionServer: unknown, extensionAPI: unknown) => unknown)): string; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 199 | chrome: PublicAPI.Chrome.DevTools.Chrome; |
| 200 | webInspector?: APIImpl.InspectorExtensionAPI; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | export type ExtensionDescriptor = { |
| 205 | startPage: string, |
| 206 | name: string, |
| 207 | exposeExperimentalAPIs: boolean, |
| 208 | exposeWebInspectorNamespace?: boolean, |
| 209 | }; |
| 210 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 211 | namespace APIImpl { |
| 212 | export interface InspectorExtensionAPI { |
| 213 | languageServices: PublicAPI.Chrome.DevTools.LanguageExtensions; |
| 214 | network: PublicAPI.Chrome.DevTools.Network; |
| 215 | panels: PublicAPI.Chrome.DevTools.Panels; |
| 216 | inspectedWindow: PublicAPI.Chrome.DevTools.InspectedWindow; |
| 217 | } |
| 218 | |
| 219 | export interface ExtensionServerClient { |
| 220 | _callbacks: {[key: string]: (response: unknown) => unknown}; |
| 221 | _handlers: {[key: string]: (request: {arguments: unknown[]}) => unknown}; |
| 222 | _lastRequestId: number; |
| 223 | _lastObjectId: number; |
| 224 | _port: MessagePort; |
| 225 | |
| 226 | _onCallback(request: unknown): void; |
| 227 | _onMessage(event: MessageEvent<{command: string, requestId: number, arguments: unknown[]}>): void; |
| 228 | _registerCallback(callback: (response: unknown) => unknown): number; |
| 229 | registerHandler(command: string, handler: (request: {arguments: unknown[]}) => unknown): void; |
| 230 | unregisterHandler(command: string): void; |
| 231 | hasHandler(command: string): boolean; |
| 232 | sendRequest(request: PrivateAPI.ServerRequests, callback?: ((response: unknown) => unknown), transfers?: unknown[]): |
| 233 | void; |
| 234 | nextObjectId(): string; |
| 235 | } |
| 236 | |
| 237 | // We cannot use the stronger `unknown` type in place of `any` in the following type definition. The type is used as |
| 238 | // the right-hand side of `extends` in a few places, which doesn't narrow `unknown`. Without narrowing, overload |
| 239 | // resolution and meaningful type inference of arguments break, for example. |
| 240 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 241 | export type Callable = (...args: any) => any; |
| 242 | |
| 243 | export interface EventSink<ListenerT extends Callable> extends PublicAPI.Chrome.DevTools.EventSink<ListenerT> { |
| 244 | _type: string; |
| 245 | _listeners: ListenerT[]; |
| 246 | _customDispatch: undefined|((this: EventSink<ListenerT>, request: {arguments: unknown[]}) => unknown); |
| 247 | |
| 248 | _fire(..._vararg: Parameters<ListenerT>): void; |
| 249 | _dispatch(request: {arguments: unknown[]}): void; |
| 250 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 251 | |
| 252 | export interface Network extends PublicAPI.Chrome.DevTools.Network { |
| 253 | addRequestHeaders(headers: {[key: string]: string}): void; |
| 254 | } |
| 255 | |
| 256 | export interface Request extends PublicAPI.Chrome.DevTools.Request, HAR.Log.EntryDTO { |
| 257 | _id: number; |
| 258 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 259 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 260 | |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 261 | self.injectedExtensionAPI = function( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 262 | extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
| 263 | testHook: (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown, |
| 264 | injectedScriptId: number): void { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 265 | const keysToForwardSet = new Set<number>(keysToForward); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 266 | const chrome = window.chrome || {}; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 267 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 268 | const devtools_descriptor = Object.getOwnPropertyDescriptor(chrome, 'devtools'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 269 | if (devtools_descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 270 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 271 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 272 | let userAction = false; |
| 273 | |
| 274 | // Here and below, all constructors are private to API implementation. |
| 275 | // For a public type Foo, if internal fields are present, these are on |
| 276 | // a private FooImpl type, an instance of FooImpl is used in a closure |
| 277 | // by Foo consutrctor to re-bind publicly exported members to an instance |
| 278 | // of Foo. |
| 279 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 280 | function EventSinkImpl<ListenerT extends APIImpl.Callable>( |
| 281 | this: APIImpl.EventSink<ListenerT>, type: string, |
| 282 | customDispatch?: (this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 283 | this._type = type; |
| 284 | this._listeners = []; |
| 285 | this._customDispatch = customDispatch; |
| 286 | } |
| 287 | |
| 288 | EventSinkImpl.prototype = { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 289 | addListener: function<ListenerT extends APIImpl.Callable>(this: APIImpl.EventSink<ListenerT>, callback: ListenerT): |
| 290 | void { |
| 291 | if (typeof callback !== 'function') { |
| 292 | throw 'addListener: callback is not a function'; |
| 293 | } |
| 294 | if (this._listeners.length === 0) { |
| 295 | extensionServer.sendRequest({command: PrivateAPI.Commands.Subscribe, type: this._type}); |
| 296 | } |
| 297 | this._listeners.push(callback); |
| 298 | extensionServer.registerHandler('notify-' + this._type, this._dispatch.bind(this)); |
| 299 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 300 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 301 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 302 | removeListener: function<ListenerT extends APIImpl.Callable>( |
| 303 | this: APIImpl.EventSink<ListenerT>, callback: ListenerT): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 304 | const listeners = this._listeners; |
| 305 | |
| 306 | for (let i = 0; i < listeners.length; ++i) { |
| 307 | if (listeners[i] === callback) { |
| 308 | listeners.splice(i, 1); |
| 309 | break; |
| 310 | } |
| 311 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 312 | if (this._listeners.length === 0) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 313 | extensionServer.sendRequest({command: PrivateAPI.Commands.Unsubscribe, type: this._type}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 314 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 315 | }, |
| 316 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 317 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 318 | _fire: function<ListenerT extends APIImpl.Callable>( |
| 319 | this: APIImpl.EventSink<ListenerT>, ..._vararg: Parameters<ListenerT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 320 | const listeners = this._listeners.slice(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 321 | for (let i = 0; i < listeners.length; ++i) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 322 | listeners[i].apply(null, Array.from(arguments)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 323 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 324 | }, |
| 325 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 326 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 327 | _dispatch: function<ListenerT extends APIImpl.Callable>( |
| 328 | this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 329 | if (this._customDispatch) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 330 | this._customDispatch.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 331 | } else { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 332 | this._fire.apply(this, request.arguments as Parameters<ListenerT>); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 333 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 334 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | }; |
| 336 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 337 | function Constructor<NewT extends APIImpl.Callable>(ctor: NewT): new (...args: Parameters<NewT>) => |
| 338 | ThisParameterType<NewT> { |
| 339 | return ctor as unknown as new (...args: Parameters<NewT>) => ThisParameterType<NewT>; |
| 340 | } |
| 341 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 342 | /** |
| 343 | * @constructor |
| 344 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 345 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 346 | function InspectorExtensionAPI(this: APIImpl.InspectorExtensionAPI): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 347 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 348 | this.inspectedWindow = new InspectedWindow(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 349 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 350 | this.panels = new Panels(); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 351 | this.network = new (Constructor(Network))(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 352 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 353 | this.timeline = new Timeline(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 354 | // @ts-ignore |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 355 | this.languageServices = new LanguageServicesAPI(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 356 | defineDeprecatedProperty(this, 'webInspector', 'resources', 'network'); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * @constructor |
| 361 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 362 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 363 | function Network(this: APIImpl.Network): void { |
| 364 | function dispatchRequestEvent( |
| 365 | this: APIImpl.EventSink<(request: PublicAPI.Chrome.DevTools.Request) => unknown>, |
| 366 | message: {arguments: unknown[]}): void { |
| 367 | const request = message.arguments[1] as APIImpl.Request & {__proto__: APIImpl.Request}; |
| 368 | |
| 369 | request.__proto__ = new (Constructor(Request))(message.arguments[0] as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 370 | this._fire(request); |
| 371 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 372 | |
| 373 | this.onRequestFinished = |
| 374 | new (Constructor(EventSink))(PrivateAPI.Events.NetworkRequestFinished, dispatchRequestEvent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 375 | defineDeprecatedProperty(this, 'network', 'onFinished', 'onRequestFinished'); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 376 | |
| 377 | this.onNavigated = new (Constructor(EventSink))(PrivateAPI.Events.InspectedURLChanged); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 378 | } |
| 379 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 380 | (Network.prototype as Pick<APIImpl.Network, 'getHAR'|'addRequestHeaders'>) = { |
| 381 | getHAR: function(this: PublicAPI.Chrome.DevTools.Network, callback?: (harLog: Object) => unknown): void { |
| 382 | function callbackWrapper(response: unknown): void { |
| 383 | const result = |
| 384 | response as ({entries: Array<HAR.Log.EntryDTO&{__proto__?: APIImpl.Request, _requestId?: number}>}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 385 | const entries = (result && result.entries) || []; |
| 386 | for (let i = 0; i < entries.length; ++i) { |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 387 | entries[i].__proto__ = new (Constructor(Request))(entries[i]._requestId as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 388 | delete entries[i]._requestId; |
| 389 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 390 | callback && callback(result as Object); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 391 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 392 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetHAR}, callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 393 | }, |
| 394 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 395 | addRequestHeaders: function(headers: {[key: string]: string}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 396 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 397 | {command: PrivateAPI.Commands.AddRequestHeaders, headers: headers, extensionId: window.location.hostname}); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 398 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 399 | }; |
| 400 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 401 | function RequestImpl(this: APIImpl.Request, id: number): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 402 | this._id = id; |
| 403 | } |
| 404 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame^] | 405 | (RequestImpl.prototype as Pick<APIImpl.Request, 'getContent'>) = { |
| 406 | getContent: function(this: APIImpl.Request, callback?: (content: string, encoding: string) => unknown): void { |
| 407 | function callbackWrapper(response: unknown): void { |
| 408 | const {content, encoding} = response as {content: string, encoding: string}; |
| 409 | callback && callback(content, encoding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 410 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 411 | extensionServer.sendRequest( |
| 412 | {command: PrivateAPI.Commands.GetRequestContent, id: this._id}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 413 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | /** |
| 417 | * @constructor |
| 418 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 419 | function Panels(this: any): void { |
| 420 | const panels: {[key: string]: any} = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 421 | elements: new ElementsPanel(), |
| 422 | sources: new SourcesPanel(), |
| 423 | }; |
| 424 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 425 | function panelGetter(name: any): any { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 426 | return panels[name]; |
| 427 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 428 | for (const panel in panels) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 429 | Object.defineProperty(this, panel, {get: panelGetter.bind(null, panel), enumerable: true}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 430 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 431 | this.applyStyleSheet = function(styleSheet: any): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 432 | extensionServer.sendRequest({command: PrivateAPI.Commands.ApplyStyleSheet, styleSheet: styleSheet}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 433 | }; |
| 434 | } |
| 435 | |
| 436 | Panels.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 437 | create: function(title: any, icon: any, page: any, callback: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 438 | const id = 'extension-panel-' + extensionServer.nextObjectId(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 439 | const request = {command: PrivateAPI.Commands.CreatePanel, id: id, title: title, icon: icon, page: page}; |
| 440 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 441 | extensionServer.sendRequest(request, callback && callback.bind(this, new ExtensionPanel(id))); |
| 442 | }, |
| 443 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 444 | setOpenResourceHandler: function(callback: any): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 445 | const hadHandler = extensionServer.hasHandler(PrivateAPI.Events.OpenResource); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 446 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 447 | function callbackWrapper(message: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 448 | // Allow the panel to show itself when handling the event. |
| 449 | userAction = true; |
| 450 | try { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 451 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 452 | callback.call(null, new Resource(message.resource), message.lineNumber); |
| 453 | } finally { |
| 454 | userAction = false; |
| 455 | } |
| 456 | } |
| 457 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 458 | if (!callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 459 | extensionServer.unregisterHandler(PrivateAPI.Events.OpenResource); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 460 | } else { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 461 | extensionServer.registerHandler(PrivateAPI.Events.OpenResource, callbackWrapper); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 462 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 463 | |
| 464 | // 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] | 465 | if (hadHandler === !callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 466 | extensionServer.sendRequest( |
| 467 | {command: PrivateAPI.Commands.SetOpenResourceHandler, 'handlerPresent': Boolean(callback)}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 468 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 469 | }, |
| 470 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 471 | openResource: function(url: any, lineNumber: any, callback: any): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 472 | extensionServer.sendRequest( |
| 473 | {command: PrivateAPI.Commands.OpenResource, 'url': url, 'lineNumber': lineNumber}, callback); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 474 | }, |
| 475 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 476 | get SearchAction(): any { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 477 | return { |
| 478 | CancelSearch: PrivateAPI.Panels.SearchAction.CancelSearch, |
| 479 | PerformSearch: PrivateAPI.Panels.SearchAction.PerformSearch, |
| 480 | NextSearchResult: PrivateAPI.Panels.SearchAction.NextSearchResult, |
| 481 | PreviousSearchResult: PrivateAPI.Panels.SearchAction.PreviousSearchResult, |
| 482 | }; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 483 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | /** |
| 487 | * @constructor |
| 488 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 489 | function ExtensionViewImpl(this: any, id: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 490 | this._id = id; |
| 491 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 492 | function dispatchShowEvent(this: any, message: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 493 | const frameIndex = message.arguments[0]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 494 | if (typeof frameIndex === 'number') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 495 | this._fire(window.parent.frames[frameIndex]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 496 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 497 | this._fire(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 498 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (id) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 502 | // @ts-ignore |
| 503 | this.onShown = new EventSink(PrivateAPI.Events.ViewShown + id, dispatchShowEvent); |
| 504 | // @ts-ignore |
| 505 | this.onHidden = new EventSink(PrivateAPI.Events.ViewHidden + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * @constructor |
| 511 | * @extends {ExtensionViewImpl} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 512 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 513 | function PanelWithSidebarImpl(this: any, hostPanelName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 514 | ExtensionViewImpl.call(this, null); |
| 515 | this._hostPanelName = hostPanelName; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 516 | // @ts-ignore |
| 517 | this.onSelectionChanged = new EventSink(PrivateAPI.Events.PanelObjectSelected + hostPanelName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | PanelWithSidebarImpl.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 521 | createSidebarPane: function(title: any, callback: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 522 | const id = 'extension-sidebar-' + extensionServer.nextObjectId(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 523 | function callbackWrapper(): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 524 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 525 | callback(new ExtensionSidebarPane(id)); |
| 526 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 527 | extensionServer.sendRequest( |
| 528 | {command: PrivateAPI.Commands.CreateSidebarPane, panel: this._hostPanelName, id: id, title: title}, |
| 529 | callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 530 | }, |
| 531 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 532 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 533 | }; |
| 534 | |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 535 | /** |
| 536 | * @constructor |
| 537 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 538 | function LanguageServicesAPIImpl(this: any): void { |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 539 | /** @type {!Map<*, !MessagePort>} */ |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 540 | this._plugins = new Map(); |
| 541 | } |
| 542 | |
| 543 | LanguageServicesAPIImpl.prototype = { |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 544 | registerLanguageExtensionPlugin: async function( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 545 | plugin: any, pluginName: string, supportedScriptTypes: PublicAPI.Chrome.DevTools.SupportedScriptTypes): |
| 546 | Promise<void> { |
| 547 | if (this._plugins.has(plugin)) { |
| 548 | throw new Error(`Tried to register plugin '${pluginName}' twice`); |
| 549 | } |
| 550 | const channel = new MessageChannel(); |
| 551 | const port = channel.port1; |
| 552 | this._plugins.set(plugin, port); |
| 553 | port.onmessage = ({data: {requestId, method, parameters}}: MessageEvent<any>): void => { |
| 554 | console.time(`${requestId}: ${method}`); |
| 555 | dispatchMethodCall(method, parameters) |
| 556 | .then(result => port.postMessage({requestId, result})) |
| 557 | .catch(error => port.postMessage({requestId, error: {message: error.message}})) |
| 558 | .finally(() => console.timeEnd(`${requestId}: ${method}`)); |
| 559 | }; |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 560 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 561 | function dispatchMethodCall(method: string, parameters: any): Promise<any> { |
| 562 | switch (method) { |
| 563 | case PrivateAPI.LanguageExtensionPluginCommands.AddRawModule: |
| 564 | return plugin.addRawModule(parameters.rawModuleId, parameters.symbolsURL, parameters.rawModule); |
| 565 | case PrivateAPI.LanguageExtensionPluginCommands.RemoveRawModule: |
| 566 | return plugin.removeRawModule(parameters.rawModuleId); |
| 567 | case PrivateAPI.LanguageExtensionPluginCommands.SourceLocationToRawLocation: |
| 568 | return plugin.sourceLocationToRawLocation(parameters.sourceLocation); |
| 569 | case PrivateAPI.LanguageExtensionPluginCommands.RawLocationToSourceLocation: |
| 570 | return plugin.rawLocationToSourceLocation(parameters.rawLocation); |
| 571 | case PrivateAPI.LanguageExtensionPluginCommands.GetScopeInfo: |
| 572 | return plugin.getScopeInfo(parameters.type); |
| 573 | case PrivateAPI.LanguageExtensionPluginCommands.ListVariablesInScope: |
| 574 | return plugin.listVariablesInScope(parameters.rawLocation); |
| 575 | case PrivateAPI.LanguageExtensionPluginCommands.GetTypeInfo: |
| 576 | return plugin.getTypeInfo(parameters.expression, parameters.context); |
| 577 | case PrivateAPI.LanguageExtensionPluginCommands.GetFormatter: |
| 578 | return plugin.getFormatter(parameters.expressionOrField, parameters.context); |
| 579 | case PrivateAPI.LanguageExtensionPluginCommands.GetInspectableAddress: |
| 580 | if ('getInspectableAddress' in plugin) { |
| 581 | return plugin.getInspectableAddress(parameters.field); |
| 582 | } |
| 583 | return Promise.resolve({js: ''}); |
| 584 | case PrivateAPI.LanguageExtensionPluginCommands.GetFunctionInfo: |
| 585 | return plugin.getFunctionInfo(parameters.rawLocation); |
| 586 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedFunctionRanges: |
| 587 | return plugin.getInlinedFunctionRanges(parameters.rawLocation); |
| 588 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedCalleesRanges: |
| 589 | return plugin.getInlinedCalleesRanges(parameters.rawLocation); |
| 590 | case PrivateAPI.LanguageExtensionPluginCommands.GetMappedLines: |
| 591 | if ('getMappedLines' in plugin) { |
| 592 | return plugin.getMappedLines(parameters.rawModuleId, parameters.sourceFileURL); |
| 593 | } |
| 594 | return Promise.resolve(undefined); |
Kim-Anh Tran | 8fd7d6f | 2021-01-28 11:07:02 | [diff] [blame] | 595 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 596 | throw new Error(`Unknown language plugin method ${method}`); |
| 597 | } |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 598 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 599 | await new Promise<void>(resolve => { |
| 600 | extensionServer.sendRequest( |
| 601 | { |
| 602 | command: PrivateAPI.Commands.RegisterLanguageExtensionPlugin, |
| 603 | pluginName, |
| 604 | port: channel.port2, |
| 605 | supportedScriptTypes, |
| 606 | }, |
| 607 | () => resolve(), [channel.port2]); |
| 608 | }); |
| 609 | }, |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 610 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 611 | unregisterLanguageExtensionPlugin: async function(plugin: any): Promise<void> { |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 612 | const port = this._plugins.get(plugin); |
| 613 | if (!port) { |
| 614 | throw new Error('Tried to unregister a plugin that was not previously registered'); |
| 615 | } |
| 616 | this._plugins.delete(plugin); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 617 | port.postMessage({event: PrivateAPI.LanguageExtensionPluginEvents.UnregisteredLanguageExtensionPlugin}); |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 618 | port.close(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 619 | }, |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 620 | }; |
| 621 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 622 | function declareInterfaceClass<ImplT extends APIImpl.Callable>(implConstructor: ImplT): ( |
| 623 | this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>) => void { |
| 624 | return function(this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 625 | const impl = {__proto__: implConstructor.prototype}; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 626 | implConstructor.apply(impl, args); |
| 627 | populateInterfaceClass(this as {[key: string]: unknown}, impl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 628 | }; |
| 629 | } |
| 630 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 631 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 632 | function defineDeprecatedProperty(object: any, className: string, oldName: string, newName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 633 | let warningGiven = false; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 634 | function getter(): unknown { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 635 | if (!warningGiven) { |
| 636 | console.warn(className + '.' + oldName + ' is deprecated. Use ' + className + '.' + newName + ' instead'); |
| 637 | warningGiven = true; |
| 638 | } |
| 639 | return object[newName]; |
| 640 | } |
| 641 | object.__defineGetter__(oldName, getter); |
| 642 | } |
| 643 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 644 | function extractCallbackArgument(args: IArguments): ((...args: unknown[]) => unknown)|undefined { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 645 | const lastArgument = args[args.length - 1]; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 646 | return typeof lastArgument === 'function' ? lastArgument as (...args: unknown[]) => unknown : undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 647 | } |
| 648 | |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 649 | const LanguageServicesAPI = declareInterfaceClass(LanguageServicesAPIImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 650 | const Button = declareInterfaceClass(ButtonImpl); |
| 651 | const EventSink = declareInterfaceClass(EventSinkImpl); |
| 652 | const ExtensionPanel = declareInterfaceClass(ExtensionPanelImpl); |
| 653 | const ExtensionSidebarPane = declareInterfaceClass(ExtensionSidebarPaneImpl); |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 654 | /** |
| 655 | * @constructor |
| 656 | * @param {string} hostPanelName |
| 657 | */ |
| 658 | const PanelWithSidebarClass = declareInterfaceClass(PanelWithSidebarImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 659 | const Request = declareInterfaceClass(RequestImpl); |
| 660 | const Resource = declareInterfaceClass(ResourceImpl); |
| 661 | const TraceSession = declareInterfaceClass(TraceSessionImpl); |
| 662 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 663 | // @ts-ignore |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 664 | class ElementsPanel extends PanelWithSidebarClass { |
| 665 | constructor() { |
| 666 | super('elements'); |
| 667 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 668 | } |
| 669 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 670 | // @ts-ignore |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 671 | class SourcesPanel extends PanelWithSidebarClass { |
| 672 | constructor() { |
| 673 | super('sources'); |
| 674 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 675 | } |
| 676 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 677 | /** |
| 678 | * @constructor |
| 679 | * @extends {ExtensionViewImpl} |
| 680 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 681 | function ExtensionPanelImpl(this: any, id: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 682 | ExtensionViewImpl.call(this, id); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 683 | // @ts-ignore |
| 684 | this.onSearch = new EventSink(PrivateAPI.Events.PanelSearch + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | ExtensionPanelImpl.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 688 | createStatusBarButton: function(iconPath: any, tooltipText: any, disabled: any): Object { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 689 | const id = 'button-' + extensionServer.nextObjectId(); |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 690 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 691 | command: PrivateAPI.Commands.CreateToolbarButton, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 692 | panel: this._id, |
| 693 | id: id, |
| 694 | icon: iconPath, |
| 695 | tooltip: tooltipText, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 696 | disabled: Boolean(disabled), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 697 | }); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 698 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 699 | return new Button(id); |
| 700 | }, |
| 701 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 702 | show: function(): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 703 | if (!userAction) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 704 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 705 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 706 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 707 | extensionServer.sendRequest({command: PrivateAPI.Commands.ShowPanel, id: this._id}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 708 | }, |
| 709 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 710 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | /** |
| 714 | * @constructor |
| 715 | * @extends {ExtensionViewImpl} |
| 716 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 717 | function ExtensionSidebarPaneImpl(this: any, id: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 718 | ExtensionViewImpl.call(this, id); |
| 719 | } |
| 720 | |
| 721 | ExtensionSidebarPaneImpl.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 722 | setHeight: function(height: any): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 723 | extensionServer.sendRequest({command: PrivateAPI.Commands.SetSidebarHeight, id: this._id, height: height}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 724 | }, |
| 725 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 726 | setExpression: function(expression: any, rootTitle: any, evaluateOptions: any): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 727 | extensionServer.sendRequest( |
| 728 | { |
| 729 | command: PrivateAPI.Commands.SetSidebarContent, |
| 730 | id: this._id, |
| 731 | expression: expression, |
| 732 | rootTitle: rootTitle, |
| 733 | evaluateOnPage: true, |
| 734 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : undefined), |
| 735 | }, |
| 736 | extractCallbackArgument(arguments)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 737 | }, |
| 738 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 739 | setObject: function(jsonObject: any, rootTitle: any, callback: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 740 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 741 | {command: PrivateAPI.Commands.SetSidebarContent, id: this._id, expression: jsonObject, rootTitle: rootTitle}, |
| 742 | callback); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 743 | }, |
| 744 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 745 | setPage: function(page: any): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 746 | extensionServer.sendRequest({command: PrivateAPI.Commands.SetSidebarPage, id: this._id, page: page}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 747 | }, |
| 748 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 749 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | /** |
| 753 | * @constructor |
| 754 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 755 | function ButtonImpl(this: any, id: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 756 | this._id = id; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 757 | // @ts-ignore |
| 758 | this.onClicked = new EventSink(PrivateAPI.Events.ButtonClicked + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | ButtonImpl.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 762 | update: function(iconPath: any, tooltipText: any, disabled: any): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 763 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 764 | command: PrivateAPI.Commands.UpdateButton, |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 765 | id: this._id, |
| 766 | icon: iconPath, |
| 767 | tooltip: tooltipText, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 768 | disabled: Boolean(disabled), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 769 | }); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 770 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | /** |
| 774 | * @constructor |
| 775 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 776 | function Timeline(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | Timeline.prototype = { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 780 | // @ts-ignore |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 781 | addTraceProvider: function(categoryName: string, categoryTooltip: string): TraceProvider { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 782 | const id = 'extension-trace-provider-' + extensionServer.nextObjectId(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 783 | extensionServer.sendRequest({ |
| 784 | command: PrivateAPI.Commands.AddTraceProvider, |
| 785 | id: id, |
| 786 | categoryName: categoryName, |
| 787 | categoryTooltip: categoryTooltip, |
| 788 | }); |
| 789 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 790 | return new TraceProvider(id); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 791 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 792 | }; |
| 793 | |
| 794 | /** |
| 795 | * @constructor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 796 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 797 | function TraceSessionImpl(this: any, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 798 | this._id = id; |
| 799 | } |
| 800 | |
| 801 | TraceSessionImpl.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 802 | complete: function(url?: string, timeOffset?: number): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 803 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 804 | command: PrivateAPI.Commands.CompleteTraceSession, |
| 805 | id: this._id, |
| 806 | url: url || '', |
| 807 | timeOffset: timeOffset || 0, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 808 | }); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 809 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 810 | }; |
| 811 | |
| 812 | /** |
| 813 | * @constructor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 814 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 815 | function TraceProvider(id: string): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 816 | function dispatchRecordingStarted(this: any, message: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 817 | const sessionId = message.arguments[0]; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 818 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 819 | this._fire(new TraceSession(sessionId)); |
| 820 | } |
| 821 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 822 | // @ts-ignore |
| 823 | this.onRecordingStarted = new EventSink(PrivateAPI.Events.RecordingStarted + id, dispatchRecordingStarted); |
| 824 | // @ts-ignore |
| 825 | this.onRecordingStopped = new EventSink(PrivateAPI.Events.RecordingStopped + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** |
| 829 | * @constructor |
| 830 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 831 | function InspectedWindow(this: any): void { |
| 832 | function dispatchResourceEvent(this: any, message: any): void { |
| 833 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 834 | this._fire(new Resource(message.arguments[0])); |
| 835 | } |
| 836 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 837 | function dispatchResourceContentEvent(this: any, message: any): void { |
| 838 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 839 | this._fire(new Resource(message.arguments[0]), message.arguments[1]); |
| 840 | } |
| 841 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 842 | // @ts-ignore |
| 843 | this.onResourceAdded = new EventSink(PrivateAPI.Events.ResourceAdded, dispatchResourceEvent); |
| 844 | this.onResourceContentCommitted = |
| 845 | // @ts-ignore |
| 846 | new EventSink(PrivateAPI.Events.ResourceContentCommitted, dispatchResourceContentEvent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | InspectedWindow.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 850 | reload: function(optionsOrUserAgent: any): void { |
| 851 | let options: { |
| 852 | userAgent: string, |
| 853 | }|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 854 | if (typeof optionsOrUserAgent === 'object') { |
| 855 | options = optionsOrUserAgent; |
| 856 | } else if (typeof optionsOrUserAgent === 'string') { |
| 857 | options = {userAgent: optionsOrUserAgent}; |
| 858 | console.warn( |
| 859 | 'Passing userAgent as string parameter to inspectedWindow.reload() is deprecated. ' + |
| 860 | 'Use inspectedWindow.reload({ userAgent: value}) instead.'); |
| 861 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 862 | extensionServer.sendRequest({command: PrivateAPI.Commands.Reload, options: options}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 863 | }, |
| 864 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 865 | eval: function(expression: any, evaluateOptions: any): Object | |
| 866 | null { |
| 867 | const callback = extractCallbackArgument(arguments); |
| 868 | function callbackWrapper(result: any): void { |
| 869 | if (result.isError || result.isException) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 870 | callback && callback(undefined, result); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 871 | } else { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 872 | callback && callback(result.value); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 873 | } |
| 874 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 875 | extensionServer.sendRequest( |
| 876 | { |
| 877 | command: PrivateAPI.Commands.EvaluateOnInspectedPage, |
| 878 | expression: expression, |
| 879 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : undefined), |
| 880 | }, |
| 881 | callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 882 | return null; |
| 883 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 884 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 885 | getResources: function(callback: any): void { |
| 886 | function wrapResource(resourceData: any): any { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 887 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 888 | return new Resource(resourceData); |
| 889 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 890 | function callbackWrapper(resources: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 891 | callback(resources.map(wrapResource)); |
| 892 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 893 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetPageResources}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 894 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 895 | }; |
| 896 | |
| 897 | /** |
| 898 | * @constructor |
| 899 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 900 | function ResourceImpl(this: any, resourceData: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 901 | this._url = resourceData.url; |
| 902 | this._type = resourceData.type; |
| 903 | } |
| 904 | |
| 905 | ResourceImpl.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 906 | get url(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 907 | return this._url; |
| 908 | }, |
| 909 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 910 | get type(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 911 | return this._type; |
| 912 | }, |
| 913 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 914 | getContent: function(callback: any): void { |
| 915 | function callbackWrapper(response: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 916 | callback(response.content, response.encoding); |
| 917 | } |
| 918 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 919 | extensionServer.sendRequest( |
| 920 | {command: PrivateAPI.Commands.GetResourceContent, url: this._url}, callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 921 | }, |
| 922 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 923 | setContent: function(content: any, commit: any, callback: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 924 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 925 | {command: PrivateAPI.Commands.SetResourceContent, url: this._url, content: content, commit: commit}, |
| 926 | callback); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 927 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 928 | }; |
| 929 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 930 | function getTabId(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 931 | return inspectedTabId; |
| 932 | } |
| 933 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 934 | let keyboardEventRequestQueue: KeyboardEventInit&{eventType: string}[] = []; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 935 | let forwardTimer: number|null = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 936 | function forwardKeyboardEvent(event: KeyboardEvent): void { |
Jan Scheffler | e7d7bb1 | 2019-10-24 09:18:52 | [diff] [blame] | 937 | // Check if the event should be forwarded. |
| 938 | // This is a workaround for crbug.com/923338. |
| 939 | const focused = document.activeElement; |
| 940 | if (focused) { |
| 941 | const isInput = focused.nodeName === 'INPUT' || focused.nodeName === 'TEXTAREA'; |
| 942 | if (isInput && !(event.ctrlKey || event.altKey || event.metaKey)) { |
| 943 | return; |
| 944 | } |
| 945 | } |
| 946 | |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 947 | let modifiers = 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 948 | if (event.shiftKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 949 | modifiers |= 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 950 | } |
| 951 | if (event.ctrlKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 952 | modifiers |= 2; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 953 | } |
| 954 | if (event.altKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 955 | modifiers |= 4; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 956 | } |
| 957 | if (event.metaKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 958 | modifiers |= 8; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 959 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 960 | const num = (event.keyCode & 255) | (modifiers << 8); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 961 | // We only care about global hotkeys, not about random text |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 962 | if (!keysToForwardSet.has(num)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 963 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 964 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 965 | event.preventDefault(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 966 | const requestPayload = { |
| 967 | eventType: event.type, |
| 968 | ctrlKey: event.ctrlKey, |
| 969 | altKey: event.altKey, |
| 970 | metaKey: event.metaKey, |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 971 | shiftKey: event.shiftKey, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 972 | // @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] | 973 | keyIdentifier: event.keyIdentifier, |
| 974 | key: event.key, |
| 975 | code: event.code, |
| 976 | location: event.location, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 977 | keyCode: event.keyCode, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 978 | }; |
| 979 | keyboardEventRequestQueue.push(requestPayload); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 980 | if (!forwardTimer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 981 | forwardTimer = setTimeout(forwardEventQueue, 0); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 982 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 983 | } |
| 984 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 985 | function forwardEventQueue(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 986 | forwardTimer = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 987 | extensionServer.sendRequest( |
| 988 | {command: PrivateAPI.Commands.ForwardKeyboardEvent, entries: keyboardEventRequestQueue}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 989 | keyboardEventRequestQueue = []; |
| 990 | } |
| 991 | |
| 992 | document.addEventListener('keydown', forwardKeyboardEvent, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 993 | |
| 994 | /** |
| 995 | * @constructor |
| 996 | */ |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 997 | function ExtensionServerClient(this: APIImpl.ExtensionServerClient): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 998 | this._callbacks = {}; |
| 999 | this._handlers = {}; |
| 1000 | this._lastRequestId = 0; |
| 1001 | this._lastObjectId = 0; |
| 1002 | |
| 1003 | this.registerHandler('callback', this._onCallback.bind(this)); |
| 1004 | |
| 1005 | const channel = new MessageChannel(); |
| 1006 | this._port = channel.port1; |
| 1007 | this._port.addEventListener('message', this._onMessage.bind(this), false); |
| 1008 | this._port.start(); |
| 1009 | |
| 1010 | window.parent.postMessage('registerExtension', '*', [channel.port2]); |
| 1011 | } |
| 1012 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1013 | (ExtensionServerClient.prototype as Pick< |
| 1014 | APIImpl.ExtensionServerClient, |
| 1015 | 'sendRequest'|'hasHandler'|'registerHandler'|'unregisterHandler'|'nextObjectId'|'_registerCallback'| |
| 1016 | '_onCallback'|'_onMessage'>) = { |
| 1017 | sendRequest: function( |
| 1018 | this: APIImpl.ExtensionServerClient, message: PrivateAPI.ServerRequests, |
| 1019 | callback?: (response: unknown) => unknown, transfers?: Transferable[]): void { |
| 1020 | if (typeof callback === 'function') { |
| 1021 | (message as PrivateAPI.ExtensionServerRequestMessage).requestId = this._registerCallback(callback); |
| 1022 | } |
| 1023 | // @ts-expect-error |
| 1024 | this._port.postMessage(message, transfers); |
| 1025 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1026 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1027 | hasHandler: function(this: APIImpl.ExtensionServerClient, command: string): boolean { |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1028 | return Boolean(this._handlers[command]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1029 | }, |
| 1030 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1031 | registerHandler: function( |
| 1032 | this: APIImpl.ExtensionServerClient, command: string, handler: (request: {arguments: unknown[]}) => unknown): |
| 1033 | void { |
| 1034 | this._handlers[command] = handler; |
| 1035 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1036 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1037 | unregisterHandler: function(this: APIImpl.ExtensionServerClient, command: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1038 | delete this._handlers[command]; |
| 1039 | }, |
| 1040 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1041 | nextObjectId: function(this: APIImpl.ExtensionServerClient): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1042 | return injectedScriptId.toString() + '_' + ++this._lastObjectId; |
| 1043 | }, |
| 1044 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1045 | _registerCallback: function(this: APIImpl.ExtensionServerClient, callback: (response: unknown) => unknown): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1046 | const id = ++this._lastRequestId; |
| 1047 | this._callbacks[id] = callback; |
| 1048 | return id; |
| 1049 | }, |
| 1050 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1051 | _onCallback: function(this: APIImpl.ExtensionServerClient, request: {requestId: number, result: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1052 | if (request.requestId in this._callbacks) { |
| 1053 | const callback = this._callbacks[request.requestId]; |
| 1054 | delete this._callbacks[request.requestId]; |
| 1055 | callback(request.result); |
| 1056 | } |
| 1057 | }, |
| 1058 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1059 | _onMessage: function( |
| 1060 | this: APIImpl.ExtensionServerClient, |
| 1061 | event: MessageEvent<{command: string, requestId: number, arguments: unknown[]}>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1062 | const request = event.data; |
| 1063 | const handler = this._handlers[request.command]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1064 | if (handler) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1065 | handler.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1066 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1067 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1068 | }; |
| 1069 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1070 | function populateInterfaceClass(interfaze: {[key: string]: unknown}, implementation: {[key: string]: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1071 | for (const member in implementation) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1072 | if (member.charAt(0) === '_') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1073 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1074 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1075 | let descriptor: (PropertyDescriptor|undefined)|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1076 | // Traverse prototype chain until we find the owner. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1077 | for (let owner = implementation; owner && !descriptor; owner = owner.__proto__ as {[key: string]: unknown}) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1078 | descriptor = Object.getOwnPropertyDescriptor(owner, member); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1079 | } |
| 1080 | if (!descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1081 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1082 | } |
| 1083 | if (typeof descriptor.value === 'function') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1084 | interfaze[member] = descriptor.value.bind(implementation); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1085 | } else if (typeof descriptor.get === 'function') { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1086 | // @ts-expect-error |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1087 | interfaze.__defineGetter__(member, descriptor.get.bind(implementation)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1088 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1089 | Object.defineProperty(interfaze, member, descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1090 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1094 | |
| 1095 | const extensionServer = new (Constructor(ExtensionServerClient))(); |
| 1096 | |
| 1097 | const coreAPI = new (Constructor(InspectorExtensionAPI))(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1098 | |
| 1099 | Object.defineProperty(chrome, 'devtools', {value: {}, enumerable: true}); |
| 1100 | |
| 1101 | // Only expose tabId on chrome.devtools.inspectedWindow, not webInspector.inspectedWindow. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1102 | // @ts-expect-error |
| 1103 | chrome.devtools!.inspectedWindow = {}; |
| 1104 | Object.defineProperty(chrome.devtools!.inspectedWindow, 'tabId', {get: getTabId}); |
| 1105 | // @ts-expect-error |
| 1106 | chrome.devtools!.inspectedWindow.__proto__ = coreAPI.inspectedWindow; |
| 1107 | chrome.devtools!.network = coreAPI.network; |
| 1108 | chrome.devtools!.panels = coreAPI.panels; |
| 1109 | chrome.devtools!.panels.themeName = themeName; |
| 1110 | chrome.devtools!.languageServices = coreAPI.languageServices; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1111 | |
| 1112 | // default to expose experimental APIs for now. |
| 1113 | if (extensionInfo.exposeExperimentalAPIs !== false) { |
| 1114 | chrome.experimental = chrome.experimental || {}; |
| 1115 | chrome.experimental.devtools = chrome.experimental.devtools || {}; |
| 1116 | |
| 1117 | const properties = Object.getOwnPropertyNames(coreAPI); |
| 1118 | for (let i = 0; i < properties.length; ++i) { |
| 1119 | const descriptor = Object.getOwnPropertyDescriptor(coreAPI, properties[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1120 | if (descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1121 | Object.defineProperty(chrome.experimental.devtools, properties[i], descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1122 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1123 | } |
| 1124 | chrome.experimental.devtools.inspectedWindow = chrome.devtools.inspectedWindow; |
| 1125 | } |
| 1126 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1127 | if (extensionInfo.exposeWebInspectorNamespace) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1128 | window.webInspector = coreAPI; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1129 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1130 | testHook(extensionServer, coreAPI); |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1131 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1132 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1133 | self.buildExtensionAPIInjectedScript = function( |
| 1134 | extensionInfo: { |
| 1135 | startPage: string, |
| 1136 | name: string, |
| 1137 | exposeExperimentalAPIs: boolean, |
| 1138 | }, |
| 1139 | inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1140 | testHook: |
| 1141 | ((extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown)| |
| 1142 | undefined): string { |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 1143 | const argumentsJSON = |
| 1144 | [extensionInfo, inspectedTabId || null, themeName, keysToForward].map(_ => JSON.stringify(_)).join(','); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1145 | if (!testHook) { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1146 | testHook = (): void => {}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1147 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1148 | return '(function(injectedScriptId){ ' + |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1149 | '(' + self.injectedExtensionAPI.toString() + ')(' + argumentsJSON + ',' + testHook + ', injectedScriptId);' + |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1150 | '})'; |
Tim van der Lippe | 29fab47 | 2019-08-15 14:46:48 | [diff] [blame] | 1151 | }; |