blob: 11993cbd0723d54c8427389507a409f914e0a92e [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
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 Pfaffe7523faf2021-06-28 14:23:1431import type * as PublicAPI from '../../../extension-api/ExtensionAPI'; // eslint-disable-line rulesdir/es_modules_import
Philip Pfaffe4b88c662021-06-25 12:30:4732import type * as HAR from '../har/har.js';
Philip Pfaffe7523faf2021-06-28 14:23:1433
34/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/naming-convention,@typescript-eslint/no-non-null-assertion */
Philip Pfaffe939605d2021-06-25 12:20:0435export 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 Lippe1d6e57a2019-09-30 11:55:3443 }
Blink Reformat4c46d092018-04-07 15:32:3744
Philip Pfaffe939605d2021-06-25 12:20:0445 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 Reformat4c46d092018-04-07 15:32:3759
Philip Pfaffe939605d2021-06-25 12:20:0460 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 Pfaffeedad8322020-07-20 10:24:2587
Philip Pfaffe939605d2021-06-25 12:20:0488 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 Meurer929fc7c2020-11-20 14:21:06103
Philip Pfaffe939605d2021-06-25 12:20:04104 export const enum LanguageExtensionPluginEvents {
105 UnregisteredLanguageExtensionPlugin = 'unregisteredLanguageExtensionPlugin',
106 }
Philip Pfaffed662bdf2021-06-25 13:30:32107
108 export interface EvaluateOptions {
109 frameURL?: string;
110 useContentScriptContext?: boolean;
111 scriptExecutionContext?: string;
112 }
113
Philip Pfaffed662bdf2021-06-25 13:30:32114 type RegisterLanguageExtensionPluginRequest = {
115 command: Commands.RegisterLanguageExtensionPlugin,
116 pluginName: string,
117 port: MessagePort,
Philip Pfaffe7523faf2021-06-28 14:23:14118 supportedScriptTypes: PublicAPI.Chrome.DevTools.SupportedScriptTypes,
Philip Pfaffed662bdf2021-06-25 13:30:32119 };
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 Reformat4c46d092018-04-07 15:32:37187}
188
Philip Pfaffe939605d2021-06-25 12:20:04189declare global {
190 interface Window {
191 injectedExtensionAPI:
192 (extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[],
Philip Pfaffe7523faf2021-06-28 14:23:14193 testHook:
194 (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown,
195 injectedScriptId: number) => void;
Philip Pfaffe939605d2021-06-25 12:20:04196 buildExtensionAPIInjectedScript(
197 extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[],
Philip Pfaffed662bdf2021-06-25 13:30:32198 testHook: undefined|((extensionServer: unknown, extensionAPI: unknown) => unknown)): string;
Philip Pfaffe7523faf2021-06-28 14:23:14199 chrome: PublicAPI.Chrome.DevTools.Chrome;
200 webInspector?: APIImpl.InspectorExtensionAPI;
Philip Pfaffe939605d2021-06-25 12:20:04201 }
202}
203
204export type ExtensionDescriptor = {
205 startPage: string,
206 name: string,
207 exposeExperimentalAPIs: boolean,
208 exposeWebInspectorNamespace?: boolean,
209};
210
Philip Pfaffe7523faf2021-06-28 14:23:14211namespace 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 Pfaffe4b88c662021-06-25 12:30:47251
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 Pfaffe6fd04c42021-06-25 12:31:48259
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 Pfaffe7523faf2021-06-28 14:23:14300}
Philip Pfaffe939605d2021-06-25 12:20:04301
Tim van der Lippe226fc222019-10-10 12:17:12302self.injectedExtensionAPI = function(
Philip Pfaffe7523faf2021-06-28 14:23:14303 extensionInfo: ExtensionDescriptor, inspectedTabId: string, themeName: string, keysToForward: number[],
304 testHook: (extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown,
305 injectedScriptId: number): void {
Jan Schefflerd76b4162021-03-29 07:52:16306 const keysToForwardSet = new Set<number>(keysToForward);
Blink Reformat4c46d092018-04-07 15:32:37307 const chrome = window.chrome || {};
Jan Schefflerd76b4162021-03-29 07:52:16308
Blink Reformat4c46d092018-04-07 15:32:37309 const devtools_descriptor = Object.getOwnPropertyDescriptor(chrome, 'devtools');
Tim van der Lippe1d6e57a2019-09-30 11:55:34310 if (devtools_descriptor) {
Blink Reformat4c46d092018-04-07 15:32:37311 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34312 }
Blink Reformat4c46d092018-04-07 15:32:37313 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 Pfaffe7523faf2021-06-28 14:23:14321 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 Reformat4c46d092018-04-07 15:32:37324 this._type = type;
325 this._listeners = [];
326 this._customDispatch = customDispatch;
327 }
328
329 EventSinkImpl.prototype = {
Philip Pfaffe7523faf2021-06-28 14:23:14330 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 Reformat4c46d092018-04-07 15:32:37341
Jan Schefflerd76b4162021-03-29 07:52:16342
Philip Pfaffe7523faf2021-06-28 14:23:14343 removeListener: function<ListenerT extends APIImpl.Callable>(
344 this: APIImpl.EventSink<ListenerT>, callback: ListenerT): void {
Blink Reformat4c46d092018-04-07 15:32:37345 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 Lippe1d6e57a2019-09-30 11:55:34353 if (this._listeners.length === 0) {
Philip Pfaffe939605d2021-06-25 12:20:04354 extensionServer.sendRequest({command: PrivateAPI.Commands.Unsubscribe, type: this._type});
Tim van der Lippe1d6e57a2019-09-30 11:55:34355 }
Blink Reformat4c46d092018-04-07 15:32:37356 },
357
Jan Schefflerd76b4162021-03-29 07:52:16358
Philip Pfaffe7523faf2021-06-28 14:23:14359 _fire: function<ListenerT extends APIImpl.Callable>(
360 this: APIImpl.EventSink<ListenerT>, ..._vararg: Parameters<ListenerT>): void {
Blink Reformat4c46d092018-04-07 15:32:37361 const listeners = this._listeners.slice();
Tim van der Lippe1d6e57a2019-09-30 11:55:34362 for (let i = 0; i < listeners.length; ++i) {
Philip Pfaffe7523faf2021-06-28 14:23:14363 listeners[i].apply(null, Array.from(arguments));
Tim van der Lippe1d6e57a2019-09-30 11:55:34364 }
Blink Reformat4c46d092018-04-07 15:32:37365 },
366
Jan Schefflerd76b4162021-03-29 07:52:16367
Philip Pfaffe7523faf2021-06-28 14:23:14368 _dispatch: function<ListenerT extends APIImpl.Callable>(
369 this: APIImpl.EventSink<ListenerT>, request: {arguments: unknown[]}): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34370 if (this._customDispatch) {
Blink Reformat4c46d092018-04-07 15:32:37371 this._customDispatch.call(this, request);
Tim van der Lippe1d6e57a2019-09-30 11:55:34372 } else {
Philip Pfaffe7523faf2021-06-28 14:23:14373 this._fire.apply(this, request.arguments as Parameters<ListenerT>);
Tim van der Lippe1d6e57a2019-09-30 11:55:34374 }
Jan Schefflerd76b4162021-03-29 07:52:16375 },
Blink Reformat4c46d092018-04-07 15:32:37376 };
377
Philip Pfaffe7523faf2021-06-28 14:23:14378 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 Reformat4c46d092018-04-07 15:32:37383 /**
384 * @constructor
385 */
Jan Schefflerd76b4162021-03-29 07:52:16386
Philip Pfaffe7523faf2021-06-28 14:23:14387 function InspectorExtensionAPI(this: APIImpl.InspectorExtensionAPI): void {
Philip Pfaffe939605d2021-06-25 12:20:04388 // @ts-ignore
Blink Reformat4c46d092018-04-07 15:32:37389 this.inspectedWindow = new InspectedWindow();
Philip Pfaffe6fd04c42021-06-25 12:31:48390 this.panels = new (Constructor(Panels))();
Philip Pfaffe4b88c662021-06-25 12:30:47391 this.network = new (Constructor(Network))();
Philip Pfaffe939605d2021-06-25 12:20:04392 // @ts-ignore
Blink Reformat4c46d092018-04-07 15:32:37393 this.timeline = new Timeline();
Philip Pfaffe939605d2021-06-25 12:20:04394 // @ts-ignore
Philip Pfaffeedad8322020-07-20 10:24:25395 this.languageServices = new LanguageServicesAPI();
Blink Reformat4c46d092018-04-07 15:32:37396 defineDeprecatedProperty(this, 'webInspector', 'resources', 'network');
397 }
398
399 /**
400 * @constructor
401 */
Jan Schefflerd76b4162021-03-29 07:52:16402
Philip Pfaffe4b88c662021-06-25 12:30:47403 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 Reformat4c46d092018-04-07 15:32:37410 this._fire(request);
411 }
Philip Pfaffe4b88c662021-06-25 12:30:47412
413 this.onRequestFinished =
414 new (Constructor(EventSink))(PrivateAPI.Events.NetworkRequestFinished, dispatchRequestEvent);
Blink Reformat4c46d092018-04-07 15:32:37415 defineDeprecatedProperty(this, 'network', 'onFinished', 'onRequestFinished');
Philip Pfaffe4b88c662021-06-25 12:30:47416
417 this.onNavigated = new (Constructor(EventSink))(PrivateAPI.Events.InspectedURLChanged);
Blink Reformat4c46d092018-04-07 15:32:37418 }
419
Philip Pfaffe4b88c662021-06-25 12:30:47420 (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 Reformat4c46d092018-04-07 15:32:37425 const entries = (result && result.entries) || [];
426 for (let i = 0; i < entries.length; ++i) {
Philip Pfaffe4b88c662021-06-25 12:30:47427 entries[i].__proto__ = new (Constructor(Request))(entries[i]._requestId as number);
Blink Reformat4c46d092018-04-07 15:32:37428 delete entries[i]._requestId;
429 }
Philip Pfaffe4b88c662021-06-25 12:30:47430 callback && callback(result as Object);
Blink Reformat4c46d092018-04-07 15:32:37431 }
Philip Pfaffe939605d2021-06-25 12:20:04432 extensionServer.sendRequest({command: PrivateAPI.Commands.GetHAR}, callback && callbackWrapper);
Blink Reformat4c46d092018-04-07 15:32:37433 },
434
Philip Pfaffe4b88c662021-06-25 12:30:47435 addRequestHeaders: function(headers: {[key: string]: string}): void {
Blink Reformat4c46d092018-04-07 15:32:37436 extensionServer.sendRequest(
Philip Pfaffe939605d2021-06-25 12:20:04437 {command: PrivateAPI.Commands.AddRequestHeaders, headers: headers, extensionId: window.location.hostname});
Jan Schefflerd76b4162021-03-29 07:52:16438 },
Blink Reformat4c46d092018-04-07 15:32:37439 };
440
Philip Pfaffe4b88c662021-06-25 12:30:47441 function RequestImpl(this: APIImpl.Request, id: number): void {
Blink Reformat4c46d092018-04-07 15:32:37442 this._id = id;
443 }
444
Philip Pfaffe4b88c662021-06-25 12:30:47445 (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 Reformat4c46d092018-04-07 15:32:37450 }
Philip Pfaffe939605d2021-06-25 12:20:04451 extensionServer.sendRequest(
452 {command: PrivateAPI.Commands.GetRequestContent, id: this._id}, callback && callbackWrapper);
Jan Schefflerd76b4162021-03-29 07:52:16453 },
Blink Reformat4c46d092018-04-07 15:32:37454 };
455
456 /**
457 * @constructor
458 */
Philip Pfaffe6fd04c42021-06-25 12:31:48459 function Panels(this: APIImpl.Panels): void {
460 const panels: {[key: string]: ElementsPanel|SourcesPanel} = {
Blink Reformat4c46d092018-04-07 15:32:37461 elements: new ElementsPanel(),
462 sources: new SourcesPanel(),
463 };
464
Philip Pfaffe6fd04c42021-06-25 12:31:48465 function panelGetter(name: string): ElementsPanel|SourcesPanel {
Blink Reformat4c46d092018-04-07 15:32:37466 return panels[name];
467 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34468 for (const panel in panels) {
Tim van der Lippeffa78622019-09-16 12:07:12469 Object.defineProperty(this, panel, {get: panelGetter.bind(null, panel), enumerable: true});
Tim van der Lippe1d6e57a2019-09-30 11:55:34470 }
Philip Pfaffe6fd04c42021-06-25 12:31:48471 this.applyStyleSheet = function(styleSheet: string): void {
Philip Pfaffe939605d2021-06-25 12:20:04472 extensionServer.sendRequest({command: PrivateAPI.Commands.ApplyStyleSheet, styleSheet: styleSheet});
Blink Reformat4c46d092018-04-07 15:32:37473 };
474 }
475
Philip Pfaffe6fd04c42021-06-25 12:31:48476 (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 Reformat4c46d092018-04-07 15:32:37480 const id = 'extension-panel-' + extensionServer.nextObjectId();
Philip Pfaffe6fd04c42021-06-25 12:31:48481 extensionServer.sendRequest(
482 {command: PrivateAPI.Commands.CreatePanel, id, title, page},
483 callback && callback.bind(this, new (Constructor(ExtensionPanel))(id)));
Blink Reformat4c46d092018-04-07 15:32:37484 },
485
Philip Pfaffe6fd04c42021-06-25 12:31:48486 setOpenResourceHandler: function(
487 callback: (resource: PublicAPI.Chrome.DevTools.Resource, lineNumber: number) => unknown): void {
Philip Pfaffe939605d2021-06-25 12:20:04488 const hadHandler = extensionServer.hasHandler(PrivateAPI.Events.OpenResource);
Blink Reformat4c46d092018-04-07 15:32:37489
Philip Pfaffe6fd04c42021-06-25 12:31:48490 function callbackWrapper(message: unknown): void {
Blink Reformat4c46d092018-04-07 15:32:37491 // Allow the panel to show itself when handling the event.
492 userAction = true;
493 try {
Philip Pfaffe6fd04c42021-06-25 12:31:48494 const {resource, lineNumber} = message as {resource: APIImpl.ResourceData, lineNumber: number};
495 callback.call(null, new (Constructor(Resource))(resource), lineNumber);
Blink Reformat4c46d092018-04-07 15:32:37496 } finally {
497 userAction = false;
498 }
499 }
500
Tim van der Lippe1d6e57a2019-09-30 11:55:34501 if (!callback) {
Philip Pfaffe939605d2021-06-25 12:20:04502 extensionServer.unregisterHandler(PrivateAPI.Events.OpenResource);
Tim van der Lippe1d6e57a2019-09-30 11:55:34503 } else {
Philip Pfaffe939605d2021-06-25 12:20:04504 extensionServer.registerHandler(PrivateAPI.Events.OpenResource, callbackWrapper);
Tim van der Lippe1d6e57a2019-09-30 11:55:34505 }
Blink Reformat4c46d092018-04-07 15:32:37506
507 // Only send command if we either removed an existing handler or added handler and had none before.
Tim van der Lippe1d6e57a2019-09-30 11:55:34508 if (hadHandler === !callback) {
Philip Pfaffe939605d2021-06-25 12:20:04509 extensionServer.sendRequest(
510 {command: PrivateAPI.Commands.SetOpenResourceHandler, 'handlerPresent': Boolean(callback)});
Tim van der Lippe1d6e57a2019-09-30 11:55:34511 }
Blink Reformat4c46d092018-04-07 15:32:37512 },
513
Philip Pfaffe6fd04c42021-06-25 12:31:48514 openResource: function(url: string, lineNumber: number, callback?: (response: unknown) => unknown): void {
515 extensionServer.sendRequest({command: PrivateAPI.Commands.OpenResource, url, lineNumber}, callback);
Blink Reformat4c46d092018-04-07 15:32:37516 },
517
Philip Pfaffe6fd04c42021-06-25 12:31:48518 get SearchAction(): {[key: string]: string} {
Philip Pfaffe939605d2021-06-25 12:20:04519 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 Schefflerd76b4162021-03-29 07:52:16525 },
Blink Reformat4c46d092018-04-07 15:32:37526 };
527
528 /**
529 * @constructor
530 */
Philip Pfaffe6fd04c42021-06-25 12:31:48531 function ExtensionViewImpl(this: APIImpl.ExtensionView, id: string|null): void {
Blink Reformat4c46d092018-04-07 15:32:37532 this._id = id;
533
Philip Pfaffe6fd04c42021-06-25 12:31:48534 function dispatchShowEvent(
535 this: APIImpl.EventSink<(window?: Window) => unknown>, message: {arguments: unknown[]}): void {
Blink Reformat4c46d092018-04-07 15:32:37536 const frameIndex = message.arguments[0];
Tim van der Lippe1d6e57a2019-09-30 11:55:34537 if (typeof frameIndex === 'number') {
Blink Reformat4c46d092018-04-07 15:32:37538 this._fire(window.parent.frames[frameIndex]);
Tim van der Lippe1d6e57a2019-09-30 11:55:34539 } else {
Blink Reformat4c46d092018-04-07 15:32:37540 this._fire();
Tim van der Lippe1d6e57a2019-09-30 11:55:34541 }
Blink Reformat4c46d092018-04-07 15:32:37542 }
543
544 if (id) {
Philip Pfaffe6fd04c42021-06-25 12:31:48545 this.onShown = new (Constructor(EventSink))(PrivateAPI.Events.ViewShown + id, dispatchShowEvent);
546
547 this.onHidden = new (Constructor(EventSink))(PrivateAPI.Events.ViewHidden + id);
Blink Reformat4c46d092018-04-07 15:32:37548 }
549 }
550
551 /**
552 * @constructor
553 * @extends {ExtensionViewImpl}
Blink Reformat4c46d092018-04-07 15:32:37554 */
Philip Pfaffe6fd04c42021-06-25 12:31:48555 function PanelWithSidebarImpl(this: APIImpl.PanelWithSidebar, hostPanelName: string): void {
Blink Reformat4c46d092018-04-07 15:32:37556 ExtensionViewImpl.call(this, null);
557 this._hostPanelName = hostPanelName;
Philip Pfaffe6fd04c42021-06-25 12:31:48558
559 this.onSelectionChanged = new (Constructor(EventSink))(PrivateAPI.Events.PanelObjectSelected + hostPanelName);
Blink Reformat4c46d092018-04-07 15:32:37560 }
561
Philip Pfaffe6fd04c42021-06-25 12:31:48562 (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 Reformat4c46d092018-04-07 15:32:37567 const id = 'extension-sidebar-' + extensionServer.nextObjectId();
Jan Schefflerd76b4162021-03-29 07:52:16568 function callbackWrapper(): void {
Philip Pfaffe6fd04c42021-06-25 12:31:48569 callback && callback(new (Constructor(ExtensionSidebarPane))(id));
Blink Reformat4c46d092018-04-07 15:32:37570 }
Philip Pfaffe7523faf2021-06-28 14:23:14571 extensionServer.sendRequest(
Philip Pfaffe6fd04c42021-06-25 12:31:48572 {command: PrivateAPI.Commands.CreateSidebarPane, panel: this._hostPanelName, id, title},
Philip Pfaffe7523faf2021-06-28 14:23:14573 callback && callbackWrapper);
Blink Reformat4c46d092018-04-07 15:32:37574 },
575
Jan Schefflerd76b4162021-03-29 07:52:16576 __proto__: ExtensionViewImpl.prototype,
Blink Reformat4c46d092018-04-07 15:32:37577 };
578
Philip Pfaffeedad8322020-07-20 10:24:25579 /**
580 * @constructor
581 */
Philip Pfaffe939605d2021-06-25 12:20:04582 function LanguageServicesAPIImpl(this: any): void {
Benedikt Meurer929fc7c2020-11-20 14:21:06583 /** @type {!Map<*, !MessagePort>} */
Philip Pfaffeedad8322020-07-20 10:24:25584 this._plugins = new Map();
585 }
586
587 LanguageServicesAPIImpl.prototype = {
Philip Pfaffed662bdf2021-06-25 13:30:32588 registerLanguageExtensionPlugin: async function(
Philip Pfaffe7523faf2021-06-28 14:23:14589 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 Pfaffeedad8322020-07-20 10:24:25604
Philip Pfaffe7523faf2021-06-28 14:23:14605 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 Tran8fd7d6f2021-01-28 11:07:02639 }
Philip Pfaffe7523faf2021-06-28 14:23:14640 throw new Error(`Unknown language plugin method ${method}`);
641 }
Philip Pfaffeedad8322020-07-20 10:24:25642
Philip Pfaffe7523faf2021-06-28 14:23:14643 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 Meurer929fc7c2020-11-20 14:21:06654
Jan Schefflerd76b4162021-03-29 07:52:16655 unregisterLanguageExtensionPlugin: async function(plugin: any): Promise<void> {
Benedikt Meurer929fc7c2020-11-20 14:21:06656 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 Pfaffe939605d2021-06-25 12:20:04661 port.postMessage({event: PrivateAPI.LanguageExtensionPluginEvents.UnregisteredLanguageExtensionPlugin});
Benedikt Meurer929fc7c2020-11-20 14:21:06662 port.close();
Jan Schefflerd76b4162021-03-29 07:52:16663 },
Philip Pfaffeedad8322020-07-20 10:24:25664 };
665
Philip Pfaffe7523faf2021-06-28 14:23:14666 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 Reformat4c46d092018-04-07 15:32:37669 const impl = {__proto__: implConstructor.prototype};
Philip Pfaffe7523faf2021-06-28 14:23:14670 implConstructor.apply(impl, args);
671 populateInterfaceClass(this as {[key: string]: unknown}, impl);
Blink Reformat4c46d092018-04-07 15:32:37672 };
673 }
674
Philip Pfaffe7523faf2021-06-28 14:23:14675 // eslint-disable-next-line @typescript-eslint/no-explicit-any
676 function defineDeprecatedProperty(object: any, className: string, oldName: string, newName: string): void {
Blink Reformat4c46d092018-04-07 15:32:37677 let warningGiven = false;
Philip Pfaffe7523faf2021-06-28 14:23:14678 function getter(): unknown {
Blink Reformat4c46d092018-04-07 15:32:37679 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 Pfaffe7523faf2021-06-28 14:23:14688 function extractCallbackArgument(args: IArguments): ((...args: unknown[]) => unknown)|undefined {
Blink Reformat4c46d092018-04-07 15:32:37689 const lastArgument = args[args.length - 1];
Philip Pfaffe7523faf2021-06-28 14:23:14690 return typeof lastArgument === 'function' ? lastArgument as (...args: unknown[]) => unknown : undefined;
Blink Reformat4c46d092018-04-07 15:32:37691 }
692
Philip Pfaffeedad8322020-07-20 10:24:25693 const LanguageServicesAPI = declareInterfaceClass(LanguageServicesAPIImpl);
Blink Reformat4c46d092018-04-07 15:32:37694 const Button = declareInterfaceClass(ButtonImpl);
695 const EventSink = declareInterfaceClass(EventSinkImpl);
696 const ExtensionPanel = declareInterfaceClass(ExtensionPanelImpl);
697 const ExtensionSidebarPane = declareInterfaceClass(ExtensionSidebarPaneImpl);
Tim van der Lippeffa78622019-09-16 12:07:12698 /**
699 * @constructor
700 * @param {string} hostPanelName
701 */
702 const PanelWithSidebarClass = declareInterfaceClass(PanelWithSidebarImpl);
Blink Reformat4c46d092018-04-07 15:32:37703 const Request = declareInterfaceClass(RequestImpl);
704 const Resource = declareInterfaceClass(ResourceImpl);
705 const TraceSession = declareInterfaceClass(TraceSessionImpl);
706
Philip Pfaffe6fd04c42021-06-25 12:31:48707
708 class ElementsPanel extends (Constructor(PanelWithSidebarClass)) {
Tim van der Lippeffa78622019-09-16 12:07:12709 constructor() {
710 super('elements');
711 }
Blink Reformat4c46d092018-04-07 15:32:37712 }
713
Philip Pfaffe6fd04c42021-06-25 12:31:48714
715 class SourcesPanel extends (Constructor(PanelWithSidebarClass)) {
Tim van der Lippeffa78622019-09-16 12:07:12716 constructor() {
717 super('sources');
718 }
Blink Reformat4c46d092018-04-07 15:32:37719 }
720
Blink Reformat4c46d092018-04-07 15:32:37721 /**
722 * @constructor
723 * @extends {ExtensionViewImpl}
724 */
Philip Pfaffe6fd04c42021-06-25 12:31:48725 function ExtensionPanelImpl(this: APIImpl.ExtensionPanel, id: string): void {
Blink Reformat4c46d092018-04-07 15:32:37726 ExtensionViewImpl.call(this, id);
Philip Pfaffe6fd04c42021-06-25 12:31:48727
728 this.onSearch = new (Constructor(EventSink))(PrivateAPI.Events.PanelSearch + id);
Blink Reformat4c46d092018-04-07 15:32:37729 }
730
Philip Pfaffe6fd04c42021-06-25 12:31:48731 (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 Reformat4c46d092018-04-07 15:32:37745
Philip Pfaffe6fd04c42021-06-25 12:31:48746 return new (Constructor(Button))(id);
747 },
748
749 show: function(this: APIImpl.ExtensionPanel): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34750 if (!userAction) {
Blink Reformat4c46d092018-04-07 15:32:37751 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34752 }
Blink Reformat4c46d092018-04-07 15:32:37753
Philip Pfaffe6fd04c42021-06-25 12:31:48754 extensionServer.sendRequest({command: PrivateAPI.Commands.ShowPanel, id: this._id as string});
Blink Reformat4c46d092018-04-07 15:32:37755 },
756
Jan Schefflerd76b4162021-03-29 07:52:16757 __proto__: ExtensionViewImpl.prototype,
Blink Reformat4c46d092018-04-07 15:32:37758 };
759
760 /**
761 * @constructor
762 * @extends {ExtensionViewImpl}
763 */
Philip Pfaffe6fd04c42021-06-25 12:31:48764 function ExtensionSidebarPaneImpl(this: APIImpl.ExtensionSidebarPane, id: string): void {
Blink Reformat4c46d092018-04-07 15:32:37765 ExtensionViewImpl.call(this, id);
766 }
767
Philip Pfaffe6fd04c42021-06-25 12:31:48768 (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 Reformat4c46d092018-04-07 15:32:37774 },
775
Philip Pfaffe6fd04c42021-06-25 12:31:48776 setExpression: function(
777 this: APIImpl.ExtensionSidebarPane, expression: string, rootTitle: string,
778 evaluateOptions?: PrivateAPI.EvaluateOptions, _callback?: () => unknown): void {
Philip Pfaffe7523faf2021-06-28 14:23:14779 extensionServer.sendRequest(
780 {
781 command: PrivateAPI.Commands.SetSidebarContent,
Philip Pfaffe6fd04c42021-06-25 12:31:48782 id: this._id as string,
Philip Pfaffe7523faf2021-06-28 14:23:14783 expression: expression,
784 rootTitle: rootTitle,
785 evaluateOnPage: true,
Philip Pfaffe6fd04c42021-06-25 12:31:48786 evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : {}),
Philip Pfaffe7523faf2021-06-28 14:23:14787 },
788 extractCallbackArgument(arguments));
Blink Reformat4c46d092018-04-07 15:32:37789 },
790
Philip Pfaffe6fd04c42021-06-25 12:31:48791 setObject: function(
792 this: APIImpl.ExtensionSidebarPane, jsonObject: string, rootTitle?: string, callback?: () => unknown): void {
Blink Reformat4c46d092018-04-07 15:32:37793 extensionServer.sendRequest(
Philip Pfaffe6fd04c42021-06-25 12:31:48794 {
795 command: PrivateAPI.Commands.SetSidebarContent,
796 id: this._id as string,
797 expression: jsonObject,
798 rootTitle: rootTitle,
799 },
Philip Pfaffe939605d2021-06-25 12:20:04800 callback);
Blink Reformat4c46d092018-04-07 15:32:37801 },
802
Philip Pfaffe6fd04c42021-06-25 12:31:48803 setPage: function(this: APIImpl.ExtensionSidebarPane, page: string): void {
804 extensionServer.sendRequest({command: PrivateAPI.Commands.SetSidebarPage, id: this._id as string, page: page});
Blink Reformat4c46d092018-04-07 15:32:37805 },
806
Jan Schefflerd76b4162021-03-29 07:52:16807 __proto__: ExtensionViewImpl.prototype,
Blink Reformat4c46d092018-04-07 15:32:37808 };
809
810 /**
811 * @constructor
812 */
Philip Pfaffe6fd04c42021-06-25 12:31:48813 function ButtonImpl(this: APIImpl.Button, id: string): void {
Blink Reformat4c46d092018-04-07 15:32:37814 this._id = id;
Philip Pfaffe6fd04c42021-06-25 12:31:48815
816 this.onClicked = new (Constructor(EventSink))(PrivateAPI.Events.ButtonClicked + id);
Blink Reformat4c46d092018-04-07 15:32:37817 }
818
Philip Pfaffe6fd04c42021-06-25 12:31:48819 (ButtonImpl.prototype as Pick<APIImpl.Button, 'update'>) = {
820 update: function(this: APIImpl.Button, iconPath?: string, tooltipText?: string, disabled?: boolean): void {
Philip Pfaffe7523faf2021-06-28 14:23:14821 extensionServer.sendRequest({
Philip Pfaffe939605d2021-06-25 12:20:04822 command: PrivateAPI.Commands.UpdateButton,
Tim van der Lipped7cfd142021-01-07 12:17:24823 id: this._id,
824 icon: iconPath,
825 tooltip: tooltipText,
Jan Schefflerd76b4162021-03-29 07:52:16826 disabled: Boolean(disabled),
Philip Pfaffe7523faf2021-06-28 14:23:14827 });
Jan Schefflerd76b4162021-03-29 07:52:16828 },
Blink Reformat4c46d092018-04-07 15:32:37829 };
830
831 /**
832 * @constructor
833 */
Jan Schefflerd76b4162021-03-29 07:52:16834 function Timeline(): void {
Blink Reformat4c46d092018-04-07 15:32:37835 }
836
837 Timeline.prototype = {
Philip Pfaffe939605d2021-06-25 12:20:04838 // @ts-ignore
Jan Schefflerd76b4162021-03-29 07:52:16839 addTraceProvider: function(categoryName: string, categoryTooltip: string): TraceProvider {
Blink Reformat4c46d092018-04-07 15:32:37840 const id = 'extension-trace-provider-' + extensionServer.nextObjectId();
Philip Pfaffe939605d2021-06-25 12:20:04841 extensionServer.sendRequest({
842 command: PrivateAPI.Commands.AddTraceProvider,
843 id: id,
844 categoryName: categoryName,
845 categoryTooltip: categoryTooltip,
846 });
847 // @ts-ignore
Blink Reformat4c46d092018-04-07 15:32:37848 return new TraceProvider(id);
Jan Schefflerd76b4162021-03-29 07:52:16849 },
Blink Reformat4c46d092018-04-07 15:32:37850 };
851
852 /**
853 * @constructor
Blink Reformat4c46d092018-04-07 15:32:37854 */
Philip Pfaffe939605d2021-06-25 12:20:04855 function TraceSessionImpl(this: any, id: string): void {
Blink Reformat4c46d092018-04-07 15:32:37856 this._id = id;
857 }
858
859 TraceSessionImpl.prototype = {
Jan Schefflerd76b4162021-03-29 07:52:16860 complete: function(url?: string, timeOffset?: number): void {
Philip Pfaffe7523faf2021-06-28 14:23:14861 extensionServer.sendRequest({
Philip Pfaffe939605d2021-06-25 12:20:04862 command: PrivateAPI.Commands.CompleteTraceSession,
863 id: this._id,
864 url: url || '',
865 timeOffset: timeOffset || 0,
Philip Pfaffe7523faf2021-06-28 14:23:14866 });
Jan Schefflerd76b4162021-03-29 07:52:16867 },
Blink Reformat4c46d092018-04-07 15:32:37868 };
869
870 /**
871 * @constructor
Blink Reformat4c46d092018-04-07 15:32:37872 */
Jan Schefflerd76b4162021-03-29 07:52:16873 function TraceProvider(id: string): void {
Philip Pfaffe939605d2021-06-25 12:20:04874 function dispatchRecordingStarted(this: any, message: any): void {
Blink Reformat4c46d092018-04-07 15:32:37875 const sessionId = message.arguments[0];
Philip Pfaffe939605d2021-06-25 12:20:04876 // @ts-ignore
Blink Reformat4c46d092018-04-07 15:32:37877 this._fire(new TraceSession(sessionId));
878 }
879
Philip Pfaffe939605d2021-06-25 12:20:04880 // @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 Reformat4c46d092018-04-07 15:32:37884 }
885
886 /**
887 * @constructor
888 */
Philip Pfaffe939605d2021-06-25 12:20:04889 function InspectedWindow(this: any): void {
890 function dispatchResourceEvent(this: any, message: any): void {
891 // @ts-ignore
Blink Reformat4c46d092018-04-07 15:32:37892 this._fire(new Resource(message.arguments[0]));
893 }
894
Philip Pfaffe939605d2021-06-25 12:20:04895 function dispatchResourceContentEvent(this: any, message: any): void {
896 // @ts-ignore
Blink Reformat4c46d092018-04-07 15:32:37897 this._fire(new Resource(message.arguments[0]), message.arguments[1]);
898 }
899
Philip Pfaffe939605d2021-06-25 12:20:04900 // @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 Reformat4c46d092018-04-07 15:32:37905 }
906
907 InspectedWindow.prototype = {
Jan Schefflerd76b4162021-03-29 07:52:16908 reload: function(optionsOrUserAgent: any): void {
909 let options: {
910 userAgent: string,
911 }|null = null;
Blink Reformat4c46d092018-04-07 15:32:37912 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 Pfaffe939605d2021-06-25 12:20:04920 extensionServer.sendRequest({command: PrivateAPI.Commands.Reload, options: options});
Blink Reformat4c46d092018-04-07 15:32:37921 },
922
Jan Schefflerd76b4162021-03-29 07:52:16923 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 Pfaffe7523faf2021-06-28 14:23:14928 callback && callback(undefined, result);
Jan Schefflerd76b4162021-03-29 07:52:16929 } else {
Philip Pfaffe7523faf2021-06-28 14:23:14930 callback && callback(result.value);
Jan Schefflerd76b4162021-03-29 07:52:16931 }
932 }
Philip Pfaffe7523faf2021-06-28 14:23:14933 extensionServer.sendRequest(
934 {
935 command: PrivateAPI.Commands.EvaluateOnInspectedPage,
936 expression: expression,
937 evaluateOptions: (typeof evaluateOptions === 'object' ? evaluateOptions : undefined),
938 },
939 callback && callbackWrapper);
Jan Schefflerd76b4162021-03-29 07:52:16940 return null;
941 },
Blink Reformat4c46d092018-04-07 15:32:37942
Jan Schefflerd76b4162021-03-29 07:52:16943 getResources: function(callback: any): void {
944 function wrapResource(resourceData: any): any {
Philip Pfaffe939605d2021-06-25 12:20:04945 // @ts-ignore
Blink Reformat4c46d092018-04-07 15:32:37946 return new Resource(resourceData);
947 }
Jan Schefflerd76b4162021-03-29 07:52:16948 function callbackWrapper(resources: any): void {
Blink Reformat4c46d092018-04-07 15:32:37949 callback(resources.map(wrapResource));
950 }
Philip Pfaffe939605d2021-06-25 12:20:04951 extensionServer.sendRequest({command: PrivateAPI.Commands.GetPageResources}, callback && callbackWrapper);
Jan Schefflerd76b4162021-03-29 07:52:16952 },
Blink Reformat4c46d092018-04-07 15:32:37953 };
954
955 /**
956 * @constructor
957 */
Philip Pfaffe6fd04c42021-06-25 12:31:48958 function ResourceImpl(this: APIImpl.Resource, resourceData: APIImpl.ResourceData): void {
Blink Reformat4c46d092018-04-07 15:32:37959 this._url = resourceData.url;
960 this._type = resourceData.type;
961 }
962
Philip Pfaffe6fd04c42021-06-25 12:31:48963 (ResourceImpl.prototype as Pick<APIImpl.Resource, 'url'|'type'|'getContent'|'setContent'>) = {
Jan Schefflerd76b4162021-03-29 07:52:16964 get url(): string {
Philip Pfaffe6fd04c42021-06-25 12:31:48965 return (this as APIImpl.Resource)._url;
Blink Reformat4c46d092018-04-07 15:32:37966 },
967
Jan Schefflerd76b4162021-03-29 07:52:16968 get type(): string {
Philip Pfaffe6fd04c42021-06-25 12:31:48969 return (this as APIImpl.Resource)._type;
Blink Reformat4c46d092018-04-07 15:32:37970 },
971
Philip Pfaffe6fd04c42021-06-25 12:31:48972 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 Reformat4c46d092018-04-07 15:32:37976 }
977
Philip Pfaffe939605d2021-06-25 12:20:04978 extensionServer.sendRequest(
979 {command: PrivateAPI.Commands.GetResourceContent, url: this._url}, callback && callbackWrapper);
Blink Reformat4c46d092018-04-07 15:32:37980 },
981
Philip Pfaffe6fd04c42021-06-25 12:31:48982 setContent: function(
983 this: APIImpl.Resource, content: string, commit: boolean, callback: (error?: Object) => unknown): void {
Blink Reformat4c46d092018-04-07 15:32:37984 extensionServer.sendRequest(
Philip Pfaffe939605d2021-06-25 12:20:04985 {command: PrivateAPI.Commands.SetResourceContent, url: this._url, content: content, commit: commit},
Philip Pfaffe6fd04c42021-06-25 12:31:48986 callback as (response: unknown) => unknown);
Jan Schefflerd76b4162021-03-29 07:52:16987 },
Blink Reformat4c46d092018-04-07 15:32:37988 };
989
Jan Schefflerd76b4162021-03-29 07:52:16990 function getTabId(): string {
Blink Reformat4c46d092018-04-07 15:32:37991 return inspectedTabId;
992 }
993
Philip Pfaffe7523faf2021-06-28 14:23:14994 let keyboardEventRequestQueue: KeyboardEventInit&{eventType: string}[] = [];
Jan Schefflerd76b4162021-03-29 07:52:16995 let forwardTimer: number|null = null;
Philip Pfaffe7523faf2021-06-28 14:23:14996 function forwardKeyboardEvent(event: KeyboardEvent): void {
Jan Schefflere7d7bb12019-10-24 09:18:52997 // 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 Einbinder67f28fb2018-08-02 00:33:471007 let modifiers = 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:341008 if (event.shiftKey) {
Joel Einbinder67f28fb2018-08-02 00:33:471009 modifiers |= 1;
Tim van der Lippe1d6e57a2019-09-30 11:55:341010 }
1011 if (event.ctrlKey) {
Joel Einbinder67f28fb2018-08-02 00:33:471012 modifiers |= 2;
Tim van der Lippe1d6e57a2019-09-30 11:55:341013 }
1014 if (event.altKey) {
Joel Einbinder67f28fb2018-08-02 00:33:471015 modifiers |= 4;
Tim van der Lippe1d6e57a2019-09-30 11:55:341016 }
1017 if (event.metaKey) {
Joel Einbinder67f28fb2018-08-02 00:33:471018 modifiers |= 8;
Tim van der Lippe1d6e57a2019-09-30 11:55:341019 }
Joel Einbinder67f28fb2018-08-02 00:33:471020 const num = (event.keyCode & 255) | (modifiers << 8);
Blink Reformat4c46d092018-04-07 15:32:371021 // We only care about global hotkeys, not about random text
Tim van der Lippe1d6e57a2019-09-30 11:55:341022 if (!keysToForwardSet.has(num)) {
Blink Reformat4c46d092018-04-07 15:32:371023 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:341024 }
Joel Einbinder67f28fb2018-08-02 00:33:471025 event.preventDefault();
Blink Reformat4c46d092018-04-07 15:32:371026 const requestPayload = {
1027 eventType: event.type,
1028 ctrlKey: event.ctrlKey,
1029 altKey: event.altKey,
1030 metaKey: event.metaKey,
Joel Einbinder67f28fb2018-08-02 00:33:471031 shiftKey: event.shiftKey,
Philip Pfaffe7523faf2021-06-28 14:23:141032 // @ts-expect-error keyIdentifier is a deprecated non-standard property that typescript doesn't know about.
Blink Reformat4c46d092018-04-07 15:32:371033 keyIdentifier: event.keyIdentifier,
1034 key: event.key,
1035 code: event.code,
1036 location: event.location,
Jan Schefflerd76b4162021-03-29 07:52:161037 keyCode: event.keyCode,
Blink Reformat4c46d092018-04-07 15:32:371038 };
1039 keyboardEventRequestQueue.push(requestPayload);
Tim van der Lippe1d6e57a2019-09-30 11:55:341040 if (!forwardTimer) {
Blink Reformat4c46d092018-04-07 15:32:371041 forwardTimer = setTimeout(forwardEventQueue, 0);
Tim van der Lippe1d6e57a2019-09-30 11:55:341042 }
Blink Reformat4c46d092018-04-07 15:32:371043 }
1044
Jan Schefflerd76b4162021-03-29 07:52:161045 function forwardEventQueue(): void {
Blink Reformat4c46d092018-04-07 15:32:371046 forwardTimer = null;
Philip Pfaffe7523faf2021-06-28 14:23:141047 extensionServer.sendRequest(
1048 {command: PrivateAPI.Commands.ForwardKeyboardEvent, entries: keyboardEventRequestQueue});
Blink Reformat4c46d092018-04-07 15:32:371049 keyboardEventRequestQueue = [];
1050 }
1051
1052 document.addEventListener('keydown', forwardKeyboardEvent, false);
Blink Reformat4c46d092018-04-07 15:32:371053
1054 /**
1055 * @constructor
1056 */
Philip Pfaffe7523faf2021-06-28 14:23:141057 function ExtensionServerClient(this: APIImpl.ExtensionServerClient): void {
Blink Reformat4c46d092018-04-07 15:32:371058 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 Pfaffe7523faf2021-06-28 14:23:141073 (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 Reformat4c46d092018-04-07 15:32:371086
Philip Pfaffe7523faf2021-06-28 14:23:141087 hasHandler: function(this: APIImpl.ExtensionServerClient, command: string): boolean {
Tim van der Lipped7cfd142021-01-07 12:17:241088 return Boolean(this._handlers[command]);
Blink Reformat4c46d092018-04-07 15:32:371089 },
1090
Philip Pfaffe7523faf2021-06-28 14:23:141091 registerHandler: function(
1092 this: APIImpl.ExtensionServerClient, command: string, handler: (request: {arguments: unknown[]}) => unknown):
1093 void {
1094 this._handlers[command] = handler;
1095 },
Blink Reformat4c46d092018-04-07 15:32:371096
Philip Pfaffe7523faf2021-06-28 14:23:141097 unregisterHandler: function(this: APIImpl.ExtensionServerClient, command: string): void {
Blink Reformat4c46d092018-04-07 15:32:371098 delete this._handlers[command];
1099 },
1100
Philip Pfaffe7523faf2021-06-28 14:23:141101 nextObjectId: function(this: APIImpl.ExtensionServerClient): string {
Blink Reformat4c46d092018-04-07 15:32:371102 return injectedScriptId.toString() + '_' + ++this._lastObjectId;
1103 },
1104
Philip Pfaffe7523faf2021-06-28 14:23:141105 _registerCallback: function(this: APIImpl.ExtensionServerClient, callback: (response: unknown) => unknown): number {
Blink Reformat4c46d092018-04-07 15:32:371106 const id = ++this._lastRequestId;
1107 this._callbacks[id] = callback;
1108 return id;
1109 },
1110
Philip Pfaffe7523faf2021-06-28 14:23:141111 _onCallback: function(this: APIImpl.ExtensionServerClient, request: {requestId: number, result: unknown}): void {
Blink Reformat4c46d092018-04-07 15:32:371112 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 Pfaffe7523faf2021-06-28 14:23:141119 _onMessage: function(
1120 this: APIImpl.ExtensionServerClient,
1121 event: MessageEvent<{command: string, requestId: number, arguments: unknown[]}>): void {
Blink Reformat4c46d092018-04-07 15:32:371122 const request = event.data;
1123 const handler = this._handlers[request.command];
Tim van der Lippe1d6e57a2019-09-30 11:55:341124 if (handler) {
Blink Reformat4c46d092018-04-07 15:32:371125 handler.call(this, request);
Tim van der Lippe1d6e57a2019-09-30 11:55:341126 }
Jan Schefflerd76b4162021-03-29 07:52:161127 },
Blink Reformat4c46d092018-04-07 15:32:371128 };
1129
Philip Pfaffe7523faf2021-06-28 14:23:141130 function populateInterfaceClass(interfaze: {[key: string]: unknown}, implementation: {[key: string]: unknown}): void {
Blink Reformat4c46d092018-04-07 15:32:371131 for (const member in implementation) {
Tim van der Lippe1d6e57a2019-09-30 11:55:341132 if (member.charAt(0) === '_') {
Blink Reformat4c46d092018-04-07 15:32:371133 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341134 }
Jan Schefflerd76b4162021-03-29 07:52:161135 let descriptor: (PropertyDescriptor|undefined)|null = null;
Blink Reformat4c46d092018-04-07 15:32:371136 // Traverse prototype chain until we find the owner.
Philip Pfaffe7523faf2021-06-28 14:23:141137 for (let owner = implementation; owner && !descriptor; owner = owner.__proto__ as {[key: string]: unknown}) {
Blink Reformat4c46d092018-04-07 15:32:371138 descriptor = Object.getOwnPropertyDescriptor(owner, member);
Tim van der Lippe1d6e57a2019-09-30 11:55:341139 }
1140 if (!descriptor) {
Blink Reformat4c46d092018-04-07 15:32:371141 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:341142 }
1143 if (typeof descriptor.value === 'function') {
Blink Reformat4c46d092018-04-07 15:32:371144 interfaze[member] = descriptor.value.bind(implementation);
Tim van der Lippe1d6e57a2019-09-30 11:55:341145 } else if (typeof descriptor.get === 'function') {
Philip Pfaffe7523faf2021-06-28 14:23:141146 // @ts-expect-error
Blink Reformat4c46d092018-04-07 15:32:371147 interfaze.__defineGetter__(member, descriptor.get.bind(implementation));
Tim van der Lippe1d6e57a2019-09-30 11:55:341148 } else {
Blink Reformat4c46d092018-04-07 15:32:371149 Object.defineProperty(interfaze, member, descriptor);
Tim van der Lippe1d6e57a2019-09-30 11:55:341150 }
Blink Reformat4c46d092018-04-07 15:32:371151 }
1152 }
1153
Philip Pfaffe7523faf2021-06-28 14:23:141154
1155 const extensionServer = new (Constructor(ExtensionServerClient))();
1156
1157 const coreAPI = new (Constructor(InspectorExtensionAPI))();
Blink Reformat4c46d092018-04-07 15:32:371158
1159 Object.defineProperty(chrome, 'devtools', {value: {}, enumerable: true});
1160
1161 // Only expose tabId on chrome.devtools.inspectedWindow, not webInspector.inspectedWindow.
Philip Pfaffe7523faf2021-06-28 14:23:141162 // @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 Reformat4c46d092018-04-07 15:32:371171
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 Lippe1d6e57a2019-09-30 11:55:341180 if (descriptor) {
Blink Reformat4c46d092018-04-07 15:32:371181 Object.defineProperty(chrome.experimental.devtools, properties[i], descriptor);
Tim van der Lippe1d6e57a2019-09-30 11:55:341182 }
Blink Reformat4c46d092018-04-07 15:32:371183 }
1184 chrome.experimental.devtools.inspectedWindow = chrome.devtools.inspectedWindow;
1185 }
1186
Tim van der Lippe1d6e57a2019-09-30 11:55:341187 if (extensionInfo.exposeWebInspectorNamespace) {
Blink Reformat4c46d092018-04-07 15:32:371188 window.webInspector = coreAPI;
Tim van der Lippe1d6e57a2019-09-30 11:55:341189 }
Blink Reformat4c46d092018-04-07 15:32:371190 testHook(extensionServer, coreAPI);
Tim van der Lippe226fc222019-10-10 12:17:121191};
Blink Reformat4c46d092018-04-07 15:32:371192
Jan Schefflerd76b4162021-03-29 07:52:161193self.buildExtensionAPIInjectedScript = function(
1194 extensionInfo: {
1195 startPage: string,
1196 name: string,
1197 exposeExperimentalAPIs: boolean,
1198 },
1199 inspectedTabId: string, themeName: string, keysToForward: number[],
Philip Pfaffe7523faf2021-06-28 14:23:141200 testHook:
1201 ((extensionServer: APIImpl.ExtensionServerClient, extensionAPI: APIImpl.InspectorExtensionAPI) => unknown)|
1202 undefined): string {
Philip Pfaffeedad8322020-07-20 10:24:251203 const argumentsJSON =
1204 [extensionInfo, inspectedTabId || null, themeName, keysToForward].map(_ => JSON.stringify(_)).join(',');
Tim van der Lippe1d6e57a2019-09-30 11:55:341205 if (!testHook) {
Jan Schefflerd76b4162021-03-29 07:52:161206 testHook = (): void => {};
Tim van der Lippe1d6e57a2019-09-30 11:55:341207 }
Philip Pfaffe939605d2021-06-25 12:20:041208 return '(function(injectedScriptId){ ' +
Tim van der Lippe226fc222019-10-10 12:17:121209 '(' + self.injectedExtensionAPI.toString() + ')(' + argumentsJSON + ',' + testHook + ', injectedScriptId);' +
Blink Reformat4c46d092018-04-07 15:32:371210 '})';
Tim van der Lippe29fab472019-08-15 14:46:481211};