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 | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 259 | |
| 260 | export interface Panels extends PublicAPI.Chrome.DevTools.Panels { |
| 261 | get SearchAction(): {[key: string]: string}; |
| 262 | applyStyleSheet(styleSheet: string): void; |
| 263 | setOpenResourceHandler(callback?: (resource: PublicAPI.Chrome.DevTools.Resource, lineNumber: number) => unknown): |
| 264 | void; |
| 265 | } |
| 266 | |
| 267 | export interface ExtensionView extends PublicAPI.Chrome.DevTools.ExtensionView { |
| 268 | _id: string|null; |
| 269 | } |
| 270 | |
| 271 | export interface ExtensionSidebarPane extends ExtensionView, PublicAPI.Chrome.DevTools.ExtensionSidebarPane { |
| 272 | setExpression( |
| 273 | expression: string, rootTitle?: string, evaluteOptions?: PrivateAPI.EvaluateOptions, |
| 274 | callback?: () => unknown): void; |
| 275 | } |
| 276 | |
| 277 | export interface PanelWithSidebar extends ExtensionView, PublicAPI.Chrome.DevTools.PanelWithSidebar { |
| 278 | _hostPanelName: string; |
| 279 | } |
| 280 | |
| 281 | export interface LanguageExtensions extends PublicAPI.Chrome.DevTools.LanguageExtensions { |
| 282 | _plugins: Map<PublicAPI.Chrome.DevTools.LanguageExtensionPlugin, MessagePort>; |
| 283 | } |
| 284 | |
| 285 | export interface ExtensionPanel extends ExtensionView, PublicAPI.Chrome.DevTools.ExtensionPanel { |
| 286 | show(): void; |
| 287 | } |
| 288 | |
| 289 | export interface Button extends PublicAPI.Chrome.DevTools.Button { |
| 290 | _id: string; |
| 291 | } |
| 292 | |
| 293 | export type ResourceData = {url: string, type: string}; |
| 294 | export interface Resource extends PublicAPI.Chrome.DevTools.Resource { |
| 295 | _type: string; |
| 296 | _url: string; |
| 297 | |
| 298 | get type(): string; |
| 299 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 300 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 301 | |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 302 | self.injectedExtensionAPI = function( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 303 | extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[], |
| 304 | testHook: (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown, |
| 305 | injectedScriptId: number): void { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 306 | const keysToForwardSet = new Set<number>(keysToForward); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 307 | const chrome = window.chrome || {}; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 308 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | const devtools_descriptor = Object.getOwnPropertyDescriptor(chrome, 'devtools'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 310 | if (devtools_descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 311 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 312 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 313 | let userAction = false; |
| 314 | |
| 315 | // Here and below, all constructors are private to API implementation. |
| 316 | // For a public type Foo, if internal fields are present, these are on |
| 317 | // a private FooImpl type, an instance of FooImpl is used in a closure |
| 318 | // by Foo consutrctor to re-bind publicly exported members to an instance |
| 319 | // of Foo. |
| 320 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 321 | function EventSinkImpl<ListenerT extends APIImpl.Callable>( |
| 322 | this: APIImpl.EventSink<ListenerT>, type: string, |
| 323 | customDispatch?: (this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 324 | this._type = type; |
| 325 | this._listeners = []; |
| 326 | this._customDispatch = customDispatch; |
| 327 | } |
| 328 | |
| 329 | EventSinkImpl.prototype = { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 330 | addListener: function<ListenerT extends APIImpl.Callable>(this: APIImpl.EventSink<ListenerT>, callback: ListenerT): |
| 331 | void { |
| 332 | if (typeof callback !== 'function') { |
| 333 | throw 'addListener: callback is not a function'; |
| 334 | } |
| 335 | if (this._listeners.length === 0) { |
| 336 | extensionServer.sendRequest({command: PrivateAPI.Commands.Subscribe, type: this._type}); |
| 337 | } |
| 338 | this._listeners.push(callback); |
| 339 | extensionServer.registerHandler('notify-' + this._type, this._dispatch.bind(this)); |
| 340 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 341 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 342 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 343 | removeListener: function<ListenerT extends APIImpl.Callable>( |
| 344 | this: APIImpl.EventSink<ListenerT>, callback: ListenerT): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 345 | const listeners = this._listeners; |
| 346 | |
| 347 | for (let i = 0; i < listeners.length; ++i) { |
| 348 | if (listeners[i] === callback) { |
| 349 | listeners.splice(i, 1); |
| 350 | break; |
| 351 | } |
| 352 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 353 | if (this._listeners.length === 0) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 354 | extensionServer.sendRequest({command: PrivateAPI.Commands.Unsubscribe, type: this._type}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 355 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 356 | }, |
| 357 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 358 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 359 | _fire: function<ListenerT extends APIImpl.Callable>( |
| 360 | this: APIImpl.EventSink<ListenerT>, ..._vararg: Parameters<ListenerT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 361 | const listeners = this._listeners.slice(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 362 | for (let i = 0; i < listeners.length; ++i) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 363 | listeners[i].apply(null, Array.from(arguments)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 364 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 365 | }, |
| 366 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 367 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 368 | _dispatch: function<ListenerT extends APIImpl.Callable>( |
| 369 | this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 370 | if (this._customDispatch) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 371 | this._customDispatch.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 372 | } else { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 373 | this._fire.apply(this, request.arguments as Parameters<ListenerT>); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 374 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 375 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 376 | }; |
| 377 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 378 | function Constructor<NewT extends APIImpl.Callable>(ctor: NewT): new (...args: Parameters<NewT>) => |
| 379 | ThisParameterType<NewT> { |
| 380 | return ctor as unknown as new (...args: Parameters<NewT>) => ThisParameterType<NewT>; |
| 381 | } |
| 382 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 383 | /** |
| 384 | * @constructor |
| 385 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 386 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 387 | function InspectorExtensionAPI(this: APIImpl.InspectorExtensionAPI): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 388 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 389 | this.inspectedWindow = new InspectedWindow(); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 390 | this.panels = new (Constructor(Panels))(); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 391 | this.network = new (Constructor(Network))(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 392 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 393 | this.timeline = new Timeline(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 394 | // @ts-ignore |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 395 | this.languageServices = new LanguageServicesAPI(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 396 | defineDeprecatedProperty(this, 'webInspector', 'resources', 'network'); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @constructor |
| 401 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 402 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 403 | function Network(this: APIImpl.Network): void { |
| 404 | function dispatchRequestEvent( |
| 405 | this: APIImpl.EventSink<(request: PublicAPI.Chrome.DevTools.Request) => unknown>, |
| 406 | message: {arguments: unknown[]}): void { |
| 407 | const request = message.arguments[1] as APIImpl.Request & {__proto__: APIImpl.Request}; |
| 408 | |
| 409 | request.__proto__ = new (Constructor(Request))(message.arguments[0] as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 410 | this._fire(request); |
| 411 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 412 | |
| 413 | this.onRequestFinished = |
| 414 | new (Constructor(EventSink))(PrivateAPI.Events.NetworkRequestFinished, dispatchRequestEvent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 415 | defineDeprecatedProperty(this, 'network', 'onFinished', 'onRequestFinished'); |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 416 | |
| 417 | this.onNavigated = new (Constructor(EventSink))(PrivateAPI.Events.InspectedURLChanged); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 418 | } |
| 419 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 420 | (Network.prototype as Pick<APIImpl.Network, 'getHAR'|'addRequestHeaders'>) = { |
| 421 | getHAR: function(this: PublicAPI.Chrome.DevTools.Network, callback?: (harLog: Object) => unknown): void { |
| 422 | function callbackWrapper(response: unknown): void { |
| 423 | const result = |
| 424 | response as ({entries: Array<HAR.Log.EntryDTO&{__proto__?: APIImpl.Request, _requestId?: number}>}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 425 | const entries = (result && result.entries) || []; |
| 426 | for (let i = 0; i < entries.length; ++i) { |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 427 | entries[i].__proto__ = new (Constructor(Request))(entries[i]._requestId as number); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 428 | delete entries[i]._requestId; |
| 429 | } |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 430 | callback && callback(result as Object); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 431 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 432 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetHAR}, callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 433 | }, |
| 434 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 435 | addRequestHeaders: function(headers: {[key: string]: string}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 436 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 437 | {command: PrivateAPI.Commands.AddRequestHeaders, headers: headers, extensionId: window.location.hostname}); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 438 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 439 | }; |
| 440 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 441 | function RequestImpl(this: APIImpl.Request, id: number): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 442 | this._id = id; |
| 443 | } |
| 444 | |
Philip Pfaffe | 4b88c66 | 2021-06-25 12:30:47 | [diff] [blame] | 445 | (RequestImpl.prototype as Pick<APIImpl.Request, 'getContent'>) = { |
| 446 | getContent: function(this: APIImpl.Request, callback?: (content: string, encoding: string) => unknown): void { |
| 447 | function callbackWrapper(response: unknown): void { |
| 448 | const {content, encoding} = response as {content: string, encoding: string}; |
| 449 | callback && callback(content, encoding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 450 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 451 | extensionServer.sendRequest( |
| 452 | {command: PrivateAPI.Commands.GetRequestContent, id: this._id}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 453 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | /** |
| 457 | * @constructor |
| 458 | */ |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 459 | function Panels(this: APIImpl.Panels): void { |
| 460 | const panels: {[key: string]: ElementsPanel|SourcesPanel} = { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 461 | elements: new ElementsPanel(), |
| 462 | sources: new SourcesPanel(), |
| 463 | }; |
| 464 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 465 | function panelGetter(name: string): ElementsPanel|SourcesPanel { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 466 | return panels[name]; |
| 467 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 468 | for (const panel in panels) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 469 | Object.defineProperty(this, panel, {get: panelGetter.bind(null, panel), enumerable: true}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 470 | } |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 471 | this.applyStyleSheet = function(styleSheet: string): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 472 | extensionServer.sendRequest({command: PrivateAPI.Commands.ApplyStyleSheet, styleSheet: styleSheet}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 473 | }; |
| 474 | } |
| 475 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 476 | (Panels.prototype as Pick<APIImpl.Panels, 'create'|'setOpenResourceHandler'|'openResource'|'SearchAction'>) = { |
| 477 | create: function( |
| 478 | title: string, icon: string, page: string, |
| 479 | callback: (panel: PublicAPI.Chrome.DevTools.ExtensionPanel) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 480 | const id = 'extension-panel-' + extensionServer.nextObjectId(); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 481 | extensionServer.sendRequest( |
| 482 | {command: PrivateAPI.Commands.CreatePanel, id, title, page}, |
| 483 | callback && callback.bind(this, new (Constructor(ExtensionPanel))(id))); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 484 | }, |
| 485 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 486 | setOpenResourceHandler: function( |
| 487 | callback: (resource: PublicAPI.Chrome.DevTools.Resource, lineNumber: number) => unknown): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 488 | const hadHandler = extensionServer.hasHandler(PrivateAPI.Events.OpenResource); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 489 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 490 | function callbackWrapper(message: unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 491 | // Allow the panel to show itself when handling the event. |
| 492 | userAction = true; |
| 493 | try { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 494 | const {resource, lineNumber} = message as {resource: APIImpl.ResourceData, lineNumber: number}; |
| 495 | callback.call(null, new (Constructor(Resource))(resource), lineNumber); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 496 | } finally { |
| 497 | userAction = false; |
| 498 | } |
| 499 | } |
| 500 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 501 | if (!callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 502 | extensionServer.unregisterHandler(PrivateAPI.Events.OpenResource); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 503 | } else { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 504 | extensionServer.registerHandler(PrivateAPI.Events.OpenResource, callbackWrapper); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 505 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 506 | |
| 507 | // 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] | 508 | if (hadHandler === !callback) { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 509 | extensionServer.sendRequest( |
| 510 | {command: PrivateAPI.Commands.SetOpenResourceHandler, 'handlerPresent': Boolean(callback)}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 511 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 512 | }, |
| 513 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 514 | openResource: function(url: string, lineNumber: number, callback?: (response: unknown) => unknown): void { |
| 515 | extensionServer.sendRequest({command: PrivateAPI.Commands.OpenResource, url, lineNumber}, callback); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 516 | }, |
| 517 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 518 | get SearchAction(): {[key: string]: string} { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 519 | return { |
| 520 | CancelSearch: PrivateAPI.Panels.SearchAction.CancelSearch, |
| 521 | PerformSearch: PrivateAPI.Panels.SearchAction.PerformSearch, |
| 522 | NextSearchResult: PrivateAPI.Panels.SearchAction.NextSearchResult, |
| 523 | PreviousSearchResult: PrivateAPI.Panels.SearchAction.PreviousSearchResult, |
| 524 | }; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 525 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | /** |
| 529 | * @constructor |
| 530 | */ |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 531 | function ExtensionViewImpl(this: APIImpl.ExtensionView, id: string|null): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 532 | this._id = id; |
| 533 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 534 | function dispatchShowEvent( |
| 535 | this: APIImpl.EventSink<(window?: Window) => unknown>, message: {arguments: unknown[]}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 536 | const frameIndex = message.arguments[0]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 537 | if (typeof frameIndex === 'number') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 538 | this._fire(window.parent.frames[frameIndex]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 539 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 540 | this._fire(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 541 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | if (id) { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 545 | this.onShown = new (Constructor(EventSink))(PrivateAPI.Events.ViewShown + id, dispatchShowEvent); |
| 546 | |
| 547 | this.onHidden = new (Constructor(EventSink))(PrivateAPI.Events.ViewHidden + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * @constructor |
| 553 | * @extends {ExtensionViewImpl} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 554 | */ |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 555 | function PanelWithSidebarImpl(this: APIImpl.PanelWithSidebar, hostPanelName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 556 | ExtensionViewImpl.call(this, null); |
| 557 | this._hostPanelName = hostPanelName; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 558 | |
| 559 | this.onSelectionChanged = new (Constructor(EventSink))(PrivateAPI.Events.PanelObjectSelected + hostPanelName); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 560 | } |
| 561 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 562 | (PanelWithSidebarImpl.prototype as Pick<APIImpl.PanelWithSidebar, 'createSidebarPane'>& |
| 563 | {__proto__: APIImpl.ExtensionView}) = { |
| 564 | createSidebarPane: function( |
| 565 | this: APIImpl.PanelWithSidebar, title: string, |
| 566 | callback?: (pane: PublicAPI.Chrome.DevTools.ExtensionSidebarPane) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 567 | const id = 'extension-sidebar-' + extensionServer.nextObjectId(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 568 | function callbackWrapper(): void { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 569 | callback && callback(new (Constructor(ExtensionSidebarPane))(id)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 571 | extensionServer.sendRequest( |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 572 | {command: PrivateAPI.Commands.CreateSidebarPane, panel: this._hostPanelName, id, title}, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 573 | callback && callbackWrapper); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 574 | }, |
| 575 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 576 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 577 | }; |
| 578 | |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 579 | /** |
| 580 | * @constructor |
| 581 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 582 | function LanguageServicesAPIImpl(this: any): void { |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 583 | /** @type {!Map<*, !MessagePort>} */ |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 584 | this._plugins = new Map(); |
| 585 | } |
| 586 | |
| 587 | LanguageServicesAPIImpl.prototype = { |
Philip Pfaffe | d662bdf | 2021-06-25 13:30:32 | [diff] [blame] | 588 | registerLanguageExtensionPlugin: async function( |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 589 | plugin: any, pluginName: string, supportedScriptTypes: PublicAPI.Chrome.DevTools.SupportedScriptTypes): |
| 590 | Promise<void> { |
| 591 | if (this._plugins.has(plugin)) { |
| 592 | throw new Error(`Tried to register plugin '${pluginName}' twice`); |
| 593 | } |
| 594 | const channel = new MessageChannel(); |
| 595 | const port = channel.port1; |
| 596 | this._plugins.set(plugin, port); |
| 597 | port.onmessage = ({data: {requestId, method, parameters}}: MessageEvent<any>): void => { |
| 598 | console.time(`${requestId}: ${method}`); |
| 599 | dispatchMethodCall(method, parameters) |
| 600 | .then(result => port.postMessage({requestId, result})) |
| 601 | .catch(error => port.postMessage({requestId, error: {message: error.message}})) |
| 602 | .finally(() => console.timeEnd(`${requestId}: ${method}`)); |
| 603 | }; |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 604 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 605 | function dispatchMethodCall(method: string, parameters: any): Promise<any> { |
| 606 | switch (method) { |
| 607 | case PrivateAPI.LanguageExtensionPluginCommands.AddRawModule: |
| 608 | return plugin.addRawModule(parameters.rawModuleId, parameters.symbolsURL, parameters.rawModule); |
| 609 | case PrivateAPI.LanguageExtensionPluginCommands.RemoveRawModule: |
| 610 | return plugin.removeRawModule(parameters.rawModuleId); |
| 611 | case PrivateAPI.LanguageExtensionPluginCommands.SourceLocationToRawLocation: |
| 612 | return plugin.sourceLocationToRawLocation(parameters.sourceLocation); |
| 613 | case PrivateAPI.LanguageExtensionPluginCommands.RawLocationToSourceLocation: |
| 614 | return plugin.rawLocationToSourceLocation(parameters.rawLocation); |
| 615 | case PrivateAPI.LanguageExtensionPluginCommands.GetScopeInfo: |
| 616 | return plugin.getScopeInfo(parameters.type); |
| 617 | case PrivateAPI.LanguageExtensionPluginCommands.ListVariablesInScope: |
| 618 | return plugin.listVariablesInScope(parameters.rawLocation); |
| 619 | case PrivateAPI.LanguageExtensionPluginCommands.GetTypeInfo: |
| 620 | return plugin.getTypeInfo(parameters.expression, parameters.context); |
| 621 | case PrivateAPI.LanguageExtensionPluginCommands.GetFormatter: |
| 622 | return plugin.getFormatter(parameters.expressionOrField, parameters.context); |
| 623 | case PrivateAPI.LanguageExtensionPluginCommands.GetInspectableAddress: |
| 624 | if ('getInspectableAddress' in plugin) { |
| 625 | return plugin.getInspectableAddress(parameters.field); |
| 626 | } |
| 627 | return Promise.resolve({js: ''}); |
| 628 | case PrivateAPI.LanguageExtensionPluginCommands.GetFunctionInfo: |
| 629 | return plugin.getFunctionInfo(parameters.rawLocation); |
| 630 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedFunctionRanges: |
| 631 | return plugin.getInlinedFunctionRanges(parameters.rawLocation); |
| 632 | case PrivateAPI.LanguageExtensionPluginCommands.GetInlinedCalleesRanges: |
| 633 | return plugin.getInlinedCalleesRanges(parameters.rawLocation); |
| 634 | case PrivateAPI.LanguageExtensionPluginCommands.GetMappedLines: |
| 635 | if ('getMappedLines' in plugin) { |
| 636 | return plugin.getMappedLines(parameters.rawModuleId, parameters.sourceFileURL); |
| 637 | } |
| 638 | return Promise.resolve(undefined); |
Kim-Anh Tran | 8fd7d6f | 2021-01-28 11:07:02 | [diff] [blame] | 639 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 640 | throw new Error(`Unknown language plugin method ${method}`); |
| 641 | } |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 642 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 643 | await new Promise<void>(resolve => { |
| 644 | extensionServer.sendRequest( |
| 645 | { |
| 646 | command: PrivateAPI.Commands.RegisterLanguageExtensionPlugin, |
| 647 | pluginName, |
| 648 | port: channel.port2, |
| 649 | supportedScriptTypes, |
| 650 | }, |
| 651 | () => resolve(), [channel.port2]); |
| 652 | }); |
| 653 | }, |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 654 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 655 | unregisterLanguageExtensionPlugin: async function(plugin: any): Promise<void> { |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 656 | const port = this._plugins.get(plugin); |
| 657 | if (!port) { |
| 658 | throw new Error('Tried to unregister a plugin that was not previously registered'); |
| 659 | } |
| 660 | this._plugins.delete(plugin); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 661 | port.postMessage({event: PrivateAPI.LanguageExtensionPluginEvents.UnregisteredLanguageExtensionPlugin}); |
Benedikt Meurer | 929fc7c | 2020-11-20 14:21:06 | [diff] [blame] | 662 | port.close(); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 663 | }, |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 664 | }; |
| 665 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 666 | function declareInterfaceClass<ImplT extends APIImpl.Callable>(implConstructor: ImplT): ( |
| 667 | this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>) => void { |
| 668 | return function(this: ThisParameterType<ImplT>, ...args: Parameters<ImplT>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 669 | const impl = {__proto__: implConstructor.prototype}; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 670 | implConstructor.apply(impl, args); |
| 671 | populateInterfaceClass(this as {[key: string]: unknown}, impl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 672 | }; |
| 673 | } |
| 674 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 675 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 676 | function defineDeprecatedProperty(object: any, className: string, oldName: string, newName: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 677 | let warningGiven = false; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 678 | function getter(): unknown { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 679 | if (!warningGiven) { |
| 680 | console.warn(className + '.' + oldName + ' is deprecated. Use ' + className + '.' + newName + ' instead'); |
| 681 | warningGiven = true; |
| 682 | } |
| 683 | return object[newName]; |
| 684 | } |
| 685 | object.__defineGetter__(oldName, getter); |
| 686 | } |
| 687 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 688 | function extractCallbackArgument(args: IArguments): ((...args: unknown[]) => unknown)|undefined { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 689 | const lastArgument = args[args.length - 1]; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 690 | return typeof lastArgument === 'function' ? lastArgument as (...args: unknown[]) => unknown : undefined; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 691 | } |
| 692 | |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 693 | const LanguageServicesAPI = declareInterfaceClass(LanguageServicesAPIImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 694 | const Button = declareInterfaceClass(ButtonImpl); |
| 695 | const EventSink = declareInterfaceClass(EventSinkImpl); |
| 696 | const ExtensionPanel = declareInterfaceClass(ExtensionPanelImpl); |
| 697 | const ExtensionSidebarPane = declareInterfaceClass(ExtensionSidebarPaneImpl); |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 698 | /** |
| 699 | * @constructor |
| 700 | * @param {string} hostPanelName |
| 701 | */ |
| 702 | const PanelWithSidebarClass = declareInterfaceClass(PanelWithSidebarImpl); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 703 | const Request = declareInterfaceClass(RequestImpl); |
| 704 | const Resource = declareInterfaceClass(ResourceImpl); |
| 705 | const TraceSession = declareInterfaceClass(TraceSessionImpl); |
| 706 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 707 | |
| 708 | class ElementsPanel extends (Constructor(PanelWithSidebarClass)) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 709 | constructor() { |
| 710 | super('elements'); |
| 711 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 712 | } |
| 713 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 714 | |
| 715 | class SourcesPanel extends (Constructor(PanelWithSidebarClass)) { |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 716 | constructor() { |
| 717 | super('sources'); |
| 718 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 719 | } |
| 720 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 721 | /** |
| 722 | * @constructor |
| 723 | * @extends {ExtensionViewImpl} |
| 724 | */ |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 725 | function ExtensionPanelImpl(this: APIImpl.ExtensionPanel, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 726 | ExtensionViewImpl.call(this, id); |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 727 | |
| 728 | this.onSearch = new (Constructor(EventSink))(PrivateAPI.Events.PanelSearch + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 729 | } |
| 730 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 731 | (ExtensionPanelImpl.prototype as Pick<APIImpl.ExtensionPanel, 'createStatusBarButton'|'show'>& |
| 732 | {__proto__: APIImpl.ExtensionView}) = { |
| 733 | createStatusBarButton: function( |
| 734 | this: APIImpl.ExtensionPanel, iconPath: string, tooltipText: string, disabled: boolean): |
| 735 | PublicAPI.Chrome.DevTools.Button { |
| 736 | const id = 'button-' + extensionServer.nextObjectId(); |
| 737 | extensionServer.sendRequest({ |
| 738 | command: PrivateAPI.Commands.CreateToolbarButton, |
| 739 | panel: this._id as string, |
| 740 | id: id, |
| 741 | icon: iconPath, |
| 742 | tooltip: tooltipText, |
| 743 | disabled: Boolean(disabled), |
| 744 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 745 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 746 | return new (Constructor(Button))(id); |
| 747 | }, |
| 748 | |
| 749 | show: function(this: APIImpl.ExtensionPanel): void { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 750 | if (!userAction) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 751 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 752 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 753 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 754 | extensionServer.sendRequest({command: PrivateAPI.Commands.ShowPanel, id: this._id as string}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 755 | }, |
| 756 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 757 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 758 | }; |
| 759 | |
| 760 | /** |
| 761 | * @constructor |
| 762 | * @extends {ExtensionViewImpl} |
| 763 | */ |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 764 | function ExtensionSidebarPaneImpl(this: APIImpl.ExtensionSidebarPane, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 765 | ExtensionViewImpl.call(this, id); |
| 766 | } |
| 767 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 768 | (ExtensionSidebarPaneImpl.prototype as |
| 769 | Pick<APIImpl.ExtensionSidebarPane, 'setHeight'|'setExpression'|'setObject'|'setPage'>& |
| 770 | {__proto__: APIImpl.ExtensionView}) = { |
| 771 | setHeight: function(this: APIImpl.ExtensionSidebarPane, height: string): void { |
| 772 | extensionServer.sendRequest( |
| 773 | {command: PrivateAPI.Commands.SetSidebarHeight, id: this._id as string, height: height}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 774 | }, |
| 775 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 776 | setExpression: function( |
| 777 | this: APIImpl.ExtensionSidebarPane, expression: string, rootTitle: string, |
| 778 | evaluateOptions?: PrivateAPI.EvaluateOptions, _callback?: () => unknown): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 779 | extensionServer.sendRequest( |
| 780 | { |
| 781 | command: PrivateAPI.Commands.SetSidebarContent, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 782 | id: this._id as string, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 783 | expression: expression, |
| 784 | rootTitle: rootTitle, |
| 785 | evaluateOnPage: true, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 786 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : {}), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 787 | }, |
| 788 | extractCallbackArgument(arguments)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 789 | }, |
| 790 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 791 | setObject: function( |
| 792 | this: APIImpl.ExtensionSidebarPane, jsonObject: string, rootTitle?: string, callback?: () => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 793 | extensionServer.sendRequest( |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 794 | { |
| 795 | command: PrivateAPI.Commands.SetSidebarContent, |
| 796 | id: this._id as string, |
| 797 | expression: jsonObject, |
| 798 | rootTitle: rootTitle, |
| 799 | }, |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 800 | callback); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 801 | }, |
| 802 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 803 | setPage: function(this: APIImpl.ExtensionSidebarPane, page: string): void { |
| 804 | extensionServer.sendRequest({command: PrivateAPI.Commands.SetSidebarPage, id: this._id as string, page: page}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 805 | }, |
| 806 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 807 | __proto__: ExtensionViewImpl.prototype, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 808 | }; |
| 809 | |
| 810 | /** |
| 811 | * @constructor |
| 812 | */ |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 813 | function ButtonImpl(this: APIImpl.Button, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 814 | this._id = id; |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 815 | |
| 816 | this.onClicked = new (Constructor(EventSink))(PrivateAPI.Events.ButtonClicked + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 817 | } |
| 818 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 819 | (ButtonImpl.prototype as Pick<APIImpl.Button, 'update'>) = { |
| 820 | update: function(this: APIImpl.Button, iconPath?: string, tooltipText?: string, disabled?: boolean): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 821 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 822 | command: PrivateAPI.Commands.UpdateButton, |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 823 | id: this._id, |
| 824 | icon: iconPath, |
| 825 | tooltip: tooltipText, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 826 | disabled: Boolean(disabled), |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 827 | }); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 828 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 829 | }; |
| 830 | |
| 831 | /** |
| 832 | * @constructor |
| 833 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 834 | function Timeline(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | Timeline.prototype = { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 838 | // @ts-ignore |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 839 | addTraceProvider: function(categoryName: string, categoryTooltip: string): TraceProvider { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 840 | const id = 'extension-trace-provider-' + extensionServer.nextObjectId(); |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 841 | extensionServer.sendRequest({ |
| 842 | command: PrivateAPI.Commands.AddTraceProvider, |
| 843 | id: id, |
| 844 | categoryName: categoryName, |
| 845 | categoryTooltip: categoryTooltip, |
| 846 | }); |
| 847 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 848 | return new TraceProvider(id); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 849 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 850 | }; |
| 851 | |
| 852 | /** |
| 853 | * @constructor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 854 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 855 | function TraceSessionImpl(this: any, id: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 856 | this._id = id; |
| 857 | } |
| 858 | |
| 859 | TraceSessionImpl.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 860 | complete: function(url?: string, timeOffset?: number): void { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 861 | extensionServer.sendRequest({ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 862 | command: PrivateAPI.Commands.CompleteTraceSession, |
| 863 | id: this._id, |
| 864 | url: url || '', |
| 865 | timeOffset: timeOffset || 0, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 866 | }); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 867 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 868 | }; |
| 869 | |
| 870 | /** |
| 871 | * @constructor |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 872 | */ |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 873 | function TraceProvider(id: string): void { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 874 | function dispatchRecordingStarted(this: any, message: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 875 | const sessionId = message.arguments[0]; |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 876 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 877 | this._fire(new TraceSession(sessionId)); |
| 878 | } |
| 879 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 880 | // @ts-ignore |
| 881 | this.onRecordingStarted = new EventSink(PrivateAPI.Events.RecordingStarted + id, dispatchRecordingStarted); |
| 882 | // @ts-ignore |
| 883 | this.onRecordingStopped = new EventSink(PrivateAPI.Events.RecordingStopped + id); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /** |
| 887 | * @constructor |
| 888 | */ |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 889 | function InspectedWindow(this: any): void { |
| 890 | function dispatchResourceEvent(this: any, message: any): void { |
| 891 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 892 | this._fire(new Resource(message.arguments[0])); |
| 893 | } |
| 894 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 895 | function dispatchResourceContentEvent(this: any, message: any): void { |
| 896 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 897 | this._fire(new Resource(message.arguments[0]), message.arguments[1]); |
| 898 | } |
| 899 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 900 | // @ts-ignore |
| 901 | this.onResourceAdded = new EventSink(PrivateAPI.Events.ResourceAdded, dispatchResourceEvent); |
| 902 | this.onResourceContentCommitted = |
| 903 | // @ts-ignore |
| 904 | new EventSink(PrivateAPI.Events.ResourceContentCommitted, dispatchResourceContentEvent); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | InspectedWindow.prototype = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 908 | reload: function(optionsOrUserAgent: any): void { |
| 909 | let options: { |
| 910 | userAgent: string, |
| 911 | }|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 912 | if (typeof optionsOrUserAgent === 'object') { |
| 913 | options = optionsOrUserAgent; |
| 914 | } else if (typeof optionsOrUserAgent === 'string') { |
| 915 | options = {userAgent: optionsOrUserAgent}; |
| 916 | console.warn( |
| 917 | 'Passing userAgent as string parameter to inspectedWindow.reload() is deprecated. ' + |
| 918 | 'Use inspectedWindow.reload({ userAgent: value}) instead.'); |
| 919 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 920 | extensionServer.sendRequest({command: PrivateAPI.Commands.Reload, options: options}); |
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 | eval: function(expression: any, evaluateOptions: any): Object | |
| 924 | null { |
| 925 | const callback = extractCallbackArgument(arguments); |
| 926 | function callbackWrapper(result: any): void { |
| 927 | if (result.isError || result.isException) { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 928 | callback && callback(undefined, result); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 929 | } else { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 930 | callback && callback(result.value); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 931 | } |
| 932 | } |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 933 | extensionServer.sendRequest( |
| 934 | { |
| 935 | command: PrivateAPI.Commands.EvaluateOnInspectedPage, |
| 936 | expression: expression, |
| 937 | evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : undefined), |
| 938 | }, |
| 939 | callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 940 | return null; |
| 941 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 942 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 943 | getResources: function(callback: any): void { |
| 944 | function wrapResource(resourceData: any): any { |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 945 | // @ts-ignore |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 946 | return new Resource(resourceData); |
| 947 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 948 | function callbackWrapper(resources: any): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 949 | callback(resources.map(wrapResource)); |
| 950 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 951 | extensionServer.sendRequest({command: PrivateAPI.Commands.GetPageResources}, callback && callbackWrapper); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 952 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 953 | }; |
| 954 | |
| 955 | /** |
| 956 | * @constructor |
| 957 | */ |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 958 | function ResourceImpl(this: APIImpl.Resource, resourceData: APIImpl.ResourceData): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 959 | this._url = resourceData.url; |
| 960 | this._type = resourceData.type; |
| 961 | } |
| 962 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 963 | (ResourceImpl.prototype as Pick<APIImpl.Resource, 'url'|'type'|'getContent'|'setContent'>) = { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 964 | get url(): string { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 965 | return (this as APIImpl.Resource)._url; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 966 | }, |
| 967 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 968 | get type(): string { |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 969 | return (this as APIImpl.Resource)._type; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 970 | }, |
| 971 | |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 972 | getContent: function(this: APIImpl.Resource, callback?: (content: string, encoding: string) => unknown): void { |
| 973 | function callbackWrapper(response: unknown): void { |
| 974 | const {content, encoding} = response as {content: string, encoding: string}; |
| 975 | callback && callback(content, encoding); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 976 | } |
| 977 | |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 978 | extensionServer.sendRequest( |
| 979 | {command: PrivateAPI.Commands.GetResourceContent, url: this._url}, callback && callbackWrapper); |
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 | setContent: function( |
| 983 | this: APIImpl.Resource, content: string, commit: boolean, callback: (error?: Object) => unknown): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 984 | extensionServer.sendRequest( |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 985 | {command: PrivateAPI.Commands.SetResourceContent, url: this._url, content: content, commit: commit}, |
Philip Pfaffe | 6fd04c4 | 2021-06-25 12:31:48 | [diff] [blame] | 986 | callback as (response: unknown) => unknown); |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 987 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 988 | }; |
| 989 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 990 | function getTabId(): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 991 | return inspectedTabId; |
| 992 | } |
| 993 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 994 | let keyboardEventRequestQueue: KeyboardEventInit&{eventType: string}[] = []; |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 995 | let forwardTimer: number|null = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 996 | function forwardKeyboardEvent(event: KeyboardEvent): void { |
Jan Scheffler | e7d7bb1 | 2019-10-24 09:18:52 | [diff] [blame] | 997 | // Check if the event should be forwarded. |
| 998 | // This is a workaround for crbug.com/923338. |
| 999 | const focused = document.activeElement; |
| 1000 | if (focused) { |
| 1001 | const isInput = focused.nodeName === 'INPUT' || focused.nodeName === 'TEXTAREA'; |
| 1002 | if (isInput && !(event.ctrlKey || event.altKey || event.metaKey)) { |
| 1003 | return; |
| 1004 | } |
| 1005 | } |
| 1006 | |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1007 | let modifiers = 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1008 | if (event.shiftKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1009 | modifiers |= 1; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1010 | } |
| 1011 | if (event.ctrlKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1012 | modifiers |= 2; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1013 | } |
| 1014 | if (event.altKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1015 | modifiers |= 4; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1016 | } |
| 1017 | if (event.metaKey) { |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1018 | modifiers |= 8; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1019 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1020 | const num = (event.keyCode & 255) | (modifiers << 8); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1021 | // We only care about global hotkeys, not about random text |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1022 | if (!keysToForwardSet.has(num)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1023 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1024 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1025 | event.preventDefault(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1026 | const requestPayload = { |
| 1027 | eventType: event.type, |
| 1028 | ctrlKey: event.ctrlKey, |
| 1029 | altKey: event.altKey, |
| 1030 | metaKey: event.metaKey, |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 1031 | shiftKey: event.shiftKey, |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1032 | // @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] | 1033 | keyIdentifier: event.keyIdentifier, |
| 1034 | key: event.key, |
| 1035 | code: event.code, |
| 1036 | location: event.location, |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1037 | keyCode: event.keyCode, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1038 | }; |
| 1039 | keyboardEventRequestQueue.push(requestPayload); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1040 | if (!forwardTimer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1041 | forwardTimer = setTimeout(forwardEventQueue, 0); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1042 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1043 | } |
| 1044 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1045 | function forwardEventQueue(): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1046 | forwardTimer = null; |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1047 | extensionServer.sendRequest( |
| 1048 | {command: PrivateAPI.Commands.ForwardKeyboardEvent, entries: keyboardEventRequestQueue}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1049 | keyboardEventRequestQueue = []; |
| 1050 | } |
| 1051 | |
| 1052 | document.addEventListener('keydown', forwardKeyboardEvent, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1053 | |
| 1054 | /** |
| 1055 | * @constructor |
| 1056 | */ |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1057 | function ExtensionServerClient(this: APIImpl.ExtensionServerClient): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1058 | this._callbacks = {}; |
| 1059 | this._handlers = {}; |
| 1060 | this._lastRequestId = 0; |
| 1061 | this._lastObjectId = 0; |
| 1062 | |
| 1063 | this.registerHandler('callback', this._onCallback.bind(this)); |
| 1064 | |
| 1065 | const channel = new MessageChannel(); |
| 1066 | this._port = channel.port1; |
| 1067 | this._port.addEventListener('message', this._onMessage.bind(this), false); |
| 1068 | this._port.start(); |
| 1069 | |
| 1070 | window.parent.postMessage('registerExtension', '*', [channel.port2]); |
| 1071 | } |
| 1072 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1073 | (ExtensionServerClient.prototype as Pick< |
| 1074 | APIImpl.ExtensionServerClient, |
| 1075 | 'sendRequest'|'hasHandler'|'registerHandler'|'unregisterHandler'|'nextObjectId'|'_registerCallback'| |
| 1076 | '_onCallback'|'_onMessage'>) = { |
| 1077 | sendRequest: function( |
| 1078 | this: APIImpl.ExtensionServerClient, message: PrivateAPI.ServerRequests, |
| 1079 | callback?: (response: unknown) => unknown, transfers?: Transferable[]): void { |
| 1080 | if (typeof callback === 'function') { |
| 1081 | (message as PrivateAPI.ExtensionServerRequestMessage).requestId = this._registerCallback(callback); |
| 1082 | } |
| 1083 | // @ts-expect-error |
| 1084 | this._port.postMessage(message, transfers); |
| 1085 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1086 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1087 | hasHandler: function(this: APIImpl.ExtensionServerClient, command: string): boolean { |
Tim van der Lippe | d7cfd14 | 2021-01-07 12:17:24 | [diff] [blame] | 1088 | return Boolean(this._handlers[command]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1089 | }, |
| 1090 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1091 | registerHandler: function( |
| 1092 | this: APIImpl.ExtensionServerClient, command: string, handler: (request: {arguments: unknown[]}) => unknown): |
| 1093 | void { |
| 1094 | this._handlers[command] = handler; |
| 1095 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1096 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1097 | unregisterHandler: function(this: APIImpl.ExtensionServerClient, command: string): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1098 | delete this._handlers[command]; |
| 1099 | }, |
| 1100 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1101 | nextObjectId: function(this: APIImpl.ExtensionServerClient): string { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1102 | return injectedScriptId.toString() + '_' + ++this._lastObjectId; |
| 1103 | }, |
| 1104 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1105 | _registerCallback: function(this: APIImpl.ExtensionServerClient, callback: (response: unknown) => unknown): number { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1106 | const id = ++this._lastRequestId; |
| 1107 | this._callbacks[id] = callback; |
| 1108 | return id; |
| 1109 | }, |
| 1110 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1111 | _onCallback: function(this: APIImpl.ExtensionServerClient, request: {requestId: number, result: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1112 | if (request.requestId in this._callbacks) { |
| 1113 | const callback = this._callbacks[request.requestId]; |
| 1114 | delete this._callbacks[request.requestId]; |
| 1115 | callback(request.result); |
| 1116 | } |
| 1117 | }, |
| 1118 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1119 | _onMessage: function( |
| 1120 | this: APIImpl.ExtensionServerClient, |
| 1121 | event: MessageEvent<{command: string, requestId: number, arguments: unknown[]}>): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1122 | const request = event.data; |
| 1123 | const handler = this._handlers[request.command]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1124 | if (handler) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1125 | handler.call(this, request); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1126 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1127 | }, |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1128 | }; |
| 1129 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1130 | function populateInterfaceClass(interfaze: {[key: string]: unknown}, implementation: {[key: string]: unknown}): void { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1131 | for (const member in implementation) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1132 | if (member.charAt(0) === '_') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1133 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1134 | } |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1135 | let descriptor: (PropertyDescriptor|undefined)|null = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1136 | // Traverse prototype chain until we find the owner. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1137 | for (let owner = implementation; owner && !descriptor; owner = owner.__proto__ as {[key: string]: unknown}) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1138 | descriptor = Object.getOwnPropertyDescriptor(owner, member); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1139 | } |
| 1140 | if (!descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1141 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1142 | } |
| 1143 | if (typeof descriptor.value === 'function') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1144 | interfaze[member] = descriptor.value.bind(implementation); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1145 | } else if (typeof descriptor.get === 'function') { |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1146 | // @ts-expect-error |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1147 | interfaze.__defineGetter__(member, descriptor.get.bind(implementation)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1148 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1149 | Object.defineProperty(interfaze, member, descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1150 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1154 | |
| 1155 | const extensionServer = new (Constructor(ExtensionServerClient))(); |
| 1156 | |
| 1157 | const coreAPI = new (Constructor(InspectorExtensionAPI))(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1158 | |
| 1159 | Object.defineProperty(chrome, 'devtools', {value: {}, enumerable: true}); |
| 1160 | |
| 1161 | // Only expose tabId on chrome.devtools.inspectedWindow, not webInspector.inspectedWindow. |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1162 | // @ts-expect-error |
| 1163 | chrome.devtools!.inspectedWindow = {}; |
| 1164 | Object.defineProperty(chrome.devtools!.inspectedWindow, 'tabId', {get: getTabId}); |
| 1165 | // @ts-expect-error |
| 1166 | chrome.devtools!.inspectedWindow.__proto__ = coreAPI.inspectedWindow; |
| 1167 | chrome.devtools!.network = coreAPI.network; |
| 1168 | chrome.devtools!.panels = coreAPI.panels; |
| 1169 | chrome.devtools!.panels.themeName = themeName; |
| 1170 | chrome.devtools!.languageServices = coreAPI.languageServices; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1171 | |
| 1172 | // default to expose experimental APIs for now. |
| 1173 | if (extensionInfo.exposeExperimentalAPIs !== false) { |
| 1174 | chrome.experimental = chrome.experimental || {}; |
| 1175 | chrome.experimental.devtools = chrome.experimental.devtools || {}; |
| 1176 | |
| 1177 | const properties = Object.getOwnPropertyNames(coreAPI); |
| 1178 | for (let i = 0; i < properties.length; ++i) { |
| 1179 | const descriptor = Object.getOwnPropertyDescriptor(coreAPI, properties[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1180 | if (descriptor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1181 | Object.defineProperty(chrome.experimental.devtools, properties[i], descriptor); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1182 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1183 | } |
| 1184 | chrome.experimental.devtools.inspectedWindow = chrome.devtools.inspectedWindow; |
| 1185 | } |
| 1186 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1187 | if (extensionInfo.exposeWebInspectorNamespace) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1188 | window.webInspector = coreAPI; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1189 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1190 | testHook(extensionServer, coreAPI); |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1191 | }; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1192 | |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1193 | self.buildExtensionAPIInjectedScript = function( |
| 1194 | extensionInfo: { |
| 1195 | startPage: string, |
| 1196 | name: string, |
| 1197 | exposeExperimentalAPIs: boolean, |
| 1198 | }, |
| 1199 | inspectedTabId: string, themeName: string, keysToForward: number[], |
Philip Pfaffe | 7523faf | 2021-06-28 14:23:14 | [diff] [blame] | 1200 | testHook: |
| 1201 | ((extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown)| |
| 1202 | undefined): string { |
Philip Pfaffe | edad832 | 2020-07-20 10:24:25 | [diff] [blame] | 1203 | const argumentsJSON = |
| 1204 | [extensionInfo, inspectedTabId || null, themeName, keysToForward].map(_ => JSON.stringify(_)).join(','); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1205 | if (!testHook) { |
Jan Scheffler | d76b416 | 2021-03-29 07:52:16 | [diff] [blame] | 1206 | testHook = (): void => {}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 1207 | } |
Philip Pfaffe | 939605d | 2021-06-25 12:20:04 | [diff] [blame] | 1208 | return '(function(injectedScriptId){ ' + |
Tim van der Lippe | 226fc22 | 2019-10-10 12:17:12 | [diff] [blame] | 1209 | '(' + self.injectedExtensionAPI.toString() + ')(' + argumentsJSON + ',' + testHook + ', injectedScriptId);' + |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1210 | '})'; |
Tim van der Lippe | 29fab47 | 2019-08-15 14:46:48 | [diff] [blame] | 1211 | }; |