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